pc.cpp 473 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pc.hpp"
  4. #include <cmath>
  5. #include <cstdlib>
  6. #include <map>
  7. #ifdef MAP_GENERATOR
  8. #include <fstream>
  9. #include <iostream>
  10. #include <nlohmann/json.hpp>
  11. #endif
  12. #include <common/cbasetypes.hpp>
  13. #include <common/core.hpp> // get_svn_revision()
  14. #include <common/database.hpp>
  15. #include <common/ers.hpp> // ers_destroy
  16. #include <common/grfio.hpp>
  17. #include <common/malloc.hpp>
  18. #include <common/mmo.hpp> //NAME_LENGTH
  19. #include <common/nullpo.hpp>
  20. #include <common/random.hpp>
  21. #include <common/showmsg.hpp>
  22. #include <common/socket.hpp> // session[]
  23. #include <common/strlib.hpp> // safestrncpy()
  24. #include <common/timer.hpp>
  25. #include <common/utilities.hpp>
  26. #include <common/utils.hpp>
  27. #include "achievement.hpp"
  28. #include "atcommand.hpp" // get_atcommand_level()
  29. #include "battle.hpp" // battle_config
  30. #include "battleground.hpp"
  31. #include "buyingstore.hpp" // struct s_buyingstore
  32. #include "channel.hpp"
  33. #include "chat.hpp"
  34. #include "chrif.hpp"
  35. #include "clan.hpp"
  36. #include "clif.hpp"
  37. #include "date.hpp" // is_day_of_*()
  38. #include "duel.hpp"
  39. #include "elemental.hpp"
  40. #include "guild.hpp"
  41. #include "homunculus.hpp"
  42. #include "instance.hpp"
  43. #include "intif.hpp"
  44. #include "itemdb.hpp" // MAX_ITEMGROUP
  45. #include "log.hpp"
  46. #include "map.hpp"
  47. #include "mercenary.hpp"
  48. #include "mob.hpp"
  49. #include "npc.hpp"
  50. #include "party.hpp" // party_search()
  51. #include "pc_groups.hpp"
  52. #include "pet.hpp" // pet_unlocktarget()
  53. #include "quest.hpp"
  54. #include "skill.hpp" // skill_isCopyable()
  55. #include "script.hpp" // struct script_reg, struct script_regstr
  56. #include "searchstore.hpp" // struct s_search_store_info
  57. #include "status.hpp" // OPTION_*, struct weapon_atk
  58. #include "storage.hpp"
  59. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  60. #include "vending.hpp" // struct s_vending
  61. using namespace rathena;
  62. JobDatabase job_db;
  63. CaptchaDatabase captcha_db;
  64. const char *macro_allowed_answer_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  65. 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 = nullptr;
  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 = nullptr; // char_id -> struct item_cd
  538. struct item_cd {
  539. t_tick tick[MAX_ITEMDELAYS]; //tick
  540. t_itemid nameid[MAX_ITEMDELAYS]; //item id
  541. };
  542. /**
  543. * Converts a class to its array index for CLASS_COUNT defined arrays.
  544. * Note that it does not do a validity check for speed purposes, where parsing
  545. * player input make sure to use a pcdb_checkid first!
  546. * @param class_ Job ID see enum e_job
  547. * @return Class Index
  548. */
  549. 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. if (sd->group == nullptr)
  568. return 0;
  569. return sd->group->level;
  570. }
  571. /**
  572. * Remove a player from queue after timeout
  573. * @param tid: Timer ID
  574. * @param tick: Timer
  575. * @param id: ID
  576. * @return 0 on success or 1 otherwise
  577. */
  578. static TIMER_FUNC(pc_on_expire_active)
  579. {
  580. map_session_data *sd = (map_session_data *)data;
  581. nullpo_retr(1, sd);
  582. sd->tid_queue_active = INVALID_TIMER;
  583. bg_queue_leave(sd);
  584. clif_bg_queue_entry_init(sd);
  585. return 0;
  586. }
  587. /**
  588. * Function used to set timer externally
  589. * @param sd: Player data
  590. */
  591. void pc_set_bg_queue_timer(map_session_data *sd) {
  592. nullpo_retv(sd);
  593. if (sd->tid_queue_active != INVALID_TIMER) {
  594. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  595. sd->tid_queue_active = INVALID_TIMER;
  596. }
  597. sd->tid_queue_active = add_timer(gettick() + 20000, pc_on_expire_active, 0, (intptr_t)sd);
  598. }
  599. /**
  600. * Function used to delete timer externally
  601. * @param sd: Player data
  602. */
  603. void pc_delete_bg_queue_timer(map_session_data *sd) {
  604. nullpo_retv(sd);
  605. if (sd->tid_queue_active != INVALID_TIMER) {
  606. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  607. sd->tid_queue_active = INVALID_TIMER;
  608. }
  609. }
  610. static TIMER_FUNC(pc_invincible_timer){
  611. map_session_data *sd;
  612. if( (sd=(map_session_data *)map_id2sd(id)) == nullptr || sd->bl.type!=BL_PC )
  613. return 1;
  614. if(sd->invincible_timer != tid){
  615. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  616. return 0;
  617. }
  618. sd->invincible_timer = INVALID_TIMER;
  619. skill_unit_move(&sd->bl,tick,1);
  620. return 0;
  621. }
  622. void pc_setinvincibletimer(map_session_data* sd, int val) {
  623. nullpo_retv(sd);
  624. if (val <= 0) {
  625. if (sd->invincible_timer != INVALID_TIMER) {
  626. delete_timer(sd->invincible_timer, pc_invincible_timer);
  627. sd->invincible_timer = INVALID_TIMER;
  628. }
  629. return;
  630. }
  631. if( sd->invincible_timer != INVALID_TIMER )
  632. delete_timer(sd->invincible_timer,pc_invincible_timer);
  633. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  634. }
  635. void pc_delinvincibletimer(map_session_data* sd)
  636. {
  637. nullpo_retv(sd);
  638. if( sd->invincible_timer != INVALID_TIMER )
  639. {
  640. delete_timer(sd->invincible_timer,pc_invincible_timer);
  641. sd->invincible_timer = INVALID_TIMER;
  642. skill_unit_move(&sd->bl,gettick(),1);
  643. }
  644. }
  645. static TIMER_FUNC(pc_spiritball_timer){
  646. map_session_data *sd;
  647. int i;
  648. if( (sd=(map_session_data *)map_id2sd(id)) == nullptr || sd->bl.type!=BL_PC )
  649. return 1;
  650. if( sd->spiritball <= 0 )
  651. {
  652. 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);
  653. sd->spiritball = 0;
  654. return 0;
  655. }
  656. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  657. if( i == sd->spiritball )
  658. {
  659. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  660. return 0;
  661. }
  662. sd->spiritball--;
  663. if( i != sd->spiritball )
  664. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  665. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  666. clif_spiritball(&sd->bl);
  667. return 0;
  668. }
  669. /**
  670. * Adds a spiritball to player for 'interval' ms
  671. * @param sd
  672. * @param interval
  673. * @param max
  674. */
  675. void pc_addspiritball(map_session_data *sd,int interval,int max)
  676. {
  677. int tid;
  678. uint8 i;
  679. nullpo_retv(sd);
  680. if(max > MAX_SPIRITBALL)
  681. max = MAX_SPIRITBALL;
  682. if(sd->spiritball < 0)
  683. sd->spiritball = 0;
  684. if( sd->spiritball && sd->spiritball >= max )
  685. {
  686. if(sd->spirit_timer[0] != INVALID_TIMER)
  687. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  688. sd->spiritball--;
  689. if( sd->spiritball != 0 )
  690. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  691. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  692. }
  693. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  694. 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);
  695. if( i != sd->spiritball )
  696. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  697. sd->spirit_timer[i] = tid;
  698. sd->spiritball++;
  699. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  700. clif_millenniumshield(&sd->bl,sd->spiritball);
  701. else
  702. clif_spiritball(&sd->bl);
  703. }
  704. /**
  705. * Removes number of spiritball from player
  706. * @param sd
  707. * @param count
  708. * @param type 1 = doesn't give client effect
  709. */
  710. void pc_delspiritball(map_session_data *sd,int count,int type)
  711. {
  712. uint8 i;
  713. nullpo_retv(sd);
  714. if(sd->spiritball <= 0) {
  715. sd->spiritball = 0;
  716. return;
  717. }
  718. if(count == 0)
  719. return;
  720. if(count > sd->spiritball)
  721. count = sd->spiritball;
  722. sd->spiritball -= count;
  723. if(count > MAX_SPIRITBALL)
  724. count = MAX_SPIRITBALL;
  725. for(i=0;i<count;i++) {
  726. if(sd->spirit_timer[i] != INVALID_TIMER) {
  727. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  728. sd->spirit_timer[i] = INVALID_TIMER;
  729. }
  730. }
  731. for(i=count;i<MAX_SPIRITBALL;i++) {
  732. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  733. sd->spirit_timer[i] = INVALID_TIMER;
  734. }
  735. if(!type) {
  736. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  737. clif_millenniumshield(&sd->bl,sd->spiritball);
  738. else
  739. clif_spiritball(&sd->bl);
  740. }
  741. }
  742. /**
  743. * Adds a soulball to player
  744. * @param sd: Player data
  745. * @param max: Max amount of soulballs
  746. */
  747. int pc_addsoulball(map_session_data *sd, int max)
  748. {
  749. nullpo_ret(sd);
  750. status_change *sc = status_get_sc(&sd->bl);
  751. if (sc == nullptr || sc->getSCE(SC_SOULENERGY) == nullptr) {
  752. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, 0, skill_get_time2(SP_SOULCOLLECT, 1));
  753. sd->soulball = 0;
  754. }
  755. max = min(max, MAX_SOUL_BALL);
  756. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  757. if (sd->soulball && sd->soulball >= max)
  758. sd->soulball--;
  759. sd->soulball++;
  760. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, sd->soulball, skill_get_time2(SP_SOULCOLLECT, 1));
  761. clif_soulball(sd);
  762. return 0;
  763. }
  764. /**
  765. * Removes number of soulball from player
  766. * @param sd: Player data
  767. * @param count: Amount to remove
  768. * @param type: true = doesn't give client effect
  769. */
  770. int pc_delsoulball(map_session_data *sd, int count, bool type)
  771. {
  772. nullpo_ret(sd);
  773. if (count <= 0)
  774. return 0;
  775. status_change *sc = status_get_sc(&sd->bl);
  776. if (sd->soulball <= 0 || sc == nullptr || sc->getSCE(SC_SOULENERGY) == nullptr) {
  777. sd->soulball = 0;
  778. }else{
  779. sd->soulball -= cap_value(count, 0, sd->soulball);
  780. if (sd->soulball == 0)
  781. status_change_end(&sd->bl, SC_SOULENERGY);
  782. else
  783. sc->getSCE(SC_SOULENERGY)->val1 = sd->soulball;
  784. }
  785. if (!type)
  786. clif_soulball(sd);
  787. return 0;
  788. }
  789. /**
  790. * Adds servantballs to a player
  791. * @param sd: Player data
  792. * @param amount: Amount to add
  793. */
  794. void pc_addservantball( map_session_data& sd, int count ){
  795. sd.servantball = cap_value( sd.servantball + count, 0, MAX_SERVANTBALL );
  796. clif_servantball( sd );
  797. }
  798. /**
  799. * Removes number of servantballs from player
  800. * @param sd: Player data
  801. * @param count: Amount to remove
  802. */
  803. void pc_delservantball( map_session_data& sd, int count ){
  804. sd.servantball = cap_value( sd.servantball - count, 0, MAX_SERVANTBALL );
  805. clif_servantball( sd );
  806. }
  807. /**
  808. * Adds abyssballs to a player
  809. * @param sd: Player data
  810. * @param amount: Amount to add
  811. */
  812. void pc_addabyssball( map_session_data& sd, int count ){
  813. sd.abyssball = cap_value( sd.abyssball + count, 0, MAX_ABYSSBALL );
  814. clif_abyssball( sd );
  815. }
  816. /**
  817. * Removes number of abyssballs from player
  818. * @param sd: Player data
  819. * @param count: Amount to remove
  820. */
  821. void pc_delabyssball( map_session_data& sd, int count ){
  822. sd.abyssball = cap_value( sd.abyssball - count, 0, MAX_ABYSSBALL );
  823. clif_abyssball( sd );
  824. }
  825. /**
  826. * Increases a player's fame points and displays a notice to him
  827. * @param sd Player
  828. * @param count Fame point
  829. * @return true: on success, false: on error
  830. */
  831. bool pc_addfame(map_session_data &sd, int count)
  832. {
  833. enum e_rank ranktype;
  834. sd.status.fame += count;
  835. if (sd.status.fame > MAX_FAME)
  836. sd.status.fame = MAX_FAME;
  837. switch(sd.class_&MAPID_UPPERMASK){
  838. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  839. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  840. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  841. default:
  842. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd.status.class_), sd.status.class_ );
  843. return false;
  844. }
  845. clif_update_rankingpoint(sd, ranktype, count);
  846. chrif_updatefamelist(sd, ranktype);
  847. return true;
  848. }
  849. /**
  850. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  851. * @param sd
  852. * @param job Job use enum e_mapid
  853. * @return Rank
  854. */
  855. unsigned char pc_famerank(uint32 char_id, int job)
  856. {
  857. uint8 i;
  858. switch(job){
  859. case MAPID_BLACKSMITH: // Blacksmith
  860. for(i = 0; i < MAX_FAME_LIST; i++){
  861. if(smith_fame_list[i].id == char_id)
  862. return i + 1;
  863. }
  864. break;
  865. case MAPID_ALCHEMIST: // Alchemist
  866. for(i = 0; i < MAX_FAME_LIST; i++){
  867. if(chemist_fame_list[i].id == char_id)
  868. return i + 1;
  869. }
  870. break;
  871. case MAPID_TAEKWON: // Taekwon
  872. for(i = 0; i < MAX_FAME_LIST; i++){
  873. if(taekwon_fame_list[i].id == char_id)
  874. return i + 1;
  875. }
  876. break;
  877. }
  878. return 0;
  879. }
  880. /**
  881. * Restart player's HP & SP value
  882. * @param sd
  883. * @param type Restart type: 1 - Normal Resurection
  884. */
  885. void pc_setrestartvalue(map_session_data *sd, char type) {
  886. struct status_data *status, *b_status;
  887. nullpo_retv(sd);
  888. b_status = &sd->base_status;
  889. status = &sd->battle_status;
  890. if (type&1) { //Normal resurrection
  891. status->hp = 1; //Otherwise status_heal may fail if dead.
  892. status_heal(&sd->bl, b_status->hp, 0, 1);
  893. if( status->sp < b_status->sp )
  894. status_set_sp(&sd->bl, b_status->sp, 1);
  895. } else { //Just for saving on the char-server (with values as if respawned)
  896. sd->status.hp = b_status->hp;
  897. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  898. sd->status.ap = (status->ap < b_status->ap)?b_status->ap:status->ap;
  899. }
  900. }
  901. /*==========================================
  902. Rental System
  903. *------------------------------------------*/
  904. /**
  905. * Ends a rental and removes the item/effect
  906. * @param tid: Tick ID
  907. * @param tick: Timer
  908. * @param id: Timer ID
  909. * @param data: Data
  910. * @return false - failure, true - success
  911. */
  912. TIMER_FUNC(pc_inventory_rental_end){
  913. map_session_data *sd = map_id2sd(id);
  914. if( sd == nullptr )
  915. return 0;
  916. if( tid != sd->rental_timer ) {
  917. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  918. return 0;
  919. }
  920. pc_inventory_rentals(sd);
  921. return 1;
  922. }
  923. /**
  924. * Removes the rental timer from the player
  925. * @param sd: Player data
  926. */
  927. void pc_inventory_rental_clear(map_session_data *sd)
  928. {
  929. if( sd->rental_timer != INVALID_TIMER ) {
  930. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  931. sd->rental_timer = INVALID_TIMER;
  932. }
  933. }
  934. /**
  935. * Check for items in the player's inventory that are rental type
  936. * @param sd: Player data
  937. */
  938. void pc_inventory_rentals(map_session_data *sd)
  939. {
  940. int i, c = 0;
  941. unsigned int next_tick = UINT_MAX;
  942. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  943. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  944. continue; // Nothing here
  945. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  946. continue;
  947. if( sd->inventory.u.items_inventory[i].expire_time <= time(nullptr) ) {
  948. if (sd->inventory_data[i]->unequip_script)
  949. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  950. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  951. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  952. } else {
  953. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(nullptr));
  954. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  955. next_tick = umin(expire_tick * 1000U, next_tick);
  956. c++;
  957. }
  958. }
  959. 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
  960. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  961. else
  962. sd->rental_timer = INVALID_TIMER;
  963. }
  964. /**
  965. * Add a rental item to the player and adjusts the rental timer appropriately
  966. * @param sd: Player data
  967. * @param seconds: Rental time
  968. */
  969. void pc_inventory_rental_add(map_session_data *sd, unsigned int seconds)
  970. {
  971. t_tick tick = seconds * 1000;
  972. if( sd == nullptr )
  973. return;
  974. if( sd->rental_timer != INVALID_TIMER ) {
  975. const struct TimerData * td;
  976. td = get_timer(sd->rental_timer);
  977. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  978. pc_inventory_rental_clear(sd);
  979. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  980. }
  981. } else
  982. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  983. }
  984. /**
  985. * Check if the player can sell the current item
  986. * @param sd: map_session_data of the player
  987. * @param item: struct of the checking item
  988. * @param shoptype: NPC's sub type see enum npc_subtype
  989. * @return bool 'true' is sellable, 'false' otherwise
  990. */
  991. bool pc_can_sell_item(map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  992. if (sd == nullptr || item == nullptr)
  993. return false;
  994. if (!pc_can_give_items(sd))
  995. return false;
  996. if (item->equip > 0 || item->amount < 0)
  997. return false;
  998. if (battle_config.hide_fav_sell && item->favorite != 0)
  999. return false; //Cannot sell favs (optional config)
  1000. if (!battle_config.rental_transaction && item->expire_time)
  1001. return false; // Cannot Sell Rental Items
  1002. if( item->equipSwitch ){
  1003. return false;
  1004. }
  1005. if (itemdb_ishatched_egg(item))
  1006. return false;
  1007. switch (shoptype) {
  1008. case NPCTYPE_SHOP:
  1009. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  1010. item->bound != BOUND_GUILD ||
  1011. (sd->guild && sd->status.char_id == sd->guild->guild.member[0].char_id) ||
  1012. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  1013. ))
  1014. return true;
  1015. break;
  1016. case NPCTYPE_ITEMSHOP:
  1017. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  1018. item->bound != BOUND_GUILD ||
  1019. (sd->guild && sd->status.char_id == sd->guild->guild.member[0].char_id) ||
  1020. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  1021. ))
  1022. return true;
  1023. else if (!item->bound) {
  1024. struct item_data *itd = itemdb_search(item->nameid);
  1025. if (itd && itd->flag.trade_restriction.sell && battle_config.allow_bound_sell&ISR_SELLABLE)
  1026. return true;
  1027. }
  1028. break;
  1029. }
  1030. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  1031. return false;
  1032. if (item->bound && !pc_can_give_bounded_items(sd))
  1033. return false; // Don't allow sale of bound items
  1034. return true;
  1035. }
  1036. /**
  1037. * Determines if player can give / drop / trade / vend items
  1038. */
  1039. bool pc_can_give_items(map_session_data *sd)
  1040. {
  1041. return (pc_has_permission(sd, PC_PERM_TRADE) || pc_has_permission(sd, PC_PERM_TRADE_UNCONDITIONAL));
  1042. }
  1043. /**
  1044. * Determines if player can give / drop / trade / vend bounded items
  1045. */
  1046. bool pc_can_give_bounded_items(map_session_data *sd)
  1047. {
  1048. return (pc_has_permission(sd, PC_PERM_TRADE_BOUNDED) || pc_has_permission(sd, PC_PERM_TRADE_UNCONDITIONAL));
  1049. }
  1050. /**
  1051. * Determine if an item in a player's inventory is tradeable based on several merits.
  1052. * Checks for item_trade, bound, and rental restrictions.
  1053. * @param sd: Player data
  1054. * @param index: Item inventory index
  1055. * @return True if the item can be traded or false otherwise
  1056. */
  1057. bool pc_can_trade_item(map_session_data *sd, int index) {
  1058. if (sd && index >= 0) {
  1059. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  1060. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  1061. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  1062. );
  1063. }
  1064. return false;
  1065. }
  1066. /*==========================================
  1067. * Prepares character for saving.
  1068. * @param sd
  1069. *------------------------------------------*/
  1070. void pc_makesavestatus(map_session_data *sd) {
  1071. nullpo_retv(sd);
  1072. if(!battle_config.save_clothcolor)
  1073. sd->status.clothes_color = 0;
  1074. if(!battle_config.save_body_style)
  1075. sd->status.body = 0;
  1076. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  1077. //status change load/saving. [Skotlex]
  1078. #ifdef NEW_CARTS
  1079. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1080. #else
  1081. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1082. #endif
  1083. // Mark last point as not instance related by default
  1084. sd->status.last_point_instanceid = 0;
  1085. if (sd->sc.getSCE(SC_JAILED)) { //When Jailed, do not move last point.
  1086. if(pc_isdead(sd)){
  1087. pc_setrestartvalue(sd, 0);
  1088. } else {
  1089. sd->status.hp = sd->battle_status.hp;
  1090. sd->status.sp = sd->battle_status.sp;
  1091. sd->status.ap = sd->battle_status.ap;
  1092. }
  1093. mapindex_getmapname( mapindex_id2name( sd->mapindex ), sd->status.last_point.map );
  1094. sd->status.last_point.x = sd->bl.x;
  1095. sd->status.last_point.y = sd->bl.y;
  1096. return;
  1097. }
  1098. if( pc_isdead( sd ) ){
  1099. pc_setrestartvalue( sd, 0 );
  1100. // Return to save point
  1101. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1102. sd->status.last_point.x = sd->status.save_point.x;
  1103. sd->status.last_point.y = sd->status.save_point.y;
  1104. }else{
  1105. sd->status.hp = sd->battle_status.hp;
  1106. sd->status.sp = sd->battle_status.sp;
  1107. sd->status.ap = sd->battle_status.ap;
  1108. struct map_data* mapdata = map_getmapdata( sd->bl.m );
  1109. // If saving is not allowed on the map, we return the player to the designated point
  1110. if( mapdata->getMapFlag(MF_NOSAVE) ){
  1111. // The map has a specific return point
  1112. if( mapdata->save.map ){
  1113. safestrncpy( sd->status.last_point.map, mapindex_id2name( mapdata->save.map ), sizeof( sd->status.last_point.map ) );
  1114. sd->status.last_point.x = mapdata->save.x;
  1115. sd->status.last_point.y = mapdata->save.y;
  1116. // Return the user to his save point
  1117. }else{
  1118. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1119. sd->status.last_point.x = sd->status.save_point.x;
  1120. sd->status.last_point.y = sd->status.save_point.y;
  1121. }
  1122. // If the user is on a instance map, special handling is needed
  1123. }else if( mapdata->instance_id ){
  1124. if( battle_config.instance_allow_reconnect ){
  1125. // Store the original mapname
  1126. struct map_data* mapdata_source = map_getmapdata( mapdata->instance_src_map );
  1127. mapindex_getmapname( mapindex_id2name( mapdata_source->index ), sd->status.last_point.map );
  1128. sd->status.last_point.x = sd->bl.x;
  1129. sd->status.last_point.y = sd->bl.y;
  1130. sd->status.last_point_instanceid = mapdata->instance_id;
  1131. }else{
  1132. // Return the user to his save point
  1133. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1134. sd->status.last_point.x = sd->status.save_point.x;
  1135. sd->status.last_point.y = sd->status.save_point.y;
  1136. }
  1137. }else{
  1138. // Save normally
  1139. mapindex_getmapname( mapindex_id2name( sd->mapindex ), sd->status.last_point.map );
  1140. sd->status.last_point.x = sd->bl.x;
  1141. sd->status.last_point.y = sd->bl.y;
  1142. }
  1143. }
  1144. }
  1145. /*==========================================
  1146. * Off init ? Connection?
  1147. *------------------------------------------*/
  1148. void pc_setnewpc(map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, t_tick client_tick, int sex, int fd) {
  1149. nullpo_retv(sd);
  1150. sd->bl.id = account_id;
  1151. sd->status.account_id = account_id;
  1152. sd->status.char_id = char_id;
  1153. sd->status.sex = sex;
  1154. sd->login_id1 = login_id1;
  1155. sd->login_id2 = 0; // at this point, we can not know the value :(
  1156. sd->client_tick = client_tick;
  1157. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  1158. sd->bl.type = BL_PC;
  1159. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  1160. sd->canlog_tick = gettick();
  1161. //Required to prevent homunculus copuing a base speed of 0.
  1162. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  1163. }
  1164. /**
  1165. * Get equip point for an equip
  1166. * @param sd
  1167. * @param id
  1168. */
  1169. int pc_equippoint_sub(map_session_data *sd,struct item_data* id){
  1170. int ep = 0;
  1171. nullpo_ret(sd);
  1172. nullpo_ret(id);
  1173. if (!itemdb_isequip2(id))
  1174. return 0; //Not equippable by players.
  1175. ep = id->equip;
  1176. if(id->subtype == W_DAGGER || id->subtype == W_1HSWORD || id->subtype == W_1HAXE) {
  1177. 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]
  1178. if (ep == EQP_WEAPON)
  1179. return EQP_ARMS;
  1180. if (ep == EQP_SHADOW_WEAPON)
  1181. return EQP_SHADOW_ARMS;
  1182. }
  1183. }
  1184. return ep;
  1185. }
  1186. /**
  1187. * Get equip point for an equip
  1188. * @param sd
  1189. * @param n Equip index in inventory
  1190. */
  1191. int pc_equippoint(map_session_data *sd,int n){
  1192. nullpo_ret(sd);
  1193. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  1194. }
  1195. /**
  1196. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  1197. * @param sd : player session
  1198. * @return 0 sucess, 1:invalid sd
  1199. */
  1200. void pc_setinventorydata(map_session_data *sd)
  1201. {
  1202. uint8 i;
  1203. nullpo_retv(sd);
  1204. for(i = 0; i < MAX_INVENTORY; i++) {
  1205. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  1206. sd->inventory_data[i] = id?itemdb_search(id):nullptr;
  1207. }
  1208. }
  1209. /**
  1210. * 'Calculates' weapon type
  1211. * @param sd : Player
  1212. */
  1213. void pc_calcweapontype(map_session_data *sd)
  1214. {
  1215. nullpo_retv(sd);
  1216. // single-hand
  1217. if(sd->weapontype2 == W_FIST) {
  1218. sd->status.weapon = sd->weapontype1;
  1219. return;
  1220. }
  1221. if(sd->weapontype1 == W_FIST) {
  1222. sd->status.weapon = sd->weapontype2;
  1223. return;
  1224. }
  1225. // dual-wield
  1226. sd->status.weapon = W_FIST;
  1227. switch (sd->weapontype1){
  1228. case W_DAGGER:
  1229. switch (sd->weapontype2) {
  1230. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  1231. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  1232. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  1233. }
  1234. break;
  1235. case W_1HSWORD:
  1236. switch (sd->weapontype2) {
  1237. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  1238. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  1239. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  1240. }
  1241. break;
  1242. case W_1HAXE:
  1243. switch (sd->weapontype2) {
  1244. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  1245. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  1246. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  1247. }
  1248. }
  1249. // unknown, default to right hand type
  1250. if (!sd->status.weapon)
  1251. sd->status.weapon = sd->weapontype1;
  1252. }
  1253. /**
  1254. * Set equip index
  1255. * @param sd : Player
  1256. */
  1257. void pc_setequipindex(map_session_data *sd)
  1258. {
  1259. uint16 i;
  1260. nullpo_retv(sd);
  1261. for (i = 0; i < EQI_MAX; i++){
  1262. sd->equip_index[i] = -1;
  1263. sd->equip_switch_index[i] = -1;
  1264. }
  1265. for (i = 0; i < MAX_INVENTORY; i++) {
  1266. if (sd->inventory.u.items_inventory[i].nameid == 0)
  1267. continue;
  1268. if (sd->inventory.u.items_inventory[i].equip) {
  1269. uint8 j;
  1270. for (j = 0; j < EQI_MAX; j++)
  1271. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  1272. sd->equip_index[j] = i;
  1273. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  1274. if (sd->inventory_data[i])
  1275. sd->weapontype1 = sd->inventory_data[i]->subtype;
  1276. else
  1277. sd->weapontype1 = W_FIST;
  1278. }
  1279. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  1280. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  1281. sd->weapontype2 = sd->inventory_data[i]->subtype;
  1282. else
  1283. sd->weapontype2 = W_FIST;
  1284. }
  1285. }
  1286. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  1287. for (uint8 j = 0; j < EQI_MAX; j++) {
  1288. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  1289. sd->equip_switch_index[j] = i;
  1290. }
  1291. }
  1292. }
  1293. }
  1294. pc_calcweapontype(sd);
  1295. }
  1296. //static int pc_isAllowedCardOn(map_session_data *sd,int s,int eqindex,int flag)
  1297. //{
  1298. // int i;
  1299. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  1300. // std::shared_ptr<item_data> data;
  1301. //
  1302. // //Crafted/made/hatched items.
  1303. // if (itemdb_isspecial(item->card[0]))
  1304. // return 1;
  1305. //
  1306. // /* scan for enchant armor gems */
  1307. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  1308. // s = MAX_SLOTS - 1;
  1309. //
  1310. // ARR_FIND( 0, s, i, item->card[i] && (data = item_db.find(item->card[i])) != nullptr && data->flag.no_equip&flag );
  1311. // return( i < s ) ? 0 : 1;
  1312. //}
  1313. /**
  1314. * Check if an item is equiped by player
  1315. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  1316. * @param sd : player session
  1317. * @param nameid : itemid
  1318. * @return 1:yes, 0:no
  1319. */
  1320. bool pc_isequipped(map_session_data *sd, t_itemid nameid)
  1321. {
  1322. uint8 i;
  1323. for( i = 0; i < EQI_MAX; i++ )
  1324. {
  1325. short index = sd->equip_index[i], j;
  1326. if( index < 0 )
  1327. continue;
  1328. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  1329. continue;
  1330. if( !sd->inventory_data[index] )
  1331. continue;
  1332. if( sd->inventory_data[index]->nameid == nameid )
  1333. return true;
  1334. for( j = 0; j < MAX_SLOTS; j++ ){
  1335. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  1336. return true;
  1337. }
  1338. }
  1339. return false;
  1340. }
  1341. /**
  1342. * Check adoption rules
  1343. * @param p1_sd: Player 1
  1344. * @param p2_sd: Player 2
  1345. * @param b_sd: Player that will be adopted
  1346. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  1347. * ADOPT_ALREADY_ADOPTED - Already adopted
  1348. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  1349. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  1350. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  1351. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  1352. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  1353. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  1354. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  1355. */
  1356. enum adopt_responses pc_try_adopt(map_session_data *p1_sd, map_session_data *p2_sd, map_session_data *b_sd)
  1357. {
  1358. if( !p1_sd || !p2_sd || !b_sd )
  1359. return ADOPT_CHARACTER_NOT_FOUND;
  1360. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1361. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1362. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1363. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1364. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1365. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1366. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1367. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1368. // Parents need to have their ring equipped
  1369. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1370. return ADOPT_EQUIP_RINGS;
  1371. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1372. return ADOPT_EQUIP_RINGS;
  1373. // Already adopted a baby
  1374. if( p1_sd->status.child || p2_sd->status.child ) {
  1375. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1376. return ADOPT_MORE_CHILDREN;
  1377. }
  1378. // Parents need at least lvl 70 to adopt
  1379. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1380. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1381. return ADOPT_LEVEL_70;
  1382. }
  1383. if( b_sd->status.partner_id ) {
  1384. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1385. return ADOPT_MARRIED;
  1386. }
  1387. 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 ) )
  1388. return ADOPT_NOT_NOVICE;
  1389. return ADOPT_ALLOWED;
  1390. }
  1391. /*==========================================
  1392. * Adoption Process
  1393. *------------------------------------------*/
  1394. bool pc_adoption(map_session_data *p1_sd, map_session_data *p2_sd, map_session_data *b_sd)
  1395. {
  1396. int job, joblevel;
  1397. t_exp jobexp;
  1398. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1399. return false;
  1400. // Preserve current job levels and progress
  1401. joblevel = b_sd->status.job_level;
  1402. jobexp = b_sd->status.job_exp;
  1403. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1404. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1405. { // Success, proceed to configure parents and baby skills
  1406. p1_sd->status.child = b_sd->status.char_id;
  1407. p2_sd->status.child = b_sd->status.char_id;
  1408. b_sd->status.father = p1_sd->status.char_id;
  1409. b_sd->status.mother = p2_sd->status.char_id;
  1410. // Restore progress
  1411. b_sd->status.job_level = joblevel;
  1412. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1413. b_sd->status.job_exp = jobexp;
  1414. clif_updatestatus(b_sd, SP_JOBEXP);
  1415. // Baby Skills
  1416. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1417. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1418. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1419. // Parents Skills
  1420. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1421. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1422. chrif_save(p1_sd, CSAVE_NORMAL);
  1423. chrif_save(p2_sd, CSAVE_NORMAL);
  1424. chrif_save(b_sd, CSAVE_NORMAL);
  1425. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1426. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1427. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1428. return true;
  1429. }
  1430. return false; // Job Change Fail
  1431. }
  1432. static bool pc_job_can_use_item( map_session_data* sd, struct item_data* item ){
  1433. nullpo_retr( false, sd );
  1434. nullpo_retr( false, item );
  1435. // Calculate the required bit to check
  1436. uint64 job = 1ULL << ( sd->class_ & MAPID_BASEMASK );
  1437. size_t index;
  1438. // 2-1
  1439. if( ( sd->class_ & JOBL_2_1 ) != 0 ){
  1440. index = 1;
  1441. // 2-2
  1442. }else if( ( sd->class_ & JOBL_2_2 ) != 0 ){
  1443. index = 2;
  1444. // Basejob
  1445. }else{
  1446. index = 0;
  1447. }
  1448. return ( item->class_base[index] & job ) != 0;
  1449. }
  1450. /*==========================================
  1451. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1452. Returns:
  1453. false : Cannot use/equip
  1454. true : Can use/equip
  1455. * Credits:
  1456. [Inkfish] for first idea
  1457. [Haru] for third-classes extension
  1458. [Cydh] finishing :D
  1459. *------------------------------------------*/
  1460. static bool pc_isItemClass (map_session_data *sd, struct item_data* item) {
  1461. while (1) {
  1462. 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)
  1463. break;
  1464. #ifndef RENEWAL
  1465. //allow third classes to use trans. class items
  1466. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1467. break;
  1468. //third-baby classes can use same item too
  1469. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1470. break;
  1471. //don't need to decide specific rules for third-classes?
  1472. //items for third classes can be used for all third classes
  1473. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_UPPER|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1474. break;
  1475. #else
  1476. //trans. classes (exl. third-trans.)
  1477. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1478. break;
  1479. //baby classes (exl. third-baby)
  1480. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1481. break;
  1482. //third classes (exl. third-trans. and baby-third and fourth)
  1483. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)) && !(sd->class_&JOBL_FOURTH))
  1484. break;
  1485. //trans-third classes (exl. fourth)
  1486. if (item->class_upper&ITEMJ_THIRD_UPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_FOURTH))
  1487. break;
  1488. //third-baby classes (exl. fourth)
  1489. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY && !(sd->class_&JOBL_FOURTH))
  1490. break;
  1491. //fourth classes
  1492. if (item->class_upper&ITEMJ_FOURTH && sd->class_&JOBL_FOURTH)
  1493. break;
  1494. #endif
  1495. return false;
  1496. }
  1497. return true;
  1498. }
  1499. /*=================================================
  1500. * Checks if the player can equip the item at index n in inventory.
  1501. * @param sd
  1502. * @param n Item index in inventory
  1503. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1504. *------------------------------------------------*/
  1505. uint8 pc_isequip(map_session_data *sd,int n)
  1506. {
  1507. struct item_data *item;
  1508. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1509. item = sd->inventory_data[n];
  1510. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1511. return ITEM_EQUIP_ACK_OK;
  1512. if(item == nullptr)
  1513. return ITEM_EQUIP_ACK_FAIL;
  1514. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1515. return ITEM_EQUIP_ACK_FAILLEVEL;
  1516. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1517. return ITEM_EQUIP_ACK_FAILLEVEL;
  1518. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1519. return ITEM_EQUIP_ACK_FAIL;
  1520. //fail to equip if item is restricted
  1521. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(*sd, item->nameid))
  1522. return ITEM_EQUIP_ACK_FAIL;
  1523. if (item->equip&EQP_AMMO) {
  1524. switch (item->subtype) {
  1525. case AMMO_ARROW:
  1526. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1527. clif_msg(sd, ITEM_NEED_BOW);
  1528. return ITEM_EQUIP_ACK_FAIL;
  1529. }
  1530. break;
  1531. case AMMO_DAGGER:
  1532. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1533. return ITEM_EQUIP_ACK_FAIL;
  1534. break;
  1535. case AMMO_BULLET:
  1536. case AMMO_SHELL:
  1537. 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
  1538. #ifdef RENEWAL
  1539. && sd->status.weapon != W_GRENADE
  1540. #endif
  1541. ) {
  1542. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1543. return ITEM_EQUIP_ACK_FAIL;
  1544. }
  1545. break;
  1546. #ifndef RENEWAL
  1547. case AMMO_GRENADE:
  1548. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1549. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1550. return ITEM_EQUIP_ACK_FAIL;
  1551. }
  1552. break;
  1553. #endif
  1554. case AMMO_CANNONBALL:
  1555. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1556. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1557. return ITEM_EQUIP_ACK_FAIL;
  1558. }
  1559. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1560. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1561. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1562. return ITEM_EQUIP_ACK_FAIL;
  1563. }
  1564. break;
  1565. }
  1566. }
  1567. if (sd->sc.count) {
  1568. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.getSCE(SC_STRIPWEAPON)) // Also works with left-hand weapons [DracoRPG]
  1569. return ITEM_EQUIP_ACK_FAIL;
  1570. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.getSCE(SC_STRIPSHIELD))
  1571. return ITEM_EQUIP_ACK_FAIL;
  1572. if(item->equip & EQP_ARMOR && sd->sc.getSCE(SC_STRIPARMOR))
  1573. return ITEM_EQUIP_ACK_FAIL;
  1574. if(item->equip & EQP_HEAD_TOP && sd->sc.getSCE(SC_STRIPHELM))
  1575. return ITEM_EQUIP_ACK_FAIL;
  1576. if(item->equip & EQP_ACC && sd->sc.getSCE(SC__STRIPACCESSORY))
  1577. return ITEM_EQUIP_ACK_FAIL;
  1578. if (item->equip & EQP_ARMS && sd->sc.getSCE(SC__WEAKNESS))
  1579. return ITEM_EQUIP_ACK_FAIL;
  1580. if (item->equip & EQP_SHADOW_GEAR && sd->sc.getSCE(SC_SHADOW_STRIP))
  1581. return ITEM_EQUIP_ACK_FAIL;
  1582. if(item->equip && (sd->sc.getSCE(SC_KYOUGAKU) || sd->sc.getSCE(SC_SUHIDE)))
  1583. return ITEM_EQUIP_ACK_FAIL;
  1584. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_SUPERNOVICE) {
  1585. //Spirit of Super Novice equip bonuses. [Skotlex]
  1586. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1587. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1588. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->weapon_level == 4)
  1589. switch(item->subtype) { //In weapons, the look determines type of weapon.
  1590. case W_DAGGER: //All level 4 - Daggers
  1591. case W_1HSWORD: //All level 4 - 1H Swords
  1592. case W_1HAXE: //All level 4 - 1H Axes
  1593. case W_MACE: //All level 4 - 1H Maces
  1594. case W_STAFF: //All level 4 - 1H Staves
  1595. case W_2HSTAFF: //All level 4 - 2H Staves
  1596. return ITEM_EQUIP_ACK_OK;
  1597. }
  1598. }
  1599. }
  1600. //Not equipable by class. [Skotlex]
  1601. if (!pc_job_can_use_item(sd,item))
  1602. return ITEM_EQUIP_ACK_FAIL;
  1603. if (!pc_isItemClass(sd, item))
  1604. return ITEM_EQUIP_ACK_FAIL;
  1605. return ITEM_EQUIP_ACK_OK;
  1606. }
  1607. /**
  1608. * Performs some special modifications to a player's last point location,
  1609. * if the map has a nosave mapflag or if the map was an instance.
  1610. * @param sd: Player data
  1611. * @return True if the player should be returned to his savepoint or false if not
  1612. */
  1613. bool pc_lastpoint_special( map_session_data& sd ){
  1614. int16 mapid = map_mapname2mapid( sd.status.last_point.map );
  1615. // Should not happen because otherwise the char-server would have sent the player to another map-server
  1616. if( mapid < 0 ){
  1617. // Return the player to his savepoint
  1618. return true;
  1619. }
  1620. struct map_data* mapdata = map_getmapdata( mapid );
  1621. if( mapdata == nullptr ){
  1622. // Return the player to his savepoint
  1623. return true;
  1624. }
  1625. if (strcmpi(sd.status.last_point.map, MAP_JAIL) == 0) {
  1626. // Don't return jailed player to save point.
  1627. return false;
  1628. }
  1629. // Maybe since the player's logout the nosave mapflag was added to the map
  1630. if( mapdata->getMapFlag(MF_NOSAVE) ){
  1631. // The map has a specific return point
  1632. if( mapdata->save.map ){
  1633. safestrncpy( sd.status.last_point.map, mapindex_id2name( mapdata->save.map ), sizeof( sd.status.last_point.map ) );
  1634. sd.status.last_point.x = mapdata->save.x;
  1635. sd.status.last_point.y = mapdata->save.y;
  1636. return false;
  1637. }else{
  1638. // Return the user to his save point
  1639. return true;
  1640. }
  1641. }
  1642. // Check if the last point was an instance
  1643. if( sd.status.last_point_instanceid == 0 ){
  1644. // Nothing to do
  1645. return false;
  1646. }
  1647. // Check if returning to the instance is allowed in general
  1648. if( !battle_config.instance_allow_reconnect ){
  1649. // Return the player to his savepoint
  1650. return true;
  1651. }
  1652. std::shared_ptr<s_instance_data> instance_data = util::umap_find( instances, sd.status.last_point_instanceid );
  1653. if( instance_data == nullptr ){
  1654. // Instance does not exist anymore, return the player to his savepoint
  1655. return true;
  1656. }
  1657. switch( instance_data->mode ){
  1658. case IM_NONE:
  1659. // Cannot validate
  1660. break;
  1661. case IM_CHAR:
  1662. if( sd.status.char_id != instance_data->owner_id ){
  1663. // It is a character bound instance and the player is not the owner, return the player to his savepoint
  1664. return true;
  1665. }
  1666. break;
  1667. case IM_PARTY:
  1668. if( sd.status.party_id != instance_data->owner_id ){
  1669. // It is a party bound instance and the player is not in the party, return the player to his savepoint
  1670. return true;
  1671. }
  1672. break;
  1673. case IM_GUILD:
  1674. if( sd.status.guild_id != instance_data->owner_id ){
  1675. // It is a guild bound instance and the player is not in the guild, return the player to his savepoint
  1676. return true;
  1677. }
  1678. break;
  1679. case IM_CLAN:
  1680. if( sd.status.clan_id != instance_data->owner_id ){
  1681. // It is a clan bound instance and the player is not in the clan, return the player to his savepoint
  1682. return true;
  1683. }
  1684. break;
  1685. }
  1686. int16 imapid = instance_mapid( mapid, sd.status.last_point_instanceid );
  1687. if( imapid < 0 ){
  1688. // Instance does not contain this map anymore, return the player to his savepoint
  1689. return true;
  1690. }
  1691. // Overwrite the stored "source mapname" with the real mapname
  1692. instance_generate_mapname( mapid, sd.status.last_point_instanceid, sd.status.last_point.map );
  1693. // X/Y coordinates are already correct and do not need to be modified
  1694. // Dont warp the player back to his savepoint
  1695. return false;
  1696. }
  1697. /*==========================================
  1698. * No problem with the session id
  1699. * set the status that has been sent from char server
  1700. *------------------------------------------*/
  1701. bool pc_authok(map_session_data *sd, uint32 login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
  1702. {
  1703. t_tick tick = gettick();
  1704. uint32 ip = session[sd->fd]->client_addr;
  1705. sd->login_id2 = login_id2;
  1706. sd->group_id = group_id;
  1707. /* load user permissions */
  1708. pc_group_pc_load(sd);
  1709. memcpy(&sd->status, st, sizeof(*st));
  1710. if (st->sex != sd->status.sex) {
  1711. clif_authfail_fd(sd->fd, 0);
  1712. return false;
  1713. }
  1714. //Set the map-server used job id. [Skotlex]
  1715. uint64 class_ = pc_jobid2mapid(sd->status.class_);
  1716. if (class_ == -1 || !job_db.exists(sd->status.class_)) { //Invalid class?
  1717. 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);
  1718. sd->status.class_ = JOB_NOVICE;
  1719. sd->class_ = MAPID_NOVICE;
  1720. } else
  1721. sd->class_ = class_;
  1722. // Checks and fixes to character status data, that are required
  1723. // in case of configuration change or stuff, which cannot be
  1724. // checked on char-server.
  1725. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1726. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1727. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1728. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1729. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1730. sd->state.connect_new = 1;
  1731. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1732. sd->invincible_timer = INVALID_TIMER;
  1733. sd->npc_timer_id = INVALID_TIMER;
  1734. sd->pvp_timer = INVALID_TIMER;
  1735. sd->expiration_tid = INVALID_TIMER;
  1736. sd->autotrade_tid = INVALID_TIMER;
  1737. sd->respawn_tid = INVALID_TIMER;
  1738. sd->tid_queue_active = INVALID_TIMER;
  1739. sd->macro_detect.timer = INVALID_TIMER;
  1740. sd->skill_keep_using.tid = INVALID_TIMER;
  1741. sd->skill_keep_using.skill_id = 0;
  1742. sd->skill_keep_using.level = 0;
  1743. sd->skill_keep_using.target = 0;
  1744. #ifdef SECURE_NPCTIMEOUT
  1745. // Initialize to defaults/expected
  1746. sd->npc_idle_timer = INVALID_TIMER;
  1747. sd->npc_idle_tick = tick;
  1748. sd->npc_idle_type = NPCT_INPUT;
  1749. sd->state.ignoretimeout = false;
  1750. #endif
  1751. sd->canuseitem_tick = tick;
  1752. sd->canusecashfood_tick = tick;
  1753. sd->canequip_tick = tick;
  1754. sd->cantalk_tick = tick;
  1755. sd->canskill_tick = tick;
  1756. sd->cansendmail_tick = tick;
  1757. sd->idletime = last_tick;
  1758. sd->regen.tick.hp = tick;
  1759. sd->regen.tick.sp = tick;
  1760. for(int i = 0; i < MAX_SPIRITBALL; i++)
  1761. sd->spirit_timer[i] = INVALID_TIMER;
  1762. if (battle_config.item_auto_get)
  1763. sd->state.autoloot = 10000;
  1764. if (battle_config.disp_experience)
  1765. sd->state.showexp = 1;
  1766. if (battle_config.disp_zeny)
  1767. sd->state.showzeny = 1;
  1768. #ifdef VIP_ENABLE
  1769. if (!battle_config.vip_disp_rate)
  1770. sd->vip.disableshowrate = 1;
  1771. #endif
  1772. if (!(battle_config.display_skill_fail&2))
  1773. sd->state.showdelay = 1;
  1774. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1775. memset(&sd->cart, 0, sizeof(struct s_storage));
  1776. memset(&sd->storage, 0, sizeof(struct s_storage));
  1777. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1778. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1779. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1780. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1781. sd->status.option &= ~OPTION_INVISIBLE;
  1782. }
  1783. status_change_init(&sd->bl);
  1784. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1785. unit_dataset(&sd->bl);
  1786. sd->guild_x = -1;
  1787. sd->guild_y = -1;
  1788. sd->delayed_damage = 0;
  1789. // Event Timers
  1790. for( int i = 0; i < MAX_EVENTTIMER; i++ )
  1791. sd->eventtimer[i] = INVALID_TIMER;
  1792. // Rental Timer
  1793. sd->rental_timer = INVALID_TIMER;
  1794. for( int i = 0; i < 3; i++ )
  1795. sd->hate_mob[i] = -1;
  1796. sd->quest_log = nullptr;
  1797. sd->num_quests = 0;
  1798. sd->avail_quests = 0;
  1799. sd->save_quest = false;
  1800. sd->count_rewarp = 0;
  1801. sd->mail.pending_weight = 0;
  1802. sd->mail.pending_zeny = 0;
  1803. sd->mail.pending_slots = 0;
  1804. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1805. sd->regs.arrays = nullptr;
  1806. sd->vars_dirty = false;
  1807. sd->vars_ok = false;
  1808. sd->vars_received = 0x0;
  1809. // Check if the player's last point requires special handling and if conditions apply to return the player to his savepoint
  1810. if( pc_lastpoint_special( *sd ) ){
  1811. // The player should be warped back to his savepoint
  1812. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1813. sd->status.last_point.x = sd->status.save_point.x;
  1814. sd->status.last_point.y = sd->status.save_point.y;
  1815. }
  1816. //warp player
  1817. 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 );
  1818. if( setpos_result != SETPOS_OK ){
  1819. ShowError( "Last_point_map %s not found (error code %d)\n", sd->status.last_point.map, setpos_result );
  1820. // try warping to a default map instead (church graveyard)
  1821. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1822. // if we fail again
  1823. clif_authfail_fd(sd->fd, 0);
  1824. return false;
  1825. }
  1826. }
  1827. clif_inventory_expansion_info( sd );
  1828. clif_authok(sd);
  1829. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1830. sd->die_counter=-1;
  1831. //display login notice
  1832. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1833. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1834. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1835. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1836. sd->status.name, sd->status.account_id, sd->status.char_id,
  1837. CONVIP(ip), sd->group_id);
  1838. // Send friends list
  1839. clif_friendslist_send( *sd );
  1840. if( !changing_mapservers ) {
  1841. if (battle_config.display_version == 1)
  1842. pc_show_version(sd);
  1843. // Message of the Day [Valaris]
  1844. for(int i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1845. if (battle_config.motd_type)
  1846. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1847. else
  1848. clif_displaymessage(sd->fd, motd_text[i]);
  1849. }
  1850. if (expiration_time != 0)
  1851. sd->expiration_time = expiration_time;
  1852. /**
  1853. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1854. **/
  1855. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1856. }
  1857. pc_validate_skill(sd);
  1858. /* [Ind] */
  1859. sd->sc_display = nullptr;
  1860. sd->sc_display_count = 0;
  1861. // Player has not yet received the CashShop list
  1862. sd->status.cashshop_sent = false;
  1863. sd->last_addeditem_index = -1;
  1864. sd->bonus_script.head = nullptr;
  1865. sd->bonus_script.count = 0;
  1866. // Initialize BG queue
  1867. sd->bg_queue_id = 0;
  1868. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  1869. sd->hatEffects = {};
  1870. #endif
  1871. sd->catch_target_class = PET_CATCH_FAIL;
  1872. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1873. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1874. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1875. clif_updatestatus(sd, SP_BASEEXP);
  1876. }
  1877. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1878. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1879. clif_updatestatus(sd, SP_JOBEXP);
  1880. }
  1881. // Request all registries (auth is considered completed whence they arrive)
  1882. intif_request_registry(sd,7);
  1883. return true;
  1884. }
  1885. /*==========================================
  1886. * Closes a connection because it failed to be authenticated from the char server.
  1887. *------------------------------------------*/
  1888. void pc_authfail(map_session_data *sd)
  1889. {
  1890. clif_authfail_fd(sd->fd, 0);
  1891. return;
  1892. }
  1893. /**
  1894. * Player register a bl as hatred
  1895. * @param sd : player session
  1896. * @param pos : hate position [0;2]
  1897. * @param bl : target bl
  1898. * @return false:failed, true:success
  1899. */
  1900. bool pc_set_hate_mob(map_session_data *sd, int pos, struct block_list *bl)
  1901. {
  1902. int class_;
  1903. if (!sd || !bl || pos < 0 || pos > 2)
  1904. return false;
  1905. if (sd->hate_mob[pos] != -1)
  1906. { //Can't change hate targets.
  1907. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1908. return false;
  1909. }
  1910. class_ = status_get_class(bl);
  1911. if (!pcdb_checkid(class_)) {
  1912. unsigned int max_hp = status_get_max_hp(bl);
  1913. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1914. return false;
  1915. if (pos != status_get_size(bl))
  1916. return false; //Wrong size
  1917. }
  1918. sd->hate_mob[pos] = class_;
  1919. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1920. clif_hate_info(sd, pos, class_, 1);
  1921. return true;
  1922. }
  1923. /*==========================================
  1924. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1925. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1926. * See intif_parse_StorageReceived() for item operations [lighta]
  1927. *------------------------------------------*/
  1928. void pc_reg_received(map_session_data *sd)
  1929. {
  1930. uint8 i;
  1931. sd->vars_ok = true;
  1932. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1933. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1934. sd->change_level_4th = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE4TH_VAR)));
  1935. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1936. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1937. if (msg_checklangtype(sd->langtype,true) < 0)
  1938. sd->langtype = 0; //invalid langtype reset to default
  1939. // Cash shop
  1940. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1941. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1942. // Cooking Exp
  1943. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1944. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1945. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1946. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1947. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1948. }
  1949. if (battle_config.feature_banking)
  1950. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1951. if (battle_config.feature_roulette) {
  1952. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1953. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1954. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1955. }
  1956. sd->roulette.prizeIdx = -1;
  1957. //SG map and mob read [Komurka]
  1958. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1959. uint16 j;
  1960. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1961. sd->feel_map[i].index = j;
  1962. sd->feel_map[i].m = map_mapindex2mapid(j);
  1963. } else {
  1964. sd->feel_map[i].index = 0;
  1965. sd->feel_map[i].m = -1;
  1966. }
  1967. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1968. }
  1969. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1970. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1971. sd->cloneskill_idx = skill_get_index(skid);
  1972. if (sd->cloneskill_idx > 0) {
  1973. sd->status.skill[sd->cloneskill_idx].id = skid;
  1974. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1975. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1976. sd->status.skill[sd->cloneskill_idx].lv = i;
  1977. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1978. }
  1979. }
  1980. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1981. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1982. sd->reproduceskill_idx = skill_get_index(skid);
  1983. if (sd->reproduceskill_idx > 0) {
  1984. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1985. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1986. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1987. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1988. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1989. }
  1990. }
  1991. //Weird... maybe registries were reloaded?
  1992. if (sd->state.active)
  1993. return;
  1994. sd->state.active = 1;
  1995. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1996. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1997. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1998. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1999. // Restore IM_CHAR instance to the player
  2000. for (const auto &instance : instances) {
  2001. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  2002. sd->instance_id = instance.first;
  2003. break;
  2004. }
  2005. }
  2006. if (sd->status.party_id > 0)
  2007. party_member_joined(sd);
  2008. if (sd->status.guild_id > 0)
  2009. guild_member_joined(sd);
  2010. if (sd->status.clan_id > 0)
  2011. clan_member_joined(sd);
  2012. #if !( PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410 )
  2013. // 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
  2014. clif_instance_info( *sd );
  2015. #endif
  2016. // pet
  2017. if (sd->status.pet_id > 0)
  2018. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  2019. // Homunculus [albator]
  2020. if( sd->status.hom_id > 0 )
  2021. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  2022. if( sd->status.mer_id > 0 )
  2023. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  2024. if( sd->status.ele_id > 0 )
  2025. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  2026. map_addiddb(&sd->bl);
  2027. map_delnickdb(sd->status.char_id, sd->status.name);
  2028. if (!chrif_auth_finished(sd))
  2029. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  2030. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  2031. chrif_bsdata_request(sd->status.char_id);
  2032. #ifdef VIP_ENABLE
  2033. sd->vip.time = 0;
  2034. sd->vip.enabled = 0;
  2035. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  2036. #endif
  2037. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  2038. intif_request_questlog(sd);
  2039. if (battle_config.feature_achievement) {
  2040. sd->achievement_data.total_score = 0;
  2041. sd->achievement_data.level = 0;
  2042. sd->achievement_data.save = false;
  2043. sd->achievement_data.count = 0;
  2044. sd->achievement_data.incompleteCount = 0;
  2045. sd->achievement_data.achievements = nullptr;
  2046. intif_request_achievements(sd->status.char_id);
  2047. }
  2048. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  2049. sd->state.connect_new = 1;
  2050. clif_parse_LoadEndAck(sd->fd, sd);
  2051. if( pc_isinvisible( sd ) ){
  2052. sd->vd.class_ = JT_INVISIBLE;
  2053. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  2054. // decrement the number of pvp players on the map
  2055. map_getmapdata( sd->bl.m )->users_pvp--;
  2056. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  2057. // unregister the player for ranking
  2058. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  2059. sd->pvp_timer = INVALID_TIMER;
  2060. }
  2061. clif_changeoption( &sd->bl );
  2062. }
  2063. }
  2064. channel_autojoin(sd);
  2065. }
  2066. static int pc_calc_skillpoint(map_session_data* sd)
  2067. {
  2068. uint16 i, skill_point = 0;
  2069. nullpo_ret(sd);
  2070. for(i = 1; i < MAX_SKILL; i++) {
  2071. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  2072. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  2073. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  2074. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  2075. )
  2076. {
  2077. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  2078. skill_point += sd->status.skill[i].lv;
  2079. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  2080. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  2081. }
  2082. }
  2083. }
  2084. return skill_point;
  2085. }
  2086. static bool pc_grant_allskills(map_session_data *sd, bool addlv) {
  2087. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  2088. return false;
  2089. /**
  2090. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  2091. * skill tree and since they have no icons they'll give resource errors
  2092. * Get ALL skills except npc/guild ones. [Skotlex]
  2093. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  2094. **/
  2095. for (const auto &skill : skill_db) {
  2096. uint16 skill_id = skill.second->nameid;
  2097. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  2098. continue;
  2099. switch (skill_id) {
  2100. case SM_SELFPROVOKE:
  2101. case AB_DUPLELIGHT_MELEE:
  2102. case AB_DUPLELIGHT_MAGIC:
  2103. case WL_CHAINLIGHTNING_ATK:
  2104. case WL_TETRAVORTEX_FIRE:
  2105. case WL_TETRAVORTEX_WATER:
  2106. case WL_TETRAVORTEX_WIND:
  2107. case WL_TETRAVORTEX_GROUND:
  2108. case WL_SUMMON_ATK_FIRE:
  2109. case WL_SUMMON_ATK_WIND:
  2110. case WL_SUMMON_ATK_WATER:
  2111. case WL_SUMMON_ATK_GROUND:
  2112. case LG_OVERBRAND_BRANDISH:
  2113. case LG_OVERBRAND_PLUSATK:
  2114. case WM_SEVERE_RAINSTORM_MELEE:
  2115. case RL_R_TRIP_PLUSATK:
  2116. case SG_DEVIL:
  2117. case MO_TRIPLEATTACK:
  2118. case RG_SNATCHER:
  2119. continue;
  2120. default:
  2121. {
  2122. uint8 lv = (uint8)skill_get_max(skill_id);
  2123. if (lv > 0) {
  2124. uint16 idx = skill_get_index(skill_id);
  2125. sd->status.skill[idx].id = skill_id;
  2126. if (addlv)
  2127. sd->status.skill[idx].lv = lv;
  2128. }
  2129. }
  2130. break;
  2131. }
  2132. }
  2133. return true;
  2134. }
  2135. /*==========================================
  2136. * Calculation of skill level.
  2137. * @param sd
  2138. *------------------------------------------*/
  2139. void pc_calc_skilltree(map_session_data *sd)
  2140. {
  2141. nullpo_retv(sd);
  2142. uint64 job = pc_calc_skilltree_normalize_job(sd);
  2143. int class_ = pc_mapid2jobid(job, sd->status.sex);
  2144. if( class_ == -1 )
  2145. { //Unable to normalize job??
  2146. 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 );
  2147. return;
  2148. }
  2149. uint16 job_id = class_;
  2150. class_ = pc_class2idx(class_);
  2151. for (const auto &skill : skill_db) {
  2152. uint16 skill_id = skill.second->nameid;
  2153. uint16 idx = skill_get_index(skill_id);
  2154. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  2155. sd->status.skill[idx].id = 0; //First clear skills.
  2156. /* permanent skills that must be re-checked */
  2157. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  2158. if (skill_id == 0) {
  2159. sd->status.skill[idx].id = 0;
  2160. sd->status.skill[idx].lv = 0;
  2161. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2162. continue;
  2163. }
  2164. switch (skill_id) {
  2165. case NV_TRICKDEAD:
  2166. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  2167. sd->status.skill[idx].id = 0;
  2168. sd->status.skill[idx].lv = 0;
  2169. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2170. }
  2171. break;
  2172. }
  2173. }
  2174. }
  2175. for (const auto &skill : skill_db) {
  2176. uint16 idx = skill_get_index(skill.second->nameid);
  2177. // Restore original level of skills after deleting earned skills.
  2178. 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 ) {
  2179. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2180. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2181. }
  2182. }
  2183. // Removes Taekwon Ranker skill bonus
  2184. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  2185. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(JOB_TAEKWON);
  2186. if (tree != nullptr && !tree->skills.empty()) {
  2187. for (const auto &it : tree->skills) {
  2188. uint16 sk_idx = skill_get_index(it.first);
  2189. if (sk_idx == 0)
  2190. continue;
  2191. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  2192. if (it.first == NV_BASIC || it.first == NV_FIRSTAID || it.first == WE_CALLBABY)
  2193. continue;
  2194. sd->status.skill[sk_idx].id = 0;
  2195. }
  2196. }
  2197. }
  2198. }
  2199. // Grant all skills
  2200. pc_grant_allskills(sd, false);
  2201. int flag;
  2202. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2203. do {
  2204. flag = 0;
  2205. if (tree == nullptr || tree->skills.empty())
  2206. break;
  2207. for (const auto &skillsit : tree->skills) {
  2208. bool fail = false;
  2209. uint16 skid = skillsit.first;
  2210. uint16 sk_idx = skill_get_index(skid);
  2211. if (sd->status.skill[sk_idx].id)
  2212. continue; //Skill already known.
  2213. if (!battle_config.skillfree) {
  2214. // Checking required skills
  2215. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2216. if (entry != nullptr && !entry->need.empty()) {
  2217. for (const auto &it : entry->need) {
  2218. uint16 sk_need_id = it.first;
  2219. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2220. if (sk_need_idx > 0) {
  2221. uint16 sk_need = sk_need_id;
  2222. 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)
  2223. sk_need = 0; //Not learned.
  2224. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  2225. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2226. else
  2227. sk_need = pc_checkskill(sd,sk_need_id);
  2228. if (sk_need < it.second) {
  2229. fail = true;
  2230. break;
  2231. }
  2232. }
  2233. }
  2234. }
  2235. if (sd->status.base_level < entry->baselv) { //We need to get the actual class in this case
  2236. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2237. c_ = pc_class2idx(c_);
  2238. if (class_ == c_ || (class_ != c_ && sd->status.base_level < entry->baselv))
  2239. fail = true; // base level requirement wasn't satisfied
  2240. }
  2241. if (sd->status.job_level < entry->joblv) { //We need to get the actual class in this case
  2242. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2243. c_ = pc_class2idx(c_);
  2244. if (class_ == c_ || (class_ != c_ && sd->status.job_level < entry->joblv))
  2245. fail = true; // job level requirement wasn't satisfied
  2246. }
  2247. }
  2248. if (!fail) {
  2249. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2250. if (!sd->status.skill[sk_idx].lv && (
  2251. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2252. skill->inf2[INF2_ISWEDDING] ||
  2253. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2254. ))
  2255. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  2256. sd->status.skill[sk_idx].id = skid;
  2257. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  2258. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  2259. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  2260. }
  2261. flag = 1; // skill list has changed, perform another pass
  2262. }
  2263. }
  2264. } while(flag);
  2265. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  2266. uint16 skid = 0;
  2267. /* Taekwon Ranker Bonus Skill Tree
  2268. ============================================
  2269. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  2270. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  2271. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  2272. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2273. if (tree != nullptr && !tree->skills.empty()) {
  2274. for (const auto &it : tree->skills) {
  2275. skid = it.first;
  2276. uint16 sk_idx = skill_get_index(skid);
  2277. if (sk_idx == 0)
  2278. continue;
  2279. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  2280. continue; //Do not include Quest/Wedding skills.
  2281. if( sd->status.skill[sk_idx].id == 0 ) {
  2282. sd->status.skill[sk_idx].id = skid;
  2283. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  2284. } else if( skid != NV_BASIC )
  2285. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  2286. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  2287. }
  2288. }
  2289. }
  2290. // Enable Bard/Dancer spirit linked skills.
  2291. if (sd->sc.count && sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_BARDDANCER) {
  2292. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  2293. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  2294. { BA_POEMBRAGI, DC_FORTUNEKISS },
  2295. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  2296. for (const auto &skill : linked_skills) {
  2297. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  2298. continue;
  2299. // Tag it as a non-savable, non-uppable, bonus skill
  2300. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  2301. }
  2302. }
  2303. }
  2304. //Checks if you can learn a new skill after having leveled up a skill.
  2305. static void pc_check_skilltree(map_session_data *sd)
  2306. {
  2307. int flag = 0;
  2308. if (battle_config.skillfree)
  2309. return; //Function serves no purpose if this is set
  2310. int c = pc_mapid2jobid( pc_calc_skilltree_normalize_job( sd ), sd->status.sex );
  2311. if (c == -1) { //Unable to normalize job??
  2312. 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);
  2313. return;
  2314. }
  2315. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(c);
  2316. if (tree == nullptr || tree->skills.empty())
  2317. return;
  2318. do {
  2319. flag = 0;
  2320. for (const auto &skillsit : tree->skills) {
  2321. uint16 skid = skillsit.first;
  2322. uint16 sk_idx = skill_get_index(skid);
  2323. bool fail = false;
  2324. if (sd->status.skill[sk_idx].id) //Already learned
  2325. continue;
  2326. // Checking required skills
  2327. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2328. if (entry != nullptr && !entry->need.empty()) {
  2329. for (const auto &it : entry->need) {
  2330. uint16 sk_need_id = it.first;
  2331. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2332. if (sk_need_id > 0) {
  2333. short sk_need = sk_need_id;
  2334. 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)
  2335. sk_need = 0; //Not learned.
  2336. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  2337. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2338. else
  2339. sk_need = pc_checkskill(sd,sk_need_id);
  2340. if (sk_need < it.second) {
  2341. fail = true;
  2342. break;
  2343. }
  2344. }
  2345. }
  2346. }
  2347. if( fail )
  2348. continue;
  2349. if (sd->status.base_level < entry->baselv || sd->status.job_level < entry->joblv)
  2350. continue;
  2351. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2352. if( !sd->status.skill[sk_idx].lv && (
  2353. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2354. skill->inf2[INF2_ISWEDDING] ||
  2355. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2356. ) )
  2357. continue; //Cannot be learned via normal means.
  2358. sd->status.skill[sk_idx].id = skid;
  2359. flag = 1;
  2360. }
  2361. } while(flag);
  2362. }
  2363. // Make sure all the skills are in the correct condition
  2364. // before persisting to the backend.. [MouseJstr]
  2365. void pc_clean_skilltree(map_session_data *sd)
  2366. {
  2367. uint16 i;
  2368. for (i = 0; i < MAX_SKILL; i++){
  2369. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  2370. sd->status.skill[i].id = 0;
  2371. sd->status.skill[i].lv = 0;
  2372. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2373. }
  2374. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  2375. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  2376. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2377. }
  2378. }
  2379. }
  2380. uint64 pc_calc_skilltree_normalize_job_sub( map_session_data *sd ){
  2381. int skill_point = pc_calc_skillpoint( sd );
  2382. if( sd->class_ & MAPID_SUMMONER ){
  2383. // Novice's skill points for basic skill.
  2384. std::shared_ptr<s_job_info> summoner_job = job_db.find( JOB_SUMMONER );
  2385. int summoner_skills = summoner_job->max_job_level - 1;
  2386. if( skill_point < summoner_skills ){
  2387. return MAPID_SUMMONER;
  2388. }
  2389. skill_point -= summoner_skills;
  2390. }else{
  2391. // Novice's skill points for basic skill.
  2392. std::shared_ptr<s_job_info> novice_job = job_db.find( JOB_NOVICE );
  2393. int novice_skills = novice_job->max_job_level - 1;
  2394. if( skill_point < novice_skills ){
  2395. return MAPID_NOVICE;
  2396. }
  2397. skill_point -= novice_skills;
  2398. }
  2399. // 1st Class Job LV Check
  2400. if( sd->class_ & JOBL_2 && ( sd->class_ & MAPID_UPPERMASK ) != MAPID_SUPER_NOVICE ){
  2401. uint64 mapid_1st = sd->class_ & MAPID_BASEMASK;
  2402. int class_1st = pc_mapid2jobid( mapid_1st, sd->status.sex );
  2403. if( !sd->change_level_2nd ){
  2404. if( class_1st >= JOB_NOVICE ){
  2405. sd->change_level_2nd = job_db.find( class_1st )->max_job_level;
  2406. }else{
  2407. sd->change_level_2nd = 0;
  2408. }
  2409. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  2410. }
  2411. if( class_1st > 0 && skill_point < ( sd->change_level_2nd - 1 ) ){
  2412. return mapid_1st;
  2413. }
  2414. skill_point -= ( sd->change_level_2nd - 1 );
  2415. }
  2416. // 2nd Class Job LV Check
  2417. if( sd->class_ & JOBL_THIRD && (sd->class_ & MAPID_THIRDMASK) != MAPID_SUPER_NOVICE_E ){
  2418. uint64 mapid_2nd = sd->class_ & MAPID_UPPERMASK;
  2419. int class_2nd = pc_mapid2jobid( mapid_2nd, sd->status.sex );
  2420. if( !sd->change_level_3rd ){
  2421. if( class_2nd >= JOB_NOVICE ){
  2422. sd->change_level_3rd = job_db.find( class_2nd )->max_job_level;
  2423. }else{
  2424. sd->change_level_3rd = 0;
  2425. }
  2426. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  2427. }
  2428. if( class_2nd > 0 && skill_point < ( sd->change_level_3rd - 1 ) ){
  2429. return mapid_2nd;
  2430. }
  2431. skill_point -= ( sd->change_level_3rd - 1 );
  2432. }
  2433. // 3rd Class Job LV Check
  2434. if( sd->class_ & JOBL_FOURTH ){
  2435. uint64 mapid_3rd = sd->class_ & MAPID_THIRDMASK | JOBL_THIRD;
  2436. int class_3rd = pc_mapid2jobid( mapid_3rd, sd->status.sex );
  2437. if( !sd->change_level_4th ){
  2438. if( class_3rd >= JOB_NOVICE ){
  2439. sd->change_level_4th = job_db.find( class_3rd )->max_job_level;
  2440. }else{
  2441. sd->change_level_4th = 0;
  2442. }
  2443. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ), sd->change_level_4th );
  2444. }
  2445. if( class_3rd > 0 && skill_point < ( sd->change_level_4th - 1 ) ){
  2446. return mapid_3rd;
  2447. }
  2448. skill_point -= ( sd->change_level_4th - 1 );
  2449. }
  2450. return sd->class_;
  2451. }
  2452. uint64 pc_calc_skilltree_normalize_job( map_session_data *sd ){
  2453. if( !battle_config.skillup_limit || pc_has_permission( sd, PC_PERM_ALL_SKILL ) ){
  2454. return sd->class_;
  2455. }
  2456. uint64 c = pc_calc_skilltree_normalize_job_sub( sd );
  2457. // Special Masks
  2458. if( sd->class_&JOBL_UPPER ){
  2459. if( pc_mapid2jobid( c | JOBL_UPPER, sd->status.sex ) >= JOB_NOVICE ){
  2460. c |= JOBL_UPPER;// Rebirth Job
  2461. }
  2462. }
  2463. if( sd->class_&JOBL_BABY ){
  2464. if( pc_mapid2jobid( c | JOBL_BABY, sd->status.sex ) >= JOB_NOVICE ){
  2465. c |= JOBL_BABY;// Baby Job
  2466. }
  2467. }
  2468. return c;
  2469. }
  2470. /*==========================================
  2471. * Updates the weight status
  2472. *------------------------------------------
  2473. * 1: overweight 50% for pre-renewal and 70% for renewal
  2474. * 2: overweight 90%
  2475. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  2476. */
  2477. void pc_updateweightstatus(map_session_data *sd)
  2478. {
  2479. int old_overweight;
  2480. int new_overweight;
  2481. nullpo_retv(sd);
  2482. old_overweight = (sd->sc.getSCE(SC_WEIGHT90)) ? 2 : (sd->sc.getSCE(SC_WEIGHT50)) ? 1 : 0;
  2483. #ifdef RENEWAL
  2484. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  2485. #else
  2486. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  2487. #endif
  2488. if( old_overweight == new_overweight )
  2489. return; // no change
  2490. // stop old status change
  2491. if( old_overweight == 1 )
  2492. status_change_end(&sd->bl, SC_WEIGHT50);
  2493. else if( old_overweight == 2 )
  2494. status_change_end(&sd->bl, SC_WEIGHT90);
  2495. // start new status change
  2496. if( new_overweight == 1 )
  2497. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  2498. else if( new_overweight == 2 )
  2499. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  2500. // update overweight status
  2501. sd->regen.state.overweight = new_overweight;
  2502. }
  2503. int pc_disguise(map_session_data *sd, int class_)
  2504. {
  2505. if (!class_ && !sd->disguise)
  2506. return 0;
  2507. if (class_ && sd->disguise == class_)
  2508. return 0;
  2509. if(pc_isinvisible(sd))
  2510. { //Character is invisible. Stealth class-change. [Skotlex]
  2511. sd->disguise = class_; //viewdata is set on uncloaking.
  2512. return 2;
  2513. }
  2514. if (sd->bl.prev != nullptr) {
  2515. pc_stop_walking(sd, 0);
  2516. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  2517. }
  2518. if (!class_) {
  2519. sd->disguise = 0;
  2520. class_ = sd->status.class_;
  2521. } else
  2522. sd->disguise=class_;
  2523. status_set_viewdata(&sd->bl, class_);
  2524. clif_changeoption(&sd->bl);
  2525. if (sd->bl.prev != nullptr) {
  2526. clif_spawn(&sd->bl);
  2527. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2528. { //It seems the cart info is lost on undisguise.
  2529. clif_cartlist(sd);
  2530. clif_updatestatus(sd,SP_CARTINFO);
  2531. }
  2532. if (sd->chatID) {
  2533. struct chat_data* cd;
  2534. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != nullptr)
  2535. clif_dispchat(cd,0);
  2536. }
  2537. }
  2538. return 1;
  2539. }
  2540. /// Show error message
  2541. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2542. /// Check for valid Element, break & show error message if invalid Element
  2543. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2544. /// Check for valid Race, break & show error message if invalid Race
  2545. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2546. /// Check for valid Race2, break & show error message if invalid Race2
  2547. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2548. /// Check for valid Class, break & show error message if invalid Class
  2549. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2550. /// Check for valid Size, break & show error message if invalid Size
  2551. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2552. /// Check for valid SC, break & show error message if invalid SC
  2553. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2554. /**
  2555. * Add auto spell bonus for player while attacking/attacked
  2556. * @param spell: Spell array
  2557. * @param id: Skill to cast
  2558. * @param lv: Skill level
  2559. * @param rate: Success chance
  2560. * @param battle_flag: Battle flag
  2561. * @param card_id: Used to prevent card stacking
  2562. * @param flag: Flags used for extra arguments
  2563. * &0: forces the skill to be casted on self, rather than on the target
  2564. * &1: forces the skill to be casted on target, rather than self
  2565. * &2: random skill level in [1..lv] is chosen
  2566. */
  2567. 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)
  2568. {
  2569. if (spell.size() == MAX_PC_BONUS) {
  2570. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2571. return;
  2572. }
  2573. if (!rate)
  2574. return;
  2575. if (!(battle_flag&BF_RANGEMASK))
  2576. battle_flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2577. if (!(battle_flag&BF_WEAPONMASK))
  2578. battle_flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2579. if (!(battle_flag&BF_SKILLMASK)) {
  2580. if (battle_flag&(BF_MAGIC | BF_MISC))
  2581. battle_flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2582. if (battle_flag&BF_WEAPON)
  2583. battle_flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2584. }
  2585. for (auto &it : spell) {
  2586. 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) {
  2587. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2588. return;
  2589. it.rate = util::safe_addition_cap(it.rate, rate, (short)1000);
  2590. return;
  2591. }
  2592. }
  2593. struct s_autospell entry = {};
  2594. entry.id = id;
  2595. entry.lv = lv;
  2596. entry.rate = cap_value(rate, -1000, 1000);
  2597. entry.battle_flag = battle_flag;
  2598. entry.card_id = card_id;
  2599. entry.flag = flag;
  2600. spell.push_back(entry);
  2601. }
  2602. /**
  2603. * Add auto spell bonus for player while using skills
  2604. * @param spell: Spell array
  2605. * @param src_skill: Trigger skill
  2606. * @param id: Support or target type
  2607. * @param lv: Skill level
  2608. * @param rate: Success chance
  2609. * @param card_id: Used to prevent card stacking
  2610. * @param flag: Flags used for extra arguments
  2611. * &1: forces the skill to be casted on self, rather than on the target of skill
  2612. * &2: random skill level in [1..lv] is chosen
  2613. */
  2614. 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)
  2615. {
  2616. if (spell.size() == MAX_PC_BONUS) {
  2617. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2618. return;
  2619. }
  2620. if (!rate)
  2621. return;
  2622. struct s_autospell entry = {};
  2623. entry.trigger_skill = src_skill;
  2624. entry.id = id;
  2625. entry.lv = lv;
  2626. entry.rate = cap_value(rate, -1000, 1000);
  2627. entry.card_id = card_id;
  2628. entry.flag = flag;
  2629. spell.push_back(entry);
  2630. }
  2631. /**
  2632. * Add inflict effect bonus for player while attacking/attacked
  2633. * @param effect: Effect array
  2634. * @param sc: SC/Effect type
  2635. * @param rate: Success chance
  2636. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2637. * @param flag: Target flag
  2638. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2639. */
  2640. 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)
  2641. {
  2642. if (effect.size() == MAX_PC_BONUS) {
  2643. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2644. return;
  2645. }
  2646. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2647. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2648. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2649. flag |= ATF_TARGET; //Default target: enemy.
  2650. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2651. flag |= ATF_WEAPON; //Default type: weapon.
  2652. if (!duration)
  2653. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2654. for (auto &it : effect) {
  2655. if (it.sc == sc && it.flag == flag) {
  2656. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2657. it.arrow_rate = util::safe_addition_cap(it.arrow_rate, arrow_rate, (short)SHRT_MAX);
  2658. it.duration = umax(it.duration, duration);
  2659. return;
  2660. }
  2661. }
  2662. struct s_addeffect entry = {};
  2663. entry.sc = sc;
  2664. entry.rate = rate;
  2665. entry.arrow_rate = arrow_rate;
  2666. entry.flag = flag;
  2667. entry.duration = duration;
  2668. effect.push_back(entry);
  2669. }
  2670. /**
  2671. * Add inflict effect bonus for player while attacking using skill
  2672. * @param effect: Effect array
  2673. * @param sc: SC/Effect type
  2674. * @param rate: Success chance
  2675. * @param skill_id: Skill to cast
  2676. * @param target: Target type
  2677. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2678. */
  2679. 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)
  2680. {
  2681. if (effect.size() == MAX_PC_BONUS) {
  2682. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2683. return;
  2684. }
  2685. if (!duration)
  2686. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2687. for (auto &it : effect) {
  2688. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2689. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2690. it.duration = umax(it.duration, duration);
  2691. return;
  2692. }
  2693. }
  2694. struct s_addeffectonskill entry = {};
  2695. entry.sc = sc;
  2696. entry.rate = rate;
  2697. entry.skill_id = skill_id;
  2698. entry.target = target;
  2699. entry.duration = duration;
  2700. effect.push_back(entry);
  2701. }
  2702. /**
  2703. * Adjust/add drop rate modifier for player
  2704. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2705. * @param nameid: item id that will be dropped
  2706. * @param group: group id
  2707. * @param class_: target class
  2708. * @param race: target race. if < 0, means monster_id
  2709. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2710. */
  2711. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int class_, short race, int rate)
  2712. {
  2713. if (!nameid && !group) {
  2714. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2715. return;
  2716. }
  2717. if (nameid && !item_db.exists(nameid)) {
  2718. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2719. return;
  2720. }
  2721. if (group && !itemdb_group.exists(group)) {
  2722. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2723. return;
  2724. }
  2725. if (drop.size() == MAX_PC_BONUS) {
  2726. 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);
  2727. return;
  2728. }
  2729. //Apply config rate adjustment settings.
  2730. if (rate >= 0) { //Absolute drop.
  2731. if (battle_config.item_rate_adddrop != 100)
  2732. rate = rate*battle_config.item_rate_adddrop/100;
  2733. if (rate < battle_config.item_drop_adddrop_min)
  2734. rate = battle_config.item_drop_adddrop_min;
  2735. else if (rate > battle_config.item_drop_adddrop_max)
  2736. rate = battle_config.item_drop_adddrop_max;
  2737. } else { //Relative drop, max/min limits are applied at drop time.
  2738. if (battle_config.item_rate_adddrop != 100)
  2739. rate = rate*battle_config.item_rate_adddrop/100;
  2740. if (rate > -1)
  2741. rate = -1;
  2742. }
  2743. for (auto &it : drop) {
  2744. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2745. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2746. it.rate = util::safe_addition_cap(it.rate, rate, 10000);
  2747. return;
  2748. }
  2749. }
  2750. struct s_add_drop entry = {};
  2751. entry.nameid = nameid;
  2752. entry.group = group;
  2753. entry.race = race;
  2754. entry.class_ = class_;
  2755. entry.rate = cap_value(rate, -10000, 10000);
  2756. drop.push_back(entry);
  2757. }
  2758. s_autobonus::~s_autobonus(){
  2759. if( this->active != INVALID_TIMER ){
  2760. delete_timer( this->active, pc_endautobonus );
  2761. this->active = INVALID_TIMER;
  2762. }
  2763. if( this->bonus_script != nullptr ){
  2764. aFree( this->bonus_script );
  2765. this->bonus_script = nullptr;
  2766. }
  2767. if( this->other_script != nullptr ){
  2768. aFree( this->other_script );
  2769. this->other_script = nullptr;
  2770. }
  2771. }
  2772. /**
  2773. * Add autobonus to player when attacking/attacked
  2774. * @param bonus: Bonus array
  2775. * @param script: Script to execute
  2776. * @param rate: Success chance
  2777. * @param dur: Duration
  2778. * @param flag: Battle flag/skill
  2779. * @param other_script: Secondary script to execute
  2780. * @param pos: Item equip position
  2781. * @param onskill: Skill used to trigger autobonus
  2782. * @return True on success or false otherwise
  2783. */
  2784. 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){
  2785. // Check if the same bonus already exists
  2786. for( std::shared_ptr<s_autobonus> autobonus : bonus ){
  2787. // Compare based on position and bonus script
  2788. if( autobonus->pos == pos && strcmp( script, autobonus->bonus_script ) == 0 ){
  2789. return false;
  2790. }
  2791. }
  2792. if (bonus.size() == MAX_PC_BONUS) {
  2793. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2794. return false;
  2795. }
  2796. if (!onskill) {
  2797. if (!(flag&BF_RANGEMASK))
  2798. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2799. if (!(flag&BF_WEAPONMASK))
  2800. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2801. if (!(flag&BF_SKILLMASK)) {
  2802. if (flag&(BF_MAGIC | BF_MISC))
  2803. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2804. if (flag&BF_WEAPON)
  2805. flag |= BF_NORMAL | BF_SKILL;
  2806. }
  2807. }
  2808. std::shared_ptr<s_autobonus> entry = std::make_shared<s_autobonus>();
  2809. if (rate < -10000 || rate > 10000)
  2810. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2811. entry->rate = cap_value(rate, -10000, 10000);
  2812. entry->duration = dur;
  2813. entry->active = INVALID_TIMER;
  2814. entry->atk_type = flag;
  2815. entry->pos = pos;
  2816. entry->bonus_script = aStrdup(script);
  2817. entry->other_script = (other_script ? aStrdup(other_script) : nullptr);
  2818. bonus.push_back(entry);
  2819. return true;
  2820. }
  2821. /**
  2822. * Remove an autobonus from player
  2823. * @param sd: Player data
  2824. * @param bonus: Autobonus array
  2825. * @param restore: Run script on clearing or not
  2826. */
  2827. void pc_delautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> &bonus, bool restore){
  2828. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  2829. while( it != bonus.end() ){
  2830. std::shared_ptr<s_autobonus> b = *it;
  2831. if( b->active != INVALID_TIMER && restore && b->bonus_script != nullptr ){
  2832. unsigned int equip_pos_idx = 0;
  2833. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2834. for (uint8 j = 0; j < EQI_MAX; j++) {
  2835. if (sd.equip_index[j] >= 0)
  2836. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2837. }
  2838. if( ( equip_pos_idx&b->pos ) == b->pos ){
  2839. script_run_autobonus(b->bonus_script, &sd, b->pos);
  2840. }else{
  2841. // Not all required items equipped anymore
  2842. restore = false;
  2843. }
  2844. }
  2845. if( restore ){
  2846. it++;
  2847. continue;
  2848. }
  2849. it = bonus.erase(it);
  2850. }
  2851. }
  2852. /**
  2853. * Execute autobonus on player
  2854. * @param sd: Player data
  2855. * @param autobonus: Autobonus to run
  2856. */
  2857. void pc_exeautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> *bonus, std::shared_ptr<s_autobonus> autobonus)
  2858. {
  2859. if (autobonus->active != INVALID_TIMER)
  2860. delete_timer(autobonus->active, pc_endautobonus);
  2861. if( autobonus->other_script )
  2862. {
  2863. int j;
  2864. unsigned int equip_pos_idx = 0;
  2865. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2866. for(j = 0; j < EQI_MAX; j++) {
  2867. if(sd.equip_index[j] >= 0)
  2868. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2869. }
  2870. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2871. script_run_autobonus(autobonus->other_script,&sd,autobonus->pos);
  2872. }
  2873. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd.bl.id, (intptr_t)bonus);
  2874. status_calc_pc(&sd,SCO_FORCE);
  2875. }
  2876. /**
  2877. * Remove autobonus timer from player
  2878. */
  2879. TIMER_FUNC(pc_endautobonus){
  2880. map_session_data *sd = map_id2sd(id);
  2881. std::vector<std::shared_ptr<s_autobonus>> *bonus = (std::vector<std::shared_ptr<s_autobonus>> *)data;
  2882. nullpo_ret(sd);
  2883. nullpo_ret(bonus);
  2884. for( std::shared_ptr<s_autobonus> autobonus : *bonus ){
  2885. if( autobonus->active == tid ){
  2886. autobonus->active = INVALID_TIMER;
  2887. break;
  2888. }
  2889. }
  2890. status_calc_pc(sd,SCO_FORCE);
  2891. return 0;
  2892. }
  2893. /**
  2894. * Add element bonus to player when attacking
  2895. * @param sd: Player data
  2896. * @param ele: Element to adjust
  2897. * @param rate: Success chance
  2898. * @param flag: Battle flag
  2899. */
  2900. static void pc_bonus_addele(map_session_data* sd, unsigned char ele, short rate, short flag)
  2901. {
  2902. nullpo_retv(sd);
  2903. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2904. if (wd->addele2.size() == MAX_PC_BONUS) {
  2905. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2906. return;
  2907. }
  2908. if (!(flag&BF_RANGEMASK))
  2909. flag |= BF_SHORT | BF_LONG;
  2910. if (!(flag&BF_WEAPONMASK))
  2911. flag |= BF_WEAPON;
  2912. if (!(flag&BF_SKILLMASK)) {
  2913. if (flag&(BF_MAGIC | BF_MISC))
  2914. flag |= BF_SKILL;
  2915. if (flag&BF_WEAPON)
  2916. flag |= BF_NORMAL | BF_SKILL;
  2917. }
  2918. for (auto &it : wd->addele2) {
  2919. if (it.ele == ele && it.flag == flag) {
  2920. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2921. return;
  2922. }
  2923. }
  2924. struct s_addele2 entry = {};
  2925. entry.ele = ele;
  2926. entry.rate = cap_value(rate, -10000, 10000);
  2927. entry.flag = flag;
  2928. wd->addele2.push_back(entry);
  2929. }
  2930. /**
  2931. * Reduce element bonus to player when attacking
  2932. * @param sd: Player data
  2933. * @param ele: Element to adjust
  2934. * @param rate: Success chance
  2935. * @param flag: Battle flag
  2936. */
  2937. static void pc_bonus_subele(map_session_data* sd, unsigned char ele, short rate, short flag)
  2938. {
  2939. nullpo_retv(sd);
  2940. if (sd->subele2.size() == MAX_PC_BONUS) {
  2941. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2942. return;
  2943. }
  2944. if (!(flag&BF_RANGEMASK))
  2945. flag |= BF_SHORT | BF_LONG;
  2946. if (!(flag&BF_WEAPONMASK))
  2947. flag |= BF_WEAPON;
  2948. if (!(flag&BF_SKILLMASK)) {
  2949. if (flag&(BF_MAGIC | BF_MISC))
  2950. flag |= BF_SKILL;
  2951. if (flag&BF_WEAPON)
  2952. flag |= BF_NORMAL | BF_SKILL;
  2953. }
  2954. for (auto &it : sd->subele2) {
  2955. if (it.ele == ele && it.flag == flag) {
  2956. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2957. return;
  2958. }
  2959. }
  2960. struct s_addele2 entry = {};
  2961. entry.ele = ele;
  2962. entry.rate = cap_value(rate, -10000, 10000);
  2963. entry.flag = flag;
  2964. sd->subele2.push_back(entry);
  2965. }
  2966. /**
  2967. * Adjust race damage to target when attacking
  2968. * @param sd: Player data
  2969. * @param race: Race to adjust
  2970. * @param rate: Success chance
  2971. * @param flag: Battle flag
  2972. */
  2973. static void pc_bonus_subrace(map_session_data* sd, unsigned char race, short rate, short flag)
  2974. {
  2975. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2976. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2977. return;
  2978. }
  2979. if (!(flag&BF_RANGEMASK))
  2980. flag |= BF_SHORT | BF_LONG;
  2981. if (!(flag&BF_WEAPONMASK))
  2982. flag |= BF_WEAPON;
  2983. if (!(flag&BF_SKILLMASK)) {
  2984. if (flag&(BF_MAGIC | BF_MISC))
  2985. flag |= BF_SKILL;
  2986. if (flag&BF_WEAPON)
  2987. flag |= BF_NORMAL | BF_SKILL;
  2988. }
  2989. for (auto &it : sd->subrace3) {
  2990. if (it.race == race && it.flag == flag) {
  2991. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2992. return;
  2993. }
  2994. }
  2995. struct s_addrace2 entry = {};
  2996. entry.race = race;
  2997. entry.rate = cap_value(rate, -10000, 10000);
  2998. entry.flag = flag;
  2999. sd->subrace3.push_back(entry);
  3000. }
  3001. /**
  3002. * General item bonus for player
  3003. * @param bonus: Bonus array
  3004. * @param id: Key
  3005. * @param val: Value
  3006. * @param cap_rate: If Value is a rate value that needs to be capped
  3007. */
  3008. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  3009. {
  3010. for (auto &it : bonus) {
  3011. if (it.id == id) {
  3012. if (cap_rate)
  3013. it.val = util::safe_addition_cap(it.val, val, 10000);
  3014. else
  3015. it.val += val;
  3016. return;
  3017. }
  3018. }
  3019. struct s_item_bonus entry = {};
  3020. entry.id = id;
  3021. entry.val = cap_rate ? cap_value(val, -10000, 10000) : val;
  3022. bonus.push_back(entry);
  3023. }
  3024. /**
  3025. * Remove HP/SP to player when attacking
  3026. * @param bonus: Bonus array
  3027. * @param rate: Success chance
  3028. * @param per: Percentage of HP/SP to vanish
  3029. * @param flag: Battle flag
  3030. */
  3031. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  3032. if (bonus.size() == MAX_PC_BONUS) {
  3033. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  3034. return;
  3035. }
  3036. if (!(flag&BF_RANGEMASK))
  3037. flag |= BF_SHORT | BF_LONG;
  3038. if (!(flag&BF_WEAPONMASK))
  3039. flag |= BF_WEAPON;
  3040. if (!(flag&BF_SKILLMASK)) {
  3041. if (flag&(BF_MAGIC | BF_MISC))
  3042. flag |= BF_SKILL;
  3043. if (flag&BF_WEAPON)
  3044. flag |= BF_NORMAL | BF_SKILL;
  3045. }
  3046. for (auto &it : bonus) {
  3047. if (it.flag == flag) {
  3048. it.rate = util::safe_addition_cap(it.rate, rate, (int16)10000);
  3049. it.per += per;
  3050. return;
  3051. }
  3052. }
  3053. struct s_vanish_bonus entry = {};
  3054. entry.rate = cap_value(rate, -10000, 10000);
  3055. entry.per = per;
  3056. entry.flag = flag;
  3057. bonus.push_back(entry);
  3058. }
  3059. /*==========================================
  3060. * Add a bonus(type) to player sd
  3061. * format: bonus bBonusName,val;
  3062. * @param sd
  3063. * @param type Bonus type used by bBonusName
  3064. * @param val Value that usually for rate or fixed value
  3065. *------------------------------------------*/
  3066. void pc_bonus(map_session_data *sd,int type,int val)
  3067. {
  3068. struct status_data *status;
  3069. int bonus;
  3070. nullpo_retv(sd);
  3071. status = &sd->base_status;
  3072. switch(type){
  3073. case SP_STR:
  3074. case SP_AGI:
  3075. case SP_VIT:
  3076. case SP_INT:
  3077. case SP_DEX:
  3078. case SP_LUK:
  3079. if(sd->state.lr_flag != 2)
  3080. sd->indexed_bonus.param_bonus[type-SP_STR]+=val;
  3081. break;
  3082. case SP_POW:
  3083. case SP_STA:
  3084. case SP_WIS:
  3085. case SP_SPL:
  3086. case SP_CON:
  3087. case SP_CRT:
  3088. if (sd->state.lr_flag != 2)
  3089. sd->indexed_bonus.param_bonus[type - SP_POW + PARAM_POW] += val;
  3090. break;
  3091. case SP_ATK1:
  3092. if(!sd->state.lr_flag) {
  3093. bonus = status->rhw.atk + val;
  3094. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  3095. }
  3096. else if(sd->state.lr_flag == 1) {
  3097. bonus = status->lhw.atk + val;
  3098. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  3099. }
  3100. break;
  3101. case SP_ATK2:
  3102. if(!sd->state.lr_flag) {
  3103. bonus = status->rhw.atk2 + val;
  3104. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  3105. }
  3106. else if(sd->state.lr_flag == 1) {
  3107. bonus = status->lhw.atk2 + val;
  3108. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  3109. }
  3110. break;
  3111. case SP_BASE_ATK:
  3112. if(sd->state.lr_flag != 2) {
  3113. #ifdef RENEWAL
  3114. bonus = sd->bonus.eatk + val;
  3115. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3116. #else
  3117. bonus = status->batk + val;
  3118. status->batk = cap_value(bonus, 0, USHRT_MAX);
  3119. #endif
  3120. }
  3121. break;
  3122. case SP_DEF1:
  3123. if(sd->state.lr_flag != 2) {
  3124. bonus = status->def + val;
  3125. #ifdef RENEWAL
  3126. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3127. #else
  3128. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3129. #endif
  3130. }
  3131. break;
  3132. case SP_DEF2:
  3133. if(sd->state.lr_flag != 2) {
  3134. bonus = status->def2 + val;
  3135. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3136. }
  3137. break;
  3138. case SP_MDEF1:
  3139. if(sd->state.lr_flag != 2) {
  3140. bonus = status->mdef + val;
  3141. #ifdef RENEWAL
  3142. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3143. #else
  3144. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3145. #endif
  3146. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  3147. sd->bonus.shieldmdef += bonus;
  3148. }
  3149. }
  3150. break;
  3151. case SP_MDEF2:
  3152. if(sd->state.lr_flag != 2) {
  3153. bonus = status->mdef2 + val;
  3154. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3155. }
  3156. break;
  3157. case SP_HIT:
  3158. if(sd->state.lr_flag != 2) {
  3159. bonus = status->hit + val;
  3160. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3161. } else
  3162. sd->bonus.arrow_hit+=val;
  3163. break;
  3164. case SP_FLEE1:
  3165. if(sd->state.lr_flag != 2) {
  3166. bonus = status->flee + val;
  3167. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3168. }
  3169. break;
  3170. case SP_FLEE2:
  3171. if(sd->state.lr_flag != 2) {
  3172. bonus = status->flee2 + val*10;
  3173. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3174. }
  3175. break;
  3176. case SP_CRITICAL:
  3177. if(sd->state.lr_flag != 2) {
  3178. bonus = status->cri + val*10;
  3179. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3180. } else
  3181. sd->bonus.arrow_cri += val*10;
  3182. break;
  3183. case SP_PATK:
  3184. if (sd->state.lr_flag != 2) {
  3185. bonus = status->patk + val;
  3186. status->patk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3187. }
  3188. break;
  3189. case SP_SMATK:
  3190. if (sd->state.lr_flag != 2) {
  3191. bonus = status->smatk + val;
  3192. status->smatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3193. }
  3194. break;
  3195. case SP_RES:
  3196. if (sd->state.lr_flag != 2) {
  3197. bonus = status->res + val;
  3198. status->res = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3199. }
  3200. break;
  3201. case SP_MRES:
  3202. if (sd->state.lr_flag != 2) {
  3203. bonus = status->mres + val;
  3204. status->mres = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3205. }
  3206. break;
  3207. case SP_HPLUS:
  3208. if (sd->state.lr_flag != 2) {
  3209. bonus = status->hplus + val;
  3210. status->hplus = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3211. }
  3212. break;
  3213. case SP_CRATE:
  3214. if (sd->state.lr_flag != 2) {
  3215. bonus = status->crate + val;
  3216. status->crate = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3217. }
  3218. break;
  3219. case SP_ATKELE:
  3220. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  3221. switch (sd->state.lr_flag)
  3222. {
  3223. case 2:
  3224. switch (sd->status.weapon) {
  3225. case W_BOW:
  3226. case W_REVOLVER:
  3227. case W_RIFLE:
  3228. case W_GATLING:
  3229. case W_SHOTGUN:
  3230. case W_GRENADE:
  3231. //Become weapon element.
  3232. status->rhw.ele=val;
  3233. break;
  3234. default: //Become arrow element.
  3235. sd->bonus.arrow_ele=val;
  3236. break;
  3237. }
  3238. break;
  3239. case 1:
  3240. status->lhw.ele=val;
  3241. break;
  3242. default:
  3243. status->rhw.ele=val;
  3244. break;
  3245. }
  3246. break;
  3247. case SP_DEFELE:
  3248. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  3249. if(sd->state.lr_flag != 2)
  3250. status->def_ele=val;
  3251. break;
  3252. case SP_MAXHP:
  3253. if(sd->state.lr_flag == 2)
  3254. break;
  3255. sd->bonus.hp += val;
  3256. break;
  3257. case SP_MAXSP:
  3258. if(sd->state.lr_flag == 2)
  3259. break;
  3260. sd->bonus.sp += val;
  3261. break;
  3262. case SP_MAXAP:
  3263. if (sd->state.lr_flag == 2)
  3264. break;
  3265. sd->bonus.ap += val;
  3266. break;
  3267. case SP_MAXHPRATE:
  3268. if(sd->state.lr_flag != 2)
  3269. sd->hprate+=val;
  3270. break;
  3271. case SP_MAXSPRATE:
  3272. if(sd->state.lr_flag != 2)
  3273. sd->sprate+=val;
  3274. break;
  3275. case SP_MAXAPRATE:
  3276. if (sd->state.lr_flag != 2)
  3277. sd->aprate += val;
  3278. break;
  3279. case SP_SPRATE:
  3280. if(sd->state.lr_flag != 2)
  3281. sd->dsprate+=val;
  3282. break;
  3283. case SP_ATTACKRANGE:
  3284. switch (sd->state.lr_flag) {
  3285. case 2:
  3286. switch (sd->status.weapon) {
  3287. case W_BOW:
  3288. case W_REVOLVER:
  3289. case W_RIFLE:
  3290. case W_GATLING:
  3291. case W_SHOTGUN:
  3292. case W_GRENADE:
  3293. status->rhw.range += val;
  3294. }
  3295. break;
  3296. case 1:
  3297. status->lhw.range += val;
  3298. break;
  3299. default:
  3300. status->rhw.range += val;
  3301. break;
  3302. }
  3303. break;
  3304. case SP_SPEED_RATE: //Non stackable increase
  3305. if(sd->state.lr_flag != 2)
  3306. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  3307. break;
  3308. case SP_SPEED_ADDRATE: //Stackable increase
  3309. if(sd->state.lr_flag != 2)
  3310. sd->bonus.speed_add_rate -= val;
  3311. break;
  3312. case SP_ASPD: //Raw increase
  3313. if(sd->state.lr_flag != 2)
  3314. sd->bonus.aspd_add -= 10*val;
  3315. break;
  3316. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  3317. if(sd->state.lr_flag != 2)
  3318. #ifndef RENEWAL_ASPD
  3319. status->aspd_rate -= 10*val;
  3320. #else
  3321. status->aspd_rate2 += val;
  3322. #endif
  3323. break;
  3324. case SP_HP_RECOV_RATE:
  3325. if(sd->state.lr_flag != 2)
  3326. sd->hprecov_rate += val;
  3327. break;
  3328. case SP_SP_RECOV_RATE:
  3329. if(sd->state.lr_flag != 2)
  3330. sd->sprecov_rate += val;
  3331. break;
  3332. case SP_CRITICAL_DEF:
  3333. if(sd->state.lr_flag != 2)
  3334. sd->bonus.critical_def += val;
  3335. break;
  3336. case SP_NEAR_ATK_DEF:
  3337. if(sd->state.lr_flag != 2)
  3338. sd->bonus.near_attack_def_rate += val;
  3339. break;
  3340. case SP_LONG_ATK_DEF:
  3341. if(sd->state.lr_flag != 2)
  3342. sd->bonus.long_attack_def_rate += val;
  3343. break;
  3344. case SP_DOUBLE_RATE:
  3345. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  3346. sd->bonus.double_rate = val;
  3347. break;
  3348. case SP_DOUBLE_ADD_RATE:
  3349. if(sd->state.lr_flag == 0)
  3350. sd->bonus.double_add_rate += val;
  3351. break;
  3352. case SP_MATK_RATE:
  3353. if(sd->state.lr_flag != 2)
  3354. sd->matk_rate += val;
  3355. break;
  3356. case SP_IGNORE_DEF_ELE:
  3357. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  3358. if(!sd->state.lr_flag)
  3359. sd->right_weapon.ignore_def_ele |= 1<<val;
  3360. else if(sd->state.lr_flag == 1)
  3361. sd->left_weapon.ignore_def_ele |= 1<<val;
  3362. break;
  3363. case SP_IGNORE_DEF_RACE:
  3364. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  3365. if(!sd->state.lr_flag)
  3366. sd->right_weapon.ignore_def_race |= 1<<val;
  3367. else if(sd->state.lr_flag == 1)
  3368. sd->left_weapon.ignore_def_race |= 1<<val;
  3369. break;
  3370. case SP_IGNORE_DEF_CLASS:
  3371. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  3372. if(!sd->state.lr_flag)
  3373. sd->right_weapon.ignore_def_class |= 1<<val;
  3374. else if(sd->state.lr_flag == 1)
  3375. sd->left_weapon.ignore_def_class |= 1<<val;
  3376. break;
  3377. case SP_ATK_RATE:
  3378. if(sd->state.lr_flag != 2)
  3379. sd->bonus.atk_rate += val;
  3380. break;
  3381. case SP_MAGIC_ATK_DEF:
  3382. if(sd->state.lr_flag != 2)
  3383. sd->bonus.magic_def_rate += val;
  3384. break;
  3385. case SP_MISC_ATK_DEF:
  3386. if(sd->state.lr_flag != 2)
  3387. sd->bonus.misc_def_rate += val;
  3388. break;
  3389. case SP_IGNORE_MDEF_ELE:
  3390. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  3391. if(sd->state.lr_flag != 2)
  3392. sd->bonus.ignore_mdef_ele |= 1<<val;
  3393. break;
  3394. case SP_IGNORE_MDEF_RACE:
  3395. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  3396. if(sd->state.lr_flag != 2)
  3397. sd->bonus.ignore_mdef_race |= 1<<val;
  3398. break;
  3399. case SP_PERFECT_HIT_RATE:
  3400. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  3401. sd->bonus.perfect_hit = val;
  3402. break;
  3403. case SP_PERFECT_HIT_ADD_RATE:
  3404. if(sd->state.lr_flag != 2)
  3405. sd->bonus.perfect_hit_add += val;
  3406. break;
  3407. case SP_CRITICAL_RATE:
  3408. if(sd->state.lr_flag != 2)
  3409. sd->critical_rate+=val;
  3410. break;
  3411. case SP_DEF_RATIO_ATK_ELE:
  3412. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  3413. if(!sd->state.lr_flag)
  3414. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  3415. else if(sd->state.lr_flag == 1)
  3416. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  3417. break;
  3418. case SP_DEF_RATIO_ATK_RACE:
  3419. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  3420. if(!sd->state.lr_flag)
  3421. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  3422. else if(sd->state.lr_flag == 1)
  3423. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  3424. break;
  3425. case SP_DEF_RATIO_ATK_CLASS:
  3426. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  3427. if(!sd->state.lr_flag)
  3428. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  3429. else if(sd->state.lr_flag == 1)
  3430. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  3431. break;
  3432. case SP_HIT_RATE:
  3433. if(sd->state.lr_flag != 2)
  3434. sd->hit_rate += val;
  3435. break;
  3436. case SP_FLEE_RATE:
  3437. if(sd->state.lr_flag != 2)
  3438. sd->flee_rate += val;
  3439. break;
  3440. case SP_FLEE2_RATE:
  3441. if(sd->state.lr_flag != 2)
  3442. sd->flee2_rate += val;
  3443. break;
  3444. case SP_DEF_RATE:
  3445. if(sd->state.lr_flag != 2)
  3446. sd->def_rate += val;
  3447. break;
  3448. case SP_DEF2_RATE:
  3449. if(sd->state.lr_flag != 2)
  3450. sd->def2_rate += val;
  3451. break;
  3452. case SP_MDEF_RATE:
  3453. if(sd->state.lr_flag != 2)
  3454. sd->mdef_rate += val;
  3455. break;
  3456. case SP_MDEF2_RATE:
  3457. if(sd->state.lr_flag != 2)
  3458. sd->mdef2_rate += val;
  3459. break;
  3460. case SP_PATK_RATE:
  3461. if (sd->state.lr_flag != 2)
  3462. sd->patk_rate += val;
  3463. break;
  3464. case SP_SMATK_RATE:
  3465. if (sd->state.lr_flag != 2)
  3466. sd->smatk_rate += val;
  3467. break;
  3468. case SP_RES_RATE:
  3469. if (sd->state.lr_flag != 2)
  3470. sd->res_rate += val;
  3471. break;
  3472. case SP_MRES_RATE:
  3473. if (sd->state.lr_flag != 2)
  3474. sd->mres_rate += val;
  3475. break;
  3476. case SP_HPLUS_RATE:
  3477. if (sd->state.lr_flag != 2)
  3478. sd->hplus_rate += val;
  3479. break;
  3480. case SP_CRATE_RATE:
  3481. if (sd->state.lr_flag != 2)
  3482. sd->crate_rate += val;
  3483. break;
  3484. case SP_RESTART_FULL_RECOVER:
  3485. if(sd->state.lr_flag != 2)
  3486. sd->special_state.restart_full_recover = 1;
  3487. break;
  3488. case SP_NO_CASTCANCEL:
  3489. if(sd->state.lr_flag != 2)
  3490. sd->special_state.no_castcancel = 1;
  3491. break;
  3492. case SP_NO_CASTCANCEL2:
  3493. if(sd->state.lr_flag != 2)
  3494. sd->special_state.no_castcancel2 = 1;
  3495. break;
  3496. case SP_NO_SIZEFIX:
  3497. if(sd->state.lr_flag != 2)
  3498. sd->special_state.no_sizefix = 1;
  3499. break;
  3500. case SP_NO_MAGIC_DAMAGE:
  3501. if(sd->state.lr_flag == 2)
  3502. break;
  3503. val+= sd->special_state.no_magic_damage;
  3504. sd->special_state.no_magic_damage = cap_value(val,0,100);
  3505. break;
  3506. case SP_NO_WEAPON_DAMAGE:
  3507. if(sd->state.lr_flag == 2)
  3508. break;
  3509. val+= sd->special_state.no_weapon_damage;
  3510. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  3511. break;
  3512. case SP_NO_MISC_DAMAGE:
  3513. if(sd->state.lr_flag == 2)
  3514. break;
  3515. val+= sd->special_state.no_misc_damage;
  3516. sd->special_state.no_misc_damage = cap_value(val,0,100);
  3517. break;
  3518. case SP_NO_GEMSTONE:
  3519. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  3520. sd->special_state.no_gemstone = 1;
  3521. break;
  3522. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  3523. if(sd->state.lr_flag != 2) {
  3524. sd->special_state.intravision = 1;
  3525. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  3526. }
  3527. break;
  3528. case SP_NO_KNOCKBACK:
  3529. if(sd->state.lr_flag != 2)
  3530. sd->special_state.no_knockback = 1;
  3531. break;
  3532. case SP_SPLASH_RANGE:
  3533. if(sd->bonus.splash_range < val)
  3534. sd->bonus.splash_range = val;
  3535. break;
  3536. case SP_SPLASH_ADD_RANGE:
  3537. sd->bonus.splash_add_range += val;
  3538. break;
  3539. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  3540. if(sd->state.lr_flag != 2)
  3541. sd->bonus.short_weapon_damage_return += val;
  3542. break;
  3543. case SP_LONG_WEAPON_DAMAGE_RETURN:
  3544. if(sd->state.lr_flag != 2)
  3545. sd->bonus.long_weapon_damage_return += val;
  3546. break;
  3547. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  3548. if(sd->state.lr_flag != 2)
  3549. sd->bonus.magic_damage_return += val;
  3550. break;
  3551. case SP_REDUCE_DAMAGE_RETURN:
  3552. if (sd->state.lr_flag != 2)
  3553. sd->bonus.reduce_damage_return += val;
  3554. break;
  3555. case SP_ALL_STATS: // [Valaris]
  3556. if(sd->state.lr_flag!=2) {
  3557. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3558. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3559. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3560. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  3561. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3562. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  3563. }
  3564. break;
  3565. case SP_ALL_TRAIT_STATS:
  3566. if (sd->state.lr_flag != 2) {
  3567. sd->indexed_bonus.param_bonus[PARAM_POW] += val;
  3568. sd->indexed_bonus.param_bonus[PARAM_STA] += val;
  3569. sd->indexed_bonus.param_bonus[PARAM_WIS] += val;
  3570. sd->indexed_bonus.param_bonus[PARAM_SPL] += val;
  3571. sd->indexed_bonus.param_bonus[PARAM_CON] += val;
  3572. sd->indexed_bonus.param_bonus[PARAM_CRT] += val;
  3573. }
  3574. break;
  3575. case SP_AGI_VIT: // [Valaris]
  3576. if(sd->state.lr_flag!=2) {
  3577. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3578. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3579. }
  3580. break;
  3581. case SP_AGI_DEX_STR: // [Valaris]
  3582. if(sd->state.lr_flag!=2) {
  3583. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3584. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3585. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3586. }
  3587. break;
  3588. case SP_PERFECT_HIDE: // [Valaris]
  3589. if(sd->state.lr_flag!=2)
  3590. sd->special_state.perfect_hiding=1;
  3591. break;
  3592. case SP_UNBREAKABLE:
  3593. if(sd->state.lr_flag!=2)
  3594. sd->bonus.unbreakable += val;
  3595. break;
  3596. case SP_UNBREAKABLE_WEAPON:
  3597. if(sd->state.lr_flag != 2)
  3598. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3599. break;
  3600. case SP_UNBREAKABLE_ARMOR:
  3601. if(sd->state.lr_flag != 2)
  3602. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3603. break;
  3604. case SP_UNBREAKABLE_HELM:
  3605. if(sd->state.lr_flag != 2)
  3606. sd->bonus.unbreakable_equip |= EQP_HELM;
  3607. break;
  3608. case SP_UNBREAKABLE_SHIELD:
  3609. if(sd->state.lr_flag != 2)
  3610. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3611. break;
  3612. case SP_UNBREAKABLE_GARMENT:
  3613. if(sd->state.lr_flag != 2)
  3614. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3615. break;
  3616. case SP_UNBREAKABLE_SHOES:
  3617. if(sd->state.lr_flag != 2)
  3618. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3619. break;
  3620. case SP_CLASSCHANGE: // [Valaris]
  3621. if(sd->state.lr_flag !=2)
  3622. sd->bonus.classchange=val;
  3623. break;
  3624. case SP_SHORT_ATK_RATE:
  3625. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3626. sd->bonus.short_attack_atk_rate+=val;
  3627. break;
  3628. case SP_LONG_ATK_RATE:
  3629. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3630. sd->bonus.long_attack_atk_rate+=val;
  3631. break;
  3632. case SP_BREAK_WEAPON_RATE:
  3633. if(sd->state.lr_flag != 2)
  3634. sd->bonus.break_weapon_rate+=val;
  3635. break;
  3636. case SP_BREAK_ARMOR_RATE:
  3637. if(sd->state.lr_flag != 2)
  3638. sd->bonus.break_armor_rate+=val;
  3639. break;
  3640. case SP_ADD_STEAL_RATE:
  3641. if(sd->state.lr_flag != 2)
  3642. sd->bonus.add_steal_rate+=val;
  3643. break;
  3644. case SP_DELAYRATE:
  3645. if(sd->state.lr_flag != 2)
  3646. sd->bonus.delayrate -= val;
  3647. break;
  3648. case SP_CRIT_ATK_RATE:
  3649. if(sd->state.lr_flag != 2)
  3650. sd->bonus.crit_atk_rate += val;
  3651. break;
  3652. case SP_CRIT_DEF_RATE:
  3653. if(sd->state.lr_flag != 2)
  3654. sd->bonus.crit_def_rate += val;
  3655. break;
  3656. case SP_NO_REGEN:
  3657. if(sd->state.lr_flag != 2)
  3658. sd->regen.state.block|=val;
  3659. break;
  3660. case SP_UNSTRIPABLE_WEAPON:
  3661. if(sd->state.lr_flag != 2)
  3662. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3663. break;
  3664. case SP_UNSTRIPABLE:
  3665. case SP_UNSTRIPABLE_ARMOR:
  3666. if(sd->state.lr_flag != 2)
  3667. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3668. break;
  3669. case SP_UNSTRIPABLE_HELM:
  3670. if(sd->state.lr_flag != 2)
  3671. sd->bonus.unstripable_equip |= EQP_HELM;
  3672. break;
  3673. case SP_UNSTRIPABLE_SHIELD:
  3674. if(sd->state.lr_flag != 2)
  3675. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3676. break;
  3677. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3678. if(!sd->state.lr_flag) {
  3679. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3680. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3681. } else if(sd->state.lr_flag == 1) {
  3682. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3683. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3684. }
  3685. break;
  3686. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3687. if(!sd->state.lr_flag) {
  3688. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3689. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3690. } else if(sd->state.lr_flag == 1) {
  3691. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3692. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3693. }
  3694. break;
  3695. case SP_SP_GAIN_VALUE:
  3696. if(!sd->state.lr_flag)
  3697. sd->bonus.sp_gain_value += val;
  3698. break;
  3699. case SP_HP_GAIN_VALUE:
  3700. if(!sd->state.lr_flag)
  3701. sd->bonus.hp_gain_value += val;
  3702. break;
  3703. case SP_LONG_SP_GAIN_VALUE:
  3704. if(!sd->state.lr_flag)
  3705. sd->bonus.long_sp_gain_value += val;
  3706. break;
  3707. case SP_LONG_HP_GAIN_VALUE:
  3708. if(!sd->state.lr_flag)
  3709. sd->bonus.long_hp_gain_value += val;
  3710. break;
  3711. case SP_MAGIC_SP_GAIN_VALUE:
  3712. if(!sd->state.lr_flag)
  3713. sd->bonus.magic_sp_gain_value += val;
  3714. break;
  3715. case SP_MAGIC_HP_GAIN_VALUE:
  3716. if(!sd->state.lr_flag)
  3717. sd->bonus.magic_hp_gain_value += val;
  3718. break;
  3719. case SP_ADD_HEAL_RATE:
  3720. if(sd->state.lr_flag != 2)
  3721. sd->bonus.add_heal_rate += val;
  3722. break;
  3723. case SP_ADD_HEAL2_RATE:
  3724. if(sd->state.lr_flag != 2)
  3725. sd->bonus.add_heal2_rate += val;
  3726. break;
  3727. case SP_ADD_ITEM_HEAL_RATE:
  3728. if(sd->state.lr_flag != 2)
  3729. sd->bonus.itemhealrate2 += val;
  3730. break;
  3731. case SP_EMATK:
  3732. if(sd->state.lr_flag != 2)
  3733. sd->bonus.ematk += val;
  3734. break;
  3735. case SP_ADD_VARIABLECAST:
  3736. if (sd->state.lr_flag != 2)
  3737. sd->bonus.add_varcast += val;
  3738. break;
  3739. #ifdef RENEWAL_CAST
  3740. case SP_FIXCASTRATE:
  3741. if(sd->state.lr_flag != 2)
  3742. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3743. break;
  3744. case SP_ADD_FIXEDCAST:
  3745. if(sd->state.lr_flag != 2)
  3746. sd->bonus.add_fixcast += val;
  3747. break;
  3748. case SP_CASTRATE:
  3749. case SP_VARCASTRATE:
  3750. if(sd->state.lr_flag != 2)
  3751. sd->bonus.varcastrate -= val;
  3752. break;
  3753. #else
  3754. case SP_ADD_FIXEDCAST:
  3755. case SP_FIXCASTRATE:
  3756. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3757. break;
  3758. case SP_VARCASTRATE:
  3759. case SP_CASTRATE:
  3760. if(sd->state.lr_flag != 2)
  3761. sd->castrate += val;
  3762. break;
  3763. #endif
  3764. case SP_ADDMAXWEIGHT:
  3765. if (sd->state.lr_flag != 2)
  3766. sd->add_max_weight += val;
  3767. break;
  3768. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3769. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3770. break;
  3771. case SP_ABSORB_DMG_MAXHP2:
  3772. sd->bonus.absorb_dmg_maxhp2 = max(sd->bonus.absorb_dmg_maxhp2, val);
  3773. break;
  3774. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3775. if (sd->state.lr_flag != 2)
  3776. sd->bonus.critical_rangeatk += val*10;
  3777. else
  3778. sd->bonus.arrow_cri += val*10;
  3779. break;
  3780. case SP_WEAPON_ATK_RATE:
  3781. if (sd->state.lr_flag != 2)
  3782. sd->bonus.weapon_atk_rate += val;
  3783. break;
  3784. case SP_WEAPON_MATK_RATE:
  3785. if (sd->state.lr_flag != 2)
  3786. sd->bonus.weapon_matk_rate += val;
  3787. break;
  3788. case SP_NO_MADO_FUEL:
  3789. if (sd->state.lr_flag != 2)
  3790. sd->special_state.no_mado_fuel = 1;
  3791. break;
  3792. case SP_NO_WALK_DELAY:
  3793. if (sd->state.lr_flag != 2)
  3794. sd->special_state.no_walk_delay = 1;
  3795. break;
  3796. case SP_ADD_ITEM_SPHEAL_RATE:
  3797. if(sd->state.lr_flag != 2)
  3798. sd->bonus.itemsphealrate2 += val;
  3799. break;
  3800. default:
  3801. if (current_equip_combo_pos > 0) {
  3802. 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);
  3803. }
  3804. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3805. 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);
  3806. }
  3807. else {
  3808. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3809. }
  3810. break;
  3811. }
  3812. }
  3813. /*==========================================
  3814. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3815. * format: bonus2 bBonusName,type2,val;
  3816. * @param sd
  3817. * @param type Bonus type used by bBonusName
  3818. * @param type2
  3819. * @param val Value that usually for rate or fixed value
  3820. *------------------------------------------*/
  3821. void pc_bonus2(map_session_data *sd,int type,int type2,int val)
  3822. {
  3823. nullpo_retv(sd);
  3824. switch(type){
  3825. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3826. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3827. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3828. sd->right_weapon.addele[type2]+=val;
  3829. else if(sd->state.lr_flag == 1)
  3830. sd->left_weapon.addele[type2]+=val;
  3831. else if(sd->state.lr_flag == 2)
  3832. sd->indexed_bonus.arrow_addele[type2]+=val;
  3833. break;
  3834. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3835. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3836. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3837. sd->right_weapon.addrace[type2]+=val;
  3838. else if(sd->state.lr_flag == 1)
  3839. sd->left_weapon.addrace[type2]+=val;
  3840. else if(sd->state.lr_flag == 2)
  3841. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3842. break;
  3843. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3844. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3845. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3846. sd->right_weapon.addclass[type2]+=val;
  3847. else if(sd->state.lr_flag == 1)
  3848. sd->left_weapon.addclass[type2]+=val;
  3849. else if(sd->state.lr_flag == 2)
  3850. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3851. break;
  3852. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3853. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3854. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3855. sd->right_weapon.addsize[type2]+=val;
  3856. else if(sd->state.lr_flag == 1)
  3857. sd->left_weapon.addsize[type2]+=val;
  3858. else if(sd->state.lr_flag == 2)
  3859. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3860. break;
  3861. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3862. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3863. if(sd->state.lr_flag != 2)
  3864. sd->indexed_bonus.subele_script[type2] += val;
  3865. break;
  3866. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3867. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3868. if(sd->state.lr_flag != 2)
  3869. sd->indexed_bonus.subrace[type2]+=val;
  3870. break;
  3871. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3872. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3873. if(sd->state.lr_flag != 2)
  3874. sd->indexed_bonus.subclass[type2]+=val;
  3875. break;
  3876. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3877. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3878. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3879. break;
  3880. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3881. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3882. 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);
  3883. break;
  3884. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3885. if(sd->state.lr_flag == 2)
  3886. break;
  3887. if (sd->reseff.size() == MAX_PC_BONUS) {
  3888. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3889. break;
  3890. }
  3891. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3892. break;
  3893. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3894. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3895. if(sd->state.lr_flag != 2)
  3896. sd->indexed_bonus.magic_addele_script[type2] += val;
  3897. break;
  3898. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3899. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3900. if(sd->state.lr_flag != 2)
  3901. sd->indexed_bonus.magic_addrace[type2]+=val;
  3902. break;
  3903. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3904. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3905. if(sd->state.lr_flag != 2)
  3906. sd->indexed_bonus.magic_addclass[type2]+=val;
  3907. break;
  3908. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3909. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3910. if(sd->state.lr_flag != 2)
  3911. sd->indexed_bonus.magic_addsize[type2]+=val;
  3912. break;
  3913. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3914. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3915. if(sd->state.lr_flag != 2)
  3916. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3917. break;
  3918. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3919. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3920. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3921. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3922. break;
  3923. }
  3924. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3925. }
  3926. break;
  3927. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3928. if(sd->state.lr_flag == 2)
  3929. break;
  3930. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3931. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3932. break;
  3933. }
  3934. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3935. break;
  3936. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3937. if(sd->state.lr_flag == 2)
  3938. break;
  3939. if (sd->add_def.size() == MAX_PC_BONUS) {
  3940. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3941. break;
  3942. }
  3943. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3944. break;
  3945. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3946. if(sd->state.lr_flag == 2)
  3947. break;
  3948. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3949. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3950. break;
  3951. }
  3952. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3953. break;
  3954. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3955. if(!sd->state.lr_flag) {
  3956. sd->right_weapon.hp_drain_rate.rate += type2;
  3957. sd->right_weapon.hp_drain_rate.per += val;
  3958. }
  3959. else if(sd->state.lr_flag == 1) {
  3960. sd->left_weapon.hp_drain_rate.rate += type2;
  3961. sd->left_weapon.hp_drain_rate.per += val;
  3962. }
  3963. break;
  3964. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3965. if(!sd->state.lr_flag) {
  3966. sd->right_weapon.sp_drain_rate.rate += type2;
  3967. sd->right_weapon.sp_drain_rate.per += val;
  3968. }
  3969. else if(sd->state.lr_flag == 1) {
  3970. sd->left_weapon.sp_drain_rate.rate += type2;
  3971. sd->left_weapon.sp_drain_rate.per += val;
  3972. }
  3973. break;
  3974. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3975. if(sd->state.lr_flag != 2) {
  3976. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3977. }
  3978. break;
  3979. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3980. if(sd->state.lr_flag != 2) {
  3981. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3982. }
  3983. break;
  3984. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3985. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3986. sd->bonus.get_zeny_rate = val;
  3987. sd->bonus.get_zeny_num = type2;
  3988. }
  3989. break;
  3990. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3991. if(sd->state.lr_flag != 2) {
  3992. sd->bonus.get_zeny_rate += val;
  3993. sd->bonus.get_zeny_num += type2;
  3994. }
  3995. break;
  3996. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3997. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3998. if(sd->state.lr_flag == 2)
  3999. break;
  4000. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  4001. sd->special_state.bonus_coma = 1;
  4002. break;
  4003. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  4004. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  4005. if(sd->state.lr_flag == 2)
  4006. break;
  4007. sd->indexed_bonus.weapon_coma_race[type2] += val;
  4008. sd->special_state.bonus_coma = 1;
  4009. break;
  4010. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  4011. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  4012. if(sd->state.lr_flag == 2)
  4013. break;
  4014. sd->indexed_bonus.weapon_coma_class[type2] += val;
  4015. sd->special_state.bonus_coma = 1;
  4016. break;
  4017. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  4018. if(sd->state.lr_flag != 2)
  4019. sd->indexed_bonus.weapon_atk[type2]+=val;
  4020. break;
  4021. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  4022. if(sd->state.lr_flag != 2)
  4023. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  4024. break;
  4025. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  4026. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  4027. if(sd->state.lr_flag != 2)
  4028. sd->indexed_bonus.critaddrace[type2] += val*10;
  4029. break;
  4030. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  4031. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4032. if(sd->state.lr_flag != 2)
  4033. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  4034. break;
  4035. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  4036. if(sd->state.lr_flag == 2)
  4037. break;
  4038. if (sd->skillatk.size() == MAX_PC_BONUS) {
  4039. 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);
  4040. break;
  4041. }
  4042. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  4043. break;
  4044. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  4045. if(sd->state.lr_flag == 2)
  4046. break;
  4047. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  4048. 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);
  4049. break;
  4050. }
  4051. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  4052. break;
  4053. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  4054. if(sd->state.lr_flag == 2)
  4055. break;
  4056. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  4057. 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);
  4058. break;
  4059. }
  4060. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  4061. break;
  4062. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  4063. if(sd->state.lr_flag == 2)
  4064. break;
  4065. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4066. 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);
  4067. break;
  4068. }
  4069. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  4070. break;
  4071. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  4072. if(sd->state.lr_flag != 2) {
  4073. sd->hp_loss.value = type2;
  4074. sd->hp_loss.rate = val;
  4075. }
  4076. break;
  4077. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  4078. if(sd->state.lr_flag != 2) {
  4079. sd->hp_regen.value = type2;
  4080. sd->hp_regen.rate = val;
  4081. }
  4082. break;
  4083. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  4084. if (sd->state.lr_flag != 2) {
  4085. sd->percent_hp_regen.value = type2;
  4086. sd->percent_hp_regen.rate = val;
  4087. }
  4088. break;
  4089. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  4090. if (sd->state.lr_flag != 2) {
  4091. sd->percent_sp_regen.value = type2;
  4092. sd->percent_sp_regen.rate = val;
  4093. }
  4094. break;
  4095. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  4096. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  4097. if(sd->state.lr_flag != 2)
  4098. sd->right_weapon.addrace2[type2] += val;
  4099. else
  4100. sd->left_weapon.addrace2[type2] += val;
  4101. break;
  4102. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  4103. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  4104. if(sd->state.lr_flag != 2)
  4105. sd->indexed_bonus.subsize[type2]+=val;
  4106. break;
  4107. case SP_WEAPON_SUBSIZE: // bonus2 bWeaponSubSize,s,x;
  4108. PC_BONUS_CHK_SIZE(type2, SP_WEAPON_SUBSIZE);
  4109. if (sd->state.lr_flag != 2)
  4110. sd->indexed_bonus.weapon_subsize[type2] += val;
  4111. break;
  4112. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  4113. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  4114. if(sd->state.lr_flag != 2)
  4115. sd->indexed_bonus.magic_subsize[type2]+=val;
  4116. break;
  4117. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  4118. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  4119. if(sd->state.lr_flag != 2)
  4120. sd->indexed_bonus.subrace2[type2]+=val;
  4121. break;
  4122. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  4123. if(sd->state.lr_flag == 2)
  4124. break;
  4125. if( !item_db.exists( type2 ) ){
  4126. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  4127. break;
  4128. }
  4129. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  4130. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4131. break;
  4132. }
  4133. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  4134. break;
  4135. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  4136. if (sd->state.lr_flag == 2)
  4137. break;
  4138. if (!type2 || !itemdb_group.exists(type2)) {
  4139. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  4140. break;
  4141. }
  4142. if (sd->itemgrouphealrate.size() == MAX_PC_BONUS) {
  4143. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4144. break;
  4145. }
  4146. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  4147. break;
  4148. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  4149. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  4150. if(sd->state.lr_flag != 2)
  4151. sd->indexed_bonus.expaddrace[type2]+=val;
  4152. break;
  4153. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  4154. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  4155. if(sd->state.lr_flag != 2)
  4156. sd->indexed_bonus.expaddclass[type2]+=val;
  4157. break;
  4158. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  4159. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  4160. if(sd->state.lr_flag != 2)
  4161. sd->indexed_bonus.sp_gain_race[type2]+=val;
  4162. break;
  4163. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  4164. if (sd->state.lr_flag != 2)
  4165. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  4166. break;
  4167. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  4168. if (sd->state.lr_flag != 2)
  4169. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  4170. break;
  4171. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  4172. if(sd->state.lr_flag != 2) {
  4173. sd->sp_loss.value = type2;
  4174. sd->sp_loss.rate = val;
  4175. }
  4176. break;
  4177. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  4178. if(sd->state.lr_flag != 2) {
  4179. sd->sp_regen.value = type2;
  4180. sd->sp_regen.rate = val;
  4181. }
  4182. break;
  4183. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  4184. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  4185. if(!sd->state.lr_flag) {
  4186. sd->right_weapon.hp_drain_race[type2] += val;
  4187. }
  4188. else if(sd->state.lr_flag == 1) {
  4189. sd->left_weapon.hp_drain_race[type2] += val;
  4190. }
  4191. break;
  4192. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  4193. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  4194. if(!sd->state.lr_flag) {
  4195. sd->right_weapon.sp_drain_race[type2] += val;
  4196. }
  4197. else if(sd->state.lr_flag == 1) {
  4198. sd->left_weapon.sp_drain_race[type2] += val;
  4199. }
  4200. break;
  4201. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  4202. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  4203. if(!sd->state.lr_flag) {
  4204. sd->right_weapon.hp_drain_class[type2] += val;
  4205. }
  4206. else if(sd->state.lr_flag == 1) {
  4207. sd->left_weapon.hp_drain_class[type2] += val;
  4208. }
  4209. break;
  4210. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  4211. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  4212. if(!sd->state.lr_flag) {
  4213. sd->right_weapon.sp_drain_class[type2] += val;
  4214. }
  4215. else if(sd->state.lr_flag == 1) {
  4216. sd->left_weapon.sp_drain_class[type2] += val;
  4217. }
  4218. break;
  4219. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  4220. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  4221. if(sd->state.lr_flag != 2)
  4222. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  4223. break;
  4224. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  4225. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  4226. if(sd->state.lr_flag != 2)
  4227. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  4228. break;
  4229. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  4230. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  4231. if(sd->state.lr_flag != 2)
  4232. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  4233. break;
  4234. case SP_SP_IGNORE_RES_RACE_RATE: // bonus2 bIgnoreResRaceRate,r,n;
  4235. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_RES_RACE_RATE);
  4236. if(sd->state.lr_flag != 2)
  4237. sd->indexed_bonus.ignore_res_by_race[type2] += val;
  4238. break;
  4239. case SP_SP_IGNORE_MRES_RACE_RATE: // bonus2 bIgnoreMResRaceRate,r,n;
  4240. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_MRES_RACE_RATE);
  4241. if(sd->state.lr_flag != 2)
  4242. sd->indexed_bonus.ignore_mres_by_race[type2] += val;
  4243. break;
  4244. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  4245. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  4246. if (sd->state.lr_flag != 2)
  4247. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  4248. break;
  4249. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  4250. if(sd->state.lr_flag == 2)
  4251. break;
  4252. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  4253. 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);
  4254. break;
  4255. }
  4256. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  4257. break;
  4258. case SP_SKILL_DELAY:
  4259. if(sd->state.lr_flag == 2)
  4260. break;
  4261. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  4262. 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);
  4263. break;
  4264. }
  4265. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  4266. break;
  4267. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  4268. if(sd->state.lr_flag == 2)
  4269. break;
  4270. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  4271. 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);
  4272. break;
  4273. }
  4274. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  4275. break;
  4276. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  4277. if (sd->state.lr_flag == 2)
  4278. break;
  4279. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  4280. 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);
  4281. break;
  4282. }
  4283. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  4284. break;
  4285. #ifdef RENEWAL_CAST
  4286. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4287. if(sd->state.lr_flag == 2)
  4288. break;
  4289. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  4290. 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);
  4291. break;
  4292. }
  4293. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  4294. break;
  4295. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4296. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4297. if(sd->state.lr_flag == 2)
  4298. break;
  4299. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  4300. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4301. break;
  4302. }
  4303. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  4304. break;
  4305. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4306. if(sd->state.lr_flag == 2)
  4307. break;
  4308. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  4309. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4310. break;
  4311. }
  4312. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  4313. break;
  4314. #else
  4315. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4316. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4317. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  4318. break;
  4319. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4320. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4321. if(sd->state.lr_flag == 2)
  4322. break;
  4323. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4324. 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);
  4325. break;
  4326. }
  4327. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  4328. break;
  4329. #endif
  4330. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  4331. if(sd->state.lr_flag == 2)
  4332. break;
  4333. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  4334. 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);
  4335. break;
  4336. }
  4337. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  4338. break;
  4339. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  4340. if (sd->subskill.size() == MAX_PC_BONUS) {
  4341. 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);
  4342. break;
  4343. }
  4344. pc_bonus_itembonus(sd->subskill, type2, val, false);
  4345. break;
  4346. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  4347. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  4348. sd->indexed_bonus.subdefele[type2] += val;
  4349. break;
  4350. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  4351. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  4352. sd->indexed_bonus.coma_class[type2] += val;
  4353. sd->special_state.bonus_coma = 1;
  4354. break;
  4355. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  4356. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  4357. sd->indexed_bonus.coma_race[type2] += val;
  4358. sd->special_state.bonus_coma = 1;
  4359. break;
  4360. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  4361. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  4362. if(sd->state.lr_flag != 2)
  4363. sd->indexed_bonus.magic_addrace2[type2] += val;
  4364. break;
  4365. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  4366. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  4367. if (sd->state.lr_flag != 2)
  4368. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  4369. break;
  4370. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  4371. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  4372. if (sd->state.lr_flag != 2)
  4373. sd->indexed_bonus.dropaddrace[type2] += val;
  4374. break;
  4375. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  4376. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  4377. if (sd->state.lr_flag != 2)
  4378. sd->indexed_bonus.dropaddclass[type2] += val;
  4379. break;
  4380. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  4381. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  4382. sd->indexed_bonus.magic_subdefele[type2] += val;
  4383. break;
  4384. case SP_ADD_ITEM_SPHEAL_RATE: // bonus2 bAddItemSPHealRate,iid,n;
  4385. if( sd->state.lr_flag == 2 ){
  4386. break;
  4387. }
  4388. if( !item_db.exists( type2 ) ){
  4389. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE Invalid item with id %d\n", type2 );
  4390. break;
  4391. }
  4392. if( sd->itemsphealrate.size() == MAX_PC_BONUS ){
  4393. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4394. break;
  4395. }
  4396. pc_bonus_itembonus( sd->itemsphealrate, type2, val, false );
  4397. break;
  4398. case SP_ADD_ITEMGROUP_SPHEAL_RATE: // bonus2 bAddItemGroupSPHealRate,ig,n;
  4399. if( sd->state.lr_flag == 2 ){
  4400. break;
  4401. }
  4402. if( !type2 || !itemdb_group.exists( type2 ) ){
  4403. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Invalid item group with id %d\n", type2 );
  4404. break;
  4405. }
  4406. if( sd->itemgroupsphealrate.size() == MAX_PC_BONUS ){
  4407. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4408. break;
  4409. }
  4410. pc_bonus_itembonus( sd->itemgroupsphealrate, type2, val, false );
  4411. break;
  4412. default:
  4413. if (current_equip_combo_pos > 0) {
  4414. 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);
  4415. }
  4416. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4417. 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);
  4418. }
  4419. else {
  4420. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  4421. }
  4422. break;
  4423. }
  4424. }
  4425. /**
  4426. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  4427. * @param sd
  4428. * @param type Bonus type used by bBonusName
  4429. * @param type2
  4430. * @param type3
  4431. * @param val Value that usually for rate or fixed value
  4432. */
  4433. void pc_bonus3(map_session_data *sd,int type,int type2,int type3,int val)
  4434. {
  4435. nullpo_retv(sd);
  4436. switch(type){
  4437. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  4438. if(sd->state.lr_flag != 2)
  4439. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  4440. break;
  4441. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  4442. if(sd->state.lr_flag != 2)
  4443. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  4444. break;
  4445. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  4446. if(sd->state.lr_flag != 2)
  4447. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  4448. break;
  4449. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  4450. if(sd->state.lr_flag != 2)
  4451. {
  4452. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4453. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4454. pc_bonus_autospell(sd->autospell, type2, type3, val, 0, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4455. }
  4456. break;
  4457. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  4458. if(sd->state.lr_flag != 2)
  4459. {
  4460. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4461. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4462. pc_bonus_autospell(sd->autospell2, type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4463. }
  4464. break;
  4465. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  4466. if (sd->state.lr_flag != 2)
  4467. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  4468. break;
  4469. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  4470. if (sd->state.lr_flag != 2)
  4471. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  4472. break;
  4473. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  4474. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4475. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  4476. break;
  4477. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  4478. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4479. if(sd->state.lr_flag != 2)
  4480. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  4481. break;
  4482. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  4483. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4484. if( sd->state.lr_flag != 2 )
  4485. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  4486. break;
  4487. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  4488. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  4489. if (sd->state.lr_flag != 2)
  4490. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  4491. break;
  4492. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  4493. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  4494. if (sd->state.lr_flag != 2)
  4495. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  4496. break;
  4497. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  4498. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  4499. if (sd->state.lr_flag != 2)
  4500. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  4501. break;
  4502. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  4503. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  4504. if(sd->state.lr_flag != 2) {
  4505. sd->sp_vanish_race[type2].rate += type3;
  4506. sd->sp_vanish_race[type2].per += val;
  4507. }
  4508. break;
  4509. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  4510. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  4511. if(sd->state.lr_flag != 2) {
  4512. sd->hp_vanish_race[type2].rate += type3;
  4513. sd->hp_vanish_race[type2].per += val;
  4514. }
  4515. break;
  4516. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  4517. if(sd->state.lr_flag != 2) {
  4518. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  4519. }
  4520. break;
  4521. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  4522. if(sd->state.lr_flag != 2) {
  4523. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  4524. }
  4525. break;
  4526. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  4527. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  4528. if (sd->state.lr_flag == 2)
  4529. break;
  4530. //! CONFIRM: Is it not stackable? Does not check max or min value?
  4531. //if (type3 > sd->norecover_state_race[type2].rate) {
  4532. // sd->norecover_state_race[type2].rate = type3;
  4533. // sd->norecover_state_race[type2].tick = val;
  4534. // break;
  4535. //}
  4536. sd->norecover_state_race[type2].rate = type3;
  4537. sd->norecover_state_race[type2].tick = val;
  4538. break;
  4539. default:
  4540. if (current_equip_combo_pos > 0) {
  4541. 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);
  4542. }
  4543. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4544. 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);
  4545. }
  4546. else {
  4547. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  4548. }
  4549. break;
  4550. }
  4551. }
  4552. /**
  4553. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  4554. * @param sd
  4555. * @param type Bonus type used by bBonusName
  4556. * @param type2
  4557. * @param type3
  4558. * @param type4
  4559. * @param val Value that usually for rate or fixed value
  4560. */
  4561. void pc_bonus4(map_session_data *sd,int type,int type2,int type3,int type4,int val)
  4562. {
  4563. nullpo_retv(sd);
  4564. switch(type){
  4565. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4566. if(sd->state.lr_flag != 2)
  4567. pc_bonus_autospell(sd->autospell, type2, type3, type4, 0, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4568. break;
  4569. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4570. if(sd->state.lr_flag != 2)
  4571. pc_bonus_autospell(sd->autospell2, type2, type3, type4, BF_NORMAL|BF_SKILL, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4572. break;
  4573. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4574. if(sd->state.lr_flag != 2)
  4575. {
  4576. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4577. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4578. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, val, current_equip_card_id, target ? AUTOSPELL_FORCE_TARGET : AUTOSPELL_FORCE_SELF);
  4579. }
  4580. break;
  4581. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4582. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4583. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  4584. break;
  4585. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4586. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4587. if (sd->state.lr_flag != 2)
  4588. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4589. break;
  4590. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4591. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4592. if( sd->state.lr_flag != 2 )
  4593. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4594. break;
  4595. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4596. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4597. if(sd->state.lr_flag == 2)
  4598. break;
  4599. sd->def_set_race[type2].rate = type3;
  4600. sd->def_set_race[type2].tick = type4;
  4601. sd->def_set_race[type2].value = val;
  4602. break;
  4603. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4604. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4605. if(sd->state.lr_flag == 2)
  4606. break;
  4607. sd->mdef_set_race[type2].rate = type3;
  4608. sd->mdef_set_race[type2].tick = type4;
  4609. sd->mdef_set_race[type2].value = val;
  4610. break;
  4611. default:
  4612. if (current_equip_combo_pos > 0) {
  4613. 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);
  4614. }
  4615. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4616. 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);
  4617. }
  4618. else {
  4619. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4620. }
  4621. break;
  4622. }
  4623. }
  4624. /**
  4625. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4626. * @param sd
  4627. * @param type Bonus type used by bBonusName
  4628. * @param type2
  4629. * @param type3
  4630. * @param type4
  4631. * @param val Value that usually for rate or fixed value
  4632. */
  4633. void pc_bonus5(map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  4634. {
  4635. nullpo_retv(sd);
  4636. switch(type){
  4637. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4638. if(sd->state.lr_flag != 2)
  4639. pc_bonus_autospell(sd->autospell, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4640. break;
  4641. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4642. if(sd->state.lr_flag != 2)
  4643. pc_bonus_autospell(sd->autospell2, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4644. break;
  4645. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4646. if(sd->state.lr_flag != 2)
  4647. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4648. break;
  4649. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4650. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4651. if( sd->state.lr_flag != 2 )
  4652. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4653. break;
  4654. default:
  4655. if (current_equip_combo_pos > 0) {
  4656. 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);
  4657. }
  4658. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4659. 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);
  4660. }
  4661. else {
  4662. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4663. }
  4664. break;
  4665. }
  4666. }
  4667. /*==========================================
  4668. * Grants a player a given skill. Flag values are:
  4669. * 0 - Grant permanent skill to be bound to skill tree
  4670. * 1 - Grant an item skill (temporary)
  4671. * 2 - Like 1, except the level granted can stack with previously learned level.
  4672. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4673. *------------------------------------------*/
  4674. bool pc_skill(map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  4675. uint16 idx = 0;
  4676. nullpo_ret(sd);
  4677. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4678. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4679. return false;
  4680. }
  4681. if (level > MAX_SKILL_LEVEL) {
  4682. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4683. return false;
  4684. }
  4685. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4686. 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);
  4687. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4688. }
  4689. switch (type) {
  4690. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4691. sd->status.skill[idx].id = skill_id;
  4692. sd->status.skill[idx].lv = level;
  4693. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4694. if (level == 0) { //Remove skill.
  4695. sd->status.skill[idx].id = 0;
  4696. clif_deleteskill(sd,skill_id);
  4697. } else
  4698. clif_addskill(sd,skill_id);
  4699. 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.
  4700. status_calc_pc(sd, SCO_NONE);
  4701. break;
  4702. case ADDSKILL_TEMP: //Item bonus skill.
  4703. if (sd->status.skill[idx].id != 0) {
  4704. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4705. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4706. } else {
  4707. sd->status.skill[idx].id = skill_id;
  4708. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4709. }
  4710. sd->status.skill[idx].lv = max(sd->status.skill[idx].lv, level);
  4711. break;
  4712. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4713. if (sd->status.skill[idx].id != 0) {
  4714. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4715. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4716. } else {
  4717. sd->status.skill[idx].id = skill_id;
  4718. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4719. }
  4720. sd->status.skill[idx].lv += level;
  4721. break;
  4722. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4723. sd->status.skill[idx].id = skill_id;
  4724. sd->status.skill[idx].lv = level;
  4725. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4726. if (level == 0) { //Remove skill.
  4727. sd->status.skill[idx].id = 0;
  4728. clif_deleteskill(sd,skill_id);
  4729. } else
  4730. clif_addskill(sd,skill_id);
  4731. 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.
  4732. status_calc_pc(sd, SCO_NONE);
  4733. break;
  4734. default:
  4735. return false;
  4736. }
  4737. return true;
  4738. }
  4739. /**
  4740. * Set's a player's plagiarized skill.
  4741. * @param sd: Player
  4742. * @param skill_id: Skill to be plagiarized
  4743. * @param skill_lv: Skill level to be plagiarized
  4744. * @return True on success or false otherwise
  4745. */
  4746. bool pc_skill_plagiarism(map_session_data &sd, uint16 skill_id, uint16 skill_lv)
  4747. {
  4748. skill_id = skill_dummy2skill_id(skill_id);
  4749. uint16 idx = skill_get_index(skill_id);
  4750. // Use skill index, avoiding out-of-bound array [Cydh]
  4751. if (idx == 0) {
  4752. ShowWarning("pc_skill_plagiarism: invalid skill idx 0 for skill %d.\n", skill_id);
  4753. return false;
  4754. }
  4755. skill_lv = cap_value(skill_lv, 1, skill_get_max(skill_id));
  4756. int type = skill_isCopyable(&sd, skill_id);
  4757. if (type == 1) {
  4758. pc_skill_plagiarism_reset(sd, type);
  4759. sd.cloneskill_idx = idx;
  4760. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), skill_id);
  4761. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), skill_lv);
  4762. } else if (type == 2) {
  4763. pc_skill_plagiarism_reset(sd, type);
  4764. sd.reproduceskill_idx = idx;
  4765. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), skill_id);
  4766. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), skill_lv);
  4767. } else {
  4768. ShowWarning("pc_skill_plagiarism: skill %d is not copyable.\n", skill_id);
  4769. return false;
  4770. }
  4771. sd.status.skill[idx].id = skill_id;
  4772. sd.status.skill[idx].lv = static_cast<uint8>(skill_lv);
  4773. sd.status.skill[idx].flag = SKILL_FLAG_PLAGIARIZED;
  4774. clif_addskill(&sd, skill_id);
  4775. return true;
  4776. }
  4777. /**
  4778. * Clear plagiarized skills from a player.
  4779. * @param sd: Player
  4780. * @param type: 1 for Plagiarism or 2 for Reproduce
  4781. * @return True on success or false otherwise
  4782. */
  4783. bool pc_skill_plagiarism_reset(map_session_data &sd, uint8 type)
  4784. {
  4785. uint16 idx;
  4786. if (type == 1)
  4787. idx = sd.cloneskill_idx;
  4788. else if (type == 2)
  4789. idx = sd.reproduceskill_idx;
  4790. else {
  4791. ShowError("pc_skill_plagiarism_reset: Unknown type %d.\n", type);
  4792. return false;
  4793. }
  4794. if (sd.status.skill[idx].flag == SKILL_FLAG_PLAGIARIZED) {
  4795. uint16 skill_id = sd.status.skill[idx].id;
  4796. sd.status.skill[idx].id = 0;
  4797. sd.status.skill[idx].lv = 0;
  4798. sd.status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4799. clif_deleteskill(&sd, skill_id);
  4800. if (type == 1) {
  4801. sd.cloneskill_idx = 0;
  4802. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  4803. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  4804. }
  4805. else if (type == 2) {
  4806. sd.reproduceskill_idx = 0;
  4807. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), 0);
  4808. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  4809. }
  4810. }
  4811. return true;
  4812. }
  4813. /*==========================================
  4814. * Append a card to an item ?
  4815. *------------------------------------------*/
  4816. int pc_insert_card(map_session_data* sd, int idx_card, int idx_equip)
  4817. {
  4818. nullpo_ret(sd);
  4819. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4820. return 0;
  4821. }
  4822. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4823. return 0;
  4824. }
  4825. int i;
  4826. t_itemid nameid;
  4827. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4828. struct item_data* item_card = sd->inventory_data[idx_card];
  4829. if(item_eq == nullptr)
  4830. return 0; //Invalid item index.
  4831. if(item_card == nullptr)
  4832. return 0; //Invalid card index.
  4833. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4834. return 0; // target item missing
  4835. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4836. return 0; // target card missing
  4837. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4838. return 0; // only weapons and armor are allowed
  4839. if( item_card->type != IT_CARD )
  4840. return 0; // must be a card
  4841. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4842. return 0; // target must be identified
  4843. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4844. return 0; // card slots reserved for other purposes
  4845. if( (item_eq->equip & item_card->equip) == 0 )
  4846. return 0; // card cannot be compounded on this item type
  4847. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4848. return 0; // attempted to place shield card on left-hand weapon.
  4849. 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)) )
  4850. return 0; // specific accessory-card can only be inserted to specific accessory.
  4851. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4852. return 0; // item must be unequipped
  4853. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4854. if( i == item_eq->slots )
  4855. return 0; // no free slots
  4856. // remember the card id to insert
  4857. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4858. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4859. {// failed
  4860. clif_insert_card(sd,idx_equip,idx_card,1);
  4861. }
  4862. else
  4863. {// success
  4864. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4865. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4866. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4867. clif_insert_card(sd,idx_equip,idx_card,0);
  4868. }
  4869. return 0;
  4870. }
  4871. /**
  4872. * Returns the count of unidentified items with the option to identify too.
  4873. * @param sd: Player data
  4874. * @param identify_item: Whether or not to identify any unidentified items
  4875. * @return Unidentified items count
  4876. */
  4877. int pc_identifyall(map_session_data *sd, bool identify_item)
  4878. {
  4879. int unidentified_count = 0;
  4880. for (int i = 0; i < MAX_INVENTORY; i++) {
  4881. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4882. if (identify_item == true) {
  4883. sd->inventory.u.items_inventory[i].identify = 1;
  4884. clif_item_identified(sd,i,0);
  4885. }
  4886. unidentified_count++;
  4887. }
  4888. }
  4889. return unidentified_count;
  4890. }
  4891. //
  4892. // Items
  4893. //
  4894. /*==========================================
  4895. * Update buying value by skills
  4896. *------------------------------------------*/
  4897. int pc_modifybuyvalue(map_session_data *sd,int orig_value)
  4898. {
  4899. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4900. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4901. rate1 = 5+skill*2-((skill==10)? 1:0);
  4902. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4903. rate2 = 5+skill*4;
  4904. if(rate1 < rate2) rate1 = rate2;
  4905. if(rate1)
  4906. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4907. if(val < battle_config.min_shop_buy)
  4908. val = battle_config.min_shop_buy;
  4909. return val;
  4910. }
  4911. /*==========================================
  4912. * Update selling value by skills
  4913. *------------------------------------------*/
  4914. int pc_modifysellvalue(map_session_data *sd,int orig_value)
  4915. {
  4916. int skill,val = orig_value,rate = 0;
  4917. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4918. rate = 5+skill*2-((skill==10)? 1:0);
  4919. if(rate)
  4920. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4921. if (val < battle_config.min_shop_sell)
  4922. val = battle_config.min_shop_sell;
  4923. return val;
  4924. }
  4925. /*==========================================
  4926. * Checking if we have enough place on inventory for new item
  4927. * Make sure to take 30k as limit (for client I guess)
  4928. * @param sd
  4929. * @param nameid
  4930. * @param amount
  4931. * @return e_chkitem_result
  4932. *------------------------------------------*/
  4933. char pc_checkadditem(map_session_data *sd, t_itemid nameid, int amount)
  4934. {
  4935. int i;
  4936. struct item_data* data;
  4937. nullpo_ret(sd);
  4938. if(amount > MAX_AMOUNT)
  4939. return CHKADDITEM_OVERAMOUNT;
  4940. data = itemdb_search(nameid);
  4941. if(!itemdb_isstackable2(data))
  4942. return CHKADDITEM_NEW;
  4943. if( data->stack.inventory && amount > data->stack.amount )
  4944. return CHKADDITEM_OVERAMOUNT;
  4945. if (data->flag.guid)
  4946. return CHKADDITEM_NEW;
  4947. for(i=0;i<MAX_INVENTORY;i++){
  4948. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4949. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4950. 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 ) )
  4951. return CHKADDITEM_OVERAMOUNT;
  4952. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4953. if( i >= sd->status.inventory_slots ){
  4954. return CHKADDITEM_OVERAMOUNT;
  4955. }
  4956. return CHKADDITEM_EXIST;
  4957. }
  4958. }
  4959. return CHKADDITEM_NEW;
  4960. }
  4961. /*==========================================
  4962. * Return number of available place in inventory
  4963. * Each non stackable item will reduce place by 1
  4964. * @param sd
  4965. * @return Number of empty slots
  4966. *------------------------------------------*/
  4967. uint8 pc_inventoryblank(map_session_data *sd)
  4968. {
  4969. uint16 i;
  4970. uint8 b;
  4971. nullpo_ret(sd);
  4972. for(i = 0, b = 0; i < sd->status.inventory_slots; i++){
  4973. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4974. b++;
  4975. }
  4976. return b;
  4977. }
  4978. /**
  4979. * Attempts to remove zeny from player
  4980. * @param sd: Player
  4981. * @param zeny: Zeny removed
  4982. * @param type: Log type
  4983. * @param log_charid: (optional) From who to log (if not needed, use 0)
  4984. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4985. */
  4986. char pc_payzeny(map_session_data *sd, int zeny, enum e_log_pick_type type, uint32 log_charid)
  4987. {
  4988. nullpo_retr(2,sd);
  4989. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4990. if( zeny < 0 )
  4991. {
  4992. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4993. return 1;
  4994. }
  4995. if( sd->status.zeny < zeny )
  4996. return 1; //Not enough.
  4997. sd->status.zeny -= zeny;
  4998. clif_updatestatus(sd,SP_ZENY);
  4999. log_zeny(*sd, type, log_charid, -zeny);
  5000. if( zeny > 0 && sd->state.showzeny ) {
  5001. char output[255];
  5002. sprintf(output, "Removed %dz.", zeny);
  5003. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5004. }
  5005. return 0;
  5006. }
  5007. /**
  5008. * Attempts to give zeny to player
  5009. * @param sd: Player
  5010. * @param type: Log type
  5011. * @param log_charid: (optional) From who to log (if not needed, use 0)
  5012. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  5013. */
  5014. char pc_getzeny(map_session_data *sd, int zeny, enum e_log_pick_type type, uint32 log_charid)
  5015. {
  5016. nullpo_retr(-1,sd);
  5017. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  5018. if( zeny < 0 )
  5019. {
  5020. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  5021. return 1;
  5022. }
  5023. if( zeny > MAX_ZENY - sd->status.zeny )
  5024. zeny = MAX_ZENY - sd->status.zeny;
  5025. sd->status.zeny += zeny;
  5026. clif_updatestatus(sd,SP_ZENY);
  5027. log_zeny(*sd, type, log_charid, zeny);
  5028. if( zeny > 0 && sd->state.showzeny ) {
  5029. char output[255];
  5030. sprintf(output, "Gained %dz.", zeny);
  5031. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5032. }
  5033. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  5034. return 0;
  5035. }
  5036. /**
  5037. * Attempts to remove Cash Points from player
  5038. * @param sd: Player
  5039. * @param price: Total points (cash + kafra) the player has to pay
  5040. * @param points: Kafra points the player has to pay
  5041. * @param type: Log type
  5042. * @return -1: Not enough points, otherwise success (cash+points)
  5043. */
  5044. int pc_paycash(map_session_data *sd, int price, int points, e_log_pick_type type)
  5045. {
  5046. int cash;
  5047. nullpo_retr(-1,sd);
  5048. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  5049. cash = price-points;
  5050. if( sd->cashPoints < cash || sd->kafraPoints < points )
  5051. {
  5052. 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);
  5053. return -1;
  5054. }
  5055. if( cash ){
  5056. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  5057. sd->cashPoints -= cash;
  5058. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  5059. }
  5060. if( points ){
  5061. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  5062. sd->kafraPoints -= points;
  5063. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  5064. }
  5065. if( battle_config.cashshop_show_points )
  5066. {
  5067. char output[CHAT_SIZE_MAX];
  5068. 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.
  5069. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5070. }
  5071. return cash+points;
  5072. }
  5073. /**
  5074. * Attempts to give Cash Points to player
  5075. * @param sd: Player
  5076. * @param cash: Cash points the player gets
  5077. * @param points: Kafra points the player gets
  5078. * @param type: Log type
  5079. * @return -1: Error, otherwise success (cash or points)
  5080. */
  5081. int pc_getcash(map_session_data *sd, int cash, int points, e_log_pick_type type)
  5082. {
  5083. char output[CHAT_SIZE_MAX];
  5084. nullpo_retr(-1,sd);
  5085. cash = cap_value(cash, 0, MAX_CASHPOINT); //prevent command UB
  5086. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  5087. if( cash > 0 )
  5088. {
  5089. if( cash > MAX_CASHPOINT-sd->cashPoints )
  5090. {
  5091. 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);
  5092. cash = MAX_CASHPOINT-sd->cashPoints;
  5093. }
  5094. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  5095. sd->cashPoints += cash;
  5096. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  5097. if( battle_config.cashshop_show_points )
  5098. {
  5099. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  5100. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5101. }
  5102. return cash;
  5103. }
  5104. if( points > 0 )
  5105. {
  5106. if( points > MAX_KAFRAPOINT-sd->kafraPoints )
  5107. {
  5108. 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);
  5109. points = MAX_KAFRAPOINT-sd->kafraPoints;
  5110. }
  5111. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  5112. sd->kafraPoints += points;
  5113. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  5114. if( battle_config.cashshop_show_points )
  5115. {
  5116. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  5117. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5118. }
  5119. return points;
  5120. }
  5121. return -1; //shouldn't happen but just in case
  5122. }
  5123. /**
  5124. * Searching a specified itemid in inventory and return his stored index
  5125. * @param sd Player
  5126. * @param nameid Find this Item!
  5127. * @return Stored index in inventory, or -1 if not found.
  5128. **/
  5129. short pc_search_inventory(map_session_data *sd, t_itemid nameid) {
  5130. short i;
  5131. nullpo_retr(-1, sd);
  5132. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  5133. return ( i < MAX_INVENTORY ) ? i : -1;
  5134. }
  5135. /** Attempt to add a new item to player inventory
  5136. * @param sd
  5137. * @param item_data
  5138. * @param amount
  5139. * @param log_type
  5140. * @return
  5141. * 0 = success
  5142. * 1 = invalid itemid not found or negative amount
  5143. * 2 = overweight
  5144. * 3 = ?
  5145. * 4 = no free place found
  5146. * 5 = max amount reached
  5147. * 6 = ?
  5148. * 7 = stack limitation
  5149. */
  5150. enum e_additem_result pc_additem(map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  5151. struct item_data *id;
  5152. int16 i;
  5153. unsigned int w;
  5154. nullpo_retr(ADDITEM_INVALID, sd);
  5155. nullpo_retr(ADDITEM_INVALID, item);
  5156. if( item->nameid == 0 || amount <= 0 )
  5157. return ADDITEM_INVALID;
  5158. if( amount > MAX_AMOUNT )
  5159. return ADDITEM_OVERAMOUNT;
  5160. id = itemdb_search(item->nameid);
  5161. if( id->stack.inventory && amount > id->stack.amount )
  5162. {// item stack limitation
  5163. return ADDITEM_STACKLIMIT;
  5164. }
  5165. w = id->weight*amount;
  5166. if(sd->weight + w > sd->max_weight)
  5167. return ADDITEM_OVERWEIGHT;
  5168. if (id->flag.guid && !item->unique_id)
  5169. item->unique_id = pc_generate_unique_id(sd);
  5170. // Stackable | Non Rental
  5171. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  5172. for( i = 0; i < MAX_INVENTORY; i++ ) {
  5173. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  5174. sd->inventory.u.items_inventory[i].bound == item->bound &&
  5175. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  5176. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  5177. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  5178. 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 ) )
  5179. return ADDITEM_OVERAMOUNT;
  5180. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  5181. if( i >= sd->status.inventory_slots ){
  5182. return ADDITEM_OVERAMOUNT;
  5183. }
  5184. sd->inventory.u.items_inventory[i].amount += amount;
  5185. clif_additem(sd,i,amount,0);
  5186. break;
  5187. }
  5188. }
  5189. }else{
  5190. i = MAX_INVENTORY;
  5191. }
  5192. if (i >= MAX_INVENTORY) {
  5193. i = pc_search_inventory(sd,0);
  5194. if( i < 0 )
  5195. return ADDITEM_OVERITEM;
  5196. if( i >= sd->status.inventory_slots ){
  5197. return ADDITEM_OVERITEM;
  5198. }
  5199. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  5200. // clear equip and favorite fields first, just in case
  5201. if( item->equip )
  5202. sd->inventory.u.items_inventory[i].equip = 0;
  5203. if( item->favorite != 0 )
  5204. sd->inventory.u.items_inventory[i].favorite = 0;
  5205. if( item->equipSwitch )
  5206. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  5207. sd->inventory.u.items_inventory[i].amount = amount;
  5208. sd->inventory_data[i] = id;
  5209. sd->last_addeditem_index = i;
  5210. if (!itemdb_isstackable2(id) || id->flag.guid)
  5211. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  5212. clif_additem(sd,i,amount,0);
  5213. }
  5214. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  5215. sd->weight += w;
  5216. clif_updatestatus(sd,SP_WEIGHT);
  5217. //Auto-equip
  5218. if(id->flag.autoequip)
  5219. pc_equipitem(sd, i, id->equip);
  5220. /* rental item check */
  5221. if( item->expire_time ) {
  5222. if( time(nullptr) > item->expire_time ) {
  5223. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  5224. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  5225. } else {
  5226. unsigned int seconds = (unsigned int)( item->expire_time - time(nullptr) );
  5227. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  5228. pc_inventory_rental_add(sd, seconds);
  5229. }
  5230. }
  5231. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  5232. pc_show_questinfo(sd);
  5233. return ADDITEM_SUCCESS;
  5234. }
  5235. /*==========================================
  5236. * Remove an item at index n from inventory by amount.
  5237. * @param sd
  5238. * @param n Item index in inventory
  5239. * @param amount
  5240. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  5241. * @param reason Delete reason
  5242. * @param log_type e_log_pick_type
  5243. * @return 1 - invalid itemid or negative amount; 0 - Success
  5244. *------------------------------------------*/
  5245. char pc_delitem(map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  5246. {
  5247. nullpo_retr(1, sd);
  5248. if(n < 0 || sd->inventory.u.items_inventory[n].nameid == 0 || amount <= 0 || sd->inventory.u.items_inventory[n].amount<amount || sd->inventory_data[n] == nullptr)
  5249. return 1;
  5250. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  5251. sd->inventory.u.items_inventory[n].amount -= amount;
  5252. sd->weight -= sd->inventory_data[n]->weight*amount ;
  5253. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  5254. if(sd->inventory.u.items_inventory[n].equip)
  5255. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  5256. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  5257. sd->inventory_data[n] = nullptr;
  5258. }
  5259. if(!(type&1))
  5260. clif_delitem(sd,n,amount,reason);
  5261. if(!(type&2))
  5262. clif_updatestatus(sd,SP_WEIGHT);
  5263. pc_show_questinfo(sd);
  5264. return 0;
  5265. }
  5266. /*==========================================
  5267. * Attempt to drop an item.
  5268. * @param sd
  5269. * @param n Item index in inventory
  5270. * @param amount Amount of item
  5271. * @return False = fail; True = success
  5272. *------------------------------------------*/
  5273. bool pc_dropitem(map_session_data *sd,int n,int amount)
  5274. {
  5275. nullpo_retr(1, sd);
  5276. if(n < 0 || n >= MAX_INVENTORY)
  5277. return false;
  5278. if(amount <= 0)
  5279. return false;
  5280. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  5281. sd->inventory.u.items_inventory[n].amount <= 0 ||
  5282. sd->inventory.u.items_inventory[n].amount < amount ||
  5283. sd->state.trading || sd->state.vending ||
  5284. !sd->inventory_data[n] //pc_delitem would fail on this case.
  5285. )
  5286. return false;
  5287. if( sd->inventory.u.items_inventory[n].equipSwitch )
  5288. return false;
  5289. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  5290. {
  5291. clif_displaymessage (sd->fd, msg_txt(sd,271));
  5292. return false; //Can't drop items in nodrop mapflag maps.
  5293. }
  5294. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  5295. {
  5296. clif_displaymessage (sd->fd, msg_txt(sd,263));
  5297. return false;
  5298. }
  5299. // bypass drop restriction in map_addflooritem because we've already checked it above
  5300. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2|4, 0))
  5301. return false;
  5302. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  5303. clif_dropitem(sd, n, amount);
  5304. return true;
  5305. }
  5306. /*==========================================
  5307. * Attempt to pick up an item.
  5308. * @param sd
  5309. * @param fitem Item that will be picked
  5310. * @return False = fail; True = success
  5311. *------------------------------------------*/
  5312. bool pc_takeitem(map_session_data *sd,struct flooritem_data *fitem)
  5313. {
  5314. int flag = 0;
  5315. t_tick tick = gettick();
  5316. struct party_data *p = nullptr;
  5317. nullpo_ret(sd);
  5318. nullpo_ret(fitem);
  5319. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  5320. return false; // Distance is too far
  5321. if (sd->sc.cant.pickup)
  5322. return false;
  5323. if (sd->status.party_id)
  5324. p = party_search(sd->status.party_id);
  5325. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  5326. map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  5327. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  5328. if (!(p && p->party.item&1 &&
  5329. first_sd && first_sd->status.party_id == sd->status.party_id
  5330. ))
  5331. return false;
  5332. }
  5333. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  5334. map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  5335. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  5336. if (!(p && p->party.item&1 &&
  5337. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5338. (second_sd && second_sd->status.party_id == sd->status.party_id))
  5339. ))
  5340. return false;
  5341. }
  5342. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  5343. map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  5344. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  5345. if(!(p && p->party.item&1 &&
  5346. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5347. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  5348. (third_sd && third_sd->status.party_id == sd->status.party_id))
  5349. ))
  5350. return false;
  5351. }
  5352. }
  5353. }
  5354. }
  5355. //This function takes care of giving the item to whoever should have it, considering party-share options.
  5356. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  5357. clif_additem(sd,0,0,flag);
  5358. return true;
  5359. }
  5360. //Display pickup animation.
  5361. pc_stop_attack(sd);
  5362. clif_takeitem(&sd->bl,&fitem->bl);
  5363. if (fitem->mob_id &&
  5364. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  5365. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  5366. )
  5367. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  5368. map_clearflooritem(&fitem->bl);
  5369. return true;
  5370. }
  5371. /*==========================================
  5372. * Check if item is usable.
  5373. * Return:
  5374. * false = no
  5375. * true = yes
  5376. *------------------------------------------*/
  5377. bool pc_isUseitem(map_session_data *sd,int n)
  5378. {
  5379. struct item_data *item;
  5380. t_itemid nameid;
  5381. nullpo_ret(sd);
  5382. item = sd->inventory_data[n];
  5383. nameid = sd->inventory.u.items_inventory[n].nameid;
  5384. if( item == nullptr )
  5385. return false;
  5386. //Not consumable item
  5387. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  5388. return false;
  5389. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  5390. return true;
  5391. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5392. if(mapdata->getMapFlag(MF_NOITEMCONSUMPTION)) //consumable but mapflag prevent it
  5393. return false;
  5394. //Prevent mass item usage. [Skotlex]
  5395. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  5396. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  5397. )
  5398. return false;
  5399. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  5400. clif_msg(sd,ITEM_NOUSE_SITTING);
  5401. return false; // You cannot use this item while sitting.
  5402. }
  5403. if (sd->state.storage_flag && item->type != IT_CASH) {
  5404. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  5405. return false; // You cannot use this item while storage is open.
  5406. }
  5407. if (item->flag.dead_branch && (mapdata->getMapFlag(MF_NOBRANCH) || mapdata_flag_gvg2(mapdata)))
  5408. return false;
  5409. if( itemdb_group.item_exists( IG_MF_NOTELEPORT, nameid ) ){
  5410. if( ( mapdata->getMapFlag(MF_NOTELEPORT) || mapdata_flag_gvg2( mapdata ) ) ){
  5411. clif_skill_teleportmessage( sd, 0 );
  5412. return false;
  5413. }
  5414. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5415. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5416. return false;
  5417. }
  5418. }
  5419. if( itemdb_group.item_exists( IG_MF_NORETURN, nameid ) ){
  5420. if( mapdata->getMapFlag(MF_NORETURN) ){
  5421. return false;
  5422. }
  5423. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5424. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5425. return false;
  5426. }
  5427. }
  5428. if( itemdb_group.item_exists( IG_GIANT_FLY_WING, nameid ) ){
  5429. struct party_data *pd = party_search( sd->status.party_id );
  5430. if( pd ){
  5431. int i;
  5432. ARR_FIND( 0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader );
  5433. // User is not party leader
  5434. if( i == MAX_PARTY ){
  5435. clif_msg( sd, ITEM_PARTY_MEMBER_NOT_SUMMONED );
  5436. return false;
  5437. }
  5438. 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 ) );
  5439. // No party members found on same map
  5440. if( i == MAX_PARTY ){
  5441. clif_msg( sd, ITEM_PARTY_NO_MEMBER_IN_MAP );
  5442. return false;
  5443. }
  5444. }else{
  5445. clif_msg( sd, ITEM_PARTY_MEMBER_NOT_SUMMONED );
  5446. return false;
  5447. }
  5448. }
  5449. switch( nameid ) {
  5450. case ITEMID_MERCENARY_RED_POTION:
  5451. case ITEMID_MERCENARY_BLUE_POTION:
  5452. case ITEMID_M_CENTER_POTION:
  5453. case ITEMID_M_AWAKENING_POTION:
  5454. case ITEMID_M_BERSERK_POTION:
  5455. if( sd->md == nullptr || sd->md->db == nullptr )
  5456. return false;
  5457. if( sd->md->sc.cant.consume )
  5458. return false;
  5459. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  5460. return false;
  5461. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  5462. return false;
  5463. break;
  5464. case ITEMID_NEURALIZER:
  5465. if( !mapdata->getMapFlag(MF_RESET) )
  5466. return false;
  5467. break;
  5468. }
  5469. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != nullptr )
  5470. return false; // Mercenary Scrolls
  5471. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  5472. if( pc_is90overweight(sd) ) {
  5473. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  5474. return false;
  5475. }
  5476. if( !pc_inventoryblank(sd) ) {
  5477. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  5478. return false;
  5479. }
  5480. }
  5481. //Gender check
  5482. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  5483. return false;
  5484. //Required level check
  5485. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  5486. return false;
  5487. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  5488. return false;
  5489. //Not equipable by class. [Skotlex]
  5490. if (!pc_job_can_use_item(sd,item))
  5491. return false;
  5492. if (sd->sc.cant.consume)
  5493. return false;
  5494. if (!pc_isItemClass(sd,item))
  5495. return false;
  5496. //Dead Branch items
  5497. if( item->flag.dead_branch )
  5498. log_branch(sd);
  5499. return true;
  5500. }
  5501. /*==========================================
  5502. * Last checks to use an item.
  5503. * Return:
  5504. * 0 = fail
  5505. * 1 = success
  5506. *------------------------------------------*/
  5507. int pc_useitem(map_session_data *sd,int n)
  5508. {
  5509. t_tick tick = gettick();
  5510. int amount;
  5511. t_itemid nameid;
  5512. struct script_code *script;
  5513. struct item item;
  5514. struct item_data *id;
  5515. nullpo_ret(sd);
  5516. if (sd->state.mail_writing)
  5517. return 0;
  5518. if (sd->npc_id) {
  5519. if (sd->progressbar.npc_id) {
  5520. clif_progressbar_abort(sd);
  5521. return 0; // First item use attempt cancels the progress bar
  5522. }
  5523. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  5524. #ifdef RENEWAL
  5525. clif_msg( sd, WORK_IN_PROGRESS );
  5526. #endif
  5527. return 0;
  5528. }
  5529. }
  5530. item = sd->inventory.u.items_inventory[n];
  5531. id = sd->inventory_data[n];
  5532. if (item.nameid == 0 || item.amount <= 0)
  5533. return 0;
  5534. if( !pc_isUseitem(sd,n) )
  5535. return 0;
  5536. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  5537. nameid = id->nameid;
  5538. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  5539. return 0;
  5540. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  5541. if( id->flag.delay_consume > 0 ) {
  5542. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.getSCE(SC_ALL_RIDING) )
  5543. return 0;
  5544. else if( pc_issit(sd) )
  5545. return 0;
  5546. }
  5547. //Since most delay-consume items involve using a "skill-type" target cursor,
  5548. //perform a skill-use check before going through. [Skotlex]
  5549. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  5550. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  5551. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  5552. return 0;
  5553. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  5554. return 0;
  5555. /* on restricted maps the item is consumed but the effect is not used */
  5556. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(*sd, id->nameid)) {
  5557. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  5558. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  5559. clif_useitemack(sd,n,item.amount-1,true);
  5560. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  5561. }
  5562. return 0;/* regardless, effect is not run */
  5563. }
  5564. if (pet_db_search(id->nameid, PET_CATCH) != nullptr && map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  5565. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  5566. return 0;
  5567. }
  5568. sd->itemid = item.nameid;
  5569. sd->itemindex = n;
  5570. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  5571. sd->catch_target_class = PET_CATCH_FAIL;
  5572. amount = item.amount;
  5573. script = id->script;
  5574. //Check if the item is to be consumed immediately [Skotlex]
  5575. if (id->flag.delay_consume > 0)
  5576. clif_useitemack(sd, n, amount, true);
  5577. else
  5578. {
  5579. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  5580. {
  5581. clif_useitemack(sd, n, amount - 1, true);
  5582. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  5583. }
  5584. else
  5585. clif_useitemack(sd, n, 0, false);
  5586. }
  5587. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  5588. potion_flag = 2; // Famous player's potions have 50% more efficiency
  5589. //Update item use time.
  5590. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  5591. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  5592. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  5593. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  5594. potion_flag = 0;
  5595. return 1;
  5596. }
  5597. /**
  5598. * Add item on cart for given index.
  5599. * @param sd
  5600. * @param item
  5601. * @param amount
  5602. * @param log_type
  5603. * @return See pc.hpp::e_additem_result
  5604. */
  5605. enum e_additem_result pc_cart_additem(map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  5606. {
  5607. struct item_data *data;
  5608. int i,w;
  5609. nullpo_retr(ADDITEM_INVALID, sd);
  5610. nullpo_retr(ADDITEM_INVALID, item);
  5611. if(item->nameid == 0 || amount <= 0)
  5612. return ADDITEM_INVALID;
  5613. if (itemdb_ishatched_egg(item))
  5614. return ADDITEM_INVALID;
  5615. data = itemdb_search(item->nameid);
  5616. if( data->stack.cart && amount > data->stack.amount )
  5617. {// item stack limitation
  5618. return ADDITEM_STACKLIMIT;
  5619. }
  5620. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  5621. { // Check item trade restrictions [Skotlex]
  5622. clif_displaymessage (sd->fd, msg_txt(sd,264));
  5623. return ADDITEM_INVALID;
  5624. }
  5625. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  5626. return ADDITEM_OVERWEIGHT;
  5627. i = MAX_CART;
  5628. if( itemdb_isstackable2(data) && !item->expire_time )
  5629. {
  5630. for (i = 0; i < MAX_CART; i++) {
  5631. if (sd->cart.u.items_cart[i].nameid == item->nameid
  5632. && sd->cart.u.items_cart[i].bound == item->bound
  5633. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5634. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5635. )
  5636. break;
  5637. }
  5638. }
  5639. if( i < MAX_CART )
  5640. {// item already in cart, stack it
  5641. 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 ) )
  5642. return ADDITEM_OVERAMOUNT; // no slot
  5643. sd->cart.u.items_cart[i].amount += amount;
  5644. clif_cart_additem(sd,i,amount);
  5645. }
  5646. else
  5647. {// item not stackable or not present, add it
  5648. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5649. if( i == MAX_CART )
  5650. return ADDITEM_OVERAMOUNT; // no slot
  5651. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5652. sd->cart.u.items_cart[i].id = 0;
  5653. sd->cart.u.items_cart[i].amount = amount;
  5654. sd->cart_num++;
  5655. clif_cart_additem(sd,i,amount);
  5656. }
  5657. sd->cart.u.items_cart[i].favorite = 0; // clear
  5658. sd->cart.u.items_cart[i].equipSwitch = 0;
  5659. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5660. sd->cart_weight += w;
  5661. clif_updatestatus(sd,SP_CARTINFO);
  5662. return ADDITEM_SUCCESS;
  5663. }
  5664. /*==========================================
  5665. * Delete item on cart for given index.
  5666. *------------------------------------------*/
  5667. void pc_cart_delitem(map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  5668. {
  5669. nullpo_retv(sd);
  5670. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5671. sd->cart.u.items_cart[n].amount < amount)
  5672. return;
  5673. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5674. sd->cart.u.items_cart[n].amount -= amount;
  5675. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5676. if(sd->cart.u.items_cart[n].amount <= 0) {
  5677. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5678. sd->cart_num--;
  5679. }
  5680. if(!type) {
  5681. clif_cart_delitem(sd,n,amount);
  5682. clif_updatestatus(sd,SP_CARTINFO);
  5683. }
  5684. }
  5685. /*==========================================
  5686. * Transfer item from inventory to cart.
  5687. *------------------------------------------*/
  5688. void pc_putitemtocart(map_session_data *sd,int idx,int amount)
  5689. {
  5690. nullpo_retv(sd);
  5691. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5692. return;
  5693. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5694. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5695. return;
  5696. if( item_data->equipSwitch ){
  5697. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  5698. return;
  5699. }
  5700. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5701. if (flag == ADDITEM_SUCCESS)
  5702. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5703. else {
  5704. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  5705. clif_additem(sd, idx, amount, 0);
  5706. clif_delitem(sd, idx, amount, 0);
  5707. }
  5708. }
  5709. /*==========================================
  5710. * Get number of item in cart.
  5711. * Return:
  5712. -1 = itemid not found or no amount found
  5713. x = remaining itemid on cart after get
  5714. *------------------------------------------*/
  5715. int pc_cartitem_amount(map_session_data* sd, int idx, int amount)
  5716. {
  5717. struct item* item_data;
  5718. nullpo_retr(-1, sd);
  5719. item_data = &sd->cart.u.items_cart[idx];
  5720. if( item_data->nameid == 0 || item_data->amount == 0 )
  5721. return -1;
  5722. return item_data->amount - amount;
  5723. }
  5724. /*==========================================
  5725. * Retrieve an item at index idx from cart.
  5726. *------------------------------------------*/
  5727. bool pc_getitemfromcart(map_session_data *sd,int idx,int amount)
  5728. {
  5729. nullpo_retr(1, sd);
  5730. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5731. return false;
  5732. struct item *item_data=&sd->cart.u.items_cart[idx];
  5733. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5734. return false;
  5735. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5736. if (flag == ADDITEM_SUCCESS)
  5737. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5738. else {
  5739. clif_cart_delitem(sd, idx, amount);
  5740. clif_additem(sd, idx, amount, flag);
  5741. clif_cart_additem(sd, idx, amount);
  5742. }
  5743. return true;
  5744. }
  5745. /*==========================================
  5746. * Bound Item Check
  5747. * Type:
  5748. * 1 Account Bound
  5749. * 2 Guild Bound
  5750. * 3 Party Bound
  5751. * 4 Character Bound
  5752. *------------------------------------------*/
  5753. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  5754. {
  5755. int i = 0, j = 0;
  5756. for(i = 0; i < MAX_INVENTORY; i++) {
  5757. 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) {
  5758. idxlist[j] = i;
  5759. j++;
  5760. }
  5761. }
  5762. return j;
  5763. }
  5764. /*==========================================
  5765. * Display item stolen msg to player sd
  5766. *------------------------------------------*/
  5767. int pc_show_steal(struct block_list *bl,va_list ap)
  5768. {
  5769. map_session_data *sd;
  5770. t_itemid itemid;
  5771. char output[100];
  5772. sd=va_arg(ap,map_session_data *);
  5773. itemid=va_arg(ap,int);
  5774. std::shared_ptr<item_data> id = item_db.find(itemid);
  5775. if(id == nullptr)
  5776. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5777. else
  5778. sprintf(output,"%s stole %s.",sd->status.name,id->ename.c_str());
  5779. clif_displaymessage( ((map_session_data *)bl)->fd, output);
  5780. return 0;
  5781. }
  5782. /**
  5783. * Steal an item from bl (mob).
  5784. * @param sd: Player data
  5785. * @param bl: Object to steal from
  5786. * @param skill_lv: Level of skill used
  5787. * @return True on success or false otherwise
  5788. */
  5789. bool pc_steal_item(map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5790. {
  5791. int i;
  5792. t_itemid itemid;
  5793. double rate;
  5794. unsigned char flag = 0;
  5795. struct status_data *sd_status, *md_status;
  5796. struct mob_data *md;
  5797. if(!sd || !bl || bl->type!=BL_MOB)
  5798. return false;
  5799. md = (TBL_MOB *)bl;
  5800. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5801. return false;
  5802. sd_status= status_get_status_data(&sd->bl);
  5803. md_status= status_get_status_data(bl);
  5804. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5805. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5806. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5807. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5808. ) { //Can't steal from
  5809. md->state.steal_flag = UCHAR_MAX;
  5810. return false;
  5811. }
  5812. // base skill success chance (percentual)
  5813. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5814. rate += sd->bonus.add_steal_rate;
  5815. if( rate < 1
  5816. #ifdef RENEWAL
  5817. || rnd()%100 >= rate
  5818. #endif
  5819. )
  5820. return false;
  5821. // Try dropping one item, in the order from first to last possible slot.
  5822. // Droprate is affected by the skill success rate.
  5823. for( i = 0; i < MAX_MOB_DROP; i++ )
  5824. if( item_db.exists(md->db->dropitem[i].nameid) && !md->db->dropitem[i].steal_protected && rnd() % 10000 < md->db->dropitem[i].rate
  5825. #ifndef RENEWAL
  5826. * rate/100.
  5827. #endif
  5828. )
  5829. break;
  5830. if( i == MAX_MOB_DROP )
  5831. return false;
  5832. itemid = md->db->dropitem[i].nameid;
  5833. struct item tmp_item = {};
  5834. tmp_item.nameid = itemid;
  5835. tmp_item.amount = 1;
  5836. tmp_item.identify = itemdb_isidentified(itemid);
  5837. if( battle_config.skill_steal_random_options ){
  5838. mob_setdropitem_option( &tmp_item, &md->db->dropitem[i] );
  5839. }
  5840. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5841. //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?
  5842. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5843. if(flag) { //Failed to steal due to overweight
  5844. clif_additem(sd,0,0,flag);
  5845. return false;
  5846. }
  5847. if(battle_config.show_steal_in_same_party)
  5848. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5849. //Logs items, Stolen from mobs [Lupus]
  5850. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5851. //A Rare Steal Global Announce by Lupus
  5852. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5853. struct item_data *i_data;
  5854. char message[128];
  5855. i_data = itemdb_search(itemid);
  5856. 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);
  5857. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5858. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5859. }
  5860. return true;
  5861. }
  5862. /*==========================================
  5863. * Stole zeny from bl (mob)
  5864. * return
  5865. * 0 = fail
  5866. * 1 = success
  5867. *------------------------------------------*/
  5868. int pc_steal_coin(map_session_data *sd,struct block_list *target)
  5869. {
  5870. int rate, target_lv;
  5871. struct mob_data *md;
  5872. if(!sd || !target || target->type != BL_MOB)
  5873. return 0;
  5874. md = (TBL_MOB*)target;
  5875. target_lv = status_get_lv(target);
  5876. if (md->state.steal_coin_flag || md->sc.getSCE(SC_STONE) || md->sc.getSCE(SC_FREEZE) || md->sc.getSCE(SC_HANDICAPSTATE_FROSTBITE) ||
  5877. md->sc.getSCE(SC_HANDICAPSTATE_SWOONING) || md->sc.getSCE(SC_HANDICAPSTATE_LIGHTNINGSTRIKE) || md->sc.getSCE(SC_HANDICAPSTATE_CRYSTALLIZATION) ||
  5878. status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5879. return 0;
  5880. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5881. if(rnd()%1000 < rate)
  5882. {
  5883. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5884. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5885. pc_getzeny(sd, amount, LOG_TYPE_STEAL);
  5886. md->state.steal_coin_flag = 1;
  5887. return 1;
  5888. }
  5889. return 0;
  5890. }
  5891. /*==========================================
  5892. * Set's a player position.
  5893. * @param sd
  5894. * @param mapindex
  5895. * @param x
  5896. * @param y
  5897. * @param clrtype
  5898. * @return SETPOS_OK Success
  5899. * SETPOS_MAPINDEX Invalid map index
  5900. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5901. * SETPOS_AUTOTRADE Player is in autotrade state
  5902. *------------------------------------------*/
  5903. enum e_setpos pc_setpos(map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5904. {
  5905. nullpo_retr(SETPOS_OK,sd);
  5906. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5907. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5908. return SETPOS_MAPINDEX;
  5909. }
  5910. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5911. return SETPOS_AUTOTRADE;
  5912. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5913. pc_setstand(sd, true);
  5914. pc_setrestartvalue(sd,1);
  5915. }
  5916. int16 m = map_mapindex2mapid(mapindex);
  5917. struct map_data *mapdata = map_getmapdata(m);
  5918. status_change *sc = status_get_sc(&sd->bl);
  5919. sd->state.changemap = (sd->mapindex != mapindex);
  5920. sd->state.warping = 1;
  5921. sd->state.workinprogress = WIP_DISABLE_NONE;
  5922. sd->state.mail_writing = false;
  5923. sd->state.refineui_open = false;
  5924. if( sd->state.changemap ) { // Misc map-changing settings
  5925. int curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5926. if (curr_map_instance_id != new_map_instance_id) {
  5927. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5928. instance_delusers(curr_map_instance_id);
  5929. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5930. }
  5931. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5932. instance_addusers(new_map_instance_id);
  5933. }
  5934. if (sd->bg_id && mapdata && !mapdata->getMapFlag(MF_BATTLEGROUND)) // Moving to a map that isn't a Battlegrounds
  5935. bg_team_leave(sd, false, true);
  5936. sd->state.pmap = sd->bl.m;
  5937. if (sc && sc->count) { // Cancel some map related stuff.
  5938. if (sc->cant.warp)
  5939. return SETPOS_MAPINDEX; // You may not get out!
  5940. for (const auto &it : status_db) {
  5941. if (sc->getSCE(it.first)) {
  5942. if (it.second->flag[SCF_REMOVEONMAPWARP])
  5943. status_change_end(&sd->bl, static_cast<sc_type>(it.first));
  5944. if (it.second->flag[SCF_RESTARTONMAPWARP] && it.second->skill_id > 0) {
  5945. status_change_entry *sce = sd->sc.getSCE(it.first);
  5946. if (sce->timer != INVALID_TIMER)
  5947. delete_timer(sce->timer, status_change_timer);
  5948. sce->timer = add_timer(gettick() + skill_get_time(it.second->skill_id, sce->val1), status_change_timer, sd->bl.id, it.first);
  5949. }
  5950. }
  5951. }
  5952. }
  5953. for(int i = 0; i < EQI_MAX; i++ ) {
  5954. if( sd->equip_index[i] >= 0 )
  5955. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  5956. pc_unequipitem(sd,sd->equip_index[i],2);
  5957. }
  5958. }
  5959. if (battle_config.clear_unit_onwarp&BL_PC)
  5960. skill_clear_unitgroup(&sd->bl);
  5961. if( battle_config.loose_ap_on_map && mapdata_flag_vs( mapdata ) ){
  5962. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  5963. }
  5964. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5965. guild_send_dot_remove(sd);
  5966. bg_send_dot_remove(sd);
  5967. if (sd->regen.state.gc)
  5968. sd->regen.state.gc = 0;
  5969. if (mapdata) {
  5970. // make sure vending is allowed here
  5971. if (sd->state.vending && mapdata->getMapFlag(MF_NOVENDING)) {
  5972. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5973. vending_closevending(sd);
  5974. }
  5975. // make sure buyingstore is allowed here
  5976. if (sd->state.buyingstore && mapdata->getMapFlag(MF_NOBUYINGSTORE)) {
  5977. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5978. buyingstore_close(sd);
  5979. }
  5980. }
  5981. channel_pcquit(sd,4); //quit map chan
  5982. // Remove Cloaked NPCs on map change
  5983. sd->cloaked_npc.clear();
  5984. }
  5985. if( m < 0 )
  5986. {
  5987. uint32 ip;
  5988. uint16 port;
  5989. struct script_state *st;
  5990. //if can't find any map-servers, just abort setting position.
  5991. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5992. return SETPOS_NO_MAPSERVER;
  5993. if (sd->npc_id){
  5994. npc_event_dequeue(sd,false);
  5995. st = sd->st;
  5996. }else{
  5997. st = nullptr;
  5998. }
  5999. if (sd->bg_id) // Switching map servers, remove from bg
  6000. bg_team_leave(sd, false, true);
  6001. if (sd->state.vending) // Stop vending
  6002. vending_closevending(sd);
  6003. if (sd->state.buyingstore) // Stop buyingstore
  6004. buyingstore_close(sd);
  6005. npc_script_event(sd, NPCE_LOGOUT);
  6006. //remove from map, THEN change x/y coordinates
  6007. unit_remove_map_pc(sd,clrtype);
  6008. sd->mapindex = mapindex;
  6009. sd->bl.x=x;
  6010. sd->bl.y=y;
  6011. pc_clean_skilltree(sd);
  6012. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  6013. chrif_changemapserver(sd, ip, (short)port);
  6014. //Free session data from this map server [Kevin]
  6015. unit_free_pc(sd);
  6016. if( st ){
  6017. // Has to be done here, because otherwise unit_free_pc will free the stack already
  6018. st->state = END;
  6019. }
  6020. return SETPOS_OK;
  6021. }
  6022. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  6023. {
  6024. 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);
  6025. x = y = 0; // make it random
  6026. }
  6027. if( x == 0 && y == 0 ) { // pick a random walkable cell
  6028. int c=0;
  6029. do {
  6030. x = rnd()%(mapdata->xs-2)+1;
  6031. y = rnd()%(mapdata->ys-2)+1;
  6032. c++;
  6033. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  6034. 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));
  6035. return SETPOS_OK; //preventing warp
  6036. //break; //allow warp anyway
  6037. }
  6038. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  6039. }
  6040. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  6041. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  6042. vending_closevending(sd);
  6043. }
  6044. if (sd->state.buyingstore && map_getcell(m, x, y, CELL_CHKNOBUYINGSTORE)) {
  6045. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  6046. buyingstore_close(sd);
  6047. }
  6048. if(sd->bl.prev != nullptr){
  6049. unit_remove_map_pc(sd,clrtype);
  6050. clif_changemap(sd,m,x,y); // [MouseJstr]
  6051. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  6052. sd->state.rewarp = 1;
  6053. sd->mapindex = mapindex;
  6054. sd->bl.m = m;
  6055. sd->bl.x = sd->ud.to_x = x;
  6056. sd->bl.y = sd->ud.to_y = y;
  6057. if( sd->status.guild_id > 0 && mapdata->getMapFlag(MF_GVG_CASTLE) )
  6058. { // Increased guild castle regen [Valaris]
  6059. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  6060. if(gc && gc->guild_id == sd->status.guild_id)
  6061. sd->regen.state.gc = 1;
  6062. }
  6063. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  6064. {
  6065. sd->pd->bl.m = m;
  6066. sd->pd->bl.x = sd->pd->ud.to_x = x;
  6067. sd->pd->bl.y = sd->pd->ud.to_y = y;
  6068. sd->pd->ud.dir = sd->ud.dir;
  6069. }
  6070. if( hom_is_active(sd->hd) )
  6071. {
  6072. sd->hd->bl.m = m;
  6073. sd->hd->bl.x = sd->hd->ud.to_x = x;
  6074. sd->hd->bl.y = sd->hd->ud.to_y = y;
  6075. sd->hd->ud.dir = sd->ud.dir;
  6076. }
  6077. if( sd->md )
  6078. {
  6079. sd->md->bl.m = m;
  6080. sd->md->bl.x = sd->md->ud.to_x = x;
  6081. sd->md->bl.y = sd->md->ud.to_y = y;
  6082. sd->md->ud.dir = sd->ud.dir;
  6083. }
  6084. if( sd->ed ) {
  6085. sd->ed->bl.m = m;
  6086. sd->ed->bl.x = sd->ed->ud.to_x = x;
  6087. sd->ed->bl.y = sd->ed->ud.to_y = y;
  6088. sd->ed->ud.dir = sd->ud.dir;
  6089. }
  6090. pc_cell_basilica(sd);
  6091. //check if we gonna be rewarped [lighta]
  6092. if(npc_check_areanpc(1,m,x,y,0)){
  6093. sd->count_rewarp++;
  6094. }
  6095. else
  6096. sd->count_rewarp = 0;
  6097. if (sd->state.vending)
  6098. vending_update(*sd);
  6099. if (sd->state.buyingstore)
  6100. buyingstore_update(*sd);
  6101. return SETPOS_OK;
  6102. }
  6103. enum e_setpos pc_setpos_savepoint( map_session_data& sd, clr_type clrtype ){
  6104. struct map_data *mapdata = map_getmapdata( sd.bl.m );
  6105. if( mapdata != nullptr && mapdata->getMapFlag(MF_NOSAVE) && mapdata->save.map ){
  6106. return pc_setpos( &sd, mapdata->save.map, mapdata->save.x, mapdata->save.y, clrtype );
  6107. }else{
  6108. return pc_setpos( &sd, mapindex_name2id( sd.status.save_point.map ), sd.status.save_point.x, sd.status.save_point.y, clrtype );
  6109. }
  6110. }
  6111. /*==========================================
  6112. * Warp player sd to random location on current map.
  6113. * May fail if no walkable cell found (1000 attempts).
  6114. * Return:
  6115. * 0 = Success
  6116. * 1,2,3 = Fail
  6117. *------------------------------------------*/
  6118. char pc_randomwarp(map_session_data *sd, clr_type type, bool ignore_mapflag)
  6119. {
  6120. int x,y,i=0;
  6121. nullpo_ret(sd);
  6122. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6123. if (mapdata->getMapFlag(MF_NOTELEPORT) && !ignore_mapflag) //Teleport forbidden
  6124. return 3;
  6125. do {
  6126. x = rnd()%(mapdata->xs-2)+1;
  6127. y = rnd()%(mapdata->ys-2)+1;
  6128. } 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);
  6129. if (i < 1000)
  6130. return pc_setpos(sd,mapdata->index,x,y,type);
  6131. return 3;
  6132. }
  6133. /*==========================================
  6134. * Records a memo point at sd's current position
  6135. * pos - entry to replace, (-1: shift oldest entry out)
  6136. *------------------------------------------*/
  6137. bool pc_memo(map_session_data* sd, int pos)
  6138. {
  6139. int skill;
  6140. nullpo_ret(sd);
  6141. // check mapflags
  6142. 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) ) {
  6143. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  6144. return false;
  6145. }
  6146. // check inputs
  6147. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  6148. return false; // invalid input
  6149. // check required skill level
  6150. skill = pc_checkskill(sd, AL_WARP);
  6151. if( skill < 1 ) {
  6152. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  6153. return false;
  6154. }
  6155. if( skill < 2 || skill - 2 < pos ) {
  6156. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  6157. return false;
  6158. }
  6159. if( pos == -1 )
  6160. {
  6161. uint8 i;
  6162. const char* mapname = map_mapid2mapname( sd->bl.m );
  6163. // prevent memo-ing the same map multiple times
  6164. ARR_FIND( 0, MAX_MEMOPOINTS, i, strncmp( sd->status.memo_point[i].map, mapname, sizeof( sd->status.memo_point[i].map ) ) == 0 );
  6165. memmove( &sd->status.memo_point[1], &sd->status.memo_point[0], ( u8min( i, MAX_MEMOPOINTS - 1 ) ) * sizeof( struct s_point_str ) );
  6166. pos = 0;
  6167. }
  6168. if( map_getmapdata(sd->bl.m)->instance_id ) {
  6169. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  6170. return false;
  6171. }
  6172. safestrncpy( sd->status.memo_point[pos].map, map_mapid2mapname( sd->bl.m ), sizeof( sd->status.memo_point[pos].map ) );
  6173. sd->status.memo_point[pos].x = sd->bl.x;
  6174. sd->status.memo_point[pos].y = sd->bl.y;
  6175. clif_skill_memomessage(sd, 0);
  6176. return true;
  6177. }
  6178. //
  6179. // Skills
  6180. //
  6181. /**
  6182. * Get the skill current cooldown for player.
  6183. * (get the db base cooldown for skill + player specific cooldown)
  6184. * @param sd : player pointer
  6185. * @param id : skill id
  6186. * @param lv : skill lv
  6187. * @return player skill cooldown
  6188. */
  6189. int pc_get_skillcooldown(map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  6190. if (skill_id == SJ_NOVAEXPLOSING) {
  6191. status_change *sc = status_get_sc(&sd->bl);
  6192. if (sc && sc->getSCE(SC_DIMENSION))
  6193. return 0;
  6194. }
  6195. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  6196. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  6197. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  6198. for (auto &it : sd->skillcooldown) {
  6199. if (it.id == skill_id) {
  6200. cooldown += it.val;
  6201. break;
  6202. }
  6203. }
  6204. return max(0, cooldown);
  6205. }
  6206. /*==========================================
  6207. * Return player sd skill_lv learned for given skill
  6208. *------------------------------------------*/
  6209. uint8 pc_checkskill(map_session_data *sd, uint16 skill_id)
  6210. {
  6211. uint16 idx = 0;
  6212. if (sd == nullptr)
  6213. return 0;
  6214. #ifdef RENEWAL
  6215. if ((idx = skill_get_index(skill_id)) == 0) {
  6216. #else
  6217. if( ( idx = skill_db.get_index( skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__ ) ) == 0 ){
  6218. if( skill_id >= RK_ENCHANTBLADE ){
  6219. // Silently fail for now -> future update planned
  6220. return 0;
  6221. }
  6222. #endif
  6223. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  6224. return 0;
  6225. }
  6226. if (SKILL_CHK_GUILD(skill_id) ) {
  6227. if (sd->status.guild_id>0 && sd->guild)
  6228. return guild_checkskill(sd->guild->guild,skill_id);
  6229. return 0;
  6230. }
  6231. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  6232. }
  6233. /**
  6234. * Returns the flag of the given skill (when learned).
  6235. * @param sd: Player data
  6236. * @param skill_id: Skill to lookup
  6237. * @return Skill flag type
  6238. */
  6239. e_skill_flag pc_checkskill_flag(map_session_data &sd, uint16 skill_id) {
  6240. uint16 idx;
  6241. #ifdef RENEWAL
  6242. if ((idx = skill_get_index(skill_id)) == 0) {
  6243. #else
  6244. if ((idx = skill_db.get_index(skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__)) == 0) {
  6245. if (skill_id >= RK_ENCHANTBLADE) {
  6246. // Silently fail for now -> future update planned
  6247. return SKILL_FLAG_NONE;
  6248. }
  6249. #endif
  6250. ShowError("pc_checkskill_flag: Invalid skill id %d (char_id=%d).\n", skill_id, sd.status.char_id);
  6251. return SKILL_FLAG_NONE;
  6252. }
  6253. 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;
  6254. }
  6255. /**
  6256. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  6257. * @param sd: Player data
  6258. * @param type: Summoner Power Type
  6259. * @return Skill points invested
  6260. */
  6261. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  6262. if (sd == nullptr)
  6263. return 0;
  6264. uint8 count = 0;
  6265. switch (type) {
  6266. case SUMMONER_POWER_SEA:
  6267. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  6268. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  6269. break;
  6270. case SUMMONER_POWER_LAND:
  6271. 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) +
  6272. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  6273. break;
  6274. case SUMMONER_POWER_LIFE:
  6275. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  6276. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  6277. break;
  6278. }
  6279. return count;
  6280. }
  6281. /**
  6282. * Checks for Imperial Guard's passive skills.
  6283. * @param sd: Player data
  6284. * @param flag:
  6285. * Flag&1 = IG_SHIELD_MASTERY
  6286. * Flag&2 = IG_SPEAR_SWORD_M
  6287. */
  6288. uint8 pc_checkskill_imperial_guard(map_session_data *sd, short flag)
  6289. {
  6290. nullpo_retr(0, sd);
  6291. uint8 count = 0;
  6292. if (flag&1 && sd->status.shield > 0)
  6293. count += pc_checkskill(sd, IG_SHIELD_MASTERY);
  6294. if (flag&2 && (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_1HSPEAR || sd->status.weapon == W_2HSPEAR))
  6295. count += pc_checkskill(sd, IG_SPEAR_SWORD_M);
  6296. return count;
  6297. }
  6298. /**
  6299. * Check if we still have the correct weapon to continue the skill (actually status)
  6300. * If not ending it
  6301. * @param sd
  6302. * @return 0:error, 1:check done
  6303. */
  6304. static void pc_checkallowskill(map_session_data *sd)
  6305. {
  6306. nullpo_retv(sd);
  6307. if(!sd->sc.count)
  6308. return;
  6309. for (const auto &it : status_db) {
  6310. sc_type status = it.second->type;
  6311. std::bitset<SCF_MAX> flag = it.second->flag;
  6312. if (flag[SCF_REQUIREWEAPON]) { // Skills requiring specific weapon types
  6313. if (status == SC_DANCING && !battle_config.dancing_weaponswitch_fix)
  6314. continue;
  6315. if (sd->sc.getSCE(status) && !pc_check_weapontype(sd, skill_get_weapontype(it.second->skill_id)))
  6316. status_change_end(&sd->bl, status);
  6317. }
  6318. if (flag[SCF_REQUIRENOWEAPON]) {
  6319. if (sd->sc.getSCE(status) && sd->status.weapon)
  6320. status_change_end(&sd->bl, status, INVALID_TIMER);
  6321. }
  6322. if (flag[SCF_REQUIRESHIELD]) { // Skills requiring a shield
  6323. if (sd->sc.getSCE(status) && sd->status.shield <= 0)
  6324. status_change_end(&sd->bl, status);
  6325. }
  6326. }
  6327. }
  6328. /*==========================================
  6329. * Return equipped index of item on player sd at pos
  6330. * Return
  6331. * -1 : Nothing equipped
  6332. * idx : (this index could be used in inventory to found item_data)
  6333. *------------------------------------------*/
  6334. short pc_checkequip(map_session_data *sd,int pos, bool checkall)
  6335. {
  6336. uint8 i;
  6337. nullpo_retr(-1, sd);
  6338. for(i=0;i<EQI_MAX;i++){
  6339. if(pos & equip_bitmask[i]){
  6340. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  6341. // Check all if any match is found
  6342. continue;
  6343. }
  6344. return sd->equip_index[i];
  6345. }
  6346. }
  6347. return -1;
  6348. }
  6349. /*==========================================
  6350. * Check if sd has nameid equipped somewhere
  6351. * @sd : the player session
  6352. * @nameid : id of the item to check
  6353. * @min : : see pc.hpp enum equip_index from ? to @max
  6354. * @max : see pc.hpp enum equip_index for @min to ?
  6355. * -return true,false
  6356. *------------------------------------------*/
  6357. bool pc_checkequip2(map_session_data *sd, t_itemid nameid, int min, int max)
  6358. {
  6359. int i;
  6360. for(i = min; i < max; i++) {
  6361. if(equip_bitmask[i]) {
  6362. int idx = sd->equip_index[i];
  6363. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  6364. return true;
  6365. }
  6366. }
  6367. return false;
  6368. }
  6369. /*==========================================
  6370. * Convert's from the client's lame Job ID system
  6371. * to the map server's 'makes sense' system. [Skotlex]
  6372. *------------------------------------------*/
  6373. uint64 pc_jobid2mapid(unsigned short b_class)
  6374. {
  6375. switch(b_class)
  6376. {
  6377. //Novice And 1-1 Jobs
  6378. case JOB_NOVICE: return MAPID_NOVICE;
  6379. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  6380. case JOB_MAGE: return MAPID_MAGE;
  6381. case JOB_ARCHER: return MAPID_ARCHER;
  6382. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  6383. case JOB_MERCHANT: return MAPID_MERCHANT;
  6384. case JOB_THIEF: return MAPID_THIEF;
  6385. case JOB_TAEKWON: return MAPID_TAEKWON;
  6386. case JOB_WEDDING: return MAPID_WEDDING;
  6387. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  6388. case JOB_NINJA: return MAPID_NINJA;
  6389. case JOB_XMAS: return MAPID_XMAS;
  6390. case JOB_SUMMER: return MAPID_SUMMER;
  6391. case JOB_HANBOK: return MAPID_HANBOK;
  6392. case JOB_GANGSI: return MAPID_GANGSI;
  6393. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  6394. case JOB_SUMMER2: return MAPID_SUMMER2;
  6395. //2-1 Jobs
  6396. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  6397. case JOB_KNIGHT: return MAPID_KNIGHT;
  6398. case JOB_WIZARD: return MAPID_WIZARD;
  6399. case JOB_HUNTER: return MAPID_HUNTER;
  6400. case JOB_PRIEST: return MAPID_PRIEST;
  6401. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  6402. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  6403. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  6404. case JOB_KAGEROU:
  6405. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  6406. case JOB_REBELLION: return MAPID_REBELLION;
  6407. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  6408. //2-2 Jobs
  6409. case JOB_CRUSADER: return MAPID_CRUSADER;
  6410. case JOB_SAGE: return MAPID_SAGE;
  6411. case JOB_BARD:
  6412. case JOB_DANCER: return MAPID_BARDDANCER;
  6413. case JOB_MONK: return MAPID_MONK;
  6414. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  6415. case JOB_ROGUE: return MAPID_ROGUE;
  6416. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  6417. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  6418. //Trans Novice And Trans 1-1 Jobs
  6419. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  6420. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  6421. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  6422. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  6423. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  6424. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  6425. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  6426. //Trans 2-1 Jobs
  6427. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  6428. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  6429. case JOB_SNIPER: return MAPID_SNIPER;
  6430. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  6431. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  6432. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  6433. //Trans 2-2 Jobs
  6434. case JOB_PALADIN: return MAPID_PALADIN;
  6435. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  6436. case JOB_CLOWN:
  6437. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  6438. case JOB_CHAMPION: return MAPID_CHAMPION;
  6439. case JOB_CREATOR: return MAPID_CREATOR;
  6440. case JOB_STALKER: return MAPID_STALKER;
  6441. //Baby Novice And Baby 1-1 Jobs
  6442. case JOB_BABY: return MAPID_BABY;
  6443. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  6444. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  6445. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  6446. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  6447. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  6448. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  6449. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  6450. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  6451. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  6452. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  6453. //Baby 2-1 Jobs
  6454. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  6455. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  6456. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  6457. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  6458. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  6459. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  6460. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  6461. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  6462. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  6463. case JOB_BABY_KAGEROU:
  6464. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  6465. //Baby 2-2 Jobs
  6466. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  6467. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  6468. case JOB_BABY_BARD:
  6469. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  6470. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  6471. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  6472. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  6473. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  6474. //3-1 Jobs
  6475. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  6476. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  6477. case JOB_WARLOCK: return MAPID_WARLOCK;
  6478. case JOB_RANGER: return MAPID_RANGER;
  6479. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  6480. case JOB_MECHANIC: return MAPID_MECHANIC;
  6481. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  6482. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  6483. //3-2 Jobs
  6484. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  6485. case JOB_SORCERER: return MAPID_SORCERER;
  6486. case JOB_MINSTREL:
  6487. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  6488. case JOB_SURA: return MAPID_SURA;
  6489. case JOB_GENETIC: return MAPID_GENETIC;
  6490. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  6491. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  6492. //Trans 3-1 Jobs
  6493. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  6494. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  6495. case JOB_RANGER_T: return MAPID_RANGER_T;
  6496. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  6497. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  6498. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  6499. //Trans 3-2 Jobs
  6500. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  6501. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  6502. case JOB_MINSTREL_T:
  6503. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  6504. case JOB_SURA_T: return MAPID_SURA_T;
  6505. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  6506. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  6507. //Baby 3-1 Jobs
  6508. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  6509. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  6510. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  6511. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  6512. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  6513. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  6514. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  6515. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  6516. //Baby 3-2 Jobs
  6517. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  6518. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  6519. case JOB_BABY_MINSTREL:
  6520. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  6521. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  6522. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  6523. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  6524. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  6525. //Doram Jobs
  6526. case JOB_SUMMONER: return MAPID_SUMMONER;
  6527. case JOB_SPIRIT_HANDLER: return MAPID_SPIRIT_HANDLER;
  6528. //4-1 Jobs
  6529. case JOB_HYPER_NOVICE: return MAPID_HYPER_NOVICE;
  6530. case JOB_DRAGON_KNIGHT: return MAPID_DRAGON_KNIGHT;
  6531. case JOB_ARCH_MAGE: return MAPID_ARCH_MAGE;
  6532. case JOB_WINDHAWK: return MAPID_WINDHAWK;
  6533. case JOB_CARDINAL: return MAPID_CARDINAL;
  6534. case JOB_MEISTER: return MAPID_MEISTER;
  6535. case JOB_SHADOW_CROSS: return MAPID_SHADOW_CROSS;
  6536. case JOB_SKY_EMPEROR: return MAPID_SKY_EMPEROR;
  6537. case JOB_NIGHT_WATCH: return MAPID_NIGHT_WATCH;
  6538. case JOB_SHINKIRO:
  6539. case JOB_SHIRANUI: return MAPID_SHINKIRO_SHIRANUI;
  6540. //4-2 Jobs
  6541. case JOB_IMPERIAL_GUARD: return MAPID_IMPERIAL_GUARD;
  6542. case JOB_ELEMENTAL_MASTER: return MAPID_ELEMENTAL_MASTER;
  6543. case JOB_INQUISITOR: return MAPID_INQUISITOR;
  6544. case JOB_TROUBADOUR:
  6545. case JOB_TROUVERE: return MAPID_TROUBADOURTROUVERE;
  6546. case JOB_BIOLO: return MAPID_BIOLO;
  6547. case JOB_ABYSS_CHASER: return MAPID_ABYSS_CHASER;
  6548. case JOB_SOUL_ASCETIC: return MAPID_SOUL_ASCETIC;
  6549. //Unknown
  6550. default:
  6551. return -1;
  6552. }
  6553. }
  6554. //Reverts the map-style class id to the client-style one.
  6555. int pc_mapid2jobid(uint64 class_, int sex)
  6556. {
  6557. switch(class_) {
  6558. //Novice And 1-1 Jobs
  6559. case MAPID_NOVICE: return JOB_NOVICE;
  6560. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  6561. case MAPID_MAGE: return JOB_MAGE;
  6562. case MAPID_ARCHER: return JOB_ARCHER;
  6563. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  6564. case MAPID_MERCHANT: return JOB_MERCHANT;
  6565. case MAPID_THIEF: return JOB_THIEF;
  6566. case MAPID_TAEKWON: return JOB_TAEKWON;
  6567. case MAPID_WEDDING: return JOB_WEDDING;
  6568. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  6569. case MAPID_NINJA: return JOB_NINJA;
  6570. case MAPID_XMAS: return JOB_XMAS;
  6571. case MAPID_SUMMER: return JOB_SUMMER;
  6572. case MAPID_HANBOK: return JOB_HANBOK;
  6573. case MAPID_GANGSI: return JOB_GANGSI;
  6574. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  6575. case MAPID_SUMMER2: return JOB_SUMMER2;
  6576. //2-1 Jobs
  6577. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  6578. case MAPID_KNIGHT: return JOB_KNIGHT;
  6579. case MAPID_WIZARD: return JOB_WIZARD;
  6580. case MAPID_HUNTER: return JOB_HUNTER;
  6581. case MAPID_PRIEST: return JOB_PRIEST;
  6582. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  6583. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  6584. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  6585. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  6586. case MAPID_REBELLION: return JOB_REBELLION;
  6587. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  6588. //2-2 Jobs
  6589. case MAPID_CRUSADER: return JOB_CRUSADER;
  6590. case MAPID_SAGE: return JOB_SAGE;
  6591. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  6592. case MAPID_MONK: return JOB_MONK;
  6593. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  6594. case MAPID_ROGUE: return JOB_ROGUE;
  6595. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  6596. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  6597. //Trans Novice And Trans 2-1 Jobs
  6598. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  6599. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  6600. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  6601. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  6602. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  6603. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  6604. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  6605. //Trans 2-1 Jobs
  6606. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  6607. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  6608. case MAPID_SNIPER: return JOB_SNIPER;
  6609. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  6610. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  6611. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  6612. //Trans 2-2 Jobs
  6613. case MAPID_PALADIN: return JOB_PALADIN;
  6614. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  6615. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  6616. case MAPID_CHAMPION: return JOB_CHAMPION;
  6617. case MAPID_CREATOR: return JOB_CREATOR;
  6618. case MAPID_STALKER: return JOB_STALKER;
  6619. //Baby Novice And Baby 1-1 Jobs
  6620. case MAPID_BABY: return JOB_BABY;
  6621. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  6622. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  6623. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  6624. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  6625. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  6626. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  6627. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  6628. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  6629. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  6630. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  6631. //Baby 2-1 Jobs
  6632. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  6633. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  6634. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  6635. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  6636. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  6637. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  6638. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  6639. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  6640. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  6641. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  6642. //Baby 2-2 Jobs
  6643. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  6644. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  6645. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  6646. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  6647. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  6648. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  6649. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  6650. //3-1 Jobs
  6651. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  6652. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  6653. case MAPID_WARLOCK: return JOB_WARLOCK;
  6654. case MAPID_RANGER: return JOB_RANGER;
  6655. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  6656. case MAPID_MECHANIC: return JOB_MECHANIC;
  6657. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  6658. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  6659. //3-2 Jobs
  6660. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  6661. case MAPID_SORCERER: return JOB_SORCERER;
  6662. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  6663. case MAPID_SURA: return JOB_SURA;
  6664. case MAPID_GENETIC: return JOB_GENETIC;
  6665. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  6666. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  6667. //Trans 3-1 Jobs
  6668. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  6669. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  6670. case MAPID_RANGER_T: return JOB_RANGER_T;
  6671. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  6672. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  6673. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  6674. //Trans 3-2 Jobs
  6675. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  6676. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  6677. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  6678. case MAPID_SURA_T: return JOB_SURA_T;
  6679. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  6680. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  6681. //Baby 3-1 Jobs
  6682. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  6683. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  6684. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  6685. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  6686. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  6687. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  6688. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  6689. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  6690. //Baby 3-2 Jobs
  6691. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  6692. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  6693. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  6694. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  6695. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  6696. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  6697. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  6698. //Doram Jobs
  6699. case MAPID_SUMMONER: return JOB_SUMMONER;
  6700. case MAPID_SPIRIT_HANDLER: return JOB_SPIRIT_HANDLER;
  6701. //4-1 Jobs
  6702. case MAPID_HYPER_NOVICE: return JOB_HYPER_NOVICE;
  6703. case MAPID_DRAGON_KNIGHT: return JOB_DRAGON_KNIGHT;
  6704. case MAPID_ARCH_MAGE: return JOB_ARCH_MAGE;
  6705. case MAPID_WINDHAWK: return JOB_WINDHAWK;
  6706. case MAPID_CARDINAL: return JOB_CARDINAL;
  6707. case MAPID_MEISTER: return JOB_MEISTER;
  6708. case MAPID_SHADOW_CROSS: return JOB_SHADOW_CROSS;
  6709. case MAPID_SKY_EMPEROR: return JOB_SKY_EMPEROR;
  6710. case MAPID_NIGHT_WATCH: return JOB_NIGHT_WATCH;
  6711. case MAPID_SHINKIRO_SHIRANUI: return sex?JOB_SHINKIRO:JOB_SHIRANUI;
  6712. //4-2 Jobs
  6713. case MAPID_IMPERIAL_GUARD: return JOB_IMPERIAL_GUARD;
  6714. case MAPID_ELEMENTAL_MASTER: return JOB_ELEMENTAL_MASTER;
  6715. case MAPID_INQUISITOR: return JOB_INQUISITOR;
  6716. case MAPID_TROUBADOURTROUVERE: return sex?JOB_TROUBADOUR:JOB_TROUVERE;
  6717. case MAPID_BIOLO: return JOB_BIOLO;
  6718. case MAPID_ABYSS_CHASER: return JOB_ABYSS_CHASER;
  6719. case MAPID_SOUL_ASCETIC: return JOB_SOUL_ASCETIC;
  6720. //Unknown
  6721. default:
  6722. return -1;
  6723. }
  6724. }
  6725. /*====================================================
  6726. * This function return the name of the job (by [Yor])
  6727. *----------------------------------------------------*/
  6728. const char* job_name(int class_)
  6729. {
  6730. switch (class_) {
  6731. case JOB_NOVICE:
  6732. case JOB_SWORDMAN:
  6733. case JOB_MAGE:
  6734. case JOB_ARCHER:
  6735. case JOB_ACOLYTE:
  6736. case JOB_MERCHANT:
  6737. case JOB_THIEF:
  6738. return msg_txt(nullptr,550 - JOB_NOVICE+class_);
  6739. case JOB_KNIGHT:
  6740. case JOB_PRIEST:
  6741. case JOB_WIZARD:
  6742. case JOB_BLACKSMITH:
  6743. case JOB_HUNTER:
  6744. case JOB_ASSASSIN:
  6745. return msg_txt(nullptr,557 - JOB_KNIGHT+class_);
  6746. case JOB_KNIGHT2:
  6747. return msg_txt(nullptr,557);
  6748. case JOB_CRUSADER:
  6749. case JOB_MONK:
  6750. case JOB_SAGE:
  6751. case JOB_ROGUE:
  6752. case JOB_ALCHEMIST:
  6753. case JOB_BARD:
  6754. case JOB_DANCER:
  6755. return msg_txt(nullptr,563 - JOB_CRUSADER+class_);
  6756. case JOB_CRUSADER2:
  6757. return msg_txt(nullptr,563);
  6758. case JOB_WEDDING:
  6759. case JOB_SUPER_NOVICE:
  6760. case JOB_GUNSLINGER:
  6761. case JOB_NINJA:
  6762. case JOB_XMAS:
  6763. return msg_txt(nullptr,570 - JOB_WEDDING+class_);
  6764. case JOB_SUMMER:
  6765. case JOB_SUMMER2:
  6766. return msg_txt(nullptr,621);
  6767. case JOB_HANBOK:
  6768. return msg_txt(nullptr,694);
  6769. case JOB_OKTOBERFEST:
  6770. return msg_txt(nullptr,696);
  6771. case JOB_NOVICE_HIGH:
  6772. case JOB_SWORDMAN_HIGH:
  6773. case JOB_MAGE_HIGH:
  6774. case JOB_ARCHER_HIGH:
  6775. case JOB_ACOLYTE_HIGH:
  6776. case JOB_MERCHANT_HIGH:
  6777. case JOB_THIEF_HIGH:
  6778. return msg_txt(nullptr,575 - JOB_NOVICE_HIGH+class_);
  6779. case JOB_LORD_KNIGHT:
  6780. case JOB_HIGH_PRIEST:
  6781. case JOB_HIGH_WIZARD:
  6782. case JOB_WHITESMITH:
  6783. case JOB_SNIPER:
  6784. case JOB_ASSASSIN_CROSS:
  6785. return msg_txt(nullptr,582 - JOB_LORD_KNIGHT+class_);
  6786. case JOB_LORD_KNIGHT2:
  6787. return msg_txt(nullptr,582);
  6788. case JOB_PALADIN:
  6789. case JOB_CHAMPION:
  6790. case JOB_PROFESSOR:
  6791. case JOB_STALKER:
  6792. case JOB_CREATOR:
  6793. case JOB_CLOWN:
  6794. case JOB_GYPSY:
  6795. return msg_txt(nullptr,588 - JOB_PALADIN + class_);
  6796. case JOB_PALADIN2:
  6797. return msg_txt(nullptr,588);
  6798. case JOB_BABY:
  6799. case JOB_BABY_SWORDMAN:
  6800. case JOB_BABY_MAGE:
  6801. case JOB_BABY_ARCHER:
  6802. case JOB_BABY_ACOLYTE:
  6803. case JOB_BABY_MERCHANT:
  6804. case JOB_BABY_THIEF:
  6805. return msg_txt(nullptr,595 - JOB_BABY + class_);
  6806. case JOB_BABY_KNIGHT:
  6807. case JOB_BABY_PRIEST:
  6808. case JOB_BABY_WIZARD:
  6809. case JOB_BABY_BLACKSMITH:
  6810. case JOB_BABY_HUNTER:
  6811. case JOB_BABY_ASSASSIN:
  6812. return msg_txt(nullptr,602 - JOB_BABY_KNIGHT + class_);
  6813. case JOB_BABY_KNIGHT2:
  6814. return msg_txt(nullptr,602);
  6815. case JOB_BABY_CRUSADER:
  6816. case JOB_BABY_MONK:
  6817. case JOB_BABY_SAGE:
  6818. case JOB_BABY_ROGUE:
  6819. case JOB_BABY_ALCHEMIST:
  6820. case JOB_BABY_BARD:
  6821. case JOB_BABY_DANCER:
  6822. return msg_txt(nullptr,608 - JOB_BABY_CRUSADER + class_);
  6823. case JOB_BABY_CRUSADER2:
  6824. return msg_txt(nullptr,608);
  6825. case JOB_SUPER_BABY:
  6826. return msg_txt(nullptr,615);
  6827. case JOB_TAEKWON:
  6828. return msg_txt(nullptr,616);
  6829. case JOB_STAR_GLADIATOR:
  6830. case JOB_STAR_GLADIATOR2:
  6831. return msg_txt(nullptr,617);
  6832. case JOB_SOUL_LINKER:
  6833. return msg_txt(nullptr,618);
  6834. case JOB_GANGSI:
  6835. case JOB_DEATH_KNIGHT:
  6836. case JOB_DARK_COLLECTOR:
  6837. return msg_txt(nullptr,622 - JOB_GANGSI+class_);
  6838. case JOB_RUNE_KNIGHT:
  6839. case JOB_WARLOCK:
  6840. case JOB_RANGER:
  6841. case JOB_ARCH_BISHOP:
  6842. case JOB_MECHANIC:
  6843. case JOB_GUILLOTINE_CROSS:
  6844. return msg_txt(nullptr,625 - JOB_RUNE_KNIGHT+class_);
  6845. case JOB_RUNE_KNIGHT_T:
  6846. case JOB_WARLOCK_T:
  6847. case JOB_RANGER_T:
  6848. case JOB_ARCH_BISHOP_T:
  6849. case JOB_MECHANIC_T:
  6850. case JOB_GUILLOTINE_CROSS_T:
  6851. return msg_txt(nullptr,681 - JOB_RUNE_KNIGHT_T+class_);
  6852. case JOB_ROYAL_GUARD:
  6853. case JOB_SORCERER:
  6854. case JOB_MINSTREL:
  6855. case JOB_WANDERER:
  6856. case JOB_SURA:
  6857. case JOB_GENETIC:
  6858. case JOB_SHADOW_CHASER:
  6859. return msg_txt(nullptr,631 - JOB_ROYAL_GUARD+class_);
  6860. case JOB_ROYAL_GUARD_T:
  6861. case JOB_SORCERER_T:
  6862. case JOB_MINSTREL_T:
  6863. case JOB_WANDERER_T:
  6864. case JOB_SURA_T:
  6865. case JOB_GENETIC_T:
  6866. case JOB_SHADOW_CHASER_T:
  6867. return msg_txt(nullptr,687 - JOB_ROYAL_GUARD_T+class_);
  6868. case JOB_RUNE_KNIGHT2:
  6869. case JOB_RUNE_KNIGHT_T2:
  6870. return msg_txt(nullptr,625);
  6871. case JOB_ROYAL_GUARD2:
  6872. case JOB_ROYAL_GUARD_T2:
  6873. return msg_txt(nullptr,631);
  6874. case JOB_RANGER2:
  6875. case JOB_RANGER_T2:
  6876. return msg_txt(nullptr,627);
  6877. case JOB_MECHANIC2:
  6878. case JOB_MECHANIC_T2:
  6879. return msg_txt(nullptr,629);
  6880. case JOB_BABY_RUNE_KNIGHT:
  6881. case JOB_BABY_WARLOCK:
  6882. case JOB_BABY_RANGER:
  6883. case JOB_BABY_ARCH_BISHOP:
  6884. case JOB_BABY_MECHANIC:
  6885. case JOB_BABY_GUILLOTINE_CROSS:
  6886. case JOB_BABY_ROYAL_GUARD:
  6887. case JOB_BABY_SORCERER:
  6888. case JOB_BABY_MINSTREL:
  6889. case JOB_BABY_WANDERER:
  6890. case JOB_BABY_SURA:
  6891. case JOB_BABY_GENETIC:
  6892. case JOB_BABY_SHADOW_CHASER:
  6893. return msg_txt(nullptr,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6894. case JOB_BABY_RUNE_KNIGHT2:
  6895. return msg_txt(nullptr,638);
  6896. case JOB_BABY_ROYAL_GUARD2:
  6897. return msg_txt(nullptr,644);
  6898. case JOB_BABY_RANGER2:
  6899. return msg_txt(nullptr,640);
  6900. case JOB_BABY_MECHANIC2:
  6901. return msg_txt(nullptr,642);
  6902. case JOB_SUPER_NOVICE_E:
  6903. case JOB_SUPER_BABY_E:
  6904. return msg_txt(nullptr,651 - JOB_SUPER_NOVICE_E+class_);
  6905. case JOB_KAGEROU:
  6906. case JOB_OBORO:
  6907. return msg_txt(nullptr,653 - JOB_KAGEROU+class_);
  6908. case JOB_REBELLION:
  6909. return msg_txt(nullptr,695);
  6910. case JOB_SUMMONER:
  6911. return msg_txt(nullptr,697);
  6912. case JOB_BABY_SUMMONER:
  6913. return msg_txt(nullptr,698);
  6914. case JOB_BABY_NINJA:
  6915. return msg_txt(nullptr,699);
  6916. case JOB_BABY_KAGEROU:
  6917. case JOB_BABY_OBORO:
  6918. case JOB_BABY_TAEKWON:
  6919. case JOB_BABY_STAR_GLADIATOR:
  6920. case JOB_BABY_SOUL_LINKER:
  6921. case JOB_BABY_GUNSLINGER:
  6922. case JOB_BABY_REBELLION:
  6923. return msg_txt(nullptr,753 - JOB_BABY_KAGEROU+class_);
  6924. case JOB_BABY_STAR_GLADIATOR2:
  6925. return msg_txt(nullptr,756);
  6926. case JOB_STAR_EMPEROR:
  6927. case JOB_SOUL_REAPER:
  6928. case JOB_BABY_STAR_EMPEROR:
  6929. case JOB_BABY_SOUL_REAPER:
  6930. return msg_txt(nullptr,782 - JOB_STAR_EMPEROR + class_);
  6931. case JOB_STAR_EMPEROR2:
  6932. return msg_txt(nullptr,782);
  6933. case JOB_BABY_STAR_EMPEROR2:
  6934. return msg_txt(nullptr,784);
  6935. case JOB_DRAGON_KNIGHT:
  6936. case JOB_MEISTER:
  6937. case JOB_SHADOW_CROSS:
  6938. case JOB_ARCH_MAGE:
  6939. case JOB_CARDINAL:
  6940. case JOB_WINDHAWK:
  6941. case JOB_IMPERIAL_GUARD:
  6942. case JOB_BIOLO:
  6943. case JOB_ABYSS_CHASER:
  6944. case JOB_ELEMENTAL_MASTER:
  6945. case JOB_INQUISITOR:
  6946. case JOB_TROUBADOUR:
  6947. case JOB_TROUVERE:
  6948. return msg_txt( nullptr, 800 - JOB_DRAGON_KNIGHT + class_ );
  6949. case JOB_WINDHAWK2:
  6950. return msg_txt( nullptr, 805);
  6951. case JOB_MEISTER2:
  6952. return msg_txt( nullptr, 801 );
  6953. case JOB_DRAGON_KNIGHT2:
  6954. return msg_txt( nullptr, 800 );
  6955. case JOB_IMPERIAL_GUARD2:
  6956. return msg_txt( nullptr, 806 );
  6957. case JOB_SKY_EMPEROR:
  6958. case JOB_SOUL_ASCETIC:
  6959. case JOB_SHINKIRO:
  6960. case JOB_SHIRANUI:
  6961. case JOB_NIGHT_WATCH:
  6962. case JOB_HYPER_NOVICE:
  6963. case JOB_SPIRIT_HANDLER:
  6964. return msg_txt( nullptr, 813 - JOB_SKY_EMPEROR + class_ );
  6965. case JOB_SKY_EMPEROR2:
  6966. return msg_txt( nullptr, 813 );
  6967. default:
  6968. return msg_txt(nullptr,655);
  6969. }
  6970. }
  6971. /*====================================================
  6972. * Timered function to make id follow a target.
  6973. * @id = bl.id (player only atm)
  6974. * target is define in sd->followtarget (bl.id)
  6975. * used by pc_follow
  6976. *----------------------------------------------------*/
  6977. TIMER_FUNC(pc_follow_timer){
  6978. map_session_data *sd;
  6979. struct block_list *tbl;
  6980. sd = map_id2sd(id);
  6981. nullpo_ret(sd);
  6982. if (sd->followtimer != tid){
  6983. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6984. sd->followtimer = INVALID_TIMER;
  6985. return 0;
  6986. }
  6987. sd->followtimer = INVALID_TIMER;
  6988. tbl = map_id2bl(sd->followtarget);
  6989. if (tbl == nullptr || pc_isdead(sd))
  6990. {
  6991. pc_stop_following(sd);
  6992. return 0;
  6993. }
  6994. // either player or target is currently detached from map blocks (could be teleporting),
  6995. // but still connected to this map, so we'll just increment the timer and check back later
  6996. if (sd->bl.prev != nullptr && tbl->prev != nullptr &&
  6997. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6998. {
  6999. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, nullptr, nullptr)) {
  7000. if (!check_distance_bl(&sd->bl, tbl, 5))
  7001. unit_walktobl(&sd->bl, tbl, 5, 0);
  7002. } else
  7003. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  7004. }
  7005. sd->followtimer = add_timer(
  7006. tick + 1000, // increase time a bit to loosen up map's load
  7007. pc_follow_timer, sd->bl.id, 0);
  7008. return 0;
  7009. }
  7010. int pc_stop_following (map_session_data *sd)
  7011. {
  7012. nullpo_ret(sd);
  7013. if (sd->followtimer != INVALID_TIMER) {
  7014. delete_timer(sd->followtimer,pc_follow_timer);
  7015. sd->followtimer = INVALID_TIMER;
  7016. }
  7017. sd->followtarget = -1;
  7018. sd->ud.target_to = 0;
  7019. unit_stop_walking(&sd->bl, 1);
  7020. return 0;
  7021. }
  7022. int pc_follow(map_session_data *sd,int target_id)
  7023. {
  7024. struct block_list *bl = map_id2bl(target_id);
  7025. if (bl == nullptr /*|| bl->type != BL_PC*/)
  7026. return 1;
  7027. if (sd->followtimer != INVALID_TIMER)
  7028. pc_stop_following(sd);
  7029. sd->followtarget = target_id;
  7030. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7031. return 0;
  7032. }
  7033. int pc_checkbaselevelup(map_session_data *sd) {
  7034. t_exp next = pc_nextbaseexp(sd);
  7035. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  7036. return 0;
  7037. uint32 base_level = sd->status.base_level;
  7038. do {
  7039. sd->status.base_exp -= next;
  7040. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7041. 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 )
  7042. sd->status.base_exp = next-1;
  7043. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level);
  7044. sd->status.trait_point += statpoint_db.pc_gets_trait_point(sd->status.base_level);
  7045. sd->status.base_level++;
  7046. if( pc_is_maxbaselv(sd) ){
  7047. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  7048. break;
  7049. }
  7050. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  7051. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  7052. status_calc_pet(sd->pd,SCO_NONE);
  7053. clif_updatestatus(sd,SP_STATUSPOINT);
  7054. clif_updatestatus(sd,SP_TRAITPOINT);
  7055. clif_updatestatus(sd,SP_BASELEVEL);
  7056. clif_updatestatus(sd,SP_BASEEXP);
  7057. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7058. status_calc_pc(sd,SCO_FORCE);
  7059. status_percent_heal(&sd->bl,100,100);
  7060. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7061. for (const auto &status : status_db) {
  7062. if (status.second->flag[SCF_SUPERNOVICEANGEL])
  7063. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 1, 120000); //All buffs with super novice flag for 2 minutes
  7064. }
  7065. if (sd->state.snovice_dead_flag)
  7066. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  7067. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  7068. for (const auto &status : status_db) {
  7069. if (status.second->flag[SCF_TAEKWONANGEL])
  7070. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 10, 600000);
  7071. }
  7072. }
  7073. clif_misceffect(&sd->bl,0);
  7074. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  7075. if(sd->status.party_id)
  7076. party_send_levelup(sd);
  7077. pc_baselevelchanged(sd);
  7078. for (; base_level <= sd->status.base_level; base_level++) {
  7079. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  7080. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  7081. }
  7082. return 1;
  7083. }
  7084. void pc_baselevelchanged(map_session_data *sd) {
  7085. uint8 i;
  7086. for( i = 0; i < EQI_MAX; i++ ) {
  7087. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  7088. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  7089. pc_unequipitem(sd, sd->equip_index[i], 3);
  7090. }
  7091. }
  7092. pc_show_questinfo(sd);
  7093. }
  7094. int pc_checkjoblevelup(map_session_data *sd)
  7095. {
  7096. t_exp next = pc_nextjobexp(sd);
  7097. nullpo_ret(sd);
  7098. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  7099. return 0;
  7100. uint32 job_level = sd->status.job_level;
  7101. do {
  7102. sd->status.job_exp -= next;
  7103. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7104. 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 )
  7105. sd->status.job_exp = next-1;
  7106. sd->status.job_level ++;
  7107. sd->status.skill_point ++;
  7108. if( pc_is_maxjoblv(sd) ){
  7109. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  7110. break;
  7111. }
  7112. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  7113. clif_updatestatus(sd,SP_JOBLEVEL);
  7114. clif_updatestatus(sd,SP_JOBEXP);
  7115. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7116. clif_updatestatus(sd,SP_SKILLPOINT);
  7117. status_calc_pc(sd,SCO_FORCE);
  7118. clif_misceffect(&sd->bl,1);
  7119. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7120. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7121. npc_script_event(sd, NPCE_JOBLVUP);
  7122. for (; job_level <= sd->status.job_level; job_level++)
  7123. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  7124. pc_show_questinfo(sd);
  7125. return 1;
  7126. }
  7127. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  7128. * @param sd Player
  7129. * @param base_exp Base EXP before peronal bonuses
  7130. * @param job_exp Job EXP before peronal bonuses
  7131. * @param src Block list that affecting the exp calculation
  7132. */
  7133. static void pc_calcexp(map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  7134. {
  7135. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  7136. if (src) {
  7137. struct status_data *status = status_get_status_data(src);
  7138. if( sd->indexed_bonus.expaddrace[status->race] )
  7139. bonus += sd->indexed_bonus.expaddrace[status->race];
  7140. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  7141. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  7142. if( sd->indexed_bonus.expaddclass[status->class_] )
  7143. bonus += sd->indexed_bonus.expaddclass[status->class_];
  7144. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  7145. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  7146. if (battle_config.pk_mode &&
  7147. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  7148. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  7149. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  7150. vip_bonus_base = battle_config.vip_base_exp_increase;
  7151. vip_bonus_job = battle_config.vip_job_exp_increase;
  7152. }
  7153. }
  7154. // Give EXPBOOST for quests even if src is nullptr.
  7155. if (sd->sc.getSCE(SC_EXPBOOST)) {
  7156. bonus += sd->sc.getSCE(SC_EXPBOOST)->val1;
  7157. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  7158. bonus += (sd->sc.getSCE(SC_EXPBOOST)->val1 / battle_config.vip_bm_increase);
  7159. }
  7160. if (sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND))
  7161. bonus += sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)->val1;
  7162. if (*base_exp) {
  7163. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  7164. *base_exp = cap_value(exp, 1, MAX_EXP);
  7165. }
  7166. // Give JEXPBOOST for quests even if src is nullptr.
  7167. if (sd->sc.getSCE(SC_JEXPBOOST))
  7168. bonus += sd->sc.getSCE(SC_JEXPBOOST)->val1;
  7169. if (sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)) // Increase Jexp as well
  7170. bonus += sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)->val1;
  7171. if (*job_exp) {
  7172. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  7173. *job_exp = cap_value(exp, 1, MAX_EXP);
  7174. }
  7175. return;
  7176. }
  7177. /**
  7178. * Show EXP gained by player in percentage by @showexp
  7179. * @param sd Player
  7180. * @param base_exp Base EXP gained/loss
  7181. * @param next_base_exp Base EXP needed for next base level
  7182. * @param job_exp Job EXP gained/loss
  7183. * @param next_job_exp Job EXP needed for next job level
  7184. * @param lost True:EXP penalty, lose EXP
  7185. **/
  7186. 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) {
  7187. char output[CHAT_SIZE_MAX];
  7188. nullpo_retv(sd);
  7189. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  7190. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  7191. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  7192. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  7193. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  7194. }
  7195. /**
  7196. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  7197. * @param sd Player
  7198. * @param src EXP source
  7199. * @param base_exp Base EXP gained
  7200. * @param base_exp Job EXP gained
  7201. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  7202. * @return
  7203. **/
  7204. void pc_gainexp(map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  7205. {
  7206. t_exp nextb = 0, nextj = 0;
  7207. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  7208. nullpo_retv(sd);
  7209. if(sd->bl.prev == nullptr || pc_isdead(sd))
  7210. return;
  7211. if (!(exp_flag&2)) {
  7212. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  7213. return; // no exp on pvp maps
  7214. if (sd->status.guild_id>0)
  7215. base_exp -= guild_payexp(sd,base_exp);
  7216. }
  7217. flag = ((base_exp) ? 1 : 0) |
  7218. ((job_exp) ? 2 : 0) |
  7219. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  7220. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  7221. if (!(exp_flag&2))
  7222. pc_calcexp(sd, &base_exp, &job_exp, src);
  7223. nextb = pc_nextbaseexp(sd);
  7224. nextj = pc_nextjobexp(sd);
  7225. if (flag&4){
  7226. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  7227. base_exp = 0;
  7228. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  7229. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  7230. }
  7231. if (flag&8){
  7232. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  7233. job_exp = 0;
  7234. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  7235. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  7236. }
  7237. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  7238. //Note that this value should never be greater than the original
  7239. //therefore no overflow checks are needed. [Skotlex]
  7240. if (nextb > 0) {
  7241. float nextbp = (float) base_exp / (float) nextb;
  7242. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  7243. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  7244. }
  7245. if (nextj > 0) {
  7246. float nextjp = (float) job_exp / (float) nextj;
  7247. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  7248. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  7249. }
  7250. }
  7251. // Give EXP for Base Level
  7252. if (base_exp) {
  7253. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  7254. if (!pc_checkbaselevelup(sd))
  7255. clif_updatestatus(sd,SP_BASEEXP);
  7256. }
  7257. // Give EXP for Job Level
  7258. if (job_exp) {
  7259. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  7260. if (!pc_checkjoblevelup(sd))
  7261. clif_updatestatus(sd,SP_JOBEXP);
  7262. }
  7263. if (flag&1)
  7264. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  7265. if (flag&2)
  7266. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  7267. if (sd->state.showexp && (base_exp || job_exp))
  7268. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  7269. }
  7270. /**
  7271. * Lost Base/Job EXP from a player
  7272. * @param sd Player
  7273. * @param base_exp Base EXP lost
  7274. * @param job_exp Job EXP lost
  7275. **/
  7276. void pc_lostexp(map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  7277. nullpo_retv(sd);
  7278. if (base_exp) {
  7279. base_exp = u64min(sd->status.base_exp, base_exp);
  7280. sd->status.base_exp -= base_exp;
  7281. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  7282. clif_updatestatus(sd, SP_BASEEXP);
  7283. }
  7284. if (job_exp) {
  7285. job_exp = u64min(sd->status.job_exp, job_exp);
  7286. sd->status.job_exp -= job_exp;
  7287. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  7288. clif_updatestatus(sd, SP_JOBEXP);
  7289. }
  7290. if (sd->state.showexp && (base_exp || job_exp))
  7291. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  7292. }
  7293. /**
  7294. * Returns max base level for this character's class.
  7295. * @param job_id: Player's class
  7296. * @return Max Base Level
  7297. */
  7298. uint32 JobDatabase::get_maxBaseLv(uint16 job_id) {
  7299. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7300. return job ? job->max_base_level : 0;
  7301. }
  7302. /**
  7303. * Returns max base level for this character.
  7304. * @param sd Player
  7305. * @return Max Base Level
  7306. **/
  7307. unsigned int pc_maxbaselv(map_session_data *sd){
  7308. return job_db.get_maxBaseLv(sd->status.class_);
  7309. }
  7310. /**
  7311. * Returns max job level for this character's class.
  7312. * @param job_id: Player's class
  7313. * @return Max Job Level
  7314. */
  7315. uint32 JobDatabase::get_maxJobLv(uint16 job_id) {
  7316. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7317. return job ? job->max_job_level : 0;
  7318. }
  7319. /**
  7320. * Returns max job level for this character.
  7321. * @param sd Player
  7322. * @return Max Job Level
  7323. **/
  7324. unsigned int pc_maxjoblv(map_session_data *sd){
  7325. return job_db.get_maxJobLv(sd->status.class_);
  7326. }
  7327. /**
  7328. * Check if player is reached max base level
  7329. * @param sd
  7330. * @return True if reached max level
  7331. **/
  7332. bool pc_is_maxbaselv(map_session_data *sd) {
  7333. nullpo_retr(false, sd);
  7334. return (sd->status.base_level >= pc_maxbaselv(sd));
  7335. }
  7336. /**
  7337. * Check if player is reached max base level
  7338. * @param sd
  7339. * @return True if reached max level
  7340. **/
  7341. bool pc_is_maxjoblv(map_session_data *sd) {
  7342. nullpo_retr(false, sd);
  7343. return (sd->status.job_level >= pc_maxjoblv(sd));
  7344. }
  7345. /**
  7346. * Returns base experience for this character's class.
  7347. * @param job_id: Player's class
  7348. * @param level: Player's level
  7349. * @return Base EXP
  7350. */
  7351. t_exp JobDatabase::get_baseExp(uint16 job_id, uint32 level) {
  7352. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7353. return job ? job->base_exp[level - 1] : 0;
  7354. }
  7355. /**
  7356. * Base exp needed for player to level up.
  7357. * @param sd
  7358. * @return Base EXP needed for next base level
  7359. **/
  7360. t_exp pc_nextbaseexp(map_session_data *sd){
  7361. nullpo_ret(sd);
  7362. if (sd->status.base_level == 0) // Is this something that possible?
  7363. return 0;
  7364. if (pc_is_maxbaselv(sd))
  7365. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  7366. return static_cast<t_exp>(job_db.get_baseExp(sd->status.class_, sd->status.base_level));
  7367. }
  7368. /**
  7369. * Returns job experience for this character's class.
  7370. * @param job_id: Player's class
  7371. * @param level: Player's level
  7372. * @return Job EXP
  7373. */
  7374. t_exp JobDatabase::get_jobExp(uint16 job_id, uint32 level) {
  7375. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7376. return job ? job->job_exp[level - 1] : 0;
  7377. }
  7378. /**
  7379. * Job exp needed for player to level up.
  7380. * @param sd
  7381. * @return Job EXP needed for next job level
  7382. **/
  7383. t_exp pc_nextjobexp(map_session_data *sd){
  7384. nullpo_ret(sd);
  7385. if (sd->status.job_level == 0) // Is this something that possible?
  7386. return 0;
  7387. if (pc_is_maxjoblv(sd))
  7388. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  7389. return static_cast<t_exp>(job_db.get_jobExp(sd->status.class_, sd->status.job_level));
  7390. }
  7391. /**
  7392. * Returns max weight base for this character's class.
  7393. * @param job_id: Player's class
  7394. * @return Max weight base
  7395. */
  7396. int32 JobDatabase::get_maxWeight(uint16 job_id) {
  7397. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7398. return job ? job->max_weight_base : 0;
  7399. }
  7400. /// Returns the value of the specified stat.
  7401. int pc_getstat(map_session_data *sd, int type)
  7402. {
  7403. nullpo_retr(-1, sd);
  7404. switch( type ) {
  7405. case SP_STR: return sd->status.str;
  7406. case SP_AGI: return sd->status.agi;
  7407. case SP_VIT: return sd->status.vit;
  7408. case SP_INT: return sd->status.int_;
  7409. case SP_DEX: return sd->status.dex;
  7410. case SP_LUK: return sd->status.luk;
  7411. case SP_POW: return sd->status.pow;
  7412. case SP_STA: return sd->status.sta;
  7413. case SP_WIS: return sd->status.wis;
  7414. case SP_SPL: return sd->status.spl;
  7415. case SP_CON: return sd->status.con;
  7416. case SP_CRT: return sd->status.crt;
  7417. default:
  7418. return -1;
  7419. }
  7420. }
  7421. /// Sets the specified stat to the specified value.
  7422. /// Returns the new value.
  7423. int pc_setstat(map_session_data* sd, int type, int val)
  7424. {
  7425. nullpo_retr(-1, sd);
  7426. switch( type ) {
  7427. case SP_STR: sd->status.str = val; break;
  7428. case SP_AGI: sd->status.agi = val; break;
  7429. case SP_VIT: sd->status.vit = val; break;
  7430. case SP_INT: sd->status.int_ = val; break;
  7431. case SP_DEX: sd->status.dex = val; break;
  7432. case SP_LUK: sd->status.luk = val; break;
  7433. case SP_POW: sd->status.pow = val; break;
  7434. case SP_STA: sd->status.sta = val; break;
  7435. case SP_WIS: sd->status.wis = val; break;
  7436. case SP_SPL: sd->status.spl = val; break;
  7437. case SP_CON: sd->status.con = val; break;
  7438. case SP_CRT: sd->status.crt = val; break;
  7439. default:
  7440. return -1;
  7441. }
  7442. return val;
  7443. }
  7444. /**
  7445. * Gets the total number of status points at the provided level.
  7446. * @param level: Player base level.
  7447. * @return Total number of status points at specific base level.
  7448. */
  7449. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  7450. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7451. if( entry != nullptr ){
  7452. return entry->statpoints;
  7453. }else{
  7454. return 0;
  7455. }
  7456. }
  7457. /**
  7458. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  7459. * @param level: Player base level.
  7460. * @param table: Use table value or formula.
  7461. * @return Status points at specific base level.
  7462. */
  7463. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  7464. uint32 next_level = this->get_table_point( level + 1 );
  7465. uint32 current_level = this->get_table_point( level );
  7466. if( next_level > current_level ){
  7467. return next_level - current_level;
  7468. }else{
  7469. return 0;
  7470. }
  7471. }
  7472. /**
  7473. * Gets the total number of trait points at the provided level.
  7474. * @param level: Player base level.
  7475. * @return Total number of trait points at specific base level.
  7476. */
  7477. uint32 PlayerStatPointDatabase::get_trait_table_point(uint16 level) {
  7478. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7479. if( entry != nullptr ){
  7480. return entry->traitpoints;
  7481. }else{
  7482. return 0;
  7483. }
  7484. }
  7485. /**
  7486. * Calculates the number of trait points PC gets when leveling up (from level to level+1)
  7487. * @param level: Player base level.
  7488. * @param table: Use table value or formula.
  7489. * @return Trait points at specific base level.
  7490. */
  7491. uint32 PlayerStatPointDatabase::pc_gets_trait_point(uint16 level) {
  7492. uint32 next_level = this->get_trait_table_point( level + 1 );
  7493. uint32 current_level = this->get_trait_table_point( level );
  7494. if( next_level > current_level ){
  7495. return next_level - current_level;
  7496. }else{
  7497. return 0;
  7498. }
  7499. }
  7500. #ifdef RENEWAL_STAT
  7501. /// Renewal status point cost formula
  7502. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  7503. #else
  7504. /// Pre-Renewal status point cost formula
  7505. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  7506. #endif
  7507. /// Returns the number of stat points needed to change the specified stat by val.
  7508. /// If val is negative, returns the number of stat points that would be needed to
  7509. /// raise the specified stat from (current value - val) to current value.
  7510. int pc_need_status_point(map_session_data* sd, int type, int val)
  7511. {
  7512. int low, high, sp = 0, max = 0;
  7513. if ( val == 0 )
  7514. return 0;
  7515. low = pc_getstat(sd,type);
  7516. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  7517. if ( low >= max && val > 0 )
  7518. return 0; // Official servers show '0' when max is reached
  7519. high = low + val;
  7520. if ( val < 0 )
  7521. std::swap(low, high);
  7522. for ( ; low < high; low++ )
  7523. sp += PC_STATUS_POINT_COST(low);
  7524. return sp;
  7525. }
  7526. /**
  7527. * Returns the value the specified stat can be increased by with the current
  7528. * amount of available status points for the current character's class.
  7529. *
  7530. * @param sd The target character.
  7531. * @param type Stat to verify.
  7532. * @return Maximum value the stat could grow by.
  7533. */
  7534. int pc_maxparameterincrease(map_session_data* sd, int type)
  7535. {
  7536. int base, final_val, status_points, max_param;
  7537. nullpo_ret(sd);
  7538. base = final_val = pc_getstat(sd, type);
  7539. status_points = sd->status.status_point;
  7540. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  7541. while (final_val <= max_param && status_points >= 0) {
  7542. status_points -= PC_STATUS_POINT_COST(final_val);
  7543. final_val++;
  7544. }
  7545. final_val--;
  7546. return (final_val > base ? final_val-base : 0);
  7547. }
  7548. /**
  7549. * Raises a stat by the specified amount.
  7550. *
  7551. * Obeys max_parameter limits.
  7552. * Subtracts status points according to the cost of the increased stat points.
  7553. *
  7554. * @param sd The target character.
  7555. * @param type The stat to change (see enum _sp)
  7556. * @param increase The stat increase (strictly positive) amount.
  7557. * @retval true if the stat was increased by any amount.
  7558. * @retval false if there were no changes.
  7559. */
  7560. bool pc_statusup(map_session_data* sd, int type, int increase)
  7561. {
  7562. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  7563. nullpo_ret(sd);
  7564. // check conditions
  7565. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  7566. clif_statusupack(sd, type, 0, 0);
  7567. return false;
  7568. }
  7569. // check limits
  7570. current = pc_getstat(sd, type);
  7571. max_increase = pc_maxparameterincrease(sd, type);
  7572. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7573. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  7574. clif_statusupack(sd, type, 0, 0);
  7575. return false;
  7576. }
  7577. // check status points
  7578. needed_points = pc_need_status_point(sd, type, increase);
  7579. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  7580. clif_statusupack(sd, type, 0, 0);
  7581. return false;
  7582. }
  7583. // set new values
  7584. final_value = pc_setstat(sd, type, current + increase);
  7585. sd->status.status_point -= needed_points;
  7586. status_calc_pc(sd,SCO_NONE);
  7587. // update increase cost indicator
  7588. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7589. // update statpoint count
  7590. clif_updatestatus(sd, SP_STATUSPOINT);
  7591. // update stat value
  7592. clif_statusupack(sd, type, 1, final_value); // required
  7593. if( final_value > 255 )
  7594. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7595. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7596. return true;
  7597. }
  7598. /**
  7599. * Raises a stat by the specified amount.
  7600. *
  7601. * Obeys max_parameter limits.
  7602. * Does not subtract status points for the cost of the modified stat points.
  7603. *
  7604. * @param sd The target character.
  7605. * @param type The stat to change (see enum _sp)
  7606. * @param val The stat increase (or decrease) amount.
  7607. * @return the stat increase amount.
  7608. * @retval 0 if no changes were made.
  7609. */
  7610. int pc_statusup2(map_session_data* sd, int type, int val)
  7611. {
  7612. int max, need;
  7613. nullpo_ret(sd);
  7614. if( type < SP_STR || type > SP_LUK )
  7615. {
  7616. clif_statusupack(sd,type,0,0);
  7617. return 0;
  7618. }
  7619. need = pc_need_status_point(sd,type,1);
  7620. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  7621. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  7622. status_calc_pc(sd,SCO_NONE);
  7623. // update increase cost indicator
  7624. if( need != pc_need_status_point(sd,type,1) )
  7625. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7626. // update stat value
  7627. clif_statusupack(sd,type,1,val); // required
  7628. if( val > 255 )
  7629. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  7630. return val;
  7631. }
  7632. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  7633. /// If val is negative, returns the number of trait stat points that would be needed to
  7634. /// raise the specified trait stat from (current value - val) to current value.
  7635. int pc_need_trait_point(map_session_data* sd, int type, int val)
  7636. {
  7637. nullpo_retr(0, sd);
  7638. if (val == 0 || type < SP_POW || type > SP_CRT)
  7639. return 0;
  7640. int low = pc_getstat(sd, type);
  7641. int max = pc_maxparameter(sd, (e_params)(PARAM_POW + type - SP_POW));
  7642. if (low >= max && val > 0)
  7643. return 0; // Official servers show '0' when max is reached
  7644. int high = low + val, sp = 0;
  7645. if (val < 0)
  7646. std::swap(low, high);
  7647. for (; low < high; low++)
  7648. sp += 1;
  7649. return sp;
  7650. }
  7651. /**
  7652. * Returns the value the specified trait stat can be increased by with the current
  7653. * amount of available trait status points for the current character's class.
  7654. *
  7655. * @param sd The target character.
  7656. * @param type Trait stat to verify.
  7657. * @return Maximum value the stat could grow by.
  7658. */
  7659. int pc_maxtraitparameterincrease(map_session_data* sd, int type)
  7660. {
  7661. nullpo_ret(sd);
  7662. if( type < SP_POW || type > SP_CRT ){
  7663. return 0;
  7664. }
  7665. int base, final_val = pc_getstat(sd, type);
  7666. int trait_points = sd->status.trait_point;
  7667. int max_param = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW));
  7668. base = final_val;
  7669. while (final_val <= max_param && trait_points >= 0) {
  7670. trait_points -= 1;
  7671. final_val++;
  7672. }
  7673. final_val--;
  7674. return (final_val > base ? final_val - base : 0);
  7675. }
  7676. /**
  7677. * Raises a trait stat by the specified amount.
  7678. *
  7679. * Obeys max_traitparameter limits.
  7680. * Subtracts trait status points according to the cost of the increased trait stat points.
  7681. *
  7682. * @param sd The target character.
  7683. * @param type The stat to change (see enum _sp)
  7684. * @param increase The stat increase (strictly positive) amount.
  7685. * @retval true if the trait stat was increased by any amount.
  7686. * @retval false if there were no changes.
  7687. */
  7688. bool pc_traitstatusup(map_session_data* sd, int type, int increase)
  7689. {
  7690. nullpo_ret(sd);
  7691. // check conditions
  7692. if (type < SP_POW || type > SP_CRT || increase <= 0) {
  7693. clif_statusupack(sd, type, 0, 0);
  7694. return false;
  7695. }
  7696. // check limits
  7697. int current = pc_getstat(sd, type);
  7698. int max_increase = pc_maxtraitparameterincrease(sd, type);
  7699. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7700. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW))) {
  7701. clif_statusupack(sd, type, 0, 0);
  7702. return false;
  7703. }
  7704. // check status points
  7705. int needed_points = pc_need_trait_point(sd, type, increase);
  7706. if (needed_points < 0 || needed_points > sd->status.trait_point) { // Sanity check
  7707. clif_statusupack(sd, type, 0, 0);
  7708. return false;
  7709. }
  7710. // set new values
  7711. int final_value = pc_setstat(sd, type, current + increase);
  7712. sd->status.trait_point -= needed_points;
  7713. status_calc_pc(sd, SCO_NONE);
  7714. // update increase cost indicator
  7715. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7716. // update statpoint count
  7717. clif_updatestatus(sd, SP_TRAITPOINT);
  7718. // update stat value
  7719. clif_statusupack(sd, type, 1, final_value); // required
  7720. if (final_value > 255)
  7721. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7722. //achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7723. return true;
  7724. }
  7725. /**
  7726. * Raises a trait stat by the specified amount.
  7727. *
  7728. * Obeys max_trait_parameter limits.
  7729. * Does not subtract status points for the cost of the modified stat points.
  7730. *
  7731. * @param sd The target character.
  7732. * @param type The stat to change (see enum _sp)
  7733. * @param val The stat increase (or decrease) amount.
  7734. * @return the stat increase amount.
  7735. * @retval 0 if no changes were made.
  7736. */
  7737. int pc_traitstatusup2(map_session_data* sd, int type, int val)
  7738. {
  7739. nullpo_ret(sd);
  7740. if (type < SP_POW || type > SP_CRT) {
  7741. clif_statusupack(sd, type, 0, 0);
  7742. return 0;
  7743. }
  7744. int need = pc_need_trait_point(sd, type, 1);
  7745. int max = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW)); // set new value
  7746. val = pc_setstat(sd, type, cap_value(pc_getstat(sd, type) + val, 0, max));
  7747. status_calc_pc(sd, SCO_NONE);
  7748. // update increase cost indicator
  7749. if (need != pc_need_trait_point(sd, type, 1))
  7750. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7751. // update stat value
  7752. clif_statusupack(sd, type, 1, val); // required
  7753. if (val > 255)
  7754. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7755. return val;
  7756. }
  7757. /*==========================================
  7758. * Update skill_lv for player sd
  7759. * Skill point allocation
  7760. *------------------------------------------*/
  7761. void pc_skillup(map_session_data *sd,uint16 skill_id)
  7762. {
  7763. uint16 idx = skill_get_index(skill_id);
  7764. nullpo_retv(sd);
  7765. if (!idx) {
  7766. if (skill_id)
  7767. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  7768. return;
  7769. }
  7770. // Level up guild skill
  7771. if (SKILL_CHK_GUILD(skill_id)) {
  7772. guild_skillup(sd, skill_id);
  7773. return;
  7774. }
  7775. // Level up homunculus skill
  7776. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  7777. hom_skillup(sd->hd, skill_id);
  7778. return;
  7779. }
  7780. else {
  7781. if( sd->status.skill_point > 0 &&
  7782. sd->status.skill[idx].id &&
  7783. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  7784. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  7785. {
  7786. int lv, range, upgradable;
  7787. sd->status.skill[idx].lv++;
  7788. sd->status.skill_point--;
  7789. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  7790. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  7791. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  7792. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  7793. else
  7794. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  7795. lv = sd->status.skill[idx].lv;
  7796. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  7797. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  7798. clif_skillup(sd,skill_id,lv,range,upgradable);
  7799. clif_updatestatus(sd,SP_SKILLPOINT);
  7800. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  7801. clif_updatestatus(sd,SP_CARTINFO);
  7802. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  7803. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7804. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  7805. clif_skillinfoblock(sd);
  7806. }
  7807. //else
  7808. // 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);
  7809. }
  7810. }
  7811. /*==========================================
  7812. * /allskill
  7813. *------------------------------------------*/
  7814. int pc_allskillup(map_session_data *sd)
  7815. {
  7816. int i;
  7817. nullpo_ret(sd);
  7818. for (i = 0; i < MAX_SKILL; i++) {
  7819. 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) {
  7820. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  7821. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  7822. if (sd->status.skill[i].lv == 0)
  7823. sd->status.skill[i].id = 0;
  7824. }
  7825. }
  7826. if (!pc_grant_allskills(sd, true)) {
  7827. uint16 sk_id;
  7828. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(sd->status.class_);
  7829. if (tree != nullptr && !tree->skills.empty()) {
  7830. for (const auto &skillsit : tree->skills) {
  7831. sk_id = skillsit.first;
  7832. uint16 sk_idx = skill_get_index(sk_id);
  7833. if (sk_idx == 0)
  7834. continue;
  7835. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  7836. if (
  7837. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  7838. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  7839. sk_id == SG_DEVIL
  7840. )
  7841. continue; //Cannot be learned normally.
  7842. sd->status.skill[sk_idx].id = sk_id;
  7843. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  7844. }
  7845. }
  7846. }
  7847. status_calc_pc(sd,SCO_NONE);
  7848. //Required because if you could level up all skills previously,
  7849. //the update will not be sent as only the lv variable changes.
  7850. clif_skillinfoblock(sd);
  7851. return 0;
  7852. }
  7853. /*==========================================
  7854. * /resetlvl
  7855. *------------------------------------------*/
  7856. int pc_resetlvl(map_session_data* sd,int type)
  7857. {
  7858. int i;
  7859. nullpo_ret(sd);
  7860. if (type != 3) //Also reset skills
  7861. pc_resetskill(sd, 0);
  7862. if(type == 1){
  7863. sd->status.skill_point=0;
  7864. sd->status.trait_point = 0;
  7865. sd->status.base_level=1;
  7866. sd->status.job_level=1;
  7867. sd->status.base_exp=0;
  7868. sd->status.job_exp=0;
  7869. if(sd->sc.option !=0)
  7870. sd->sc.option = 0;
  7871. sd->status.str=1;
  7872. sd->status.agi=1;
  7873. sd->status.vit=1;
  7874. sd->status.int_=1;
  7875. sd->status.dex=1;
  7876. sd->status.luk=1;
  7877. sd->status.pow=0;
  7878. sd->status.sta=0;
  7879. sd->status.wis=0;
  7880. sd->status.spl=0;
  7881. sd->status.con=0;
  7882. sd->status.crt=0;
  7883. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  7884. sd->status.status_point=100; // not 88 [celest]
  7885. // give platinum skills upon changing
  7886. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  7887. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  7888. }
  7889. }
  7890. if(type == 2){
  7891. sd->status.skill_point=0;
  7892. sd->status.base_level=1;
  7893. sd->status.job_level=1;
  7894. sd->status.base_exp=0;
  7895. sd->status.job_exp=0;
  7896. }
  7897. if(type == 3){
  7898. sd->status.base_level=1;
  7899. sd->status.base_exp=0;
  7900. }
  7901. if(type == 4){
  7902. sd->status.job_level=1;
  7903. sd->status.job_exp=0;
  7904. }
  7905. clif_updatestatus(sd,SP_STATUSPOINT);
  7906. clif_updatestatus(sd,SP_TRAITPOINT);
  7907. clif_updatestatus(sd,SP_STR);
  7908. clif_updatestatus(sd,SP_AGI);
  7909. clif_updatestatus(sd,SP_VIT);
  7910. clif_updatestatus(sd,SP_INT);
  7911. clif_updatestatus(sd,SP_DEX);
  7912. clif_updatestatus(sd,SP_LUK);
  7913. clif_updatestatus(sd,SP_POW);
  7914. clif_updatestatus(sd,SP_STA);
  7915. clif_updatestatus(sd,SP_WIS);
  7916. clif_updatestatus(sd,SP_SPL);
  7917. clif_updatestatus(sd,SP_CON);
  7918. clif_updatestatus(sd,SP_CRT);
  7919. clif_updatestatus(sd,SP_BASELEVEL);
  7920. clif_updatestatus(sd,SP_JOBLEVEL);
  7921. clif_updatestatus(sd,SP_STATUSPOINT);
  7922. clif_updatestatus(sd,SP_BASEEXP);
  7923. clif_updatestatus(sd,SP_JOBEXP);
  7924. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7925. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7926. clif_updatestatus(sd,SP_SKILLPOINT);
  7927. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7928. clif_updatestatus(sd,SP_UAGI);
  7929. clif_updatestatus(sd,SP_UVIT);
  7930. clif_updatestatus(sd,SP_UINT);
  7931. clif_updatestatus(sd,SP_UDEX);
  7932. clif_updatestatus(sd,SP_ULUK); // End Addition
  7933. clif_updatestatus(sd,SP_UPOW);
  7934. clif_updatestatus(sd,SP_USTA);
  7935. clif_updatestatus(sd,SP_UWIS);
  7936. clif_updatestatus(sd,SP_USPL);
  7937. clif_updatestatus(sd,SP_UCON);
  7938. clif_updatestatus(sd,SP_UCRT);
  7939. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  7940. if(sd->equip_index[i] >= 0)
  7941. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  7942. pc_unequipitem(sd,sd->equip_index[i],2);
  7943. }
  7944. }
  7945. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  7946. party_send_levelup(sd);
  7947. status_calc_pc(sd, SCO_FORCE);
  7948. clif_skillinfoblock(sd);
  7949. return 0;
  7950. }
  7951. /*==========================================
  7952. * /resetstate
  7953. *------------------------------------------*/
  7954. int pc_resetstate(map_session_data* sd)
  7955. {
  7956. nullpo_ret(sd);
  7957. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  7958. 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",
  7959. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  7960. sd->status.base_level = pc_maxbaselv( sd );
  7961. clif_updatestatus( sd, SP_BASELEVEL );
  7962. }
  7963. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  7964. sd->status.trait_point = statpoint_db.get_trait_table_point(sd->status.base_level);
  7965. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  7966. sd->status.status_point += battle_config.transcendent_status_points;
  7967. }
  7968. if ((sd->class_&JOBL_FOURTH) != 0) {
  7969. sd->status.trait_point += battle_config.trait_points_job_change;
  7970. }
  7971. pc_setstat(sd, SP_STR, 1);
  7972. pc_setstat(sd, SP_AGI, 1);
  7973. pc_setstat(sd, SP_VIT, 1);
  7974. pc_setstat(sd, SP_INT, 1);
  7975. pc_setstat(sd, SP_DEX, 1);
  7976. pc_setstat(sd, SP_LUK, 1);
  7977. pc_setstat(sd, SP_POW, 0);
  7978. pc_setstat(sd, SP_STA, 0);
  7979. pc_setstat(sd, SP_WIS, 0);
  7980. pc_setstat(sd, SP_SPL, 0);
  7981. pc_setstat(sd, SP_CON, 0);
  7982. pc_setstat(sd, SP_CRT, 0);
  7983. clif_updatestatus(sd,SP_STR);
  7984. clif_updatestatus(sd,SP_AGI);
  7985. clif_updatestatus(sd,SP_VIT);
  7986. clif_updatestatus(sd,SP_INT);
  7987. clif_updatestatus(sd,SP_DEX);
  7988. clif_updatestatus(sd,SP_LUK);
  7989. clif_updatestatus(sd,SP_POW);
  7990. clif_updatestatus(sd,SP_STA);
  7991. clif_updatestatus(sd,SP_WIS);
  7992. clif_updatestatus(sd,SP_SPL);
  7993. clif_updatestatus(sd,SP_CON);
  7994. clif_updatestatus(sd,SP_CRT);
  7995. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7996. clif_updatestatus(sd,SP_UAGI);
  7997. clif_updatestatus(sd,SP_UVIT);
  7998. clif_updatestatus(sd,SP_UINT);
  7999. clif_updatestatus(sd,SP_UDEX);
  8000. clif_updatestatus(sd,SP_ULUK); // End Addition
  8001. clif_updatestatus(sd,SP_UPOW);
  8002. clif_updatestatus(sd,SP_USTA);
  8003. clif_updatestatus(sd,SP_UWIS);
  8004. clif_updatestatus(sd,SP_USPL);
  8005. clif_updatestatus(sd,SP_UCON);
  8006. clif_updatestatus(sd,SP_UCRT);
  8007. clif_updatestatus(sd,SP_STATUSPOINT);
  8008. clif_updatestatus(sd,SP_TRAITPOINT);
  8009. if( sd->mission_mobid ) { //bugreport:2200
  8010. sd->mission_mobid = 0;
  8011. sd->mission_count = 0;
  8012. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  8013. }
  8014. status_calc_pc(sd, SCO_NONE);
  8015. return 1;
  8016. }
  8017. /*==========================================
  8018. * /resetskill
  8019. * if flag&1, perform block resync and status_calc call.
  8020. * if flag&2, just count total amount of skill points used by player, do not really reset.
  8021. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  8022. *------------------------------------------*/
  8023. int pc_resetskill(map_session_data* sd, int flag)
  8024. {
  8025. int i, skill_point=0;
  8026. nullpo_ret(sd);
  8027. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  8028. return 0;
  8029. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  8030. /**
  8031. * 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)
  8032. **/
  8033. if( pc_is_taekwon_ranker(sd) )
  8034. return 0;
  8035. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  8036. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  8037. i = sd->sc.option;
  8038. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  8039. i &= ~OPTION_RIDING;
  8040. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  8041. i &= ~OPTION_FALCON;
  8042. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  8043. i &= ~OPTION_DRAGON;
  8044. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  8045. i &= ~OPTION_WUG;
  8046. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  8047. i &= ~OPTION_WUGRIDER;
  8048. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  8049. i &= ~OPTION_MADOGEAR;
  8050. #ifndef NEW_CARTS
  8051. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  8052. i &= ~OPTION_CART;
  8053. #else
  8054. if( sd->sc.getSCE(SC_PUSH_CART) )
  8055. pc_setcart(sd, 0);
  8056. #endif
  8057. if( i != sd->sc.option )
  8058. pc_setoption(sd, i);
  8059. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  8060. hom_vaporize(sd, HOM_ST_ACTIVE);
  8061. if (sd->sc.getSCE(SC_SPRITEMABLE) && pc_checkskill(sd, SU_SPRITEMABLE))
  8062. status_change_end(&sd->bl, SC_SPRITEMABLE);
  8063. if (sd->sc.getSCE(SC_SOULATTACK) && pc_checkskill(sd, SU_SOULATTACK))
  8064. status_change_end(&sd->bl, SC_SOULATTACK);
  8065. }
  8066. for (const auto &skill : skill_db) {
  8067. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  8068. uint8 lv = sd->status.skill[idx].lv;
  8069. if (lv == 0 || skill_id == 0)
  8070. continue;
  8071. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  8072. continue;
  8073. // Don't reset trick dead if not a novice/baby
  8074. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  8075. {
  8076. sd->status.skill[idx].lv = 0;
  8077. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8078. continue;
  8079. }
  8080. // do not reset basic skill
  8081. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  8082. continue;
  8083. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  8084. continue;
  8085. if( flag&4 && !skill_ischangesex(skill_id) )
  8086. continue;
  8087. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  8088. { //Only handle quest skills in a special way when you can't learn them manually
  8089. if( battle_config.quest_skill_reset && !(flag&2) )
  8090. { //Wipe them
  8091. sd->status.skill[idx].lv = 0;
  8092. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8093. }
  8094. continue;
  8095. }
  8096. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  8097. skill_point += lv;
  8098. else
  8099. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  8100. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  8101. if( !(flag&2) )
  8102. {// reset
  8103. sd->status.skill[idx].lv = 0;
  8104. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8105. }
  8106. }
  8107. if( flag&2 || !skill_point ) return skill_point;
  8108. sd->status.skill_point += skill_point;
  8109. if (flag&1) {
  8110. clif_updatestatus(sd,SP_SKILLPOINT);
  8111. clif_skillinfoblock(sd);
  8112. status_calc_pc(sd, SCO_FORCE);
  8113. }
  8114. return skill_point;
  8115. }
  8116. /*==========================================
  8117. * /resetfeel [Komurka]
  8118. *------------------------------------------*/
  8119. int pc_resetfeel(map_session_data* sd)
  8120. {
  8121. int i;
  8122. nullpo_ret(sd);
  8123. for (i=0; i<MAX_PC_FEELHATE; i++)
  8124. {
  8125. sd->feel_map[i].m = -1;
  8126. sd->feel_map[i].index = 0;
  8127. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  8128. }
  8129. return 0;
  8130. }
  8131. int pc_resethate(map_session_data* sd)
  8132. {
  8133. int i;
  8134. nullpo_ret(sd);
  8135. for (i=0; i<MAX_PC_FEELHATE; i++)
  8136. {
  8137. sd->hate_mob[i] = -1;
  8138. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  8139. }
  8140. return 0;
  8141. }
  8142. int pc_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8143. {
  8144. int bonus = 0;
  8145. nullpo_ret(sd);
  8146. skill_id = skill_dummy2skill_id(skill_id);
  8147. for (auto &it : sd->skillatk) {
  8148. if (it.id == skill_id) {
  8149. bonus += it.val;
  8150. break;
  8151. }
  8152. }
  8153. return bonus;
  8154. }
  8155. int pc_sub_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8156. {
  8157. int bonus = 0;
  8158. nullpo_ret(sd);
  8159. skill_id = skill_dummy2skill_id(skill_id);
  8160. for (auto &it : sd->subskill) {
  8161. if (it.id == skill_id) {
  8162. bonus += it.val;
  8163. break;
  8164. }
  8165. }
  8166. return bonus;
  8167. }
  8168. int pc_skillheal_bonus(map_session_data *sd, uint16 skill_id) {
  8169. int bonus = sd->bonus.add_heal_rate;
  8170. nullpo_ret(sd);
  8171. skill_id = skill_dummy2skill_id(skill_id);
  8172. if( bonus ) {
  8173. switch( skill_id ) {
  8174. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  8175. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  8176. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  8177. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  8178. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  8179. case AB_CHEAL: if (!(battle_config.skill_add_heal_rate & 32)) bonus = 0; break;
  8180. case AB_HIGHNESSHEAL: if (!(battle_config.skill_add_heal_rate & 64)) bonus = 0; break;
  8181. case CD_MEDIALE_VOTUM: if (!(battle_config.skill_add_heal_rate & 128)) bonus = 0; break;
  8182. case CD_DILECTIO_HEAL: if (!(battle_config.skill_add_heal_rate & 256)) bonus = 0; break;
  8183. }
  8184. }
  8185. for (auto &it : sd->skillheal) {
  8186. if (it.id == skill_id) {
  8187. bonus += it.val;
  8188. break;
  8189. }
  8190. }
  8191. return bonus;
  8192. }
  8193. int pc_skillheal2_bonus(map_session_data *sd, uint16 skill_id) {
  8194. int bonus = sd->bonus.add_heal2_rate;
  8195. skill_id = skill_dummy2skill_id(skill_id);
  8196. for (auto &it : sd->skillheal2) {
  8197. if (it.id == skill_id) {
  8198. bonus += it.val;
  8199. break;
  8200. }
  8201. }
  8202. return bonus;
  8203. }
  8204. void pc_respawn(map_session_data* sd, clr_type clrtype)
  8205. {
  8206. if( !pc_isdead(sd) )
  8207. return; // not applicable
  8208. if( sd->bg_id && bg_member_respawn(sd) )
  8209. return; // member revived by battleground
  8210. pc_setstand(sd, true);
  8211. pc_setrestartvalue(sd,3);
  8212. if( pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, clrtype ) != SETPOS_OK ){
  8213. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  8214. }
  8215. }
  8216. static TIMER_FUNC(pc_respawn_timer){
  8217. map_session_data *sd = map_id2sd(id);
  8218. if( sd != nullptr )
  8219. {
  8220. sd->pvp_point=0;
  8221. sd->respawn_tid = INVALID_TIMER;
  8222. pc_respawn(sd,CLR_OUTSIGHT);
  8223. }
  8224. return 0;
  8225. }
  8226. /*==========================================
  8227. * Invoked when a player has received damage
  8228. *------------------------------------------*/
  8229. void pc_damage(map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp, unsigned int ap)
  8230. {
  8231. if (ap) clif_updatestatus(sd,SP_AP);
  8232. if (sp) clif_updatestatus(sd,SP_SP);
  8233. if (hp) clif_updatestatus(sd,SP_HP);
  8234. else return;
  8235. if (!src)
  8236. return;
  8237. if( pc_issit(sd) ) {
  8238. pc_setstand(sd, true);
  8239. skill_sit(sd,0);
  8240. }
  8241. if (sd->progressbar.npc_id)
  8242. clif_progressbar_abort(sd);
  8243. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  8244. pet_target_check(sd->pd,src,1);
  8245. if( sd->status.ele_id > 0 )
  8246. elemental_set_target(sd,src);
  8247. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  8248. sd->canlog_tick = gettick();
  8249. }
  8250. TIMER_FUNC(pc_close_npc_timer){
  8251. TBL_PC *sd = map_id2sd(id);
  8252. if(sd) pc_close_npc(sd,data);
  8253. return 0;
  8254. }
  8255. /**
  8256. * Method to properly close a NPC for player and clear anything related.
  8257. * @param sd: Player attached
  8258. * @param flag: Method of closure
  8259. * 1: Produce a close button and end the NPC
  8260. * 2: End the NPC (best for no dialog windows)
  8261. */
  8262. void pc_close_npc(map_session_data *sd,int flag)
  8263. {
  8264. nullpo_retv(sd);
  8265. if (sd->npc_id || sd->npc_shopid) {
  8266. if (sd->state.using_fake_npc) {
  8267. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  8268. sd->state.using_fake_npc = 0;
  8269. }
  8270. if (sd->st) {
  8271. if(sd->st->state == RUN){ //wait ending code execution
  8272. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  8273. return;
  8274. }
  8275. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  8276. sd->st->mes_active = 0;
  8277. }
  8278. sd->state.menu_or_input = 0;
  8279. sd->npc_menu = 0;
  8280. sd->npc_shopid = 0;
  8281. #ifdef SECURE_NPCTIMEOUT
  8282. if( sd->npc_idle_timer != INVALID_TIMER ){
  8283. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  8284. sd->npc_idle_timer = INVALID_TIMER;
  8285. }
  8286. #endif
  8287. if (sd->st) {
  8288. if (sd->st->state == CLOSE) {
  8289. clif_scriptclose(sd, sd->npc_id);
  8290. clif_scriptclear( *sd, sd->npc_id ); // [Ind/Hercules]
  8291. sd->st->state = END; // Force to end now
  8292. }
  8293. if (sd->st->state == END) { // free attached scripts that are waiting
  8294. script_free_state(sd->st);
  8295. sd->st = nullptr;
  8296. sd->npc_id = 0;
  8297. }
  8298. }
  8299. }
  8300. }
  8301. /*==========================================
  8302. * Invoked when a player has negative current hp
  8303. *------------------------------------------*/
  8304. int pc_dead(map_session_data *sd,struct block_list *src)
  8305. {
  8306. int i=0,k=0;
  8307. t_tick tick = gettick();
  8308. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8309. // Activate Steel body if a super novice dies at 99+% exp [celest]
  8310. // Super Novices have no kill or die functions attached when saved by their angel
  8311. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  8312. t_exp exp = pc_nextbaseexp(sd);
  8313. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  8314. sd->state.snovice_dead_flag = 1;
  8315. pc_setrestartvalue(sd,1);
  8316. status_percent_heal(&sd->bl, 100, 100);
  8317. clif_resurrection(&sd->bl, 1);
  8318. pc_setinvincibletimer(sd, mapdata->getMapFlag(MF_INVINCIBLE_TIME));
  8319. sc_start(&sd->bl,&sd->bl,SC_STEELBODY,100,5,skill_get_time(MO_STEELBODY,5));
  8320. if(mapdata_flag_gvg2(mapdata))
  8321. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  8322. return 0;
  8323. }
  8324. }
  8325. for(k = 0; k < MAX_DEVOTION; k++) {
  8326. if (sd->devotion[k]){
  8327. map_session_data *devsd = map_id2sd(sd->devotion[k]);
  8328. if (devsd)
  8329. status_change_end(&devsd->bl, SC_DEVOTION);
  8330. sd->devotion[k] = 0;
  8331. }
  8332. }
  8333. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  8334. if (sd->stellar_mark[k]) {
  8335. map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  8336. if (smarksd)
  8337. status_change_end(&smarksd->bl, SC_FLASHKICK);
  8338. sd->stellar_mark[k] = 0;
  8339. }
  8340. }
  8341. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  8342. if (sd->united_soul[k]) {
  8343. map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  8344. if (usoulsd)
  8345. status_change_end(&usoulsd->bl, SC_SOULUNITY);
  8346. sd->united_soul[k] = 0;
  8347. }
  8348. }
  8349. for (k = 0; k < MAX_SERVANT_SIGN; k++) {
  8350. if (sd->servant_sign[k]) {
  8351. map_session_data *ssignsd = map_id2sd(sd->servant_sign[k]);
  8352. if (ssignsd)
  8353. status_change_end(&ssignsd->bl, SC_SERVANT_SIGN);
  8354. sd->servant_sign[k] = 0;
  8355. }
  8356. }
  8357. if(sd->shadowform_id) { //if we were target of shadowform
  8358. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM);
  8359. sd->shadowform_id = 0; //should be remove on status end anyway
  8360. }
  8361. if(sd->status.pet_id > 0 && sd->pd) {
  8362. struct pet_data *pd = sd->pd;
  8363. if( !mapdata->getMapFlag(MF_NOEXPPENALTY) ) {
  8364. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  8365. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  8366. }
  8367. if( sd->pd->target_id ) // Unlock all targets...
  8368. pet_unlocktarget(sd->pd);
  8369. }
  8370. 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)
  8371. hom_vaporize(sd, HOM_ST_ACTIVE);
  8372. if( sd->md )
  8373. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  8374. if( sd->ed )
  8375. elemental_delete(sd->ed);
  8376. // Leave duel if you die [LuzZza]
  8377. if(battle_config.duel_autoleave_when_die) {
  8378. if(sd->duel_group > 0)
  8379. duel_leave(sd->duel_group, sd);
  8380. if(sd->duel_invite > 0)
  8381. duel_reject(sd->duel_invite, sd);
  8382. }
  8383. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  8384. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  8385. sd->skill_keep_using.tid = INVALID_TIMER;
  8386. }
  8387. pc_close_npc(sd,2); //close npc if we were using one
  8388. /* e.g. not killed thru pc_damage */
  8389. if( pc_issit(sd) ) {
  8390. clif_status_load(&sd->bl,EFST_SIT,0);
  8391. }
  8392. pc_setdead(sd);
  8393. clif_party_dead( *sd );
  8394. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  8395. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  8396. if (battle_config.loose_ap_on_death == 1)
  8397. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  8398. //Reset menu skills/item skills
  8399. if ((sd->skillitem) != 0)
  8400. sd->skillitem = sd->skillitemlv = 0;
  8401. if ((sd->menuskill_id) != 0)
  8402. sd->menuskill_id = sd->menuskill_val = 0;
  8403. //Reset ticks.
  8404. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  8405. if ( sd->spiritball !=0 )
  8406. pc_delspiritball(sd,sd->spiritball,0);
  8407. if (sd->soulball != 0)
  8408. pc_delsoulball(sd, sd->soulball, false);
  8409. if (sd->servantball != 0)
  8410. pc_delservantball( *sd, sd->servantball );
  8411. if (sd->abyssball != 0)
  8412. pc_delabyssball( *sd, sd->abyssball );
  8413. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  8414. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  8415. if (src)
  8416. switch (src->type) {
  8417. case BL_MOB:
  8418. {
  8419. struct mob_data *md=(struct mob_data *)src;
  8420. if(md->target_id==sd->bl.id)
  8421. mob_unlocktarget(md,tick);
  8422. if(battle_config.mobs_level_up && md->status.hp &&
  8423. (unsigned int)md->level < pc_maxbaselv(sd) &&
  8424. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  8425. ) { // monster level up [Valaris]
  8426. clif_misceffect(&md->bl,0);
  8427. md->level++;
  8428. status_calc_mob(md, SCO_NONE);
  8429. status_percent_heal(src,10,0);
  8430. if( battle_config.show_mob_info&4 )
  8431. {// update name with new level
  8432. clif_name_area(&md->bl);
  8433. }
  8434. }
  8435. src = battle_get_master(src); // Maybe Player Summon
  8436. }
  8437. break;
  8438. case BL_PET: //Pass on to master...
  8439. case BL_HOM:
  8440. case BL_MER:
  8441. src = battle_get_master(src);
  8442. break;
  8443. }
  8444. if (src && src->type == BL_PC) {
  8445. map_session_data *ssd = (map_session_data *)src;
  8446. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  8447. npc_script_event(ssd, NPCE_KILLPC);
  8448. if (battle_config.pk_mode&2) {
  8449. ssd->status.manner -= 5;
  8450. if(ssd->status.manner < 0)
  8451. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  8452. #if 0
  8453. // PK/Karma system code (not enabled yet) [celest]
  8454. // originally from Kade Online, so i don't know if any of these is correct ^^;
  8455. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  8456. // karma going down = more 'good' / more honourable.
  8457. // The Karma System way...
  8458. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  8459. sd->status.karma--;
  8460. ssd->status.karma--;
  8461. }
  8462. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  8463. ssd->status.karma++;
  8464. // or the PK System way...
  8465. if (sd->status.karma > 0) // player killed is dishonourable?
  8466. ssd->status.karma--; // honour points earned
  8467. sd->status.karma++; // honour points lost
  8468. // To-do: Receive exp on certain occasions
  8469. #endif
  8470. }
  8471. }
  8472. if(battle_config.bone_drop==2
  8473. || (battle_config.bone_drop==1 && mapdata->getMapFlag(MF_PVP)))
  8474. {
  8475. struct item item_tmp;
  8476. memset(&item_tmp,0,sizeof(item_tmp));
  8477. item_tmp.nameid=ITEMID_SKULL_;
  8478. item_tmp.identify=1;
  8479. item_tmp.card[0]=CARD0_CREATE;
  8480. item_tmp.card[1]=0;
  8481. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  8482. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  8483. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  8484. }
  8485. //Remove bonus_script when dead
  8486. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  8487. // changed penalty options, added death by player if pk_mode [Valaris]
  8488. if(battle_config.death_penalty_type
  8489. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  8490. && !sd->sc.cant.deathpenalty
  8491. && !mapdata->getMapFlag(MF_NOEXPPENALTY) && !mapdata_flag_gvg2(mapdata))
  8492. {
  8493. t_exp base_penalty = 0;
  8494. t_exp job_penalty = 0;
  8495. uint32 zeny_penalty = 0;
  8496. if (pc_isvip(sd)) { // EXP penalty for VIP
  8497. base_penalty = battle_config.vip_exp_penalty_base;
  8498. job_penalty = battle_config.vip_exp_penalty_job;
  8499. zeny_penalty = battle_config.vip_zeny_penalty;
  8500. } else {
  8501. base_penalty = battle_config.death_penalty_base;
  8502. job_penalty = battle_config.death_penalty_job;
  8503. zeny_penalty = battle_config.zeny_penalty;
  8504. }
  8505. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  8506. switch (battle_config.death_penalty_type) {
  8507. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  8508. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  8509. }
  8510. if (base_penalty){ //recheck after altering to speedup
  8511. if (battle_config.pk_mode && src && src->type==BL_PC)
  8512. base_penalty *= 2;
  8513. base_penalty = u64min(sd->status.base_exp, base_penalty);
  8514. }
  8515. }
  8516. else
  8517. base_penalty = 0;
  8518. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  8519. switch (battle_config.death_penalty_type) {
  8520. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  8521. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  8522. }
  8523. if (job_penalty) {
  8524. if (battle_config.pk_mode && src && src->type==BL_PC)
  8525. job_penalty *= 2;
  8526. job_penalty = u64min(sd->status.job_exp, job_penalty);
  8527. }
  8528. }
  8529. else
  8530. job_penalty = 0;
  8531. if (base_penalty || job_penalty) {
  8532. short insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  8533. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  8534. pc_lostexp(sd, base_penalty, job_penalty);
  8535. }
  8536. if( zeny_penalty > 0 && !mapdata->getMapFlag(MF_NOZENYPENALTY)) {
  8537. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  8538. if(zeny_penalty)
  8539. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER);
  8540. }
  8541. }
  8542. if( mapdata->getMapFlag(MF_PVP_NIGHTMAREDROP) ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  8543. for (const auto &it : mapdata->drop_list) {
  8544. int id = it.drop_id, per = it.drop_per;
  8545. enum e_nightmare_drop_type type = it.drop_type;
  8546. if(id == 0)
  8547. continue;
  8548. if(id == -1){
  8549. int eq_num=0,eq_n[MAX_INVENTORY];
  8550. memset(eq_n,0,sizeof(eq_n));
  8551. for(i=0;i<MAX_INVENTORY;i++) {
  8552. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8553. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8554. || type&NMDT_ALL)
  8555. {
  8556. int l;
  8557. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  8558. if( l < MAX_INVENTORY )
  8559. eq_n[l] = i;
  8560. eq_num++;
  8561. }
  8562. }
  8563. if(eq_num > 0){
  8564. int n = eq_n[rnd()%eq_num];
  8565. if(rnd()%10000 < per) {
  8566. if(sd->inventory.u.items_inventory[n].equip)
  8567. pc_unequipitem(sd,n,3);
  8568. pc_dropitem(sd,n,1);
  8569. }
  8570. }
  8571. }
  8572. else if(id > 0) {
  8573. for(i=0;i<MAX_INVENTORY;i++){
  8574. if(sd->inventory.u.items_inventory[i].nameid == id
  8575. && rnd()%10000 < per
  8576. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8577. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8578. || type&NMDT_ALL) ){
  8579. if(sd->inventory.u.items_inventory[i].equip)
  8580. pc_unequipitem(sd,i,3);
  8581. pc_dropitem(sd,i,1);
  8582. break;
  8583. }
  8584. }
  8585. }
  8586. }
  8587. }
  8588. // pvp
  8589. // disable certain pvp functions on pk_mode [Valaris]
  8590. if( !battle_config.pk_mode && mapdata->getMapFlag(MF_PVP) && !mapdata->getMapFlag(MF_PVP_NOCALCRANK) ) {
  8591. sd->pvp_point -= 5;
  8592. sd->pvp_lost++;
  8593. if( src && src->type == BL_PC ) {
  8594. map_session_data *ssd = (map_session_data *)src;
  8595. ssd->pvp_point++;
  8596. ssd->pvp_won++;
  8597. }
  8598. if( sd->pvp_point < 0 ) {
  8599. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  8600. return 1|8;
  8601. }
  8602. }
  8603. //GvG
  8604. if( mapdata_flag_gvg2(mapdata) ) {
  8605. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  8606. return 1|8;
  8607. }
  8608. else if( sd->bg_id ) {
  8609. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  8610. if (bg) {
  8611. if (bg->cemetery.map > 0) { // Respawn by BG
  8612. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  8613. return 1|8;
  8614. }
  8615. }
  8616. }
  8617. //Reset "can log out" tick.
  8618. if( battle_config.prevent_logout )
  8619. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  8620. return 1;
  8621. }
  8622. void pc_revive(map_session_data *sd,unsigned int hp, unsigned int sp, unsigned int ap) {
  8623. if(hp) clif_updatestatus(sd,SP_HP);
  8624. if(sp) clif_updatestatus(sd,SP_SP);
  8625. if(ap) clif_updatestatus(sd,SP_AP);
  8626. pc_setstand(sd, true);
  8627. pc_setinvincibletimer(sd, map_getmapflag(sd->bl.m, MF_INVINCIBLE_TIME));
  8628. if (sd->state.gmaster_flag && sd->guild) {
  8629. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild->guild,GD_LEADERSHIP));
  8630. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild->guild,GD_GLORYWOUNDS));
  8631. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild->guild,GD_SOULCOLD));
  8632. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild->guild,GD_HAWKEYES));
  8633. }
  8634. }
  8635. bool pc_revive_item(map_session_data *sd) {
  8636. nullpo_retr(false, sd);
  8637. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  8638. return false;
  8639. if (sd->sc.getSCE(SC_HELLPOWER)) // Cannot resurrect while under the effect of SC_HELLPOWER.
  8640. return false;
  8641. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  8642. uint8 hp = 100, sp = 100;
  8643. if (item_position < 0) {
  8644. if (sd->sc.getSCE(SC_LIGHT_OF_REGENE)) {
  8645. hp = sd->sc.getSCE(SC_LIGHT_OF_REGENE)->val2;
  8646. sp = 0;
  8647. }
  8648. else
  8649. return false;
  8650. }
  8651. if (!status_revive(&sd->bl, hp, sp))
  8652. return false;
  8653. if (item_position < 0)
  8654. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE);
  8655. else
  8656. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  8657. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  8658. return true;
  8659. }
  8660. // script
  8661. //
  8662. /*==========================================
  8663. * script reading pc status registry
  8664. *------------------------------------------*/
  8665. int64 pc_readparam(map_session_data* sd,int64 type)
  8666. {
  8667. int64 val = 0;
  8668. nullpo_ret(sd);
  8669. switch(type) {
  8670. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  8671. case SP_STATUSPOINT: val = sd->status.status_point; break;
  8672. case SP_TRAITPOINT: val = sd->status.trait_point; break;
  8673. case SP_ZENY: val = sd->status.zeny; break;
  8674. case SP_BASELEVEL: val = sd->status.base_level; break;
  8675. case SP_JOBLEVEL: val = sd->status.job_level; break;
  8676. case SP_CLASS: val = sd->status.class_; break;
  8677. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  8678. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  8679. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  8680. case SP_SEX: val = sd->status.sex; break;
  8681. case SP_WEIGHT: val = sd->weight; break;
  8682. case SP_MAXWEIGHT: val = sd->max_weight; break;
  8683. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  8684. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  8685. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  8686. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  8687. case SP_HP: val = sd->battle_status.hp; break;
  8688. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  8689. case SP_SP: val = sd->battle_status.sp; break;
  8690. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  8691. case SP_AP: val = sd->battle_status.ap; break;
  8692. case SP_MAXAP: val = sd->battle_status.max_ap; break;
  8693. case SP_STR: val = sd->status.str; break;
  8694. case SP_AGI: val = sd->status.agi; break;
  8695. case SP_VIT: val = sd->status.vit; break;
  8696. case SP_INT: val = sd->status.int_; break;
  8697. case SP_DEX: val = sd->status.dex; break;
  8698. case SP_LUK: val = sd->status.luk; break;
  8699. case SP_POW: val = sd->status.pow; break;
  8700. case SP_STA: val = sd->status.sta; break;
  8701. case SP_WIS: val = sd->status.wis; break;
  8702. case SP_SPL: val = sd->status.spl; break;
  8703. case SP_CON: val = sd->status.con; break;
  8704. case SP_CRT: val = sd->status.crt; break;
  8705. case SP_KARMA: val = sd->status.karma; break;
  8706. case SP_MANNER: val = sd->status.manner; break;
  8707. case SP_FAME: val = sd->status.fame; break;
  8708. case SP_KILLERRID: val = sd->killerrid; break;
  8709. case SP_KILLEDRID: val = sd->killedrid; break;
  8710. case SP_KILLEDGID: val = sd->killedgid; break;
  8711. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  8712. case SP_CHARMOVE: val = sd->status.character_moves; break;
  8713. case SP_CHARRENAME: val = sd->status.rename; break;
  8714. case SP_CHARFONT: val = sd->status.font; break;
  8715. case SP_BANK_VAULT: val = sd->bank_vault; break;
  8716. case SP_CASHPOINTS: val = sd->cashPoints; break;
  8717. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  8718. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  8719. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  8720. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  8721. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  8722. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  8723. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  8724. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  8725. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  8726. case SP_BASE_ATK:
  8727. #ifdef RENEWAL
  8728. val = sd->bonus.eatk;
  8729. #else
  8730. val = sd->battle_status.batk;
  8731. #endif
  8732. break;
  8733. case SP_DEF1: val = sd->battle_status.def; break;
  8734. case SP_DEF2: val = sd->battle_status.def2; break;
  8735. case SP_MDEF1: val = sd->battle_status.mdef; break;
  8736. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  8737. case SP_HIT: val = sd->battle_status.hit; break;
  8738. case SP_FLEE1: val = sd->battle_status.flee; break;
  8739. case SP_FLEE2: val = sd->battle_status.flee2; break;
  8740. case SP_PATK: val = sd->battle_status.patk; break;
  8741. case SP_SMATK: val = sd->battle_status.smatk; break;
  8742. case SP_RES: val = sd->battle_status.res; break;
  8743. case SP_MRES: val = sd->battle_status.mres; break;
  8744. case SP_HPLUS: val = sd->battle_status.hplus; break;
  8745. case SP_CRATE: val = sd->battle_status.crate; break;
  8746. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  8747. case SP_MAXHPRATE: val = sd->hprate; break;
  8748. case SP_MAXSPRATE: val = sd->sprate; break;
  8749. case SP_MAXAPRATE: val = sd->aprate; break;
  8750. case SP_SPRATE: val = sd->dsprate; break;
  8751. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  8752. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  8753. case SP_ASPD_RATE:
  8754. #ifndef RENEWAL_ASPD
  8755. val = sd->battle_status.aspd_rate;
  8756. #else
  8757. val = sd->battle_status.aspd_rate2;
  8758. #endif
  8759. break;
  8760. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  8761. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  8762. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  8763. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  8764. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  8765. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  8766. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  8767. case SP_MATK_RATE: val = sd->matk_rate; break;
  8768. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  8769. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  8770. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  8771. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  8772. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  8773. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  8774. case SP_HIT_RATE: val = sd->hit_rate; break;
  8775. case SP_FLEE_RATE: val = sd->flee_rate; break;
  8776. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  8777. case SP_DEF_RATE: val = sd->def_rate; break;
  8778. case SP_DEF2_RATE: val = sd->def2_rate; break;
  8779. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  8780. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  8781. case SP_PATK_RATE: val = sd->patk_rate; break;
  8782. case SP_SMATK_RATE: val = sd->smatk_rate; break;
  8783. case SP_RES_RATE: val = sd->res_rate; break;
  8784. case SP_MRES_RATE: val = sd->mres_rate; break;
  8785. case SP_HPLUS_RATE: val = sd->hplus_rate; break;
  8786. case SP_CRATE_RATE: val = sd->crate_rate; break;
  8787. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  8788. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  8789. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  8790. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  8791. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  8792. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  8793. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  8794. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  8795. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  8796. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  8797. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  8798. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  8799. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  8800. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  8801. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  8802. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  8803. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  8804. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  8805. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  8806. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  8807. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  8808. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  8809. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  8810. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  8811. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  8812. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  8813. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  8814. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  8815. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  8816. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  8817. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  8818. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  8819. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  8820. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  8821. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  8822. case SP_UNSTRIPABLE:
  8823. case SP_UNSTRIPABLE_ARMOR:
  8824. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  8825. break;
  8826. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  8827. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  8828. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  8829. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  8830. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  8831. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  8832. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  8833. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  8834. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  8835. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  8836. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  8837. case SP_EMATK: val = sd->bonus.ematk; break;
  8838. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  8839. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  8840. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  8841. case SP_CASTRATE:
  8842. case SP_VARCASTRATE:
  8843. #ifdef RENEWAL_CAST
  8844. val = sd->bonus.varcastrate; break;
  8845. #else
  8846. val = sd->castrate; break;
  8847. #endif
  8848. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  8849. case SP_ADD_ITEM_SPHEAL_RATE: val = sd->bonus.itemsphealrate2; break;
  8850. default:
  8851. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  8852. return -1;
  8853. }
  8854. return val;
  8855. }
  8856. /*==========================================
  8857. * script set pc status registry
  8858. *------------------------------------------*/
  8859. bool pc_setparam(map_session_data *sd,int64 type,int64 val_tmp)
  8860. {
  8861. nullpo_retr(false,sd);
  8862. int val = static_cast<unsigned int>(val_tmp);
  8863. switch(type){
  8864. case SP_BASELEVEL:
  8865. if (val > pc_maxbaselv(sd)) //Capping to max
  8866. val = pc_maxbaselv(sd);
  8867. if (val > sd->status.base_level) {
  8868. for( int i = 0; i < (int)( val - sd->status.base_level ); i++ ){
  8869. sd->status.status_point += statpoint_db.pc_gets_status_point( sd->status.base_level + i );
  8870. sd->status.trait_point += statpoint_db.pc_gets_trait_point( sd->status.base_level + i );
  8871. }
  8872. }
  8873. sd->status.base_level = val;
  8874. sd->status.base_exp = 0;
  8875. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  8876. clif_updatestatus(sd, SP_NEXTBASEEXP);
  8877. clif_updatestatus(sd, SP_STATUSPOINT);
  8878. clif_updatestatus(sd, SP_TRAITPOINT);
  8879. clif_updatestatus(sd, SP_BASEEXP);
  8880. status_calc_pc(sd, SCO_FORCE);
  8881. if(sd->status.party_id)
  8882. party_send_levelup(sd);
  8883. break;
  8884. case SP_JOBLEVEL:
  8885. if (val >= sd->status.job_level) {
  8886. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  8887. sd->status.skill_point += val - sd->status.job_level;
  8888. clif_updatestatus(sd, SP_SKILLPOINT);
  8889. }
  8890. sd->status.job_level = val;
  8891. sd->status.job_exp = 0;
  8892. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  8893. clif_updatestatus(sd, SP_NEXTJOBEXP);
  8894. clif_updatestatus(sd, SP_JOBEXP);
  8895. status_calc_pc(sd, SCO_FORCE);
  8896. break;
  8897. case SP_SKILLPOINT:
  8898. sd->status.skill_point = val;
  8899. break;
  8900. case SP_STATUSPOINT:
  8901. sd->status.status_point = val;
  8902. break;
  8903. case SP_TRAITPOINT:
  8904. sd->status.trait_point = val;
  8905. break;
  8906. case SP_ZENY:
  8907. if( val < 0 )
  8908. return false;// can't set negative zeny
  8909. log_zeny(*sd, LOG_TYPE_SCRIPT, sd->status.char_id, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  8910. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  8911. break;
  8912. case SP_BASEEXP:
  8913. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  8914. if (val_tmp < sd->status.base_exp) // Lost
  8915. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  8916. else // Gained
  8917. pc_gainexp(sd, nullptr, val_tmp - sd->status.base_exp, 0, 2);
  8918. return true;
  8919. case SP_JOBEXP:
  8920. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  8921. if (val_tmp < sd->status.job_exp) // Lost
  8922. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  8923. else // Gained
  8924. pc_gainexp(sd, nullptr, 0, val_tmp - sd->status.job_exp, 2);
  8925. return true;
  8926. case SP_SEX:
  8927. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  8928. break;
  8929. case SP_WEIGHT:
  8930. sd->weight = val;
  8931. break;
  8932. case SP_MAXWEIGHT:
  8933. sd->max_weight = val;
  8934. break;
  8935. case SP_HP:
  8936. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  8937. break;
  8938. case SP_MAXHP:
  8939. if (sd->status.base_level < 100)
  8940. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  8941. else if (sd->status.base_level < 151)
  8942. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  8943. else
  8944. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  8945. if( sd->battle_status.max_hp < sd->battle_status.hp )
  8946. {
  8947. sd->battle_status.hp = sd->battle_status.max_hp;
  8948. clif_updatestatus(sd, SP_HP);
  8949. }
  8950. break;
  8951. case SP_SP:
  8952. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  8953. break;
  8954. case SP_MAXSP:
  8955. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  8956. if( sd->battle_status.max_sp < sd->battle_status.sp )
  8957. {
  8958. sd->battle_status.sp = sd->battle_status.max_sp;
  8959. clif_updatestatus(sd, SP_SP);
  8960. }
  8961. break;
  8962. case SP_AP:
  8963. sd->battle_status.ap = cap_value(val, 0, (int)sd->battle_status.max_ap);
  8964. break;
  8965. case SP_MAXAP:
  8966. sd->battle_status.max_ap = cap_value(val, 1, battle_config.max_ap);
  8967. if (sd->battle_status.max_ap < sd->battle_status.ap) {
  8968. sd->battle_status.ap = sd->battle_status.max_ap;
  8969. clif_updatestatus(sd, SP_AP);
  8970. }
  8971. break;
  8972. case SP_STR:
  8973. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  8974. break;
  8975. case SP_AGI:
  8976. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  8977. break;
  8978. case SP_VIT:
  8979. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  8980. break;
  8981. case SP_INT:
  8982. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  8983. break;
  8984. case SP_DEX:
  8985. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  8986. break;
  8987. case SP_LUK:
  8988. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  8989. break;
  8990. case SP_POW:
  8991. sd->status.pow = cap_value(val, 0, pc_maxparameter(sd,PARAM_POW));
  8992. break;
  8993. case SP_STA:
  8994. sd->status.sta = cap_value(val, 0, pc_maxparameter(sd,PARAM_STA));
  8995. break;
  8996. case SP_WIS:
  8997. sd->status.wis = cap_value(val, 0, pc_maxparameter(sd,PARAM_WIS));
  8998. break;
  8999. case SP_SPL:
  9000. sd->status.spl = cap_value(val, 0, pc_maxparameter(sd,PARAM_SPL));
  9001. break;
  9002. case SP_CON:
  9003. sd->status.con = cap_value(val, 0, pc_maxparameter(sd,PARAM_CON));
  9004. break;
  9005. case SP_CRT:
  9006. sd->status.crt = cap_value(val, 0, pc_maxparameter(sd,PARAM_CRT));
  9007. break;
  9008. case SP_KARMA:
  9009. sd->status.karma = val;
  9010. break;
  9011. case SP_MANNER:
  9012. sd->status.manner = val;
  9013. if( val < 0 )
  9014. sc_start(nullptr, &sd->bl, SC_NOCHAT, 100, 0, 0);
  9015. else {
  9016. status_change_end(&sd->bl, SC_NOCHAT);
  9017. clif_manner_message(sd, 5);
  9018. }
  9019. return true; // status_change_start/status_change_end already sends packets warning the client
  9020. case SP_FAME:
  9021. sd->status.fame = val;
  9022. break;
  9023. case SP_KILLERRID:
  9024. sd->killerrid = val;
  9025. return true;
  9026. case SP_KILLEDRID:
  9027. sd->killedrid = val;
  9028. return true;
  9029. case SP_KILLEDGID:
  9030. sd->killedgid = val;
  9031. return true;
  9032. case SP_CHARMOVE:
  9033. sd->status.character_moves = val;
  9034. return true;
  9035. case SP_CHARRENAME:
  9036. sd->status.rename = val;
  9037. return true;
  9038. case SP_CHARFONT:
  9039. sd->status.font = val;
  9040. clif_font(sd);
  9041. return true;
  9042. case SP_BANK_VAULT:
  9043. if (val < 0)
  9044. return false;
  9045. log_zeny(*sd, LOG_TYPE_BANK, sd->status.char_id, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  9046. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  9047. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  9048. return true;
  9049. case SP_ROULETTE_BRONZE:
  9050. sd->roulette_point.bronze = val;
  9051. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  9052. return true;
  9053. case SP_ROULETTE_SILVER:
  9054. sd->roulette_point.silver = val;
  9055. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  9056. return true;
  9057. case SP_ROULETTE_GOLD:
  9058. sd->roulette_point.gold = val;
  9059. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  9060. return true;
  9061. case SP_CASHPOINTS:
  9062. if (val < 0)
  9063. return false;
  9064. if (!sd->state.connect_new)
  9065. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_CASHPOINT)));
  9066. sd->cashPoints = cap_value(val, 0, MAX_CASHPOINT);
  9067. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  9068. return true;
  9069. case SP_KAFRAPOINTS:
  9070. if (val < 0)
  9071. return false;
  9072. if (!sd->state.connect_new)
  9073. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_KAFRAPOINT)));
  9074. sd->kafraPoints = cap_value(val, 0, MAX_KAFRAPOINT);
  9075. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  9076. return true;
  9077. case SP_PCDIECOUNTER:
  9078. if (val < 0)
  9079. return false;
  9080. if (sd->die_counter == val)
  9081. return true;
  9082. sd->die_counter = val;
  9083. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  9084. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  9085. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  9086. return true;
  9087. case SP_COOKMASTERY:
  9088. if (sd->cook_mastery == val)
  9089. return true;
  9090. val = cap_value(val, 0, 1999);
  9091. sd->cook_mastery = val;
  9092. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  9093. return true;
  9094. default:
  9095. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  9096. return false;
  9097. }
  9098. clif_updatestatus(sd,static_cast<int>(type));
  9099. return true;
  9100. }
  9101. /*==========================================
  9102. * HP/SP/AP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  9103. *------------------------------------------*/
  9104. void pc_heal(map_session_data *sd,unsigned int hp,unsigned int sp, unsigned int ap, int type)
  9105. {// Is there going to be a effect for gaining AP soon??? [Rytech]
  9106. nullpo_retv(sd);
  9107. if (type&2) {
  9108. if (hp || type&4) {
  9109. clif_heal(sd->fd,SP_HP,hp);
  9110. clif_update_hp(*sd);
  9111. }
  9112. if (sp)
  9113. clif_heal(sd->fd,SP_SP,sp);
  9114. if (ap)
  9115. clif_heal(sd->fd,SP_AP,ap);
  9116. } else {
  9117. if(hp)
  9118. clif_updatestatus(sd,SP_HP);
  9119. if(sp)
  9120. clif_updatestatus(sd,SP_SP);
  9121. if (ap)
  9122. clif_updatestatus(sd,SP_AP);
  9123. }
  9124. return;
  9125. }
  9126. /**
  9127. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  9128. * @param sd: Player data
  9129. * @param itemid: Item ID
  9130. * @param hp: HP to heal
  9131. * @param sp: SP to heal
  9132. * @return Amount healed to an object
  9133. */
  9134. int pc_itemheal(map_session_data *sd, t_itemid itemid, int hp, int sp)
  9135. {
  9136. int bonus, tmp, penalty = 0;
  9137. if (hp) {
  9138. bonus = 100 + (sd->battle_status.vit * 2) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9139. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9140. if (potion_flag == 2) {
  9141. bonus += bonus * 50 / 100;
  9142. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_ROGUE)
  9143. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  9144. }
  9145. //All item bonuses.
  9146. bonus += sd->bonus.itemhealrate2;
  9147. //Item Group bonuses
  9148. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid, sd->itemgrouphealrate) / 100;
  9149. //Individual item bonuses.
  9150. for(const auto &it : sd->itemhealrate) {
  9151. if (it.id == itemid) {
  9152. bonus += bonus * it.val / 100;
  9153. break;
  9154. }
  9155. }
  9156. // Recovery Potion
  9157. if (sd->sc.getSCE(SC_INCHEALRATE))
  9158. bonus += bonus * sd->sc.getSCE(SC_INCHEALRATE)->val1 / 100;
  9159. // 2014 Halloween Event : Pumpkin Bonus
  9160. if (sd->sc.getSCE(SC_MTF_PUMPKIN)) {
  9161. if (itemid == ITEMID_PUMPKIN)
  9162. bonus += bonus * sd->sc.getSCE(SC_MTF_PUMPKIN)->val1 / 100;
  9163. else if (itemid == ITEMID_COOKIE_BAT)
  9164. bonus += sd->sc.getSCE(SC_MTF_PUMPKIN)->val2;
  9165. }
  9166. if (sd->sc.getSCE(SC_VITALIZE_POTION))
  9167. bonus += bonus * 10 / 100;
  9168. tmp = hp * bonus / 100; // Overflow check
  9169. if (bonus != 100 && tmp > hp)
  9170. hp = tmp;
  9171. }
  9172. if (sp) {
  9173. bonus = 100 + (sd->battle_status.int_ * 2) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9174. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9175. if (potion_flag == 2)
  9176. bonus += bonus * 50 / 100;
  9177. // All item bonuses.
  9178. bonus += sd->bonus.itemsphealrate2;
  9179. // Item Group bonuses
  9180. bonus += bonus * pc_get_itemgroup_bonus( sd, itemid, sd->itemgroupsphealrate ) / 100;
  9181. // Individual item bonuses.
  9182. for( const auto &it : sd->itemsphealrate ){
  9183. if( it.id == itemid ){
  9184. bonus += bonus * it.val / 100;
  9185. break;
  9186. }
  9187. }
  9188. tmp = sp * bonus / 100; // Overflow check
  9189. if (bonus != 100 && tmp > sp)
  9190. sp = tmp;
  9191. }
  9192. if (sd->sc.count) {
  9193. // Critical Wound and Death Hurt stack
  9194. if (sd->sc.getSCE(SC_CRITICALWOUND))
  9195. penalty += sd->sc.getSCE(SC_CRITICALWOUND)->val2;
  9196. if (sd->sc.getSCE(SC_DEATHHURT) && sd->sc.getSCE(SC_DEATHHURT)->val3 == 1)
  9197. penalty += 20;
  9198. if (sd->sc.getSCE(SC_NORECOVER_STATE))
  9199. penalty = 100;
  9200. if (sd->sc.getSCE(SC_VITALITYACTIVATION))
  9201. hp += hp / 2; // 1.5 times
  9202. if (sd->sc.getSCE(SC_WATER_INSIGNIA) && sd->sc.getSCE(SC_WATER_INSIGNIA)->val1 == 2) {
  9203. hp += hp / 10;
  9204. sp += sp / 10;
  9205. }
  9206. #ifdef RENEWAL
  9207. if (sd->sc.getSCE(SC_APPLEIDUN))
  9208. hp += sd->sc.getSCE(SC_APPLEIDUN)->val3 / 100;
  9209. #endif
  9210. if (penalty > 0) {
  9211. hp -= hp * penalty / 100;
  9212. sp -= sp * penalty / 100;
  9213. }
  9214. #ifdef RENEWAL
  9215. if (sd->sc.getSCE(SC_EXTREMITYFIST2))
  9216. sp = 0;
  9217. #endif
  9218. if (sd->sc.getSCE(SC_BITESCAR))
  9219. hp = 0;
  9220. }
  9221. return status_heal(&sd->bl, hp, sp, 1);
  9222. }
  9223. /*==========================================
  9224. * HP/SP Recovery
  9225. * Heal player hp nad/or sp by rate
  9226. *------------------------------------------*/
  9227. int pc_percentheal(map_session_data *sd,int hp,int sp)
  9228. {
  9229. nullpo_ret(sd);
  9230. if (hp > 100) hp = 100;
  9231. else if (hp <-100) hp = -100;
  9232. if (sp > 100) sp = 100;
  9233. else if (sp <-100) sp = -100;
  9234. if(hp >= 0 && sp >= 0) //Heal
  9235. return status_percent_heal(&sd->bl, hp, sp);
  9236. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  9237. return status_percent_damage(nullptr, &sd->bl, hp, sp, hp==-100);
  9238. //Crossed signs
  9239. if(hp) {
  9240. if(hp > 0)
  9241. status_percent_heal(&sd->bl, hp, 0);
  9242. else
  9243. status_percent_damage(nullptr, &sd->bl, hp, 0, hp==-100);
  9244. }
  9245. if(sp) {
  9246. if(sp > 0)
  9247. status_percent_heal(&sd->bl, 0, sp);
  9248. else
  9249. status_percent_damage(nullptr, &sd->bl, 0, sp, false);
  9250. }
  9251. return 0;
  9252. }
  9253. static int jobchange_killclone(struct block_list *bl, va_list ap)
  9254. {
  9255. struct mob_data *md;
  9256. int flag;
  9257. md = (struct mob_data *)bl;
  9258. nullpo_ret(md);
  9259. flag = va_arg(ap, int);
  9260. if (md->master_id && md->special_state.clone && md->master_id == flag)
  9261. status_kill(&md->bl);
  9262. return 1;
  9263. }
  9264. /**
  9265. * Called when player changes job
  9266. * Rewrote to make it tidider [Celest]
  9267. * @param sd
  9268. * @param job JOB ID. See enum e_job
  9269. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  9270. * @return True if success, false if failed
  9271. **/
  9272. bool pc_jobchange(map_session_data *sd,int job, char upper)
  9273. {
  9274. int i, fame_flag = 0;
  9275. nullpo_retr(false,sd);
  9276. if (job < 0)
  9277. return false;
  9278. //Normalize job.
  9279. uint64 b_class = pc_jobid2mapid(job);
  9280. if (b_class == -1)
  9281. return false;
  9282. switch (upper) {
  9283. case 1:
  9284. b_class|= JOBL_UPPER;
  9285. break;
  9286. case 2:
  9287. b_class|= JOBL_BABY;
  9288. break;
  9289. }
  9290. //This will automatically adjust bard/dancer classes to the correct gender
  9291. //That is, if you try to jobchange into dancer, it will turn you to bard.
  9292. job = pc_mapid2jobid(b_class, sd->status.sex);
  9293. if (job == -1)
  9294. return false;
  9295. if ((unsigned short)b_class == sd->class_)
  9296. return false; //Nothing to change.
  9297. // If the job does not exist in the job db, dont allow changing to it
  9298. if( !job_db.exists( job ) ){
  9299. return false;
  9300. }
  9301. if( ( b_class&JOBL_FOURTH ) && !( sd->class_&JOBL_FOURTH ) ){
  9302. // Changing to 4th job
  9303. sd->change_level_4th = sd->status.job_level;
  9304. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ) , sd->change_level_4th );
  9305. }else if( ( b_class&JOBL_THIRD ) && !( sd->class_&JOBL_THIRD ) ){
  9306. // changing from 2nd to 3rd job
  9307. sd->change_level_3rd = sd->status.job_level;
  9308. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  9309. }else if( ( b_class&JOBL_2 ) && !( sd->class_&JOBL_2 ) ){
  9310. // changing from 1st to 2nd job
  9311. sd->change_level_2nd = sd->status.job_level;
  9312. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  9313. }
  9314. if(sd->cloneskill_idx > 0) {
  9315. pc_skill_plagiarism_reset(*sd, 1);
  9316. }
  9317. if(sd->reproduceskill_idx > 0) {
  9318. pc_skill_plagiarism_reset(*sd, 2);
  9319. }
  9320. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  9321. status_db.changeSkillTree(sd);
  9322. }
  9323. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  9324. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  9325. pc_resetfeel(sd);
  9326. pc_resethate(sd);
  9327. }
  9328. // Reset body style to 0 before changing job to avoid
  9329. // errors since not every job has a alternate outfit.
  9330. sd->status.body = 0;
  9331. clif_changelook(&sd->bl,LOOK_BODY2,0);
  9332. sd->status.class_ = job;
  9333. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  9334. uint64 previous_class = sd->class_;
  9335. sd->class_ = (unsigned short)b_class;
  9336. sd->status.job_level=1;
  9337. sd->status.job_exp=0;
  9338. if (sd->status.base_level > pc_maxbaselv(sd)) {
  9339. sd->status.base_level = pc_maxbaselv(sd);
  9340. sd->status.base_exp=0;
  9341. pc_resetstate(sd);
  9342. clif_updatestatus(sd,SP_STATUSPOINT);
  9343. clif_updatestatus(sd,SP_TRAITPOINT);
  9344. clif_updatestatus(sd,SP_BASELEVEL);
  9345. clif_updatestatus(sd,SP_BASEEXP);
  9346. clif_updatestatus(sd,SP_NEXTBASEEXP);
  9347. }
  9348. // Give or reduce transcendent status points
  9349. if( (b_class&JOBL_UPPER) && !(previous_class&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  9350. sd->status.status_point += battle_config.transcendent_status_points;
  9351. clif_updatestatus(sd,SP_STATUSPOINT);
  9352. }else if( !(b_class&JOBL_UPPER) && (previous_class&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  9353. if( sd->status.status_point < battle_config.transcendent_status_points ){
  9354. // The player already used his bonus points, so we have to reset his status points
  9355. pc_resetstate(sd);
  9356. }else{
  9357. sd->status.status_point -= battle_config.transcendent_status_points;
  9358. clif_updatestatus(sd,SP_STATUSPOINT);
  9359. }
  9360. }
  9361. // Give or reduce trait status points
  9362. if ((b_class & JOBL_FOURTH) && !(previous_class & JOBL_FOURTH)) {// Change to a 4th job.
  9363. sd->status.trait_point += battle_config.trait_points_job_change;
  9364. clif_updatestatus(sd, SP_TRAITPOINT);
  9365. clif_updatestatus(sd, SP_UPOW);
  9366. clif_updatestatus(sd, SP_USTA);
  9367. clif_updatestatus(sd, SP_UWIS);
  9368. clif_updatestatus(sd, SP_USPL);
  9369. clif_updatestatus(sd, SP_UCON);
  9370. clif_updatestatus(sd, SP_UCRT);
  9371. } else if (!(b_class & JOBL_FOURTH) && (previous_class & JOBL_FOURTH)) {// Change to a non 4th job.
  9372. if (sd->status.trait_point < battle_config.trait_points_job_change) {
  9373. // Player may have already used the trait status points. Force a reset.
  9374. pc_resetstate(sd);
  9375. } else {
  9376. sd->status.trait_point = 0;
  9377. clif_updatestatus(sd, SP_TRAITPOINT);
  9378. clif_updatestatus(sd, SP_UPOW);
  9379. clif_updatestatus(sd, SP_USTA);
  9380. clif_updatestatus(sd, SP_UWIS);
  9381. clif_updatestatus(sd, SP_USPL);
  9382. clif_updatestatus(sd, SP_UCON);
  9383. clif_updatestatus(sd, SP_UCRT);
  9384. }
  9385. }
  9386. clif_updatestatus(sd,SP_JOBLEVEL);
  9387. clif_updatestatus(sd,SP_JOBEXP);
  9388. clif_updatestatus(sd,SP_NEXTJOBEXP);
  9389. for(i=0;i<EQI_MAX;i++) {
  9390. if(sd->equip_index[i] >= 0)
  9391. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  9392. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  9393. }
  9394. }
  9395. //Change look, if disguised, you need to undisguise
  9396. //to correctly calculate new job sprite without
  9397. if (sd->disguise)
  9398. pc_disguise(sd, 0);
  9399. status_set_viewdata(&sd->bl, job);
  9400. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  9401. #if PACKETVER >= 20151001
  9402. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  9403. #endif
  9404. if(sd->vd.cloth_color)
  9405. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9406. /*
  9407. if(sd->vd.body_style)
  9408. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9409. */
  9410. //Update skill tree.
  9411. pc_calc_skilltree(sd);
  9412. clif_skillinfoblock(sd);
  9413. if (sd->ed)
  9414. elemental_delete(sd->ed);
  9415. if (sd->state.vending)
  9416. vending_closevending(sd);
  9417. if (sd->state.buyingstore)
  9418. buyingstore_close(sd);
  9419. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  9420. //Remove peco/cart/falcon
  9421. i = sd->sc.option;
  9422. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  9423. i&=~OPTION_RIDING;
  9424. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  9425. i&=~OPTION_FALCON;
  9426. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  9427. i&=~OPTION_DRAGON;
  9428. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  9429. i&=~OPTION_WUGRIDER;
  9430. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  9431. i&=~OPTION_WUG;
  9432. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  9433. i&=~OPTION_MADOGEAR;
  9434. #ifndef NEW_CARTS
  9435. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  9436. i&=~OPTION_CART;
  9437. #else
  9438. if( sd->sc.getSCE(SC_PUSH_CART) && !pc_checkskill(sd, MC_PUSHCART) )
  9439. pc_setcart(sd, 0);
  9440. #endif
  9441. if(i != sd->sc.option)
  9442. pc_setoption(sd, i);
  9443. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  9444. hom_vaporize(sd, HOM_ST_ACTIVE);
  9445. if (sd->sc.getSCE(SC_SPRITEMABLE) && !pc_checkskill(sd, SU_SPRITEMABLE))
  9446. status_change_end(&sd->bl, SC_SPRITEMABLE);
  9447. if (sd->sc.getSCE(SC_SOULATTACK) && !pc_checkskill(sd, SU_SOULATTACK))
  9448. status_change_end(&sd->bl, SC_SOULATTACK);
  9449. if( sd->sc.getSCE(SC_SPIRIT) ){
  9450. status_change_end( &sd->bl, SC_SPIRIT );
  9451. }
  9452. if(sd->status.manner < 0)
  9453. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  9454. status_calc_pc(sd,SCO_FORCE);
  9455. pc_checkallowskill(sd);
  9456. pc_equiplookall(sd);
  9457. pc_show_questinfo(sd);
  9458. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  9459. if( sd->status.party_id ){
  9460. struct party_data* p;
  9461. if( ( p = party_search( sd->status.party_id ) ) != nullptr ){
  9462. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  9463. if( i < MAX_PARTY ){
  9464. p->party.member[i].class_ = sd->status.class_;
  9465. clif_party_job_and_level( *sd );
  9466. }
  9467. }
  9468. }
  9469. chrif_save(sd, CSAVE_NORMAL);
  9470. //if you were previously famous, not anymore.
  9471. if (fame_flag)
  9472. chrif_buildfamelist();
  9473. else if (sd->status.fame > 0) {
  9474. //It may be that now they are famous?
  9475. switch (sd->class_&MAPID_UPPERMASK) {
  9476. case MAPID_BLACKSMITH:
  9477. case MAPID_ALCHEMIST:
  9478. case MAPID_TAEKWON:
  9479. chrif_buildfamelist();
  9480. break;
  9481. }
  9482. }
  9483. return true;
  9484. }
  9485. /*==========================================
  9486. * Tell client player sd has change equipement
  9487. *------------------------------------------*/
  9488. void pc_equiplookall(map_session_data *sd)
  9489. {
  9490. nullpo_retv(sd);
  9491. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  9492. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9493. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  9494. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  9495. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  9496. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  9497. }
  9498. /*==========================================
  9499. * Tell client player sd has change look (hair,equip...)
  9500. *------------------------------------------*/
  9501. void pc_changelook(map_session_data *sd,int type,int val) {
  9502. nullpo_retv(sd);
  9503. switch(type) {
  9504. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  9505. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  9506. if (sd->status.hair != val) {
  9507. sd->status.hair = val;
  9508. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9509. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9510. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  9511. }
  9512. break;
  9513. case LOOK_WEAPON:
  9514. sd->status.weapon = val;
  9515. break;
  9516. case LOOK_HEAD_BOTTOM:
  9517. sd->status.head_bottom = val;
  9518. sd->setlook_head_bottom = val;
  9519. break;
  9520. case LOOK_HEAD_TOP:
  9521. sd->status.head_top = val;
  9522. sd->setlook_head_top = val;
  9523. break;
  9524. case LOOK_HEAD_MID:
  9525. sd->status.head_mid = val;
  9526. sd->setlook_head_mid = val;
  9527. break;
  9528. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  9529. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  9530. if (sd->status.hair_color != val) {
  9531. sd->status.hair_color = val;
  9532. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9533. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9534. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  9535. }
  9536. break;
  9537. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  9538. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  9539. sd->status.clothes_color = val;
  9540. break;
  9541. case LOOK_SHIELD:
  9542. sd->status.shield = val;
  9543. break;
  9544. case LOOK_SHOES:
  9545. break;
  9546. case LOOK_ROBE:
  9547. sd->status.robe = val;
  9548. sd->setlook_robe = val;
  9549. break;
  9550. case LOOK_BODY2:
  9551. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  9552. sd->status.body = val;
  9553. break;
  9554. }
  9555. clif_changelook(&sd->bl, type, val);
  9556. }
  9557. /*==========================================
  9558. * Give an option (type) to player (sd) and display it to client
  9559. *------------------------------------------*/
  9560. void pc_setoption(map_session_data *sd,int type, int subtype)
  9561. {
  9562. int p_type, new_look=0;
  9563. nullpo_retv(sd);
  9564. p_type = sd->sc.option;
  9565. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  9566. sd->sc.option=type;
  9567. clif_changeoption(&sd->bl);
  9568. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9569. { // Mounting
  9570. clif_status_load(&sd->bl,EFST_RIDING,1);
  9571. status_calc_pc(sd,SCO_NONE);
  9572. }
  9573. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9574. { // Dismount
  9575. clif_status_load(&sd->bl,EFST_RIDING,0);
  9576. status_calc_pc(sd,SCO_NONE);
  9577. }
  9578. #ifndef NEW_CARTS
  9579. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  9580. clif_cartlist(sd);
  9581. clif_updatestatus(sd, SP_CARTINFO);
  9582. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9583. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  9584. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  9585. clif_clearcart(sd->fd);
  9586. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9587. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  9588. }
  9589. #endif
  9590. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  9591. clif_status_load(&sd->bl,EFST_FALCON,1);
  9592. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  9593. clif_status_load(&sd->bl,EFST_FALCON,0);
  9594. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  9595. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  9596. status_calc_pc(sd,SCO_NONE);
  9597. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  9598. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  9599. status_calc_pc(sd,SCO_NONE);
  9600. }
  9601. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  9602. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  9603. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  9604. status_change_end(&sd->bl, SC_MADOGEAR);
  9605. }
  9606. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  9607. new_look = JOB_STAR_GLADIATOR2;
  9608. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  9609. new_look = -1;
  9610. if (sd->disguise || !new_look)
  9611. return; //Disguises break sprite changes
  9612. if (new_look < 0) { //Restore normal look.
  9613. status_set_viewdata(&sd->bl, sd->status.class_);
  9614. new_look = sd->vd.class_;
  9615. }
  9616. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  9617. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  9618. if (sd->vd.cloth_color)
  9619. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9620. if( sd->vd.body_style )
  9621. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9622. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  9623. }
  9624. /**
  9625. * Give player a cart
  9626. * @param sd Player
  9627. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  9628. **/
  9629. bool pc_setcart(map_session_data *sd,int type) {
  9630. #ifndef NEW_CARTS
  9631. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  9632. int option;
  9633. #endif
  9634. nullpo_retr(false,sd);
  9635. if( type < 0 || type > MAX_CARTS )
  9636. return false;// Never trust the values sent by the client! [Skotlex]
  9637. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  9638. return false;// Push cart is required
  9639. #ifdef NEW_CARTS
  9640. switch( type ) {
  9641. case 0:
  9642. if( !sd->sc.getSCE(SC_PUSH_CART) )
  9643. return 0;
  9644. status_change_end(&sd->bl,SC_PUSH_CART);
  9645. clif_clearcart(sd->fd);
  9646. break;
  9647. default:/* everything else is an allowed ID so we can move on */
  9648. if( !sd->sc.getSCE(SC_PUSH_CART) ) { /* first time, so fill cart data */
  9649. clif_cartlist(sd);
  9650. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  9651. }
  9652. clif_updatestatus(sd, SP_CARTINFO);
  9653. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  9654. break;
  9655. }
  9656. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9657. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  9658. #else
  9659. // Update option
  9660. option = sd->sc.option;
  9661. option &= ~OPTION_CART;// clear cart bits
  9662. option |= cart[type]; // set cart
  9663. pc_setoption(sd, option);
  9664. #endif
  9665. return true;
  9666. }
  9667. /*==========================================
  9668. * Give player a falcon
  9669. *------------------------------------------*/
  9670. void pc_setfalcon(map_session_data* sd, int flag)
  9671. {
  9672. if( flag ){
  9673. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  9674. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  9675. } else if( pc_isfalcon(sd) ){
  9676. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  9677. }
  9678. }
  9679. /*==========================================
  9680. * Set player riding
  9681. *------------------------------------------*/
  9682. void pc_setriding(map_session_data* sd, int flag)
  9683. {
  9684. if( sd->sc.getSCE(SC_ALL_RIDING) )
  9685. return;
  9686. if( flag ){
  9687. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  9688. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  9689. } else if( pc_isriding(sd) ){
  9690. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  9691. }
  9692. }
  9693. /**
  9694. * Give player a mado
  9695. * @param sd: Player
  9696. * @param flag: Enable or disable mado
  9697. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  9698. */
  9699. void pc_setmadogear(map_session_data *sd, bool flag, e_mado_type type)
  9700. {
  9701. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  9702. return;
  9703. if (flag) {
  9704. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  9705. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  9706. } else if (pc_ismadogear(sd)) {
  9707. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  9708. }
  9709. }
  9710. /*==========================================
  9711. * Check if player can drop an item
  9712. *------------------------------------------*/
  9713. bool pc_candrop(map_session_data *sd, struct item *item)
  9714. {
  9715. if (sd->sc.cant.drop)
  9716. return false;
  9717. if( item && itemdb_ishatched_egg(item) )
  9718. return false;
  9719. if (pc_has_permission(sd, PC_PERM_TRADE_UNCONDITIONAL)) // no restriction
  9720. return true;
  9721. if( !pc_can_give_items(sd) )
  9722. return false;
  9723. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  9724. return false;
  9725. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  9726. }
  9727. /*==========================================
  9728. * Read '@type' variables (temporary numeric char reg)
  9729. *------------------------------------------*/
  9730. int64 pc_readreg(map_session_data* sd, int64 reg)
  9731. {
  9732. return i64db_i64get(sd->regs.vars, reg);
  9733. }
  9734. /*==========================================
  9735. * Set '@type' variables (temporary numeric char reg)
  9736. *------------------------------------------*/
  9737. bool pc_setreg(map_session_data* sd, int64 reg, int64 val)
  9738. {
  9739. uint32 index = script_getvaridx(reg);
  9740. nullpo_retr(false, sd);
  9741. if( val ) {
  9742. i64db_i64put(sd->regs.vars, reg, val);
  9743. if( index )
  9744. script_array_update(&sd->regs, reg, false);
  9745. } else {
  9746. i64db_remove(sd->regs.vars, reg);
  9747. if( index )
  9748. script_array_update(&sd->regs, reg, true);
  9749. }
  9750. return true;
  9751. }
  9752. /*==========================================
  9753. * Read '@type$' variables (temporary string char reg)
  9754. *------------------------------------------*/
  9755. char* pc_readregstr(map_session_data* sd, int64 reg)
  9756. {
  9757. struct script_reg_str *p = nullptr;
  9758. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9759. return p ? p->value : nullptr;
  9760. }
  9761. /*==========================================
  9762. * Set '@type$' variables (temporary string char reg)
  9763. *------------------------------------------*/
  9764. bool pc_setregstr(map_session_data* sd, int64 reg, const char* str)
  9765. {
  9766. struct script_reg_str *p = nullptr;
  9767. unsigned int index = script_getvaridx(reg);
  9768. DBData prev;
  9769. nullpo_retr(false, sd);
  9770. if( str[0] ) {
  9771. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9772. p->value = aStrdup(str);
  9773. p->flag.type = 1;
  9774. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9775. p = (struct script_reg_str *)db_data2ptr(&prev);
  9776. if( p->value )
  9777. aFree(p->value);
  9778. ers_free(str_reg_ers, p);
  9779. } else {
  9780. if( index )
  9781. script_array_update(&sd->regs, reg, false);
  9782. }
  9783. } else {
  9784. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  9785. p = (struct script_reg_str *)db_data2ptr(&prev);
  9786. if( p->value )
  9787. aFree(p->value);
  9788. ers_free(str_reg_ers, p);
  9789. if( index )
  9790. script_array_update(&sd->regs, reg, true);
  9791. }
  9792. }
  9793. return true;
  9794. }
  9795. /**
  9796. * Serves the following variable types:
  9797. * - 'type' (permanent numeric char reg)
  9798. * - '#type' (permanent numeric account reg)
  9799. * - '##type' (permanent numeric account reg2)
  9800. **/
  9801. int64 pc_readregistry(map_session_data *sd, int64 reg)
  9802. {
  9803. struct script_reg_num *p = nullptr;
  9804. if (!sd->vars_ok) {
  9805. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9806. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9807. //intif->request_registry(sd,type==3?4:type);
  9808. set_eof(sd->fd);
  9809. return 0;
  9810. }
  9811. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  9812. return p ? p->value : 0;
  9813. }
  9814. /**
  9815. * Serves the following variable types:
  9816. * - 'type$' (permanent str char reg)
  9817. * - '#type$' (permanent str account reg)
  9818. * - '##type$' (permanent str account reg2)
  9819. **/
  9820. char* pc_readregistry_str(map_session_data *sd, int64 reg)
  9821. {
  9822. struct script_reg_str *p = nullptr;
  9823. if (!sd->vars_ok) {
  9824. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9825. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9826. //intif->request_registry(sd,type==3?4:type);
  9827. set_eof(sd->fd);
  9828. return nullptr;
  9829. }
  9830. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9831. return p ? p->value : nullptr;
  9832. }
  9833. /**
  9834. * Serves the following variable types:
  9835. * - 'type' (permanent numeric char reg)
  9836. * - '#type' (permanent numeric account reg)
  9837. * - '##type' (permanent numeric account reg2)
  9838. **/
  9839. bool pc_setregistry(map_session_data *sd, int64 reg, int64 val)
  9840. {
  9841. struct script_reg_num *p = nullptr;
  9842. const char *regname = get_str(script_getvarid(reg));
  9843. uint32 index = script_getvaridx(reg);
  9844. if ( !reg_load && !sd->vars_ok ) {
  9845. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  9846. return false;
  9847. }
  9848. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  9849. if( val ) {
  9850. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  9851. script_array_update(&sd->regs, reg, false);
  9852. p->value = val;
  9853. } else {
  9854. p->value = 0;
  9855. if( index )
  9856. script_array_update(&sd->regs, reg, true);
  9857. }
  9858. if (!reg_load)
  9859. p->flag.update = 1;/* either way, it will require either delete or replace */
  9860. } else if( val ) {
  9861. DBData prev;
  9862. if( index )
  9863. script_array_update(&sd->regs, reg, false);
  9864. p = ers_alloc(num_reg_ers, struct script_reg_num);
  9865. p->value = val;
  9866. if (!reg_load)
  9867. p->flag.update = 1;
  9868. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9869. p = (struct script_reg_num *)db_data2ptr(&prev);
  9870. ers_free(num_reg_ers, p);
  9871. }
  9872. }
  9873. if (!reg_load && p)
  9874. sd->vars_dirty = true;
  9875. return true;
  9876. }
  9877. /**
  9878. * Serves the following variable types:
  9879. * - 'type$' (permanent str char reg)
  9880. * - '#type$' (permanent str account reg)
  9881. * - '##type$' (permanent str account reg2)
  9882. **/
  9883. bool pc_setregistry_str(map_session_data *sd, int64 reg, const char *val)
  9884. {
  9885. struct script_reg_str *p = nullptr;
  9886. const char *regname = get_str(script_getvarid(reg));
  9887. unsigned int index = script_getvaridx(reg);
  9888. size_t vlen = 0;
  9889. if (!reg_load && !sd->vars_ok) {
  9890. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  9891. return false;
  9892. }
  9893. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  9894. {
  9895. 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);
  9896. return false;
  9897. }
  9898. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  9899. if( val[0] ) {
  9900. if( p->value )
  9901. aFree(p->value);
  9902. else if ( index ) // an entry that was deleted, so we reset
  9903. script_array_update(&sd->regs, reg, false);
  9904. p->value = aStrdup(val);
  9905. } else {
  9906. if (p->value)
  9907. aFree(p->value);
  9908. p->value = nullptr;
  9909. if( index )
  9910. script_array_update(&sd->regs, reg, true);
  9911. }
  9912. if( !reg_load )
  9913. p->flag.update = 1; // either way, it will require either delete or replace
  9914. } else if( val[0] ) {
  9915. DBData prev;
  9916. if( index )
  9917. script_array_update(&sd->regs, reg, false);
  9918. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9919. p->value = aStrdup(val);
  9920. if( !reg_load )
  9921. p->flag.update = 1;
  9922. p->flag.type = 1;
  9923. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  9924. p = (struct script_reg_str *)db_data2ptr(&prev);
  9925. if( p->value )
  9926. aFree(p->value);
  9927. ers_free(str_reg_ers, p);
  9928. }
  9929. }
  9930. if( !reg_load && p )
  9931. sd->vars_dirty = true;
  9932. return true;
  9933. }
  9934. /**
  9935. * Set value of player variable
  9936. * @param sd Player
  9937. * @param reg Variable name
  9938. * @param value
  9939. * @return True if success, false if failed.
  9940. **/
  9941. bool pc_setreg2(map_session_data *sd, const char *reg, int64 val) {
  9942. char prefix = reg[0];
  9943. nullpo_retr(false, sd);
  9944. if (reg[strlen(reg)-1] == '$') {
  9945. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9946. return false;
  9947. }
  9948. val = cap_value(val, INT_MIN, INT_MAX);
  9949. switch (prefix) {
  9950. case '.':
  9951. case '\'':
  9952. case '$':
  9953. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9954. return false;
  9955. case '@':
  9956. return pc_setreg(sd, add_str(reg), val);
  9957. case '#':
  9958. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  9959. default:
  9960. return pc_setglobalreg(sd, add_str(reg), val);
  9961. }
  9962. return false;
  9963. }
  9964. /**
  9965. * Get value of player variable
  9966. * @param sd Player
  9967. * @param reg Variable name
  9968. * @return Variable value or 0 if failed.
  9969. **/
  9970. int64 pc_readreg2(map_session_data *sd, const char *reg) {
  9971. char prefix = reg[0];
  9972. nullpo_ret(sd);
  9973. if (reg[strlen(reg)-1] == '$') {
  9974. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9975. return 0;
  9976. }
  9977. switch (prefix) {
  9978. case '.':
  9979. case '\'':
  9980. case '$':
  9981. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9982. return 0;
  9983. case '@':
  9984. return pc_readreg(sd, add_str(reg));
  9985. case '#':
  9986. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  9987. default:
  9988. return pc_readglobalreg(sd, add_str(reg));
  9989. }
  9990. return 0;
  9991. }
  9992. /*==========================================
  9993. * Exec eventtimer for player sd (retrieved from map_session (id))
  9994. *------------------------------------------*/
  9995. static TIMER_FUNC(pc_eventtimer){
  9996. map_session_data *sd=map_id2sd(id);
  9997. char *p = (char *)data;
  9998. int i;
  9999. if(sd==nullptr)
  10000. return 0;
  10001. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  10002. if( i < MAX_EVENTTIMER )
  10003. {
  10004. sd->eventtimer[i] = INVALID_TIMER;
  10005. sd->eventcount--;
  10006. npc_event(sd,p,0);
  10007. }
  10008. else
  10009. ShowError("pc_eventtimer: no such event timer\n");
  10010. if (p) aFree(p);
  10011. return 0;
  10012. }
  10013. /*==========================================
  10014. * Add eventtimer for player sd ?
  10015. *------------------------------------------*/
  10016. bool pc_addeventtimer(map_session_data *sd,int tick,const char *name)
  10017. {
  10018. int i;
  10019. nullpo_retr(false,sd);
  10020. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  10021. if( i == MAX_EVENTTIMER )
  10022. return false;
  10023. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  10024. sd->eventcount++;
  10025. return true;
  10026. }
  10027. /*==========================================
  10028. * Del eventtimer for player sd ?
  10029. *------------------------------------------*/
  10030. bool pc_deleventtimer(map_session_data *sd,const char *name)
  10031. {
  10032. char* p = nullptr;
  10033. int i;
  10034. nullpo_retr(false,sd);
  10035. if (sd->eventcount == 0)
  10036. return false;
  10037. // find the named event timer
  10038. ARR_FIND( 0, MAX_EVENTTIMER, i,
  10039. sd->eventtimer[i] != INVALID_TIMER &&
  10040. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != nullptr &&
  10041. strcmp(p, name) == 0
  10042. );
  10043. if( i == MAX_EVENTTIMER )
  10044. return false; // not found
  10045. delete_timer(sd->eventtimer[i],pc_eventtimer);
  10046. sd->eventtimer[i] = INVALID_TIMER;
  10047. if(sd->eventcount > 0)
  10048. sd->eventcount--;
  10049. aFree(p);
  10050. return true;
  10051. }
  10052. /*==========================================
  10053. * Update eventtimer count for player sd
  10054. *------------------------------------------*/
  10055. void pc_addeventtimercount(map_session_data *sd,const char *name,int tick)
  10056. {
  10057. int i;
  10058. nullpo_retv(sd);
  10059. for(i=0;i<MAX_EVENTTIMER;i++)
  10060. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  10061. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  10062. addtick_timer(sd->eventtimer[i],tick);
  10063. break;
  10064. }
  10065. }
  10066. /*==========================================
  10067. * Remove all eventtimer for player sd
  10068. *------------------------------------------*/
  10069. void pc_cleareventtimer(map_session_data *sd)
  10070. {
  10071. int i;
  10072. nullpo_retv(sd);
  10073. if (sd->eventcount == 0)
  10074. return;
  10075. for(i=0;i<MAX_EVENTTIMER;i++){
  10076. if( sd->eventtimer[i] != INVALID_TIMER ){
  10077. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  10078. delete_timer(sd->eventtimer[i],pc_eventtimer);
  10079. sd->eventtimer[i] = INVALID_TIMER;
  10080. if(sd->eventcount > 0) //avoid looping to max val
  10081. sd->eventcount--;
  10082. if (p) aFree(p);
  10083. }
  10084. }
  10085. }
  10086. /**
  10087. * Called when an item with combo is worn
  10088. * @param sd: Player data
  10089. * @param data: Item data
  10090. * @return Number of succeeded combo(s)
  10091. */
  10092. static int pc_checkcombo(map_session_data *sd, item_data *data) {
  10093. int success = 0;
  10094. for (const auto &item_combo : data->combos) {
  10095. bool do_continue = false;
  10096. // Ensure this isn't a duplicate combo
  10097. for (const auto player_combo : sd->combos) {
  10098. if (player_combo->id == item_combo->id) {
  10099. do_continue = true;
  10100. break;
  10101. }
  10102. }
  10103. // Combo already equipped
  10104. if (do_continue)
  10105. continue;
  10106. size_t nb_itemCombo = item_combo->nameid.size();
  10107. if (nb_itemCombo < 2) // A combo with less then 2 item?
  10108. continue;
  10109. struct s_itemchk {
  10110. int idx;
  10111. t_itemid nameid, card[MAX_SLOTS];
  10112. s_itemchk() : idx(0), nameid(0), card() {};
  10113. };
  10114. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  10115. size_t j;
  10116. unsigned int pos = 0;
  10117. for (j = 0; j < nb_itemCombo; j++) {
  10118. t_itemid id = item_combo->nameid[j];
  10119. bool found = false;
  10120. for (int16 k = 0; k < EQI_MAX; k++) {
  10121. short index = sd->equip_index[k];
  10122. if (index < 0)
  10123. continue;
  10124. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  10125. continue;
  10126. if (!sd->inventory_data[index])
  10127. continue;
  10128. if (itemdb_type(id) != IT_CARD) {
  10129. if (sd->inventory_data[index]->nameid != id)
  10130. continue;
  10131. if (j > 0) { // Check if this item not already used
  10132. do_continue = false;
  10133. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  10134. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  10135. do_continue = true;
  10136. break;
  10137. }
  10138. }
  10139. if (do_continue)
  10140. continue;
  10141. }
  10142. combo_idx[j].nameid = id;
  10143. combo_idx[j].idx = index;
  10144. pos |= sd->inventory.u.items_inventory[index].equip;
  10145. found = true;
  10146. break;
  10147. } else { // Cards and enchants
  10148. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  10149. continue;
  10150. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  10151. do_continue = false;
  10152. if (sd->inventory.u.items_inventory[index].card[z] != id)
  10153. continue;
  10154. if (j > 0) {
  10155. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  10156. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  10157. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  10158. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  10159. do_continue = true;
  10160. break;
  10161. }
  10162. }
  10163. }
  10164. }
  10165. }
  10166. if (do_continue)
  10167. continue;
  10168. combo_idx[j].nameid = id;
  10169. combo_idx[j].idx = index;
  10170. combo_idx[j].card[z] = id;
  10171. pos |= sd->inventory.u.items_inventory[index].equip;
  10172. found = true;
  10173. break;
  10174. }
  10175. }
  10176. }
  10177. if (!found)
  10178. break; // Unable to found all the IDs for this combo, return
  10179. }
  10180. // Broke out of the count loop without finding all IDs, move to the next combo
  10181. if (j < nb_itemCombo)
  10182. continue;
  10183. // All items in the combo are matching
  10184. auto entry = std::make_shared<s_combos>();
  10185. entry->bonus = item_combo->script;
  10186. entry->id = item_combo->id;
  10187. entry->pos = pos;
  10188. sd->combos.push_back(entry);
  10189. combo_idx.clear();
  10190. success++;
  10191. }
  10192. return success;
  10193. }
  10194. /**
  10195. * Called when an item with combo is removed
  10196. * @param sd: Player data
  10197. * @param data: Item data
  10198. * @return Number of removed combo(s)
  10199. */
  10200. static int pc_removecombo(map_session_data *sd, item_data *data ) {
  10201. if (sd->combos.empty())
  10202. return 0; // Nothing to do here, player has no combos
  10203. int retval = 0;
  10204. for (const auto &item_combo : data->combos) {
  10205. std::shared_ptr<s_combos> del_combo = nullptr;
  10206. // Check if this combo exists on this player
  10207. for (const auto &player_combo : sd->combos) {
  10208. if (player_combo->id == item_combo->id) {
  10209. del_combo = player_combo;
  10210. break;
  10211. }
  10212. }
  10213. // No match, skip this combo
  10214. if (del_combo == nullptr)
  10215. continue;
  10216. util::vector_erase_if_exists(sd->combos, del_combo);
  10217. retval++;
  10218. // Check if combo requirements still fit
  10219. if (pc_checkcombo(sd, data))
  10220. continue;
  10221. // It's empty, clear all the memory
  10222. if (sd->combos.empty()) {
  10223. sd->combos.clear();
  10224. return retval; // Return at this point as there are no more combos to check
  10225. }
  10226. }
  10227. return retval;
  10228. }
  10229. /**
  10230. * Load combo data(s) of player
  10231. * @param sd: Player data
  10232. * @return ret numbers of succeed combo
  10233. */
  10234. int pc_load_combo(map_session_data *sd) {
  10235. int ret = 0;
  10236. for (int16 i = 0; i < EQI_MAX; i++) {
  10237. item_data *id;
  10238. short idx = sd->equip_index[i];
  10239. if (idx < 0 || !(id = sd->inventory_data[idx]))
  10240. continue;
  10241. if (!id->combos.empty())
  10242. ret += pc_checkcombo(sd, id);
  10243. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  10244. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  10245. if (!sd->inventory.u.items_inventory[idx].card[j])
  10246. continue;
  10247. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[idx].card[j]);
  10248. if (data != nullptr) {
  10249. if (!data->combos.empty())
  10250. ret += pc_checkcombo(sd, data.get());
  10251. }
  10252. }
  10253. }
  10254. }
  10255. return ret;
  10256. }
  10257. /*==========================================
  10258. * Equip item on player sd at req_pos from inventory index n
  10259. * return: false - fail; true - success
  10260. *------------------------------------------*/
  10261. bool pc_equipitem(map_session_data *sd,short n,int req_pos,bool equipswitch)
  10262. {
  10263. int i, pos, flag = 0, iflag;
  10264. struct item_data *id;
  10265. short* equip_index;
  10266. nullpo_retr(false,sd);
  10267. if( n < 0 || n >= MAX_INVENTORY ) {
  10268. if( equipswitch ){
  10269. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10270. }else{
  10271. // Does not deserve an answer... [Lemongrass]
  10272. //clif_equipitemack( sd, ITEM_EQUIP_ACK_FAIL, n );
  10273. }
  10274. return false;
  10275. }
  10276. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  10277. if( equipswitch ){
  10278. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10279. }else{
  10280. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n );
  10281. }
  10282. return false;
  10283. }
  10284. if (!(id = sd->inventory_data[n]))
  10285. return false;
  10286. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  10287. if(battle_config.battle_log && !equipswitch)
  10288. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  10289. uint8 res = pc_isequip( sd, n );
  10290. if( res != ITEM_EQUIP_ACK_OK ){
  10291. if( equipswitch ){
  10292. clif_equipswitch_add( sd, n, req_pos, res );
  10293. }else{
  10294. clif_equipitemack( *sd, res, n ); // fail
  10295. }
  10296. return false;
  10297. }
  10298. if( equipswitch && id->type == IT_AMMO ){
  10299. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10300. return false;
  10301. }
  10302. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  10303. if( equipswitch ){
  10304. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10305. }else{
  10306. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); // fail
  10307. }
  10308. return false;
  10309. }
  10310. if( sd->sc.count && (sd->sc.cant.equip || (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  10311. if( equipswitch ){
  10312. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10313. }else{
  10314. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); //Fail
  10315. }
  10316. return false;
  10317. }
  10318. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  10319. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  10320. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  10321. clif_notify_bindOnEquip( *sd, n );
  10322. }
  10323. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  10324. pos = req_pos&EQP_ACC;
  10325. if (pos == EQP_ACC) //User specified both slots.
  10326. pos = (equip_index[EQI_ACC_R] >= 0 && equip_index[EQI_ACC_L] < 0) ? EQP_ACC_L : EQP_ACC_R;
  10327. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  10328. if (!sd->inventory.u.items_inventory[n].card[i])
  10329. continue;
  10330. std::shared_ptr<item_data> card_data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10331. if (card_data) {
  10332. int card_pos = card_data->equip;
  10333. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  10334. pos = card_pos; // Use the card's equip position
  10335. break;
  10336. }
  10337. }
  10338. }
  10339. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  10340. pos = (req_pos&EQP_ARMS);
  10341. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  10342. #ifdef RENEWAL
  10343. pos = (equip_index[EQI_HAND_R] >= 0 && equip_index[EQI_HAND_L] < 0) ? EQP_HAND_L : EQP_HAND_R;
  10344. #else
  10345. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  10346. #endif
  10347. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  10348. pos = req_pos&EQP_SHADOW_ACC;
  10349. if (pos == EQP_SHADOW_ACC)
  10350. pos = (equip_index[EQI_SHADOW_ACC_L] >= 0 && equip_index[EQI_SHADOW_ACC_R] < 0) ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  10351. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  10352. pos = (req_pos&EQP_SHADOW_ARMS);
  10353. if( pos == EQP_SHADOW_ARMS )
  10354. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  10355. }
  10356. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  10357. //Update skill-block range database when weapon range changes. [Skotlex]
  10358. i = equip_index[EQI_HAND_R];
  10359. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  10360. flag = 1;
  10361. else
  10362. flag = id->range != sd->inventory_data[i]->range;
  10363. }
  10364. if( equipswitch ){
  10365. for( i = 0; i < EQI_MAX; i++ ){
  10366. if( pos&equip_bitmask[i] ){
  10367. // If there was already an item assigned to this slot
  10368. if( sd->equip_switch_index[i] >= 0 ){
  10369. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  10370. }
  10371. // Assign the new index to it
  10372. sd->equip_switch_index[i] = n;
  10373. }
  10374. }
  10375. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  10376. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  10377. return true;
  10378. }else{
  10379. for(i=0;i<EQI_MAX;i++) {
  10380. if(pos & equip_bitmask[i]) {
  10381. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  10382. pc_unequipitem(sd,sd->equip_index[i], 1 | 2 | 4);
  10383. sd->equip_index[i] = n;
  10384. }
  10385. }
  10386. pc_equipswitch_remove(sd, n);
  10387. if(pos==EQP_AMMO) {
  10388. clif_arrowequip(sd,n);
  10389. clif_arrow_fail(sd,3);
  10390. }
  10391. else
  10392. clif_equipitemack( *sd, ITEM_EQUIP_ACK_OK, n, pos );
  10393. sd->inventory.u.items_inventory[n].equip = pos;
  10394. }
  10395. if(pos & EQP_HAND_R) {
  10396. if(id)
  10397. sd->weapontype1 = id->subtype;
  10398. else
  10399. sd->weapontype1 = W_FIST;
  10400. pc_calcweapontype(sd);
  10401. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10402. }
  10403. if(pos & EQP_HAND_L) {
  10404. if(id) {
  10405. if(id->type == IT_WEAPON) {
  10406. sd->status.shield = W_FIST;
  10407. sd->weapontype2 = id->subtype;
  10408. }
  10409. else
  10410. if(id->type == IT_ARMOR) {
  10411. sd->status.shield = id->look;
  10412. sd->weapontype2 = W_FIST;
  10413. }
  10414. }
  10415. else
  10416. sd->status.shield = sd->weapontype2 = W_FIST;
  10417. pc_calcweapontype(sd);
  10418. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10419. }
  10420. if(pos & EQP_SHOES)
  10421. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10422. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  10423. short idx = sd->equip_index[EQI_AMMO];
  10424. if (idx >= 0) {
  10425. switch (sd->inventory_data[idx]->subtype) {
  10426. case AMMO_ARROW:
  10427. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  10428. pc_unequipitem(sd, idx, 2 | 4);
  10429. break;
  10430. case AMMO_BULLET:
  10431. case AMMO_SHELL:
  10432. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  10433. #ifdef RENEWAL
  10434. && id->subtype != W_GRENADE
  10435. #endif
  10436. )
  10437. pc_unequipitem(sd, idx, 2 | 4);
  10438. break;
  10439. #ifndef RENEWAL
  10440. case AMMO_GRENADE:
  10441. if (id->subtype != W_GRENADE)
  10442. pc_unequipitem(sd, idx, 2 | 4);
  10443. break;
  10444. #endif
  10445. }
  10446. }
  10447. }
  10448. pc_set_costume_view(sd);
  10449. pc_checkallowskill(sd); //Check if status changes should be halted.
  10450. iflag = sd->npc_item_flag;
  10451. // Check for combos (MUST be before status_calc_pc)
  10452. if (!id->combos.empty())
  10453. pc_checkcombo(sd, id);
  10454. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10455. ; // No cards
  10456. else {
  10457. for (i = 0; i < MAX_SLOTS; i++) {
  10458. if (!sd->inventory.u.items_inventory[n].card[i])
  10459. continue;
  10460. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10461. if (data != nullptr) {
  10462. if (!data->combos.empty())
  10463. pc_checkcombo(sd, data.get());
  10464. }
  10465. }
  10466. }
  10467. status_calc_pc(sd,SCO_NONE);
  10468. if (flag) //Update skill data
  10469. clif_skillinfoblock(sd);
  10470. //OnEquip script [Skotlex]
  10471. if (id) {
  10472. current_equip_item_index = n;
  10473. current_equip_card_id = 0;
  10474. //only run the script if item isn't restricted
  10475. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(*sd, id->nameid)))
  10476. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10477. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10478. ; //No cards
  10479. else {
  10480. for( i = 0; i < MAX_SLOTS; i++ ) {
  10481. if (!sd->inventory.u.items_inventory[n].card[i])
  10482. continue;
  10483. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10484. if ( data != nullptr ) {
  10485. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(*sd, data->nameid))) {
  10486. current_equip_card_id = sd->inventory.u.items_inventory[n].card[i];
  10487. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10488. }
  10489. }
  10490. current_equip_card_id = 0;
  10491. }
  10492. }
  10493. current_equip_item_index = -1;
  10494. }
  10495. sd->npc_item_flag = iflag;
  10496. return true;
  10497. }
  10498. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int position ){
  10499. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  10500. while( it != bonus.end() ){
  10501. std::shared_ptr<s_autobonus> b = *it;
  10502. if( ( b->pos & position ) != b->pos ){
  10503. it++;
  10504. continue;
  10505. }
  10506. it = bonus.erase( it );
  10507. }
  10508. }
  10509. /**
  10510. * Recalculate player status on unequip
  10511. * @param sd: Player data
  10512. * @param n: Item inventory index
  10513. * @param flag: Whether to recalculate a player's status or not
  10514. * @return True on success or false on failure
  10515. */
  10516. static void pc_unequipitem_sub(map_session_data *sd, int n, int flag) {
  10517. int i, iflag;
  10518. bool status_calc = false;
  10519. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  10520. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  10521. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  10522. sd->inventory.u.items_inventory[n].equip = 0;
  10523. if (!(flag & 4))
  10524. pc_checkallowskill(sd);
  10525. iflag = sd->npc_item_flag;
  10526. // Check for combos (MUST be before status_calc_pc)
  10527. if (sd->inventory_data[n]) {
  10528. if (!sd->inventory_data[n]->combos.empty()) {
  10529. if (pc_removecombo(sd, sd->inventory_data[n]))
  10530. status_calc = true;
  10531. }
  10532. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10533. ; // No cards
  10534. else {
  10535. for (i = 0; i < MAX_SLOTS; i++) {
  10536. if (!sd->inventory.u.items_inventory[n].card[i])
  10537. continue;
  10538. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10539. if (data != nullptr) {
  10540. if (!data->combos.empty()) {
  10541. if (pc_removecombo(sd, data.get()))
  10542. status_calc = true;
  10543. }
  10544. }
  10545. }
  10546. }
  10547. }
  10548. if (flag & 1 || status_calc) {
  10549. pc_checkallowskill(sd);
  10550. status_calc_pc(sd, SCO_FORCE);
  10551. }
  10552. if (sd->sc.getSCE(SC_SIGNUMCRUCIS) && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  10553. status_change_end(&sd->bl, SC_SIGNUMCRUCIS);
  10554. //OnUnEquip script [Skotlex]
  10555. if (sd->inventory_data[n]) {
  10556. current_equip_item_index = n;
  10557. current_equip_card_id = 0;
  10558. if (sd->inventory_data[n]->unequip_script)
  10559. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10560. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10561. ; //No cards
  10562. else {
  10563. for (i = 0; i < MAX_SLOTS; i++) {
  10564. if (!sd->inventory.u.items_inventory[n].card[i])
  10565. continue;
  10566. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10567. if (data != nullptr) {
  10568. if (data->unequip_script) {
  10569. current_equip_card_id = sd->inventory.u.items_inventory[n].card[i];
  10570. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10571. }
  10572. }
  10573. current_equip_card_id = 0;
  10574. }
  10575. }
  10576. current_equip_item_index = -1;
  10577. }
  10578. sd->npc_item_flag = iflag;
  10579. }
  10580. /**
  10581. * Called when attempting to unequip an item from a player
  10582. * @param sd: Player data
  10583. * @param n: Item inventory index
  10584. * @param flag: Type of unequip
  10585. * 0 - only unequip
  10586. * 1 - calculate status after unequipping
  10587. * 2 - force unequip
  10588. * 4 - unequip by switching equipment
  10589. * @return True on success or false on failure
  10590. */
  10591. bool pc_unequipitem(map_session_data *sd, int n, int flag) {
  10592. int i, pos;
  10593. nullpo_retr(false,sd);
  10594. if (n < 0 || n >= MAX_INVENTORY) {
  10595. clif_unequipitemack(sd,0,0,0);
  10596. return false;
  10597. }
  10598. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  10599. clif_unequipitemack(sd,n,0,0);
  10600. return false; //Nothing to unequip
  10601. }
  10602. // status change that makes player cannot unequip equipment
  10603. if (!(flag&2) && sd->sc.count &&( sd->sc.cant.unequip ||
  10604. (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  10605. {
  10606. clif_unequipitemack(sd,n,0,0);
  10607. return false;
  10608. }
  10609. if (battle_config.battle_log)
  10610. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  10611. for(i = 0; i < EQI_MAX; i++) {
  10612. if (pos & equip_bitmask[i])
  10613. sd->equip_index[i] = -1;
  10614. }
  10615. if(pos & EQP_HAND_R) {
  10616. sd->weapontype1 = W_FIST;
  10617. sd->status.weapon = sd->weapontype2;
  10618. pc_calcweapontype(sd);
  10619. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10620. if( !battle_config.dancing_weaponswitch_fix )
  10621. status_change_end(&sd->bl, SC_DANCING); // Unequipping => stop dancing.
  10622. #ifdef RENEWAL
  10623. if (battle_config.switch_remove_edp&2) {
  10624. #else
  10625. if (battle_config.switch_remove_edp&1) {
  10626. #endif
  10627. status_change_end(&sd->bl, SC_EDP);
  10628. }
  10629. }
  10630. if(pos & EQP_HAND_L) {
  10631. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  10632. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  10633. sd->status.shield = sd->weapontype2 = W_FIST;
  10634. pc_calcweapontype(sd);
  10635. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10636. }
  10637. if(pos & EQP_SHOES)
  10638. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10639. clif_unequipitemack(sd,n,pos,1);
  10640. pc_set_costume_view(sd);
  10641. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIP });
  10642. // On weapon change (right and left hand)
  10643. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  10644. if (battle_config.ammo_unequip && !(flag & 4)) {
  10645. switch (sd->inventory_data[n]->subtype) {
  10646. case W_BOW:
  10647. case W_MUSICAL:
  10648. case W_WHIP:
  10649. case W_REVOLVER:
  10650. case W_RIFLE:
  10651. case W_GATLING:
  10652. case W_SHOTGUN:
  10653. case W_GRENADE: {
  10654. short idx = sd->equip_index[EQI_AMMO];
  10655. if (idx >= 0) {
  10656. sd->equip_index[EQI_AMMO] = -1;
  10657. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  10658. pc_unequipitem_sub(sd, idx, 0);
  10659. }
  10660. }
  10661. break;
  10662. }
  10663. }
  10664. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPWEAPON });
  10665. }
  10666. // On armor change
  10667. if (pos & EQP_ARMOR) {
  10668. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPARMOR });
  10669. }
  10670. // On equipment change
  10671. #ifndef RENEWAL
  10672. if (!(flag&4))
  10673. status_change_end(&sd->bl, SC_CONCENTRATION);
  10674. #endif
  10675. // On ammo change
  10676. 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_))
  10677. status_change_end(&sd->bl, SC_P_ALTER);
  10678. pc_unequipitem_sub(sd, n, flag);
  10679. return true;
  10680. }
  10681. int pc_equipswitch( map_session_data* sd, int index ){
  10682. // Get the target equip mask
  10683. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  10684. // Get the currently equipped item
  10685. short equippedItem = pc_checkequip( sd, position, true );
  10686. // No item equipped at the target
  10687. if( equippedItem == -1 ){
  10688. // Remove it from the equip switch
  10689. pc_equipswitch_remove( sd, index );
  10690. pc_equipitem( sd, index, position );
  10691. return position;
  10692. }else{
  10693. std::map<int, int> unequipped;
  10694. int unequipped_position = 0;
  10695. // Unequip all items that interfere
  10696. for( int i = 0; i < EQI_MAX; i++ ){
  10697. int unequip_index = sd->equip_index[i];
  10698. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  10699. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  10700. // Store the unequipped index and position mask for later
  10701. unequipped[unequip_index] = unequip_item->equip;
  10702. // Keep the position for later
  10703. unequipped_position |= unequip_item->equip;
  10704. // Unequip the item
  10705. pc_unequipitem( sd, unequip_index, 0 );
  10706. }
  10707. }
  10708. int all_position = position | unequipped_position;
  10709. // Equip everything that is hit by the mask
  10710. for( int i = 0; i < EQI_MAX; i++ ){
  10711. int exchange_index = sd->equip_switch_index[i];
  10712. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  10713. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  10714. // Store the target position
  10715. int exchange_position = exchange_item->equipSwitch;
  10716. // Remove the item from equip switch
  10717. pc_equipswitch_remove( sd, exchange_index );
  10718. // Equip the item at the destinated position
  10719. pc_equipitem( sd, exchange_index, exchange_position );
  10720. }
  10721. }
  10722. // Place all unequipped items into the equip switch window
  10723. for( std::pair<int, int> pair : unequipped ){
  10724. int unequipped_index = pair.first;
  10725. int unequipped_position = pair.second;
  10726. // Rebuild the index cache
  10727. for( int i = 0; i < EQI_MAX; i++ ){
  10728. if( unequipped_position & equip_bitmask[i] ){
  10729. sd->equip_switch_index[i] = unequipped_index;
  10730. }
  10731. }
  10732. // Set the correct position mask
  10733. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  10734. // Notify the client
  10735. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  10736. }
  10737. return all_position;
  10738. }
  10739. }
  10740. void pc_equipswitch_remove( map_session_data* sd, int index ){
  10741. struct item* item = &sd->inventory.u.items_inventory[index];
  10742. if( !item->equipSwitch ){
  10743. return;
  10744. }
  10745. for( int i = 0; i < EQI_MAX; i++ ){
  10746. // If a match is found
  10747. if( sd->equip_switch_index[i] == index ){
  10748. // Remove it from the slot
  10749. sd->equip_switch_index[i] = -1;
  10750. }
  10751. }
  10752. // Send out one packet for all slots using the current item's mask
  10753. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  10754. item->equipSwitch = 0;
  10755. }
  10756. /*==========================================
  10757. * Checking if player (sd) has an invalid item
  10758. * and is unequiped on map load (item_noequip)
  10759. *------------------------------------------*/
  10760. void pc_checkitem(map_session_data *sd) {
  10761. int i, calc_flag = 0;
  10762. struct item* it;
  10763. nullpo_retv(sd);
  10764. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  10765. return;
  10766. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  10767. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10768. it = &sd->inventory.u.items_inventory[i];
  10769. if( it->nameid == 0 )
  10770. continue;
  10771. if( !it->equip )
  10772. continue;
  10773. if( it->equip&~pc_equippoint(sd,i) ) {
  10774. pc_unequipitem(sd, i, 2);
  10775. calc_flag = 1;
  10776. continue;
  10777. }
  10778. if( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(*sd, sd->inventory_data[i]->nameid) ) {
  10779. pc_unequipitem(sd, i, 2);
  10780. calc_flag = 1;
  10781. continue;
  10782. }
  10783. }
  10784. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10785. it = &sd->inventory.u.items_inventory[i];
  10786. if( it->nameid == 0 )
  10787. continue;
  10788. if( !it->equipSwitch )
  10789. continue;
  10790. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  10791. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(*sd, sd->inventory_data[i]->nameid) ) ){
  10792. for( int j = 0; j < EQI_MAX; j++ ){
  10793. if( sd->equip_switch_index[j] == i ){
  10794. sd->equip_switch_index[j] = -1;
  10795. }
  10796. }
  10797. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  10798. continue;
  10799. }
  10800. }
  10801. if( calc_flag && sd->state.active ) {
  10802. pc_checkallowskill(sd);
  10803. status_calc_pc(sd,SCO_NONE);
  10804. }
  10805. }
  10806. /*==========================================
  10807. * Checks for unavailable items and removes them.
  10808. * @param sd: Player data
  10809. * @param type Forced check:
  10810. * 1 - Inventory
  10811. * 2 - Cart
  10812. * 4 - Storage
  10813. *------------------------------------------*/
  10814. void pc_check_available_item(map_session_data *sd, uint8 type)
  10815. {
  10816. int i;
  10817. t_itemid nameid;
  10818. char output[256];
  10819. nullpo_retv(sd);
  10820. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  10821. for(i = 0; i < MAX_INVENTORY; i++) {
  10822. nameid = sd->inventory.u.items_inventory[i].nameid;
  10823. if (!nameid)
  10824. continue;
  10825. if (!itemdb_available(nameid)) {
  10826. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  10827. clif_displaymessage(sd->fd, output);
  10828. 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);
  10829. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  10830. continue;
  10831. }
  10832. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  10833. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  10834. }
  10835. }
  10836. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  10837. for(i = 0; i < MAX_CART; i++) {
  10838. nameid = sd->cart.u.items_cart[i].nameid;
  10839. if (!nameid)
  10840. continue;
  10841. if (!itemdb_available(nameid)) {
  10842. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  10843. clif_displaymessage(sd->fd, output);
  10844. 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);
  10845. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  10846. continue;
  10847. }
  10848. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  10849. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  10850. }
  10851. }
  10852. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  10853. for(i = 0; i < sd->storage.max_amount; i++) {
  10854. nameid = sd->storage.u.items_storage[i].nameid;
  10855. if (!nameid)
  10856. continue;
  10857. if (!itemdb_available(nameid)) {
  10858. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  10859. clif_displaymessage(sd->fd, output);
  10860. 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);
  10861. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  10862. continue;
  10863. }
  10864. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  10865. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  10866. }
  10867. }
  10868. }
  10869. /*==========================================
  10870. * Update PVP rank for sd1 in cmp to sd2
  10871. *------------------------------------------*/
  10872. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  10873. {
  10874. map_session_data *sd1,*sd2;
  10875. sd1=(map_session_data *)bl;
  10876. sd2=va_arg(ap,map_session_data *);
  10877. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  10878. {// cannot register pvp rank for hidden GMs
  10879. return 0;
  10880. }
  10881. if( sd1->pvp_point > sd2->pvp_point )
  10882. sd2->pvp_rank++;
  10883. return 0;
  10884. }
  10885. /*==========================================
  10886. * Calculate new rank beetween all present players (map_foreachinallarea)
  10887. * and display result
  10888. *------------------------------------------*/
  10889. int pc_calc_pvprank(map_session_data *sd)
  10890. {
  10891. int old = sd->pvp_rank;
  10892. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10893. sd->pvp_rank=1;
  10894. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  10895. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  10896. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  10897. return sd->pvp_rank;
  10898. }
  10899. /*==========================================
  10900. * Calculate next sd ranking calculation from config
  10901. *------------------------------------------*/
  10902. TIMER_FUNC(pc_calc_pvprank_timer){
  10903. map_session_data *sd;
  10904. sd=map_id2sd(id);
  10905. if(sd==nullptr)
  10906. return 0;
  10907. sd->pvp_timer = INVALID_TIMER;
  10908. if( pc_isinvisible(sd) )
  10909. {// do not calculate the pvp rank for a hidden GM
  10910. return 0;
  10911. }
  10912. if( pc_calc_pvprank(sd) > 0 )
  10913. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  10914. return 0;
  10915. }
  10916. /*==========================================
  10917. * Checking if sd is married
  10918. * Return:
  10919. * partner_id = yes
  10920. * 0 = no
  10921. *------------------------------------------*/
  10922. int pc_ismarried(map_session_data *sd)
  10923. {
  10924. if(sd == nullptr)
  10925. return -1;
  10926. if(sd->status.partner_id > 0)
  10927. return sd->status.partner_id;
  10928. else
  10929. return 0;
  10930. }
  10931. /*==========================================
  10932. * Marry player sd to player dstsd
  10933. * Return:
  10934. * false = fail
  10935. * true = success
  10936. *------------------------------------------*/
  10937. bool pc_marriage(map_session_data *sd,map_session_data *dstsd)
  10938. {
  10939. if(sd == nullptr || dstsd == nullptr ||
  10940. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  10941. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  10942. return false;
  10943. sd->status.partner_id = dstsd->status.char_id;
  10944. dstsd->status.partner_id = sd->status.char_id;
  10945. achievement_update_objective(sd, AG_MARRY, 1, 1);
  10946. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  10947. return true;
  10948. }
  10949. /*==========================================
  10950. * Divorce sd from its partner
  10951. * Return:
  10952. * false = fail
  10953. * true = success
  10954. *------------------------------------------*/
  10955. bool pc_divorce(map_session_data *sd)
  10956. {
  10957. map_session_data *p_sd;
  10958. int i;
  10959. if( sd == nullptr || !pc_ismarried(sd) )
  10960. return false;
  10961. if( !sd->status.partner_id )
  10962. return false; // Char is not married
  10963. if( (p_sd = map_charid2sd(sd->status.partner_id)) == nullptr )
  10964. { // Lets char server do the divorce
  10965. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  10966. return false; // No char server connected
  10967. return true;
  10968. }
  10969. // Both players online, lets do the divorce manually
  10970. sd->status.partner_id = 0;
  10971. p_sd->status.partner_id = 0;
  10972. for( i = 0; i < MAX_INVENTORY; i++ )
  10973. {
  10974. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10975. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10976. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10977. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10978. }
  10979. clif_divorced(sd, p_sd->status.name);
  10980. clif_divorced(p_sd, sd->status.name);
  10981. return true;
  10982. }
  10983. /**
  10984. * Get the partner map_session_data of a player
  10985. * @param sd : the husband|wife session
  10986. * @return partner session or nullptr
  10987. */
  10988. map_session_data *pc_get_partner(map_session_data *sd){
  10989. if (!sd || !pc_ismarried(sd))
  10990. return nullptr;
  10991. return map_charid2sd(sd->status.partner_id);
  10992. }
  10993. /**
  10994. * Get the father map_session_data of a player
  10995. * @param sd : the baby session
  10996. * @return father session or nullptr
  10997. */
  10998. map_session_data *pc_get_father (map_session_data *sd){
  10999. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  11000. return nullptr;
  11001. return map_charid2sd(sd->status.father);
  11002. }
  11003. /**
  11004. * Get the mother map_session_data of a player
  11005. * @param sd : the baby session
  11006. * @return mother session or nullptr
  11007. */
  11008. map_session_data *pc_get_mother (map_session_data *sd){
  11009. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  11010. return nullptr;
  11011. return map_charid2sd(sd->status.mother);
  11012. }
  11013. /*==========================================
  11014. * Get sd children charid. (Need to be married)
  11015. *------------------------------------------*/
  11016. map_session_data *pc_get_child (map_session_data *sd)
  11017. {
  11018. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  11019. // charid2sd returns nullptr if not found
  11020. return nullptr;
  11021. return map_charid2sd(sd->status.child);
  11022. }
  11023. /*==========================================
  11024. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  11025. *------------------------------------------*/
  11026. void pc_bleeding (map_session_data *sd, t_tick diff_tick)
  11027. {
  11028. int hp = 0, sp = 0;
  11029. if( pc_isdead(sd) )
  11030. return;
  11031. if (sd->hp_loss.value) {
  11032. sd->hp_loss.tick += diff_tick;
  11033. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  11034. hp += sd->hp_loss.value;
  11035. sd->hp_loss.tick -= sd->hp_loss.rate;
  11036. }
  11037. if(hp >= sd->battle_status.hp)
  11038. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  11039. }
  11040. if (sd->sp_loss.value) {
  11041. sd->sp_loss.tick += diff_tick;
  11042. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  11043. sp += sd->sp_loss.value;
  11044. sd->sp_loss.tick -= sd->sp_loss.rate;
  11045. }
  11046. }
  11047. if (hp > 0 || sp > 0)
  11048. status_zap(&sd->bl, hp, sp);
  11049. }
  11050. //Character regen. Flag is used to know which types of regen can take place.
  11051. //&1: HP regen
  11052. //&2: SP regen
  11053. void pc_regen (map_session_data *sd, t_tick diff_tick)
  11054. {
  11055. int hp = 0, sp = 0;
  11056. if (sd->hp_regen.value) {
  11057. sd->hp_regen.tick += diff_tick;
  11058. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  11059. hp += sd->hp_regen.value;
  11060. sd->hp_regen.tick -= sd->hp_regen.rate;
  11061. }
  11062. }
  11063. if (sd->sp_regen.value) {
  11064. sd->sp_regen.tick += diff_tick;
  11065. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  11066. sp += sd->sp_regen.value;
  11067. sd->sp_regen.tick -= sd->sp_regen.rate;
  11068. }
  11069. }
  11070. if (sd->percent_hp_regen.value) {
  11071. sd->percent_hp_regen.tick += diff_tick;
  11072. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  11073. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  11074. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  11075. }
  11076. }
  11077. if (sd->percent_sp_regen.value) {
  11078. sd->percent_sp_regen.tick += diff_tick;
  11079. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  11080. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  11081. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  11082. }
  11083. }
  11084. if (hp > 0 || sp > 0)
  11085. status_heal(&sd->bl, hp, sp, 0);
  11086. }
  11087. /*==========================================
  11088. * Memo player sd savepoint. (map,x,y)
  11089. *------------------------------------------*/
  11090. void pc_setsavepoint(map_session_data *sd, short mapindex,int x,int y)
  11091. {
  11092. nullpo_retv(sd);
  11093. safestrncpy( sd->status.save_point.map, mapindex_id2name( mapindex ), sizeof( sd->status.save_point.map ) );
  11094. sd->status.save_point.x = x;
  11095. sd->status.save_point.y = y;
  11096. }
  11097. /*==========================================
  11098. * Save 1 player data at autosave interval
  11099. *------------------------------------------*/
  11100. static TIMER_FUNC(pc_autosave){
  11101. int interval;
  11102. struct s_mapiterator* iter;
  11103. map_session_data* sd;
  11104. static int last_save_id = 0, save_flag = 0;
  11105. if(save_flag == 2) //Someone was saved on last call, normal cycle
  11106. save_flag = 0;
  11107. else
  11108. save_flag = 1; //Noone was saved, so save first found char.
  11109. iter = mapit_getallusers();
  11110. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  11111. {
  11112. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  11113. continue;
  11114. if(sd->bl.id == last_save_id && save_flag != 1) {
  11115. save_flag = 1;
  11116. continue;
  11117. }
  11118. if(save_flag != 1) //Not our turn to save yet.
  11119. continue;
  11120. //Save char.
  11121. last_save_id = sd->bl.id;
  11122. save_flag = 2;
  11123. if (pc_isvip(sd)) // Check if we're still VIP
  11124. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  11125. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  11126. break;
  11127. }
  11128. mapit_free(iter);
  11129. interval = autosave_interval/(map_usercount()+1);
  11130. if(interval < minsave_interval)
  11131. interval = minsave_interval;
  11132. add_timer(gettick()+interval,pc_autosave,0,0);
  11133. return 0;
  11134. }
  11135. static int pc_daynight_timer_sub(map_session_data *sd,va_list ap)
  11136. {
  11137. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  11138. { //Night/day state does not match.
  11139. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  11140. sd->state.night = night_flag;
  11141. return 1;
  11142. }
  11143. return 0;
  11144. }
  11145. /*================================================
  11146. * timer to do the day [Yor]
  11147. * data: 0 = called by timer, 1 = gmcommand/script
  11148. *------------------------------------------------*/
  11149. TIMER_FUNC(map_day_timer){
  11150. char tmp_soutput[1024];
  11151. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  11152. return 0;
  11153. if (!night_flag)
  11154. return 0; //Already day.
  11155. night_flag = 0; // 0=day, 1=night [Yor]
  11156. map_foreachpc(pc_daynight_timer_sub);
  11157. strcpy(tmp_soutput, (data == 0) ? msg_txt(nullptr,502) : msg_txt(nullptr,60)); // The day has arrived!
  11158. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11159. return 0;
  11160. }
  11161. /*================================================
  11162. * timer to do the night [Yor]
  11163. * data: 0 = called by timer, 1 = gmcommand/script
  11164. *------------------------------------------------*/
  11165. TIMER_FUNC(map_night_timer){
  11166. char tmp_soutput[1024];
  11167. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  11168. return 0;
  11169. if (night_flag)
  11170. return 0; //Already nigth.
  11171. night_flag = 1; // 0=day, 1=night [Yor]
  11172. map_foreachpc(pc_daynight_timer_sub);
  11173. strcpy(tmp_soutput, (data == 0) ? msg_txt(nullptr,503) : msg_txt(nullptr,59)); // The night has fallen...
  11174. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11175. return 0;
  11176. }
  11177. /**
  11178. * Attempt to stand up a player
  11179. * @param sd
  11180. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  11181. * @return True if success, Fals if failed
  11182. */
  11183. bool pc_setstand(map_session_data *sd, bool force){
  11184. nullpo_ret(sd);
  11185. // Cannot stand yet
  11186. // TODO: Move to SCS_NOSTAND [Cydh]
  11187. if (!force && (sd->sc.getSCE(SC_SITDOWN_FORCE) || sd->sc.getSCE(SC_BANANA_BOMB_SITDOWN)))
  11188. return false;
  11189. status_change_end(&sd->bl, SC_TENSIONRELAX);
  11190. clif_status_load(&sd->bl,EFST_SIT,0);
  11191. clif_standing(&sd->bl); //Inform area PC is standing
  11192. //Reset sitting tick.
  11193. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  11194. if( pc_isdead( sd ) ){
  11195. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11196. clif_party_dead( *sd );
  11197. }else{
  11198. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11199. }
  11200. return true;
  11201. }
  11202. /**
  11203. * Calculate Overheat value
  11204. * @param sd: Player data
  11205. * @param heat: Amount of Heat to adjust
  11206. **/
  11207. void pc_overheat(map_session_data &sd, int16 heat) {
  11208. status_change_entry *sce = sd.sc.getSCE(SC_OVERHEAT_LIMITPOINT);
  11209. if (sce) {
  11210. sce->val1 += heat;
  11211. sce->val1 = cap_value(sce->val1, 0, 1000);
  11212. if (heat < 0 && sce->val1 == 0) { // Cooling device used.
  11213. status_change_end(&sd.bl, SC_OVERHEAT_LIMITPOINT);
  11214. status_change_end(&sd.bl, SC_OVERHEAT);
  11215. }
  11216. } else if (heat > 0)
  11217. sc_start(&sd.bl, &sd.bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  11218. }
  11219. /**
  11220. * Check if player is autolooting given itemID.
  11221. */
  11222. bool pc_isautolooting(map_session_data *sd, t_itemid nameid)
  11223. {
  11224. uint8 i = 0;
  11225. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  11226. return true;
  11227. if (!sd->state.autolooting)
  11228. return false;
  11229. if (sd->state.autolooting)
  11230. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  11231. return (i != AUTOLOOTITEM_SIZE);
  11232. }
  11233. /**
  11234. * Checks if player can use @/#command
  11235. * @param sd Player map session data
  11236. * @param command Command name without @/# and params
  11237. * @param type is it atcommand or charcommand
  11238. */
  11239. bool pc_can_use_command( map_session_data *sd, const char *command, AtCommandType type ){
  11240. return sd->group->can_use_command( command, type );
  11241. }
  11242. bool pc_has_permission( map_session_data* sd, e_pc_permission permission ){
  11243. return sd->permissions.test( permission );
  11244. }
  11245. /**
  11246. * Checks if commands used by a player should be logged
  11247. * according to their group setting.
  11248. * @param sd Player map session data
  11249. */
  11250. bool pc_should_log_commands( map_session_data *sd ){
  11251. return sd->group->log_commands;
  11252. }
  11253. /**
  11254. * Spirit Charm expiration timer.
  11255. * @see TimerFunc
  11256. */
  11257. static TIMER_FUNC(pc_spiritcharm_timer){
  11258. map_session_data *sd;
  11259. int i;
  11260. if ((sd = (map_session_data *)map_id2sd(id)) == nullptr || sd->bl.type != BL_PC)
  11261. return 1;
  11262. if (sd->spiritcharm <= 0) {
  11263. 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);
  11264. sd->spiritcharm = 0;
  11265. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11266. return 0;
  11267. }
  11268. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  11269. if (i == sd->spiritcharm) {
  11270. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  11271. return 0;
  11272. }
  11273. sd->spiritcharm--;
  11274. if (i != sd->spiritcharm)
  11275. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  11276. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11277. if (sd->spiritcharm <= 0)
  11278. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11279. clif_spiritcharm(sd);
  11280. return 0;
  11281. }
  11282. /**
  11283. * Adds a spirit charm.
  11284. * @param sd: Target character
  11285. * @param interval: Duration
  11286. * @param max: Maximum amount of charms to add
  11287. * @param type: Charm type (@see spirit_charm_types)
  11288. */
  11289. void pc_addspiritcharm(map_session_data *sd, int interval, int max, int type)
  11290. {
  11291. int tid, i;
  11292. nullpo_retv(sd);
  11293. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  11294. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  11295. if (max > MAX_SPIRITCHARM)
  11296. max = MAX_SPIRITCHARM;
  11297. if (sd->spiritcharm < 0)
  11298. sd->spiritcharm = 0;
  11299. if (sd->spiritcharm && sd->spiritcharm >= max) {
  11300. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  11301. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  11302. sd->spiritcharm--;
  11303. if (sd->spiritcharm != 0)
  11304. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  11305. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11306. }
  11307. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  11308. 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);
  11309. if (i != sd->spiritcharm)
  11310. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  11311. sd->spiritcharm_timer[i] = tid;
  11312. sd->spiritcharm++;
  11313. sd->spiritcharm_type = type;
  11314. clif_spiritcharm(sd);
  11315. }
  11316. /**
  11317. * Removes one or more spirit charms.
  11318. * @param sd: The target character
  11319. * @param count: Amount of charms to remove
  11320. * @param type: Type of charm to remove
  11321. */
  11322. void pc_delspiritcharm(map_session_data *sd, int count, int type)
  11323. {
  11324. int i;
  11325. nullpo_retv(sd);
  11326. if (sd->spiritcharm_type != type)
  11327. return;
  11328. if (sd->spiritcharm <= 0) {
  11329. sd->spiritcharm = 0;
  11330. return;
  11331. }
  11332. if (count <= 0)
  11333. return;
  11334. if (count > sd->spiritcharm)
  11335. count = sd->spiritcharm;
  11336. sd->spiritcharm -= count;
  11337. if (count > MAX_SPIRITCHARM)
  11338. count = MAX_SPIRITCHARM;
  11339. for (i = 0; i < count; i++) {
  11340. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  11341. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  11342. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11343. }
  11344. }
  11345. for (i = count; i < MAX_SPIRITCHARM; i++) {
  11346. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  11347. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11348. }
  11349. if (sd->spiritcharm <= 0)
  11350. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11351. clif_spiritcharm(sd);
  11352. }
  11353. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  11354. /**
  11355. * Renewal EXP/Item Drop rate modifier based on level penalty
  11356. * @param level_diff: Monster and Player level difference
  11357. * @param mob_class: Monster class
  11358. * @param mode: Monster mode
  11359. * @param type: 1 - EXP, 2 - Item Drop
  11360. * @return Penalty rate
  11361. */
  11362. uint16 pc_level_penalty_mod( map_session_data* sd, e_penalty_type type, std::shared_ptr<s_mob_db> mob, mob_data* md ){
  11363. // No player was attached, we don't use any modifier (100 = rates are not touched)
  11364. if( sd == nullptr ){
  11365. return 100;
  11366. }
  11367. if ((type == PENALTY_DROP && map_getmapflag(sd->bl.m, MF_NORENEWALDROPPENALTY)) || (type == PENALTY_EXP && map_getmapflag(sd->bl.m, MF_NORENEWALEXPPENALTY))) {
  11368. return 100;
  11369. }
  11370. int monster_level;
  11371. if( md != nullptr ){
  11372. monster_level = md->level;
  11373. mob = md->db;
  11374. }else if( mob != nullptr ){
  11375. monster_level = mob->lv;
  11376. }else{
  11377. return 100;
  11378. }
  11379. if( ( type == PENALTY_DROP || type == PENALTY_MVP_DROP ) && status_has_mode( &mob->status, MD_FIXEDITEMDROP ) ){
  11380. return 100;
  11381. }
  11382. int level_difference = monster_level - sd->status.base_level;
  11383. std::shared_ptr<s_penalty> penalty = penalty_db.find( type );
  11384. if( penalty != nullptr ){
  11385. return penalty->rate[ level_difference + MAX_LEVEL - 1 ];
  11386. }else{
  11387. return 100;
  11388. }
  11389. }
  11390. #endif
  11391. int pc_split_str(char *str,char **val,int num)
  11392. {
  11393. int i;
  11394. for (i=0; i<num && str; i++){
  11395. val[i] = str;
  11396. str = strchr(str,',');
  11397. if (str && i<num-1) //Do not remove a trailing comma.
  11398. *str++=0;
  11399. }
  11400. return i;
  11401. }
  11402. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  11403. {
  11404. static int warning=0;
  11405. int i,j;
  11406. for (i=0; i<max; i++) {
  11407. double f;
  11408. if (!str) break;
  11409. f = atof(str);
  11410. if (f < 0)
  11411. val[i] = 0;
  11412. else if (f > UINT_MAX) {
  11413. val[i] = UINT_MAX;
  11414. if (!warning) {
  11415. warning = 1;
  11416. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  11417. }
  11418. } else
  11419. val[i] = (unsigned int)f;
  11420. str = strchr(str,sep);
  11421. if (str)
  11422. *str++=0;
  11423. }
  11424. //Zero up the remaining.
  11425. for(j=i; j < max; j++)
  11426. val[j] = 0;
  11427. return i;
  11428. }
  11429. std::shared_ptr<s_skill_tree_entry> SkillTreeDatabase::get_skill_data(int class_, uint16 skill_id) {
  11430. std::shared_ptr<s_skill_tree> tree = this->find(class_);
  11431. if (tree != nullptr)
  11432. return util::umap_find(tree->skills, skill_id);
  11433. return nullptr;
  11434. }
  11435. const std::string SkillTreeDatabase::getDefaultLocation() {
  11436. return std::string(db_path) + "/skill_tree.yml";
  11437. }
  11438. /**
  11439. * Reads and parses an entry from the skill_tree.
  11440. * @param node: YAML node containing the entry.
  11441. * @return count of successfully parsed rows
  11442. */
  11443. uint64 SkillTreeDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11444. std::string job_name;
  11445. if (!this->asString(node, "Job", job_name))
  11446. return 0;
  11447. int64 constant;
  11448. std::string job_name_constant = "JOB_" + job_name;
  11449. if (!script_get_constant(job_name_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11450. this->invalidWarning(node["Job"], "Invalid job %s.\n", job_name.c_str());
  11451. return 0;
  11452. }
  11453. uint16 job_id = static_cast<uint16>(constant);
  11454. std::shared_ptr<s_skill_tree> tree = this->find(job_id);
  11455. bool exists = tree != nullptr;
  11456. if (!exists)
  11457. tree = std::make_shared<s_skill_tree>();
  11458. if (this->nodeExists(node, "Inherit")) {
  11459. const ryml::NodeRef& InheritNode = node["Inherit"];
  11460. for (const auto &Inheritit : InheritNode) {
  11461. std::string inheritname;
  11462. c4::from_chars(Inheritit.key(), &inheritname);
  11463. std::string inheritname_constant = "JOB_" + inheritname;
  11464. if (!script_get_constant(inheritname_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11465. this->invalidWarning(InheritNode[Inheritit.key()], "Invalid job %s.\n", inheritname.c_str());
  11466. return 0;
  11467. }
  11468. bool active;
  11469. if (!this->asBool(InheritNode, inheritname, active))
  11470. return 0;
  11471. uint16 inherit_job = static_cast<uint16>(constant);
  11472. if (!active) {
  11473. if (exists)
  11474. util::vector_erase_if_exists(tree->inherit_job, inherit_job);
  11475. }
  11476. else {
  11477. if (!util::vector_exists(tree->inherit_job, inherit_job))
  11478. tree->inherit_job.push_back(inherit_job);
  11479. }
  11480. }
  11481. }
  11482. if (this->nodeExists(node, "Tree")) {
  11483. for (const auto &it : node["Tree"]) {
  11484. std::string skill_name;
  11485. if (!this->asString(it, "Name", skill_name))
  11486. return 0;
  11487. uint16 skill_id = skill_name2id(skill_name.c_str());
  11488. if (skill_id == 0) {
  11489. this->invalidWarning(it["Name"], "Invalid skill name \"%s\".\n", skill_name.c_str());
  11490. return 0;
  11491. }
  11492. if (!skill_get_index(skill_id)) {
  11493. this->invalidWarning(it["Name"], "Unable to load skill %s into job %hu's tree.\n", skill_name.c_str(), job_id);
  11494. return 0;
  11495. }
  11496. std::shared_ptr<s_skill_tree_entry> entry;
  11497. bool skill_exists = tree->skills.count(skill_id) > 0;
  11498. if (skill_exists)
  11499. entry = tree->skills[skill_id];
  11500. else
  11501. entry = std::make_shared<s_skill_tree_entry>();
  11502. entry->skill_id = skill_id;
  11503. uint16 max_lv;
  11504. if (!this->asUInt16(it, "MaxLevel", max_lv))
  11505. return 0;
  11506. if (max_lv > MAX_SKILL_LEVEL) {
  11507. this->invalidWarning(it["MaxLevel"], "MaxLevel exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11508. return 0;
  11509. }
  11510. uint16 skill_lv_max = skill_get_max(skill_id);
  11511. if (max_lv > skill_lv_max) {
  11512. 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);
  11513. max_lv = skill_lv_max;
  11514. }
  11515. // if (max_lv == 0) { // skill lvl 0 removed on loadingFinished (because of inherit)
  11516. // if (!skill_exists || entry->skill_id.erase(skill_id) == 0)
  11517. // 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());
  11518. // continue;
  11519. // }
  11520. entry->max_lv = max_lv;
  11521. if (this->nodeExists(it, "BaseLevel")) {
  11522. uint32 baselv;
  11523. if (!this->asUInt32(it, "BaseLevel", baselv))
  11524. return 0;
  11525. uint32 baselv_max = job_db.get_maxBaseLv(job_id);
  11526. if (baselv > baselv_max) {
  11527. this->invalidWarning(it["BaseLevel"], "Skill %hu's base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11528. skill_id, baselv, job_name.c_str(), baselv_max);
  11529. baselv = baselv_max;
  11530. }
  11531. entry->baselv = baselv;
  11532. } else {
  11533. if (!skill_exists)
  11534. entry->baselv = 0;
  11535. }
  11536. if (this->nodeExists(it, "JobLevel")) {
  11537. uint32 joblv;
  11538. if (!this->asUInt32(it, "JobLevel", joblv))
  11539. return 0;
  11540. uint32 joblv_max = job_db.get_maxJobLv(job_id);
  11541. if (joblv > joblv_max) {
  11542. this->invalidWarning(it["JobLevel"], "Skill %hu's job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11543. skill_id, joblv, job_name.c_str(), joblv_max);
  11544. joblv = joblv_max;
  11545. }
  11546. entry->joblv = joblv;
  11547. } else {
  11548. if (!skill_exists)
  11549. entry->joblv = 0;
  11550. }
  11551. if (this->nodeExists(it, "Requires")) {
  11552. for (const auto &Requiresit : it["Requires"]) {
  11553. if (!this->nodesExist(Requiresit, { "Name" }))
  11554. return 0;
  11555. std::string skill_name_req;
  11556. if (!this->asString(Requiresit, "Name", skill_name_req))
  11557. return 0;
  11558. uint16 skill_id_req = skill_name2id(skill_name_req.c_str());
  11559. if (skill_id_req == 0) {
  11560. this->invalidWarning(Requiresit["Name"], "Invalid skill name \"%s\".\n", skill_name_req.c_str());
  11561. return 0;
  11562. }
  11563. uint16 lv_req;
  11564. if (!this->asUInt16(Requiresit, "Level", lv_req))
  11565. return 0;
  11566. if (lv_req > MAX_SKILL_LEVEL) {
  11567. this->invalidWarning(Requiresit["Level"], "Level exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11568. return 0;
  11569. }
  11570. uint16 lv_req_max = skill_get_max(skill_id_req);
  11571. if (lv_req > lv_req_max) {
  11572. 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);
  11573. lv_req = lv_req_max;
  11574. }
  11575. if (lv_req == 0) {
  11576. if (entry->need.erase(skill_id_req) == 0)
  11577. 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());
  11578. continue;
  11579. }
  11580. entry->need[skill_id_req] = lv_req;
  11581. }
  11582. }
  11583. if (this->nodeExists(it, "Exclude")) {
  11584. bool exclude;
  11585. if (!this->asBool(it, "Exclude", exclude))
  11586. return 0;
  11587. entry->exclude_inherit = exclude;
  11588. } else {
  11589. if (!skill_exists)
  11590. entry->exclude_inherit = false;
  11591. }
  11592. if (!skill_exists)
  11593. tree->skills.insert({ skill_id, entry });
  11594. }
  11595. }
  11596. if (!exists)
  11597. this->put(job_id, tree);
  11598. return true;
  11599. }
  11600. void SkillTreeDatabase::loadingFinished() {
  11601. std::unordered_map<uint16, std::shared_ptr<s_skill_tree>> job_tree; // get the data from skill_tree_db before populate it
  11602. for (auto &data : *this) {
  11603. if (data.second->inherit_job.empty())
  11604. continue;
  11605. std::shared_ptr<s_skill_tree> skill_tree = std::make_shared<s_skill_tree>();
  11606. uint32 baselv_max = job_db.get_maxBaseLv(data.first);
  11607. uint32 joblv_max = job_db.get_maxJobLv(data.first);
  11608. for (const auto &inherit_job : data.second->inherit_job) {
  11609. std::shared_ptr<s_skill_tree> tree = this->find(inherit_job);
  11610. if (tree == nullptr || tree->skills.empty())
  11611. continue;
  11612. for (const auto &it : tree->skills) {
  11613. if (it.second->exclude_inherit)
  11614. continue;
  11615. if (data.second->skills.count(it.first) > 0) // skill already in the skill tree
  11616. continue;
  11617. if (skill_tree->skills.count(it.first) > 0) // replaced by the last inheritance
  11618. skill_tree->skills[it.first] = it.second;
  11619. else
  11620. skill_tree->skills.insert({ it.first, it.second });
  11621. std::shared_ptr<s_skill_tree_entry> skill = skill_tree->skills[it.first];
  11622. if (skill->baselv > baselv_max) {
  11623. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11624. skill_get_name(skill->skill_id), skill->skill_id, skill->baselv, job_name(data.first), baselv_max);
  11625. skill->baselv = baselv_max;
  11626. }
  11627. if (skill->joblv > joblv_max) {
  11628. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11629. skill_get_name(skill->skill_id), skill->skill_id, skill->joblv, job_name(data.first), joblv_max);
  11630. skill->joblv = joblv_max;
  11631. }
  11632. }
  11633. }
  11634. if (skill_tree != nullptr && !skill_tree->skills.empty())
  11635. job_tree.insert({ data.first, skill_tree });
  11636. }
  11637. if (!job_tree.empty()) {
  11638. for (auto &data : *this) {
  11639. if (job_tree.count(data.first) == 0)
  11640. continue;
  11641. data.second->skills.insert(job_tree[data.first]->skills.begin(), job_tree[data.first]->skills.end());
  11642. }
  11643. }
  11644. // remove skills with max_lv = 0
  11645. for (const auto &job : *this) {
  11646. if (job.second->skills.empty())
  11647. continue;
  11648. auto it = job.second->skills.begin();
  11649. while( it != job.second->skills.end() ){
  11650. if( it->second->max_lv == 0 ){
  11651. it = job.second->skills.erase( it );
  11652. }else{
  11653. it++;
  11654. }
  11655. }
  11656. }
  11657. TypesafeYamlDatabase::loadingFinished();
  11658. }
  11659. /**
  11660. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  11661. * @param level: Base level of player
  11662. * @param job_id: Job ID @see enum e_job
  11663. * @return base_hp
  11664. * @author [Cydh]
  11665. */
  11666. static unsigned int pc_calc_basehp(uint16 level, uint16 job_id) {
  11667. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11668. double base_hp = 35 + level * (job->hp_increase / 100.);
  11669. #ifndef RENEWAL
  11670. if (level >= 10 && (job_id == JOB_NINJA || job_id == JOB_GUNSLINGER))
  11671. base_hp += 90;
  11672. #endif
  11673. for (uint16 i = 2; i <= level; i++)
  11674. base_hp += floor(((job->hp_factor / 100.) * i) + 0.5); //Don't have round()
  11675. if (job_id == JOB_SUMMONER || job_id == JOB_SPIRIT_HANDLER)
  11676. base_hp += floor((base_hp / 2) + 0.5);
  11677. return (unsigned int)base_hp;
  11678. }
  11679. /**
  11680. * Calculates base sp of player.
  11681. * @param level: Base level of player
  11682. * @param job_id: Job ID @see enum e_job
  11683. * @return base_sp
  11684. * @author [Playtester]
  11685. */
  11686. static unsigned int pc_calc_basesp(uint16 level, uint16 job_id) {
  11687. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11688. double base_sp = 10 + floor(level * (job->sp_increase / 100.));
  11689. switch (job_id) {
  11690. case JOB_NINJA:
  11691. if (level >= 10)
  11692. base_sp -= 22;
  11693. else
  11694. base_sp = 11 + 3*level;
  11695. break;
  11696. case JOB_GUNSLINGER:
  11697. if (level > 10)
  11698. base_sp -= 18;
  11699. else
  11700. base_sp = 9 + 3*level;
  11701. break;
  11702. case JOB_SUMMONER:
  11703. case JOB_SPIRIT_HANDLER:
  11704. base_sp -= floor(base_sp / 2);
  11705. break;
  11706. }
  11707. return (unsigned int)base_sp;
  11708. }
  11709. const std::string JobDatabase::getDefaultLocation() {
  11710. return std::string(db_path) + "/job_stats.yml";
  11711. }
  11712. /**
  11713. * Reads and parses an entry from the job_db.
  11714. * @param node: YAML node containing the entry.
  11715. * @return count of successfully parsed rows
  11716. */
  11717. uint64 JobDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11718. if (this->nodeExists(node, "Jobs")) {
  11719. const ryml::NodeRef& jobsNode = node["Jobs"];
  11720. for (const auto &jobit : jobsNode) {
  11721. std::string job_name;
  11722. c4::from_chars(jobit.key(), &job_name);
  11723. std::string job_name_constant = "JOB_" + job_name;
  11724. int64 job_id;
  11725. if (!script_get_constant(job_name_constant.c_str(), &job_id)) {
  11726. this->invalidWarning(node["Job"], "Job %s does not exist.\n", job_name.c_str());
  11727. return 0;
  11728. }
  11729. std::shared_ptr<s_job_info> job = job_db.find(static_cast<uint16>(job_id));
  11730. bool exists = job != nullptr;
  11731. if (!exists) {
  11732. job = std::make_shared<s_job_info>();
  11733. job->job_bonus.resize(MAX_LEVEL);
  11734. std::fill(job->job_bonus.begin(), job->job_bonus.end(), std::array<uint16, PARAM_MAX> { 0 });
  11735. job->base_hp.resize(MAX_LEVEL);
  11736. std::fill(job->base_hp.begin(), job->base_hp.end(), 0);
  11737. job->base_sp.resize(MAX_LEVEL);
  11738. std::fill(job->base_sp.begin(), job->base_sp.end(), 0);
  11739. job->base_ap.resize(MAX_LEVEL);
  11740. std::fill(job->base_ap.begin(), job->base_ap.end(), 0);
  11741. }
  11742. if (this->nodeExists(node, "MaxWeight")) {
  11743. uint32 weight;
  11744. if (!this->asUInt32(node, "MaxWeight", weight))
  11745. return 0;
  11746. job->max_weight_base = weight;
  11747. } else {
  11748. if (!exists)
  11749. job->max_weight_base = 20000;
  11750. }
  11751. if (this->nodeExists(node, "HpFactor")) {
  11752. uint32 hp;
  11753. if (!this->asUInt32(node, "HpFactor", hp))
  11754. return 0;
  11755. job->hp_factor = hp;
  11756. } else {
  11757. if (!exists)
  11758. job->hp_factor = 0;
  11759. }
  11760. if (this->nodeExists(node, "HpIncrease")) {
  11761. uint32 hp;
  11762. if (!this->asUInt32(node, "HpIncrease", hp))
  11763. return 0;
  11764. job->hp_increase = hp;
  11765. } else {
  11766. if (!exists)
  11767. job->hp_increase = 500;
  11768. }
  11769. if (this->nodeExists(node, "SpIncrease")) {
  11770. uint32 sp;
  11771. if (!this->asUInt32(node, "SpIncrease", sp))
  11772. return 0;
  11773. job->sp_increase = sp;
  11774. } else {
  11775. if (!exists)
  11776. job->sp_increase = 100;
  11777. }
  11778. if (this->nodeExists(node, "BaseASPD")) {
  11779. const ryml::NodeRef& aspdNode = node["BaseASPD"];
  11780. uint8 max = MAX_WEAPON_TYPE;
  11781. #ifdef RENEWAL // Renewal adds an extra column for shields
  11782. max += 1;
  11783. #endif
  11784. if (!exists) {
  11785. job->aspd_base.resize(max);
  11786. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11787. }
  11788. for (const auto &aspdit : aspdNode) {
  11789. std::string weapon;
  11790. c4::from_chars(aspdit.key(), &weapon);
  11791. std::string weapon_constant = "W_" + weapon;
  11792. int64 constant;
  11793. if (!script_get_constant(weapon_constant.c_str(), &constant)) {
  11794. this->invalidWarning(aspdNode["BaseASPD"], "Unknown weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11795. continue;
  11796. }
  11797. if (constant < W_FIST || constant >= max) {
  11798. this->invalidWarning(aspdNode["BaseASPD"], "Invalid weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11799. continue;
  11800. }
  11801. int16 aspd;
  11802. if (!this->asInt16(aspdNode, weapon.c_str(), aspd))
  11803. return 0;
  11804. job->aspd_base[static_cast<int16>(constant)] = aspd;
  11805. }
  11806. } else {
  11807. if (!exists) {
  11808. uint8 max = MAX_WEAPON_TYPE;
  11809. #ifdef RENEWAL // Renewal adds an extra column for shields
  11810. max += 1;
  11811. #endif
  11812. job->aspd_base.resize(max);
  11813. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11814. }
  11815. }
  11816. if (this->nodeExists(node, "MaxStats")) {
  11817. const ryml::NodeRef& statNode = node["MaxStats"];
  11818. for (const auto &statit : statNode) {
  11819. std::string stat;
  11820. c4::from_chars(statit.key(), &stat);
  11821. std::string stat_constant = "PARAM_" + stat;
  11822. int64 constant;
  11823. if (!script_get_constant(stat_constant.c_str(), &constant)) {
  11824. this->invalidWarning(statNode["Bonus"], "Unknown max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11825. continue;
  11826. }
  11827. if (constant < PARAM_STR || constant >= PARAM_MAX) {
  11828. this->invalidWarning(statNode["Bonus"], "Invalid max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11829. continue;
  11830. }
  11831. uint16 max;
  11832. if (!this->asUInt16(statNode, stat.c_str(), max))
  11833. return 0;
  11834. job->max_param[constant] = max;
  11835. }
  11836. }
  11837. if (this->nodeExists(node, "MaxBaseLevel")) {
  11838. uint16 level;
  11839. if (!this->asUInt16(node, "MaxBaseLevel", level))
  11840. return 0;
  11841. if (level == 0 || level > MAX_LEVEL) {
  11842. this->invalidWarning(node["MaxBaseLevel"], "MaxBaseLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11843. level = MAX_LEVEL;
  11844. }
  11845. job->max_base_level = level;
  11846. } else {
  11847. if (!exists)
  11848. job->max_base_level = MAX_LEVEL;
  11849. }
  11850. if (this->nodeExists(node, "BaseExp")) {
  11851. for (const ryml::NodeRef& bexpNode : node["BaseExp"]) {
  11852. uint16 level;
  11853. if (!this->asUInt16(bexpNode, "Level", level))
  11854. return 0;
  11855. if (level > job->max_base_level)
  11856. continue;
  11857. if (level == 0 || level > MAX_LEVEL) {
  11858. this->invalidWarning(bexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11859. return 0;
  11860. }
  11861. if (this->nodeExists(bexpNode, "Exp")) {
  11862. t_exp exp;
  11863. if (!this->asUInt64(bexpNode, "Exp", exp))
  11864. return 0;
  11865. job->base_exp[level - 1] = exp;
  11866. }
  11867. }
  11868. }
  11869. if (this->nodeExists(node, "MaxJobLevel")) {
  11870. uint16 level;
  11871. if (!this->asUInt16(node, "MaxJobLevel", level))
  11872. return 0;
  11873. if (level == 0 || level > MAX_LEVEL) {
  11874. this->invalidWarning(node["MaxJobLevel"], "MaxJobLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11875. level = MAX_LEVEL;
  11876. }
  11877. job->max_job_level = level;
  11878. } else {
  11879. if (!exists)
  11880. job->max_job_level = MAX_LEVEL;
  11881. }
  11882. if (this->nodeExists(node, "JobExp")) {
  11883. for (const ryml::NodeRef& jexpNode : node["JobExp"]) {
  11884. uint16 level;
  11885. if (!this->asUInt16(jexpNode, "Level", level))
  11886. return 0;
  11887. if (level > job->max_job_level)
  11888. continue;
  11889. if (level == 0 || level > MAX_LEVEL) {
  11890. this->invalidWarning(jexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11891. return 0;
  11892. }
  11893. if (this->nodeExists(jexpNode, "Exp")) {
  11894. t_exp exp;
  11895. if (!this->asUInt64(jexpNode, "Exp", exp))
  11896. return 0;
  11897. job->job_exp[level - 1] = exp;
  11898. }
  11899. }
  11900. }
  11901. if (this->nodeExists(node, "BonusStats")) {
  11902. const ryml::NodeRef& bonusNode = node["BonusStats"];
  11903. for (const ryml::NodeRef& levelNode : bonusNode) {
  11904. uint16 level;
  11905. if (!this->asUInt16(levelNode, "Level", level))
  11906. return 0;
  11907. if (level == 0 || level > MAX_LEVEL) {
  11908. this->invalidWarning(levelNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11909. return 0;
  11910. }
  11911. for (uint8 idx = PARAM_STR; idx < PARAM_MAX; idx++) {
  11912. if (this->nodeExists(levelNode, parameter_names[idx])) {
  11913. int16 change;
  11914. if (!this->asInt16(levelNode, parameter_names[idx], change))
  11915. return 0;
  11916. job->job_bonus[level - 1][idx] = change;
  11917. }
  11918. }
  11919. }
  11920. }
  11921. #ifdef HP_SP_TABLES
  11922. if (this->nodeExists(node, "BaseHp")) {
  11923. for (const ryml::NodeRef& bhpNode : node["BaseHp"]) {
  11924. uint16 level;
  11925. if (!this->asUInt16(bhpNode, "Level", level))
  11926. return 0;
  11927. if (level > job->max_base_level)
  11928. continue;
  11929. if (level == 0 || level > MAX_LEVEL) {
  11930. this->invalidWarning(bhpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11931. return 0;
  11932. }
  11933. if (this->nodeExists(bhpNode, "Hp")) {
  11934. uint32 points;
  11935. if (!this->asUInt32(bhpNode, "Hp", points))
  11936. return 0;
  11937. job->base_hp[level - 1] = points;
  11938. }
  11939. }
  11940. }
  11941. if (this->nodeExists(node, "BaseSp")) {
  11942. for (const ryml::NodeRef& bspNode : node["BaseSp"]) {
  11943. uint16 level;
  11944. if (!this->asUInt16(bspNode, "Level", level))
  11945. return 0;
  11946. if (level > job->max_base_level)
  11947. continue;
  11948. if (level == 0 || level > MAX_LEVEL) {
  11949. this->invalidWarning(bspNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11950. return 0;
  11951. }
  11952. if (this->nodeExists(bspNode, "Sp")) {
  11953. uint32 points;
  11954. if (!this->asUInt32(bspNode, "Sp", points))
  11955. return 0;
  11956. job->base_sp[level - 1] = points;
  11957. }
  11958. }
  11959. }
  11960. if (this->nodeExists(node, "BaseAp")) {
  11961. for (const ryml::NodeRef& bapNode : node["BaseAp"]) {
  11962. uint16 level;
  11963. if (!this->asUInt16(bapNode, "Level", level))
  11964. return 0;
  11965. if (level > job->max_base_level)
  11966. continue;
  11967. if (level == 0 || level > MAX_LEVEL) {
  11968. this->invalidWarning(bapNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11969. return 0;
  11970. }
  11971. if (this->nodeExists(bapNode, "Ap")) {
  11972. uint32 points;
  11973. if (!this->asUInt32(bapNode, "Ap", points))
  11974. return 0;
  11975. job->base_ap[level - 1] = points;
  11976. }
  11977. }
  11978. }
  11979. #endif
  11980. if (!exists)
  11981. this->put(static_cast<uint16>(job_id), job);
  11982. }
  11983. }
  11984. return 1;
  11985. }
  11986. void JobDatabase::loadingFinished() {
  11987. // Checking if all class have their data
  11988. for (auto &jobIt : *this) {
  11989. uint16 job_id = jobIt.first;
  11990. if (!pcdb_checkid(job_id))
  11991. continue;
  11992. 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)
  11993. continue; // Classes that do not need exp tables.
  11994. std::shared_ptr<s_job_info> job = jobIt.second;
  11995. uint16 maxBaseLv = job->max_base_level, maxJobLv = job->max_job_level;
  11996. if (!maxBaseLv)
  11997. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(job_id), job_id);
  11998. if (!maxJobLv)
  11999. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(job_id), job_id);
  12000. // Init and checking the empty value of Base HP/SP [Cydh]
  12001. if (job->base_hp.empty())
  12002. job->base_hp.resize(maxBaseLv);
  12003. for (uint16 j = 0; j < maxBaseLv; j++) {
  12004. if (job->base_hp[j] == 0)
  12005. job->base_hp[j] = pc_calc_basehp(j + 1, job_id);
  12006. }
  12007. if (job->base_sp.empty())
  12008. job->base_sp.resize(maxBaseLv);
  12009. for (uint16 j = 0; j < maxBaseLv; j++) {
  12010. if (job->base_sp[j] == 0)
  12011. job->base_sp[j] = pc_calc_basesp(j + 1, job_id);
  12012. }
  12013. // Resize to the maximum base level
  12014. if (job->base_hp.capacity() > maxBaseLv)
  12015. job->base_hp.erase(job->base_hp.begin() + maxBaseLv, job->base_hp.end());
  12016. if (job->base_sp.capacity() > maxBaseLv)
  12017. job->base_sp.erase(job->base_sp.begin() + maxBaseLv, job->base_sp.end());
  12018. if (job->base_ap.capacity() > maxBaseLv)
  12019. job->base_ap.erase(job->base_ap.begin() + maxBaseLv, job->base_ap.end());
  12020. // Resize to the maximum job level
  12021. if (job->job_bonus.capacity() > maxJobLv)
  12022. job->job_bonus.erase(job->job_bonus.begin() + maxJobLv, job->job_bonus.end());
  12023. for (uint16 parameter = PARAM_STR; parameter < PARAM_MAX; parameter++) {
  12024. // Store total
  12025. int16 current = 0;
  12026. for (uint16 job_level = 0; job_level < maxJobLv; job_level++) {
  12027. // Add the bonus from this job level
  12028. current += job->job_bonus[job_level][parameter];
  12029. // Set the new total on this job level
  12030. job->job_bonus[job_level][parameter] = current;
  12031. }
  12032. }
  12033. uint64 class_ = pc_jobid2mapid( job_id );
  12034. // Set normal status limits
  12035. uint16 max = battle_config.max_parameter;
  12036. do{
  12037. // Always check babies first
  12038. if( class_ & JOBL_BABY ){
  12039. if( class_ & JOBL_THIRD ){
  12040. max = battle_config.max_baby_third_parameter;
  12041. break;
  12042. }else{
  12043. max = battle_config.max_baby_parameter;
  12044. break;
  12045. }
  12046. }
  12047. // Summoner
  12048. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  12049. max = battle_config.max_summoner_parameter;
  12050. break;
  12051. }
  12052. // Extended classes
  12053. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  12054. max = battle_config.max_extended_parameter;
  12055. break;
  12056. }
  12057. if( class_ & JOBL_FOURTH ){
  12058. max = battle_config.max_fourth_parameter;
  12059. break;
  12060. }
  12061. // 3rd class
  12062. if( class_ & JOBL_THIRD ){
  12063. // Transcendent
  12064. if( class_ & JOBL_UPPER ){
  12065. max = battle_config.max_third_trans_parameter;
  12066. break;
  12067. }else{
  12068. max = battle_config.max_third_parameter;
  12069. break;
  12070. }
  12071. }
  12072. // Transcendent
  12073. if( class_ & JOBL_UPPER ){
  12074. max = battle_config.max_trans_parameter;
  12075. break;
  12076. }
  12077. }while( false );
  12078. for( uint16 parameter = PARAM_STR; parameter < PARAM_POW; parameter++ ){
  12079. // If it is not explicitly set in the database file
  12080. if( job->max_param[parameter] == 0 ){
  12081. job->max_param[parameter] = max;
  12082. }
  12083. }
  12084. // Set trait status limit
  12085. if( class_ & JOBL_FOURTH ){
  12086. max = battle_config.max_trait_parameter;
  12087. }else{
  12088. max = 0;
  12089. }
  12090. for( uint16 parameter = PARAM_POW; parameter < PARAM_MAX; parameter++ ){
  12091. // If it is not explicitly set in the database file
  12092. if( job->max_param[parameter] == 0 ){
  12093. job->max_param[parameter] = max;
  12094. }
  12095. }
  12096. }
  12097. TypesafeCachedYamlDatabase::loadingFinished();
  12098. }
  12099. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  12100. return std::string(db_path) + "/statpoint.yml";
  12101. }
  12102. uint64 PlayerStatPointDatabase::parseBodyNode(const ryml::NodeRef& node) {
  12103. uint16 level;
  12104. if (!this->asUInt16(node, "Level", level))
  12105. return 0;
  12106. if (level == 0) {
  12107. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  12108. return 0;
  12109. }
  12110. if (level > MAX_LEVEL) {
  12111. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  12112. return 0;
  12113. }
  12114. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12115. bool exists = entry != nullptr;
  12116. if( !exists ){
  12117. if( !this->nodesExist( node, { "Points" } ) ){
  12118. return 0;
  12119. }
  12120. entry = std::make_shared<s_statpoint_entry>();
  12121. entry->level = level;
  12122. }
  12123. if( this->nodeExists( node, "Points" ) ){
  12124. uint32 points;
  12125. if( !this->asUInt32( node, "Points", points ) ){
  12126. return 0;
  12127. }
  12128. entry->statpoints = points;
  12129. }
  12130. if( this->nodeExists( node, "TraitPoints" ) ){
  12131. uint32 traitpoints;
  12132. if( !this->asUInt32( node, "TraitPoints", traitpoints ) ){
  12133. return 0;
  12134. }
  12135. entry->traitpoints = traitpoints;
  12136. }else{
  12137. if( !exists ){
  12138. entry->traitpoints = 0;
  12139. }
  12140. }
  12141. if( !exists ){
  12142. this->put( level, entry );
  12143. }
  12144. return 1;
  12145. }
  12146. /**
  12147. * Generate the remaining parts of the db if necessary.
  12148. */
  12149. void PlayerStatPointDatabase::loadingFinished(){
  12150. const uint16 trait_start_level = 200;
  12151. std::shared_ptr<s_statpoint_entry> level_one = this->find( 1 );
  12152. if( level_one == nullptr ){
  12153. if( battle_config.use_statpoint_table ){
  12154. ShowError( "Missing status points for Level 1\n" );
  12155. }
  12156. level_one = std::make_shared<s_statpoint_entry>();
  12157. level_one->level = 1;
  12158. level_one->statpoints = inter_config.start_status_points;
  12159. level_one->traitpoints = 0;
  12160. this->put( 1, level_one );
  12161. }else if( battle_config.use_statpoint_table ){
  12162. if( level_one->statpoints != inter_config.start_status_points ){
  12163. ShowError( "Status points for Level 1 (=%u) do not match inter_athena.conf value (=%u).\n", level_one->statpoints, inter_config.start_status_points );
  12164. level_one->statpoints = inter_config.start_status_points;
  12165. }
  12166. }else{
  12167. level_one->statpoints = inter_config.start_status_points;
  12168. level_one->traitpoints = 0;
  12169. }
  12170. std::shared_ptr<s_statpoint_entry> last_level = level_one;
  12171. for( uint16 level = 2; level <= MAX_LEVEL; level++ ){
  12172. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12173. bool exists = entry != nullptr;
  12174. if( !exists ){
  12175. entry = std::make_shared<s_statpoint_entry>();
  12176. entry->level = level;
  12177. this->put( level, entry );
  12178. }
  12179. if( !battle_config.use_statpoint_table || !exists ){
  12180. if( battle_config.use_statpoint_table ){
  12181. ShowError("Missing status points for Level %hu\n", level);
  12182. }
  12183. if( level <= trait_start_level ){
  12184. entry->statpoints = last_level->statpoints + ( ( level - 1 + 15 ) / 5 );
  12185. }else{
  12186. entry->statpoints = last_level->statpoints;
  12187. }
  12188. }
  12189. if( !battle_config.use_traitpoint_table || !exists ){
  12190. if( battle_config.use_traitpoint_table && level > trait_start_level ){
  12191. ShowError( "Missing trait points for Level %hu\n", level );
  12192. }
  12193. if( level > trait_start_level ){
  12194. entry->traitpoints = ( level - trait_start_level ) * 3 + ( level - trait_start_level ) / 5 * 4;
  12195. }else{
  12196. entry->traitpoints = 0;
  12197. }
  12198. }
  12199. // Store it for next iteration
  12200. last_level = entry;
  12201. }
  12202. TypesafeCachedYamlDatabase::loadingFinished();
  12203. }
  12204. /*==========================================
  12205. * pc DB reading.
  12206. * job_stats.yml - Job values
  12207. * skill_tree.txt - skill tree for every class
  12208. * attr_fix.yml - elemental adjustment table
  12209. *------------------------------------------*/
  12210. void pc_readdb(void) {
  12211. //reset
  12212. job_db.clear(); // job_info table
  12213. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  12214. penalty_db.load();
  12215. #endif
  12216. statpoint_db.clear();
  12217. job_db.load();
  12218. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  12219. skill_tree_db.reload();
  12220. statpoint_db.load();
  12221. }
  12222. // Read MOTD on startup. [Valaris]
  12223. int pc_read_motd(void)
  12224. {
  12225. FILE* fp;
  12226. // clear old MOTD
  12227. memset(motd_text, 0, sizeof(motd_text));
  12228. // read current MOTD
  12229. if( ( fp = fopen(motd_txt, "r") ) != nullptr )
  12230. {
  12231. unsigned int entries = 0;
  12232. char buf[CHAT_SIZE_MAX];
  12233. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  12234. {
  12235. unsigned int lines = 0;
  12236. size_t len;
  12237. lines++;
  12238. if( buf[0] == '/' && buf[1] == '/' )
  12239. continue;
  12240. len = strlen(buf);
  12241. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  12242. len--;
  12243. if( len ) {
  12244. char * ptr;
  12245. buf[len] = 0;
  12246. if( ( ptr = strstr(buf, " :") ) != nullptr && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  12247. 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);
  12248. }
  12249. else {// empty line
  12250. buf[0] = ' ';
  12251. buf[1] = 0;
  12252. }
  12253. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  12254. entries++;
  12255. }
  12256. fclose(fp);
  12257. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  12258. }
  12259. else
  12260. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  12261. return 0;
  12262. }
  12263. void pc_itemcd_do(map_session_data *sd, bool load) {
  12264. int i,cursor = 0;
  12265. struct item_cd* cd = nullptr;
  12266. if( load ) {
  12267. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  12268. // no item cooldown is associated with this character
  12269. return;
  12270. }
  12271. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12272. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  12273. sd->item_delay[cursor].tick = cd->tick[i];
  12274. sd->item_delay[cursor].nameid = cd->nameid[i];
  12275. cursor++;
  12276. }
  12277. }
  12278. idb_remove(itemcd_db,sd->status.char_id);
  12279. } else {
  12280. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  12281. // create a new skill cooldown object for map storage
  12282. CREATE( cd, struct item_cd, 1 );
  12283. idb_put( itemcd_db, sd->status.char_id, cd );
  12284. }
  12285. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12286. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  12287. cd->tick[cursor] = sd->item_delay[i].tick;
  12288. cd->nameid[cursor] = sd->item_delay[i].nameid;
  12289. cursor++;
  12290. }
  12291. }
  12292. }
  12293. return;
  12294. }
  12295. /**
  12296. * Add item delay to player's item delay data
  12297. * @param sd Player
  12298. * @param id Item data
  12299. * @param tick Current tick
  12300. * @param n Item index in inventory
  12301. * @return 0: No delay, can consume item.
  12302. * 1: Has delay, cancel consumption.
  12303. **/
  12304. uint8 pc_itemcd_add(map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12305. int i;
  12306. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  12307. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  12308. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  12309. if( i < MAX_ITEMDELAYS ) {
  12310. if( sd->item_delay[i].nameid ) {// found
  12311. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  12312. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  12313. char e_msg[CHAT_SIZE_MAX];
  12314. if( e_tick > 99 )
  12315. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  12316. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  12317. else
  12318. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  12319. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  12320. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  12321. return 1; // Delay has not expired yet
  12322. }
  12323. } else {// not yet used item (all slots are initially empty)
  12324. sd->item_delay[i].nameid = id->nameid;
  12325. }
  12326. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  12327. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  12328. } else {// should not happen
  12329. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  12330. }
  12331. //clean up used delays so we can give room for more
  12332. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12333. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  12334. sd->item_delay[i].tick = 0;
  12335. sd->item_delay[i].nameid = 0;
  12336. }
  12337. }
  12338. return 0;
  12339. }
  12340. /**
  12341. * Check if player has delay to reuse item
  12342. * @param sd Player
  12343. * @param id Item data
  12344. * @param tick Current tick
  12345. * @param n Item index in inventory
  12346. * @return 0: No delay, can consume item.
  12347. * 1: Has delay, cancel consumption.
  12348. **/
  12349. uint8 pc_itemcd_check(map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12350. status_change *sc = nullptr;
  12351. nullpo_retr(0, sd);
  12352. nullpo_retr(0, id);
  12353. // Do normal delay assignment
  12354. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  12355. return pc_itemcd_add(sd, id, tick, n);
  12356. // Send reply of delay remains
  12357. if (sc->getSCE(id->delay.sc)) {
  12358. const struct TimerData *timer = get_timer(sc->getSCE(id->delay.sc)->timer);
  12359. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  12360. return 1;
  12361. }
  12362. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  12363. return 0;
  12364. }
  12365. /**
  12366. * Clear the dmglog data from player
  12367. * @param sd
  12368. * @param md
  12369. **/
  12370. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  12371. {
  12372. uint8 i;
  12373. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  12374. if (i < DAMAGELOG_SIZE) {
  12375. md->dmglog[i].id = 0;
  12376. md->dmglog[i].dmg = 0;
  12377. md->dmglog[i].flag = 0;
  12378. }
  12379. }
  12380. /**
  12381. * Add log to player's dmglog
  12382. * @param sd
  12383. * @param id Monster's GID
  12384. **/
  12385. void pc_damage_log_add(map_session_data *sd, int id)
  12386. {
  12387. uint8 i = 0;
  12388. if (!sd || !id)
  12389. return;
  12390. //Only store new data, don't need to renew the old one with same id
  12391. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  12392. if (i < DAMAGELOG_SIZE_PC)
  12393. return;
  12394. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12395. if (sd->dmglog[i] == 0) {
  12396. sd->dmglog[i] = id;
  12397. return;
  12398. }
  12399. }
  12400. }
  12401. /**
  12402. * Clear dmglog data from player
  12403. * @param sd
  12404. * @param id Monster's id
  12405. **/
  12406. void pc_damage_log_clear(map_session_data *sd, int id)
  12407. {
  12408. uint8 i;
  12409. struct mob_data *md = nullptr;
  12410. if (!sd)
  12411. return;
  12412. if (!id) {
  12413. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12414. if( !sd->dmglog[i] ) //skip the empty value
  12415. continue;
  12416. if ((md = map_id2md(sd->dmglog[i])))
  12417. pc_clear_log_damage_sub(sd->status.char_id,md);
  12418. sd->dmglog[i] = 0;
  12419. }
  12420. }
  12421. else {
  12422. if ((md = map_id2md(id)))
  12423. pc_clear_log_damage_sub(sd->status.char_id,md);
  12424. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  12425. if (i < DAMAGELOG_SIZE_PC)
  12426. sd->dmglog[i] = 0;
  12427. }
  12428. }
  12429. /**
  12430. * Status change data arrived from char-server
  12431. * @param sd: Player data
  12432. */
  12433. void pc_scdata_received(map_session_data *sd) {
  12434. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  12435. clif_weight_limit( sd );
  12436. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) && pc_attendance_counter(sd) < 200 ){
  12437. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  12438. }
  12439. sd->state.pc_loaded = true;
  12440. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  12441. sd->state.connect_new = 1;
  12442. clif_parse_LoadEndAck(sd->fd, sd);
  12443. }
  12444. if (pc_iscarton(sd)) {
  12445. sd->cart_weight_max = 0; // Force a client refesh
  12446. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  12447. }
  12448. if (sd->sc.getSCE(SC_SOULENERGY))
  12449. sd->soulball = sd->sc.getSCE(SC_SOULENERGY)->val1;
  12450. }
  12451. /**
  12452. * Check player account expiration time and rental item expirations
  12453. * @param sd: Player data
  12454. */
  12455. void pc_check_expiration(map_session_data *sd) {
  12456. #ifndef ENABLE_SC_SAVING
  12457. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  12458. #endif
  12459. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  12460. time_t exp_time = sd->expiration_time;
  12461. char tmpstr[1024];
  12462. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  12463. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  12464. pc_expire_check(sd);
  12465. }
  12466. }
  12467. TIMER_FUNC(pc_expiration_timer){
  12468. map_session_data *sd = map_id2sd(id);
  12469. if( !sd ) return 0;
  12470. sd->expiration_tid = INVALID_TIMER;
  12471. if( sd->fd )
  12472. clif_authfail_fd(sd->fd,10);
  12473. map_quit(sd);
  12474. return 0;
  12475. }
  12476. TIMER_FUNC(pc_autotrade_timer){
  12477. map_session_data *sd = map_id2sd(id);
  12478. if (!sd)
  12479. return 0;
  12480. sd->autotrade_tid = INVALID_TIMER;
  12481. if (sd->state.autotrade&2)
  12482. vending_reopen(*sd);
  12483. if (sd->state.autotrade&4)
  12484. buyingstore_reopen(sd);
  12485. if (!sd->vender_id && !sd->buyer_id) {
  12486. sd->state.autotrade = 0;
  12487. map_quit(sd);
  12488. }
  12489. return 0;
  12490. }
  12491. /* this timer exists only when a character with a expire timer > 24h is online */
  12492. /* it loops thru online players once an hour to check whether a new < 24h is available */
  12493. TIMER_FUNC(pc_global_expiration_timer){
  12494. struct s_mapiterator* iter;
  12495. map_session_data* sd;
  12496. iter = mapit_getallusers();
  12497. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  12498. if( sd->expiration_time )
  12499. pc_expire_check(sd);
  12500. mapit_free(iter);
  12501. return 0;
  12502. }
  12503. void pc_expire_check(map_session_data *sd) {
  12504. /* ongoing timer */
  12505. if( sd->expiration_tid != INVALID_TIMER )
  12506. return;
  12507. /* not within the next 24h, enable the global check */
  12508. if( sd->expiration_time > (time(nullptr) + ((60 * 60) * 24)) ) {
  12509. /* global check not running, enable */
  12510. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  12511. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  12512. return;
  12513. }
  12514. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(nullptr)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  12515. }
  12516. /**
  12517. * Deposit some money to bank
  12518. * @param sd
  12519. * @param money Amount of money to deposit
  12520. **/
  12521. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(map_session_data *sd, int money) {
  12522. if (battle_config.feature_banking_state_enforce && !sd->state.banking) {
  12523. return BDA_ERROR;
  12524. }
  12525. unsigned int limit_check = money + sd->bank_vault;
  12526. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  12527. return BDA_OVERFLOW;
  12528. } else if ( money > sd->status.zeny ) {
  12529. return BDA_NO_MONEY;
  12530. }
  12531. if( pc_payzeny(sd, money, LOG_TYPE_BANK) )
  12532. return BDA_NO_MONEY;
  12533. sd->bank_vault += money;
  12534. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12535. if( save_settings&CHARSAVE_BANK )
  12536. chrif_save(sd, CSAVE_NORMAL);
  12537. return BDA_SUCCESS;
  12538. }
  12539. /**
  12540. * Withdraw money from bank
  12541. * @param sd
  12542. * @param money Amount of money that will be withdrawn
  12543. **/
  12544. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(map_session_data *sd, int money) {
  12545. if (battle_config.feature_banking_state_enforce && !sd->state.banking) {
  12546. return BWA_UNKNOWN_ERROR;
  12547. }
  12548. unsigned int limit_check = money + sd->status.zeny;
  12549. if( money <= 0 ) {
  12550. return BWA_UNKNOWN_ERROR;
  12551. } else if ( money > sd->bank_vault ) {
  12552. return BWA_NO_MONEY;
  12553. } else if ( limit_check > MAX_ZENY ) {
  12554. /* no official response for this scenario exists. */
  12555. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  12556. return BWA_UNKNOWN_ERROR;
  12557. }
  12558. if( pc_getzeny(sd,money, LOG_TYPE_BANK) )
  12559. return BWA_NO_MONEY;
  12560. sd->bank_vault -= money;
  12561. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12562. if( save_settings&CHARSAVE_BANK )
  12563. chrif_save(sd, CSAVE_NORMAL);
  12564. return BWA_SUCCESS;
  12565. }
  12566. /**
  12567. * Clear Crimson Marker data from caster
  12568. * @param sd: Player
  12569. **/
  12570. void pc_crimson_marker_clear(map_session_data *sd) {
  12571. uint8 i;
  12572. if (!sd)
  12573. return;
  12574. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  12575. struct block_list *bl = nullptr;
  12576. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  12577. status_change_end(bl,SC_C_MARKER);
  12578. sd->c_marker[i] = 0;
  12579. }
  12580. }
  12581. /**
  12582. * Show version to player
  12583. * @param sd: Player
  12584. **/
  12585. void pc_show_version(map_session_data *sd) {
  12586. const char* svn = get_svn_revision();
  12587. char buf[CHAT_SIZE_MAX];
  12588. if( svn[0] != UNKNOWN_VERSION )
  12589. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  12590. else {
  12591. const char* git = get_git_hash();
  12592. if( git[0] != UNKNOWN_VERSION )
  12593. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  12594. else
  12595. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  12596. }
  12597. clif_displaymessage(sd->fd,buf);
  12598. }
  12599. /**
  12600. * Run bonus_script on player
  12601. * @param sd
  12602. * @author [Cydh]
  12603. **/
  12604. void pc_bonus_script(map_session_data *sd) {
  12605. t_tick now = gettick();
  12606. struct linkdb_node *node = nullptr, *next = nullptr;
  12607. if (!sd || !(node = sd->bonus_script.head))
  12608. return;
  12609. while (node) {
  12610. struct s_bonus_script_entry *entry = nullptr;
  12611. next = node->next;
  12612. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  12613. // Only start timer for new bonus_script
  12614. if (entry->tid == INVALID_TIMER) {
  12615. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  12616. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  12617. entry->tick += now;
  12618. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  12619. }
  12620. if (entry->script)
  12621. run_script(entry->script, 0, sd->bl.id, 0);
  12622. else
  12623. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  12624. }
  12625. node = next;
  12626. }
  12627. }
  12628. /**
  12629. * Add bonus_script to player
  12630. * @param sd Player
  12631. * @param script_str Script string
  12632. * @param dur Duration in ms
  12633. * @param icon EFST
  12634. * @param flag Flags @see enum e_bonus_script_flags
  12635. * @param type 0 - None, 1 - Buff, 2 - Debuff
  12636. * @return New created entry pointer or nullptr if failed or nullptr if duplicate fail
  12637. * @author [Cydh]
  12638. **/
  12639. 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) {
  12640. struct script_code *script = nullptr;
  12641. struct linkdb_node *node = nullptr;
  12642. struct s_bonus_script_entry *entry = nullptr;
  12643. if (!sd)
  12644. return nullptr;
  12645. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  12646. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  12647. return nullptr;
  12648. }
  12649. // Duplication checks
  12650. if ((node = sd->bonus_script.head)) {
  12651. while (node) {
  12652. entry = (struct s_bonus_script_entry *)node->data;
  12653. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  12654. t_tick newdur = gettick() + dur;
  12655. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  12656. settick_timer(entry->tid, newdur);
  12657. script_free_code(script);
  12658. return nullptr;
  12659. }
  12660. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  12661. break;
  12662. else { // No duplicate bonus
  12663. script_free_code(script);
  12664. return nullptr;
  12665. }
  12666. }
  12667. node = node->next;
  12668. }
  12669. }
  12670. CREATE(entry, struct s_bonus_script_entry, 1);
  12671. entry->script_buf = StringBuf_Malloc();
  12672. StringBuf_AppendStr(entry->script_buf, script_str);
  12673. entry->tid = INVALID_TIMER;
  12674. entry->flag = flag;
  12675. entry->icon = icon;
  12676. entry->tick = dur; // Use duration first, on run change to expire time
  12677. entry->type = type;
  12678. entry->script = script;
  12679. sd->bonus_script.count++;
  12680. return entry;
  12681. }
  12682. /**
  12683. * Remove bonus_script data from player
  12684. * @param sd: Target player
  12685. * @param list: Bonus script entry from player
  12686. * @author [Cydh]
  12687. **/
  12688. void pc_bonus_script_free_entry(map_session_data *sd, struct s_bonus_script_entry *entry) {
  12689. if (entry->tid != INVALID_TIMER)
  12690. delete_timer(entry->tid, pc_bonus_script_timer);
  12691. if (entry->script)
  12692. script_free_code(entry->script);
  12693. if (entry->script_buf)
  12694. StringBuf_Free(entry->script_buf);
  12695. if (sd) {
  12696. if (entry->icon != EFST_BLANK)
  12697. clif_status_load(&sd->bl, entry->icon, 0);
  12698. if (sd->bonus_script.count > 0)
  12699. sd->bonus_script.count--;
  12700. }
  12701. aFree(entry);
  12702. }
  12703. /**
  12704. * Do final process if no entry left
  12705. * @param sd
  12706. **/
  12707. static void inline pc_bonus_script_check_final(map_session_data *sd) {
  12708. if (sd->bonus_script.count == 0) {
  12709. if (sd->bonus_script.head && sd->bonus_script.head->data)
  12710. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  12711. linkdb_final(&sd->bonus_script.head);
  12712. }
  12713. }
  12714. /**
  12715. * Timer for bonus_script
  12716. * @param tid
  12717. * @param tick
  12718. * @param id
  12719. * @param data
  12720. * @author [Cydh]
  12721. **/
  12722. TIMER_FUNC(pc_bonus_script_timer){
  12723. map_session_data *sd;
  12724. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  12725. sd = map_id2sd(id);
  12726. if (!sd) {
  12727. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  12728. return 0;
  12729. }
  12730. if (tid == INVALID_TIMER)
  12731. return 0;
  12732. if (!sd->bonus_script.head || entry == nullptr) {
  12733. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  12734. return 0;
  12735. }
  12736. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12737. pc_bonus_script_free_entry(sd, entry);
  12738. pc_bonus_script_check_final(sd);
  12739. status_calc_pc(sd,SCO_NONE);
  12740. return 0;
  12741. }
  12742. /**
  12743. * Check then clear all active timer(s) of bonus_script data from player based on reason
  12744. * @param sd: Target player
  12745. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  12746. * @author [Cydh]
  12747. **/
  12748. void pc_bonus_script_clear(map_session_data *sd, uint32 flag) {
  12749. struct linkdb_node *node = nullptr;
  12750. uint16 count = 0;
  12751. if (!sd || !(node = sd->bonus_script.head))
  12752. return;
  12753. while (node) {
  12754. struct linkdb_node *next = node->next;
  12755. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  12756. if (entry && (
  12757. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  12758. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  12759. (flag&entry->flag) || // Matched flag
  12760. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  12761. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  12762. )
  12763. )
  12764. {
  12765. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12766. pc_bonus_script_free_entry(sd, entry);
  12767. count++;
  12768. }
  12769. node = next;
  12770. }
  12771. pc_bonus_script_check_final(sd);
  12772. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  12773. status_calc_pc(sd,SCO_NONE);
  12774. }
  12775. /** [Cydh]
  12776. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  12777. * @param sd: Target player
  12778. */
  12779. void pc_cell_basilica(map_session_data *sd) {
  12780. nullpo_retv(sd);
  12781. #ifdef RENEWAL
  12782. enum sc_type type = SC_BASILICA_CELL;
  12783. #else
  12784. enum sc_type type = SC_BASILICA;
  12785. #endif
  12786. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  12787. if (sd->sc.getSCE(type))
  12788. status_change_end(&sd->bl, type);
  12789. }
  12790. else if (!sd->sc.getSCE(type))
  12791. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  12792. }
  12793. /** [Cydh]
  12794. * Get maximum specified parameter for specified class
  12795. * @param sd: Player data
  12796. * @param param: Max parameter to check
  12797. * @return max_param
  12798. */
  12799. uint16 pc_maxparameter(map_session_data *sd, e_params param) {
  12800. nullpo_retr(0, sd);
  12801. std::shared_ptr<s_job_info> job = job_db.find(pc_mapid2jobid(sd->class_,sd->status.sex));
  12802. if( job == nullptr || param == PARAM_MAX ){
  12803. return 0;
  12804. }
  12805. return job->max_param[param];
  12806. }
  12807. /**
  12808. * Get max ASPD for player based on Class
  12809. * @param sd Player
  12810. * @return ASPD
  12811. */
  12812. short pc_maxaspd(map_session_data *sd) {
  12813. nullpo_ret(sd);
  12814. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  12815. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  12816. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  12817. battle_config.max_aspd ));
  12818. }
  12819. /**
  12820. * Calculates total item-group related bonuses for the given item
  12821. * @param sd Player
  12822. * @param nameid Item ID
  12823. * @return Heal rate
  12824. **/
  12825. short pc_get_itemgroup_bonus(map_session_data* sd, t_itemid nameid, std::vector<s_item_bonus>& bonuses) {
  12826. if (bonuses.empty())
  12827. return 0;
  12828. short bonus = 0;
  12829. for (const auto &it : bonuses) {
  12830. uint16 group_id = it.id;
  12831. if (group_id == 0)
  12832. continue;
  12833. if (itemdb_group.item_exists(group_id, nameid))
  12834. bonus += it.val;
  12835. }
  12836. return bonus;
  12837. }
  12838. /**
  12839. * Calculates total item-group related bonuses for the given item group
  12840. * @param sd Player
  12841. * @param group_id Item Group ID
  12842. * @return Heal rate
  12843. **/
  12844. short pc_get_itemgroup_bonus_group(map_session_data* sd, uint16 group_id, std::vector<s_item_bonus>& bonuses) {
  12845. if (bonuses.empty())
  12846. return 0;
  12847. for (const auto &it : bonuses) {
  12848. if (it.id == group_id)
  12849. return it.val;
  12850. }
  12851. return 0;
  12852. }
  12853. /**
  12854. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  12855. * @param eqi Item EQI of enum equip_index
  12856. * @param *equip_index Player's equip_index[]
  12857. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  12858. * @return True if item in same inventory index, False if doesn't
  12859. */
  12860. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  12861. if (index < 0 || index >= MAX_INVENTORY)
  12862. return true;
  12863. // Dual weapon checks
  12864. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  12865. return true;
  12866. // Headgear with Mid & Low location
  12867. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  12868. return true;
  12869. // Headgear with Top & Mid or Low location
  12870. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  12871. return true;
  12872. // Headgear with Mid & Low location
  12873. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  12874. return true;
  12875. // Headgear with Top & Mid or Low location
  12876. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  12877. return true;
  12878. return false;
  12879. }
  12880. /**
  12881. * Generate Unique item ID for player
  12882. * @param sd : Player
  12883. * @return A generated Unique item ID
  12884. */
  12885. uint64 pc_generate_unique_id(map_session_data *sd) {
  12886. nullpo_ret(sd);
  12887. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  12888. }
  12889. /**
  12890. * Validating skill from player after logged on
  12891. * @param sd
  12892. **/
  12893. void pc_validate_skill(map_session_data *sd) {
  12894. if (sd) {
  12895. uint16 i = 0, count = 0;
  12896. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  12897. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  12898. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  12899. for (i = 0; i < MAX_SKILL; i++) {
  12900. uint16 idx = 0;
  12901. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  12902. continue;
  12903. if ((idx = skill_get_index(tmp_skills[i].id))) {
  12904. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  12905. count++;
  12906. }
  12907. else
  12908. 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);
  12909. }
  12910. }
  12911. }
  12912. /**
  12913. * Show available NPC Quest / Event Icon Check [Kisuka]
  12914. * @param sd Player
  12915. **/
  12916. void pc_show_questinfo(map_session_data *sd) {
  12917. #if PACKETVER >= 20090218
  12918. nullpo_retv(sd);
  12919. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12920. return;
  12921. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12922. nullpo_retv(mapdata);
  12923. if (mapdata->qi_npc.empty())
  12924. return;
  12925. if (mapdata->qi_npc.size() != sd->qi_display.size())
  12926. return; // init was not called yet
  12927. for (int i = 0; i < mapdata->qi_npc.size(); i++) {
  12928. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  12929. if (!nd || nd->qi_data.empty())
  12930. continue;
  12931. bool show = false;
  12932. for (auto &qi : nd->qi_data) {
  12933. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  12934. show = true;
  12935. // Check if need to be displayed
  12936. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  12937. sd->qi_display[i].is_active = true;
  12938. sd->qi_display[i].icon = qi->icon;
  12939. sd->qi_display[i].color = qi->color;
  12940. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  12941. }
  12942. break;
  12943. }
  12944. }
  12945. if (show == false) {
  12946. // Check if need to be hide
  12947. if (sd->qi_display[i].is_active) {
  12948. sd->qi_display[i].is_active = false;
  12949. sd->qi_display[i].icon = QTYPE_NONE;
  12950. sd->qi_display[i].color = QMARK_NONE;
  12951. #if PACKETVER >= 20120410
  12952. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  12953. #else
  12954. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  12955. #endif
  12956. }
  12957. }
  12958. }
  12959. #endif
  12960. }
  12961. /**
  12962. * Reinit the questinfo for player when changing map
  12963. * @param sd Player
  12964. **/
  12965. void pc_show_questinfo_reinit(map_session_data *sd) {
  12966. #if PACKETVER >= 20090218
  12967. nullpo_retv(sd);
  12968. sd->qi_display.clear();
  12969. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12970. return;
  12971. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12972. nullpo_retv(mapdata);
  12973. if (mapdata->qi_npc.empty())
  12974. return;
  12975. sd->qi_display.reserve( mapdata->qi_npc.size() );
  12976. for( int i = 0; i < mapdata->qi_npc.size(); i++ ){
  12977. sd->qi_display.push_back( s_qi_display() );
  12978. }
  12979. #endif
  12980. }
  12981. /**
  12982. * Check if a job is allowed to enter the map.
  12983. * @param jobid: Job ID see enum e_job or sd->status.class_
  12984. * @param m: ID -an index- for direct indexing map[] array
  12985. * @param group_lv: Group level of the player
  12986. * @return True if job is allowed, false otherwise
  12987. **/
  12988. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  12989. if (!pcdb_checkid(jobid))
  12990. return false;
  12991. map_data *mapdata = map_getmapdata(m);
  12992. if (mapdata != nullptr && mapdata->zone->isJobRestricted(jobid, group_lv))
  12993. return false;
  12994. return true;
  12995. }
  12996. /**
  12997. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  12998. * @param sd
  12999. **/
  13000. void pc_set_costume_view(map_session_data *sd) {
  13001. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  13002. struct item_data *id = nullptr;
  13003. nullpo_retv(sd);
  13004. head_low = sd->status.head_bottom;
  13005. head_mid = sd->status.head_mid;
  13006. head_top = sd->status.head_top;
  13007. robe = sd->status.robe;
  13008. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  13009. //Added check to prevent sending the same look on multiple slots ->
  13010. //causes client to redraw item on top of itself. (suggested by Lupus)
  13011. // Normal headgear checks
  13012. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  13013. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  13014. sd->status.head_bottom = id->look;
  13015. else
  13016. sd->status.head_bottom = 0;
  13017. }
  13018. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  13019. if (!(id->equip&(EQP_HEAD_TOP)))
  13020. sd->status.head_mid = id->look;
  13021. else
  13022. sd->status.head_mid = 0;
  13023. }
  13024. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  13025. sd->status.head_top = id->look;
  13026. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  13027. sd->status.robe = id->look;
  13028. // Costumes check
  13029. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  13030. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  13031. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  13032. sd->status.head_bottom = id->look;
  13033. else
  13034. sd->status.head_bottom = 0;
  13035. }
  13036. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  13037. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  13038. sd->status.head_mid = id->look;
  13039. else
  13040. sd->status.head_mid = 0;
  13041. }
  13042. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  13043. sd->status.head_top = id->look;
  13044. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  13045. sd->status.robe = id->look;
  13046. }
  13047. if (sd->setlook_head_bottom)
  13048. sd->status.head_bottom = sd->setlook_head_bottom;
  13049. if (sd->setlook_head_mid)
  13050. sd->status.head_mid = sd->setlook_head_mid;
  13051. if (sd->setlook_head_top)
  13052. sd->status.head_top = sd->setlook_head_top;
  13053. if (sd->setlook_robe)
  13054. sd->status.robe = sd->setlook_robe;
  13055. if (head_low != sd->status.head_bottom)
  13056. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  13057. if (head_mid != sd->status.head_mid)
  13058. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  13059. if (head_top != sd->status.head_top)
  13060. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  13061. if (robe != sd->status.robe)
  13062. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  13063. }
  13064. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  13065. uint32 date = date_get(DT_YYYYMMDD);
  13066. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  13067. std::shared_ptr<s_attendance_period> period = pair.second;
  13068. if( period->start <= date && period->end >= date ){
  13069. return period;
  13070. }
  13071. }
  13072. return nullptr;
  13073. }
  13074. bool pc_attendance_enabled(){
  13075. // Check if the attendance feature is disabled
  13076. if( !battle_config.feature_attendance ){
  13077. return false;
  13078. }
  13079. // Check if there is a running attendance period
  13080. return pc_attendance_period() != nullptr;
  13081. }
  13082. static inline bool pc_attendance_rewarded_today( map_session_data* sd ){
  13083. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  13084. }
  13085. int32 pc_attendance_counter( map_session_data* sd ){
  13086. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13087. // No running attendance period
  13088. if( period == nullptr ){
  13089. return 0;
  13090. }
  13091. // Get the counter for the current period
  13092. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13093. // Check if we have a remaining counter from a previous period
  13094. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  13095. // Reset the counter to zero
  13096. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  13097. return 0;
  13098. }
  13099. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  13100. }
  13101. void pc_attendance_claim_reward( map_session_data* sd ){
  13102. // If the user's group does not have the permission
  13103. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  13104. return;
  13105. }
  13106. // Check if the attendance feature is disabled
  13107. if( !pc_attendance_enabled() ){
  13108. return;
  13109. }
  13110. // Check if the user already got his reward today
  13111. if( pc_attendance_rewarded_today( sd ) ){
  13112. return;
  13113. }
  13114. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13115. attendance_counter += 1;
  13116. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13117. if( period == nullptr ){
  13118. return;
  13119. }
  13120. if( period->rewards.size() < attendance_counter ){
  13121. return;
  13122. }
  13123. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  13124. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  13125. if( save_settings&CHARSAVE_ATTENDANCE )
  13126. chrif_save(sd, CSAVE_NORMAL);
  13127. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  13128. struct mail_message msg;
  13129. memset( &msg, 0, sizeof( struct mail_message ) );
  13130. msg.dest_id = sd->status.char_id;
  13131. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  13132. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  13133. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  13134. msg.item[0].nameid = reward->item_id;
  13135. msg.item[0].amount = reward->amount;
  13136. msg.item[0].identify = 1;
  13137. msg.status = MAIL_NEW;
  13138. msg.type = MAIL_INBOX_NORMAL;
  13139. msg.timestamp = time(nullptr);
  13140. intif_Mail_send(0, &msg);
  13141. clif_attendence_response( sd, attendance_counter );
  13142. }
  13143. /**
  13144. * Send a player to jail and determine the location to send in jail.
  13145. * @param sd: Player data
  13146. * @param duration: Duration in minutes (default INT_MAX = infinite)
  13147. */
  13148. void pc_jail(map_session_data &sd, int32 duration) {
  13149. uint16 m_index = mapindex_name2id(MAP_JAIL);
  13150. int16 x, y;
  13151. switch (rnd() % 2) { // Jail Locations
  13152. case 0: // Jail #1
  13153. x = 49;
  13154. y = 75;
  13155. break;
  13156. default: // Default Jail
  13157. x = 24;
  13158. y = 75;
  13159. break;
  13160. }
  13161. duration = i32max(0, duration); // Can't be less than 0 seconds.
  13162. // If duration > 0 then triggered via jailfor which checks every minute.
  13163. // If duration == INT_MAX then triggered via jail for infinite duration.
  13164. // If duration == 0 then triggered via unjail and end status.
  13165. if (duration > 0)
  13166. sc_start4(nullptr, &sd.bl, SC_JAILED, 100, duration, m_index, x, y, 60000);
  13167. else
  13168. status_change_end(&sd.bl, SC_JAILED);
  13169. }
  13170. /**
  13171. * Determine the punishment type when failing macro checks.
  13172. * @param sd: Player data
  13173. * @param stype: Macro detection status type (for banning)
  13174. */
  13175. static void pc_macro_punishment(map_session_data &sd, e_macro_detect_status stype) {
  13176. int32 duration = 0;
  13177. // Determine if there's a unique duration
  13178. if (battle_config.macro_detection_punishment_time > 0) {
  13179. char time[13];
  13180. safesnprintf(time, 13, "+%dnm", battle_config.macro_detection_punishment_time);
  13181. duration = static_cast<int32>(solve_time(time));
  13182. }
  13183. // Delete the timer
  13184. if (sd.macro_detect.timer != INVALID_TIMER)
  13185. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13186. // Clear the macro detect data
  13187. sd.macro_detect = {};
  13188. sd.macro_detect.timer = INVALID_TIMER;
  13189. // Unblock all actions for the player
  13190. sd.state.block_action &= ~PCBLOCK_ALL;
  13191. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13192. if (battle_config.macro_detection_punishment == 0) { // Ban
  13193. clif_macro_detector_status(sd, stype);
  13194. 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);
  13195. } else { // Jail
  13196. // Send success to close the window without closing the client
  13197. clif_macro_detector_status(sd, MCD_GOOD);
  13198. pc_jail(sd, (duration == 0 ? INT_MAX : duration / 60));
  13199. }
  13200. }
  13201. /**
  13202. * Save a captcha image to memory via /macro_register.
  13203. * @param sd: Player data
  13204. * @param image_size: Captcha image size
  13205. * @param captcha_answer: Answer to captcha
  13206. */
  13207. void pc_macro_captcha_register(map_session_data &sd, uint16 image_size, char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13208. nullpo_retv(captcha_answer);
  13209. sd.captcha_upload.cd = nullptr;
  13210. sd.captcha_upload.upload_size = 0;
  13211. if (strlen(captcha_answer) < 4 || image_size == 0 || image_size > CAPTCHA_BMP_SIZE) {
  13212. clif_captcha_upload_request(sd); // Notify client of failure.
  13213. return;
  13214. }
  13215. std::shared_ptr<s_captcha_data> cd = std::make_shared<s_captcha_data>();
  13216. sd.captcha_upload.cd = cd;
  13217. cd->image_size = image_size;
  13218. safestrncpy(cd->captcha_answer, captcha_answer, sizeof(cd->captcha_answer));
  13219. memset(cd->image_data, 0, sizeof(cd->image_data));
  13220. // Request the image data from the client.
  13221. clif_captcha_upload_request(sd);
  13222. }
  13223. /**
  13224. * Save captcha image to server.
  13225. * @param sd: Player data
  13226. * @param captcha_key: Captcha ID
  13227. * @param upload_size: Captcha size
  13228. * @param upload_data: Image data
  13229. */
  13230. void pc_macro_captcha_register_upload(map_session_data &sd, uint16 upload_size, char *upload_data) {
  13231. nullpo_retv(upload_data);
  13232. memcpy(&sd.captcha_upload.cd->image_data[sd.captcha_upload.upload_size], upload_data, upload_size);
  13233. sd.captcha_upload.upload_size += upload_size;
  13234. // Notify that the image finished uploading.
  13235. if (sd.captcha_upload.upload_size == sd.captcha_upload.cd->image_size) {
  13236. // Tell the client that the upload was finished
  13237. clif_captcha_upload_end(sd);
  13238. // Look for a free key
  13239. uint16 index;
  13240. for (index = 0; index < UINT16_MAX; index++) {
  13241. if (!captcha_db.exists(index)) {
  13242. break;
  13243. }
  13244. }
  13245. if (index == UINT16_MAX) {
  13246. // no free key found...
  13247. sd.captcha_upload.cd = nullptr;
  13248. sd.captcha_upload.upload_size = 0;
  13249. return;
  13250. }
  13251. captcha_db.put(index, sd.captcha_upload.cd);
  13252. sd.captcha_upload.cd = nullptr;
  13253. sd.captcha_upload.upload_size = 0;
  13254. // TODO: write YAML and BMP file?
  13255. }
  13256. }
  13257. /**
  13258. * Timer attached to target player with attempts to confirm captcha.
  13259. */
  13260. TIMER_FUNC(pc_macro_detector_timeout) {
  13261. map_session_data *sd = map_id2sd(id);
  13262. nullpo_ret(sd);
  13263. // Remove the current timer
  13264. sd->macro_detect.timer = INVALID_TIMER;
  13265. // Deduct an answering attempt
  13266. sd->macro_detect.retry -= 1;
  13267. if (sd->macro_detect.retry == 0) {
  13268. // All attempts have been exhausted, punish the user
  13269. pc_macro_punishment(*sd, MCD_TIMEOUT);
  13270. } else {
  13271. // Update the client
  13272. clif_macro_detector_request_show(*sd);
  13273. // Start a new timer
  13274. sd->macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd->bl.id, 0);
  13275. }
  13276. return 0;
  13277. }
  13278. /**
  13279. * Check player's captcha answer.
  13280. * @param sd: Player data
  13281. * @param captcha_answer: Captcha answer entered by player
  13282. */
  13283. void pc_macro_detector_process_answer(map_session_data &sd, char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13284. nullpo_retv(captcha_answer);
  13285. const std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  13286. // Has no captcha request
  13287. if (cd == nullptr) {
  13288. return;
  13289. }
  13290. // Correct answer
  13291. if (strcmp(captcha_answer, cd->captcha_answer) == 0) {
  13292. // Delete the timer
  13293. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13294. // Clear the macro detect data
  13295. sd.macro_detect = {};
  13296. sd.macro_detect.timer = INVALID_TIMER;
  13297. // Unblock all actions for the player
  13298. sd.state.block_action &= ~PCBLOCK_ALL;
  13299. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13300. // Assign temporary macro variable to check failures
  13301. pc_setreg(&sd, add_str("@captcha_retries"), battle_config.macro_detection_retry - sd.macro_detect.retry);
  13302. // Grant bonuses via script
  13303. run_script(cd->bonus_script, 0, sd.bl.id, fake_nd->bl.id);
  13304. // Notify the client
  13305. clif_macro_detector_status(sd, MCD_GOOD);
  13306. } else {
  13307. // Deduct an answering attempt
  13308. sd.macro_detect.retry -= 1;
  13309. // All attempts have been exhausted, punish the user
  13310. if (sd.macro_detect.retry <= 0) {
  13311. pc_macro_punishment(sd, MCD_INCORRECT);
  13312. return;
  13313. }
  13314. // Incorrect response, update the client
  13315. clif_macro_detector_request_show(sd);
  13316. // Reset the timer
  13317. addtick_timer(sd.macro_detect.timer, gettick() + battle_config.macro_detection_timeout);
  13318. }
  13319. }
  13320. /**
  13321. * Determine if a player tries to log out during a captcha check.
  13322. * @param sd: Player data
  13323. */
  13324. void pc_macro_detector_disconnect(map_session_data &sd) {
  13325. // Delete the timeout timer
  13326. if (sd.macro_detect.timer != INVALID_TIMER) {
  13327. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13328. sd.macro_detect.timer = INVALID_TIMER;
  13329. }
  13330. // If the player disconnects before clearing the challenge the player is punished.
  13331. if (sd.macro_detect.retry != 0)
  13332. pc_macro_punishment(sd, MCD_TIMEOUT);
  13333. }
  13334. /**
  13335. * Save a list of players from an area select via /macro_detector.
  13336. */
  13337. int pc_macro_reporter_area_select_sub(block_list *bl, va_list ap) {
  13338. nullpo_retr(0, bl);
  13339. if (bl->type != BL_PC)
  13340. return 0;
  13341. std::vector<uint32> *aid_list = va_arg(ap, std::vector<uint32> *);
  13342. nullpo_ret(aid_list);
  13343. aid_list->push_back(bl->id);
  13344. return 0;
  13345. }
  13346. /**
  13347. * Area select via /macro_detector.
  13348. * @param sd: Player data
  13349. * @param x: X location
  13350. * @param y: Y location
  13351. * @param radius: Area
  13352. */
  13353. void pc_macro_reporter_area_select(map_session_data &sd, const int16 x, const int16 y, const int8 radius) {
  13354. std::vector<uint32> aid_list;
  13355. map_foreachinarea(pc_macro_reporter_area_select_sub, sd.bl.m, x - radius, y - radius, x + radius, y + radius, BL_PC, &aid_list);
  13356. clif_macro_reporter_select(sd, aid_list);
  13357. }
  13358. /**
  13359. * Send out captcha check to player.
  13360. * @param sd: Target player data
  13361. * @param reporter_account_id: Account ID of reporter
  13362. */
  13363. void pc_macro_reporter_process(map_session_data &sd, int32 reporter_account_id) {
  13364. if (captcha_db.empty())
  13365. return;
  13366. // Pick a random image from the database.
  13367. const std::shared_ptr<s_captcha_data> cd = captcha_db.random();
  13368. // Set macro detection data.
  13369. sd.macro_detect.cd = cd;
  13370. sd.macro_detect.reporter_aid = reporter_account_id;
  13371. sd.macro_detect.retry = battle_config.macro_detection_retry;
  13372. // Block all actions for the target player.
  13373. sd.state.block_action |= (PCBLOCK_ALL | PCBLOCK_IMMUNE);
  13374. // Open macro detect client side.
  13375. clif_macro_detector_request(sd);
  13376. // Start the timeout timer.
  13377. sd.macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd.bl.id, 0);
  13378. }
  13379. /**
  13380. * Parse a BMP image to memory.
  13381. * @param filepath: Image file location
  13382. * @param cd: Captcha data
  13383. */
  13384. bool pc_macro_read_captcha_db_loadbmp(const std::string &filepath, std::shared_ptr<s_captcha_data> cd) {
  13385. if (cd == nullptr)
  13386. return false;
  13387. FILE *fp = fopen(filepath.c_str(), "rb");
  13388. if (fp == nullptr) {
  13389. ShowError("%s: Failed to open file \"%s\"\n", __func__, filepath.c_str());
  13390. return false;
  13391. }
  13392. // Load the file data and verify magic
  13393. char bmp_data[CAPTCHA_BMP_SIZE];
  13394. if (fread(bmp_data, CAPTCHA_BMP_SIZE, 1, fp) != 1) {
  13395. ShowError("%s: Failed to read data from \"%s\"\n", __func__, filepath.c_str());
  13396. fclose(fp);
  13397. return false;
  13398. }
  13399. fclose(fp);
  13400. if (bmp_data[0] != 'B' || bmp_data[1] != 'M') {
  13401. ShowError("%s: Invalid BMP file header given at \"%s\"\n", __func__, filepath.c_str());
  13402. return false;
  13403. }
  13404. // Compress the data into the destination
  13405. unsigned long com_size = sizeof(cd->image_data);
  13406. encode_zip(cd->image_data, &com_size, bmp_data, CAPTCHA_BMP_SIZE);
  13407. cd->image_size = static_cast<int16>(com_size);
  13408. return true;
  13409. }
  13410. const std::string CaptchaDatabase::getDefaultLocation() {
  13411. return std::string(db_path) + "/captcha_db.yml";
  13412. }
  13413. /**
  13414. * Reads and parses an entry from the captcha_db.
  13415. * @param node: YAML node containing the entry.
  13416. * @return count of successfully parsed rows
  13417. */
  13418. uint64 CaptchaDatabase::parseBodyNode(const ryml::NodeRef &node) {
  13419. uint16 index;
  13420. if (!this->asUInt16(node, "Id", index))
  13421. return 0;
  13422. std::shared_ptr<s_captcha_data> cd = captcha_db.find(index);
  13423. bool exists = cd != nullptr;
  13424. if (!exists) {
  13425. if (!this->nodesExist(node, { "Filename", "Answer" }))
  13426. return 0;
  13427. cd = std::make_shared<s_captcha_data>();
  13428. cd->index = index;
  13429. }
  13430. if (this->nodeExists(node, "Filename")) {
  13431. std::string filename;
  13432. if (!this->asString(node, "Filename", filename))
  13433. return 0;
  13434. if (!pc_macro_read_captcha_db_loadbmp(filename, cd)) {
  13435. this->invalidWarning(node["Filename"], "Failed to parse BMP image, skipping...\n");
  13436. return 0;
  13437. }
  13438. }
  13439. if (this->nodeExists(node, "Answer")) {
  13440. std::string answer;
  13441. if (!this->asString(node, "Answer", answer))
  13442. return 0;
  13443. if (answer.length() < 4 || answer.length() > CAPTCHA_ANSWER_SIZE) {
  13444. this->invalidWarning(node["Answer"], "The captcha answer must be between 4~%d characters, skipping...", CAPTCHA_ANSWER_SIZE);
  13445. return 0;
  13446. }
  13447. safestrncpy(cd->captcha_answer, answer.c_str(), sizeof(cd->captcha_answer));
  13448. }
  13449. if (this->nodeExists(node, "Bonus")) {
  13450. std::string script;
  13451. if (!this->asString(node, "Bonus", script)) {
  13452. return 0;
  13453. }
  13454. if (cd->bonus_script) {
  13455. script_free_code(cd->bonus_script);
  13456. cd->bonus_script = nullptr;
  13457. }
  13458. cd->bonus_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Bonus"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  13459. } else {
  13460. if (!exists)
  13461. 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);
  13462. }
  13463. if (!exists)
  13464. captcha_db.put(index, cd);
  13465. return 1;
  13466. }
  13467. /*==========================================
  13468. * pc Init/Terminate
  13469. *------------------------------------------*/
  13470. void do_final_pc(void) {
  13471. db_destroy(itemcd_db);
  13472. do_final_pc_groups();
  13473. ers_destroy(pc_sc_display_ers);
  13474. ers_destroy(num_reg_ers);
  13475. ers_destroy(str_reg_ers);
  13476. attendance_db.clear();
  13477. reputation_db.clear();
  13478. #ifdef MAP_GENERATOR
  13479. reputationgroup_db.clear();
  13480. #endif
  13481. penalty_db.clear();
  13482. captcha_db.clear();
  13483. }
  13484. void do_init_pc(void) {
  13485. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  13486. pc_readdb();
  13487. pc_read_motd(); // Read MOTD [Valaris]
  13488. attendance_db.load();
  13489. reputation_db.load();
  13490. #ifdef MAP_GENERATOR
  13491. reputationgroup_db.load();
  13492. #endif
  13493. captcha_db.load();
  13494. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  13495. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  13496. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  13497. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  13498. add_timer_func_list(pc_autosave, "pc_autosave");
  13499. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  13500. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  13501. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  13502. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  13503. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  13504. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  13505. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  13506. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  13507. add_timer_func_list(pc_macro_detector_timeout, "pc_macro_detector_timeout");
  13508. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  13509. // 0=day, 1=night [Yor]
  13510. night_flag = battle_config.night_at_start ? 1 : 0;
  13511. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  13512. int day_duration = battle_config.day_duration;
  13513. int night_duration = battle_config.night_duration;
  13514. // add night/day timer [Yor]
  13515. add_timer_func_list(map_day_timer, "map_day_timer");
  13516. add_timer_func_list(map_night_timer, "map_night_timer");
  13517. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  13518. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  13519. }
  13520. do_init_pc_groups();
  13521. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  13522. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13523. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13524. ers_chunk_size(pc_sc_display_ers, 150);
  13525. ers_chunk_size(num_reg_ers, 300);
  13526. ers_chunk_size(str_reg_ers, 50);
  13527. }