pc.cpp 470 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pc.hpp"
  4. #include <map>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #ifdef MAP_GENERATOR
  8. #include <fstream>
  9. #include <iostream>
  10. #include <nlohmann/json.hpp>
  11. #endif
  12. #include <common/cbasetypes.hpp>
  13. #include <common/core.hpp> // get_svn_revision()
  14. #include <common/database.hpp>
  15. #include <common/ers.hpp> // ers_destroy
  16. #include <common/grfio.hpp>
  17. #include <common/malloc.hpp>
  18. #include <common/mmo.hpp> //NAME_LENGTH
  19. #include <common/nullpo.hpp>
  20. #include <common/random.hpp>
  21. #include <common/showmsg.hpp>
  22. #include <common/socket.hpp> // session[]
  23. #include <common/strlib.hpp> // safestrncpy()
  24. #include <common/timer.hpp>
  25. #include <common/utilities.hpp>
  26. #include <common/utils.hpp>
  27. #include "achievement.hpp"
  28. #include "atcommand.hpp" // get_atcommand_level()
  29. #include "battle.hpp" // battle_config
  30. #include "battleground.hpp"
  31. #include "buyingstore.hpp" // struct s_buyingstore
  32. #include "channel.hpp"
  33. #include "chat.hpp"
  34. #include "chrif.hpp"
  35. #include "clan.hpp"
  36. #include "clif.hpp"
  37. #include "date.hpp" // is_day_of_*()
  38. #include "duel.hpp"
  39. #include "elemental.hpp"
  40. #include "guild.hpp"
  41. #include "homunculus.hpp"
  42. #include "instance.hpp"
  43. #include "intif.hpp"
  44. #include "itemdb.hpp" // MAX_ITEMGROUP
  45. #include "log.hpp"
  46. #include "map.hpp"
  47. #include "mercenary.hpp"
  48. #include "mob.hpp"
  49. #include "npc.hpp"
  50. #include "party.hpp" // party_search()
  51. #include "pc_groups.hpp"
  52. #include "pet.hpp" // pet_unlocktarget()
  53. #include "quest.hpp"
  54. #include "skill.hpp" // skill_isCopyable()
  55. #include "script.hpp" // struct script_reg, struct script_regstr
  56. #include "searchstore.hpp" // struct s_search_store_info
  57. #include "status.hpp" // OPTION_*, struct weapon_atk
  58. #include "storage.hpp"
  59. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  60. #include "vending.hpp" // struct s_vending
  61. using namespace rathena;
  62. JobDatabase job_db;
  63. CaptchaDatabase captcha_db;
  64. const char *macro_allowed_answer_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  65. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  66. static inline bool pc_attendance_rewarded_today( map_session_data* sd );
  67. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  68. PlayerStatPointDatabase statpoint_db;
  69. SkillTreeDatabase skill_tree_db;
  70. // timer for night.day implementation
  71. int day_timer_tid = INVALID_TIMER;
  72. int night_timer_tid = INVALID_TIMER;
  73. struct eri *pc_sc_display_ers = NULL;
  74. struct eri *num_reg_ers;
  75. struct eri *str_reg_ers;
  76. int pc_expiration_tid = INVALID_TIMER;
  77. struct fame_list smith_fame_list[MAX_FAME_LIST];
  78. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  79. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  80. const std::string AttendanceDatabase::getDefaultLocation(){
  81. return std::string(db_path) + "/attendance.yml";
  82. }
  83. /**
  84. * Reads and parses an entry from the attendance_db.
  85. * @param node: YAML node containing the entry.
  86. * @return count of successfully parsed rows
  87. */
  88. uint64 AttendanceDatabase::parseBodyNode(const ryml::NodeRef& node){
  89. uint32 start;
  90. if( !this->asUInt32( node, "Start", start ) ){
  91. return 0;
  92. }
  93. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  94. bool exists = attendance_period != nullptr;
  95. if( !exists ){
  96. if( !this->nodeExists( node, "End" ) ){
  97. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  98. return 0;
  99. }
  100. if( !this->nodeExists( node, "Rewards" ) ){
  101. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  102. return 0;
  103. }
  104. attendance_period = std::make_shared<s_attendance_period>();
  105. attendance_period->start = start;
  106. }
  107. // If it does not exist yet, we need to check it for sure
  108. bool requiresCollisionDetection = !exists;
  109. if( this->nodeExists( node, "End" ) ){
  110. uint32 end;
  111. if( !this->asUInt32( node, "End", end ) ){
  112. return 0;
  113. }
  114. // If the period is outdated already, we do not even bother parsing
  115. if( end < date_get( DT_YYYYMMDD ) ){
  116. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  117. return 0;
  118. }
  119. if( !exists || attendance_period->end != end ){
  120. requiresCollisionDetection = true;
  121. attendance_period->end = end;
  122. }
  123. }
  124. // Collision detection
  125. if( requiresCollisionDetection ){
  126. bool collision = false;
  127. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  128. std::shared_ptr<s_attendance_period> period = pair.second;
  129. if( exists && period->start == attendance_period->start ){
  130. // Dont compare to yourself
  131. continue;
  132. }
  133. // Check if start is inside another period
  134. if( period->start <= attendance_period->start && start <= period->end ){
  135. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  136. collision = true;
  137. break;
  138. }
  139. // Check if end is inside another period
  140. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  141. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  142. collision = true;
  143. break;
  144. }
  145. }
  146. if( collision ){
  147. return 0;
  148. }
  149. }
  150. if( this->nodeExists( node, "Rewards" ) ){
  151. const auto& rewardsNode = node["Rewards"];
  152. for( const auto& rewardNode : rewardsNode ){
  153. uint32 day;
  154. if( !this->asUInt32( rewardNode, "Day", day ) ){
  155. continue;
  156. }
  157. day -= 1;
  158. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  159. bool reward_exists = reward != nullptr;
  160. if( !reward_exists ){
  161. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  162. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  163. return 0;
  164. }
  165. reward = std::make_shared<s_attendance_reward>();
  166. }
  167. if( this->nodeExists( rewardNode, "ItemId" ) ){
  168. t_itemid item_id;
  169. if( !this->asUInt32( rewardNode, "ItemId", item_id ) ){
  170. continue;
  171. }
  172. if( !item_db.exists( item_id ) ){
  173. ShowError( "pc_attendance_load: Unknown item ID %u for day %d.\n", item_id, day + 1 );
  174. continue;
  175. }
  176. reward->item_id = item_id;
  177. }
  178. if( this->nodeExists( rewardNode, "Amount" ) ){
  179. uint16 amount;
  180. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  181. continue;
  182. }
  183. if( amount == 0 ){
  184. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  185. amount = 1;
  186. }else if( amount > MAX_AMOUNT ){
  187. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  188. amount = MAX_AMOUNT;
  189. }
  190. reward->amount = amount;
  191. }else{
  192. if( !reward_exists ){
  193. reward->amount = 1;
  194. }
  195. }
  196. if( !reward_exists ){
  197. attendance_period->rewards[day] = reward;
  198. }
  199. }
  200. bool missing_day = false;
  201. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  202. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  203. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  204. missing_day = true;
  205. break;
  206. }
  207. }
  208. if( missing_day ){
  209. return 0;
  210. }
  211. }
  212. if( !exists ){
  213. this->put( start, attendance_period );
  214. }
  215. return 1;
  216. }
  217. AttendanceDatabase attendance_db;
  218. const std::string ReputationDatabase::getDefaultLocation(){
  219. return std::string( db_path ) + "/reputation.yml";
  220. }
  221. uint64 ReputationDatabase::parseBodyNode( const ryml::NodeRef& node ){
  222. int64 id;
  223. if( !this->asInt64( node, "Id", id ) ){
  224. return 0;
  225. }
  226. std::shared_ptr<s_reputation> reputation = this->find( id );
  227. bool exists = reputation != nullptr;
  228. if( !exists ){
  229. if( !this->nodesExist( node, { "Name", "Variable" } ) ){
  230. return 0;
  231. }
  232. reputation = std::make_shared<s_reputation>();
  233. reputation->id = id;
  234. }
  235. if( this->nodeExists( node, "Name" ) ){
  236. std::string name;
  237. if( !this->asString( node, "Name", name ) ){
  238. return 0;
  239. }
  240. reputation->name = name;
  241. }
  242. if( this->nodeExists( node, "Variable" ) ){
  243. std::string variable;
  244. if( !this->asString( node, "Variable", variable ) ){
  245. return 0;
  246. }
  247. if( variable.length() > 32 ){
  248. this->invalidWarning( node, "Variable name \"%s\" exceeds maximum length 32.\n", variable.c_str() );
  249. return 0;
  250. }
  251. reputation->variable = variable;
  252. }
  253. if( this->nodeExists( node, "Minimum" ) ){
  254. int64 minimum;
  255. if( !this->asInt64( node, "Minimum", minimum ) ){
  256. return 0;
  257. }
  258. reputation->minimum = minimum;
  259. }else{
  260. if( !exists ){
  261. reputation->minimum = INT64_MIN;
  262. }
  263. }
  264. if( this->nodeExists( node, "Maximum" ) ){
  265. int64 maximum;
  266. if( !this->asInt64( node, "Maximum", maximum ) ){
  267. return 0;
  268. }
  269. reputation->maximum = maximum;
  270. }else{
  271. if( !exists ){
  272. reputation->maximum = INT64_MIN;
  273. }
  274. }
  275. #ifdef MAP_GENERATOR
  276. if (this->nodeExists(node, "Visibility")) {
  277. std::string visibility;
  278. if (!this->asString(node, "Visibility", visibility)) {
  279. return 0;
  280. }
  281. if (visibility == "Always")
  282. reputation->visibility = s_reputation::e_visibility::ALWAYS;
  283. else if (visibility == "Never")
  284. reputation->visibility = s_reputation::e_visibility::NEVER;
  285. else if (visibility == "Exist")
  286. reputation->visibility = s_reputation::e_visibility::EXIST;
  287. else {
  288. this->invalidWarning(node, "Visibility \"%s\" unknown.\n", visibility.c_str());
  289. return 0;
  290. }
  291. } else {
  292. if (!exists) {
  293. reputation->visibility = s_reputation::e_visibility::ALWAYS;
  294. }
  295. }
  296. #endif
  297. if( !exists ){
  298. this->put( id, reputation );
  299. }
  300. return 1;
  301. }
  302. ReputationDatabase reputation_db;
  303. const std::string ReputationGroupDatabase::getDefaultLocation() {
  304. return std::string(db_path) + "/reputation_group.yml";
  305. }
  306. uint64 ReputationGroupDatabase::parseBodyNode(const ryml::NodeRef& node) {
  307. int64 id;
  308. if (!this->asInt64(node, "Id", id)) {
  309. return 0;
  310. }
  311. std::shared_ptr<s_reputationgroup> group = this->find(id);
  312. bool exists = group != nullptr;
  313. if (!exists) {
  314. if (!this->nodesExist(node, {"Name", "ReputeList"})) {
  315. return 0;
  316. }
  317. group = std::make_shared<s_reputationgroup>();
  318. group->id = id;
  319. }
  320. if (this->nodeExists(node, "Name")) {
  321. std::string name;
  322. if (!this->asString(node, "Name", name)) {
  323. return 0;
  324. }
  325. group->name = name;
  326. }
  327. if (this->nodeExists(node, "ScriptName")) {
  328. std::string script_name;
  329. if (!this->asString(node, "ScriptName", script_name)) {
  330. return 0;
  331. }
  332. group->script_name = script_name;
  333. }
  334. if (this->nodeExists(node, "ReputeList")) {
  335. const auto& reputelist = node[c4::to_csubstr("ReputeList")];
  336. for (const auto& repute : reputelist) {
  337. int64 repute_id;
  338. try {
  339. repute >> repute_id;
  340. } catch (std::runtime_error const&) {
  341. this->invalidWarning(node, "Value \"%s\" cannot be parsed as int64.\n", repute.val().str);
  342. continue;
  343. }
  344. if (!reputation_db.find(repute_id)) {
  345. this->invalidWarning(node, "Reputation id %lld does not exist in reputation_db!\n", repute_id);
  346. continue;
  347. }
  348. group->reputations.push_back(repute_id);
  349. }
  350. }
  351. if (!exists) {
  352. this->put(id, group);
  353. }
  354. return 1;
  355. }
  356. ReputationGroupDatabase reputationgroup_db;
  357. void pc_reputation_generate() {
  358. #ifdef MAP_GENERATOR
  359. const std::string filePrefix = "generated/clientside/data/contentdata/";
  360. auto reputeInfo = nlohmann::json::object();
  361. for (const auto& pair : reputation_db) {
  362. auto id = pair.first;
  363. auto rep = pair.second;
  364. nlohmann::json node;
  365. switch (rep->visibility) {
  366. case s_reputation::e_visibility::ALWAYS:
  367. node["Invisible"] = "VISIBLE_TRUE";
  368. break;
  369. case s_reputation::e_visibility::NEVER:
  370. node["Invisible"] = "VISIBLE_FALSE";
  371. break;
  372. case s_reputation::e_visibility::EXIST:
  373. node["Invisible"] = "VISIBLE_EXIST";
  374. break;
  375. }
  376. node["MaxPoint_Negative"] = std::abs(rep->minimum);
  377. node["MaxPoint_Positive"] = std::abs(rep->maximum);
  378. node["Name"] = rep->name;
  379. reputeInfo[std::to_string(id)] = node;
  380. }
  381. auto j = nlohmann::json::object();
  382. j["reputeInfo"] = reputeInfo;
  383. // std::cout << j.dump(2) << "\n";
  384. auto bson = nlohmann::json::to_bson(j);
  385. auto reputation_file = std::ofstream(filePrefix + "./reputeinfodata.bson", std::ios::binary);
  386. if (!reputation_file) {
  387. ShowError("Failed to create reputation file.\n");
  388. ShowError("Maybe the file directory \"%s\" does not exist?\n", filePrefix.c_str());
  389. ShowInfo("Create the directory and rerun map-server-generator\n");
  390. exit(1);
  391. }
  392. reputation_file.write((const char *)&bson[0], bson.size());
  393. auto reputeGroupInfo = nlohmann::json::object();
  394. for (const auto& pair : reputationgroup_db) {
  395. auto id = pair.first;
  396. auto group = pair.second;
  397. nlohmann::json node;
  398. node["ID"] = group->script_name;
  399. node["Name"] = group->name;
  400. node["ReputeList"] = group->reputations;
  401. reputeGroupInfo[std::to_string(id)] = node;
  402. }
  403. j = nlohmann::json::object();
  404. j["ReputeGroup"] = reputeGroupInfo;
  405. // std::cout << j.dump(2) << "\n";
  406. bson = nlohmann::json::to_bson(j);
  407. auto reputation_group_file = std::ofstream(filePrefix + "./reputegroupdata.bson", std::ios::binary);
  408. if (!reputation_group_file) {
  409. ShowError("Failed to create reputation group file.\n");
  410. ShowError("Maybe the file directory \"%s\" does not exist?\n", filePrefix.c_str());
  411. ShowInfo("Create the directory and rerun map-server-generator\n");
  412. exit(1);
  413. }
  414. reputation_group_file.write((const char *)&bson[0], bson.size());
  415. #endif
  416. }
  417. const std::string PenaltyDatabase::getDefaultLocation(){
  418. return std::string( db_path ) + "/level_penalty.yml";
  419. }
  420. uint64 PenaltyDatabase::parseBodyNode(const ryml::NodeRef& node){
  421. std::string type_constant;
  422. if( !this->asString( node, "Type", type_constant ) ){
  423. return 0;
  424. }
  425. int64 constant_value;
  426. if( !script_get_constant( ( "PENALTY_" + type_constant ).c_str(), &constant_value ) ){
  427. this->invalidWarning( node["Type"], "Unknown penalty type \"%s\".\n", type_constant.c_str() );
  428. return 0;
  429. }
  430. if( constant_value < PENALTY_NONE || constant_value > PENALTY_MAX ){
  431. this->invalidWarning( node["Type"], "Invalid penalty type \"%s\".\n", type_constant.c_str() );
  432. return 0;
  433. }
  434. e_penalty_type type = static_cast<e_penalty_type>( constant_value );
  435. std::shared_ptr<s_penalty> penalty = this->find( type );
  436. bool exists = penalty != nullptr;
  437. if( !exists ){
  438. penalty = std::make_shared<s_penalty>();
  439. penalty->type = type;
  440. for( int i = 0, max = ARRAYLENGTH( penalty->rate ); i < max; i++ ){
  441. penalty->rate[i] = UINT16_MAX;
  442. }
  443. }
  444. if( this->nodeExists( node, "LevelDifferences" ) ){
  445. for( const auto& levelNode : node["LevelDifferences"] ){
  446. if( !this->nodesExist( levelNode, { "Difference", "Rate" } ) ){
  447. return 0;
  448. }
  449. int32 difference;
  450. if( !this->asInt32( levelNode, "Difference", difference ) ){
  451. return 0;
  452. }
  453. if( std::abs( difference ) > MAX_LEVEL ){
  454. this->invalidWarning( levelNode["Difference"], "Level difference %d is bigger than maximum level %d.\n", difference, MAX_LEVEL );
  455. return 0;
  456. }
  457. uint16 rate;
  458. if( !this->asUInt16Rate( levelNode, "Rate", rate ) ){
  459. return 0;
  460. }
  461. penalty->rate[difference + MAX_LEVEL - 1] = rate;
  462. }
  463. }
  464. if( !exists ){
  465. this->put( type, penalty );
  466. }
  467. return 1;
  468. }
  469. void PenaltyDatabase::loadingFinished(){
  470. for( const auto& pair : *this ){
  471. for( int i = MAX_LEVEL - 1, max = ARRAYLENGTH( pair.second->rate ), last_rate = 100; i < max; i++ ){
  472. uint16 rate = pair.second->rate[i];
  473. // Check if it has been defined
  474. if( rate == UINT16_MAX ){
  475. pair.second->rate[i] = last_rate;
  476. }else{
  477. last_rate = rate;
  478. }
  479. }
  480. for( int i = MAX_LEVEL - 1, last_rate = 100; i >= 0; i-- ){
  481. uint16 rate = pair.second->rate[i];
  482. // Check if it has been defined
  483. if( rate == UINT16_MAX ){
  484. pair.second->rate[i] = last_rate;
  485. }else{
  486. last_rate = rate;
  487. }
  488. }
  489. }
  490. TypesafeYamlDatabase::loadingFinished();
  491. }
  492. PenaltyDatabase penalty_db;
  493. #define MOTD_LINE_SIZE 128
  494. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  495. bool reg_load;
  496. /**
  497. * Translation table from athena equip index to aegis bitmask
  498. */
  499. unsigned int equip_bitmask[EQI_MAX] = {
  500. EQP_ACC_L, // EQI_ACC_L
  501. EQP_ACC_R, // EQI_ACC_R
  502. EQP_SHOES, // EQI_SHOES
  503. EQP_GARMENT, // EQI_GARMENT
  504. EQP_HEAD_LOW, // EQI_HEAD_LOW
  505. EQP_HEAD_MID, // EQI_HEAD_MID
  506. EQP_HEAD_TOP, // EQI_HEAD_TOP
  507. EQP_ARMOR, // EQI_ARMOR
  508. EQP_HAND_L, // EQI_HAND_L
  509. EQP_HAND_R, // EQI_HAND_R
  510. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  511. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  512. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  513. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  514. EQP_AMMO, // EQI_AMMO
  515. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  516. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  517. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  518. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  519. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  520. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  521. };
  522. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  523. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  524. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  525. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  526. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  527. };
  528. void pc_set_reg_load( bool val ){
  529. reg_load = val;
  530. }
  531. /**
  532. * Item Cool Down Delay Saving
  533. * Struct item_cd is not a member of map_session_data
  534. * to keep cooldowns in memory between player log-ins.
  535. * All cooldowns are reset when server is restarted.
  536. **/
  537. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  538. struct item_cd {
  539. t_tick tick[MAX_ITEMDELAYS]; //tick
  540. t_itemid nameid[MAX_ITEMDELAYS]; //item id
  541. };
  542. /**
  543. * Converts a class to its array index for CLASS_COUNT defined arrays.
  544. * Note that it does not do a validity check for speed purposes, where parsing
  545. * player input make sure to use a pcdb_checkid first!
  546. * @param class_ Job ID see enum e_job
  547. * @return Class Index
  548. */
  549. int pc_class2idx(int class_) {
  550. if (class_ >= JOB_NOVICE_HIGH)
  551. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  552. return class_;
  553. }
  554. /**
  555. * Get player's group ID
  556. * @param sd
  557. * @return Group ID
  558. */
  559. int pc_get_group_id(map_session_data *sd) {
  560. return sd->group_id;
  561. }
  562. /** Get player's group Level
  563. * @param sd
  564. * @return Group Level
  565. */
  566. int pc_get_group_level(map_session_data *sd) {
  567. return sd->group->level;
  568. }
  569. /**
  570. * Remove a player from queue after timeout
  571. * @param tid: Timer ID
  572. * @param tick: Timer
  573. * @param id: ID
  574. * @return 0 on success or 1 otherwise
  575. */
  576. static TIMER_FUNC(pc_on_expire_active)
  577. {
  578. map_session_data *sd = (map_session_data *)data;
  579. nullpo_retr(1, sd);
  580. sd->tid_queue_active = INVALID_TIMER;
  581. bg_queue_leave(sd);
  582. clif_bg_queue_entry_init(sd);
  583. return 0;
  584. }
  585. /**
  586. * Function used to set timer externally
  587. * @param sd: Player data
  588. */
  589. void pc_set_bg_queue_timer(map_session_data *sd) {
  590. nullpo_retv(sd);
  591. if (sd->tid_queue_active != INVALID_TIMER) {
  592. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  593. sd->tid_queue_active = INVALID_TIMER;
  594. }
  595. sd->tid_queue_active = add_timer(gettick() + 20000, pc_on_expire_active, 0, (intptr_t)sd);
  596. }
  597. /**
  598. * Function used to delete timer externally
  599. * @param sd: Player data
  600. */
  601. void pc_delete_bg_queue_timer(map_session_data *sd) {
  602. nullpo_retv(sd);
  603. if (sd->tid_queue_active != INVALID_TIMER) {
  604. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  605. sd->tid_queue_active = INVALID_TIMER;
  606. }
  607. }
  608. static TIMER_FUNC(pc_invincible_timer){
  609. map_session_data *sd;
  610. if( (sd=(map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  611. return 1;
  612. if(sd->invincible_timer != tid){
  613. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  614. return 0;
  615. }
  616. sd->invincible_timer = INVALID_TIMER;
  617. skill_unit_move(&sd->bl,tick,1);
  618. return 0;
  619. }
  620. void pc_setinvincibletimer(map_session_data* sd, int val) {
  621. nullpo_retv(sd);
  622. if( sd->invincible_timer != INVALID_TIMER )
  623. delete_timer(sd->invincible_timer,pc_invincible_timer);
  624. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  625. }
  626. void pc_delinvincibletimer(map_session_data* sd)
  627. {
  628. nullpo_retv(sd);
  629. if( sd->invincible_timer != INVALID_TIMER )
  630. {
  631. delete_timer(sd->invincible_timer,pc_invincible_timer);
  632. sd->invincible_timer = INVALID_TIMER;
  633. skill_unit_move(&sd->bl,gettick(),1);
  634. }
  635. }
  636. static TIMER_FUNC(pc_spiritball_timer){
  637. map_session_data *sd;
  638. int i;
  639. if( (sd=(map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  640. return 1;
  641. if( sd->spiritball <= 0 )
  642. {
  643. ShowError("pc_spiritball_timer: %d spiritball's available. (aid=%d cid=%d tid=%d)\n", sd->spiritball, sd->status.account_id, sd->status.char_id, tid);
  644. sd->spiritball = 0;
  645. return 0;
  646. }
  647. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  648. if( i == sd->spiritball )
  649. {
  650. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  651. return 0;
  652. }
  653. sd->spiritball--;
  654. if( i != sd->spiritball )
  655. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  656. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  657. clif_spiritball(&sd->bl);
  658. return 0;
  659. }
  660. /**
  661. * Adds a spiritball to player for 'interval' ms
  662. * @param sd
  663. * @param interval
  664. * @param max
  665. */
  666. void pc_addspiritball(map_session_data *sd,int interval,int max)
  667. {
  668. int tid;
  669. uint8 i;
  670. nullpo_retv(sd);
  671. if(max > MAX_SPIRITBALL)
  672. max = MAX_SPIRITBALL;
  673. if(sd->spiritball < 0)
  674. sd->spiritball = 0;
  675. if( sd->spiritball && sd->spiritball >= max )
  676. {
  677. if(sd->spirit_timer[0] != INVALID_TIMER)
  678. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  679. sd->spiritball--;
  680. if( sd->spiritball != 0 )
  681. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  682. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  683. }
  684. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  685. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spirit_timer[i])->tick) < 0);
  686. if( i != sd->spiritball )
  687. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  688. sd->spirit_timer[i] = tid;
  689. sd->spiritball++;
  690. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  691. clif_millenniumshield(&sd->bl,sd->spiritball);
  692. else
  693. clif_spiritball(&sd->bl);
  694. }
  695. /**
  696. * Removes number of spiritball from player
  697. * @param sd
  698. * @param count
  699. * @param type 1 = doesn't give client effect
  700. */
  701. void pc_delspiritball(map_session_data *sd,int count,int type)
  702. {
  703. uint8 i;
  704. nullpo_retv(sd);
  705. if(sd->spiritball <= 0) {
  706. sd->spiritball = 0;
  707. return;
  708. }
  709. if(count == 0)
  710. return;
  711. if(count > sd->spiritball)
  712. count = sd->spiritball;
  713. sd->spiritball -= count;
  714. if(count > MAX_SPIRITBALL)
  715. count = MAX_SPIRITBALL;
  716. for(i=0;i<count;i++) {
  717. if(sd->spirit_timer[i] != INVALID_TIMER) {
  718. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  719. sd->spirit_timer[i] = INVALID_TIMER;
  720. }
  721. }
  722. for(i=count;i<MAX_SPIRITBALL;i++) {
  723. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  724. sd->spirit_timer[i] = INVALID_TIMER;
  725. }
  726. if(!type) {
  727. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  728. clif_millenniumshield(&sd->bl,sd->spiritball);
  729. else
  730. clif_spiritball(&sd->bl);
  731. }
  732. }
  733. /**
  734. * Adds a soulball to player
  735. * @param sd: Player data
  736. * @param max: Max amount of soulballs
  737. */
  738. int pc_addsoulball(map_session_data *sd, int max)
  739. {
  740. nullpo_ret(sd);
  741. status_change *sc = status_get_sc(&sd->bl);
  742. if (sc == nullptr || sc->getSCE(SC_SOULENERGY) == nullptr) {
  743. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, 0, skill_get_time2(SP_SOULCOLLECT, 1));
  744. sd->soulball = 0;
  745. }
  746. max = min(max, MAX_SOUL_BALL);
  747. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  748. if (sd->soulball && sd->soulball >= max)
  749. sd->soulball--;
  750. sd->soulball++;
  751. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, sd->soulball, skill_get_time2(SP_SOULCOLLECT, 1));
  752. clif_soulball(sd);
  753. return 0;
  754. }
  755. /**
  756. * Removes number of soulball from player
  757. * @param sd: Player data
  758. * @param count: Amount to remove
  759. * @param type: true = doesn't give client effect
  760. */
  761. int pc_delsoulball(map_session_data *sd, int count, bool type)
  762. {
  763. nullpo_ret(sd);
  764. if (count <= 0)
  765. return 0;
  766. status_change *sc = status_get_sc(&sd->bl);
  767. if (sd->soulball <= 0 || sc == nullptr || sc->getSCE(SC_SOULENERGY) == nullptr) {
  768. sd->soulball = 0;
  769. }else{
  770. sd->soulball -= cap_value(count, 0, sd->soulball);
  771. if (sd->soulball == 0)
  772. status_change_end(&sd->bl, SC_SOULENERGY);
  773. else
  774. sc->getSCE(SC_SOULENERGY)->val1 = sd->soulball;
  775. }
  776. if (!type)
  777. clif_soulball(sd);
  778. return 0;
  779. }
  780. /**
  781. * Adds servantballs to a player
  782. * @param sd: Player data
  783. * @param amount: Amount to add
  784. */
  785. void pc_addservantball( map_session_data& sd, int count ){
  786. sd.servantball = cap_value( sd.servantball + count, 0, MAX_SERVANTBALL );
  787. clif_servantball( sd );
  788. }
  789. /**
  790. * Removes number of servantballs from player
  791. * @param sd: Player data
  792. * @param count: Amount to remove
  793. */
  794. void pc_delservantball( map_session_data& sd, int count ){
  795. sd.servantball = cap_value( sd.servantball - count, 0, MAX_SERVANTBALL );
  796. clif_servantball( sd );
  797. }
  798. /**
  799. * Adds abyssballs to a player
  800. * @param sd: Player data
  801. * @param amount: Amount to add
  802. */
  803. void pc_addabyssball( map_session_data& sd, int count ){
  804. sd.abyssball = cap_value( sd.abyssball + count, 0, MAX_ABYSSBALL );
  805. clif_abyssball( sd );
  806. }
  807. /**
  808. * Removes number of abyssballs from player
  809. * @param sd: Player data
  810. * @param count: Amount to remove
  811. */
  812. void pc_delabyssball( map_session_data& sd, int count ){
  813. sd.abyssball = cap_value( sd.abyssball - count, 0, MAX_ABYSSBALL );
  814. clif_abyssball( sd );
  815. }
  816. /**
  817. * Increases a player's fame points and displays a notice to him
  818. * @param sd Player
  819. * @param count Fame point
  820. * @return true: on success, false: on error
  821. */
  822. bool pc_addfame(map_session_data &sd, int count)
  823. {
  824. enum e_rank ranktype;
  825. sd.status.fame += count;
  826. if (sd.status.fame > MAX_FAME)
  827. sd.status.fame = MAX_FAME;
  828. switch(sd.class_&MAPID_UPPERMASK){
  829. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  830. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  831. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  832. default:
  833. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd.status.class_), sd.status.class_ );
  834. return false;
  835. }
  836. clif_update_rankingpoint(sd, ranktype, count);
  837. chrif_updatefamelist(sd, ranktype);
  838. return true;
  839. }
  840. /**
  841. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  842. * @param sd
  843. * @param job Job use enum e_mapid
  844. * @return Rank
  845. */
  846. unsigned char pc_famerank(uint32 char_id, int job)
  847. {
  848. uint8 i;
  849. switch(job){
  850. case MAPID_BLACKSMITH: // Blacksmith
  851. for(i = 0; i < MAX_FAME_LIST; i++){
  852. if(smith_fame_list[i].id == char_id)
  853. return i + 1;
  854. }
  855. break;
  856. case MAPID_ALCHEMIST: // Alchemist
  857. for(i = 0; i < MAX_FAME_LIST; i++){
  858. if(chemist_fame_list[i].id == char_id)
  859. return i + 1;
  860. }
  861. break;
  862. case MAPID_TAEKWON: // Taekwon
  863. for(i = 0; i < MAX_FAME_LIST; i++){
  864. if(taekwon_fame_list[i].id == char_id)
  865. return i + 1;
  866. }
  867. break;
  868. }
  869. return 0;
  870. }
  871. /**
  872. * Restart player's HP & SP value
  873. * @param sd
  874. * @param type Restart type: 1 - Normal Resurection
  875. */
  876. void pc_setrestartvalue(map_session_data *sd, char type) {
  877. struct status_data *status, *b_status;
  878. nullpo_retv(sd);
  879. b_status = &sd->base_status;
  880. status = &sd->battle_status;
  881. if (type&1) { //Normal resurrection
  882. status->hp = 1; //Otherwise status_heal may fail if dead.
  883. status_heal(&sd->bl, b_status->hp, 0, 1);
  884. if( status->sp < b_status->sp )
  885. status_set_sp(&sd->bl, b_status->sp, 1);
  886. } else { //Just for saving on the char-server (with values as if respawned)
  887. sd->status.hp = b_status->hp;
  888. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  889. sd->status.ap = (status->ap < b_status->ap)?b_status->ap:status->ap;
  890. }
  891. }
  892. /*==========================================
  893. Rental System
  894. *------------------------------------------*/
  895. /**
  896. * Ends a rental and removes the item/effect
  897. * @param tid: Tick ID
  898. * @param tick: Timer
  899. * @param id: Timer ID
  900. * @param data: Data
  901. * @return false - failure, true - success
  902. */
  903. TIMER_FUNC(pc_inventory_rental_end){
  904. map_session_data *sd = map_id2sd(id);
  905. if( sd == NULL )
  906. return 0;
  907. if( tid != sd->rental_timer ) {
  908. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  909. return 0;
  910. }
  911. pc_inventory_rentals(sd);
  912. return 1;
  913. }
  914. /**
  915. * Removes the rental timer from the player
  916. * @param sd: Player data
  917. */
  918. void pc_inventory_rental_clear(map_session_data *sd)
  919. {
  920. if( sd->rental_timer != INVALID_TIMER ) {
  921. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  922. sd->rental_timer = INVALID_TIMER;
  923. }
  924. }
  925. /**
  926. * Check for items in the player's inventory that are rental type
  927. * @param sd: Player data
  928. */
  929. void pc_inventory_rentals(map_session_data *sd)
  930. {
  931. int i, c = 0;
  932. unsigned int next_tick = UINT_MAX;
  933. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  934. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  935. continue; // Nothing here
  936. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  937. continue;
  938. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  939. if (sd->inventory_data[i]->unequip_script)
  940. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  941. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  942. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  943. } else {
  944. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  945. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  946. next_tick = umin(expire_tick * 1000U, next_tick);
  947. c++;
  948. }
  949. }
  950. if( c > 0 ) // min(next_tick,3600000) 1 hour each timer to keep announcing to the owner, and to avoid a but with rental time > 15 days
  951. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  952. else
  953. sd->rental_timer = INVALID_TIMER;
  954. }
  955. /**
  956. * Add a rental item to the player and adjusts the rental timer appropriately
  957. * @param sd: Player data
  958. * @param seconds: Rental time
  959. */
  960. void pc_inventory_rental_add(map_session_data *sd, unsigned int seconds)
  961. {
  962. t_tick tick = seconds * 1000;
  963. if( sd == NULL )
  964. return;
  965. if( sd->rental_timer != INVALID_TIMER ) {
  966. const struct TimerData * td;
  967. td = get_timer(sd->rental_timer);
  968. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  969. pc_inventory_rental_clear(sd);
  970. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  971. }
  972. } else
  973. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  974. }
  975. /**
  976. * Check if the player can sell the current item
  977. * @param sd: map_session_data of the player
  978. * @param item: struct of the checking item
  979. * @param shoptype: NPC's sub type see enum npc_subtype
  980. * @return bool 'true' is sellable, 'false' otherwise
  981. */
  982. bool pc_can_sell_item(map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  983. if (sd == NULL || item == NULL)
  984. return false;
  985. if (!pc_can_give_items(sd))
  986. return false;
  987. if (item->equip > 0 || item->amount < 0)
  988. return false;
  989. if (battle_config.hide_fav_sell && item->favorite)
  990. return false; //Cannot sell favs (optional config)
  991. if (!battle_config.rental_transaction && item->expire_time)
  992. return false; // Cannot Sell Rental Items
  993. if( item->equipSwitch ){
  994. return false;
  995. }
  996. if (itemdb_ishatched_egg(item))
  997. return false;
  998. switch (shoptype) {
  999. case NPCTYPE_SHOP:
  1000. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  1001. item->bound != BOUND_GUILD ||
  1002. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  1003. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  1004. ))
  1005. return true;
  1006. break;
  1007. case NPCTYPE_ITEMSHOP:
  1008. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  1009. item->bound != BOUND_GUILD ||
  1010. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  1011. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  1012. ))
  1013. return true;
  1014. else if (!item->bound) {
  1015. struct item_data *itd = itemdb_search(item->nameid);
  1016. if (itd && itd->flag.trade_restriction.sell && battle_config.allow_bound_sell&ISR_SELLABLE)
  1017. return true;
  1018. }
  1019. break;
  1020. }
  1021. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  1022. return false;
  1023. if (item->bound && !pc_can_give_bounded_items(sd))
  1024. return false; // Don't allow sale of bound items
  1025. return true;
  1026. }
  1027. /**
  1028. * Determines if player can give / drop / trade / vend items
  1029. */
  1030. bool pc_can_give_items(map_session_data *sd)
  1031. {
  1032. return pc_has_permission(sd, PC_PERM_TRADE);
  1033. }
  1034. /**
  1035. * Determines if player can give / drop / trade / vend bounded items
  1036. */
  1037. bool pc_can_give_bounded_items(map_session_data *sd)
  1038. {
  1039. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  1040. }
  1041. /**
  1042. * Determine if an item in a player's inventory is tradeable based on several merits.
  1043. * Checks for item_trade, bound, and rental restrictions.
  1044. * @param sd: Player data
  1045. * @param index: Item inventory index
  1046. * @return True if the item can be traded or false otherwise
  1047. */
  1048. bool pc_can_trade_item(map_session_data *sd, int index) {
  1049. if (sd && index >= 0) {
  1050. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  1051. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  1052. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  1053. );
  1054. }
  1055. return false;
  1056. }
  1057. /*==========================================
  1058. * Prepares character for saving.
  1059. * @param sd
  1060. *------------------------------------------*/
  1061. void pc_makesavestatus(map_session_data *sd) {
  1062. nullpo_retv(sd);
  1063. if(!battle_config.save_clothcolor)
  1064. sd->status.clothes_color = 0;
  1065. if(!battle_config.save_body_style)
  1066. sd->status.body = 0;
  1067. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  1068. //status change load/saving. [Skotlex]
  1069. #ifdef NEW_CARTS
  1070. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1071. #else
  1072. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1073. #endif
  1074. if (sd->sc.getSCE(SC_JAILED)) { //When Jailed, do not move last point.
  1075. if(pc_isdead(sd)){
  1076. pc_setrestartvalue(sd, 0);
  1077. } else {
  1078. sd->status.hp = sd->battle_status.hp;
  1079. sd->status.sp = sd->battle_status.sp;
  1080. sd->status.ap = sd->battle_status.ap;
  1081. }
  1082. mapindex_getmapname( mapindex_id2name( sd->mapindex ), sd->status.last_point.map );
  1083. sd->status.last_point.x = sd->bl.x;
  1084. sd->status.last_point.y = sd->bl.y;
  1085. return;
  1086. }
  1087. if( pc_isdead( sd ) ){
  1088. pc_setrestartvalue( sd, 0 );
  1089. // Return to save point
  1090. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1091. sd->status.last_point.x = sd->status.save_point.x;
  1092. sd->status.last_point.y = sd->status.save_point.y;
  1093. }else{
  1094. sd->status.hp = sd->battle_status.hp;
  1095. sd->status.sp = sd->battle_status.sp;
  1096. sd->status.ap = sd->battle_status.ap;
  1097. struct map_data* mapdata = map_getmapdata( sd->bl.m );
  1098. // If saving is not allowed on the map, we return the player to the designated point
  1099. if( mapdata->flag[MF_NOSAVE] ){
  1100. // The map has a specific return point
  1101. if( mapdata->save.map ){
  1102. safestrncpy( sd->status.last_point.map, mapindex_id2name( mapdata->save.map ), sizeof( sd->status.last_point.map ) );
  1103. sd->status.last_point.x = mapdata->save.x;
  1104. sd->status.last_point.y = mapdata->save.y;
  1105. // Return the user to his save point
  1106. }else{
  1107. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1108. sd->status.last_point.x = sd->status.save_point.x;
  1109. sd->status.last_point.y = sd->status.save_point.y;
  1110. }
  1111. // If the user is on a instance map, we return him to his save point
  1112. }else if( mapdata->instance_id ){
  1113. // Return the user to his save point
  1114. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1115. sd->status.last_point.x = sd->status.save_point.x;
  1116. sd->status.last_point.y = sd->status.save_point.y;
  1117. }else{
  1118. // Save normally
  1119. mapindex_getmapname( mapindex_id2name( sd->mapindex ), sd->status.last_point.map );
  1120. sd->status.last_point.x = sd->bl.x;
  1121. sd->status.last_point.y = sd->bl.y;
  1122. }
  1123. }
  1124. }
  1125. /*==========================================
  1126. * Off init ? Connection?
  1127. *------------------------------------------*/
  1128. void pc_setnewpc(map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, t_tick client_tick, int sex, int fd) {
  1129. nullpo_retv(sd);
  1130. sd->bl.id = account_id;
  1131. sd->status.account_id = account_id;
  1132. sd->status.char_id = char_id;
  1133. sd->status.sex = sex;
  1134. sd->login_id1 = login_id1;
  1135. sd->login_id2 = 0; // at this point, we can not know the value :(
  1136. sd->client_tick = client_tick;
  1137. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  1138. sd->bl.type = BL_PC;
  1139. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  1140. sd->canlog_tick = gettick();
  1141. //Required to prevent homunculus copuing a base speed of 0.
  1142. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  1143. }
  1144. /**
  1145. * Get equip point for an equip
  1146. * @param sd
  1147. * @param id
  1148. */
  1149. int pc_equippoint_sub(map_session_data *sd,struct item_data* id){
  1150. int ep = 0;
  1151. nullpo_ret(sd);
  1152. nullpo_ret(id);
  1153. if (!itemdb_isequip2(id))
  1154. return 0; //Not equippable by players.
  1155. ep = id->equip;
  1156. if(id->subtype == W_DAGGER || id->subtype == W_1HSWORD || id->subtype == W_1HAXE) {
  1157. if(pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN || (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO) { //Kagerou and Oboro can dual wield daggers. [Rytech]
  1158. if (ep == EQP_WEAPON)
  1159. return EQP_ARMS;
  1160. if (ep == EQP_SHADOW_WEAPON)
  1161. return EQP_SHADOW_ARMS;
  1162. }
  1163. }
  1164. return ep;
  1165. }
  1166. /**
  1167. * Get equip point for an equip
  1168. * @param sd
  1169. * @param n Equip index in inventory
  1170. */
  1171. int pc_equippoint(map_session_data *sd,int n){
  1172. nullpo_ret(sd);
  1173. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  1174. }
  1175. /**
  1176. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  1177. * @param sd : player session
  1178. * @return 0 sucess, 1:invalid sd
  1179. */
  1180. void pc_setinventorydata(map_session_data *sd)
  1181. {
  1182. uint8 i;
  1183. nullpo_retv(sd);
  1184. for(i = 0; i < MAX_INVENTORY; i++) {
  1185. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  1186. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  1187. }
  1188. }
  1189. /**
  1190. * 'Calculates' weapon type
  1191. * @param sd : Player
  1192. */
  1193. void pc_calcweapontype(map_session_data *sd)
  1194. {
  1195. nullpo_retv(sd);
  1196. // single-hand
  1197. if(sd->weapontype2 == W_FIST) {
  1198. sd->status.weapon = sd->weapontype1;
  1199. return;
  1200. }
  1201. if(sd->weapontype1 == W_FIST) {
  1202. sd->status.weapon = sd->weapontype2;
  1203. return;
  1204. }
  1205. // dual-wield
  1206. sd->status.weapon = W_FIST;
  1207. switch (sd->weapontype1){
  1208. case W_DAGGER:
  1209. switch (sd->weapontype2) {
  1210. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  1211. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  1212. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  1213. }
  1214. break;
  1215. case W_1HSWORD:
  1216. switch (sd->weapontype2) {
  1217. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  1218. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  1219. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  1220. }
  1221. break;
  1222. case W_1HAXE:
  1223. switch (sd->weapontype2) {
  1224. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  1225. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  1226. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  1227. }
  1228. }
  1229. // unknown, default to right hand type
  1230. if (!sd->status.weapon)
  1231. sd->status.weapon = sd->weapontype1;
  1232. }
  1233. /**
  1234. * Set equip index
  1235. * @param sd : Player
  1236. */
  1237. void pc_setequipindex(map_session_data *sd)
  1238. {
  1239. uint16 i;
  1240. nullpo_retv(sd);
  1241. for (i = 0; i < EQI_MAX; i++){
  1242. sd->equip_index[i] = -1;
  1243. sd->equip_switch_index[i] = -1;
  1244. }
  1245. for (i = 0; i < MAX_INVENTORY; i++) {
  1246. if (sd->inventory.u.items_inventory[i].nameid == 0)
  1247. continue;
  1248. if (sd->inventory.u.items_inventory[i].equip) {
  1249. uint8 j;
  1250. for (j = 0; j < EQI_MAX; j++)
  1251. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  1252. sd->equip_index[j] = i;
  1253. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  1254. if (sd->inventory_data[i])
  1255. sd->weapontype1 = sd->inventory_data[i]->subtype;
  1256. else
  1257. sd->weapontype1 = W_FIST;
  1258. }
  1259. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  1260. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  1261. sd->weapontype2 = sd->inventory_data[i]->subtype;
  1262. else
  1263. sd->weapontype2 = W_FIST;
  1264. }
  1265. }
  1266. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  1267. for (uint8 j = 0; j < EQI_MAX; j++) {
  1268. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  1269. sd->equip_switch_index[j] = i;
  1270. }
  1271. }
  1272. }
  1273. }
  1274. pc_calcweapontype(sd);
  1275. }
  1276. //static int pc_isAllowedCardOn(map_session_data *sd,int s,int eqindex,int flag)
  1277. //{
  1278. // int i;
  1279. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  1280. // std::shared_ptr<item_data> data;
  1281. //
  1282. // //Crafted/made/hatched items.
  1283. // if (itemdb_isspecial(item->card[0]))
  1284. // return 1;
  1285. //
  1286. // /* scan for enchant armor gems */
  1287. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  1288. // s = MAX_SLOTS - 1;
  1289. //
  1290. // ARR_FIND( 0, s, i, item->card[i] && (data = item_db.find(item->card[i])) != NULL && data->flag.no_equip&flag );
  1291. // return( i < s ) ? 0 : 1;
  1292. //}
  1293. /**
  1294. * Check if an item is equiped by player
  1295. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  1296. * @param sd : player session
  1297. * @param nameid : itemid
  1298. * @return 1:yes, 0:no
  1299. */
  1300. bool pc_isequipped(map_session_data *sd, t_itemid nameid)
  1301. {
  1302. uint8 i;
  1303. for( i = 0; i < EQI_MAX; i++ )
  1304. {
  1305. short index = sd->equip_index[i], j;
  1306. if( index < 0 )
  1307. continue;
  1308. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  1309. continue;
  1310. if( !sd->inventory_data[index] )
  1311. continue;
  1312. if( sd->inventory_data[index]->nameid == nameid )
  1313. return true;
  1314. for( j = 0; j < MAX_SLOTS; j++ ){
  1315. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  1316. return true;
  1317. }
  1318. }
  1319. return false;
  1320. }
  1321. /**
  1322. * Check adoption rules
  1323. * @param p1_sd: Player 1
  1324. * @param p2_sd: Player 2
  1325. * @param b_sd: Player that will be adopted
  1326. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  1327. * ADOPT_ALREADY_ADOPTED - Already adopted
  1328. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  1329. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  1330. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  1331. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  1332. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  1333. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  1334. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  1335. */
  1336. enum adopt_responses pc_try_adopt(map_session_data *p1_sd, map_session_data *p2_sd, map_session_data *b_sd)
  1337. {
  1338. if( !p1_sd || !p2_sd || !b_sd )
  1339. return ADOPT_CHARACTER_NOT_FOUND;
  1340. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1341. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1342. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1343. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1344. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1345. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1346. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1347. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1348. // Parents need to have their ring equipped
  1349. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1350. return ADOPT_EQUIP_RINGS;
  1351. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1352. return ADOPT_EQUIP_RINGS;
  1353. // Already adopted a baby
  1354. if( p1_sd->status.child || p2_sd->status.child ) {
  1355. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1356. return ADOPT_MORE_CHILDREN;
  1357. }
  1358. // Parents need at least lvl 70 to adopt
  1359. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1360. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1361. return ADOPT_LEVEL_70;
  1362. }
  1363. if( b_sd->status.partner_id ) {
  1364. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1365. return ADOPT_MARRIED;
  1366. }
  1367. if( !( ( b_sd->status.class_ >= JOB_NOVICE && b_sd->status.class_ <= JOB_THIEF ) || b_sd->status.class_ == JOB_SUPER_NOVICE || b_sd->status.class_ == JOB_SUPER_NOVICE_E ) )
  1368. return ADOPT_NOT_NOVICE;
  1369. return ADOPT_ALLOWED;
  1370. }
  1371. /*==========================================
  1372. * Adoption Process
  1373. *------------------------------------------*/
  1374. bool pc_adoption(map_session_data *p1_sd, map_session_data *p2_sd, map_session_data *b_sd)
  1375. {
  1376. int job, joblevel;
  1377. t_exp jobexp;
  1378. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1379. return false;
  1380. // Preserve current job levels and progress
  1381. joblevel = b_sd->status.job_level;
  1382. jobexp = b_sd->status.job_exp;
  1383. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1384. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1385. { // Success, proceed to configure parents and baby skills
  1386. p1_sd->status.child = b_sd->status.char_id;
  1387. p2_sd->status.child = b_sd->status.char_id;
  1388. b_sd->status.father = p1_sd->status.char_id;
  1389. b_sd->status.mother = p2_sd->status.char_id;
  1390. // Restore progress
  1391. b_sd->status.job_level = joblevel;
  1392. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1393. b_sd->status.job_exp = jobexp;
  1394. clif_updatestatus(b_sd, SP_JOBEXP);
  1395. // Baby Skills
  1396. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1397. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1398. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1399. // Parents Skills
  1400. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1401. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1402. chrif_save(p1_sd, CSAVE_NORMAL);
  1403. chrif_save(p2_sd, CSAVE_NORMAL);
  1404. chrif_save(b_sd, CSAVE_NORMAL);
  1405. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1406. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1407. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1408. return true;
  1409. }
  1410. return false; // Job Change Fail
  1411. }
  1412. static bool pc_job_can_use_item( map_session_data* sd, struct item_data* item ){
  1413. nullpo_retr( false, sd );
  1414. nullpo_retr( false, item );
  1415. // Calculate the required bit to check
  1416. uint64 job = 1ULL << ( sd->class_ & MAPID_BASEMASK );
  1417. size_t index;
  1418. // 2-1
  1419. if( ( sd->class_ & JOBL_2_1 ) != 0 ){
  1420. index = 1;
  1421. // 2-2
  1422. }else if( ( sd->class_ & JOBL_2_2 ) != 0 ){
  1423. index = 2;
  1424. // Basejob
  1425. }else{
  1426. index = 0;
  1427. }
  1428. return ( item->class_base[index] & job ) != 0;
  1429. }
  1430. /*==========================================
  1431. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1432. Returns:
  1433. false : Cannot use/equip
  1434. true : Can use/equip
  1435. * Credits:
  1436. [Inkfish] for first idea
  1437. [Haru] for third-classes extension
  1438. [Cydh] finishing :D
  1439. *------------------------------------------*/
  1440. static bool pc_isItemClass (map_session_data *sd, struct item_data* item) {
  1441. while (1) {
  1442. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_BABY|JOBL_THIRD|JOBL_FOURTH))) //normal classes (no upper, no baby, no third, no fourth)
  1443. break;
  1444. #ifndef RENEWAL
  1445. //allow third classes to use trans. class items
  1446. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1447. break;
  1448. //third-baby classes can use same item too
  1449. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1450. break;
  1451. //don't need to decide specific rules for third-classes?
  1452. //items for third classes can be used for all third classes
  1453. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_UPPER|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1454. break;
  1455. #else
  1456. //trans. classes (exl. third-trans.)
  1457. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1458. break;
  1459. //baby classes (exl. third-baby)
  1460. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1461. break;
  1462. //third classes (exl. third-trans. and baby-third and fourth)
  1463. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)) && !(sd->class_&JOBL_FOURTH))
  1464. break;
  1465. //trans-third classes (exl. fourth)
  1466. if (item->class_upper&ITEMJ_THIRD_UPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_FOURTH))
  1467. break;
  1468. //third-baby classes (exl. fourth)
  1469. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY && !(sd->class_&JOBL_FOURTH))
  1470. break;
  1471. //fourth classes
  1472. if (item->class_upper&ITEMJ_FOURTH && sd->class_&JOBL_FOURTH)
  1473. break;
  1474. #endif
  1475. return false;
  1476. }
  1477. return true;
  1478. }
  1479. /*=================================================
  1480. * Checks if the player can equip the item at index n in inventory.
  1481. * @param sd
  1482. * @param n Item index in inventory
  1483. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1484. *------------------------------------------------*/
  1485. uint8 pc_isequip(map_session_data *sd,int n)
  1486. {
  1487. struct item_data *item;
  1488. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1489. item = sd->inventory_data[n];
  1490. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1491. return ITEM_EQUIP_ACK_OK;
  1492. if(item == NULL)
  1493. return ITEM_EQUIP_ACK_FAIL;
  1494. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1495. return ITEM_EQUIP_ACK_FAILLEVEL;
  1496. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1497. return ITEM_EQUIP_ACK_FAILLEVEL;
  1498. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1499. return ITEM_EQUIP_ACK_FAIL;
  1500. //fail to equip if item is restricted
  1501. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1502. return ITEM_EQUIP_ACK_FAIL;
  1503. if (item->equip&EQP_AMMO) {
  1504. switch (item->subtype) {
  1505. case AMMO_ARROW:
  1506. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1507. clif_msg(sd, ITEM_NEED_BOW);
  1508. return ITEM_EQUIP_ACK_FAIL;
  1509. }
  1510. break;
  1511. case AMMO_DAGGER:
  1512. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1513. return ITEM_EQUIP_ACK_FAIL;
  1514. break;
  1515. case AMMO_BULLET:
  1516. case AMMO_SHELL:
  1517. if (battle_config.ammo_check_weapon && sd->status.weapon != W_REVOLVER && sd->status.weapon != W_RIFLE && sd->status.weapon != W_GATLING && sd->status.weapon != W_SHOTGUN
  1518. #ifdef RENEWAL
  1519. && sd->status.weapon != W_GRENADE
  1520. #endif
  1521. ) {
  1522. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1523. return ITEM_EQUIP_ACK_FAIL;
  1524. }
  1525. break;
  1526. #ifndef RENEWAL
  1527. case AMMO_GRENADE:
  1528. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1529. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1530. return ITEM_EQUIP_ACK_FAIL;
  1531. }
  1532. break;
  1533. #endif
  1534. case AMMO_CANNONBALL:
  1535. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1536. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1537. return ITEM_EQUIP_ACK_FAIL;
  1538. }
  1539. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1540. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1541. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1542. return ITEM_EQUIP_ACK_FAIL;
  1543. }
  1544. break;
  1545. }
  1546. }
  1547. if (sd->sc.count) {
  1548. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.getSCE(SC_STRIPWEAPON)) // Also works with left-hand weapons [DracoRPG]
  1549. return ITEM_EQUIP_ACK_FAIL;
  1550. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.getSCE(SC_STRIPSHIELD))
  1551. return ITEM_EQUIP_ACK_FAIL;
  1552. if(item->equip & EQP_ARMOR && sd->sc.getSCE(SC_STRIPARMOR))
  1553. return ITEM_EQUIP_ACK_FAIL;
  1554. if(item->equip & EQP_HEAD_TOP && sd->sc.getSCE(SC_STRIPHELM))
  1555. return ITEM_EQUIP_ACK_FAIL;
  1556. if(item->equip & EQP_ACC && sd->sc.getSCE(SC__STRIPACCESSORY))
  1557. return ITEM_EQUIP_ACK_FAIL;
  1558. if (item->equip & EQP_ARMS && sd->sc.getSCE(SC__WEAKNESS))
  1559. return ITEM_EQUIP_ACK_FAIL;
  1560. if (item->equip & EQP_SHADOW_GEAR && sd->sc.getSCE(SC_SHADOW_STRIP))
  1561. return ITEM_EQUIP_ACK_FAIL;
  1562. if(item->equip && (sd->sc.getSCE(SC_KYOUGAKU) || sd->sc.getSCE(SC_SUHIDE)))
  1563. return ITEM_EQUIP_ACK_FAIL;
  1564. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_SUPERNOVICE) {
  1565. //Spirit of Super Novice equip bonuses. [Skotlex]
  1566. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1567. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1568. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->weapon_level == 4)
  1569. switch(item->subtype) { //In weapons, the look determines type of weapon.
  1570. case W_DAGGER: //All level 4 - Daggers
  1571. case W_1HSWORD: //All level 4 - 1H Swords
  1572. case W_1HAXE: //All level 4 - 1H Axes
  1573. case W_MACE: //All level 4 - 1H Maces
  1574. case W_STAFF: //All level 4 - 1H Staves
  1575. case W_2HSTAFF: //All level 4 - 2H Staves
  1576. return ITEM_EQUIP_ACK_OK;
  1577. }
  1578. }
  1579. }
  1580. //Not equipable by class. [Skotlex]
  1581. if (!pc_job_can_use_item(sd,item))
  1582. return ITEM_EQUIP_ACK_FAIL;
  1583. if (!pc_isItemClass(sd, item))
  1584. return ITEM_EQUIP_ACK_FAIL;
  1585. return ITEM_EQUIP_ACK_OK;
  1586. }
  1587. /*==========================================
  1588. * No problem with the session id
  1589. * set the status that has been sent from char server
  1590. *------------------------------------------*/
  1591. bool pc_authok(map_session_data *sd, uint32 login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
  1592. {
  1593. t_tick tick = gettick();
  1594. uint32 ip = session[sd->fd]->client_addr;
  1595. sd->login_id2 = login_id2;
  1596. sd->group_id = group_id;
  1597. /* load user permissions */
  1598. pc_group_pc_load(sd);
  1599. memcpy(&sd->status, st, sizeof(*st));
  1600. if (st->sex != sd->status.sex) {
  1601. clif_authfail_fd(sd->fd, 0);
  1602. return false;
  1603. }
  1604. //Set the map-server used job id. [Skotlex]
  1605. uint64 class_ = pc_jobid2mapid(sd->status.class_);
  1606. if (class_ == -1 || !job_db.exists(sd->status.class_)) { //Invalid class?
  1607. ShowError("pc_authok: Invalid class %d for player %s (%d:%d). Class was changed to novice.\n", sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id);
  1608. sd->status.class_ = JOB_NOVICE;
  1609. sd->class_ = MAPID_NOVICE;
  1610. } else
  1611. sd->class_ = class_;
  1612. // Checks and fixes to character status data, that are required
  1613. // in case of configuration change or stuff, which cannot be
  1614. // checked on char-server.
  1615. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1616. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1617. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1618. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1619. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1620. sd->state.connect_new = 1;
  1621. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1622. sd->invincible_timer = INVALID_TIMER;
  1623. sd->npc_timer_id = INVALID_TIMER;
  1624. sd->pvp_timer = INVALID_TIMER;
  1625. sd->expiration_tid = INVALID_TIMER;
  1626. sd->autotrade_tid = INVALID_TIMER;
  1627. sd->respawn_tid = INVALID_TIMER;
  1628. sd->tid_queue_active = INVALID_TIMER;
  1629. sd->macro_detect.timer = INVALID_TIMER;
  1630. sd->skill_keep_using.tid = INVALID_TIMER;
  1631. sd->skill_keep_using.skill_id = 0;
  1632. sd->skill_keep_using.level = 0;
  1633. sd->skill_keep_using.target = 0;
  1634. #ifdef SECURE_NPCTIMEOUT
  1635. // Initialize to defaults/expected
  1636. sd->npc_idle_timer = INVALID_TIMER;
  1637. sd->npc_idle_tick = tick;
  1638. sd->npc_idle_type = NPCT_INPUT;
  1639. sd->state.ignoretimeout = false;
  1640. #endif
  1641. sd->canuseitem_tick = tick;
  1642. sd->canusecashfood_tick = tick;
  1643. sd->canequip_tick = tick;
  1644. sd->cantalk_tick = tick;
  1645. sd->canskill_tick = tick;
  1646. sd->cansendmail_tick = tick;
  1647. sd->idletime = last_tick;
  1648. sd->regen.tick.hp = tick;
  1649. sd->regen.tick.sp = tick;
  1650. for(int i = 0; i < MAX_SPIRITBALL; i++)
  1651. sd->spirit_timer[i] = INVALID_TIMER;
  1652. if (battle_config.item_auto_get)
  1653. sd->state.autoloot = 10000;
  1654. if (battle_config.disp_experience)
  1655. sd->state.showexp = 1;
  1656. if (battle_config.disp_zeny)
  1657. sd->state.showzeny = 1;
  1658. #ifdef VIP_ENABLE
  1659. if (!battle_config.vip_disp_rate)
  1660. sd->vip.disableshowrate = 1;
  1661. #endif
  1662. if (!(battle_config.display_skill_fail&2))
  1663. sd->state.showdelay = 1;
  1664. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1665. memset(&sd->cart, 0, sizeof(struct s_storage));
  1666. memset(&sd->storage, 0, sizeof(struct s_storage));
  1667. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1668. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1669. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1670. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1671. sd->status.option &= ~OPTION_INVISIBLE;
  1672. }
  1673. status_change_init(&sd->bl);
  1674. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1675. unit_dataset(&sd->bl);
  1676. sd->guild_x = -1;
  1677. sd->guild_y = -1;
  1678. sd->delayed_damage = 0;
  1679. // Event Timers
  1680. for( int i = 0; i < MAX_EVENTTIMER; i++ )
  1681. sd->eventtimer[i] = INVALID_TIMER;
  1682. // Rental Timer
  1683. sd->rental_timer = INVALID_TIMER;
  1684. for( int i = 0; i < 3; i++ )
  1685. sd->hate_mob[i] = -1;
  1686. sd->quest_log = NULL;
  1687. sd->num_quests = 0;
  1688. sd->avail_quests = 0;
  1689. sd->save_quest = false;
  1690. sd->count_rewarp = 0;
  1691. sd->mail.pending_weight = 0;
  1692. sd->mail.pending_zeny = 0;
  1693. sd->mail.pending_slots = 0;
  1694. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1695. sd->regs.arrays = NULL;
  1696. sd->vars_dirty = false;
  1697. sd->vars_ok = false;
  1698. sd->vars_received = 0x0;
  1699. //warp player
  1700. enum e_setpos setpos_result = pc_setpos( sd, mapindex_name2id( sd->status.last_point.map ), sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT );
  1701. if( setpos_result != SETPOS_OK ){
  1702. ShowError( "Last_point_map %s not found (error code %d)\n", sd->status.last_point.map, setpos_result );
  1703. // try warping to a default map instead (church graveyard)
  1704. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1705. // if we fail again
  1706. clif_authfail_fd(sd->fd, 0);
  1707. return false;
  1708. }
  1709. }
  1710. clif_inventory_expansion_info( sd );
  1711. clif_authok(sd);
  1712. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1713. sd->die_counter=-1;
  1714. //display login notice
  1715. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1716. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1717. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1718. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1719. sd->status.name, sd->status.account_id, sd->status.char_id,
  1720. CONVIP(ip), sd->group_id);
  1721. // Send friends list
  1722. clif_friendslist_send( *sd );
  1723. if( !changing_mapservers ) {
  1724. if (battle_config.display_version == 1)
  1725. pc_show_version(sd);
  1726. // Message of the Day [Valaris]
  1727. for(int i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1728. if (battle_config.motd_type)
  1729. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1730. else
  1731. clif_displaymessage(sd->fd, motd_text[i]);
  1732. }
  1733. if (expiration_time != 0)
  1734. sd->expiration_time = expiration_time;
  1735. /**
  1736. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1737. **/
  1738. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1739. }
  1740. pc_validate_skill(sd);
  1741. /* [Ind] */
  1742. sd->sc_display = NULL;
  1743. sd->sc_display_count = 0;
  1744. // Player has not yet received the CashShop list
  1745. sd->status.cashshop_sent = false;
  1746. sd->last_addeditem_index = -1;
  1747. sd->bonus_script.head = NULL;
  1748. sd->bonus_script.count = 0;
  1749. // Initialize BG queue
  1750. sd->bg_queue_id = 0;
  1751. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  1752. sd->hatEffects = {};
  1753. #endif
  1754. sd->catch_target_class = PET_CATCH_FAIL;
  1755. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1756. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1757. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1758. clif_updatestatus(sd, SP_BASEEXP);
  1759. }
  1760. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1761. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1762. clif_updatestatus(sd, SP_JOBEXP);
  1763. }
  1764. // Request all registries (auth is considered completed whence they arrive)
  1765. intif_request_registry(sd,7);
  1766. return true;
  1767. }
  1768. /*==========================================
  1769. * Closes a connection because it failed to be authenticated from the char server.
  1770. *------------------------------------------*/
  1771. void pc_authfail(map_session_data *sd)
  1772. {
  1773. clif_authfail_fd(sd->fd, 0);
  1774. return;
  1775. }
  1776. /**
  1777. * Player register a bl as hatred
  1778. * @param sd : player session
  1779. * @param pos : hate position [0;2]
  1780. * @param bl : target bl
  1781. * @return false:failed, true:success
  1782. */
  1783. bool pc_set_hate_mob(map_session_data *sd, int pos, struct block_list *bl)
  1784. {
  1785. int class_;
  1786. if (!sd || !bl || pos < 0 || pos > 2)
  1787. return false;
  1788. if (sd->hate_mob[pos] != -1)
  1789. { //Can't change hate targets.
  1790. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1791. return false;
  1792. }
  1793. class_ = status_get_class(bl);
  1794. if (!pcdb_checkid(class_)) {
  1795. unsigned int max_hp = status_get_max_hp(bl);
  1796. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1797. return false;
  1798. if (pos != status_get_size(bl))
  1799. return false; //Wrong size
  1800. }
  1801. sd->hate_mob[pos] = class_;
  1802. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1803. clif_hate_info(sd, pos, class_, 1);
  1804. return true;
  1805. }
  1806. /*==========================================
  1807. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1808. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1809. * See intif_parse_StorageReceived() for item operations [lighta]
  1810. *------------------------------------------*/
  1811. void pc_reg_received(map_session_data *sd)
  1812. {
  1813. uint8 i;
  1814. sd->vars_ok = true;
  1815. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1816. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1817. sd->change_level_4th = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE4TH_VAR)));
  1818. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1819. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1820. if (msg_checklangtype(sd->langtype,true) < 0)
  1821. sd->langtype = 0; //invalid langtype reset to default
  1822. // Cash shop
  1823. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1824. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1825. // Cooking Exp
  1826. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1827. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1828. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1829. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1830. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1831. }
  1832. if (battle_config.feature_banking)
  1833. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1834. if (battle_config.feature_roulette) {
  1835. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1836. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1837. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1838. }
  1839. sd->roulette.prizeIdx = -1;
  1840. //SG map and mob read [Komurka]
  1841. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1842. uint16 j;
  1843. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1844. sd->feel_map[i].index = j;
  1845. sd->feel_map[i].m = map_mapindex2mapid(j);
  1846. } else {
  1847. sd->feel_map[i].index = 0;
  1848. sd->feel_map[i].m = -1;
  1849. }
  1850. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1851. }
  1852. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1853. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1854. sd->cloneskill_idx = skill_get_index(skid);
  1855. if (sd->cloneskill_idx > 0) {
  1856. sd->status.skill[sd->cloneskill_idx].id = skid;
  1857. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1858. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1859. sd->status.skill[sd->cloneskill_idx].lv = i;
  1860. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1861. }
  1862. }
  1863. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1864. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1865. sd->reproduceskill_idx = skill_get_index(skid);
  1866. if (sd->reproduceskill_idx > 0) {
  1867. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1868. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1869. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1870. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1871. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1872. }
  1873. }
  1874. //Weird... maybe registries were reloaded?
  1875. if (sd->state.active)
  1876. return;
  1877. sd->state.active = 1;
  1878. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1879. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1880. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1881. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1882. // Restore IM_CHAR instance to the player
  1883. for (const auto &instance : instances) {
  1884. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1885. sd->instance_id = instance.first;
  1886. break;
  1887. }
  1888. }
  1889. if (sd->status.party_id > 0)
  1890. party_member_joined(sd);
  1891. if (sd->status.guild_id > 0)
  1892. guild_member_joined(sd);
  1893. if (sd->status.clan_id > 0)
  1894. clan_member_joined(sd);
  1895. #if !( PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410 )
  1896. // Before those clients you could send out the instance info even when the client was still loading the map, afterwards you need to send it later
  1897. clif_instance_info( *sd );
  1898. #endif
  1899. // pet
  1900. if (sd->status.pet_id > 0)
  1901. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1902. // Homunculus [albator]
  1903. if( sd->status.hom_id > 0 )
  1904. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1905. if( sd->status.mer_id > 0 )
  1906. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1907. if( sd->status.ele_id > 0 )
  1908. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1909. map_addiddb(&sd->bl);
  1910. map_delnickdb(sd->status.char_id, sd->status.name);
  1911. if (!chrif_auth_finished(sd))
  1912. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1913. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1914. chrif_bsdata_request(sd->status.char_id);
  1915. #ifdef VIP_ENABLE
  1916. sd->vip.time = 0;
  1917. sd->vip.enabled = 0;
  1918. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1919. #endif
  1920. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1921. intif_request_questlog(sd);
  1922. if (battle_config.feature_achievement) {
  1923. sd->achievement_data.total_score = 0;
  1924. sd->achievement_data.level = 0;
  1925. sd->achievement_data.save = false;
  1926. sd->achievement_data.count = 0;
  1927. sd->achievement_data.incompleteCount = 0;
  1928. sd->achievement_data.achievements = NULL;
  1929. intif_request_achievements(sd->status.char_id);
  1930. }
  1931. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1932. sd->state.connect_new = 1;
  1933. clif_parse_LoadEndAck(sd->fd, sd);
  1934. if( pc_isinvisible( sd ) ){
  1935. sd->vd.class_ = JT_INVISIBLE;
  1936. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1937. // decrement the number of pvp players on the map
  1938. map_getmapdata( sd->bl.m )->users_pvp--;
  1939. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1940. // unregister the player for ranking
  1941. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1942. sd->pvp_timer = INVALID_TIMER;
  1943. }
  1944. clif_changeoption( &sd->bl );
  1945. }
  1946. }
  1947. channel_autojoin(sd);
  1948. }
  1949. static int pc_calc_skillpoint(map_session_data* sd)
  1950. {
  1951. uint16 i, skill_point = 0;
  1952. nullpo_ret(sd);
  1953. for(i = 1; i < MAX_SKILL; i++) {
  1954. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1955. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1956. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1957. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1958. )
  1959. {
  1960. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1961. skill_point += sd->status.skill[i].lv;
  1962. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1963. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1964. }
  1965. }
  1966. }
  1967. return skill_point;
  1968. }
  1969. static bool pc_grant_allskills(map_session_data *sd, bool addlv) {
  1970. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1971. return false;
  1972. /**
  1973. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1974. * skill tree and since they have no icons they'll give resource errors
  1975. * Get ALL skills except npc/guild ones. [Skotlex]
  1976. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1977. **/
  1978. for (const auto &skill : skill_db) {
  1979. uint16 skill_id = skill.second->nameid;
  1980. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1981. continue;
  1982. switch (skill_id) {
  1983. case SM_SELFPROVOKE:
  1984. case AB_DUPLELIGHT_MELEE:
  1985. case AB_DUPLELIGHT_MAGIC:
  1986. case WL_CHAINLIGHTNING_ATK:
  1987. case WL_TETRAVORTEX_FIRE:
  1988. case WL_TETRAVORTEX_WATER:
  1989. case WL_TETRAVORTEX_WIND:
  1990. case WL_TETRAVORTEX_GROUND:
  1991. case WL_SUMMON_ATK_FIRE:
  1992. case WL_SUMMON_ATK_WIND:
  1993. case WL_SUMMON_ATK_WATER:
  1994. case WL_SUMMON_ATK_GROUND:
  1995. case LG_OVERBRAND_BRANDISH:
  1996. case LG_OVERBRAND_PLUSATK:
  1997. case WM_SEVERE_RAINSTORM_MELEE:
  1998. case RL_R_TRIP_PLUSATK:
  1999. case SG_DEVIL:
  2000. case MO_TRIPLEATTACK:
  2001. case RG_SNATCHER:
  2002. continue;
  2003. default:
  2004. {
  2005. uint8 lv = (uint8)skill_get_max(skill_id);
  2006. if (lv > 0) {
  2007. uint16 idx = skill_get_index(skill_id);
  2008. sd->status.skill[idx].id = skill_id;
  2009. if (addlv)
  2010. sd->status.skill[idx].lv = lv;
  2011. }
  2012. }
  2013. break;
  2014. }
  2015. }
  2016. return true;
  2017. }
  2018. /*==========================================
  2019. * Calculation of skill level.
  2020. * @param sd
  2021. *------------------------------------------*/
  2022. void pc_calc_skilltree(map_session_data *sd)
  2023. {
  2024. nullpo_retv(sd);
  2025. uint64 job = pc_calc_skilltree_normalize_job(sd);
  2026. int class_ = pc_mapid2jobid(job, sd->status.sex);
  2027. if( class_ == -1 )
  2028. { //Unable to normalize job??
  2029. ShowError( "pc_calc_skilltree: Unable to normalize job %s(%d) for character %s (%d:%d)\n", job_name( sd->status.class_ ), sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id );
  2030. return;
  2031. }
  2032. uint16 job_id = class_;
  2033. class_ = pc_class2idx(class_);
  2034. for (const auto &skill : skill_db) {
  2035. uint16 skill_id = skill.second->nameid;
  2036. uint16 idx = skill_get_index(skill_id);
  2037. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  2038. sd->status.skill[idx].id = 0; //First clear skills.
  2039. /* permanent skills that must be re-checked */
  2040. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  2041. if (skill_id == 0) {
  2042. sd->status.skill[idx].id = 0;
  2043. sd->status.skill[idx].lv = 0;
  2044. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2045. continue;
  2046. }
  2047. switch (skill_id) {
  2048. case NV_TRICKDEAD:
  2049. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  2050. sd->status.skill[idx].id = 0;
  2051. sd->status.skill[idx].lv = 0;
  2052. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2053. }
  2054. break;
  2055. }
  2056. }
  2057. }
  2058. for (const auto &skill : skill_db) {
  2059. uint16 idx = skill_get_index(skill.second->nameid);
  2060. // Restore original level of skills after deleting earned skills.
  2061. if( sd->status.skill[idx].flag != SKILL_FLAG_PERMANENT && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED && sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED ) {
  2062. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2063. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2064. }
  2065. }
  2066. // Removes Taekwon Ranker skill bonus
  2067. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  2068. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(JOB_TAEKWON);
  2069. if (tree != nullptr && !tree->skills.empty()) {
  2070. for (const auto &it : tree->skills) {
  2071. uint16 sk_idx = skill_get_index(it.first);
  2072. if (sk_idx == 0)
  2073. continue;
  2074. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  2075. if (it.first == NV_BASIC || it.first == NV_FIRSTAID || it.first == WE_CALLBABY)
  2076. continue;
  2077. sd->status.skill[sk_idx].id = 0;
  2078. }
  2079. }
  2080. }
  2081. }
  2082. // Grant all skills
  2083. pc_grant_allskills(sd, false);
  2084. int flag;
  2085. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2086. do {
  2087. flag = 0;
  2088. if (tree == nullptr || tree->skills.empty())
  2089. break;
  2090. for (const auto &skillsit : tree->skills) {
  2091. bool fail = false;
  2092. uint16 skid = skillsit.first;
  2093. uint16 sk_idx = skill_get_index(skid);
  2094. if (sd->status.skill[sk_idx].id)
  2095. continue; //Skill already known.
  2096. if (!battle_config.skillfree) {
  2097. // Checking required skills
  2098. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2099. if (entry != nullptr && !entry->need.empty()) {
  2100. for (const auto &it : entry->need) {
  2101. uint16 sk_need_id = it.first;
  2102. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2103. if (sk_need_idx > 0) {
  2104. uint16 sk_need = sk_need_id;
  2105. if (sd->status.skill[sk_need_idx].id == 0 || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_PLAGIARIZED)
  2106. sk_need = 0; //Not learned.
  2107. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  2108. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2109. else
  2110. sk_need = pc_checkskill(sd,sk_need_id);
  2111. if (sk_need < it.second) {
  2112. fail = true;
  2113. break;
  2114. }
  2115. }
  2116. }
  2117. }
  2118. if (sd->status.base_level < entry->baselv) { //We need to get the actual class in this case
  2119. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2120. c_ = pc_class2idx(c_);
  2121. if (class_ == c_ || (class_ != c_ && sd->status.base_level < entry->baselv))
  2122. fail = true; // base level requirement wasn't satisfied
  2123. }
  2124. if (sd->status.job_level < entry->joblv) { //We need to get the actual class in this case
  2125. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2126. c_ = pc_class2idx(c_);
  2127. if (class_ == c_ || (class_ != c_ && sd->status.job_level < entry->joblv))
  2128. fail = true; // job level requirement wasn't satisfied
  2129. }
  2130. }
  2131. if (!fail) {
  2132. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2133. if (!sd->status.skill[sk_idx].lv && (
  2134. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2135. skill->inf2[INF2_ISWEDDING] ||
  2136. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2137. ))
  2138. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  2139. sd->status.skill[sk_idx].id = skid;
  2140. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  2141. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  2142. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  2143. }
  2144. flag = 1; // skill list has changed, perform another pass
  2145. }
  2146. }
  2147. } while(flag);
  2148. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  2149. uint16 skid = 0;
  2150. /* Taekwon Ranker Bonus Skill Tree
  2151. ============================================
  2152. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  2153. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  2154. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  2155. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2156. if (tree != nullptr && !tree->skills.empty()) {
  2157. for (const auto &it : tree->skills) {
  2158. skid = it.first;
  2159. uint16 sk_idx = skill_get_index(skid);
  2160. if (sk_idx == 0)
  2161. continue;
  2162. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  2163. continue; //Do not include Quest/Wedding skills.
  2164. if( sd->status.skill[sk_idx].id == 0 ) {
  2165. sd->status.skill[sk_idx].id = skid;
  2166. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  2167. } else if( skid != NV_BASIC )
  2168. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  2169. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  2170. }
  2171. }
  2172. }
  2173. // Enable Bard/Dancer spirit linked skills.
  2174. if (sd->sc.count && sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_BARDDANCER) {
  2175. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  2176. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  2177. { BA_POEMBRAGI, DC_FORTUNEKISS },
  2178. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  2179. for (const auto &skill : linked_skills) {
  2180. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  2181. continue;
  2182. // Tag it as a non-savable, non-uppable, bonus skill
  2183. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  2184. }
  2185. }
  2186. }
  2187. //Checks if you can learn a new skill after having leveled up a skill.
  2188. static void pc_check_skilltree(map_session_data *sd)
  2189. {
  2190. int flag = 0;
  2191. if (battle_config.skillfree)
  2192. return; //Function serves no purpose if this is set
  2193. int c = pc_mapid2jobid( pc_calc_skilltree_normalize_job( sd ), sd->status.sex );
  2194. if (c == -1) { //Unable to normalize job??
  2195. ShowError("pc_check_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id);
  2196. return;
  2197. }
  2198. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(c);
  2199. if (tree == nullptr || tree->skills.empty())
  2200. return;
  2201. do {
  2202. flag = 0;
  2203. for (const auto &skillsit : tree->skills) {
  2204. uint16 skid = skillsit.first;
  2205. uint16 sk_idx = skill_get_index(skid);
  2206. bool fail = false;
  2207. if (sd->status.skill[sk_idx].id) //Already learned
  2208. continue;
  2209. // Checking required skills
  2210. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2211. if (entry != nullptr && !entry->need.empty()) {
  2212. for (const auto &it : entry->need) {
  2213. uint16 sk_need_id = it.first;
  2214. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2215. if (sk_need_id > 0) {
  2216. short sk_need = sk_need_id;
  2217. if (sd->status.skill[sk_need_idx].id == 0 || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_PLAGIARIZED)
  2218. sk_need = 0; //Not learned.
  2219. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  2220. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2221. else
  2222. sk_need = pc_checkskill(sd,sk_need_id);
  2223. if (sk_need < it.second) {
  2224. fail = true;
  2225. break;
  2226. }
  2227. }
  2228. }
  2229. }
  2230. if( fail )
  2231. continue;
  2232. if (sd->status.base_level < entry->baselv || sd->status.job_level < entry->joblv)
  2233. continue;
  2234. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2235. if( !sd->status.skill[sk_idx].lv && (
  2236. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2237. skill->inf2[INF2_ISWEDDING] ||
  2238. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2239. ) )
  2240. continue; //Cannot be learned via normal means.
  2241. sd->status.skill[sk_idx].id = skid;
  2242. flag = 1;
  2243. }
  2244. } while(flag);
  2245. }
  2246. // Make sure all the skills are in the correct condition
  2247. // before persisting to the backend.. [MouseJstr]
  2248. void pc_clean_skilltree(map_session_data *sd)
  2249. {
  2250. uint16 i;
  2251. for (i = 0; i < MAX_SKILL; i++){
  2252. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  2253. sd->status.skill[i].id = 0;
  2254. sd->status.skill[i].lv = 0;
  2255. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2256. }
  2257. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  2258. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  2259. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2260. }
  2261. }
  2262. }
  2263. uint64 pc_calc_skilltree_normalize_job_sub( map_session_data *sd ){
  2264. int skill_point = pc_calc_skillpoint( sd );
  2265. if( sd->class_ & MAPID_SUMMONER ){
  2266. // Novice's skill points for basic skill.
  2267. std::shared_ptr<s_job_info> summoner_job = job_db.find( JOB_SUMMONER );
  2268. int summoner_skills = summoner_job->max_job_level - 1;
  2269. if( skill_point < summoner_skills ){
  2270. return MAPID_SUMMONER;
  2271. }
  2272. skill_point -= summoner_skills;
  2273. }else{
  2274. // Novice's skill points for basic skill.
  2275. std::shared_ptr<s_job_info> novice_job = job_db.find( JOB_NOVICE );
  2276. int novice_skills = novice_job->max_job_level - 1;
  2277. if( skill_point < novice_skills ){
  2278. return MAPID_NOVICE;
  2279. }
  2280. skill_point -= novice_skills;
  2281. }
  2282. // 1st Class Job LV Check
  2283. if( sd->class_ & JOBL_2 && ( sd->class_ & MAPID_UPPERMASK ) != MAPID_SUPER_NOVICE ){
  2284. uint64 mapid_1st = sd->class_ & MAPID_BASEMASK;
  2285. int class_1st = pc_mapid2jobid( mapid_1st, sd->status.sex );
  2286. if( !sd->change_level_2nd ){
  2287. if( class_1st >= JOB_NOVICE ){
  2288. sd->change_level_2nd = job_db.find( class_1st )->max_job_level;
  2289. }else{
  2290. sd->change_level_2nd = 0;
  2291. }
  2292. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  2293. }
  2294. if( class_1st > 0 && skill_point < ( sd->change_level_2nd - 1 ) ){
  2295. return mapid_1st;
  2296. }
  2297. skill_point -= ( sd->change_level_2nd - 1 );
  2298. }
  2299. // 2nd Class Job LV Check
  2300. if( sd->class_ & JOBL_THIRD && (sd->class_ & MAPID_THIRDMASK) != MAPID_SUPER_NOVICE_E ){
  2301. uint64 mapid_2nd = sd->class_ & MAPID_UPPERMASK;
  2302. int class_2nd = pc_mapid2jobid( mapid_2nd, sd->status.sex );
  2303. if( !sd->change_level_3rd ){
  2304. if( class_2nd >= JOB_NOVICE ){
  2305. sd->change_level_3rd = job_db.find( class_2nd )->max_job_level;
  2306. }else{
  2307. sd->change_level_3rd = 0;
  2308. }
  2309. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  2310. }
  2311. if( class_2nd > 0 && skill_point < ( sd->change_level_3rd - 1 ) ){
  2312. return mapid_2nd;
  2313. }
  2314. skill_point -= ( sd->change_level_3rd - 1 );
  2315. }
  2316. // 3rd Class Job LV Check
  2317. if( sd->class_ & JOBL_FOURTH ){
  2318. uint64 mapid_3rd = sd->class_ & MAPID_THIRDMASK | JOBL_THIRD;
  2319. int class_3rd = pc_mapid2jobid( mapid_3rd, sd->status.sex );
  2320. if( !sd->change_level_4th ){
  2321. if( class_3rd >= JOB_NOVICE ){
  2322. sd->change_level_4th = job_db.find( class_3rd )->max_job_level;
  2323. }else{
  2324. sd->change_level_4th = 0;
  2325. }
  2326. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ), sd->change_level_4th );
  2327. }
  2328. if( class_3rd > 0 && skill_point < ( sd->change_level_4th - 1 ) ){
  2329. return mapid_3rd;
  2330. }
  2331. skill_point -= ( sd->change_level_4th - 1 );
  2332. }
  2333. return sd->class_;
  2334. }
  2335. uint64 pc_calc_skilltree_normalize_job( map_session_data *sd ){
  2336. if( !battle_config.skillup_limit || pc_has_permission( sd, PC_PERM_ALL_SKILL ) ){
  2337. return sd->class_;
  2338. }
  2339. uint64 c = pc_calc_skilltree_normalize_job_sub( sd );
  2340. // Special Masks
  2341. if( sd->class_&JOBL_UPPER ){
  2342. if( pc_mapid2jobid( c | JOBL_UPPER, sd->status.sex ) >= JOB_NOVICE ){
  2343. c |= JOBL_UPPER;// Rebirth Job
  2344. }
  2345. }
  2346. if( sd->class_&JOBL_BABY ){
  2347. if( pc_mapid2jobid( c | JOBL_BABY, sd->status.sex ) >= JOB_NOVICE ){
  2348. c |= JOBL_BABY;// Baby Job
  2349. }
  2350. }
  2351. return c;
  2352. }
  2353. /*==========================================
  2354. * Updates the weight status
  2355. *------------------------------------------
  2356. * 1: overweight 50% for pre-renewal and 70% for renewal
  2357. * 2: overweight 90%
  2358. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  2359. */
  2360. void pc_updateweightstatus(map_session_data *sd)
  2361. {
  2362. int old_overweight;
  2363. int new_overweight;
  2364. nullpo_retv(sd);
  2365. old_overweight = (sd->sc.getSCE(SC_WEIGHT90)) ? 2 : (sd->sc.getSCE(SC_WEIGHT50)) ? 1 : 0;
  2366. #ifdef RENEWAL
  2367. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  2368. #else
  2369. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  2370. #endif
  2371. if( old_overweight == new_overweight )
  2372. return; // no change
  2373. // stop old status change
  2374. if( old_overweight == 1 )
  2375. status_change_end(&sd->bl, SC_WEIGHT50);
  2376. else if( old_overweight == 2 )
  2377. status_change_end(&sd->bl, SC_WEIGHT90);
  2378. // start new status change
  2379. if( new_overweight == 1 )
  2380. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  2381. else if( new_overweight == 2 )
  2382. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  2383. // update overweight status
  2384. sd->regen.state.overweight = new_overweight;
  2385. }
  2386. int pc_disguise(map_session_data *sd, int class_)
  2387. {
  2388. if (!class_ && !sd->disguise)
  2389. return 0;
  2390. if (class_ && sd->disguise == class_)
  2391. return 0;
  2392. if(pc_isinvisible(sd))
  2393. { //Character is invisible. Stealth class-change. [Skotlex]
  2394. sd->disguise = class_; //viewdata is set on uncloaking.
  2395. return 2;
  2396. }
  2397. if (sd->bl.prev != NULL) {
  2398. pc_stop_walking(sd, 0);
  2399. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  2400. }
  2401. if (!class_) {
  2402. sd->disguise = 0;
  2403. class_ = sd->status.class_;
  2404. } else
  2405. sd->disguise=class_;
  2406. status_set_viewdata(&sd->bl, class_);
  2407. clif_changeoption(&sd->bl);
  2408. if (sd->bl.prev != NULL) {
  2409. clif_spawn(&sd->bl);
  2410. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2411. { //It seems the cart info is lost on undisguise.
  2412. clif_cartlist(sd);
  2413. clif_updatestatus(sd,SP_CARTINFO);
  2414. }
  2415. if (sd->chatID) {
  2416. struct chat_data* cd;
  2417. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  2418. clif_dispchat(cd,0);
  2419. }
  2420. }
  2421. return 1;
  2422. }
  2423. /// Show error message
  2424. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2425. /// Check for valid Element, break & show error message if invalid Element
  2426. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2427. /// Check for valid Race, break & show error message if invalid Race
  2428. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2429. /// Check for valid Race2, break & show error message if invalid Race2
  2430. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2431. /// Check for valid Class, break & show error message if invalid Class
  2432. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2433. /// Check for valid Size, break & show error message if invalid Size
  2434. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2435. /// Check for valid SC, break & show error message if invalid SC
  2436. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2437. /**
  2438. * Add auto spell bonus for player while attacking/attacked
  2439. * @param spell: Spell array
  2440. * @param id: Skill to cast
  2441. * @param lv: Skill level
  2442. * @param rate: Success chance
  2443. * @param battle_flag: Battle flag
  2444. * @param card_id: Used to prevent card stacking
  2445. * @param flag: Flags used for extra arguments
  2446. * &0: forces the skill to be casted on self, rather than on the target
  2447. * &1: forces the skill to be casted on target, rather than self
  2448. * &2: random skill level in [1..lv] is chosen
  2449. */
  2450. static void pc_bonus_autospell(std::vector<s_autospell> &spell, uint16 id, uint16 lv, short rate, short battle_flag, t_itemid card_id, uint8 flag)
  2451. {
  2452. if (spell.size() == MAX_PC_BONUS) {
  2453. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2454. return;
  2455. }
  2456. if (!rate)
  2457. return;
  2458. if (!(battle_flag&BF_RANGEMASK))
  2459. battle_flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2460. if (!(battle_flag&BF_WEAPONMASK))
  2461. battle_flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2462. if (!(battle_flag&BF_SKILLMASK)) {
  2463. if (battle_flag&(BF_MAGIC | BF_MISC))
  2464. battle_flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2465. if (battle_flag&BF_WEAPON)
  2466. battle_flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2467. }
  2468. for (auto &it : spell) {
  2469. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.battle_flag == battle_flag && it.flag == flag) {
  2470. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2471. return;
  2472. it.rate = util::safe_addition_cap(it.rate, rate, (short)1000);
  2473. return;
  2474. }
  2475. }
  2476. struct s_autospell entry = {};
  2477. if (rate < -1000 || rate > 1000)
  2478. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -1000~1000 range, capping.\n", rate);
  2479. entry.id = id;
  2480. entry.lv = lv;
  2481. entry.rate = cap_value(rate, -1000, 1000);
  2482. entry.battle_flag = battle_flag;
  2483. entry.card_id = card_id;
  2484. entry.flag = flag;
  2485. spell.push_back(entry);
  2486. }
  2487. /**
  2488. * Add auto spell bonus for player while using skills
  2489. * @param spell: Spell array
  2490. * @param src_skill: Trigger skill
  2491. * @param id: Support or target type
  2492. * @param lv: Skill level
  2493. * @param rate: Success chance
  2494. * @param card_id: Used to prevent card stacking
  2495. * @param flag: Flags used for extra arguments
  2496. * &1: forces the skill to be casted on self, rather than on the target of skill
  2497. * &2: random skill level in [1..lv] is chosen
  2498. */
  2499. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, uint16 src_skill, uint16 id, uint16 lv, short rate, t_itemid card_id, uint8 flag)
  2500. {
  2501. if (spell.size() == MAX_PC_BONUS) {
  2502. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2503. return;
  2504. }
  2505. if (!rate)
  2506. return;
  2507. struct s_autospell entry = {};
  2508. if (rate < -1000 || rate > 1000)
  2509. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -1000~1000 range, capping.\n", rate);
  2510. entry.trigger_skill = src_skill;
  2511. entry.id = id;
  2512. entry.lv = lv;
  2513. entry.rate = cap_value(rate, -1000, 1000);
  2514. entry.card_id = card_id;
  2515. entry.flag = flag;
  2516. spell.push_back(entry);
  2517. }
  2518. /**
  2519. * Add inflict effect bonus for player while attacking/attacked
  2520. * @param effect: Effect array
  2521. * @param sc: SC/Effect type
  2522. * @param rate: Success chance
  2523. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2524. * @param flag: Target flag
  2525. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2526. */
  2527. static void pc_bonus_addeff(std::vector<s_addeffect> &effect, enum sc_type sc, int rate, short arrow_rate, unsigned char flag, unsigned int duration)
  2528. {
  2529. if (effect.size() == MAX_PC_BONUS) {
  2530. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2531. return;
  2532. }
  2533. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2534. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2535. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2536. flag |= ATF_TARGET; //Default target: enemy.
  2537. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2538. flag |= ATF_WEAPON; //Default type: weapon.
  2539. if (!duration)
  2540. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2541. for (auto &it : effect) {
  2542. if (it.sc == sc && it.flag == flag) {
  2543. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2544. it.arrow_rate = util::safe_addition_cap(it.arrow_rate, arrow_rate, (short)SHRT_MAX);
  2545. it.duration = umax(it.duration, duration);
  2546. return;
  2547. }
  2548. }
  2549. struct s_addeffect entry = {};
  2550. entry.sc = sc;
  2551. entry.rate = rate;
  2552. entry.arrow_rate = arrow_rate;
  2553. entry.flag = flag;
  2554. entry.duration = duration;
  2555. effect.push_back(entry);
  2556. }
  2557. /**
  2558. * Add inflict effect bonus for player while attacking using skill
  2559. * @param effect: Effect array
  2560. * @param sc: SC/Effect type
  2561. * @param rate: Success chance
  2562. * @param skill_id: Skill to cast
  2563. * @param target: Target type
  2564. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2565. */
  2566. static void pc_bonus_addeff_onskill(std::vector<s_addeffectonskill> &effect, enum sc_type sc, int rate, short skill_id, unsigned char target, unsigned int duration)
  2567. {
  2568. if (effect.size() == MAX_PC_BONUS) {
  2569. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2570. return;
  2571. }
  2572. if (!duration)
  2573. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2574. for (auto &it : effect) {
  2575. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2576. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2577. it.duration = umax(it.duration, duration);
  2578. return;
  2579. }
  2580. }
  2581. struct s_addeffectonskill entry = {};
  2582. entry.sc = sc;
  2583. entry.rate = rate;
  2584. entry.skill_id = skill_id;
  2585. entry.target = target;
  2586. entry.duration = duration;
  2587. effect.push_back(entry);
  2588. }
  2589. /**
  2590. * Adjust/add drop rate modifier for player
  2591. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2592. * @param nameid: item id that will be dropped
  2593. * @param group: group id
  2594. * @param class_: target class
  2595. * @param race: target race. if < 0, means monster_id
  2596. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2597. */
  2598. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int class_, short race, int rate)
  2599. {
  2600. if (!nameid && !group) {
  2601. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2602. return;
  2603. }
  2604. if (nameid && !item_db.exists(nameid)) {
  2605. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2606. return;
  2607. }
  2608. if (group && !itemdb_group.exists(group)) {
  2609. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2610. return;
  2611. }
  2612. if (drop.size() == MAX_PC_BONUS) {
  2613. ShowWarning("pc_bonus_item_drop: Reached max (%d) number of added drops per character! (nameid: %u group: %d class_: %d race: %d rate: %d)\n", MAX_PC_BONUS, nameid, group, class_, race, rate);
  2614. return;
  2615. }
  2616. //Apply config rate adjustment settings.
  2617. if (rate >= 0) { //Absolute drop.
  2618. if (battle_config.item_rate_adddrop != 100)
  2619. rate = rate*battle_config.item_rate_adddrop/100;
  2620. if (rate < battle_config.item_drop_adddrop_min)
  2621. rate = battle_config.item_drop_adddrop_min;
  2622. else if (rate > battle_config.item_drop_adddrop_max)
  2623. rate = battle_config.item_drop_adddrop_max;
  2624. } else { //Relative drop, max/min limits are applied at drop time.
  2625. if (battle_config.item_rate_adddrop != 100)
  2626. rate = rate*battle_config.item_rate_adddrop/100;
  2627. if (rate > -1)
  2628. rate = -1;
  2629. }
  2630. for (auto &it : drop) {
  2631. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2632. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2633. it.rate = util::safe_addition_cap(it.rate, rate, 10000);
  2634. return;
  2635. }
  2636. }
  2637. struct s_add_drop entry = {};
  2638. if (rate < -10000 || rate > 10000)
  2639. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2640. entry.nameid = nameid;
  2641. entry.group = group;
  2642. entry.race = race;
  2643. entry.class_ = class_;
  2644. entry.rate = cap_value(rate, -10000, 10000);
  2645. drop.push_back(entry);
  2646. }
  2647. s_autobonus::~s_autobonus(){
  2648. if( this->active != INVALID_TIMER ){
  2649. delete_timer( this->active, pc_endautobonus );
  2650. this->active = INVALID_TIMER;
  2651. }
  2652. if( this->bonus_script != nullptr ){
  2653. aFree( this->bonus_script );
  2654. this->bonus_script = nullptr;
  2655. }
  2656. if( this->other_script != nullptr ){
  2657. aFree( this->other_script );
  2658. this->other_script = nullptr;
  2659. }
  2660. }
  2661. /**
  2662. * Add autobonus to player when attacking/attacked
  2663. * @param bonus: Bonus array
  2664. * @param script: Script to execute
  2665. * @param rate: Success chance
  2666. * @param dur: Duration
  2667. * @param flag: Battle flag/skill
  2668. * @param other_script: Secondary script to execute
  2669. * @param pos: Item equip position
  2670. * @param onskill: Skill used to trigger autobonus
  2671. * @return True on success or false otherwise
  2672. */
  2673. bool pc_addautobonus(std::vector<std::shared_ptr<s_autobonus>> &bonus, const char *script, short rate, unsigned int dur, uint16 flag, const char *other_script, unsigned int pos, bool onskill){
  2674. // Check if the same bonus already exists
  2675. for( std::shared_ptr<s_autobonus> autobonus : bonus ){
  2676. // Compare based on position and bonus script
  2677. if( autobonus->pos == pos && strcmp( script, autobonus->bonus_script ) == 0 ){
  2678. return false;
  2679. }
  2680. }
  2681. if (bonus.size() == MAX_PC_BONUS) {
  2682. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2683. return false;
  2684. }
  2685. if (!onskill) {
  2686. if (!(flag&BF_RANGEMASK))
  2687. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2688. if (!(flag&BF_WEAPONMASK))
  2689. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2690. if (!(flag&BF_SKILLMASK)) {
  2691. if (flag&(BF_MAGIC | BF_MISC))
  2692. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2693. if (flag&BF_WEAPON)
  2694. flag |= BF_NORMAL | BF_SKILL;
  2695. }
  2696. }
  2697. std::shared_ptr<s_autobonus> entry = std::make_shared<s_autobonus>();
  2698. if (rate < -10000 || rate > 10000)
  2699. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2700. entry->rate = cap_value(rate, -10000, 10000);
  2701. entry->duration = dur;
  2702. entry->active = INVALID_TIMER;
  2703. entry->atk_type = flag;
  2704. entry->pos = pos;
  2705. entry->bonus_script = aStrdup(script);
  2706. entry->other_script = (other_script ? aStrdup(other_script) : NULL);
  2707. bonus.push_back(entry);
  2708. return true;
  2709. }
  2710. /**
  2711. * Remove an autobonus from player
  2712. * @param sd: Player data
  2713. * @param bonus: Autobonus array
  2714. * @param restore: Run script on clearing or not
  2715. */
  2716. void pc_delautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> &bonus, bool restore){
  2717. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  2718. while( it != bonus.end() ){
  2719. std::shared_ptr<s_autobonus> b = *it;
  2720. if( b->active != INVALID_TIMER && restore && b->bonus_script != nullptr ){
  2721. unsigned int equip_pos_idx = 0;
  2722. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2723. for (uint8 j = 0; j < EQI_MAX; j++) {
  2724. if (sd.equip_index[j] >= 0)
  2725. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2726. }
  2727. if( ( equip_pos_idx&b->pos ) == b->pos ){
  2728. script_run_autobonus(b->bonus_script, &sd, b->pos);
  2729. }else{
  2730. // Not all required items equipped anymore
  2731. restore = false;
  2732. }
  2733. }
  2734. if( restore ){
  2735. it++;
  2736. continue;
  2737. }
  2738. it = bonus.erase(it);
  2739. }
  2740. }
  2741. /**
  2742. * Execute autobonus on player
  2743. * @param sd: Player data
  2744. * @param autobonus: Autobonus to run
  2745. */
  2746. void pc_exeautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> *bonus, std::shared_ptr<s_autobonus> autobonus)
  2747. {
  2748. if (autobonus->active != INVALID_TIMER)
  2749. delete_timer(autobonus->active, pc_endautobonus);
  2750. if( autobonus->other_script )
  2751. {
  2752. int j;
  2753. unsigned int equip_pos_idx = 0;
  2754. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2755. for(j = 0; j < EQI_MAX; j++) {
  2756. if(sd.equip_index[j] >= 0)
  2757. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2758. }
  2759. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2760. script_run_autobonus(autobonus->other_script,&sd,autobonus->pos);
  2761. }
  2762. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd.bl.id, (intptr_t)bonus);
  2763. status_calc_pc(&sd,SCO_FORCE);
  2764. }
  2765. /**
  2766. * Remove autobonus timer from player
  2767. */
  2768. TIMER_FUNC(pc_endautobonus){
  2769. map_session_data *sd = map_id2sd(id);
  2770. std::vector<std::shared_ptr<s_autobonus>> *bonus = (std::vector<std::shared_ptr<s_autobonus>> *)data;
  2771. nullpo_ret(sd);
  2772. nullpo_ret(bonus);
  2773. for( std::shared_ptr<s_autobonus> autobonus : *bonus ){
  2774. if( autobonus->active == tid ){
  2775. autobonus->active = INVALID_TIMER;
  2776. break;
  2777. }
  2778. }
  2779. status_calc_pc(sd,SCO_FORCE);
  2780. return 0;
  2781. }
  2782. /**
  2783. * Add element bonus to player when attacking
  2784. * @param sd: Player data
  2785. * @param ele: Element to adjust
  2786. * @param rate: Success chance
  2787. * @param flag: Battle flag
  2788. */
  2789. static void pc_bonus_addele(map_session_data* sd, unsigned char ele, short rate, short flag)
  2790. {
  2791. nullpo_retv(sd);
  2792. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2793. if (wd->addele2.size() == MAX_PC_BONUS) {
  2794. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2795. return;
  2796. }
  2797. if (!(flag&BF_RANGEMASK))
  2798. flag |= BF_SHORT | BF_LONG;
  2799. if (!(flag&BF_WEAPONMASK))
  2800. flag |= BF_WEAPON;
  2801. if (!(flag&BF_SKILLMASK)) {
  2802. if (flag&(BF_MAGIC | BF_MISC))
  2803. flag |= BF_SKILL;
  2804. if (flag&BF_WEAPON)
  2805. flag |= BF_NORMAL | BF_SKILL;
  2806. }
  2807. for (auto &it : wd->addele2) {
  2808. if (it.ele == ele && it.flag == flag) {
  2809. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2810. return;
  2811. }
  2812. }
  2813. struct s_addele2 entry = {};
  2814. if (rate < -10000 || rate > 10000)
  2815. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2816. entry.ele = ele;
  2817. entry.rate = cap_value(rate, -10000, 10000);
  2818. entry.flag = flag;
  2819. wd->addele2.push_back(entry);
  2820. }
  2821. /**
  2822. * Reduce element bonus to player when attacking
  2823. * @param sd: Player data
  2824. * @param ele: Element to adjust
  2825. * @param rate: Success chance
  2826. * @param flag: Battle flag
  2827. */
  2828. static void pc_bonus_subele(map_session_data* sd, unsigned char ele, short rate, short flag)
  2829. {
  2830. nullpo_retv(sd);
  2831. if (sd->subele2.size() == MAX_PC_BONUS) {
  2832. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2833. return;
  2834. }
  2835. if (!(flag&BF_RANGEMASK))
  2836. flag |= BF_SHORT | BF_LONG;
  2837. if (!(flag&BF_WEAPONMASK))
  2838. flag |= BF_WEAPON;
  2839. if (!(flag&BF_SKILLMASK)) {
  2840. if (flag&(BF_MAGIC | BF_MISC))
  2841. flag |= BF_SKILL;
  2842. if (flag&BF_WEAPON)
  2843. flag |= BF_NORMAL | BF_SKILL;
  2844. }
  2845. for (auto &it : sd->subele2) {
  2846. if (it.ele == ele && it.flag == flag) {
  2847. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2848. return;
  2849. }
  2850. }
  2851. struct s_addele2 entry = {};
  2852. if (rate < -10000 || rate > 10000)
  2853. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2854. entry.ele = ele;
  2855. entry.rate = cap_value(rate, -10000, 10000);
  2856. entry.flag = flag;
  2857. sd->subele2.push_back(entry);
  2858. }
  2859. /**
  2860. * Adjust race damage to target when attacking
  2861. * @param sd: Player data
  2862. * @param race: Race to adjust
  2863. * @param rate: Success chance
  2864. * @param flag: Battle flag
  2865. */
  2866. static void pc_bonus_subrace(map_session_data* sd, unsigned char race, short rate, short flag)
  2867. {
  2868. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2869. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2870. return;
  2871. }
  2872. if (!(flag&BF_RANGEMASK))
  2873. flag |= BF_SHORT | BF_LONG;
  2874. if (!(flag&BF_WEAPONMASK))
  2875. flag |= BF_WEAPON;
  2876. if (!(flag&BF_SKILLMASK)) {
  2877. if (flag&(BF_MAGIC | BF_MISC))
  2878. flag |= BF_SKILL;
  2879. if (flag&BF_WEAPON)
  2880. flag |= BF_NORMAL | BF_SKILL;
  2881. }
  2882. for (auto &it : sd->subrace3) {
  2883. if (it.race == race && it.flag == flag) {
  2884. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2885. return;
  2886. }
  2887. }
  2888. struct s_addrace2 entry = {};
  2889. if (rate < -10000 || rate > 10000)
  2890. ShowWarning("pc_bonus_subrace: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2891. entry.race = race;
  2892. entry.rate = cap_value(rate, -10000, 10000);
  2893. entry.flag = flag;
  2894. sd->subrace3.push_back(entry);
  2895. }
  2896. /**
  2897. * General item bonus for player
  2898. * @param bonus: Bonus array
  2899. * @param id: Key
  2900. * @param val: Value
  2901. * @param cap_rate: If Value is a rate value that needs to be capped
  2902. */
  2903. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2904. {
  2905. for (auto &it : bonus) {
  2906. if (it.id == id) {
  2907. if (cap_rate)
  2908. it.val = util::safe_addition_cap(it.val, val, 10000);
  2909. else
  2910. it.val += val;
  2911. return;
  2912. }
  2913. }
  2914. struct s_item_bonus entry = {};
  2915. if (cap_rate && (val < -10000 || val > 10000)) {
  2916. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2917. val = cap_value(val, -10000, 10000);
  2918. }
  2919. entry.id = id;
  2920. entry.val = val;
  2921. bonus.push_back(entry);
  2922. }
  2923. /**
  2924. * Remove HP/SP to player when attacking
  2925. * @param bonus: Bonus array
  2926. * @param rate: Success chance
  2927. * @param per: Percentage of HP/SP to vanish
  2928. * @param flag: Battle flag
  2929. */
  2930. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2931. if (bonus.size() == MAX_PC_BONUS) {
  2932. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2933. return;
  2934. }
  2935. if (!(flag&BF_RANGEMASK))
  2936. flag |= BF_SHORT | BF_LONG;
  2937. if (!(flag&BF_WEAPONMASK))
  2938. flag |= BF_WEAPON;
  2939. if (!(flag&BF_SKILLMASK)) {
  2940. if (flag&(BF_MAGIC | BF_MISC))
  2941. flag |= BF_SKILL;
  2942. if (flag&BF_WEAPON)
  2943. flag |= BF_NORMAL | BF_SKILL;
  2944. }
  2945. for (auto &it : bonus) {
  2946. if (it.flag == flag) {
  2947. it.rate = util::safe_addition_cap(it.rate, rate, (int16)10000);
  2948. it.per += per;
  2949. return;
  2950. }
  2951. }
  2952. struct s_vanish_bonus entry = {};
  2953. if (rate < -10000 || rate > 10000)
  2954. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2955. entry.rate = cap_value(rate, -10000, 10000);
  2956. entry.per = per;
  2957. entry.flag = flag;
  2958. bonus.push_back(entry);
  2959. }
  2960. /*==========================================
  2961. * Add a bonus(type) to player sd
  2962. * format: bonus bBonusName,val;
  2963. * @param sd
  2964. * @param type Bonus type used by bBonusName
  2965. * @param val Value that usually for rate or fixed value
  2966. *------------------------------------------*/
  2967. void pc_bonus(map_session_data *sd,int type,int val)
  2968. {
  2969. struct status_data *status;
  2970. int bonus;
  2971. nullpo_retv(sd);
  2972. status = &sd->base_status;
  2973. switch(type){
  2974. case SP_STR:
  2975. case SP_AGI:
  2976. case SP_VIT:
  2977. case SP_INT:
  2978. case SP_DEX:
  2979. case SP_LUK:
  2980. if(sd->state.lr_flag != 2)
  2981. sd->indexed_bonus.param_bonus[type-SP_STR]+=val;
  2982. break;
  2983. case SP_POW:
  2984. case SP_STA:
  2985. case SP_WIS:
  2986. case SP_SPL:
  2987. case SP_CON:
  2988. case SP_CRT:
  2989. if (sd->state.lr_flag != 2)
  2990. sd->indexed_bonus.param_bonus[type - SP_POW + PARAM_POW] += val;
  2991. break;
  2992. case SP_ATK1:
  2993. if(!sd->state.lr_flag) {
  2994. bonus = status->rhw.atk + val;
  2995. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2996. }
  2997. else if(sd->state.lr_flag == 1) {
  2998. bonus = status->lhw.atk + val;
  2999. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  3000. }
  3001. break;
  3002. case SP_ATK2:
  3003. if(!sd->state.lr_flag) {
  3004. bonus = status->rhw.atk2 + val;
  3005. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  3006. }
  3007. else if(sd->state.lr_flag == 1) {
  3008. bonus = status->lhw.atk2 + val;
  3009. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  3010. }
  3011. break;
  3012. case SP_BASE_ATK:
  3013. if(sd->state.lr_flag != 2) {
  3014. #ifdef RENEWAL
  3015. bonus = sd->bonus.eatk + val;
  3016. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3017. #else
  3018. bonus = status->batk + val;
  3019. status->batk = cap_value(bonus, 0, USHRT_MAX);
  3020. #endif
  3021. }
  3022. break;
  3023. case SP_DEF1:
  3024. if(sd->state.lr_flag != 2) {
  3025. bonus = status->def + val;
  3026. #ifdef RENEWAL
  3027. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3028. #else
  3029. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3030. #endif
  3031. }
  3032. break;
  3033. case SP_DEF2:
  3034. if(sd->state.lr_flag != 2) {
  3035. bonus = status->def2 + val;
  3036. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3037. }
  3038. break;
  3039. case SP_MDEF1:
  3040. if(sd->state.lr_flag != 2) {
  3041. bonus = status->mdef + val;
  3042. #ifdef RENEWAL
  3043. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3044. #else
  3045. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3046. #endif
  3047. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  3048. sd->bonus.shieldmdef += bonus;
  3049. }
  3050. }
  3051. break;
  3052. case SP_MDEF2:
  3053. if(sd->state.lr_flag != 2) {
  3054. bonus = status->mdef2 + val;
  3055. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3056. }
  3057. break;
  3058. case SP_HIT:
  3059. if(sd->state.lr_flag != 2) {
  3060. bonus = status->hit + val;
  3061. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3062. } else
  3063. sd->bonus.arrow_hit+=val;
  3064. break;
  3065. case SP_FLEE1:
  3066. if(sd->state.lr_flag != 2) {
  3067. bonus = status->flee + val;
  3068. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3069. }
  3070. break;
  3071. case SP_FLEE2:
  3072. if(sd->state.lr_flag != 2) {
  3073. bonus = status->flee2 + val*10;
  3074. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3075. }
  3076. break;
  3077. case SP_CRITICAL:
  3078. if(sd->state.lr_flag != 2) {
  3079. bonus = status->cri + val*10;
  3080. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3081. } else
  3082. sd->bonus.arrow_cri += val*10;
  3083. break;
  3084. case SP_PATK:
  3085. if (sd->state.lr_flag != 2) {
  3086. bonus = status->patk + val;
  3087. status->patk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3088. }
  3089. break;
  3090. case SP_SMATK:
  3091. if (sd->state.lr_flag != 2) {
  3092. bonus = status->smatk + val;
  3093. status->smatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3094. }
  3095. break;
  3096. case SP_RES:
  3097. if (sd->state.lr_flag != 2) {
  3098. bonus = status->res + val;
  3099. status->res = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3100. }
  3101. break;
  3102. case SP_MRES:
  3103. if (sd->state.lr_flag != 2) {
  3104. bonus = status->mres + val;
  3105. status->mres = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3106. }
  3107. break;
  3108. case SP_HPLUS:
  3109. if (sd->state.lr_flag != 2) {
  3110. bonus = status->hplus + val;
  3111. status->hplus = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3112. }
  3113. break;
  3114. case SP_CRATE:
  3115. if (sd->state.lr_flag != 2) {
  3116. bonus = status->crate + val;
  3117. status->crate = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3118. }
  3119. break;
  3120. case SP_ATKELE:
  3121. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  3122. switch (sd->state.lr_flag)
  3123. {
  3124. case 2:
  3125. switch (sd->status.weapon) {
  3126. case W_BOW:
  3127. case W_REVOLVER:
  3128. case W_RIFLE:
  3129. case W_GATLING:
  3130. case W_SHOTGUN:
  3131. case W_GRENADE:
  3132. //Become weapon element.
  3133. status->rhw.ele=val;
  3134. break;
  3135. default: //Become arrow element.
  3136. sd->bonus.arrow_ele=val;
  3137. break;
  3138. }
  3139. break;
  3140. case 1:
  3141. status->lhw.ele=val;
  3142. break;
  3143. default:
  3144. status->rhw.ele=val;
  3145. break;
  3146. }
  3147. break;
  3148. case SP_DEFELE:
  3149. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  3150. if(sd->state.lr_flag != 2)
  3151. status->def_ele=val;
  3152. break;
  3153. case SP_MAXHP:
  3154. if(sd->state.lr_flag == 2)
  3155. break;
  3156. sd->bonus.hp += val;
  3157. break;
  3158. case SP_MAXSP:
  3159. if(sd->state.lr_flag == 2)
  3160. break;
  3161. sd->bonus.sp += val;
  3162. break;
  3163. case SP_MAXAP:
  3164. if (sd->state.lr_flag == 2)
  3165. break;
  3166. sd->bonus.ap += val;
  3167. break;
  3168. case SP_MAXHPRATE:
  3169. if(sd->state.lr_flag != 2)
  3170. sd->hprate+=val;
  3171. break;
  3172. case SP_MAXSPRATE:
  3173. if(sd->state.lr_flag != 2)
  3174. sd->sprate+=val;
  3175. break;
  3176. case SP_MAXAPRATE:
  3177. if (sd->state.lr_flag != 2)
  3178. sd->aprate += val;
  3179. break;
  3180. case SP_SPRATE:
  3181. if(sd->state.lr_flag != 2)
  3182. sd->dsprate+=val;
  3183. break;
  3184. case SP_ATTACKRANGE:
  3185. switch (sd->state.lr_flag) {
  3186. case 2:
  3187. switch (sd->status.weapon) {
  3188. case W_BOW:
  3189. case W_REVOLVER:
  3190. case W_RIFLE:
  3191. case W_GATLING:
  3192. case W_SHOTGUN:
  3193. case W_GRENADE:
  3194. status->rhw.range += val;
  3195. }
  3196. break;
  3197. case 1:
  3198. status->lhw.range += val;
  3199. break;
  3200. default:
  3201. status->rhw.range += val;
  3202. break;
  3203. }
  3204. break;
  3205. case SP_SPEED_RATE: //Non stackable increase
  3206. if(sd->state.lr_flag != 2)
  3207. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  3208. break;
  3209. case SP_SPEED_ADDRATE: //Stackable increase
  3210. if(sd->state.lr_flag != 2)
  3211. sd->bonus.speed_add_rate -= val;
  3212. break;
  3213. case SP_ASPD: //Raw increase
  3214. if(sd->state.lr_flag != 2)
  3215. sd->bonus.aspd_add -= 10*val;
  3216. break;
  3217. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  3218. if(sd->state.lr_flag != 2)
  3219. #ifndef RENEWAL_ASPD
  3220. status->aspd_rate -= 10*val;
  3221. #else
  3222. status->aspd_rate2 += val;
  3223. #endif
  3224. break;
  3225. case SP_HP_RECOV_RATE:
  3226. if(sd->state.lr_flag != 2)
  3227. sd->hprecov_rate += val;
  3228. break;
  3229. case SP_SP_RECOV_RATE:
  3230. if(sd->state.lr_flag != 2)
  3231. sd->sprecov_rate += val;
  3232. break;
  3233. case SP_CRITICAL_DEF:
  3234. if(sd->state.lr_flag != 2)
  3235. sd->bonus.critical_def += val;
  3236. break;
  3237. case SP_NEAR_ATK_DEF:
  3238. if(sd->state.lr_flag != 2)
  3239. sd->bonus.near_attack_def_rate += val;
  3240. break;
  3241. case SP_LONG_ATK_DEF:
  3242. if(sd->state.lr_flag != 2)
  3243. sd->bonus.long_attack_def_rate += val;
  3244. break;
  3245. case SP_DOUBLE_RATE:
  3246. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  3247. sd->bonus.double_rate = val;
  3248. break;
  3249. case SP_DOUBLE_ADD_RATE:
  3250. if(sd->state.lr_flag == 0)
  3251. sd->bonus.double_add_rate += val;
  3252. break;
  3253. case SP_MATK_RATE:
  3254. if(sd->state.lr_flag != 2)
  3255. sd->matk_rate += val;
  3256. break;
  3257. case SP_IGNORE_DEF_ELE:
  3258. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  3259. if(!sd->state.lr_flag)
  3260. sd->right_weapon.ignore_def_ele |= 1<<val;
  3261. else if(sd->state.lr_flag == 1)
  3262. sd->left_weapon.ignore_def_ele |= 1<<val;
  3263. break;
  3264. case SP_IGNORE_DEF_RACE:
  3265. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  3266. if(!sd->state.lr_flag)
  3267. sd->right_weapon.ignore_def_race |= 1<<val;
  3268. else if(sd->state.lr_flag == 1)
  3269. sd->left_weapon.ignore_def_race |= 1<<val;
  3270. break;
  3271. case SP_IGNORE_DEF_CLASS:
  3272. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  3273. if(!sd->state.lr_flag)
  3274. sd->right_weapon.ignore_def_class |= 1<<val;
  3275. else if(sd->state.lr_flag == 1)
  3276. sd->left_weapon.ignore_def_class |= 1<<val;
  3277. break;
  3278. case SP_ATK_RATE:
  3279. if(sd->state.lr_flag != 2)
  3280. sd->bonus.atk_rate += val;
  3281. break;
  3282. case SP_MAGIC_ATK_DEF:
  3283. if(sd->state.lr_flag != 2)
  3284. sd->bonus.magic_def_rate += val;
  3285. break;
  3286. case SP_MISC_ATK_DEF:
  3287. if(sd->state.lr_flag != 2)
  3288. sd->bonus.misc_def_rate += val;
  3289. break;
  3290. case SP_IGNORE_MDEF_ELE:
  3291. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  3292. if(sd->state.lr_flag != 2)
  3293. sd->bonus.ignore_mdef_ele |= 1<<val;
  3294. break;
  3295. case SP_IGNORE_MDEF_RACE:
  3296. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  3297. if(sd->state.lr_flag != 2)
  3298. sd->bonus.ignore_mdef_race |= 1<<val;
  3299. break;
  3300. case SP_PERFECT_HIT_RATE:
  3301. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  3302. sd->bonus.perfect_hit = val;
  3303. break;
  3304. case SP_PERFECT_HIT_ADD_RATE:
  3305. if(sd->state.lr_flag != 2)
  3306. sd->bonus.perfect_hit_add += val;
  3307. break;
  3308. case SP_CRITICAL_RATE:
  3309. if(sd->state.lr_flag != 2)
  3310. sd->critical_rate+=val;
  3311. break;
  3312. case SP_DEF_RATIO_ATK_ELE:
  3313. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  3314. if(!sd->state.lr_flag)
  3315. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  3316. else if(sd->state.lr_flag == 1)
  3317. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  3318. break;
  3319. case SP_DEF_RATIO_ATK_RACE:
  3320. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  3321. if(!sd->state.lr_flag)
  3322. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  3323. else if(sd->state.lr_flag == 1)
  3324. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  3325. break;
  3326. case SP_DEF_RATIO_ATK_CLASS:
  3327. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  3328. if(!sd->state.lr_flag)
  3329. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  3330. else if(sd->state.lr_flag == 1)
  3331. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  3332. break;
  3333. case SP_HIT_RATE:
  3334. if(sd->state.lr_flag != 2)
  3335. sd->hit_rate += val;
  3336. break;
  3337. case SP_FLEE_RATE:
  3338. if(sd->state.lr_flag != 2)
  3339. sd->flee_rate += val;
  3340. break;
  3341. case SP_FLEE2_RATE:
  3342. if(sd->state.lr_flag != 2)
  3343. sd->flee2_rate += val;
  3344. break;
  3345. case SP_DEF_RATE:
  3346. if(sd->state.lr_flag != 2)
  3347. sd->def_rate += val;
  3348. break;
  3349. case SP_DEF2_RATE:
  3350. if(sd->state.lr_flag != 2)
  3351. sd->def2_rate += val;
  3352. break;
  3353. case SP_MDEF_RATE:
  3354. if(sd->state.lr_flag != 2)
  3355. sd->mdef_rate += val;
  3356. break;
  3357. case SP_MDEF2_RATE:
  3358. if(sd->state.lr_flag != 2)
  3359. sd->mdef2_rate += val;
  3360. break;
  3361. case SP_PATK_RATE:
  3362. if (sd->state.lr_flag != 2)
  3363. sd->patk_rate += val;
  3364. break;
  3365. case SP_SMATK_RATE:
  3366. if (sd->state.lr_flag != 2)
  3367. sd->smatk_rate += val;
  3368. break;
  3369. case SP_RES_RATE:
  3370. if (sd->state.lr_flag != 2)
  3371. sd->res_rate += val;
  3372. break;
  3373. case SP_MRES_RATE:
  3374. if (sd->state.lr_flag != 2)
  3375. sd->mres_rate += val;
  3376. break;
  3377. case SP_HPLUS_RATE:
  3378. if (sd->state.lr_flag != 2)
  3379. sd->hplus_rate += val;
  3380. break;
  3381. case SP_CRATE_RATE:
  3382. if (sd->state.lr_flag != 2)
  3383. sd->crate_rate += val;
  3384. break;
  3385. case SP_RESTART_FULL_RECOVER:
  3386. if(sd->state.lr_flag != 2)
  3387. sd->special_state.restart_full_recover = 1;
  3388. break;
  3389. case SP_NO_CASTCANCEL:
  3390. if(sd->state.lr_flag != 2)
  3391. sd->special_state.no_castcancel = 1;
  3392. break;
  3393. case SP_NO_CASTCANCEL2:
  3394. if(sd->state.lr_flag != 2)
  3395. sd->special_state.no_castcancel2 = 1;
  3396. break;
  3397. case SP_NO_SIZEFIX:
  3398. if(sd->state.lr_flag != 2)
  3399. sd->special_state.no_sizefix = 1;
  3400. break;
  3401. case SP_NO_MAGIC_DAMAGE:
  3402. if(sd->state.lr_flag == 2)
  3403. break;
  3404. val+= sd->special_state.no_magic_damage;
  3405. sd->special_state.no_magic_damage = cap_value(val,0,100);
  3406. break;
  3407. case SP_NO_WEAPON_DAMAGE:
  3408. if(sd->state.lr_flag == 2)
  3409. break;
  3410. val+= sd->special_state.no_weapon_damage;
  3411. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  3412. break;
  3413. case SP_NO_MISC_DAMAGE:
  3414. if(sd->state.lr_flag == 2)
  3415. break;
  3416. val+= sd->special_state.no_misc_damage;
  3417. sd->special_state.no_misc_damage = cap_value(val,0,100);
  3418. break;
  3419. case SP_NO_GEMSTONE:
  3420. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  3421. sd->special_state.no_gemstone = 1;
  3422. break;
  3423. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  3424. if(sd->state.lr_flag != 2) {
  3425. sd->special_state.intravision = 1;
  3426. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  3427. }
  3428. break;
  3429. case SP_NO_KNOCKBACK:
  3430. if(sd->state.lr_flag != 2)
  3431. sd->special_state.no_knockback = 1;
  3432. break;
  3433. case SP_SPLASH_RANGE:
  3434. if(sd->bonus.splash_range < val)
  3435. sd->bonus.splash_range = val;
  3436. break;
  3437. case SP_SPLASH_ADD_RANGE:
  3438. sd->bonus.splash_add_range += val;
  3439. break;
  3440. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  3441. if(sd->state.lr_flag != 2)
  3442. sd->bonus.short_weapon_damage_return += val;
  3443. break;
  3444. case SP_LONG_WEAPON_DAMAGE_RETURN:
  3445. if(sd->state.lr_flag != 2)
  3446. sd->bonus.long_weapon_damage_return += val;
  3447. break;
  3448. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  3449. if(sd->state.lr_flag != 2)
  3450. sd->bonus.magic_damage_return += val;
  3451. break;
  3452. case SP_REDUCE_DAMAGE_RETURN:
  3453. if (sd->state.lr_flag != 2)
  3454. sd->bonus.reduce_damage_return += val;
  3455. break;
  3456. case SP_ALL_STATS: // [Valaris]
  3457. if(sd->state.lr_flag!=2) {
  3458. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3459. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3460. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3461. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  3462. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3463. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  3464. }
  3465. break;
  3466. case SP_ALL_TRAIT_STATS:
  3467. if (sd->state.lr_flag != 2) {
  3468. sd->indexed_bonus.param_bonus[PARAM_POW] += val;
  3469. sd->indexed_bonus.param_bonus[PARAM_STA] += val;
  3470. sd->indexed_bonus.param_bonus[PARAM_WIS] += val;
  3471. sd->indexed_bonus.param_bonus[PARAM_SPL] += val;
  3472. sd->indexed_bonus.param_bonus[PARAM_CON] += val;
  3473. sd->indexed_bonus.param_bonus[PARAM_CRT] += val;
  3474. }
  3475. break;
  3476. case SP_AGI_VIT: // [Valaris]
  3477. if(sd->state.lr_flag!=2) {
  3478. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3479. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3480. }
  3481. break;
  3482. case SP_AGI_DEX_STR: // [Valaris]
  3483. if(sd->state.lr_flag!=2) {
  3484. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3485. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3486. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3487. }
  3488. break;
  3489. case SP_PERFECT_HIDE: // [Valaris]
  3490. if(sd->state.lr_flag!=2)
  3491. sd->special_state.perfect_hiding=1;
  3492. break;
  3493. case SP_UNBREAKABLE:
  3494. if(sd->state.lr_flag!=2)
  3495. sd->bonus.unbreakable += val;
  3496. break;
  3497. case SP_UNBREAKABLE_WEAPON:
  3498. if(sd->state.lr_flag != 2)
  3499. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3500. break;
  3501. case SP_UNBREAKABLE_ARMOR:
  3502. if(sd->state.lr_flag != 2)
  3503. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3504. break;
  3505. case SP_UNBREAKABLE_HELM:
  3506. if(sd->state.lr_flag != 2)
  3507. sd->bonus.unbreakable_equip |= EQP_HELM;
  3508. break;
  3509. case SP_UNBREAKABLE_SHIELD:
  3510. if(sd->state.lr_flag != 2)
  3511. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3512. break;
  3513. case SP_UNBREAKABLE_GARMENT:
  3514. if(sd->state.lr_flag != 2)
  3515. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3516. break;
  3517. case SP_UNBREAKABLE_SHOES:
  3518. if(sd->state.lr_flag != 2)
  3519. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3520. break;
  3521. case SP_CLASSCHANGE: // [Valaris]
  3522. if(sd->state.lr_flag !=2)
  3523. sd->bonus.classchange=val;
  3524. break;
  3525. case SP_SHORT_ATK_RATE:
  3526. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3527. sd->bonus.short_attack_atk_rate+=val;
  3528. break;
  3529. case SP_LONG_ATK_RATE:
  3530. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3531. sd->bonus.long_attack_atk_rate+=val;
  3532. break;
  3533. case SP_BREAK_WEAPON_RATE:
  3534. if(sd->state.lr_flag != 2)
  3535. sd->bonus.break_weapon_rate+=val;
  3536. break;
  3537. case SP_BREAK_ARMOR_RATE:
  3538. if(sd->state.lr_flag != 2)
  3539. sd->bonus.break_armor_rate+=val;
  3540. break;
  3541. case SP_ADD_STEAL_RATE:
  3542. if(sd->state.lr_flag != 2)
  3543. sd->bonus.add_steal_rate+=val;
  3544. break;
  3545. case SP_DELAYRATE:
  3546. if(sd->state.lr_flag != 2)
  3547. sd->bonus.delayrate -= val;
  3548. break;
  3549. case SP_CRIT_ATK_RATE:
  3550. if(sd->state.lr_flag != 2)
  3551. sd->bonus.crit_atk_rate += val;
  3552. break;
  3553. case SP_CRIT_DEF_RATE:
  3554. if(sd->state.lr_flag != 2)
  3555. sd->bonus.crit_def_rate += val;
  3556. break;
  3557. case SP_NO_REGEN:
  3558. if(sd->state.lr_flag != 2)
  3559. sd->regen.state.block|=val;
  3560. break;
  3561. case SP_UNSTRIPABLE_WEAPON:
  3562. if(sd->state.lr_flag != 2)
  3563. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3564. break;
  3565. case SP_UNSTRIPABLE:
  3566. case SP_UNSTRIPABLE_ARMOR:
  3567. if(sd->state.lr_flag != 2)
  3568. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3569. break;
  3570. case SP_UNSTRIPABLE_HELM:
  3571. if(sd->state.lr_flag != 2)
  3572. sd->bonus.unstripable_equip |= EQP_HELM;
  3573. break;
  3574. case SP_UNSTRIPABLE_SHIELD:
  3575. if(sd->state.lr_flag != 2)
  3576. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3577. break;
  3578. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3579. if(!sd->state.lr_flag) {
  3580. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3581. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3582. } else if(sd->state.lr_flag == 1) {
  3583. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3584. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3585. }
  3586. break;
  3587. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3588. if(!sd->state.lr_flag) {
  3589. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3590. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3591. } else if(sd->state.lr_flag == 1) {
  3592. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3593. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3594. }
  3595. break;
  3596. case SP_SP_GAIN_VALUE:
  3597. if(!sd->state.lr_flag)
  3598. sd->bonus.sp_gain_value += val;
  3599. break;
  3600. case SP_HP_GAIN_VALUE:
  3601. if(!sd->state.lr_flag)
  3602. sd->bonus.hp_gain_value += val;
  3603. break;
  3604. case SP_LONG_SP_GAIN_VALUE:
  3605. if(!sd->state.lr_flag)
  3606. sd->bonus.long_sp_gain_value += val;
  3607. case SP_LONG_HP_GAIN_VALUE:
  3608. if(!sd->state.lr_flag)
  3609. sd->bonus.long_hp_gain_value += val;
  3610. break;
  3611. case SP_MAGIC_SP_GAIN_VALUE:
  3612. if(!sd->state.lr_flag)
  3613. sd->bonus.magic_sp_gain_value += val;
  3614. break;
  3615. case SP_MAGIC_HP_GAIN_VALUE:
  3616. if(!sd->state.lr_flag)
  3617. sd->bonus.magic_hp_gain_value += val;
  3618. break;
  3619. case SP_ADD_HEAL_RATE:
  3620. if(sd->state.lr_flag != 2)
  3621. sd->bonus.add_heal_rate += val;
  3622. break;
  3623. case SP_ADD_HEAL2_RATE:
  3624. if(sd->state.lr_flag != 2)
  3625. sd->bonus.add_heal2_rate += val;
  3626. break;
  3627. case SP_ADD_ITEM_HEAL_RATE:
  3628. if(sd->state.lr_flag != 2)
  3629. sd->bonus.itemhealrate2 += val;
  3630. break;
  3631. case SP_EMATK:
  3632. if(sd->state.lr_flag != 2)
  3633. sd->bonus.ematk += val;
  3634. break;
  3635. case SP_ADD_VARIABLECAST:
  3636. if (sd->state.lr_flag != 2)
  3637. sd->bonus.add_varcast += val;
  3638. break;
  3639. #ifdef RENEWAL_CAST
  3640. case SP_FIXCASTRATE:
  3641. if(sd->state.lr_flag != 2)
  3642. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3643. break;
  3644. case SP_ADD_FIXEDCAST:
  3645. if(sd->state.lr_flag != 2)
  3646. sd->bonus.add_fixcast += val;
  3647. break;
  3648. case SP_CASTRATE:
  3649. case SP_VARCASTRATE:
  3650. if(sd->state.lr_flag != 2)
  3651. sd->bonus.varcastrate -= val;
  3652. break;
  3653. #else
  3654. case SP_ADD_FIXEDCAST:
  3655. case SP_FIXCASTRATE:
  3656. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3657. break;
  3658. case SP_VARCASTRATE:
  3659. case SP_CASTRATE:
  3660. if(sd->state.lr_flag != 2)
  3661. sd->castrate += val;
  3662. break;
  3663. #endif
  3664. case SP_ADDMAXWEIGHT:
  3665. if (sd->state.lr_flag != 2)
  3666. sd->add_max_weight += val;
  3667. break;
  3668. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3669. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3670. break;
  3671. case SP_ABSORB_DMG_MAXHP2:
  3672. sd->bonus.absorb_dmg_maxhp2 = max(sd->bonus.absorb_dmg_maxhp2, val);
  3673. break;
  3674. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3675. if (sd->state.lr_flag != 2)
  3676. sd->bonus.critical_rangeatk += val*10;
  3677. else
  3678. sd->bonus.arrow_cri += val*10;
  3679. break;
  3680. case SP_WEAPON_ATK_RATE:
  3681. if (sd->state.lr_flag != 2)
  3682. sd->bonus.weapon_atk_rate += val;
  3683. break;
  3684. case SP_WEAPON_MATK_RATE:
  3685. if (sd->state.lr_flag != 2)
  3686. sd->bonus.weapon_matk_rate += val;
  3687. break;
  3688. case SP_NO_MADO_FUEL:
  3689. if (sd->state.lr_flag != 2)
  3690. sd->special_state.no_mado_fuel = 1;
  3691. break;
  3692. case SP_NO_WALK_DELAY:
  3693. if (sd->state.lr_flag != 2)
  3694. sd->special_state.no_walk_delay = 1;
  3695. break;
  3696. case SP_ADD_ITEM_SPHEAL_RATE:
  3697. if(sd->state.lr_flag != 2)
  3698. sd->bonus.itemsphealrate2 += val;
  3699. break;
  3700. default:
  3701. if (current_equip_combo_pos > 0) {
  3702. ShowWarning("pc_bonus: unknown bonus type %d %d in a combo with item #%u\n", type, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3703. }
  3704. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3705. ShowWarning("pc_bonus: unknown bonus type %d %d in item #%u\n", type, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3706. }
  3707. else {
  3708. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3709. }
  3710. break;
  3711. }
  3712. }
  3713. /*==========================================
  3714. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3715. * format: bonus2 bBonusName,type2,val;
  3716. * @param sd
  3717. * @param type Bonus type used by bBonusName
  3718. * @param type2
  3719. * @param val Value that usually for rate or fixed value
  3720. *------------------------------------------*/
  3721. void pc_bonus2(map_session_data *sd,int type,int type2,int val)
  3722. {
  3723. nullpo_retv(sd);
  3724. switch(type){
  3725. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3726. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3727. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3728. sd->right_weapon.addele[type2]+=val;
  3729. else if(sd->state.lr_flag == 1)
  3730. sd->left_weapon.addele[type2]+=val;
  3731. else if(sd->state.lr_flag == 2)
  3732. sd->indexed_bonus.arrow_addele[type2]+=val;
  3733. break;
  3734. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3735. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3736. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3737. sd->right_weapon.addrace[type2]+=val;
  3738. else if(sd->state.lr_flag == 1)
  3739. sd->left_weapon.addrace[type2]+=val;
  3740. else if(sd->state.lr_flag == 2)
  3741. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3742. break;
  3743. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3744. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3745. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3746. sd->right_weapon.addclass[type2]+=val;
  3747. else if(sd->state.lr_flag == 1)
  3748. sd->left_weapon.addclass[type2]+=val;
  3749. else if(sd->state.lr_flag == 2)
  3750. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3751. break;
  3752. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3753. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3754. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3755. sd->right_weapon.addsize[type2]+=val;
  3756. else if(sd->state.lr_flag == 1)
  3757. sd->left_weapon.addsize[type2]+=val;
  3758. else if(sd->state.lr_flag == 2)
  3759. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3760. break;
  3761. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3762. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3763. if(sd->state.lr_flag != 2)
  3764. sd->indexed_bonus.subele_script[type2] += val;
  3765. break;
  3766. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3767. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3768. if(sd->state.lr_flag != 2)
  3769. sd->indexed_bonus.subrace[type2]+=val;
  3770. break;
  3771. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3772. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3773. if(sd->state.lr_flag != 2)
  3774. sd->indexed_bonus.subclass[type2]+=val;
  3775. break;
  3776. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3777. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3778. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3779. break;
  3780. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3781. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3782. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  3783. break;
  3784. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3785. if(sd->state.lr_flag == 2)
  3786. break;
  3787. if (sd->reseff.size() == MAX_PC_BONUS) {
  3788. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3789. break;
  3790. }
  3791. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3792. break;
  3793. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3794. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3795. if(sd->state.lr_flag != 2)
  3796. sd->indexed_bonus.magic_addele_script[type2] += val;
  3797. break;
  3798. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3799. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3800. if(sd->state.lr_flag != 2)
  3801. sd->indexed_bonus.magic_addrace[type2]+=val;
  3802. break;
  3803. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3804. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3805. if(sd->state.lr_flag != 2)
  3806. sd->indexed_bonus.magic_addclass[type2]+=val;
  3807. break;
  3808. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3809. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3810. if(sd->state.lr_flag != 2)
  3811. sd->indexed_bonus.magic_addsize[type2]+=val;
  3812. break;
  3813. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3814. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3815. if(sd->state.lr_flag != 2)
  3816. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3817. break;
  3818. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3819. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3820. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3821. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3822. break;
  3823. }
  3824. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3825. }
  3826. break;
  3827. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3828. if(sd->state.lr_flag == 2)
  3829. break;
  3830. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3831. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3832. break;
  3833. }
  3834. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3835. break;
  3836. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3837. if(sd->state.lr_flag == 2)
  3838. break;
  3839. if (sd->add_def.size() == MAX_PC_BONUS) {
  3840. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3841. break;
  3842. }
  3843. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3844. break;
  3845. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3846. if(sd->state.lr_flag == 2)
  3847. break;
  3848. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3849. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3850. break;
  3851. }
  3852. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3853. break;
  3854. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3855. if(!sd->state.lr_flag) {
  3856. sd->right_weapon.hp_drain_rate.rate += type2;
  3857. sd->right_weapon.hp_drain_rate.per += val;
  3858. }
  3859. else if(sd->state.lr_flag == 1) {
  3860. sd->left_weapon.hp_drain_rate.rate += type2;
  3861. sd->left_weapon.hp_drain_rate.per += val;
  3862. }
  3863. break;
  3864. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3865. if(!sd->state.lr_flag) {
  3866. sd->right_weapon.sp_drain_rate.rate += type2;
  3867. sd->right_weapon.sp_drain_rate.per += val;
  3868. }
  3869. else if(sd->state.lr_flag == 1) {
  3870. sd->left_weapon.sp_drain_rate.rate += type2;
  3871. sd->left_weapon.sp_drain_rate.per += val;
  3872. }
  3873. break;
  3874. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3875. if(sd->state.lr_flag != 2) {
  3876. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3877. }
  3878. break;
  3879. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3880. if(sd->state.lr_flag != 2) {
  3881. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3882. }
  3883. break;
  3884. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3885. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3886. sd->bonus.get_zeny_rate = val;
  3887. sd->bonus.get_zeny_num = type2;
  3888. }
  3889. break;
  3890. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3891. if(sd->state.lr_flag != 2) {
  3892. sd->bonus.get_zeny_rate += val;
  3893. sd->bonus.get_zeny_num += type2;
  3894. }
  3895. break;
  3896. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3897. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3898. if(sd->state.lr_flag == 2)
  3899. break;
  3900. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3901. sd->special_state.bonus_coma = 1;
  3902. break;
  3903. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3904. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3905. if(sd->state.lr_flag == 2)
  3906. break;
  3907. sd->indexed_bonus.weapon_coma_race[type2] += val;
  3908. sd->special_state.bonus_coma = 1;
  3909. break;
  3910. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3911. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3912. if(sd->state.lr_flag == 2)
  3913. break;
  3914. sd->indexed_bonus.weapon_coma_class[type2] += val;
  3915. sd->special_state.bonus_coma = 1;
  3916. break;
  3917. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3918. if(sd->state.lr_flag != 2)
  3919. sd->indexed_bonus.weapon_atk[type2]+=val;
  3920. break;
  3921. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3922. if(sd->state.lr_flag != 2)
  3923. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  3924. break;
  3925. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3926. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3927. if(sd->state.lr_flag != 2)
  3928. sd->indexed_bonus.critaddrace[type2] += val*10;
  3929. break;
  3930. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3931. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3932. if(sd->state.lr_flag != 2)
  3933. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3934. break;
  3935. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3936. if(sd->state.lr_flag == 2)
  3937. break;
  3938. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3939. ShowWarning("pc_bonus2: SP_SKILL_ATK: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3940. break;
  3941. }
  3942. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3943. break;
  3944. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3945. if(sd->state.lr_flag == 2)
  3946. break;
  3947. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3948. ShowWarning("pc_bonus2: SP_SKILL_HEAL: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3949. break;
  3950. }
  3951. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3952. break;
  3953. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3954. if(sd->state.lr_flag == 2)
  3955. break;
  3956. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3957. ShowWarning("pc_bonus2: SP_SKILL_HEAL2: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3958. break;
  3959. }
  3960. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3961. break;
  3962. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3963. if(sd->state.lr_flag == 2)
  3964. break;
  3965. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3966. ShowWarning("pc_bonus2: SP_ADD_SKILL_BLOW: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3967. break;
  3968. }
  3969. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3970. break;
  3971. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3972. if(sd->state.lr_flag != 2) {
  3973. sd->hp_loss.value = type2;
  3974. sd->hp_loss.rate = val;
  3975. }
  3976. break;
  3977. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3978. if(sd->state.lr_flag != 2) {
  3979. sd->hp_regen.value = type2;
  3980. sd->hp_regen.rate = val;
  3981. }
  3982. break;
  3983. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3984. if (sd->state.lr_flag != 2) {
  3985. sd->percent_hp_regen.value = type2;
  3986. sd->percent_hp_regen.rate = val;
  3987. }
  3988. break;
  3989. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3990. if (sd->state.lr_flag != 2) {
  3991. sd->percent_sp_regen.value = type2;
  3992. sd->percent_sp_regen.rate = val;
  3993. }
  3994. break;
  3995. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3996. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3997. if(sd->state.lr_flag != 2)
  3998. sd->right_weapon.addrace2[type2] += val;
  3999. else
  4000. sd->left_weapon.addrace2[type2] += val;
  4001. break;
  4002. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  4003. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  4004. if(sd->state.lr_flag != 2)
  4005. sd->indexed_bonus.subsize[type2]+=val;
  4006. break;
  4007. case SP_WEAPON_SUBSIZE: // bonus2 bWeaponSubSize,s,x;
  4008. PC_BONUS_CHK_SIZE(type2, SP_WEAPON_SUBSIZE);
  4009. if (sd->state.lr_flag != 2)
  4010. sd->indexed_bonus.weapon_subsize[type2] += val;
  4011. break;
  4012. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  4013. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  4014. if(sd->state.lr_flag != 2)
  4015. sd->indexed_bonus.magic_subsize[type2]+=val;
  4016. break;
  4017. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  4018. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  4019. if(sd->state.lr_flag != 2)
  4020. sd->indexed_bonus.subrace2[type2]+=val;
  4021. break;
  4022. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  4023. if(sd->state.lr_flag == 2)
  4024. break;
  4025. if( !item_db.exists( type2 ) ){
  4026. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  4027. break;
  4028. }
  4029. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  4030. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4031. break;
  4032. }
  4033. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  4034. break;
  4035. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  4036. if (sd->state.lr_flag == 2)
  4037. break;
  4038. if (!type2 || !itemdb_group.exists(type2)) {
  4039. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  4040. break;
  4041. }
  4042. if (sd->itemgrouphealrate.size() == MAX_PC_BONUS) {
  4043. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4044. break;
  4045. }
  4046. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  4047. break;
  4048. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  4049. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  4050. if(sd->state.lr_flag != 2)
  4051. sd->indexed_bonus.expaddrace[type2]+=val;
  4052. break;
  4053. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  4054. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  4055. if(sd->state.lr_flag != 2)
  4056. sd->indexed_bonus.expaddclass[type2]+=val;
  4057. break;
  4058. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  4059. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  4060. if(sd->state.lr_flag != 2)
  4061. sd->indexed_bonus.sp_gain_race[type2]+=val;
  4062. break;
  4063. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  4064. if (sd->state.lr_flag != 2)
  4065. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  4066. break;
  4067. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  4068. if (sd->state.lr_flag != 2)
  4069. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  4070. break;
  4071. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  4072. if(sd->state.lr_flag != 2) {
  4073. sd->sp_loss.value = type2;
  4074. sd->sp_loss.rate = val;
  4075. }
  4076. break;
  4077. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  4078. if(sd->state.lr_flag != 2) {
  4079. sd->sp_regen.value = type2;
  4080. sd->sp_regen.rate = val;
  4081. }
  4082. break;
  4083. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  4084. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  4085. if(!sd->state.lr_flag) {
  4086. sd->right_weapon.hp_drain_race[type2] += val;
  4087. }
  4088. else if(sd->state.lr_flag == 1) {
  4089. sd->left_weapon.hp_drain_race[type2] += val;
  4090. }
  4091. break;
  4092. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  4093. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  4094. if(!sd->state.lr_flag) {
  4095. sd->right_weapon.sp_drain_race[type2] += val;
  4096. }
  4097. else if(sd->state.lr_flag == 1) {
  4098. sd->left_weapon.sp_drain_race[type2] += val;
  4099. }
  4100. break;
  4101. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  4102. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  4103. if(!sd->state.lr_flag) {
  4104. sd->right_weapon.hp_drain_class[type2] += val;
  4105. }
  4106. else if(sd->state.lr_flag == 1) {
  4107. sd->left_weapon.hp_drain_class[type2] += val;
  4108. }
  4109. break;
  4110. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  4111. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  4112. if(!sd->state.lr_flag) {
  4113. sd->right_weapon.sp_drain_class[type2] += val;
  4114. }
  4115. else if(sd->state.lr_flag == 1) {
  4116. sd->left_weapon.sp_drain_class[type2] += val;
  4117. }
  4118. break;
  4119. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  4120. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  4121. if(sd->state.lr_flag != 2)
  4122. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  4123. break;
  4124. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  4125. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  4126. if(sd->state.lr_flag != 2)
  4127. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  4128. break;
  4129. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  4130. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  4131. if(sd->state.lr_flag != 2)
  4132. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  4133. break;
  4134. case SP_SP_IGNORE_RES_RACE_RATE: // bonus2 bIgnoreResRaceRate,r,n;
  4135. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_RES_RACE_RATE);
  4136. if(sd->state.lr_flag != 2)
  4137. sd->indexed_bonus.ignore_res_by_race[type2] += val;
  4138. break;
  4139. case SP_SP_IGNORE_MRES_RACE_RATE: // bonus2 bIgnoreMResRaceRate,r,n;
  4140. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_MRES_RACE_RATE);
  4141. if(sd->state.lr_flag != 2)
  4142. sd->indexed_bonus.ignore_mres_by_race[type2] += val;
  4143. break;
  4144. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  4145. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  4146. if (sd->state.lr_flag != 2)
  4147. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  4148. break;
  4149. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  4150. if(sd->state.lr_flag == 2)
  4151. break;
  4152. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  4153. 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);
  4154. break;
  4155. }
  4156. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  4157. break;
  4158. case SP_SKILL_DELAY:
  4159. if(sd->state.lr_flag == 2)
  4160. break;
  4161. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  4162. 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);
  4163. break;
  4164. }
  4165. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  4166. break;
  4167. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  4168. if(sd->state.lr_flag == 2)
  4169. break;
  4170. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  4171. 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);
  4172. break;
  4173. }
  4174. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  4175. break;
  4176. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  4177. if (sd->state.lr_flag == 2)
  4178. break;
  4179. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  4180. 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);
  4181. break;
  4182. }
  4183. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  4184. break;
  4185. #ifdef RENEWAL_CAST
  4186. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4187. if(sd->state.lr_flag == 2)
  4188. break;
  4189. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  4190. 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);
  4191. break;
  4192. }
  4193. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  4194. break;
  4195. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4196. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4197. if(sd->state.lr_flag == 2)
  4198. break;
  4199. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  4200. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4201. break;
  4202. }
  4203. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  4204. break;
  4205. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4206. if(sd->state.lr_flag == 2)
  4207. break;
  4208. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  4209. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4210. break;
  4211. }
  4212. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  4213. break;
  4214. #else
  4215. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4216. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4217. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  4218. break;
  4219. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4220. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4221. if(sd->state.lr_flag == 2)
  4222. break;
  4223. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4224. 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);
  4225. break;
  4226. }
  4227. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  4228. break;
  4229. #endif
  4230. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  4231. if(sd->state.lr_flag == 2)
  4232. break;
  4233. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  4234. 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);
  4235. break;
  4236. }
  4237. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  4238. break;
  4239. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  4240. if (sd->subskill.size() == MAX_PC_BONUS) {
  4241. 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);
  4242. break;
  4243. }
  4244. pc_bonus_itembonus(sd->subskill, type2, val, false);
  4245. break;
  4246. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  4247. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  4248. sd->indexed_bonus.subdefele[type2] += val;
  4249. break;
  4250. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  4251. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  4252. sd->indexed_bonus.coma_class[type2] += val;
  4253. sd->special_state.bonus_coma = 1;
  4254. break;
  4255. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  4256. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  4257. sd->indexed_bonus.coma_race[type2] += val;
  4258. sd->special_state.bonus_coma = 1;
  4259. break;
  4260. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  4261. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  4262. if(sd->state.lr_flag != 2)
  4263. sd->indexed_bonus.magic_addrace2[type2] += val;
  4264. break;
  4265. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  4266. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  4267. if (sd->state.lr_flag != 2)
  4268. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  4269. break;
  4270. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  4271. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  4272. if (sd->state.lr_flag != 2)
  4273. sd->indexed_bonus.dropaddrace[type2] += val;
  4274. break;
  4275. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  4276. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  4277. if (sd->state.lr_flag != 2)
  4278. sd->indexed_bonus.dropaddclass[type2] += val;
  4279. break;
  4280. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  4281. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  4282. sd->indexed_bonus.magic_subdefele[type2] += val;
  4283. break;
  4284. case SP_ADD_ITEM_SPHEAL_RATE: // bonus2 bAddItemSPHealRate,iid,n;
  4285. if( sd->state.lr_flag == 2 ){
  4286. break;
  4287. }
  4288. if( !item_db.exists( type2 ) ){
  4289. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE Invalid item with id %d\n", type2 );
  4290. break;
  4291. }
  4292. if( sd->itemsphealrate.size() == MAX_PC_BONUS ){
  4293. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4294. break;
  4295. }
  4296. pc_bonus_itembonus( sd->itemsphealrate, type2, val, false );
  4297. break;
  4298. case SP_ADD_ITEMGROUP_SPHEAL_RATE: // bonus2 bAddItemGroupSPHealRate,ig,n;
  4299. if( sd->state.lr_flag == 2 ){
  4300. break;
  4301. }
  4302. if( !type2 || !itemdb_group.exists( type2 ) ){
  4303. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Invalid item group with id %d\n", type2 );
  4304. break;
  4305. }
  4306. if( sd->itemgroupsphealrate.size() == MAX_PC_BONUS ){
  4307. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4308. break;
  4309. }
  4310. pc_bonus_itembonus( sd->itemgroupsphealrate, type2, val, false );
  4311. break;
  4312. default:
  4313. if (current_equip_combo_pos > 0) {
  4314. 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);
  4315. }
  4316. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4317. 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);
  4318. }
  4319. else {
  4320. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  4321. }
  4322. break;
  4323. }
  4324. }
  4325. /**
  4326. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  4327. * @param sd
  4328. * @param type Bonus type used by bBonusName
  4329. * @param type2
  4330. * @param type3
  4331. * @param val Value that usually for rate or fixed value
  4332. */
  4333. void pc_bonus3(map_session_data *sd,int type,int type2,int type3,int val)
  4334. {
  4335. nullpo_retv(sd);
  4336. switch(type){
  4337. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  4338. if(sd->state.lr_flag != 2)
  4339. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  4340. break;
  4341. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  4342. if(sd->state.lr_flag != 2)
  4343. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  4344. break;
  4345. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  4346. if(sd->state.lr_flag != 2)
  4347. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  4348. break;
  4349. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  4350. if(sd->state.lr_flag != 2)
  4351. {
  4352. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4353. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4354. pc_bonus_autospell(sd->autospell, type2, type3, val, 0, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4355. }
  4356. break;
  4357. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  4358. if(sd->state.lr_flag != 2)
  4359. {
  4360. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4361. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4362. pc_bonus_autospell(sd->autospell2, type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4363. }
  4364. break;
  4365. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  4366. if (sd->state.lr_flag != 2)
  4367. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  4368. break;
  4369. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  4370. if (sd->state.lr_flag != 2)
  4371. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  4372. break;
  4373. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  4374. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4375. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  4376. break;
  4377. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  4378. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4379. if(sd->state.lr_flag != 2)
  4380. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  4381. break;
  4382. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  4383. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  4384. if( sd->state.lr_flag != 2 )
  4385. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  4386. break;
  4387. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  4388. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  4389. if (sd->state.lr_flag != 2)
  4390. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  4391. break;
  4392. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  4393. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  4394. if (sd->state.lr_flag != 2)
  4395. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  4396. break;
  4397. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  4398. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  4399. if (sd->state.lr_flag != 2)
  4400. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  4401. break;
  4402. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  4403. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  4404. if(sd->state.lr_flag != 2) {
  4405. sd->sp_vanish_race[type2].rate += type3;
  4406. sd->sp_vanish_race[type2].per += val;
  4407. }
  4408. break;
  4409. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  4410. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  4411. if(sd->state.lr_flag != 2) {
  4412. sd->hp_vanish_race[type2].rate += type3;
  4413. sd->hp_vanish_race[type2].per += val;
  4414. }
  4415. break;
  4416. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  4417. if(sd->state.lr_flag != 2) {
  4418. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  4419. }
  4420. break;
  4421. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  4422. if(sd->state.lr_flag != 2) {
  4423. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  4424. }
  4425. break;
  4426. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  4427. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  4428. if (sd->state.lr_flag == 2)
  4429. break;
  4430. //! CONFIRM: Is it not stackable? Does not check max or min value?
  4431. //if (type3 > sd->norecover_state_race[type2].rate) {
  4432. // sd->norecover_state_race[type2].rate = type3;
  4433. // sd->norecover_state_race[type2].tick = val;
  4434. // break;
  4435. //}
  4436. sd->norecover_state_race[type2].rate = type3;
  4437. sd->norecover_state_race[type2].tick = val;
  4438. break;
  4439. default:
  4440. if (current_equip_combo_pos > 0) {
  4441. 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);
  4442. }
  4443. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4444. 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);
  4445. }
  4446. else {
  4447. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  4448. }
  4449. break;
  4450. }
  4451. }
  4452. /**
  4453. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  4454. * @param sd
  4455. * @param type Bonus type used by bBonusName
  4456. * @param type2
  4457. * @param type3
  4458. * @param type4
  4459. * @param val Value that usually for rate or fixed value
  4460. */
  4461. void pc_bonus4(map_session_data *sd,int type,int type2,int type3,int type4,int val)
  4462. {
  4463. nullpo_retv(sd);
  4464. switch(type){
  4465. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4466. if(sd->state.lr_flag != 2)
  4467. pc_bonus_autospell(sd->autospell, type2, type3, type4, 0, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4468. break;
  4469. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4470. if(sd->state.lr_flag != 2)
  4471. pc_bonus_autospell(sd->autospell2, type2, type3, type4, BF_NORMAL|BF_SKILL, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4472. break;
  4473. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4474. if(sd->state.lr_flag != 2)
  4475. {
  4476. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4477. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4478. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, val, current_equip_card_id, target ? AUTOSPELL_FORCE_TARGET : AUTOSPELL_FORCE_SELF);
  4479. }
  4480. break;
  4481. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4482. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4483. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  4484. break;
  4485. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4486. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4487. if (sd->state.lr_flag != 2)
  4488. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4489. break;
  4490. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4491. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4492. if( sd->state.lr_flag != 2 )
  4493. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4494. break;
  4495. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4496. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4497. if(sd->state.lr_flag == 2)
  4498. break;
  4499. sd->def_set_race[type2].rate = type3;
  4500. sd->def_set_race[type2].tick = type4;
  4501. sd->def_set_race[type2].value = val;
  4502. break;
  4503. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4504. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4505. if(sd->state.lr_flag == 2)
  4506. break;
  4507. sd->mdef_set_race[type2].rate = type3;
  4508. sd->mdef_set_race[type2].tick = type4;
  4509. sd->mdef_set_race[type2].value = val;
  4510. break;
  4511. default:
  4512. if (current_equip_combo_pos > 0) {
  4513. 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);
  4514. }
  4515. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4516. 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);
  4517. }
  4518. else {
  4519. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4520. }
  4521. break;
  4522. }
  4523. }
  4524. /**
  4525. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4526. * @param sd
  4527. * @param type Bonus type used by bBonusName
  4528. * @param type2
  4529. * @param type3
  4530. * @param type4
  4531. * @param val Value that usually for rate or fixed value
  4532. */
  4533. void pc_bonus5(map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  4534. {
  4535. nullpo_retv(sd);
  4536. switch(type){
  4537. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4538. if(sd->state.lr_flag != 2)
  4539. pc_bonus_autospell(sd->autospell, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4540. break;
  4541. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4542. if(sd->state.lr_flag != 2)
  4543. pc_bonus_autospell(sd->autospell2, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4544. break;
  4545. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4546. if(sd->state.lr_flag != 2)
  4547. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4548. break;
  4549. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4550. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4551. if( sd->state.lr_flag != 2 )
  4552. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4553. break;
  4554. default:
  4555. if (current_equip_combo_pos > 0) {
  4556. 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);
  4557. }
  4558. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4559. 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);
  4560. }
  4561. else {
  4562. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4563. }
  4564. break;
  4565. }
  4566. }
  4567. /*==========================================
  4568. * Grants a player a given skill. Flag values are:
  4569. * 0 - Grant permanent skill to be bound to skill tree
  4570. * 1 - Grant an item skill (temporary)
  4571. * 2 - Like 1, except the level granted can stack with previously learned level.
  4572. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4573. *------------------------------------------*/
  4574. bool pc_skill(map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  4575. uint16 idx = 0;
  4576. nullpo_ret(sd);
  4577. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4578. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4579. return false;
  4580. }
  4581. if (level > MAX_SKILL_LEVEL) {
  4582. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4583. return false;
  4584. }
  4585. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4586. 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);
  4587. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4588. }
  4589. switch (type) {
  4590. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4591. sd->status.skill[idx].id = skill_id;
  4592. sd->status.skill[idx].lv = level;
  4593. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4594. if (level == 0) { //Remove skill.
  4595. sd->status.skill[idx].id = 0;
  4596. clif_deleteskill(sd,skill_id);
  4597. } else
  4598. clif_addskill(sd,skill_id);
  4599. 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.
  4600. status_calc_pc(sd, SCO_NONE);
  4601. break;
  4602. case ADDSKILL_TEMP: //Item bonus skill.
  4603. if (sd->status.skill[idx].id != 0) {
  4604. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4605. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4606. } else {
  4607. sd->status.skill[idx].id = skill_id;
  4608. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4609. }
  4610. sd->status.skill[idx].lv = max(sd->status.skill[idx].lv, level);
  4611. break;
  4612. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4613. if (sd->status.skill[idx].id != 0) {
  4614. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4615. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4616. } else {
  4617. sd->status.skill[idx].id = skill_id;
  4618. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4619. }
  4620. sd->status.skill[idx].lv += level;
  4621. break;
  4622. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4623. sd->status.skill[idx].id = skill_id;
  4624. sd->status.skill[idx].lv = level;
  4625. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4626. if (level == 0) { //Remove skill.
  4627. sd->status.skill[idx].id = 0;
  4628. clif_deleteskill(sd,skill_id);
  4629. } else
  4630. clif_addskill(sd,skill_id);
  4631. 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.
  4632. status_calc_pc(sd, SCO_NONE);
  4633. break;
  4634. default:
  4635. return false;
  4636. }
  4637. return true;
  4638. }
  4639. /**
  4640. * Set's a player's plagiarized skill.
  4641. * @param sd: Player
  4642. * @param skill_id: Skill to be plagiarized
  4643. * @param skill_lv: Skill level to be plagiarized
  4644. * @return True on success or false otherwise
  4645. */
  4646. bool pc_skill_plagiarism(map_session_data &sd, uint16 skill_id, uint16 skill_lv)
  4647. {
  4648. skill_id = skill_dummy2skill_id(skill_id);
  4649. uint16 idx = skill_get_index(skill_id);
  4650. // Use skill index, avoiding out-of-bound array [Cydh]
  4651. if (idx == 0) {
  4652. ShowWarning("pc_skill_plagiarism: invalid skill idx 0 for skill %d.\n", skill_id);
  4653. return false;
  4654. }
  4655. skill_lv = cap_value(skill_lv, 1, skill_get_max(skill_id));
  4656. int type = skill_isCopyable(&sd, skill_id);
  4657. if (type == 1) {
  4658. pc_skill_plagiarism_reset(sd, type);
  4659. sd.cloneskill_idx = idx;
  4660. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), skill_id);
  4661. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), skill_lv);
  4662. } else if (type == 2) {
  4663. pc_skill_plagiarism_reset(sd, type);
  4664. sd.reproduceskill_idx = idx;
  4665. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), skill_id);
  4666. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), skill_lv);
  4667. } else {
  4668. ShowWarning("pc_skill_plagiarism: skill %d is not copyable.\n", skill_id);
  4669. return false;
  4670. }
  4671. sd.status.skill[idx].id = skill_id;
  4672. sd.status.skill[idx].lv = static_cast<uint8>(skill_lv);
  4673. sd.status.skill[idx].flag = SKILL_FLAG_PLAGIARIZED;
  4674. clif_addskill(&sd, skill_id);
  4675. return true;
  4676. }
  4677. /**
  4678. * Clear plagiarized skills from a player.
  4679. * @param sd: Player
  4680. * @param type: 1 for Plagiarism or 2 for Reproduce
  4681. * @return True on success or false otherwise
  4682. */
  4683. bool pc_skill_plagiarism_reset(map_session_data &sd, uint8 type)
  4684. {
  4685. uint16 idx;
  4686. if (type == 1)
  4687. idx = sd.cloneskill_idx;
  4688. else if (type == 2)
  4689. idx = sd.reproduceskill_idx;
  4690. else {
  4691. ShowError("pc_skill_plagiarism_reset: Unknown type %d.\n", type);
  4692. return false;
  4693. }
  4694. if (sd.status.skill[idx].flag == SKILL_FLAG_PLAGIARIZED) {
  4695. uint16 skill_id = sd.status.skill[idx].id;
  4696. sd.status.skill[idx].id = 0;
  4697. sd.status.skill[idx].lv = 0;
  4698. sd.status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4699. clif_deleteskill(&sd, skill_id);
  4700. if (type == 1) {
  4701. sd.cloneskill_idx = 0;
  4702. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  4703. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  4704. }
  4705. else if (type == 2) {
  4706. sd.reproduceskill_idx = 0;
  4707. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), 0);
  4708. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  4709. }
  4710. }
  4711. return true;
  4712. }
  4713. /*==========================================
  4714. * Append a card to an item ?
  4715. *------------------------------------------*/
  4716. int pc_insert_card(map_session_data* sd, int idx_card, int idx_equip)
  4717. {
  4718. nullpo_ret(sd);
  4719. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4720. return 0;
  4721. }
  4722. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4723. return 0;
  4724. }
  4725. int i;
  4726. t_itemid nameid;
  4727. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4728. struct item_data* item_card = sd->inventory_data[idx_card];
  4729. if(item_eq == nullptr)
  4730. return 0; //Invalid item index.
  4731. if(item_card == nullptr)
  4732. return 0; //Invalid card index.
  4733. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4734. return 0; // target item missing
  4735. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4736. return 0; // target card missing
  4737. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4738. return 0; // only weapons and armor are allowed
  4739. if( item_card->type != IT_CARD )
  4740. return 0; // must be a card
  4741. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4742. return 0; // target must be identified
  4743. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4744. return 0; // card slots reserved for other purposes
  4745. if( (item_eq->equip & item_card->equip) == 0 )
  4746. return 0; // card cannot be compounded on this item type
  4747. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4748. return 0; // attempted to place shield card on left-hand weapon.
  4749. 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)) )
  4750. return 0; // specific accessory-card can only be inserted to specific accessory.
  4751. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4752. return 0; // item must be unequipped
  4753. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4754. if( i == item_eq->slots )
  4755. return 0; // no free slots
  4756. // remember the card id to insert
  4757. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4758. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4759. {// failed
  4760. clif_insert_card(sd,idx_equip,idx_card,1);
  4761. }
  4762. else
  4763. {// success
  4764. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4765. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4766. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4767. clif_insert_card(sd,idx_equip,idx_card,0);
  4768. }
  4769. return 0;
  4770. }
  4771. /**
  4772. * Returns the count of unidentified items with the option to identify too.
  4773. * @param sd: Player data
  4774. * @param identify_item: Whether or not to identify any unidentified items
  4775. * @return Unidentified items count
  4776. */
  4777. int pc_identifyall(map_session_data *sd, bool identify_item)
  4778. {
  4779. int unidentified_count = 0;
  4780. for (int i = 0; i < MAX_INVENTORY; i++) {
  4781. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4782. if (identify_item == true) {
  4783. sd->inventory.u.items_inventory[i].identify = 1;
  4784. clif_item_identified(sd,i,0);
  4785. }
  4786. unidentified_count++;
  4787. }
  4788. }
  4789. return unidentified_count;
  4790. }
  4791. //
  4792. // Items
  4793. //
  4794. /*==========================================
  4795. * Update buying value by skills
  4796. *------------------------------------------*/
  4797. int pc_modifybuyvalue(map_session_data *sd,int orig_value)
  4798. {
  4799. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4800. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4801. rate1 = 5+skill*2-((skill==10)? 1:0);
  4802. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4803. rate2 = 5+skill*4;
  4804. if(rate1 < rate2) rate1 = rate2;
  4805. if(rate1)
  4806. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4807. if(val < battle_config.min_shop_buy)
  4808. val = battle_config.min_shop_buy;
  4809. return val;
  4810. }
  4811. /*==========================================
  4812. * Update selling value by skills
  4813. *------------------------------------------*/
  4814. int pc_modifysellvalue(map_session_data *sd,int orig_value)
  4815. {
  4816. int skill,val = orig_value,rate = 0;
  4817. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4818. rate = 5+skill*2-((skill==10)? 1:0);
  4819. if(rate)
  4820. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4821. if (val < battle_config.min_shop_sell)
  4822. val = battle_config.min_shop_sell;
  4823. return val;
  4824. }
  4825. /*==========================================
  4826. * Checking if we have enough place on inventory for new item
  4827. * Make sure to take 30k as limit (for client I guess)
  4828. * @param sd
  4829. * @param nameid
  4830. * @param amount
  4831. * @return e_chkitem_result
  4832. *------------------------------------------*/
  4833. char pc_checkadditem(map_session_data *sd, t_itemid nameid, int amount)
  4834. {
  4835. int i;
  4836. struct item_data* data;
  4837. nullpo_ret(sd);
  4838. if(amount > MAX_AMOUNT)
  4839. return CHKADDITEM_OVERAMOUNT;
  4840. data = itemdb_search(nameid);
  4841. if(!itemdb_isstackable2(data))
  4842. return CHKADDITEM_NEW;
  4843. if( data->stack.inventory && amount > data->stack.amount )
  4844. return CHKADDITEM_OVERAMOUNT;
  4845. if (data->flag.guid)
  4846. return CHKADDITEM_NEW;
  4847. for(i=0;i<MAX_INVENTORY;i++){
  4848. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4849. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4850. 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 ) )
  4851. return CHKADDITEM_OVERAMOUNT;
  4852. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4853. if( i >= sd->status.inventory_slots ){
  4854. return CHKADDITEM_OVERAMOUNT;
  4855. }
  4856. return CHKADDITEM_EXIST;
  4857. }
  4858. }
  4859. return CHKADDITEM_NEW;
  4860. }
  4861. /*==========================================
  4862. * Return number of available place in inventory
  4863. * Each non stackable item will reduce place by 1
  4864. * @param sd
  4865. * @return Number of empty slots
  4866. *------------------------------------------*/
  4867. uint8 pc_inventoryblank(map_session_data *sd)
  4868. {
  4869. uint16 i;
  4870. uint8 b;
  4871. nullpo_ret(sd);
  4872. for(i = 0, b = 0; i < sd->status.inventory_slots; i++){
  4873. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4874. b++;
  4875. }
  4876. return b;
  4877. }
  4878. /**
  4879. * Attempts to remove zeny from player
  4880. * @param sd: Player
  4881. * @param zeny: Zeny removed
  4882. * @param type: Log type
  4883. * @param tsd: (optional) From who to log (if null take sd)
  4884. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4885. */
  4886. char pc_payzeny(map_session_data *sd, int zeny, enum e_log_pick_type type, map_session_data *tsd)
  4887. {
  4888. nullpo_retr(2,sd);
  4889. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4890. if( zeny < 0 )
  4891. {
  4892. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4893. return 1;
  4894. }
  4895. if( sd->status.zeny < zeny )
  4896. return 1; //Not enough.
  4897. sd->status.zeny -= zeny;
  4898. clif_updatestatus(sd,SP_ZENY);
  4899. if(!tsd) tsd = sd;
  4900. log_zeny(sd, type, tsd, -zeny);
  4901. if( zeny > 0 && sd->state.showzeny ) {
  4902. char output[255];
  4903. sprintf(output, "Removed %dz.", zeny);
  4904. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4905. }
  4906. return 0;
  4907. }
  4908. /**
  4909. * Attempts to give zeny to player
  4910. * @param sd: Player
  4911. * @param type: Log type
  4912. * @param tsd: (optional) From who to log (if null take sd)
  4913. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4914. */
  4915. char pc_getzeny(map_session_data *sd, int zeny, enum e_log_pick_type type, map_session_data *tsd)
  4916. {
  4917. nullpo_retr(-1,sd);
  4918. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4919. if( zeny < 0 )
  4920. {
  4921. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4922. return 1;
  4923. }
  4924. if( zeny > MAX_ZENY - sd->status.zeny )
  4925. zeny = MAX_ZENY - sd->status.zeny;
  4926. sd->status.zeny += zeny;
  4927. clif_updatestatus(sd,SP_ZENY);
  4928. if(!tsd) tsd = sd;
  4929. log_zeny(sd, type, tsd, zeny);
  4930. if( zeny > 0 && sd->state.showzeny ) {
  4931. char output[255];
  4932. sprintf(output, "Gained %dz.", zeny);
  4933. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4934. }
  4935. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4936. return 0;
  4937. }
  4938. /**
  4939. * Attempts to remove Cash Points from player
  4940. * @param sd: Player
  4941. * @param price: Total points (cash + kafra) the player has to pay
  4942. * @param points: Kafra points the player has to pay
  4943. * @param type: Log type
  4944. * @return -1: Not enough points, otherwise success (cash+points)
  4945. */
  4946. int pc_paycash(map_session_data *sd, int price, int points, e_log_pick_type type)
  4947. {
  4948. int cash;
  4949. nullpo_retr(-1,sd);
  4950. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  4951. cash = price-points;
  4952. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4953. {
  4954. 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);
  4955. return -1;
  4956. }
  4957. if( cash ){
  4958. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4959. sd->cashPoints -= cash;
  4960. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4961. }
  4962. if( points ){
  4963. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4964. sd->kafraPoints -= points;
  4965. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4966. }
  4967. if( battle_config.cashshop_show_points )
  4968. {
  4969. char output[CHAT_SIZE_MAX];
  4970. 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.
  4971. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4972. }
  4973. return cash+points;
  4974. }
  4975. /**
  4976. * Attempts to give Cash Points to player
  4977. * @param sd: Player
  4978. * @param cash: Cash points the player gets
  4979. * @param points: Kafra points the player gets
  4980. * @param type: Log type
  4981. * @return -1: Error, otherwise success (cash or points)
  4982. */
  4983. int pc_getcash(map_session_data *sd, int cash, int points, e_log_pick_type type)
  4984. {
  4985. char output[CHAT_SIZE_MAX];
  4986. nullpo_retr(-1,sd);
  4987. cash = cap_value(cash, 0, MAX_CASHPOINT); //prevent command UB
  4988. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  4989. if( cash > 0 )
  4990. {
  4991. if( cash > MAX_CASHPOINT-sd->cashPoints )
  4992. {
  4993. 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);
  4994. cash = MAX_CASHPOINT-sd->cashPoints;
  4995. }
  4996. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4997. sd->cashPoints += cash;
  4998. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4999. if( battle_config.cashshop_show_points )
  5000. {
  5001. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  5002. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5003. }
  5004. return cash;
  5005. }
  5006. if( points > 0 )
  5007. {
  5008. if( points > MAX_KAFRAPOINT-sd->kafraPoints )
  5009. {
  5010. 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);
  5011. points = MAX_KAFRAPOINT-sd->kafraPoints;
  5012. }
  5013. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  5014. sd->kafraPoints += points;
  5015. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  5016. if( battle_config.cashshop_show_points )
  5017. {
  5018. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  5019. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5020. }
  5021. return points;
  5022. }
  5023. return -1; //shouldn't happen but just in case
  5024. }
  5025. /**
  5026. * Searching a specified itemid in inventory and return his stored index
  5027. * @param sd Player
  5028. * @param nameid Find this Item!
  5029. * @return Stored index in inventory, or -1 if not found.
  5030. **/
  5031. short pc_search_inventory(map_session_data *sd, t_itemid nameid) {
  5032. short i;
  5033. nullpo_retr(-1, sd);
  5034. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  5035. return ( i < MAX_INVENTORY ) ? i : -1;
  5036. }
  5037. /** Attempt to add a new item to player inventory
  5038. * @param sd
  5039. * @param item_data
  5040. * @param amount
  5041. * @param log_type
  5042. * @return
  5043. * 0 = success
  5044. * 1 = invalid itemid not found or negative amount
  5045. * 2 = overweight
  5046. * 3 = ?
  5047. * 4 = no free place found
  5048. * 5 = max amount reached
  5049. * 6 = ?
  5050. * 7 = stack limitation
  5051. */
  5052. enum e_additem_result pc_additem(map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  5053. struct item_data *id;
  5054. int16 i;
  5055. unsigned int w;
  5056. nullpo_retr(ADDITEM_INVALID, sd);
  5057. nullpo_retr(ADDITEM_INVALID, item);
  5058. if( item->nameid == 0 || amount <= 0 )
  5059. return ADDITEM_INVALID;
  5060. if( amount > MAX_AMOUNT )
  5061. return ADDITEM_OVERAMOUNT;
  5062. id = itemdb_search(item->nameid);
  5063. if( id->stack.inventory && amount > id->stack.amount )
  5064. {// item stack limitation
  5065. return ADDITEM_STACKLIMIT;
  5066. }
  5067. w = id->weight*amount;
  5068. if(sd->weight + w > sd->max_weight)
  5069. return ADDITEM_OVERWEIGHT;
  5070. if (id->flag.guid && !item->unique_id)
  5071. item->unique_id = pc_generate_unique_id(sd);
  5072. // Stackable | Non Rental
  5073. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  5074. for( i = 0; i < MAX_INVENTORY; i++ ) {
  5075. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  5076. sd->inventory.u.items_inventory[i].bound == item->bound &&
  5077. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  5078. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  5079. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  5080. 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 ) )
  5081. return ADDITEM_OVERAMOUNT;
  5082. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  5083. if( i >= sd->status.inventory_slots ){
  5084. return ADDITEM_OVERAMOUNT;
  5085. }
  5086. sd->inventory.u.items_inventory[i].amount += amount;
  5087. clif_additem(sd,i,amount,0);
  5088. break;
  5089. }
  5090. }
  5091. }else{
  5092. i = MAX_INVENTORY;
  5093. }
  5094. if (i >= MAX_INVENTORY) {
  5095. i = pc_search_inventory(sd,0);
  5096. if( i < 0 )
  5097. return ADDITEM_OVERITEM;
  5098. if( i >= sd->status.inventory_slots ){
  5099. return ADDITEM_OVERITEM;
  5100. }
  5101. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  5102. // clear equip and favorite fields first, just in case
  5103. if( item->equip )
  5104. sd->inventory.u.items_inventory[i].equip = 0;
  5105. if( item->favorite )
  5106. sd->inventory.u.items_inventory[i].favorite = 0;
  5107. if( item->equipSwitch )
  5108. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  5109. sd->inventory.u.items_inventory[i].amount = amount;
  5110. sd->inventory_data[i] = id;
  5111. sd->last_addeditem_index = i;
  5112. if (!itemdb_isstackable2(id) || id->flag.guid)
  5113. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  5114. clif_additem(sd,i,amount,0);
  5115. }
  5116. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  5117. sd->weight += w;
  5118. clif_updatestatus(sd,SP_WEIGHT);
  5119. //Auto-equip
  5120. if(id->flag.autoequip)
  5121. pc_equipitem(sd, i, id->equip);
  5122. /* rental item check */
  5123. if( item->expire_time ) {
  5124. if( time(NULL) > item->expire_time ) {
  5125. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  5126. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  5127. } else {
  5128. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  5129. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  5130. pc_inventory_rental_add(sd, seconds);
  5131. }
  5132. }
  5133. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  5134. pc_show_questinfo(sd);
  5135. return ADDITEM_SUCCESS;
  5136. }
  5137. /*==========================================
  5138. * Remove an item at index n from inventory by amount.
  5139. * @param sd
  5140. * @param n Item index in inventory
  5141. * @param amount
  5142. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  5143. * @param reason Delete reason
  5144. * @param log_type e_log_pick_type
  5145. * @return 1 - invalid itemid or negative amount; 0 - Success
  5146. *------------------------------------------*/
  5147. char pc_delitem(map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  5148. {
  5149. nullpo_retr(1, sd);
  5150. if(n < 0 || sd->inventory.u.items_inventory[n].nameid == 0 || amount <= 0 || sd->inventory.u.items_inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  5151. return 1;
  5152. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  5153. sd->inventory.u.items_inventory[n].amount -= amount;
  5154. sd->weight -= sd->inventory_data[n]->weight*amount ;
  5155. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  5156. if(sd->inventory.u.items_inventory[n].equip)
  5157. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  5158. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  5159. sd->inventory_data[n] = NULL;
  5160. }
  5161. if(!(type&1))
  5162. clif_delitem(sd,n,amount,reason);
  5163. if(!(type&2))
  5164. clif_updatestatus(sd,SP_WEIGHT);
  5165. pc_show_questinfo(sd);
  5166. return 0;
  5167. }
  5168. /*==========================================
  5169. * Attempt to drop an item.
  5170. * @param sd
  5171. * @param n Item index in inventory
  5172. * @param amount Amount of item
  5173. * @return False = fail; True = success
  5174. *------------------------------------------*/
  5175. bool pc_dropitem(map_session_data *sd,int n,int amount)
  5176. {
  5177. nullpo_retr(1, sd);
  5178. if(n < 0 || n >= MAX_INVENTORY)
  5179. return false;
  5180. if(amount <= 0)
  5181. return false;
  5182. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  5183. sd->inventory.u.items_inventory[n].amount <= 0 ||
  5184. sd->inventory.u.items_inventory[n].amount < amount ||
  5185. sd->state.trading || sd->state.vending ||
  5186. !sd->inventory_data[n] //pc_delitem would fail on this case.
  5187. )
  5188. return false;
  5189. if( sd->inventory.u.items_inventory[n].equipSwitch )
  5190. return false;
  5191. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  5192. {
  5193. clif_displaymessage (sd->fd, msg_txt(sd,271));
  5194. return false; //Can't drop items in nodrop mapflag maps.
  5195. }
  5196. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  5197. {
  5198. clif_displaymessage (sd->fd, msg_txt(sd,263));
  5199. return false;
  5200. }
  5201. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  5202. return false;
  5203. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  5204. clif_dropitem(sd, n, amount);
  5205. return true;
  5206. }
  5207. /*==========================================
  5208. * Attempt to pick up an item.
  5209. * @param sd
  5210. * @param fitem Item that will be picked
  5211. * @return False = fail; True = success
  5212. *------------------------------------------*/
  5213. bool pc_takeitem(map_session_data *sd,struct flooritem_data *fitem)
  5214. {
  5215. int flag = 0;
  5216. t_tick tick = gettick();
  5217. struct party_data *p = NULL;
  5218. nullpo_ret(sd);
  5219. nullpo_ret(fitem);
  5220. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  5221. return false; // Distance is too far
  5222. if (sd->sc.cant.pickup)
  5223. return false;
  5224. if (sd->status.party_id)
  5225. p = party_search(sd->status.party_id);
  5226. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  5227. map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  5228. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  5229. if (!(p && p->party.item&1 &&
  5230. first_sd && first_sd->status.party_id == sd->status.party_id
  5231. ))
  5232. return false;
  5233. }
  5234. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  5235. map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  5236. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  5237. if (!(p && p->party.item&1 &&
  5238. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5239. (second_sd && second_sd->status.party_id == sd->status.party_id))
  5240. ))
  5241. return false;
  5242. }
  5243. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  5244. map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  5245. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  5246. if(!(p && p->party.item&1 &&
  5247. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5248. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  5249. (third_sd && third_sd->status.party_id == sd->status.party_id))
  5250. ))
  5251. return false;
  5252. }
  5253. }
  5254. }
  5255. }
  5256. //This function takes care of giving the item to whoever should have it, considering party-share options.
  5257. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  5258. clif_additem(sd,0,0,flag);
  5259. return true;
  5260. }
  5261. //Display pickup animation.
  5262. pc_stop_attack(sd);
  5263. clif_takeitem(&sd->bl,&fitem->bl);
  5264. if (fitem->mob_id &&
  5265. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  5266. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  5267. )
  5268. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  5269. map_clearflooritem(&fitem->bl);
  5270. return true;
  5271. }
  5272. /*==========================================
  5273. * Check if item is usable.
  5274. * Return:
  5275. * false = no
  5276. * true = yes
  5277. *------------------------------------------*/
  5278. bool pc_isUseitem(map_session_data *sd,int n)
  5279. {
  5280. struct item_data *item;
  5281. t_itemid nameid;
  5282. nullpo_ret(sd);
  5283. item = sd->inventory_data[n];
  5284. nameid = sd->inventory.u.items_inventory[n].nameid;
  5285. if( item == NULL )
  5286. return false;
  5287. //Not consumable item
  5288. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  5289. return false;
  5290. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  5291. return true;
  5292. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5293. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  5294. return false;
  5295. //Prevent mass item usage. [Skotlex]
  5296. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  5297. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  5298. )
  5299. return false;
  5300. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  5301. clif_msg(sd,ITEM_NOUSE_SITTING);
  5302. return false; // You cannot use this item while sitting.
  5303. }
  5304. if (sd->state.storage_flag && item->type != IT_CASH) {
  5305. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  5306. return false; // You cannot use this item while storage is open.
  5307. }
  5308. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  5309. return false;
  5310. if( itemdb_group.item_exists( IG_MF_NOTELEPORT, nameid ) ){
  5311. if( ( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2( mapdata ) ) ){
  5312. clif_skill_teleportmessage( sd, 0 );
  5313. return false;
  5314. }
  5315. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5316. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5317. return false;
  5318. }
  5319. }
  5320. if( itemdb_group.item_exists( IG_MF_NORETURN, nameid ) ){
  5321. if( mapdata->flag[MF_NORETURN] ){
  5322. return false;
  5323. }
  5324. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5325. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5326. return false;
  5327. }
  5328. }
  5329. if( itemdb_group.item_exists( IG_GIANT_FLY_WING, nameid ) ){
  5330. struct party_data *pd = party_search( sd->status.party_id );
  5331. if( pd ){
  5332. int i;
  5333. ARR_FIND( 0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader );
  5334. // User is not party leader
  5335. if( i == MAX_PARTY ){
  5336. clif_msg( sd, ITEM_PARTY_MEMBER_NOT_SUMMONED );
  5337. return false;
  5338. }
  5339. 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 ) );
  5340. // No party members found on same map
  5341. if( i == MAX_PARTY ){
  5342. clif_msg( sd, ITEM_PARTY_NO_MEMBER_IN_MAP );
  5343. return false;
  5344. }
  5345. }else{
  5346. clif_msg( sd, ITEM_PARTY_MEMBER_NOT_SUMMONED );
  5347. return false;
  5348. }
  5349. }
  5350. switch( nameid ) {
  5351. case ITEMID_MERCENARY_RED_POTION:
  5352. case ITEMID_MERCENARY_BLUE_POTION:
  5353. case ITEMID_M_CENTER_POTION:
  5354. case ITEMID_M_AWAKENING_POTION:
  5355. case ITEMID_M_BERSERK_POTION:
  5356. if( sd->md == NULL || sd->md->db == NULL )
  5357. return false;
  5358. if( sd->md->sc.cant.consume )
  5359. return false;
  5360. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  5361. return false;
  5362. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  5363. return false;
  5364. break;
  5365. case ITEMID_NEURALIZER:
  5366. if( !mapdata->flag[MF_RESET] )
  5367. return false;
  5368. break;
  5369. }
  5370. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  5371. return false; // Mercenary Scrolls
  5372. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  5373. if( pc_is90overweight(sd) ) {
  5374. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  5375. return false;
  5376. }
  5377. if( !pc_inventoryblank(sd) ) {
  5378. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  5379. return false;
  5380. }
  5381. }
  5382. //Gender check
  5383. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  5384. return false;
  5385. //Required level check
  5386. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  5387. return false;
  5388. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  5389. return false;
  5390. //Not equipable by class. [Skotlex]
  5391. if (!pc_job_can_use_item(sd,item))
  5392. return false;
  5393. if (sd->sc.cant.consume)
  5394. return false;
  5395. if (!pc_isItemClass(sd,item))
  5396. return false;
  5397. //Dead Branch items
  5398. if( item->flag.dead_branch )
  5399. log_branch(sd);
  5400. return true;
  5401. }
  5402. /*==========================================
  5403. * Last checks to use an item.
  5404. * Return:
  5405. * 0 = fail
  5406. * 1 = success
  5407. *------------------------------------------*/
  5408. int pc_useitem(map_session_data *sd,int n)
  5409. {
  5410. t_tick tick = gettick();
  5411. int amount;
  5412. t_itemid nameid;
  5413. struct script_code *script;
  5414. struct item item;
  5415. struct item_data *id;
  5416. nullpo_ret(sd);
  5417. if (sd->state.mail_writing)
  5418. return 0;
  5419. if (sd->npc_id) {
  5420. if (sd->progressbar.npc_id) {
  5421. clif_progressbar_abort(sd);
  5422. return 0; // First item use attempt cancels the progress bar
  5423. }
  5424. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  5425. #ifdef RENEWAL
  5426. clif_msg( sd, WORK_IN_PROGRESS );
  5427. #endif
  5428. return 0;
  5429. }
  5430. }
  5431. item = sd->inventory.u.items_inventory[n];
  5432. id = sd->inventory_data[n];
  5433. if (item.nameid == 0 || item.amount <= 0)
  5434. return 0;
  5435. if( !pc_isUseitem(sd,n) )
  5436. return 0;
  5437. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  5438. nameid = id->nameid;
  5439. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  5440. return 0;
  5441. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  5442. if( id->flag.delay_consume > 0 ) {
  5443. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.getSCE(SC_ALL_RIDING) )
  5444. return 0;
  5445. else if( pc_issit(sd) )
  5446. return 0;
  5447. }
  5448. //Since most delay-consume items involve using a "skill-type" target cursor,
  5449. //perform a skill-use check before going through. [Skotlex]
  5450. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  5451. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  5452. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  5453. return 0;
  5454. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  5455. return 0;
  5456. /* on restricted maps the item is consumed but the effect is not used */
  5457. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  5458. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  5459. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  5460. clif_useitemack(sd,n,item.amount-1,true);
  5461. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  5462. }
  5463. return 0;/* regardless, effect is not run */
  5464. }
  5465. if (pet_db_search(id->nameid, PET_CATCH) != nullptr && map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  5466. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  5467. return 0;
  5468. }
  5469. sd->itemid = item.nameid;
  5470. sd->itemindex = n;
  5471. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  5472. sd->catch_target_class = PET_CATCH_FAIL;
  5473. amount = item.amount;
  5474. script = id->script;
  5475. //Check if the item is to be consumed immediately [Skotlex]
  5476. if (id->flag.delay_consume > 0)
  5477. clif_useitemack(sd, n, amount, true);
  5478. else
  5479. {
  5480. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  5481. {
  5482. clif_useitemack(sd, n, amount - 1, true);
  5483. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  5484. }
  5485. else
  5486. clif_useitemack(sd, n, 0, false);
  5487. }
  5488. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  5489. potion_flag = 2; // Famous player's potions have 50% more efficiency
  5490. //Update item use time.
  5491. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  5492. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  5493. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  5494. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  5495. potion_flag = 0;
  5496. return 1;
  5497. }
  5498. /**
  5499. * Add item on cart for given index.
  5500. * @param sd
  5501. * @param item
  5502. * @param amount
  5503. * @param log_type
  5504. * @return See pc.hpp::e_additem_result
  5505. */
  5506. enum e_additem_result pc_cart_additem(map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  5507. {
  5508. struct item_data *data;
  5509. int i,w;
  5510. nullpo_retr(ADDITEM_INVALID, sd);
  5511. nullpo_retr(ADDITEM_INVALID, item);
  5512. if(item->nameid == 0 || amount <= 0)
  5513. return ADDITEM_INVALID;
  5514. if (itemdb_ishatched_egg(item))
  5515. return ADDITEM_INVALID;
  5516. data = itemdb_search(item->nameid);
  5517. if( data->stack.cart && amount > data->stack.amount )
  5518. {// item stack limitation
  5519. return ADDITEM_STACKLIMIT;
  5520. }
  5521. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  5522. { // Check item trade restrictions [Skotlex]
  5523. clif_displaymessage (sd->fd, msg_txt(sd,264));
  5524. return ADDITEM_INVALID;
  5525. }
  5526. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  5527. return ADDITEM_OVERWEIGHT;
  5528. i = MAX_CART;
  5529. if( itemdb_isstackable2(data) && !item->expire_time )
  5530. {
  5531. for (i = 0; i < MAX_CART; i++) {
  5532. if (sd->cart.u.items_cart[i].nameid == item->nameid
  5533. && sd->cart.u.items_cart[i].bound == item->bound
  5534. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5535. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5536. )
  5537. break;
  5538. }
  5539. }
  5540. if( i < MAX_CART )
  5541. {// item already in cart, stack it
  5542. 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 ) )
  5543. return ADDITEM_OVERAMOUNT; // no slot
  5544. sd->cart.u.items_cart[i].amount += amount;
  5545. clif_cart_additem(sd,i,amount);
  5546. }
  5547. else
  5548. {// item not stackable or not present, add it
  5549. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5550. if( i == MAX_CART )
  5551. return ADDITEM_OVERAMOUNT; // no slot
  5552. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5553. sd->cart.u.items_cart[i].id = 0;
  5554. sd->cart.u.items_cart[i].amount = amount;
  5555. sd->cart_num++;
  5556. clif_cart_additem(sd,i,amount);
  5557. }
  5558. sd->cart.u.items_cart[i].favorite = 0; // clear
  5559. sd->cart.u.items_cart[i].equipSwitch = 0;
  5560. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5561. sd->cart_weight += w;
  5562. clif_updatestatus(sd,SP_CARTINFO);
  5563. return ADDITEM_SUCCESS;
  5564. }
  5565. /*==========================================
  5566. * Delete item on cart for given index.
  5567. *------------------------------------------*/
  5568. void pc_cart_delitem(map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  5569. {
  5570. nullpo_retv(sd);
  5571. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5572. sd->cart.u.items_cart[n].amount < amount)
  5573. return;
  5574. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5575. sd->cart.u.items_cart[n].amount -= amount;
  5576. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5577. if(sd->cart.u.items_cart[n].amount <= 0) {
  5578. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5579. sd->cart_num--;
  5580. }
  5581. if(!type) {
  5582. clif_cart_delitem(sd,n,amount);
  5583. clif_updatestatus(sd,SP_CARTINFO);
  5584. }
  5585. }
  5586. /*==========================================
  5587. * Transfer item from inventory to cart.
  5588. *------------------------------------------*/
  5589. void pc_putitemtocart(map_session_data *sd,int idx,int amount)
  5590. {
  5591. nullpo_retv(sd);
  5592. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5593. return;
  5594. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5595. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5596. return;
  5597. if( item_data->equipSwitch ){
  5598. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  5599. return;
  5600. }
  5601. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5602. if (flag == ADDITEM_SUCCESS)
  5603. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5604. else {
  5605. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  5606. clif_additem(sd, idx, amount, 0);
  5607. clif_delitem(sd, idx, amount, 0);
  5608. }
  5609. }
  5610. /*==========================================
  5611. * Get number of item in cart.
  5612. * Return:
  5613. -1 = itemid not found or no amount found
  5614. x = remaining itemid on cart after get
  5615. *------------------------------------------*/
  5616. int pc_cartitem_amount(map_session_data* sd, int idx, int amount)
  5617. {
  5618. struct item* item_data;
  5619. nullpo_retr(-1, sd);
  5620. item_data = &sd->cart.u.items_cart[idx];
  5621. if( item_data->nameid == 0 || item_data->amount == 0 )
  5622. return -1;
  5623. return item_data->amount - amount;
  5624. }
  5625. /*==========================================
  5626. * Retrieve an item at index idx from cart.
  5627. *------------------------------------------*/
  5628. void pc_getitemfromcart(map_session_data *sd,int idx,int amount)
  5629. {
  5630. nullpo_retv(sd);
  5631. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5632. return;
  5633. struct item *item_data=&sd->cart.u.items_cart[idx];
  5634. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5635. return;
  5636. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5637. if (flag == ADDITEM_SUCCESS)
  5638. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5639. else {
  5640. clif_cart_delitem(sd, idx, amount);
  5641. clif_additem(sd, idx, amount, flag);
  5642. clif_cart_additem(sd, idx, amount);
  5643. }
  5644. }
  5645. /*==========================================
  5646. * Bound Item Check
  5647. * Type:
  5648. * 1 Account Bound
  5649. * 2 Guild Bound
  5650. * 3 Party Bound
  5651. * 4 Character Bound
  5652. *------------------------------------------*/
  5653. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  5654. {
  5655. int i = 0, j = 0;
  5656. for(i = 0; i < MAX_INVENTORY; i++) {
  5657. 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) {
  5658. idxlist[j] = i;
  5659. j++;
  5660. }
  5661. }
  5662. return j;
  5663. }
  5664. /*==========================================
  5665. * Display item stolen msg to player sd
  5666. *------------------------------------------*/
  5667. int pc_show_steal(struct block_list *bl,va_list ap)
  5668. {
  5669. map_session_data *sd;
  5670. t_itemid itemid;
  5671. char output[100];
  5672. sd=va_arg(ap,map_session_data *);
  5673. itemid=va_arg(ap,int);
  5674. std::shared_ptr<item_data> id = item_db.find(itemid);
  5675. if(id == nullptr)
  5676. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5677. else
  5678. sprintf(output,"%s stole %s.",sd->status.name,id->ename.c_str());
  5679. clif_displaymessage( ((map_session_data *)bl)->fd, output);
  5680. return 0;
  5681. }
  5682. /**
  5683. * Steal an item from bl (mob).
  5684. * @param sd: Player data
  5685. * @param bl: Object to steal from
  5686. * @param skill_lv: Level of skill used
  5687. * @return True on success or false otherwise
  5688. */
  5689. bool pc_steal_item(map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5690. {
  5691. int i;
  5692. t_itemid itemid;
  5693. double rate;
  5694. unsigned char flag = 0;
  5695. struct status_data *sd_status, *md_status;
  5696. struct mob_data *md;
  5697. if(!sd || !bl || bl->type!=BL_MOB)
  5698. return false;
  5699. md = (TBL_MOB *)bl;
  5700. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5701. return false;
  5702. sd_status= status_get_status_data(&sd->bl);
  5703. md_status= status_get_status_data(bl);
  5704. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5705. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5706. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5707. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5708. ) { //Can't steal from
  5709. md->state.steal_flag = UCHAR_MAX;
  5710. return false;
  5711. }
  5712. // base skill success chance (percentual)
  5713. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5714. rate += sd->bonus.add_steal_rate;
  5715. if( rate < 1
  5716. #ifdef RENEWAL
  5717. || rnd()%100 >= rate
  5718. #endif
  5719. )
  5720. return false;
  5721. // Try dropping one item, in the order from first to last possible slot.
  5722. // Droprate is affected by the skill success rate.
  5723. for( i = 0; i < MAX_MOB_DROP; i++ )
  5724. if( item_db.exists(md->db->dropitem[i].nameid) && !md->db->dropitem[i].steal_protected && rnd() % 10000 < md->db->dropitem[i].rate
  5725. #ifndef RENEWAL
  5726. * rate/100.
  5727. #endif
  5728. )
  5729. break;
  5730. if( i == MAX_MOB_DROP )
  5731. return false;
  5732. itemid = md->db->dropitem[i].nameid;
  5733. struct item tmp_item = {};
  5734. tmp_item.nameid = itemid;
  5735. tmp_item.amount = 1;
  5736. tmp_item.identify = itemdb_isidentified(itemid);
  5737. if( battle_config.skill_steal_random_options ){
  5738. mob_setdropitem_option( &tmp_item, &md->db->dropitem[i] );
  5739. }
  5740. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5741. //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?
  5742. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5743. if(flag) { //Failed to steal due to overweight
  5744. clif_additem(sd,0,0,flag);
  5745. return false;
  5746. }
  5747. if(battle_config.show_steal_in_same_party)
  5748. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5749. //Logs items, Stolen from mobs [Lupus]
  5750. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5751. //A Rare Steal Global Announce by Lupus
  5752. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5753. struct item_data *i_data;
  5754. char message[128];
  5755. i_data = itemdb_search(itemid);
  5756. 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);
  5757. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5758. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5759. }
  5760. return true;
  5761. }
  5762. /*==========================================
  5763. * Stole zeny from bl (mob)
  5764. * return
  5765. * 0 = fail
  5766. * 1 = success
  5767. *------------------------------------------*/
  5768. int pc_steal_coin(map_session_data *sd,struct block_list *target)
  5769. {
  5770. int rate, target_lv;
  5771. struct mob_data *md;
  5772. if(!sd || !target || target->type != BL_MOB)
  5773. return 0;
  5774. md = (TBL_MOB*)target;
  5775. target_lv = status_get_lv(target);
  5776. if (md->state.steal_coin_flag || md->sc.getSCE(SC_STONE) || md->sc.getSCE(SC_FREEZE) || md->sc.getSCE(SC_HANDICAPSTATE_FROSTBITE) ||
  5777. md->sc.getSCE(SC_HANDICAPSTATE_SWOONING) || md->sc.getSCE(SC_HANDICAPSTATE_LIGHTNINGSTRIKE) || md->sc.getSCE(SC_HANDICAPSTATE_CRYSTALLIZATION) ||
  5778. status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5779. return 0;
  5780. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5781. if(rnd()%1000 < rate)
  5782. {
  5783. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5784. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5785. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5786. md->state.steal_coin_flag = 1;
  5787. return 1;
  5788. }
  5789. return 0;
  5790. }
  5791. /*==========================================
  5792. * Set's a player position.
  5793. * @param sd
  5794. * @param mapindex
  5795. * @param x
  5796. * @param y
  5797. * @param clrtype
  5798. * @return SETPOS_OK Success
  5799. * SETPOS_MAPINDEX Invalid map index
  5800. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5801. * SETPOS_AUTOTRADE Player is in autotrade state
  5802. *------------------------------------------*/
  5803. enum e_setpos pc_setpos(map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5804. {
  5805. nullpo_retr(SETPOS_OK,sd);
  5806. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5807. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5808. return SETPOS_MAPINDEX;
  5809. }
  5810. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5811. return SETPOS_AUTOTRADE;
  5812. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5813. pc_setstand(sd, true);
  5814. pc_setrestartvalue(sd,1);
  5815. }
  5816. int16 m = map_mapindex2mapid(mapindex);
  5817. struct map_data *mapdata = map_getmapdata(m);
  5818. status_change *sc = status_get_sc(&sd->bl);
  5819. sd->state.changemap = (sd->mapindex != mapindex);
  5820. sd->state.warping = 1;
  5821. sd->state.workinprogress = WIP_DISABLE_NONE;
  5822. sd->state.mail_writing = false;
  5823. sd->state.refineui_open = false;
  5824. if( sd->state.changemap ) { // Misc map-changing settings
  5825. int curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5826. if (curr_map_instance_id != new_map_instance_id) {
  5827. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5828. instance_delusers(curr_map_instance_id);
  5829. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5830. }
  5831. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5832. instance_addusers(new_map_instance_id);
  5833. }
  5834. if (sd->bg_id && mapdata && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5835. bg_team_leave(sd, false, true);
  5836. sd->state.pmap = sd->bl.m;
  5837. if (sc && sc->count) { // Cancel some map related stuff.
  5838. if (sc->cant.warp)
  5839. return SETPOS_MAPINDEX; // You may not get out!
  5840. for (const auto &it : status_db) {
  5841. if (sc->getSCE(it.first)) {
  5842. if (it.second->flag[SCF_REMOVEONMAPWARP])
  5843. status_change_end(&sd->bl, static_cast<sc_type>(it.first));
  5844. if (it.second->flag[SCF_RESTARTONMAPWARP] && it.second->skill_id > 0) {
  5845. status_change_entry *sce = sd->sc.getSCE(it.first);
  5846. if (sce->timer != INVALID_TIMER)
  5847. delete_timer(sce->timer, status_change_timer);
  5848. sce->timer = add_timer(gettick() + skill_get_time(it.second->skill_id, sce->val1), status_change_timer, sd->bl.id, it.first);
  5849. }
  5850. }
  5851. }
  5852. }
  5853. for(int i = 0; i < EQI_MAX; i++ ) {
  5854. if( sd->equip_index[i] >= 0 )
  5855. if( pc_isequip(sd,sd->equip_index[i]) )
  5856. pc_unequipitem(sd,sd->equip_index[i],2);
  5857. }
  5858. if (battle_config.clear_unit_onwarp&BL_PC)
  5859. skill_clear_unitgroup(&sd->bl);
  5860. if( battle_config.loose_ap_on_map && mapdata_flag_vs( mapdata ) ){
  5861. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  5862. }
  5863. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5864. guild_send_dot_remove(sd);
  5865. bg_send_dot_remove(sd);
  5866. if (sd->regen.state.gc)
  5867. sd->regen.state.gc = 0;
  5868. if (mapdata) {
  5869. // make sure vending is allowed here
  5870. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5871. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5872. vending_closevending(sd);
  5873. }
  5874. // make sure buyingstore is allowed here
  5875. if (sd->state.buyingstore && mapdata->flag[MF_NOBUYINGSTORE]) {
  5876. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5877. buyingstore_close(sd);
  5878. }
  5879. }
  5880. channel_pcquit(sd,4); //quit map chan
  5881. // Remove Cloaked NPC if changing to another map
  5882. for (auto it = sd->cloaked_npc.begin(); it != sd->cloaked_npc.end(); ++it) {
  5883. block_list *npc_bl = map_id2bl(*it);
  5884. if (npc_bl && npc_bl->m != m) {
  5885. sd->cloaked_npc.erase(it);
  5886. break;
  5887. }
  5888. }
  5889. }
  5890. if( m < 0 )
  5891. {
  5892. uint32 ip;
  5893. uint16 port;
  5894. struct script_state *st;
  5895. //if can't find any map-servers, just abort setting position.
  5896. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5897. return SETPOS_NO_MAPSERVER;
  5898. if (sd->npc_id){
  5899. npc_event_dequeue(sd,false);
  5900. st = sd->st;
  5901. }else{
  5902. st = nullptr;
  5903. }
  5904. if (sd->bg_id) // Switching map servers, remove from bg
  5905. bg_team_leave(sd, false, true);
  5906. if (sd->state.vending) // Stop vending
  5907. vending_closevending(sd);
  5908. if (sd->state.buyingstore) // Stop buyingstore
  5909. buyingstore_close(sd);
  5910. npc_script_event(sd, NPCE_LOGOUT);
  5911. //remove from map, THEN change x/y coordinates
  5912. unit_remove_map_pc(sd,clrtype);
  5913. sd->mapindex = mapindex;
  5914. sd->bl.x=x;
  5915. sd->bl.y=y;
  5916. pc_clean_skilltree(sd);
  5917. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5918. chrif_changemapserver(sd, ip, (short)port);
  5919. //Free session data from this map server [Kevin]
  5920. unit_free_pc(sd);
  5921. if( st ){
  5922. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5923. st->state = END;
  5924. }
  5925. return SETPOS_OK;
  5926. }
  5927. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5928. {
  5929. 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);
  5930. x = y = 0; // make it random
  5931. }
  5932. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5933. int c=0;
  5934. do {
  5935. x = rnd()%(mapdata->xs-2)+1;
  5936. y = rnd()%(mapdata->ys-2)+1;
  5937. c++;
  5938. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5939. 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));
  5940. return SETPOS_OK; //preventing warp
  5941. //break; //allow warp anyway
  5942. }
  5943. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5944. }
  5945. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5946. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5947. vending_closevending(sd);
  5948. }
  5949. if (sd->state.buyingstore && map_getcell(m, x, y, CELL_CHKNOBUYINGSTORE)) {
  5950. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  5951. buyingstore_close(sd);
  5952. }
  5953. if(sd->bl.prev != NULL){
  5954. unit_remove_map_pc(sd,clrtype);
  5955. clif_changemap(sd,m,x,y); // [MouseJstr]
  5956. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5957. sd->state.rewarp = 1;
  5958. sd->mapindex = mapindex;
  5959. sd->bl.m = m;
  5960. sd->bl.x = sd->ud.to_x = x;
  5961. sd->bl.y = sd->ud.to_y = y;
  5962. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5963. { // Increased guild castle regen [Valaris]
  5964. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  5965. if(gc && gc->guild_id == sd->status.guild_id)
  5966. sd->regen.state.gc = 1;
  5967. }
  5968. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5969. {
  5970. sd->pd->bl.m = m;
  5971. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5972. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5973. sd->pd->ud.dir = sd->ud.dir;
  5974. }
  5975. if( hom_is_active(sd->hd) )
  5976. {
  5977. sd->hd->bl.m = m;
  5978. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5979. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5980. sd->hd->ud.dir = sd->ud.dir;
  5981. }
  5982. if( sd->md )
  5983. {
  5984. sd->md->bl.m = m;
  5985. sd->md->bl.x = sd->md->ud.to_x = x;
  5986. sd->md->bl.y = sd->md->ud.to_y = y;
  5987. sd->md->ud.dir = sd->ud.dir;
  5988. }
  5989. if( sd->ed ) {
  5990. sd->ed->bl.m = m;
  5991. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5992. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5993. sd->ed->ud.dir = sd->ud.dir;
  5994. }
  5995. pc_cell_basilica(sd);
  5996. //check if we gonna be rewarped [lighta]
  5997. if(npc_check_areanpc(1,m,x,y,0)){
  5998. sd->count_rewarp++;
  5999. }
  6000. else
  6001. sd->count_rewarp = 0;
  6002. if (sd->state.vending)
  6003. vending_update(*sd);
  6004. if (sd->state.buyingstore)
  6005. buyingstore_update(*sd);
  6006. return SETPOS_OK;
  6007. }
  6008. enum e_setpos pc_setpos_savepoint( map_session_data& sd, clr_type clrtype ){
  6009. struct map_data *mapdata = map_getmapdata( sd.bl.m );
  6010. if( mapdata != nullptr && mapdata->flag[MF_NOSAVE] && mapdata->save.map ){
  6011. return pc_setpos( &sd, mapdata->save.map, mapdata->save.x, mapdata->save.y, clrtype );
  6012. }else{
  6013. return pc_setpos( &sd, mapindex_name2id( sd.status.save_point.map ), sd.status.save_point.x, sd.status.save_point.y, clrtype );
  6014. }
  6015. }
  6016. /*==========================================
  6017. * Warp player sd to random location on current map.
  6018. * May fail if no walkable cell found (1000 attempts).
  6019. * Return:
  6020. * 0 = Success
  6021. * 1,2,3 = Fail
  6022. *------------------------------------------*/
  6023. char pc_randomwarp(map_session_data *sd, clr_type type, bool ignore_mapflag)
  6024. {
  6025. int x,y,i=0;
  6026. nullpo_ret(sd);
  6027. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6028. if (mapdata->flag[MF_NOTELEPORT] && !ignore_mapflag) //Teleport forbidden
  6029. return 3;
  6030. do {
  6031. x = rnd()%(mapdata->xs-2)+1;
  6032. y = rnd()%(mapdata->ys-2)+1;
  6033. } 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);
  6034. if (i < 1000)
  6035. return pc_setpos(sd,mapdata->index,x,y,type);
  6036. return 3;
  6037. }
  6038. /*==========================================
  6039. * Records a memo point at sd's current position
  6040. * pos - entry to replace, (-1: shift oldest entry out)
  6041. *------------------------------------------*/
  6042. bool pc_memo(map_session_data* sd, int pos)
  6043. {
  6044. int skill;
  6045. nullpo_ret(sd);
  6046. // check mapflags
  6047. 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) ) {
  6048. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  6049. return false;
  6050. }
  6051. // check inputs
  6052. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  6053. return false; // invalid input
  6054. // check required skill level
  6055. skill = pc_checkskill(sd, AL_WARP);
  6056. if( skill < 1 ) {
  6057. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  6058. return false;
  6059. }
  6060. if( skill < 2 || skill - 2 < pos ) {
  6061. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  6062. return false;
  6063. }
  6064. if( pos == -1 )
  6065. {
  6066. uint8 i;
  6067. const char* mapname = map_mapid2mapname( sd->bl.m );
  6068. // prevent memo-ing the same map multiple times
  6069. ARR_FIND( 0, MAX_MEMOPOINTS, i, strncmp( sd->status.memo_point[i].map, mapname, sizeof( sd->status.memo_point[i].map ) ) == 0 );
  6070. memmove( &sd->status.memo_point[1], &sd->status.memo_point[0], ( u8min( i, MAX_MEMOPOINTS - 1 ) ) * sizeof( struct s_point_str ) );
  6071. pos = 0;
  6072. }
  6073. if( map_getmapdata(sd->bl.m)->instance_id ) {
  6074. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  6075. return false;
  6076. }
  6077. safestrncpy( sd->status.memo_point[pos].map, map_mapid2mapname( sd->bl.m ), sizeof( sd->status.memo_point[pos].map ) );
  6078. sd->status.memo_point[pos].x = sd->bl.x;
  6079. sd->status.memo_point[pos].y = sd->bl.y;
  6080. clif_skill_memomessage(sd, 0);
  6081. return true;
  6082. }
  6083. //
  6084. // Skills
  6085. //
  6086. /**
  6087. * Get the skill current cooldown for player.
  6088. * (get the db base cooldown for skill + player specific cooldown)
  6089. * @param sd : player pointer
  6090. * @param id : skill id
  6091. * @param lv : skill lv
  6092. * @return player skill cooldown
  6093. */
  6094. int pc_get_skillcooldown(map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  6095. if (skill_id == SJ_NOVAEXPLOSING) {
  6096. status_change *sc = status_get_sc(&sd->bl);
  6097. if (sc && sc->getSCE(SC_DIMENSION))
  6098. return 0;
  6099. }
  6100. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  6101. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  6102. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  6103. for (auto &it : sd->skillcooldown) {
  6104. if (it.id == skill_id) {
  6105. cooldown += it.val;
  6106. break;
  6107. }
  6108. }
  6109. return max(0, cooldown);
  6110. }
  6111. /*==========================================
  6112. * Return player sd skill_lv learned for given skill
  6113. *------------------------------------------*/
  6114. uint8 pc_checkskill(map_session_data *sd, uint16 skill_id)
  6115. {
  6116. uint16 idx = 0;
  6117. if (sd == NULL)
  6118. return 0;
  6119. #ifdef RENEWAL
  6120. if ((idx = skill_get_index(skill_id)) == 0) {
  6121. #else
  6122. if( ( idx = skill_db.get_index( skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__ ) ) == 0 ){
  6123. if( skill_id >= RK_ENCHANTBLADE ){
  6124. // Silently fail for now -> future update planned
  6125. return 0;
  6126. }
  6127. #endif
  6128. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  6129. return 0;
  6130. }
  6131. if (SKILL_CHK_GUILD(skill_id) ) {
  6132. struct guild *g;
  6133. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  6134. return guild_checkskill(g,skill_id);
  6135. return 0;
  6136. }
  6137. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  6138. }
  6139. /**
  6140. * Returns the flag of the given skill (when learned).
  6141. * @param sd: Player data
  6142. * @param skill_id: Skill to lookup
  6143. * @return Skill flag type
  6144. */
  6145. e_skill_flag pc_checkskill_flag(map_session_data &sd, uint16 skill_id) {
  6146. uint16 idx;
  6147. #ifdef RENEWAL
  6148. if ((idx = skill_get_index(skill_id)) == 0) {
  6149. #else
  6150. if ((idx = skill_db.get_index(skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__)) == 0) {
  6151. if (skill_id >= RK_ENCHANTBLADE) {
  6152. // Silently fail for now -> future update planned
  6153. return SKILL_FLAG_NONE;
  6154. }
  6155. #endif
  6156. ShowError("pc_checkskill_flag: Invalid skill id %d (char_id=%d).\n", skill_id, sd.status.char_id);
  6157. return SKILL_FLAG_NONE;
  6158. }
  6159. 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;
  6160. }
  6161. /**
  6162. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  6163. * @param sd: Player data
  6164. * @param type: Summoner Power Type
  6165. * @return Skill points invested
  6166. */
  6167. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  6168. if (sd == nullptr)
  6169. return 0;
  6170. uint8 count = 0;
  6171. switch (type) {
  6172. case SUMMONER_POWER_SEA:
  6173. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  6174. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  6175. break;
  6176. case SUMMONER_POWER_LAND:
  6177. 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) +
  6178. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  6179. break;
  6180. case SUMMONER_POWER_LIFE:
  6181. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  6182. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  6183. break;
  6184. }
  6185. return count;
  6186. }
  6187. /**
  6188. * Checks for Imperial Guard's passive skills.
  6189. * @param sd: Player data
  6190. * @param flag:
  6191. * Flag&1 = IG_SHIELD_MASTERY
  6192. * Flag&2 = IG_SPEAR_SWORD_M
  6193. */
  6194. uint8 pc_checkskill_imperial_guard(map_session_data *sd, short flag)
  6195. {
  6196. nullpo_retr(0, sd);
  6197. uint8 count = 0;
  6198. if (flag&1 && sd->status.shield > 0)
  6199. count += pc_checkskill(sd, IG_SHIELD_MASTERY);
  6200. if (flag&2 && (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_1HSPEAR || sd->status.weapon == W_2HSPEAR))
  6201. count += pc_checkskill(sd, IG_SPEAR_SWORD_M);
  6202. return count;
  6203. }
  6204. /**
  6205. * Check if we still have the correct weapon to continue the skill (actually status)
  6206. * If not ending it
  6207. * @param sd
  6208. * @return 0:error, 1:check done
  6209. */
  6210. static void pc_checkallowskill(map_session_data *sd)
  6211. {
  6212. nullpo_retv(sd);
  6213. if(!sd->sc.count)
  6214. return;
  6215. for (const auto &it : status_db) {
  6216. sc_type status = it.second->type;
  6217. std::bitset<SCF_MAX> flag = it.second->flag;
  6218. if (flag[SCF_REQUIREWEAPON]) { // Skills requiring specific weapon types
  6219. if (status == SC_DANCING && !battle_config.dancing_weaponswitch_fix)
  6220. continue;
  6221. if (sd->sc.getSCE(status) && !pc_check_weapontype(sd, skill_get_weapontype(it.second->skill_id)))
  6222. status_change_end(&sd->bl, status);
  6223. }
  6224. if (flag[SCF_REQUIRESHIELD]) { // Skills requiring a shield
  6225. if (sd->sc.getSCE(status) && sd->status.shield <= 0)
  6226. status_change_end(&sd->bl, status);
  6227. }
  6228. }
  6229. }
  6230. /*==========================================
  6231. * Return equipped index of item on player sd at pos
  6232. * Return
  6233. * -1 : Nothing equipped
  6234. * idx : (this index could be used in inventory to found item_data)
  6235. *------------------------------------------*/
  6236. short pc_checkequip(map_session_data *sd,int pos, bool checkall)
  6237. {
  6238. uint8 i;
  6239. nullpo_retr(-1, sd);
  6240. for(i=0;i<EQI_MAX;i++){
  6241. if(pos & equip_bitmask[i]){
  6242. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  6243. // Check all if any match is found
  6244. continue;
  6245. }
  6246. return sd->equip_index[i];
  6247. }
  6248. }
  6249. return -1;
  6250. }
  6251. /*==========================================
  6252. * Check if sd has nameid equipped somewhere
  6253. * @sd : the player session
  6254. * @nameid : id of the item to check
  6255. * @min : : see pc.hpp enum equip_index from ? to @max
  6256. * @max : see pc.hpp enum equip_index for @min to ?
  6257. * -return true,false
  6258. *------------------------------------------*/
  6259. bool pc_checkequip2(map_session_data *sd, t_itemid nameid, int min, int max)
  6260. {
  6261. int i;
  6262. for(i = min; i < max; i++) {
  6263. if(equip_bitmask[i]) {
  6264. int idx = sd->equip_index[i];
  6265. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  6266. return true;
  6267. }
  6268. }
  6269. return false;
  6270. }
  6271. /*==========================================
  6272. * Convert's from the client's lame Job ID system
  6273. * to the map server's 'makes sense' system. [Skotlex]
  6274. *------------------------------------------*/
  6275. uint64 pc_jobid2mapid(unsigned short b_class)
  6276. {
  6277. switch(b_class)
  6278. {
  6279. //Novice And 1-1 Jobs
  6280. case JOB_NOVICE: return MAPID_NOVICE;
  6281. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  6282. case JOB_MAGE: return MAPID_MAGE;
  6283. case JOB_ARCHER: return MAPID_ARCHER;
  6284. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  6285. case JOB_MERCHANT: return MAPID_MERCHANT;
  6286. case JOB_THIEF: return MAPID_THIEF;
  6287. case JOB_TAEKWON: return MAPID_TAEKWON;
  6288. case JOB_WEDDING: return MAPID_WEDDING;
  6289. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  6290. case JOB_NINJA: return MAPID_NINJA;
  6291. case JOB_XMAS: return MAPID_XMAS;
  6292. case JOB_SUMMER: return MAPID_SUMMER;
  6293. case JOB_HANBOK: return MAPID_HANBOK;
  6294. case JOB_GANGSI: return MAPID_GANGSI;
  6295. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  6296. case JOB_SUMMER2: return MAPID_SUMMER2;
  6297. //2-1 Jobs
  6298. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  6299. case JOB_KNIGHT: return MAPID_KNIGHT;
  6300. case JOB_WIZARD: return MAPID_WIZARD;
  6301. case JOB_HUNTER: return MAPID_HUNTER;
  6302. case JOB_PRIEST: return MAPID_PRIEST;
  6303. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  6304. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  6305. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  6306. case JOB_KAGEROU:
  6307. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  6308. case JOB_REBELLION: return MAPID_REBELLION;
  6309. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  6310. //2-2 Jobs
  6311. case JOB_CRUSADER: return MAPID_CRUSADER;
  6312. case JOB_SAGE: return MAPID_SAGE;
  6313. case JOB_BARD:
  6314. case JOB_DANCER: return MAPID_BARDDANCER;
  6315. case JOB_MONK: return MAPID_MONK;
  6316. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  6317. case JOB_ROGUE: return MAPID_ROGUE;
  6318. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  6319. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  6320. //Trans Novice And Trans 1-1 Jobs
  6321. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  6322. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  6323. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  6324. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  6325. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  6326. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  6327. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  6328. //Trans 2-1 Jobs
  6329. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  6330. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  6331. case JOB_SNIPER: return MAPID_SNIPER;
  6332. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  6333. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  6334. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  6335. //Trans 2-2 Jobs
  6336. case JOB_PALADIN: return MAPID_PALADIN;
  6337. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  6338. case JOB_CLOWN:
  6339. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  6340. case JOB_CHAMPION: return MAPID_CHAMPION;
  6341. case JOB_CREATOR: return MAPID_CREATOR;
  6342. case JOB_STALKER: return MAPID_STALKER;
  6343. //Baby Novice And Baby 1-1 Jobs
  6344. case JOB_BABY: return MAPID_BABY;
  6345. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  6346. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  6347. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  6348. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  6349. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  6350. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  6351. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  6352. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  6353. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  6354. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  6355. //Baby 2-1 Jobs
  6356. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  6357. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  6358. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  6359. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  6360. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  6361. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  6362. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  6363. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  6364. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  6365. case JOB_BABY_KAGEROU:
  6366. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  6367. //Baby 2-2 Jobs
  6368. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  6369. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  6370. case JOB_BABY_BARD:
  6371. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  6372. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  6373. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  6374. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  6375. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  6376. //3-1 Jobs
  6377. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  6378. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  6379. case JOB_WARLOCK: return MAPID_WARLOCK;
  6380. case JOB_RANGER: return MAPID_RANGER;
  6381. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  6382. case JOB_MECHANIC: return MAPID_MECHANIC;
  6383. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  6384. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  6385. //3-2 Jobs
  6386. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  6387. case JOB_SORCERER: return MAPID_SORCERER;
  6388. case JOB_MINSTREL:
  6389. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  6390. case JOB_SURA: return MAPID_SURA;
  6391. case JOB_GENETIC: return MAPID_GENETIC;
  6392. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  6393. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  6394. //Trans 3-1 Jobs
  6395. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  6396. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  6397. case JOB_RANGER_T: return MAPID_RANGER_T;
  6398. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  6399. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  6400. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  6401. //Trans 3-2 Jobs
  6402. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  6403. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  6404. case JOB_MINSTREL_T:
  6405. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  6406. case JOB_SURA_T: return MAPID_SURA_T;
  6407. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  6408. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  6409. //Baby 3-1 Jobs
  6410. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  6411. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  6412. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  6413. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  6414. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  6415. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  6416. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  6417. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  6418. //Baby 3-2 Jobs
  6419. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  6420. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  6421. case JOB_BABY_MINSTREL:
  6422. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  6423. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  6424. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  6425. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  6426. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  6427. //Doram Jobs
  6428. case JOB_SUMMONER: return MAPID_SUMMONER;
  6429. case JOB_SPIRIT_HANDLER: return MAPID_SPIRIT_HANDLER;
  6430. //4-1 Jobs
  6431. case JOB_HYPER_NOVICE: return MAPID_HYPER_NOVICE;
  6432. case JOB_DRAGON_KNIGHT: return MAPID_DRAGON_KNIGHT;
  6433. case JOB_ARCH_MAGE: return MAPID_ARCH_MAGE;
  6434. case JOB_WINDHAWK: return MAPID_WINDHAWK;
  6435. case JOB_CARDINAL: return MAPID_CARDINAL;
  6436. case JOB_MEISTER: return MAPID_MEISTER;
  6437. case JOB_SHADOW_CROSS: return MAPID_SHADOW_CROSS;
  6438. case JOB_SKY_EMPEROR: return MAPID_SKY_EMPEROR;
  6439. case JOB_NIGHT_WATCH: return MAPID_NIGHT_WATCH;
  6440. case JOB_SHINKIRO:
  6441. case JOB_SHIRANUI: return MAPID_SHINKIRO_SHIRANUI;
  6442. //4-2 Jobs
  6443. case JOB_IMPERIAL_GUARD: return MAPID_IMPERIAL_GUARD;
  6444. case JOB_ELEMENTAL_MASTER: return MAPID_ELEMENTAL_MASTER;
  6445. case JOB_INQUISITOR: return MAPID_INQUISITOR;
  6446. case JOB_TROUBADOUR:
  6447. case JOB_TROUVERE: return MAPID_TROUBADOURTROUVERE;
  6448. case JOB_BIOLO: return MAPID_BIOLO;
  6449. case JOB_ABYSS_CHASER: return MAPID_ABYSS_CHASER;
  6450. case JOB_SOUL_ASCETIC: return MAPID_SOUL_ASCETIC;
  6451. //Unknown
  6452. default:
  6453. return -1;
  6454. }
  6455. }
  6456. //Reverts the map-style class id to the client-style one.
  6457. int pc_mapid2jobid(uint64 class_, int sex)
  6458. {
  6459. switch(class_) {
  6460. //Novice And 1-1 Jobs
  6461. case MAPID_NOVICE: return JOB_NOVICE;
  6462. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  6463. case MAPID_MAGE: return JOB_MAGE;
  6464. case MAPID_ARCHER: return JOB_ARCHER;
  6465. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  6466. case MAPID_MERCHANT: return JOB_MERCHANT;
  6467. case MAPID_THIEF: return JOB_THIEF;
  6468. case MAPID_TAEKWON: return JOB_TAEKWON;
  6469. case MAPID_WEDDING: return JOB_WEDDING;
  6470. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  6471. case MAPID_NINJA: return JOB_NINJA;
  6472. case MAPID_XMAS: return JOB_XMAS;
  6473. case MAPID_SUMMER: return JOB_SUMMER;
  6474. case MAPID_HANBOK: return JOB_HANBOK;
  6475. case MAPID_GANGSI: return JOB_GANGSI;
  6476. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  6477. case MAPID_SUMMER2: return JOB_SUMMER2;
  6478. //2-1 Jobs
  6479. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  6480. case MAPID_KNIGHT: return JOB_KNIGHT;
  6481. case MAPID_WIZARD: return JOB_WIZARD;
  6482. case MAPID_HUNTER: return JOB_HUNTER;
  6483. case MAPID_PRIEST: return JOB_PRIEST;
  6484. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  6485. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  6486. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  6487. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  6488. case MAPID_REBELLION: return JOB_REBELLION;
  6489. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  6490. //2-2 Jobs
  6491. case MAPID_CRUSADER: return JOB_CRUSADER;
  6492. case MAPID_SAGE: return JOB_SAGE;
  6493. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  6494. case MAPID_MONK: return JOB_MONK;
  6495. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  6496. case MAPID_ROGUE: return JOB_ROGUE;
  6497. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  6498. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  6499. //Trans Novice And Trans 2-1 Jobs
  6500. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  6501. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  6502. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  6503. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  6504. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  6505. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  6506. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  6507. //Trans 2-1 Jobs
  6508. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  6509. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  6510. case MAPID_SNIPER: return JOB_SNIPER;
  6511. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  6512. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  6513. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  6514. //Trans 2-2 Jobs
  6515. case MAPID_PALADIN: return JOB_PALADIN;
  6516. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  6517. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  6518. case MAPID_CHAMPION: return JOB_CHAMPION;
  6519. case MAPID_CREATOR: return JOB_CREATOR;
  6520. case MAPID_STALKER: return JOB_STALKER;
  6521. //Baby Novice And Baby 1-1 Jobs
  6522. case MAPID_BABY: return JOB_BABY;
  6523. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  6524. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  6525. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  6526. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  6527. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  6528. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  6529. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  6530. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  6531. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  6532. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  6533. //Baby 2-1 Jobs
  6534. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  6535. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  6536. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  6537. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  6538. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  6539. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  6540. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  6541. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  6542. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  6543. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  6544. //Baby 2-2 Jobs
  6545. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  6546. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  6547. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  6548. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  6549. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  6550. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  6551. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  6552. //3-1 Jobs
  6553. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  6554. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  6555. case MAPID_WARLOCK: return JOB_WARLOCK;
  6556. case MAPID_RANGER: return JOB_RANGER;
  6557. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  6558. case MAPID_MECHANIC: return JOB_MECHANIC;
  6559. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  6560. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  6561. //3-2 Jobs
  6562. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  6563. case MAPID_SORCERER: return JOB_SORCERER;
  6564. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  6565. case MAPID_SURA: return JOB_SURA;
  6566. case MAPID_GENETIC: return JOB_GENETIC;
  6567. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  6568. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  6569. //Trans 3-1 Jobs
  6570. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  6571. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  6572. case MAPID_RANGER_T: return JOB_RANGER_T;
  6573. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  6574. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  6575. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  6576. //Trans 3-2 Jobs
  6577. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  6578. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  6579. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  6580. case MAPID_SURA_T: return JOB_SURA_T;
  6581. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  6582. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  6583. //Baby 3-1 Jobs
  6584. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  6585. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  6586. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  6587. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  6588. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  6589. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  6590. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  6591. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  6592. //Baby 3-2 Jobs
  6593. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  6594. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  6595. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  6596. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  6597. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  6598. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  6599. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  6600. //Doram Jobs
  6601. case MAPID_SUMMONER: return JOB_SUMMONER;
  6602. case MAPID_SPIRIT_HANDLER: return JOB_SPIRIT_HANDLER;
  6603. //4-1 Jobs
  6604. case MAPID_HYPER_NOVICE: return JOB_HYPER_NOVICE;
  6605. case MAPID_DRAGON_KNIGHT: return JOB_DRAGON_KNIGHT;
  6606. case MAPID_ARCH_MAGE: return JOB_ARCH_MAGE;
  6607. case MAPID_WINDHAWK: return JOB_WINDHAWK;
  6608. case MAPID_CARDINAL: return JOB_CARDINAL;
  6609. case MAPID_MEISTER: return JOB_MEISTER;
  6610. case MAPID_SHADOW_CROSS: return JOB_SHADOW_CROSS;
  6611. case MAPID_SKY_EMPEROR: return JOB_SKY_EMPEROR;
  6612. case MAPID_NIGHT_WATCH: return JOB_NIGHT_WATCH;
  6613. case MAPID_SHINKIRO_SHIRANUI: return sex?JOB_SHINKIRO:JOB_SHIRANUI;
  6614. //4-2 Jobs
  6615. case MAPID_IMPERIAL_GUARD: return JOB_IMPERIAL_GUARD;
  6616. case MAPID_ELEMENTAL_MASTER: return JOB_ELEMENTAL_MASTER;
  6617. case MAPID_INQUISITOR: return JOB_INQUISITOR;
  6618. case MAPID_TROUBADOURTROUVERE: return sex?JOB_TROUBADOUR:JOB_TROUVERE;
  6619. case MAPID_BIOLO: return JOB_BIOLO;
  6620. case MAPID_ABYSS_CHASER: return JOB_ABYSS_CHASER;
  6621. case MAPID_SOUL_ASCETIC: return JOB_SOUL_ASCETIC;
  6622. //Unknown
  6623. default:
  6624. return -1;
  6625. }
  6626. }
  6627. /*====================================================
  6628. * This function return the name of the job (by [Yor])
  6629. *----------------------------------------------------*/
  6630. const char* job_name(int class_)
  6631. {
  6632. switch (class_) {
  6633. case JOB_NOVICE:
  6634. case JOB_SWORDMAN:
  6635. case JOB_MAGE:
  6636. case JOB_ARCHER:
  6637. case JOB_ACOLYTE:
  6638. case JOB_MERCHANT:
  6639. case JOB_THIEF:
  6640. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  6641. case JOB_KNIGHT:
  6642. case JOB_PRIEST:
  6643. case JOB_WIZARD:
  6644. case JOB_BLACKSMITH:
  6645. case JOB_HUNTER:
  6646. case JOB_ASSASSIN:
  6647. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  6648. case JOB_KNIGHT2:
  6649. return msg_txt(NULL,557);
  6650. case JOB_CRUSADER:
  6651. case JOB_MONK:
  6652. case JOB_SAGE:
  6653. case JOB_ROGUE:
  6654. case JOB_ALCHEMIST:
  6655. case JOB_BARD:
  6656. case JOB_DANCER:
  6657. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  6658. case JOB_CRUSADER2:
  6659. return msg_txt(NULL,563);
  6660. case JOB_WEDDING:
  6661. case JOB_SUPER_NOVICE:
  6662. case JOB_GUNSLINGER:
  6663. case JOB_NINJA:
  6664. case JOB_XMAS:
  6665. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  6666. case JOB_SUMMER:
  6667. case JOB_SUMMER2:
  6668. return msg_txt(NULL,621);
  6669. case JOB_HANBOK:
  6670. return msg_txt(NULL,694);
  6671. case JOB_OKTOBERFEST:
  6672. return msg_txt(NULL,696);
  6673. case JOB_NOVICE_HIGH:
  6674. case JOB_SWORDMAN_HIGH:
  6675. case JOB_MAGE_HIGH:
  6676. case JOB_ARCHER_HIGH:
  6677. case JOB_ACOLYTE_HIGH:
  6678. case JOB_MERCHANT_HIGH:
  6679. case JOB_THIEF_HIGH:
  6680. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  6681. case JOB_LORD_KNIGHT:
  6682. case JOB_HIGH_PRIEST:
  6683. case JOB_HIGH_WIZARD:
  6684. case JOB_WHITESMITH:
  6685. case JOB_SNIPER:
  6686. case JOB_ASSASSIN_CROSS:
  6687. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  6688. case JOB_LORD_KNIGHT2:
  6689. return msg_txt(NULL,582);
  6690. case JOB_PALADIN:
  6691. case JOB_CHAMPION:
  6692. case JOB_PROFESSOR:
  6693. case JOB_STALKER:
  6694. case JOB_CREATOR:
  6695. case JOB_CLOWN:
  6696. case JOB_GYPSY:
  6697. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  6698. case JOB_PALADIN2:
  6699. return msg_txt(NULL,588);
  6700. case JOB_BABY:
  6701. case JOB_BABY_SWORDMAN:
  6702. case JOB_BABY_MAGE:
  6703. case JOB_BABY_ARCHER:
  6704. case JOB_BABY_ACOLYTE:
  6705. case JOB_BABY_MERCHANT:
  6706. case JOB_BABY_THIEF:
  6707. return msg_txt(NULL,595 - JOB_BABY + class_);
  6708. case JOB_BABY_KNIGHT:
  6709. case JOB_BABY_PRIEST:
  6710. case JOB_BABY_WIZARD:
  6711. case JOB_BABY_BLACKSMITH:
  6712. case JOB_BABY_HUNTER:
  6713. case JOB_BABY_ASSASSIN:
  6714. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  6715. case JOB_BABY_KNIGHT2:
  6716. return msg_txt(NULL,602);
  6717. case JOB_BABY_CRUSADER:
  6718. case JOB_BABY_MONK:
  6719. case JOB_BABY_SAGE:
  6720. case JOB_BABY_ROGUE:
  6721. case JOB_BABY_ALCHEMIST:
  6722. case JOB_BABY_BARD:
  6723. case JOB_BABY_DANCER:
  6724. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  6725. case JOB_BABY_CRUSADER2:
  6726. return msg_txt(NULL,608);
  6727. case JOB_SUPER_BABY:
  6728. return msg_txt(NULL,615);
  6729. case JOB_TAEKWON:
  6730. return msg_txt(NULL,616);
  6731. case JOB_STAR_GLADIATOR:
  6732. case JOB_STAR_GLADIATOR2:
  6733. return msg_txt(NULL,617);
  6734. case JOB_SOUL_LINKER:
  6735. return msg_txt(NULL,618);
  6736. case JOB_GANGSI:
  6737. case JOB_DEATH_KNIGHT:
  6738. case JOB_DARK_COLLECTOR:
  6739. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  6740. case JOB_RUNE_KNIGHT:
  6741. case JOB_WARLOCK:
  6742. case JOB_RANGER:
  6743. case JOB_ARCH_BISHOP:
  6744. case JOB_MECHANIC:
  6745. case JOB_GUILLOTINE_CROSS:
  6746. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  6747. case JOB_RUNE_KNIGHT_T:
  6748. case JOB_WARLOCK_T:
  6749. case JOB_RANGER_T:
  6750. case JOB_ARCH_BISHOP_T:
  6751. case JOB_MECHANIC_T:
  6752. case JOB_GUILLOTINE_CROSS_T:
  6753. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  6754. case JOB_ROYAL_GUARD:
  6755. case JOB_SORCERER:
  6756. case JOB_MINSTREL:
  6757. case JOB_WANDERER:
  6758. case JOB_SURA:
  6759. case JOB_GENETIC:
  6760. case JOB_SHADOW_CHASER:
  6761. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  6762. case JOB_ROYAL_GUARD_T:
  6763. case JOB_SORCERER_T:
  6764. case JOB_MINSTREL_T:
  6765. case JOB_WANDERER_T:
  6766. case JOB_SURA_T:
  6767. case JOB_GENETIC_T:
  6768. case JOB_SHADOW_CHASER_T:
  6769. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  6770. case JOB_RUNE_KNIGHT2:
  6771. case JOB_RUNE_KNIGHT_T2:
  6772. return msg_txt(NULL,625);
  6773. case JOB_ROYAL_GUARD2:
  6774. case JOB_ROYAL_GUARD_T2:
  6775. return msg_txt(NULL,631);
  6776. case JOB_RANGER2:
  6777. case JOB_RANGER_T2:
  6778. return msg_txt(NULL,627);
  6779. case JOB_MECHANIC2:
  6780. case JOB_MECHANIC_T2:
  6781. return msg_txt(NULL,629);
  6782. case JOB_BABY_RUNE_KNIGHT:
  6783. case JOB_BABY_WARLOCK:
  6784. case JOB_BABY_RANGER:
  6785. case JOB_BABY_ARCH_BISHOP:
  6786. case JOB_BABY_MECHANIC:
  6787. case JOB_BABY_GUILLOTINE_CROSS:
  6788. case JOB_BABY_ROYAL_GUARD:
  6789. case JOB_BABY_SORCERER:
  6790. case JOB_BABY_MINSTREL:
  6791. case JOB_BABY_WANDERER:
  6792. case JOB_BABY_SURA:
  6793. case JOB_BABY_GENETIC:
  6794. case JOB_BABY_SHADOW_CHASER:
  6795. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6796. case JOB_BABY_RUNE_KNIGHT2:
  6797. return msg_txt(NULL,638);
  6798. case JOB_BABY_ROYAL_GUARD2:
  6799. return msg_txt(NULL,644);
  6800. case JOB_BABY_RANGER2:
  6801. return msg_txt(NULL,640);
  6802. case JOB_BABY_MECHANIC2:
  6803. return msg_txt(NULL,642);
  6804. case JOB_SUPER_NOVICE_E:
  6805. case JOB_SUPER_BABY_E:
  6806. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  6807. case JOB_KAGEROU:
  6808. case JOB_OBORO:
  6809. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  6810. case JOB_REBELLION:
  6811. return msg_txt(NULL,695);
  6812. case JOB_SUMMONER:
  6813. return msg_txt(NULL,697);
  6814. case JOB_BABY_SUMMONER:
  6815. return msg_txt(NULL,698);
  6816. case JOB_BABY_NINJA:
  6817. return msg_txt(NULL,699);
  6818. case JOB_BABY_KAGEROU:
  6819. case JOB_BABY_OBORO:
  6820. case JOB_BABY_TAEKWON:
  6821. case JOB_BABY_STAR_GLADIATOR:
  6822. case JOB_BABY_SOUL_LINKER:
  6823. case JOB_BABY_GUNSLINGER:
  6824. case JOB_BABY_REBELLION:
  6825. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  6826. case JOB_BABY_STAR_GLADIATOR2:
  6827. return msg_txt(NULL,756);
  6828. case JOB_STAR_EMPEROR:
  6829. case JOB_SOUL_REAPER:
  6830. case JOB_BABY_STAR_EMPEROR:
  6831. case JOB_BABY_SOUL_REAPER:
  6832. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  6833. case JOB_STAR_EMPEROR2:
  6834. return msg_txt(NULL,782);
  6835. case JOB_BABY_STAR_EMPEROR2:
  6836. return msg_txt(NULL,784);
  6837. case JOB_DRAGON_KNIGHT:
  6838. case JOB_MEISTER:
  6839. case JOB_SHADOW_CROSS:
  6840. case JOB_ARCH_MAGE:
  6841. case JOB_CARDINAL:
  6842. case JOB_WINDHAWK:
  6843. case JOB_IMPERIAL_GUARD:
  6844. case JOB_BIOLO:
  6845. case JOB_ABYSS_CHASER:
  6846. case JOB_ELEMENTAL_MASTER:
  6847. case JOB_INQUISITOR:
  6848. case JOB_TROUBADOUR:
  6849. case JOB_TROUVERE:
  6850. return msg_txt( nullptr, 800 - JOB_DRAGON_KNIGHT + class_ );
  6851. case JOB_WINDHAWK2:
  6852. return msg_txt( nullptr, 805);
  6853. case JOB_MEISTER2:
  6854. return msg_txt( nullptr, 801 );
  6855. case JOB_DRAGON_KNIGHT2:
  6856. return msg_txt( nullptr, 800 );
  6857. case JOB_IMPERIAL_GUARD2:
  6858. return msg_txt( nullptr, 806 );
  6859. case JOB_SKY_EMPEROR:
  6860. case JOB_SOUL_ASCETIC:
  6861. case JOB_SHINKIRO:
  6862. case JOB_SHIRANUI:
  6863. case JOB_NIGHT_WATCH:
  6864. case JOB_HYPER_NOVICE:
  6865. case JOB_SPIRIT_HANDLER:
  6866. return msg_txt( nullptr, 813 - JOB_SKY_EMPEROR + class_ );
  6867. case JOB_SKY_EMPEROR2:
  6868. return msg_txt( nullptr, 813 );
  6869. default:
  6870. return msg_txt(NULL,655);
  6871. }
  6872. }
  6873. /*====================================================
  6874. * Timered function to make id follow a target.
  6875. * @id = bl.id (player only atm)
  6876. * target is define in sd->followtarget (bl.id)
  6877. * used by pc_follow
  6878. *----------------------------------------------------*/
  6879. TIMER_FUNC(pc_follow_timer){
  6880. map_session_data *sd;
  6881. struct block_list *tbl;
  6882. sd = map_id2sd(id);
  6883. nullpo_ret(sd);
  6884. if (sd->followtimer != tid){
  6885. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6886. sd->followtimer = INVALID_TIMER;
  6887. return 0;
  6888. }
  6889. sd->followtimer = INVALID_TIMER;
  6890. tbl = map_id2bl(sd->followtarget);
  6891. if (tbl == NULL || pc_isdead(sd))
  6892. {
  6893. pc_stop_following(sd);
  6894. return 0;
  6895. }
  6896. // either player or target is currently detached from map blocks (could be teleporting),
  6897. // but still connected to this map, so we'll just increment the timer and check back later
  6898. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  6899. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6900. {
  6901. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  6902. if (!check_distance_bl(&sd->bl, tbl, 5))
  6903. unit_walktobl(&sd->bl, tbl, 5, 0);
  6904. } else
  6905. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  6906. }
  6907. sd->followtimer = add_timer(
  6908. tick + 1000, // increase time a bit to loosen up map's load
  6909. pc_follow_timer, sd->bl.id, 0);
  6910. return 0;
  6911. }
  6912. int pc_stop_following (map_session_data *sd)
  6913. {
  6914. nullpo_ret(sd);
  6915. if (sd->followtimer != INVALID_TIMER) {
  6916. delete_timer(sd->followtimer,pc_follow_timer);
  6917. sd->followtimer = INVALID_TIMER;
  6918. }
  6919. sd->followtarget = -1;
  6920. sd->ud.target_to = 0;
  6921. unit_stop_walking(&sd->bl, 1);
  6922. return 0;
  6923. }
  6924. int pc_follow(map_session_data *sd,int target_id)
  6925. {
  6926. struct block_list *bl = map_id2bl(target_id);
  6927. if (bl == NULL /*|| bl->type != BL_PC*/)
  6928. return 1;
  6929. if (sd->followtimer != INVALID_TIMER)
  6930. pc_stop_following(sd);
  6931. sd->followtarget = target_id;
  6932. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6933. return 0;
  6934. }
  6935. int pc_checkbaselevelup(map_session_data *sd) {
  6936. t_exp next = pc_nextbaseexp(sd);
  6937. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  6938. return 0;
  6939. uint32 base_level = sd->status.base_level;
  6940. do {
  6941. sd->status.base_exp -= next;
  6942. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6943. 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 )
  6944. sd->status.base_exp = next-1;
  6945. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level);
  6946. sd->status.trait_point += statpoint_db.pc_gets_trait_point(sd->status.base_level);
  6947. sd->status.base_level++;
  6948. if( pc_is_maxbaselv(sd) ){
  6949. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  6950. break;
  6951. }
  6952. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  6953. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  6954. status_calc_pet(sd->pd,SCO_NONE);
  6955. clif_updatestatus(sd,SP_STATUSPOINT);
  6956. clif_updatestatus(sd,SP_TRAITPOINT);
  6957. clif_updatestatus(sd,SP_BASELEVEL);
  6958. clif_updatestatus(sd,SP_BASEEXP);
  6959. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6960. status_calc_pc(sd,SCO_FORCE);
  6961. status_percent_heal(&sd->bl,100,100);
  6962. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6963. for (const auto &status : status_db) {
  6964. if (status.second->flag[SCF_SUPERNOVICEANGEL])
  6965. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 1, skill_get_time(status.second->skill_id, 1));
  6966. }
  6967. if (sd->state.snovice_dead_flag)
  6968. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6969. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6970. for (const auto &status : status_db) {
  6971. if (status.second->flag[SCF_TAEKWONANGEL])
  6972. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 10, 600000);
  6973. }
  6974. }
  6975. clif_misceffect(&sd->bl,0);
  6976. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6977. if(sd->status.party_id)
  6978. party_send_levelup(sd);
  6979. pc_baselevelchanged(sd);
  6980. for (; base_level <= sd->status.base_level; base_level++) {
  6981. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  6982. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  6983. }
  6984. return 1;
  6985. }
  6986. void pc_baselevelchanged(map_session_data *sd) {
  6987. uint8 i;
  6988. for( i = 0; i < EQI_MAX; i++ ) {
  6989. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  6990. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6991. pc_unequipitem(sd, sd->equip_index[i], 3);
  6992. }
  6993. }
  6994. pc_show_questinfo(sd);
  6995. }
  6996. int pc_checkjoblevelup(map_session_data *sd)
  6997. {
  6998. t_exp next = pc_nextjobexp(sd);
  6999. nullpo_ret(sd);
  7000. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  7001. return 0;
  7002. uint32 job_level = sd->status.job_level;
  7003. do {
  7004. sd->status.job_exp -= next;
  7005. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7006. 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 )
  7007. sd->status.job_exp = next-1;
  7008. sd->status.job_level ++;
  7009. sd->status.skill_point ++;
  7010. if( pc_is_maxjoblv(sd) ){
  7011. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  7012. break;
  7013. }
  7014. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  7015. clif_updatestatus(sd,SP_JOBLEVEL);
  7016. clif_updatestatus(sd,SP_JOBEXP);
  7017. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7018. clif_updatestatus(sd,SP_SKILLPOINT);
  7019. status_calc_pc(sd,SCO_FORCE);
  7020. clif_misceffect(&sd->bl,1);
  7021. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7022. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7023. npc_script_event(sd, NPCE_JOBLVUP);
  7024. for (; job_level <= sd->status.job_level; job_level++)
  7025. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  7026. pc_show_questinfo(sd);
  7027. return 1;
  7028. }
  7029. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  7030. * @param sd Player
  7031. * @param base_exp Base EXP before peronal bonuses
  7032. * @param job_exp Job EXP before peronal bonuses
  7033. * @param src Block list that affecting the exp calculation
  7034. */
  7035. static void pc_calcexp(map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  7036. {
  7037. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  7038. if (src) {
  7039. struct status_data *status = status_get_status_data(src);
  7040. if( sd->indexed_bonus.expaddrace[status->race] )
  7041. bonus += sd->indexed_bonus.expaddrace[status->race];
  7042. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  7043. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  7044. if( sd->indexed_bonus.expaddclass[status->class_] )
  7045. bonus += sd->indexed_bonus.expaddclass[status->class_];
  7046. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  7047. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  7048. if (battle_config.pk_mode &&
  7049. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  7050. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  7051. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  7052. vip_bonus_base = battle_config.vip_base_exp_increase;
  7053. vip_bonus_job = battle_config.vip_job_exp_increase;
  7054. }
  7055. }
  7056. // Give EXPBOOST for quests even if src is NULL.
  7057. if (sd->sc.getSCE(SC_EXPBOOST)) {
  7058. bonus += sd->sc.getSCE(SC_EXPBOOST)->val1;
  7059. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  7060. bonus += (sd->sc.getSCE(SC_EXPBOOST)->val1 / battle_config.vip_bm_increase);
  7061. }
  7062. if (*base_exp) {
  7063. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  7064. *base_exp = cap_value(exp, 1, MAX_EXP);
  7065. }
  7066. // Give JEXPBOOST for quests even if src is NULL.
  7067. if (sd->sc.getSCE(SC_JEXPBOOST))
  7068. bonus += sd->sc.getSCE(SC_JEXPBOOST)->val1;
  7069. if (*job_exp) {
  7070. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  7071. *job_exp = cap_value(exp, 1, MAX_EXP);
  7072. }
  7073. return;
  7074. }
  7075. /**
  7076. * Show EXP gained by player in percentage by @showexp
  7077. * @param sd Player
  7078. * @param base_exp Base EXP gained/loss
  7079. * @param next_base_exp Base EXP needed for next base level
  7080. * @param job_exp Job EXP gained/loss
  7081. * @param next_job_exp Job EXP needed for next job level
  7082. * @param lost True:EXP penalty, lose EXP
  7083. **/
  7084. 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) {
  7085. char output[CHAT_SIZE_MAX];
  7086. nullpo_retv(sd);
  7087. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  7088. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  7089. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  7090. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  7091. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  7092. }
  7093. /**
  7094. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  7095. * @param sd Player
  7096. * @param src EXP source
  7097. * @param base_exp Base EXP gained
  7098. * @param base_exp Job EXP gained
  7099. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  7100. * @return
  7101. **/
  7102. void pc_gainexp(map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  7103. {
  7104. t_exp nextb = 0, nextj = 0;
  7105. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  7106. nullpo_retv(sd);
  7107. if(sd->bl.prev == NULL || pc_isdead(sd))
  7108. return;
  7109. if (!(exp_flag&2)) {
  7110. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  7111. return; // no exp on pvp maps
  7112. if (sd->status.guild_id>0)
  7113. base_exp -= guild_payexp(sd,base_exp);
  7114. }
  7115. flag = ((base_exp) ? 1 : 0) |
  7116. ((job_exp) ? 2 : 0) |
  7117. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  7118. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  7119. if (!(exp_flag&2))
  7120. pc_calcexp(sd, &base_exp, &job_exp, src);
  7121. nextb = pc_nextbaseexp(sd);
  7122. nextj = pc_nextjobexp(sd);
  7123. if (flag&4){
  7124. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  7125. base_exp = 0;
  7126. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  7127. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  7128. }
  7129. if (flag&8){
  7130. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  7131. job_exp = 0;
  7132. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  7133. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  7134. }
  7135. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  7136. //Note that this value should never be greater than the original
  7137. //therefore no overflow checks are needed. [Skotlex]
  7138. if (nextb > 0) {
  7139. float nextbp = (float) base_exp / (float) nextb;
  7140. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  7141. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  7142. }
  7143. if (nextj > 0) {
  7144. float nextjp = (float) job_exp / (float) nextj;
  7145. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  7146. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  7147. }
  7148. }
  7149. // Give EXP for Base Level
  7150. if (base_exp) {
  7151. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  7152. if (!pc_checkbaselevelup(sd))
  7153. clif_updatestatus(sd,SP_BASEEXP);
  7154. }
  7155. // Give EXP for Job Level
  7156. if (job_exp) {
  7157. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  7158. if (!pc_checkjoblevelup(sd))
  7159. clif_updatestatus(sd,SP_JOBEXP);
  7160. }
  7161. if (flag&1)
  7162. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  7163. if (flag&2)
  7164. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  7165. if (sd->state.showexp && (base_exp || job_exp))
  7166. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  7167. }
  7168. /**
  7169. * Lost Base/Job EXP from a player
  7170. * @param sd Player
  7171. * @param base_exp Base EXP lost
  7172. * @param job_exp Job EXP lost
  7173. **/
  7174. void pc_lostexp(map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  7175. nullpo_retv(sd);
  7176. if (base_exp) {
  7177. base_exp = u64min(sd->status.base_exp, base_exp);
  7178. sd->status.base_exp -= base_exp;
  7179. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  7180. clif_updatestatus(sd, SP_BASEEXP);
  7181. }
  7182. if (job_exp) {
  7183. job_exp = u64min(sd->status.job_exp, job_exp);
  7184. sd->status.job_exp -= job_exp;
  7185. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  7186. clif_updatestatus(sd, SP_JOBEXP);
  7187. }
  7188. if (sd->state.showexp && (base_exp || job_exp))
  7189. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  7190. }
  7191. /**
  7192. * Returns max base level for this character's class.
  7193. * @param job_id: Player's class
  7194. * @return Max Base Level
  7195. */
  7196. uint32 JobDatabase::get_maxBaseLv(uint16 job_id) {
  7197. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7198. return job ? job->max_base_level : 0;
  7199. }
  7200. /**
  7201. * Returns max base level for this character.
  7202. * @param sd Player
  7203. * @return Max Base Level
  7204. **/
  7205. unsigned int pc_maxbaselv(map_session_data *sd){
  7206. return job_db.get_maxBaseLv(sd->status.class_);
  7207. }
  7208. /**
  7209. * Returns max job level for this character's class.
  7210. * @param job_id: Player's class
  7211. * @return Max Job Level
  7212. */
  7213. uint32 JobDatabase::get_maxJobLv(uint16 job_id) {
  7214. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7215. return job ? job->max_job_level : 0;
  7216. }
  7217. /**
  7218. * Returns max job level for this character.
  7219. * @param sd Player
  7220. * @return Max Job Level
  7221. **/
  7222. unsigned int pc_maxjoblv(map_session_data *sd){
  7223. return job_db.get_maxJobLv(sd->status.class_);
  7224. }
  7225. /**
  7226. * Check if player is reached max base level
  7227. * @param sd
  7228. * @return True if reached max level
  7229. **/
  7230. bool pc_is_maxbaselv(map_session_data *sd) {
  7231. nullpo_retr(false, sd);
  7232. return (sd->status.base_level >= pc_maxbaselv(sd));
  7233. }
  7234. /**
  7235. * Check if player is reached max base level
  7236. * @param sd
  7237. * @return True if reached max level
  7238. **/
  7239. bool pc_is_maxjoblv(map_session_data *sd) {
  7240. nullpo_retr(false, sd);
  7241. return (sd->status.job_level >= pc_maxjoblv(sd));
  7242. }
  7243. /**
  7244. * Returns base experience for this character's class.
  7245. * @param job_id: Player's class
  7246. * @param level: Player's level
  7247. * @return Base EXP
  7248. */
  7249. t_exp JobDatabase::get_baseExp(uint16 job_id, uint32 level) {
  7250. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7251. return job ? job->base_exp[level - 1] : 0;
  7252. }
  7253. /**
  7254. * Base exp needed for player to level up.
  7255. * @param sd
  7256. * @return Base EXP needed for next base level
  7257. **/
  7258. t_exp pc_nextbaseexp(map_session_data *sd){
  7259. nullpo_ret(sd);
  7260. if (sd->status.base_level == 0) // Is this something that possible?
  7261. return 0;
  7262. if (pc_is_maxbaselv(sd))
  7263. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  7264. return static_cast<t_exp>(job_db.get_baseExp(sd->status.class_, sd->status.base_level));
  7265. }
  7266. /**
  7267. * Returns job experience for this character's class.
  7268. * @param job_id: Player's class
  7269. * @param level: Player's level
  7270. * @return Job EXP
  7271. */
  7272. t_exp JobDatabase::get_jobExp(uint16 job_id, uint32 level) {
  7273. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7274. return job ? job->job_exp[level - 1] : 0;
  7275. }
  7276. /**
  7277. * Job exp needed for player to level up.
  7278. * @param sd
  7279. * @return Job EXP needed for next job level
  7280. **/
  7281. t_exp pc_nextjobexp(map_session_data *sd){
  7282. nullpo_ret(sd);
  7283. if (sd->status.job_level == 0) // Is this something that possible?
  7284. return 0;
  7285. if (pc_is_maxjoblv(sd))
  7286. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  7287. return static_cast<t_exp>(job_db.get_jobExp(sd->status.class_, sd->status.job_level));
  7288. }
  7289. /**
  7290. * Returns max weight base for this character's class.
  7291. * @param job_id: Player's class
  7292. * @return Max weight base
  7293. */
  7294. int32 JobDatabase::get_maxWeight(uint16 job_id) {
  7295. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7296. return job ? job->max_weight_base : 0;
  7297. }
  7298. /// Returns the value of the specified stat.
  7299. int pc_getstat(map_session_data *sd, int type)
  7300. {
  7301. nullpo_retr(-1, sd);
  7302. switch( type ) {
  7303. case SP_STR: return sd->status.str;
  7304. case SP_AGI: return sd->status.agi;
  7305. case SP_VIT: return sd->status.vit;
  7306. case SP_INT: return sd->status.int_;
  7307. case SP_DEX: return sd->status.dex;
  7308. case SP_LUK: return sd->status.luk;
  7309. case SP_POW: return sd->status.pow;
  7310. case SP_STA: return sd->status.sta;
  7311. case SP_WIS: return sd->status.wis;
  7312. case SP_SPL: return sd->status.spl;
  7313. case SP_CON: return sd->status.con;
  7314. case SP_CRT: return sd->status.crt;
  7315. default:
  7316. return -1;
  7317. }
  7318. }
  7319. /// Sets the specified stat to the specified value.
  7320. /// Returns the new value.
  7321. int pc_setstat(map_session_data* sd, int type, int val)
  7322. {
  7323. nullpo_retr(-1, sd);
  7324. switch( type ) {
  7325. case SP_STR: sd->status.str = val; break;
  7326. case SP_AGI: sd->status.agi = val; break;
  7327. case SP_VIT: sd->status.vit = val; break;
  7328. case SP_INT: sd->status.int_ = val; break;
  7329. case SP_DEX: sd->status.dex = val; break;
  7330. case SP_LUK: sd->status.luk = val; break;
  7331. case SP_POW: sd->status.pow = val; break;
  7332. case SP_STA: sd->status.sta = val; break;
  7333. case SP_WIS: sd->status.wis = val; break;
  7334. case SP_SPL: sd->status.spl = val; break;
  7335. case SP_CON: sd->status.con = val; break;
  7336. case SP_CRT: sd->status.crt = val; break;
  7337. default:
  7338. return -1;
  7339. }
  7340. return val;
  7341. }
  7342. /**
  7343. * Gets the total number of status points at the provided level.
  7344. * @param level: Player base level.
  7345. * @return Total number of status points at specific base level.
  7346. */
  7347. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  7348. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7349. if( entry != nullptr ){
  7350. return entry->statpoints;
  7351. }else{
  7352. return 0;
  7353. }
  7354. }
  7355. /**
  7356. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  7357. * @param level: Player base level.
  7358. * @param table: Use table value or formula.
  7359. * @return Status points at specific base level.
  7360. */
  7361. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  7362. uint32 next_level = this->get_table_point( level + 1 );
  7363. uint32 current_level = this->get_table_point( level );
  7364. if( next_level > current_level ){
  7365. return next_level - current_level;
  7366. }else{
  7367. return 0;
  7368. }
  7369. }
  7370. /**
  7371. * Gets the total number of trait points at the provided level.
  7372. * @param level: Player base level.
  7373. * @return Total number of trait points at specific base level.
  7374. */
  7375. uint32 PlayerStatPointDatabase::get_trait_table_point(uint16 level) {
  7376. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7377. if( entry != nullptr ){
  7378. return entry->traitpoints;
  7379. }else{
  7380. return 0;
  7381. }
  7382. }
  7383. /**
  7384. * Calculates the number of trait points PC gets when leveling up (from level to level+1)
  7385. * @param level: Player base level.
  7386. * @param table: Use table value or formula.
  7387. * @return Trait points at specific base level.
  7388. */
  7389. uint32 PlayerStatPointDatabase::pc_gets_trait_point(uint16 level) {
  7390. uint32 next_level = this->get_trait_table_point( level + 1 );
  7391. uint32 current_level = this->get_trait_table_point( level );
  7392. if( next_level > current_level ){
  7393. return next_level - current_level;
  7394. }else{
  7395. return 0;
  7396. }
  7397. }
  7398. #ifdef RENEWAL_STAT
  7399. /// Renewal status point cost formula
  7400. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  7401. #else
  7402. /// Pre-Renewal status point cost formula
  7403. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  7404. #endif
  7405. /// Returns the number of stat points needed to change the specified stat by val.
  7406. /// If val is negative, returns the number of stat points that would be needed to
  7407. /// raise the specified stat from (current value - val) to current value.
  7408. int pc_need_status_point(map_session_data* sd, int type, int val)
  7409. {
  7410. int low, high, sp = 0, max = 0;
  7411. if ( val == 0 )
  7412. return 0;
  7413. low = pc_getstat(sd,type);
  7414. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  7415. if ( low >= max && val > 0 )
  7416. return 0; // Official servers show '0' when max is reached
  7417. high = low + val;
  7418. if ( val < 0 )
  7419. SWAP(low, high);
  7420. for ( ; low < high; low++ )
  7421. sp += PC_STATUS_POINT_COST(low);
  7422. return sp;
  7423. }
  7424. /**
  7425. * Returns the value the specified stat can be increased by with the current
  7426. * amount of available status points for the current character's class.
  7427. *
  7428. * @param sd The target character.
  7429. * @param type Stat to verify.
  7430. * @return Maximum value the stat could grow by.
  7431. */
  7432. int pc_maxparameterincrease(map_session_data* sd, int type)
  7433. {
  7434. int base, final_val, status_points, max_param;
  7435. nullpo_ret(sd);
  7436. base = final_val = pc_getstat(sd, type);
  7437. status_points = sd->status.status_point;
  7438. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  7439. while (final_val <= max_param && status_points >= 0) {
  7440. status_points -= PC_STATUS_POINT_COST(final_val);
  7441. final_val++;
  7442. }
  7443. final_val--;
  7444. return (final_val > base ? final_val-base : 0);
  7445. }
  7446. /**
  7447. * Raises a stat by the specified amount.
  7448. *
  7449. * Obeys max_parameter limits.
  7450. * Subtracts status points according to the cost of the increased stat points.
  7451. *
  7452. * @param sd The target character.
  7453. * @param type The stat to change (see enum _sp)
  7454. * @param increase The stat increase (strictly positive) amount.
  7455. * @retval true if the stat was increased by any amount.
  7456. * @retval false if there were no changes.
  7457. */
  7458. bool pc_statusup(map_session_data* sd, int type, int increase)
  7459. {
  7460. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  7461. nullpo_ret(sd);
  7462. // check conditions
  7463. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  7464. clif_statusupack(sd, type, 0, 0);
  7465. return false;
  7466. }
  7467. // check limits
  7468. current = pc_getstat(sd, type);
  7469. max_increase = pc_maxparameterincrease(sd, type);
  7470. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7471. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  7472. clif_statusupack(sd, type, 0, 0);
  7473. return false;
  7474. }
  7475. // check status points
  7476. needed_points = pc_need_status_point(sd, type, increase);
  7477. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  7478. clif_statusupack(sd, type, 0, 0);
  7479. return false;
  7480. }
  7481. // set new values
  7482. final_value = pc_setstat(sd, type, current + increase);
  7483. sd->status.status_point -= needed_points;
  7484. status_calc_pc(sd,SCO_NONE);
  7485. // update increase cost indicator
  7486. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7487. // update statpoint count
  7488. clif_updatestatus(sd, SP_STATUSPOINT);
  7489. // update stat value
  7490. clif_statusupack(sd, type, 1, final_value); // required
  7491. if( final_value > 255 )
  7492. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7493. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7494. return true;
  7495. }
  7496. /**
  7497. * Raises a stat by the specified amount.
  7498. *
  7499. * Obeys max_parameter limits.
  7500. * Does not subtract status points for the cost of the modified stat points.
  7501. *
  7502. * @param sd The target character.
  7503. * @param type The stat to change (see enum _sp)
  7504. * @param val The stat increase (or decrease) amount.
  7505. * @return the stat increase amount.
  7506. * @retval 0 if no changes were made.
  7507. */
  7508. int pc_statusup2(map_session_data* sd, int type, int val)
  7509. {
  7510. int max, need;
  7511. nullpo_ret(sd);
  7512. if( type < SP_STR || type > SP_LUK )
  7513. {
  7514. clif_statusupack(sd,type,0,0);
  7515. return 0;
  7516. }
  7517. need = pc_need_status_point(sd,type,1);
  7518. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  7519. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  7520. status_calc_pc(sd,SCO_NONE);
  7521. // update increase cost indicator
  7522. if( need != pc_need_status_point(sd,type,1) )
  7523. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7524. // update stat value
  7525. clif_statusupack(sd,type,1,val); // required
  7526. if( val > 255 )
  7527. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  7528. return val;
  7529. }
  7530. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  7531. /// If val is negative, returns the number of trait stat points that would be needed to
  7532. /// raise the specified trait stat from (current value - val) to current value.
  7533. int pc_need_trait_point(map_session_data* sd, int type, int val)
  7534. {
  7535. nullpo_retr(0, sd);
  7536. if (val == 0 || type < SP_POW || type > SP_CRT)
  7537. return 0;
  7538. int low = pc_getstat(sd, type);
  7539. int max = pc_maxparameter(sd, (e_params)(PARAM_POW + type - SP_POW));
  7540. if (low >= max && val > 0)
  7541. return 0; // Official servers show '0' when max is reached
  7542. int high = low + val, sp = 0;
  7543. if (val < 0)
  7544. SWAP(low, high);
  7545. for (; low < high; low++)
  7546. sp += 1;
  7547. return sp;
  7548. }
  7549. /**
  7550. * Returns the value the specified trait stat can be increased by with the current
  7551. * amount of available trait status points for the current character's class.
  7552. *
  7553. * @param sd The target character.
  7554. * @param type Trait stat to verify.
  7555. * @return Maximum value the stat could grow by.
  7556. */
  7557. int pc_maxtraitparameterincrease(map_session_data* sd, int type)
  7558. {
  7559. nullpo_ret(sd);
  7560. if( type < SP_POW || type > SP_CRT ){
  7561. return 0;
  7562. }
  7563. int base, final_val = pc_getstat(sd, type);
  7564. int trait_points = sd->status.trait_point;
  7565. int max_param = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW));
  7566. base = final_val;
  7567. while (final_val <= max_param && trait_points >= 0) {
  7568. trait_points -= 1;
  7569. final_val++;
  7570. }
  7571. final_val--;
  7572. return (final_val > base ? final_val - base : 0);
  7573. }
  7574. /**
  7575. * Raises a trait stat by the specified amount.
  7576. *
  7577. * Obeys max_traitparameter limits.
  7578. * Subtracts trait status points according to the cost of the increased trait stat points.
  7579. *
  7580. * @param sd The target character.
  7581. * @param type The stat to change (see enum _sp)
  7582. * @param increase The stat increase (strictly positive) amount.
  7583. * @retval true if the trait stat was increased by any amount.
  7584. * @retval false if there were no changes.
  7585. */
  7586. bool pc_traitstatusup(map_session_data* sd, int type, int increase)
  7587. {
  7588. nullpo_ret(sd);
  7589. // check conditions
  7590. if (type < SP_POW || type > SP_CRT || increase <= 0) {
  7591. clif_statusupack(sd, type, 0, 0);
  7592. return false;
  7593. }
  7594. // check limits
  7595. int current = pc_getstat(sd, type);
  7596. int max_increase = pc_maxtraitparameterincrease(sd, type);
  7597. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7598. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW))) {
  7599. clif_statusupack(sd, type, 0, 0);
  7600. return false;
  7601. }
  7602. // check status points
  7603. int needed_points = pc_need_trait_point(sd, type, increase);
  7604. if (needed_points < 0 || needed_points > sd->status.trait_point) { // Sanity check
  7605. clif_statusupack(sd, type, 0, 0);
  7606. return false;
  7607. }
  7608. // set new values
  7609. int final_value = pc_setstat(sd, type, current + increase);
  7610. sd->status.trait_point -= needed_points;
  7611. status_calc_pc(sd, SCO_NONE);
  7612. // update increase cost indicator
  7613. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7614. // update statpoint count
  7615. clif_updatestatus(sd, SP_TRAITPOINT);
  7616. // update stat value
  7617. clif_statusupack(sd, type, 1, final_value); // required
  7618. if (final_value > 255)
  7619. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7620. //achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7621. return true;
  7622. }
  7623. /**
  7624. * Raises a trait stat by the specified amount.
  7625. *
  7626. * Obeys max_trait_parameter limits.
  7627. * Does not subtract status points for the cost of the modified stat points.
  7628. *
  7629. * @param sd The target character.
  7630. * @param type The stat to change (see enum _sp)
  7631. * @param val The stat increase (or decrease) amount.
  7632. * @return the stat increase amount.
  7633. * @retval 0 if no changes were made.
  7634. */
  7635. int pc_traitstatusup2(map_session_data* sd, int type, int val)
  7636. {
  7637. nullpo_ret(sd);
  7638. if (type < SP_POW || type > SP_CRT) {
  7639. clif_statusupack(sd, type, 0, 0);
  7640. return 0;
  7641. }
  7642. int need = pc_need_trait_point(sd, type, 1);
  7643. int max = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW)); // set new value
  7644. val = pc_setstat(sd, type, cap_value(pc_getstat(sd, type) + val, 0, max));
  7645. status_calc_pc(sd, SCO_NONE);
  7646. // update increase cost indicator
  7647. if (need != pc_need_trait_point(sd, type, 1))
  7648. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7649. // update stat value
  7650. clif_statusupack(sd, type, 1, val); // required
  7651. if (val > 255)
  7652. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7653. return val;
  7654. }
  7655. /*==========================================
  7656. * Update skill_lv for player sd
  7657. * Skill point allocation
  7658. *------------------------------------------*/
  7659. void pc_skillup(map_session_data *sd,uint16 skill_id)
  7660. {
  7661. uint16 idx = skill_get_index(skill_id);
  7662. nullpo_retv(sd);
  7663. if (!idx) {
  7664. if (skill_id)
  7665. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  7666. return;
  7667. }
  7668. // Level up guild skill
  7669. if (SKILL_CHK_GUILD(skill_id)) {
  7670. guild_skillup(sd, skill_id);
  7671. return;
  7672. }
  7673. // Level up homunculus skill
  7674. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  7675. hom_skillup(sd->hd, skill_id);
  7676. return;
  7677. }
  7678. else {
  7679. if( sd->status.skill_point > 0 &&
  7680. sd->status.skill[idx].id &&
  7681. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  7682. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  7683. {
  7684. int lv, range, upgradable;
  7685. sd->status.skill[idx].lv++;
  7686. sd->status.skill_point--;
  7687. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  7688. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  7689. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  7690. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  7691. else
  7692. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  7693. lv = sd->status.skill[idx].lv;
  7694. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  7695. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  7696. clif_skillup(sd,skill_id,lv,range,upgradable);
  7697. clif_updatestatus(sd,SP_SKILLPOINT);
  7698. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  7699. clif_updatestatus(sd,SP_CARTINFO);
  7700. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  7701. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7702. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  7703. clif_skillinfoblock(sd);
  7704. }
  7705. //else
  7706. // 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);
  7707. }
  7708. }
  7709. /*==========================================
  7710. * /allskill
  7711. *------------------------------------------*/
  7712. int pc_allskillup(map_session_data *sd)
  7713. {
  7714. int i;
  7715. nullpo_ret(sd);
  7716. for (i = 0; i < MAX_SKILL; i++) {
  7717. 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) {
  7718. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  7719. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  7720. if (sd->status.skill[i].lv == 0)
  7721. sd->status.skill[i].id = 0;
  7722. }
  7723. }
  7724. if (!pc_grant_allskills(sd, true)) {
  7725. uint16 sk_id;
  7726. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(sd->status.class_);
  7727. if (tree != nullptr && !tree->skills.empty()) {
  7728. for (const auto &skillsit : tree->skills) {
  7729. sk_id = skillsit.first;
  7730. uint16 sk_idx = skill_get_index(sk_id);
  7731. if (sk_idx == 0)
  7732. continue;
  7733. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  7734. if (
  7735. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  7736. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  7737. sk_id == SG_DEVIL
  7738. )
  7739. continue; //Cannot be learned normally.
  7740. sd->status.skill[sk_idx].id = sk_id;
  7741. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  7742. }
  7743. }
  7744. }
  7745. status_calc_pc(sd,SCO_NONE);
  7746. //Required because if you could level up all skills previously,
  7747. //the update will not be sent as only the lv variable changes.
  7748. clif_skillinfoblock(sd);
  7749. return 0;
  7750. }
  7751. /*==========================================
  7752. * /resetlvl
  7753. *------------------------------------------*/
  7754. int pc_resetlvl(map_session_data* sd,int type)
  7755. {
  7756. int i;
  7757. nullpo_ret(sd);
  7758. if (type != 3) //Also reset skills
  7759. pc_resetskill(sd, 0);
  7760. if(type == 1){
  7761. sd->status.skill_point=0;
  7762. sd->status.trait_point = 0;
  7763. sd->status.base_level=1;
  7764. sd->status.job_level=1;
  7765. sd->status.base_exp=0;
  7766. sd->status.job_exp=0;
  7767. if(sd->sc.option !=0)
  7768. sd->sc.option = 0;
  7769. sd->status.str=1;
  7770. sd->status.agi=1;
  7771. sd->status.vit=1;
  7772. sd->status.int_=1;
  7773. sd->status.dex=1;
  7774. sd->status.luk=1;
  7775. sd->status.pow=0;
  7776. sd->status.sta=0;
  7777. sd->status.wis=0;
  7778. sd->status.spl=0;
  7779. sd->status.con=0;
  7780. sd->status.crt=0;
  7781. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  7782. sd->status.status_point=100; // not 88 [celest]
  7783. // give platinum skills upon changing
  7784. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  7785. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  7786. }
  7787. }
  7788. if(type == 2){
  7789. sd->status.skill_point=0;
  7790. sd->status.base_level=1;
  7791. sd->status.job_level=1;
  7792. sd->status.base_exp=0;
  7793. sd->status.job_exp=0;
  7794. }
  7795. if(type == 3){
  7796. sd->status.base_level=1;
  7797. sd->status.base_exp=0;
  7798. }
  7799. if(type == 4){
  7800. sd->status.job_level=1;
  7801. sd->status.job_exp=0;
  7802. }
  7803. clif_updatestatus(sd,SP_STATUSPOINT);
  7804. clif_updatestatus(sd,SP_TRAITPOINT);
  7805. clif_updatestatus(sd,SP_STR);
  7806. clif_updatestatus(sd,SP_AGI);
  7807. clif_updatestatus(sd,SP_VIT);
  7808. clif_updatestatus(sd,SP_INT);
  7809. clif_updatestatus(sd,SP_DEX);
  7810. clif_updatestatus(sd,SP_LUK);
  7811. clif_updatestatus(sd,SP_POW);
  7812. clif_updatestatus(sd,SP_STA);
  7813. clif_updatestatus(sd,SP_WIS);
  7814. clif_updatestatus(sd,SP_SPL);
  7815. clif_updatestatus(sd,SP_CON);
  7816. clif_updatestatus(sd,SP_CRT);
  7817. clif_updatestatus(sd,SP_BASELEVEL);
  7818. clif_updatestatus(sd,SP_JOBLEVEL);
  7819. clif_updatestatus(sd,SP_STATUSPOINT);
  7820. clif_updatestatus(sd,SP_BASEEXP);
  7821. clif_updatestatus(sd,SP_JOBEXP);
  7822. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7823. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7824. clif_updatestatus(sd,SP_SKILLPOINT);
  7825. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7826. clif_updatestatus(sd,SP_UAGI);
  7827. clif_updatestatus(sd,SP_UVIT);
  7828. clif_updatestatus(sd,SP_UINT);
  7829. clif_updatestatus(sd,SP_UDEX);
  7830. clif_updatestatus(sd,SP_ULUK); // End Addition
  7831. clif_updatestatus(sd,SP_UPOW);
  7832. clif_updatestatus(sd,SP_USTA);
  7833. clif_updatestatus(sd,SP_UWIS);
  7834. clif_updatestatus(sd,SP_USPL);
  7835. clif_updatestatus(sd,SP_UCON);
  7836. clif_updatestatus(sd,SP_UCRT);
  7837. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  7838. if(sd->equip_index[i] >= 0)
  7839. if(pc_isequip(sd,sd->equip_index[i]))
  7840. pc_unequipitem(sd,sd->equip_index[i],2);
  7841. }
  7842. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  7843. party_send_levelup(sd);
  7844. status_calc_pc(sd, SCO_FORCE);
  7845. clif_skillinfoblock(sd);
  7846. return 0;
  7847. }
  7848. /*==========================================
  7849. * /resetstate
  7850. *------------------------------------------*/
  7851. int pc_resetstate(map_session_data* sd)
  7852. {
  7853. nullpo_ret(sd);
  7854. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  7855. 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",
  7856. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  7857. sd->status.base_level = pc_maxbaselv( sd );
  7858. clif_updatestatus( sd, SP_BASELEVEL );
  7859. }
  7860. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  7861. sd->status.trait_point = statpoint_db.get_trait_table_point(sd->status.base_level);
  7862. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  7863. sd->status.status_point += battle_config.transcendent_status_points;
  7864. }
  7865. if ((sd->class_&JOBL_FOURTH) != 0) {
  7866. sd->status.trait_point += battle_config.trait_points_job_change;
  7867. }
  7868. pc_setstat(sd, SP_STR, 1);
  7869. pc_setstat(sd, SP_AGI, 1);
  7870. pc_setstat(sd, SP_VIT, 1);
  7871. pc_setstat(sd, SP_INT, 1);
  7872. pc_setstat(sd, SP_DEX, 1);
  7873. pc_setstat(sd, SP_LUK, 1);
  7874. pc_setstat(sd, SP_POW, 0);
  7875. pc_setstat(sd, SP_STA, 0);
  7876. pc_setstat(sd, SP_WIS, 0);
  7877. pc_setstat(sd, SP_SPL, 0);
  7878. pc_setstat(sd, SP_CON, 0);
  7879. pc_setstat(sd, SP_CRT, 0);
  7880. clif_updatestatus(sd,SP_STR);
  7881. clif_updatestatus(sd,SP_AGI);
  7882. clif_updatestatus(sd,SP_VIT);
  7883. clif_updatestatus(sd,SP_INT);
  7884. clif_updatestatus(sd,SP_DEX);
  7885. clif_updatestatus(sd,SP_LUK);
  7886. clif_updatestatus(sd,SP_POW);
  7887. clif_updatestatus(sd,SP_STA);
  7888. clif_updatestatus(sd,SP_WIS);
  7889. clif_updatestatus(sd,SP_SPL);
  7890. clif_updatestatus(sd,SP_CON);
  7891. clif_updatestatus(sd,SP_CRT);
  7892. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7893. clif_updatestatus(sd,SP_UAGI);
  7894. clif_updatestatus(sd,SP_UVIT);
  7895. clif_updatestatus(sd,SP_UINT);
  7896. clif_updatestatus(sd,SP_UDEX);
  7897. clif_updatestatus(sd,SP_ULUK); // End Addition
  7898. clif_updatestatus(sd,SP_UPOW);
  7899. clif_updatestatus(sd,SP_USTA);
  7900. clif_updatestatus(sd,SP_UWIS);
  7901. clif_updatestatus(sd,SP_USPL);
  7902. clif_updatestatus(sd,SP_UCON);
  7903. clif_updatestatus(sd,SP_UCRT);
  7904. clif_updatestatus(sd,SP_STATUSPOINT);
  7905. clif_updatestatus(sd,SP_TRAITPOINT);
  7906. if( sd->mission_mobid ) { //bugreport:2200
  7907. sd->mission_mobid = 0;
  7908. sd->mission_count = 0;
  7909. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  7910. }
  7911. status_calc_pc(sd, SCO_NONE);
  7912. return 1;
  7913. }
  7914. /*==========================================
  7915. * /resetskill
  7916. * if flag&1, perform block resync and status_calc call.
  7917. * if flag&2, just count total amount of skill points used by player, do not really reset.
  7918. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  7919. *------------------------------------------*/
  7920. int pc_resetskill(map_session_data* sd, int flag)
  7921. {
  7922. int i, skill_point=0;
  7923. nullpo_ret(sd);
  7924. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  7925. return 0;
  7926. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  7927. /**
  7928. * 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)
  7929. **/
  7930. if( pc_is_taekwon_ranker(sd) )
  7931. return 0;
  7932. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7933. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  7934. i = sd->sc.option;
  7935. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  7936. i &= ~OPTION_RIDING;
  7937. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  7938. i &= ~OPTION_FALCON;
  7939. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  7940. i &= ~OPTION_DRAGON;
  7941. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  7942. i &= ~OPTION_WUG;
  7943. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  7944. i &= ~OPTION_WUGRIDER;
  7945. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  7946. i &= ~OPTION_MADOGEAR;
  7947. #ifndef NEW_CARTS
  7948. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  7949. i &= ~OPTION_CART;
  7950. #else
  7951. if( sd->sc.getSCE(SC_PUSH_CART) )
  7952. pc_setcart(sd, 0);
  7953. #endif
  7954. if( i != sd->sc.option )
  7955. pc_setoption(sd, i);
  7956. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  7957. hom_vaporize(sd, HOM_ST_ACTIVE);
  7958. if (sd->sc.getSCE(SC_SPRITEMABLE) && pc_checkskill(sd, SU_SPRITEMABLE))
  7959. status_change_end(&sd->bl, SC_SPRITEMABLE);
  7960. if (sd->sc.getSCE(SC_SOULATTACK) && pc_checkskill(sd, SU_SOULATTACK))
  7961. status_change_end(&sd->bl, SC_SOULATTACK);
  7962. }
  7963. for (const auto &skill : skill_db) {
  7964. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  7965. uint8 lv = sd->status.skill[idx].lv;
  7966. if (lv == 0 || skill_id == 0)
  7967. continue;
  7968. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  7969. continue;
  7970. // Don't reset trick dead if not a novice/baby
  7971. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7972. {
  7973. sd->status.skill[idx].lv = 0;
  7974. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7975. continue;
  7976. }
  7977. // do not reset basic skill
  7978. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7979. continue;
  7980. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  7981. continue;
  7982. if( flag&4 && !skill_ischangesex(skill_id) )
  7983. continue;
  7984. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  7985. { //Only handle quest skills in a special way when you can't learn them manually
  7986. if( battle_config.quest_skill_reset && !(flag&2) )
  7987. { //Wipe them
  7988. sd->status.skill[idx].lv = 0;
  7989. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7990. }
  7991. continue;
  7992. }
  7993. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  7994. skill_point += lv;
  7995. else
  7996. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  7997. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  7998. if( !(flag&2) )
  7999. {// reset
  8000. sd->status.skill[idx].lv = 0;
  8001. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8002. }
  8003. }
  8004. if( flag&2 || !skill_point ) return skill_point;
  8005. sd->status.skill_point += skill_point;
  8006. if (flag&1) {
  8007. clif_updatestatus(sd,SP_SKILLPOINT);
  8008. clif_skillinfoblock(sd);
  8009. status_calc_pc(sd, SCO_FORCE);
  8010. }
  8011. return skill_point;
  8012. }
  8013. /*==========================================
  8014. * /resetfeel [Komurka]
  8015. *------------------------------------------*/
  8016. int pc_resetfeel(map_session_data* sd)
  8017. {
  8018. int i;
  8019. nullpo_ret(sd);
  8020. for (i=0; i<MAX_PC_FEELHATE; i++)
  8021. {
  8022. sd->feel_map[i].m = -1;
  8023. sd->feel_map[i].index = 0;
  8024. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  8025. }
  8026. return 0;
  8027. }
  8028. int pc_resethate(map_session_data* sd)
  8029. {
  8030. int i;
  8031. nullpo_ret(sd);
  8032. for (i=0; i<MAX_PC_FEELHATE; i++)
  8033. {
  8034. sd->hate_mob[i] = -1;
  8035. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  8036. }
  8037. return 0;
  8038. }
  8039. int pc_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8040. {
  8041. int bonus = 0;
  8042. nullpo_ret(sd);
  8043. skill_id = skill_dummy2skill_id(skill_id);
  8044. for (auto &it : sd->skillatk) {
  8045. if (it.id == skill_id) {
  8046. bonus += it.val;
  8047. break;
  8048. }
  8049. }
  8050. return bonus;
  8051. }
  8052. int pc_sub_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8053. {
  8054. int bonus = 0;
  8055. nullpo_ret(sd);
  8056. skill_id = skill_dummy2skill_id(skill_id);
  8057. for (auto &it : sd->subskill) {
  8058. if (it.id == skill_id) {
  8059. bonus += it.val;
  8060. break;
  8061. }
  8062. }
  8063. return bonus;
  8064. }
  8065. int pc_skillheal_bonus(map_session_data *sd, uint16 skill_id) {
  8066. int bonus = sd->bonus.add_heal_rate;
  8067. nullpo_ret(sd);
  8068. skill_id = skill_dummy2skill_id(skill_id);
  8069. if( bonus ) {
  8070. switch( skill_id ) {
  8071. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  8072. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  8073. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  8074. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  8075. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  8076. case AB_CHEAL: if (!(battle_config.skill_add_heal_rate & 32)) bonus = 0; break;
  8077. case AB_HIGHNESSHEAL: if (!(battle_config.skill_add_heal_rate & 64)) bonus = 0; break;
  8078. case CD_MEDIALE_VOTUM: if (!(battle_config.skill_add_heal_rate & 128)) bonus = 0; break;
  8079. case CD_DILECTIO_HEAL: if (!(battle_config.skill_add_heal_rate & 256)) bonus = 0; break;
  8080. }
  8081. }
  8082. for (auto &it : sd->skillheal) {
  8083. if (it.id == skill_id) {
  8084. bonus += it.val;
  8085. break;
  8086. }
  8087. }
  8088. return bonus;
  8089. }
  8090. int pc_skillheal2_bonus(map_session_data *sd, uint16 skill_id) {
  8091. int bonus = sd->bonus.add_heal2_rate;
  8092. skill_id = skill_dummy2skill_id(skill_id);
  8093. for (auto &it : sd->skillheal2) {
  8094. if (it.id == skill_id) {
  8095. bonus += it.val;
  8096. break;
  8097. }
  8098. }
  8099. return bonus;
  8100. }
  8101. void pc_respawn(map_session_data* sd, clr_type clrtype)
  8102. {
  8103. if( !pc_isdead(sd) )
  8104. return; // not applicable
  8105. if( sd->bg_id && bg_member_respawn(sd) )
  8106. return; // member revived by battleground
  8107. pc_setstand(sd, true);
  8108. pc_setrestartvalue(sd,3);
  8109. if( pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, clrtype ) != SETPOS_OK ){
  8110. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  8111. }
  8112. }
  8113. static TIMER_FUNC(pc_respawn_timer){
  8114. map_session_data *sd = map_id2sd(id);
  8115. if( sd != NULL )
  8116. {
  8117. sd->pvp_point=0;
  8118. sd->respawn_tid = INVALID_TIMER;
  8119. pc_respawn(sd,CLR_OUTSIGHT);
  8120. }
  8121. return 0;
  8122. }
  8123. /*==========================================
  8124. * Invoked when a player has received damage
  8125. *------------------------------------------*/
  8126. void pc_damage(map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp, unsigned int ap)
  8127. {
  8128. if (ap) clif_updatestatus(sd,SP_AP);
  8129. if (sp) clif_updatestatus(sd,SP_SP);
  8130. if (hp) clif_updatestatus(sd,SP_HP);
  8131. else return;
  8132. if (!src)
  8133. return;
  8134. if( pc_issit(sd) ) {
  8135. pc_setstand(sd, true);
  8136. skill_sit(sd,0);
  8137. }
  8138. if (sd->progressbar.npc_id)
  8139. clif_progressbar_abort(sd);
  8140. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  8141. pet_target_check(sd->pd,src,1);
  8142. if( sd->status.ele_id > 0 )
  8143. elemental_set_target(sd,src);
  8144. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  8145. sd->canlog_tick = gettick();
  8146. }
  8147. TIMER_FUNC(pc_close_npc_timer){
  8148. TBL_PC *sd = map_id2sd(id);
  8149. if(sd) pc_close_npc(sd,data);
  8150. return 0;
  8151. }
  8152. /**
  8153. * Method to properly close a NPC for player and clear anything related.
  8154. * @param sd: Player attached
  8155. * @param flag: Method of closure
  8156. * 1: Produce a close button and end the NPC
  8157. * 2: End the NPC (best for no dialog windows)
  8158. */
  8159. void pc_close_npc(map_session_data *sd,int flag)
  8160. {
  8161. nullpo_retv(sd);
  8162. if (sd->npc_id || sd->npc_shopid) {
  8163. if (sd->state.using_fake_npc) {
  8164. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  8165. sd->state.using_fake_npc = 0;
  8166. }
  8167. if (sd->st) {
  8168. if(sd->st->state == RUN){ //wait ending code execution
  8169. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  8170. return;
  8171. }
  8172. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  8173. sd->st->mes_active = 0;
  8174. }
  8175. sd->state.menu_or_input = 0;
  8176. sd->npc_menu = 0;
  8177. sd->npc_shopid = 0;
  8178. #ifdef SECURE_NPCTIMEOUT
  8179. if( sd->npc_idle_timer != INVALID_TIMER ){
  8180. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  8181. sd->npc_idle_timer = INVALID_TIMER;
  8182. }
  8183. #endif
  8184. if (sd->st) {
  8185. if (sd->st->state == CLOSE) {
  8186. clif_scriptclose(sd, sd->npc_id);
  8187. clif_scriptclear( *sd, sd->npc_id ); // [Ind/Hercules]
  8188. sd->st->state = END; // Force to end now
  8189. }
  8190. if (sd->st->state == END) { // free attached scripts that are waiting
  8191. script_free_state(sd->st);
  8192. sd->st = NULL;
  8193. sd->npc_id = 0;
  8194. }
  8195. }
  8196. }
  8197. }
  8198. /*==========================================
  8199. * Invoked when a player has negative current hp
  8200. *------------------------------------------*/
  8201. int pc_dead(map_session_data *sd,struct block_list *src)
  8202. {
  8203. int i=0,k=0;
  8204. t_tick tick = gettick();
  8205. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8206. // Activate Steel body if a super novice dies at 99+% exp [celest]
  8207. // Super Novices have no kill or die functions attached when saved by their angel
  8208. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  8209. t_exp exp = pc_nextbaseexp(sd);
  8210. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  8211. sd->state.snovice_dead_flag = 1;
  8212. pc_setrestartvalue(sd,1);
  8213. status_percent_heal(&sd->bl, 100, 100);
  8214. clif_resurrection(&sd->bl, 1);
  8215. if(battle_config.pc_invincible_time)
  8216. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8217. sc_start(&sd->bl,&sd->bl,SC_STEELBODY,100,5,skill_get_time(MO_STEELBODY,5));
  8218. if(mapdata_flag_gvg2(mapdata))
  8219. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  8220. return 0;
  8221. }
  8222. }
  8223. for(k = 0; k < MAX_DEVOTION; k++) {
  8224. if (sd->devotion[k]){
  8225. map_session_data *devsd = map_id2sd(sd->devotion[k]);
  8226. if (devsd)
  8227. status_change_end(&devsd->bl, SC_DEVOTION);
  8228. sd->devotion[k] = 0;
  8229. }
  8230. }
  8231. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  8232. if (sd->stellar_mark[k]) {
  8233. map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  8234. if (smarksd)
  8235. status_change_end(&smarksd->bl, SC_FLASHKICK);
  8236. sd->stellar_mark[k] = 0;
  8237. }
  8238. }
  8239. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  8240. if (sd->united_soul[k]) {
  8241. map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  8242. if (usoulsd)
  8243. status_change_end(&usoulsd->bl, SC_SOULUNITY);
  8244. sd->united_soul[k] = 0;
  8245. }
  8246. }
  8247. for (k = 0; k < MAX_SERVANT_SIGN; k++) {
  8248. if (sd->servant_sign[k]) {
  8249. map_session_data *ssignsd = map_id2sd(sd->servant_sign[k]);
  8250. if (ssignsd)
  8251. status_change_end(&ssignsd->bl, SC_SERVANT_SIGN);
  8252. sd->servant_sign[k] = 0;
  8253. }
  8254. }
  8255. if(sd->shadowform_id) { //if we were target of shadowform
  8256. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM);
  8257. sd->shadowform_id = 0; //should be remove on status end anyway
  8258. }
  8259. if(sd->status.pet_id > 0 && sd->pd) {
  8260. struct pet_data *pd = sd->pd;
  8261. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  8262. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  8263. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  8264. pet_set_intimate(pd, PET_INTIMATE_NONE);
  8265. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  8266. }
  8267. if( sd->pd->target_id ) // Unlock all targets...
  8268. pet_unlocktarget(sd->pd);
  8269. }
  8270. 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)
  8271. hom_vaporize(sd, HOM_ST_ACTIVE);
  8272. if( sd->md )
  8273. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  8274. if( sd->ed )
  8275. elemental_delete(sd->ed);
  8276. // Leave duel if you die [LuzZza]
  8277. if(battle_config.duel_autoleave_when_die) {
  8278. if(sd->duel_group > 0)
  8279. duel_leave(sd->duel_group, sd);
  8280. if(sd->duel_invite > 0)
  8281. duel_reject(sd->duel_invite, sd);
  8282. }
  8283. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  8284. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  8285. sd->skill_keep_using.tid = INVALID_TIMER;
  8286. }
  8287. pc_close_npc(sd,2); //close npc if we were using one
  8288. /* e.g. not killed thru pc_damage */
  8289. if( pc_issit(sd) ) {
  8290. clif_status_load(&sd->bl,EFST_SIT,0);
  8291. }
  8292. pc_setdead(sd);
  8293. clif_party_dead( *sd );
  8294. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  8295. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  8296. if (battle_config.loose_ap_on_death == 1)
  8297. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  8298. //Reset menu skills/item skills
  8299. if ((sd->skillitem) != 0)
  8300. sd->skillitem = sd->skillitemlv = 0;
  8301. if ((sd->menuskill_id) != 0)
  8302. sd->menuskill_id = sd->menuskill_val = 0;
  8303. //Reset ticks.
  8304. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  8305. if ( sd->spiritball !=0 )
  8306. pc_delspiritball(sd,sd->spiritball,0);
  8307. if (sd->soulball != 0)
  8308. pc_delsoulball(sd, sd->soulball, false);
  8309. if (sd->servantball != 0)
  8310. pc_delservantball( *sd, sd->servantball );
  8311. if (sd->abyssball != 0)
  8312. pc_delabyssball( *sd, sd->abyssball );
  8313. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  8314. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  8315. if (src)
  8316. switch (src->type) {
  8317. case BL_MOB:
  8318. {
  8319. struct mob_data *md=(struct mob_data *)src;
  8320. if(md->target_id==sd->bl.id)
  8321. mob_unlocktarget(md,tick);
  8322. if(battle_config.mobs_level_up && md->status.hp &&
  8323. (unsigned int)md->level < pc_maxbaselv(sd) &&
  8324. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  8325. ) { // monster level up [Valaris]
  8326. clif_misceffect(&md->bl,0);
  8327. md->level++;
  8328. status_calc_mob(md, SCO_NONE);
  8329. status_percent_heal(src,10,0);
  8330. if( battle_config.show_mob_info&4 )
  8331. {// update name with new level
  8332. clif_name_area(&md->bl);
  8333. }
  8334. }
  8335. src = battle_get_master(src); // Maybe Player Summon
  8336. }
  8337. break;
  8338. case BL_PET: //Pass on to master...
  8339. case BL_HOM:
  8340. case BL_MER:
  8341. src = battle_get_master(src);
  8342. break;
  8343. }
  8344. if (src && src->type == BL_PC) {
  8345. map_session_data *ssd = (map_session_data *)src;
  8346. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  8347. npc_script_event(ssd, NPCE_KILLPC);
  8348. if (battle_config.pk_mode&2) {
  8349. ssd->status.manner -= 5;
  8350. if(ssd->status.manner < 0)
  8351. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  8352. #if 0
  8353. // PK/Karma system code (not enabled yet) [celest]
  8354. // originally from Kade Online, so i don't know if any of these is correct ^^;
  8355. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  8356. // karma going down = more 'good' / more honourable.
  8357. // The Karma System way...
  8358. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  8359. sd->status.karma--;
  8360. ssd->status.karma--;
  8361. }
  8362. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  8363. ssd->status.karma++;
  8364. // or the PK System way...
  8365. if (sd->status.karma > 0) // player killed is dishonourable?
  8366. ssd->status.karma--; // honour points earned
  8367. sd->status.karma++; // honour points lost
  8368. // To-do: Receive exp on certain occasions
  8369. #endif
  8370. }
  8371. }
  8372. if(battle_config.bone_drop==2
  8373. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  8374. {
  8375. struct item item_tmp;
  8376. memset(&item_tmp,0,sizeof(item_tmp));
  8377. item_tmp.nameid=ITEMID_SKULL_;
  8378. item_tmp.identify=1;
  8379. item_tmp.card[0]=CARD0_CREATE;
  8380. item_tmp.card[1]=0;
  8381. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  8382. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  8383. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  8384. }
  8385. //Remove bonus_script when dead
  8386. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  8387. // changed penalty options, added death by player if pk_mode [Valaris]
  8388. if(battle_config.death_penalty_type
  8389. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  8390. && !sd->sc.cant.deathpenalty
  8391. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  8392. {
  8393. t_exp base_penalty = 0;
  8394. t_exp job_penalty = 0;
  8395. uint32 zeny_penalty = 0;
  8396. if (pc_isvip(sd)) { // EXP penalty for VIP
  8397. base_penalty = battle_config.vip_exp_penalty_base;
  8398. job_penalty = battle_config.vip_exp_penalty_job;
  8399. zeny_penalty = battle_config.vip_zeny_penalty;
  8400. } else {
  8401. base_penalty = battle_config.death_penalty_base;
  8402. job_penalty = battle_config.death_penalty_job;
  8403. zeny_penalty = battle_config.zeny_penalty;
  8404. }
  8405. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  8406. switch (battle_config.death_penalty_type) {
  8407. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  8408. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  8409. }
  8410. if (base_penalty){ //recheck after altering to speedup
  8411. if (battle_config.pk_mode && src && src->type==BL_PC)
  8412. base_penalty *= 2;
  8413. base_penalty = u64min(sd->status.base_exp, base_penalty);
  8414. }
  8415. }
  8416. else
  8417. base_penalty = 0;
  8418. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  8419. switch (battle_config.death_penalty_type) {
  8420. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  8421. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  8422. }
  8423. if (job_penalty) {
  8424. if (battle_config.pk_mode && src && src->type==BL_PC)
  8425. job_penalty *= 2;
  8426. job_penalty = u64min(sd->status.job_exp, job_penalty);
  8427. }
  8428. }
  8429. else
  8430. job_penalty = 0;
  8431. if (base_penalty || job_penalty) {
  8432. short insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  8433. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  8434. pc_lostexp(sd, base_penalty, job_penalty);
  8435. }
  8436. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  8437. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  8438. if(zeny_penalty)
  8439. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  8440. }
  8441. }
  8442. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  8443. for (const auto &it : mapdata->drop_list) {
  8444. int id = it.drop_id, per = it.drop_per;
  8445. enum e_nightmare_drop_type type = it.drop_type;
  8446. if(id == 0)
  8447. continue;
  8448. if(id == -1){
  8449. int eq_num=0,eq_n[MAX_INVENTORY];
  8450. memset(eq_n,0,sizeof(eq_n));
  8451. for(i=0;i<MAX_INVENTORY;i++) {
  8452. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8453. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8454. || type&NMDT_ALL)
  8455. {
  8456. int l;
  8457. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  8458. if( l < MAX_INVENTORY )
  8459. eq_n[l] = i;
  8460. eq_num++;
  8461. }
  8462. }
  8463. if(eq_num > 0){
  8464. int n = eq_n[rnd()%eq_num];
  8465. if(rnd()%10000 < per) {
  8466. if(sd->inventory.u.items_inventory[n].equip)
  8467. pc_unequipitem(sd,n,3);
  8468. pc_dropitem(sd,n,1);
  8469. }
  8470. }
  8471. }
  8472. else if(id > 0) {
  8473. for(i=0;i<MAX_INVENTORY;i++){
  8474. if(sd->inventory.u.items_inventory[i].nameid == id
  8475. && rnd()%10000 < per
  8476. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8477. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8478. || type&NMDT_ALL) ){
  8479. if(sd->inventory.u.items_inventory[i].equip)
  8480. pc_unequipitem(sd,i,3);
  8481. pc_dropitem(sd,i,1);
  8482. break;
  8483. }
  8484. }
  8485. }
  8486. }
  8487. }
  8488. // pvp
  8489. // disable certain pvp functions on pk_mode [Valaris]
  8490. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  8491. sd->pvp_point -= 5;
  8492. sd->pvp_lost++;
  8493. if( src && src->type == BL_PC ) {
  8494. map_session_data *ssd = (map_session_data *)src;
  8495. ssd->pvp_point++;
  8496. ssd->pvp_won++;
  8497. }
  8498. if( sd->pvp_point < 0 ) {
  8499. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  8500. return 1|8;
  8501. }
  8502. }
  8503. //GvG
  8504. if( mapdata_flag_gvg2(mapdata) ) {
  8505. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  8506. return 1|8;
  8507. }
  8508. else if( sd->bg_id ) {
  8509. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  8510. if (bg) {
  8511. if (bg->cemetery.map > 0) { // Respawn by BG
  8512. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  8513. return 1|8;
  8514. }
  8515. }
  8516. }
  8517. //Reset "can log out" tick.
  8518. if( battle_config.prevent_logout )
  8519. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  8520. return 1;
  8521. }
  8522. void pc_revive(map_session_data *sd,unsigned int hp, unsigned int sp, unsigned int ap) {
  8523. if(hp) clif_updatestatus(sd,SP_HP);
  8524. if(sp) clif_updatestatus(sd,SP_SP);
  8525. if(ap) clif_updatestatus(sd,SP_AP);
  8526. pc_setstand(sd, true);
  8527. if(battle_config.pc_invincible_time > 0)
  8528. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8529. if( sd->state.gmaster_flag ) {
  8530. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  8531. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  8532. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  8533. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  8534. }
  8535. }
  8536. bool pc_revive_item(map_session_data *sd) {
  8537. nullpo_retr(false, sd);
  8538. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  8539. return false;
  8540. if (sd->sc.getSCE(SC_HELLPOWER)) // Cannot resurrect while under the effect of SC_HELLPOWER.
  8541. return false;
  8542. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  8543. uint8 hp = 100, sp = 100;
  8544. if (item_position < 0) {
  8545. if (sd->sc.getSCE(SC_LIGHT_OF_REGENE)) {
  8546. hp = sd->sc.getSCE(SC_LIGHT_OF_REGENE)->val2;
  8547. sp = 0;
  8548. }
  8549. else
  8550. return false;
  8551. }
  8552. if (!status_revive(&sd->bl, hp, sp))
  8553. return false;
  8554. if (item_position < 0)
  8555. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE);
  8556. else
  8557. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  8558. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  8559. return true;
  8560. }
  8561. // script
  8562. //
  8563. /*==========================================
  8564. * script reading pc status registry
  8565. *------------------------------------------*/
  8566. int64 pc_readparam(map_session_data* sd,int64 type)
  8567. {
  8568. int64 val = 0;
  8569. nullpo_ret(sd);
  8570. switch(type) {
  8571. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  8572. case SP_STATUSPOINT: val = sd->status.status_point; break;
  8573. case SP_TRAITPOINT: val = sd->status.trait_point; break;
  8574. case SP_ZENY: val = sd->status.zeny; break;
  8575. case SP_BASELEVEL: val = sd->status.base_level; break;
  8576. case SP_JOBLEVEL: val = sd->status.job_level; break;
  8577. case SP_CLASS: val = sd->status.class_; break;
  8578. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  8579. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  8580. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  8581. case SP_SEX: val = sd->status.sex; break;
  8582. case SP_WEIGHT: val = sd->weight; break;
  8583. case SP_MAXWEIGHT: val = sd->max_weight; break;
  8584. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  8585. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  8586. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  8587. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  8588. case SP_HP: val = sd->battle_status.hp; break;
  8589. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  8590. case SP_SP: val = sd->battle_status.sp; break;
  8591. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  8592. case SP_AP: val = sd->battle_status.ap; break;
  8593. case SP_MAXAP: val = sd->battle_status.max_ap; break;
  8594. case SP_STR: val = sd->status.str; break;
  8595. case SP_AGI: val = sd->status.agi; break;
  8596. case SP_VIT: val = sd->status.vit; break;
  8597. case SP_INT: val = sd->status.int_; break;
  8598. case SP_DEX: val = sd->status.dex; break;
  8599. case SP_LUK: val = sd->status.luk; break;
  8600. case SP_POW: val = sd->status.pow; break;
  8601. case SP_STA: val = sd->status.sta; break;
  8602. case SP_WIS: val = sd->status.wis; break;
  8603. case SP_SPL: val = sd->status.spl; break;
  8604. case SP_CON: val = sd->status.con; break;
  8605. case SP_CRT: val = sd->status.crt; break;
  8606. case SP_KARMA: val = sd->status.karma; break;
  8607. case SP_MANNER: val = sd->status.manner; break;
  8608. case SP_FAME: val = sd->status.fame; break;
  8609. case SP_KILLERRID: val = sd->killerrid; break;
  8610. case SP_KILLEDRID: val = sd->killedrid; break;
  8611. case SP_KILLEDGID: val = sd->killedgid; break;
  8612. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  8613. case SP_CHARMOVE: val = sd->status.character_moves; break;
  8614. case SP_CHARRENAME: val = sd->status.rename; break;
  8615. case SP_CHARFONT: val = sd->status.font; break;
  8616. case SP_BANK_VAULT: val = sd->bank_vault; break;
  8617. case SP_CASHPOINTS: val = sd->cashPoints; break;
  8618. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  8619. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  8620. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  8621. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  8622. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  8623. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  8624. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  8625. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  8626. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  8627. case SP_BASE_ATK:
  8628. #ifdef RENEWAL
  8629. val = sd->bonus.eatk;
  8630. #else
  8631. val = sd->battle_status.batk;
  8632. #endif
  8633. break;
  8634. case SP_DEF1: val = sd->battle_status.def; break;
  8635. case SP_DEF2: val = sd->battle_status.def2; break;
  8636. case SP_MDEF1: val = sd->battle_status.mdef; break;
  8637. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  8638. case SP_HIT: val = sd->battle_status.hit; break;
  8639. case SP_FLEE1: val = sd->battle_status.flee; break;
  8640. case SP_FLEE2: val = sd->battle_status.flee2; break;
  8641. case SP_PATK: val = sd->battle_status.patk; break;
  8642. case SP_SMATK: val = sd->battle_status.smatk; break;
  8643. case SP_RES: val = sd->battle_status.res; break;
  8644. case SP_MRES: val = sd->battle_status.mres; break;
  8645. case SP_HPLUS: val = sd->battle_status.hplus; break;
  8646. case SP_CRATE: val = sd->battle_status.crate; break;
  8647. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  8648. case SP_MAXHPRATE: val = sd->hprate; break;
  8649. case SP_MAXSPRATE: val = sd->sprate; break;
  8650. case SP_MAXAPRATE: val = sd->aprate; break;
  8651. case SP_SPRATE: val = sd->dsprate; break;
  8652. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  8653. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  8654. case SP_ASPD_RATE:
  8655. #ifndef RENEWAL_ASPD
  8656. val = sd->battle_status.aspd_rate;
  8657. #else
  8658. val = sd->battle_status.aspd_rate2;
  8659. #endif
  8660. break;
  8661. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  8662. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  8663. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  8664. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  8665. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  8666. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  8667. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  8668. case SP_MATK_RATE: val = sd->matk_rate; break;
  8669. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  8670. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  8671. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  8672. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  8673. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  8674. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  8675. case SP_HIT_RATE: val = sd->hit_rate; break;
  8676. case SP_FLEE_RATE: val = sd->flee_rate; break;
  8677. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  8678. case SP_DEF_RATE: val = sd->def_rate; break;
  8679. case SP_DEF2_RATE: val = sd->def2_rate; break;
  8680. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  8681. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  8682. case SP_PATK_RATE: val = sd->patk_rate; break;
  8683. case SP_SMATK_RATE: val = sd->smatk_rate; break;
  8684. case SP_RES_RATE: val = sd->res_rate; break;
  8685. case SP_MRES_RATE: val = sd->mres_rate; break;
  8686. case SP_HPLUS_RATE: val = sd->hplus_rate; break;
  8687. case SP_CRATE_RATE: val = sd->crate_rate; break;
  8688. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  8689. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  8690. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  8691. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  8692. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  8693. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  8694. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  8695. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  8696. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  8697. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  8698. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  8699. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  8700. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  8701. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  8702. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  8703. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  8704. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  8705. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  8706. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  8707. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  8708. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  8709. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  8710. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  8711. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  8712. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  8713. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  8714. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  8715. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  8716. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  8717. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  8718. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  8719. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  8720. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  8721. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  8722. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  8723. case SP_UNSTRIPABLE:
  8724. case SP_UNSTRIPABLE_ARMOR:
  8725. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  8726. break;
  8727. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  8728. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  8729. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  8730. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  8731. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  8732. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  8733. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  8734. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  8735. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  8736. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  8737. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  8738. case SP_EMATK: val = sd->bonus.ematk; break;
  8739. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  8740. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  8741. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  8742. case SP_CASTRATE:
  8743. case SP_VARCASTRATE:
  8744. #ifdef RENEWAL_CAST
  8745. val = sd->bonus.varcastrate; break;
  8746. #else
  8747. val = sd->castrate; break;
  8748. #endif
  8749. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  8750. case SP_ADD_ITEM_SPHEAL_RATE: val = sd->bonus.itemsphealrate2; break;
  8751. default:
  8752. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  8753. return -1;
  8754. }
  8755. return val;
  8756. }
  8757. /*==========================================
  8758. * script set pc status registry
  8759. *------------------------------------------*/
  8760. bool pc_setparam(map_session_data *sd,int64 type,int64 val_tmp)
  8761. {
  8762. nullpo_retr(false,sd);
  8763. int val = static_cast<unsigned int>(val_tmp);
  8764. switch(type){
  8765. case SP_BASELEVEL:
  8766. if (val > pc_maxbaselv(sd)) //Capping to max
  8767. val = pc_maxbaselv(sd);
  8768. if (val > sd->status.base_level) {
  8769. for( int i = 0; i < (int)( val - sd->status.base_level ); i++ ){
  8770. sd->status.status_point += statpoint_db.pc_gets_status_point( sd->status.base_level + i );
  8771. sd->status.trait_point += statpoint_db.pc_gets_trait_point( sd->status.base_level + i );
  8772. }
  8773. }
  8774. sd->status.base_level = val;
  8775. sd->status.base_exp = 0;
  8776. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  8777. clif_updatestatus(sd, SP_NEXTBASEEXP);
  8778. clif_updatestatus(sd, SP_STATUSPOINT);
  8779. clif_updatestatus(sd, SP_TRAITPOINT);
  8780. clif_updatestatus(sd, SP_BASEEXP);
  8781. status_calc_pc(sd, SCO_FORCE);
  8782. if(sd->status.party_id)
  8783. party_send_levelup(sd);
  8784. break;
  8785. case SP_JOBLEVEL:
  8786. if (val >= sd->status.job_level) {
  8787. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  8788. sd->status.skill_point += val - sd->status.job_level;
  8789. clif_updatestatus(sd, SP_SKILLPOINT);
  8790. }
  8791. sd->status.job_level = val;
  8792. sd->status.job_exp = 0;
  8793. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  8794. clif_updatestatus(sd, SP_NEXTJOBEXP);
  8795. clif_updatestatus(sd, SP_JOBEXP);
  8796. status_calc_pc(sd, SCO_FORCE);
  8797. break;
  8798. case SP_SKILLPOINT:
  8799. sd->status.skill_point = val;
  8800. break;
  8801. case SP_STATUSPOINT:
  8802. sd->status.status_point = val;
  8803. break;
  8804. case SP_TRAITPOINT:
  8805. sd->status.trait_point = val;
  8806. break;
  8807. case SP_ZENY:
  8808. if( val < 0 )
  8809. return false;// can't set negative zeny
  8810. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  8811. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  8812. break;
  8813. case SP_BASEEXP:
  8814. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  8815. if (val_tmp < sd->status.base_exp) // Lost
  8816. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  8817. else // Gained
  8818. pc_gainexp(sd, NULL, val_tmp - sd->status.base_exp, 0, 2);
  8819. return true;
  8820. case SP_JOBEXP:
  8821. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  8822. if (val_tmp < sd->status.job_exp) // Lost
  8823. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  8824. else // Gained
  8825. pc_gainexp(sd, NULL, 0, val_tmp - sd->status.job_exp, 2);
  8826. return true;
  8827. case SP_SEX:
  8828. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  8829. break;
  8830. case SP_WEIGHT:
  8831. sd->weight = val;
  8832. break;
  8833. case SP_MAXWEIGHT:
  8834. sd->max_weight = val;
  8835. break;
  8836. case SP_HP:
  8837. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  8838. break;
  8839. case SP_MAXHP:
  8840. if (sd->status.base_level < 100)
  8841. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  8842. else if (sd->status.base_level < 151)
  8843. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  8844. else
  8845. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  8846. if( sd->battle_status.max_hp < sd->battle_status.hp )
  8847. {
  8848. sd->battle_status.hp = sd->battle_status.max_hp;
  8849. clif_updatestatus(sd, SP_HP);
  8850. }
  8851. break;
  8852. case SP_SP:
  8853. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  8854. break;
  8855. case SP_MAXSP:
  8856. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  8857. if( sd->battle_status.max_sp < sd->battle_status.sp )
  8858. {
  8859. sd->battle_status.sp = sd->battle_status.max_sp;
  8860. clif_updatestatus(sd, SP_SP);
  8861. }
  8862. break;
  8863. case SP_AP:
  8864. sd->battle_status.ap = cap_value(val, 0, (int)sd->battle_status.max_ap);
  8865. break;
  8866. case SP_MAXAP:
  8867. sd->battle_status.max_ap = cap_value(val, 1, battle_config.max_ap);
  8868. if (sd->battle_status.max_ap < sd->battle_status.ap) {
  8869. sd->battle_status.ap = sd->battle_status.max_ap;
  8870. clif_updatestatus(sd, SP_AP);
  8871. }
  8872. break;
  8873. case SP_STR:
  8874. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  8875. break;
  8876. case SP_AGI:
  8877. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  8878. break;
  8879. case SP_VIT:
  8880. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  8881. break;
  8882. case SP_INT:
  8883. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  8884. break;
  8885. case SP_DEX:
  8886. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  8887. break;
  8888. case SP_LUK:
  8889. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  8890. break;
  8891. case SP_POW:
  8892. sd->status.pow = cap_value(val, 0, pc_maxparameter(sd,PARAM_POW));
  8893. break;
  8894. case SP_STA:
  8895. sd->status.sta = cap_value(val, 0, pc_maxparameter(sd,PARAM_STA));
  8896. break;
  8897. case SP_WIS:
  8898. sd->status.wis = cap_value(val, 0, pc_maxparameter(sd,PARAM_WIS));
  8899. break;
  8900. case SP_SPL:
  8901. sd->status.spl = cap_value(val, 0, pc_maxparameter(sd,PARAM_SPL));
  8902. break;
  8903. case SP_CON:
  8904. sd->status.con = cap_value(val, 0, pc_maxparameter(sd,PARAM_CON));
  8905. break;
  8906. case SP_CRT:
  8907. sd->status.crt = cap_value(val, 0, pc_maxparameter(sd,PARAM_CRT));
  8908. break;
  8909. case SP_KARMA:
  8910. sd->status.karma = val;
  8911. break;
  8912. case SP_MANNER:
  8913. sd->status.manner = val;
  8914. if( val < 0 )
  8915. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  8916. else {
  8917. status_change_end(&sd->bl, SC_NOCHAT);
  8918. clif_manner_message(sd, 5);
  8919. }
  8920. return true; // status_change_start/status_change_end already sends packets warning the client
  8921. case SP_FAME:
  8922. sd->status.fame = val;
  8923. break;
  8924. case SP_KILLERRID:
  8925. sd->killerrid = val;
  8926. return true;
  8927. case SP_KILLEDRID:
  8928. sd->killedrid = val;
  8929. return true;
  8930. case SP_KILLEDGID:
  8931. sd->killedgid = val;
  8932. return true;
  8933. case SP_CHARMOVE:
  8934. sd->status.character_moves = val;
  8935. return true;
  8936. case SP_CHARRENAME:
  8937. sd->status.rename = val;
  8938. return true;
  8939. case SP_CHARFONT:
  8940. sd->status.font = val;
  8941. clif_font(sd);
  8942. return true;
  8943. case SP_BANK_VAULT:
  8944. if (val < 0)
  8945. return false;
  8946. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  8947. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  8948. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  8949. return true;
  8950. case SP_ROULETTE_BRONZE:
  8951. sd->roulette_point.bronze = val;
  8952. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  8953. return true;
  8954. case SP_ROULETTE_SILVER:
  8955. sd->roulette_point.silver = val;
  8956. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  8957. return true;
  8958. case SP_ROULETTE_GOLD:
  8959. sd->roulette_point.gold = val;
  8960. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  8961. return true;
  8962. case SP_CASHPOINTS:
  8963. if (val < 0)
  8964. return false;
  8965. if (!sd->state.connect_new)
  8966. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_CASHPOINT)));
  8967. sd->cashPoints = cap_value(val, 0, MAX_CASHPOINT);
  8968. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  8969. return true;
  8970. case SP_KAFRAPOINTS:
  8971. if (val < 0)
  8972. return false;
  8973. if (!sd->state.connect_new)
  8974. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_KAFRAPOINT)));
  8975. sd->kafraPoints = cap_value(val, 0, MAX_KAFRAPOINT);
  8976. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  8977. return true;
  8978. case SP_PCDIECOUNTER:
  8979. if (val < 0)
  8980. return false;
  8981. if (sd->die_counter == val)
  8982. return true;
  8983. sd->die_counter = val;
  8984. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  8985. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  8986. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  8987. return true;
  8988. case SP_COOKMASTERY:
  8989. if (sd->cook_mastery == val)
  8990. return true;
  8991. val = cap_value(val, 0, 1999);
  8992. sd->cook_mastery = val;
  8993. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  8994. return true;
  8995. default:
  8996. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  8997. return false;
  8998. }
  8999. clif_updatestatus(sd,static_cast<int>(type));
  9000. return true;
  9001. }
  9002. /*==========================================
  9003. * HP/SP/AP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  9004. *------------------------------------------*/
  9005. void pc_heal(map_session_data *sd,unsigned int hp,unsigned int sp, unsigned int ap, int type)
  9006. {// Is there going to be a effect for gaining AP soon??? [Rytech]
  9007. nullpo_retv(sd);
  9008. if (type&2) {
  9009. if (hp || type&4) {
  9010. clif_heal(sd->fd,SP_HP,hp);
  9011. clif_update_hp(*sd);
  9012. }
  9013. if (sp)
  9014. clif_heal(sd->fd,SP_SP,sp);
  9015. if (ap)
  9016. clif_heal(sd->fd,SP_AP,ap);
  9017. } else {
  9018. if(hp)
  9019. clif_updatestatus(sd,SP_HP);
  9020. if(sp)
  9021. clif_updatestatus(sd,SP_SP);
  9022. if (ap)
  9023. clif_updatestatus(sd,SP_AP);
  9024. }
  9025. return;
  9026. }
  9027. /**
  9028. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  9029. * @param sd: Player data
  9030. * @param itemid: Item ID
  9031. * @param hp: HP to heal
  9032. * @param sp: SP to heal
  9033. * @return Amount healed to an object
  9034. */
  9035. int pc_itemheal(map_session_data *sd, t_itemid itemid, int hp, int sp)
  9036. {
  9037. int bonus, tmp, penalty = 0;
  9038. if (hp) {
  9039. bonus = 100 + (sd->battle_status.vit * 2) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9040. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9041. if (potion_flag == 2) {
  9042. bonus += bonus * 50 / 100;
  9043. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_ROGUE)
  9044. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  9045. }
  9046. //All item bonuses.
  9047. bonus += sd->bonus.itemhealrate2;
  9048. //Item Group bonuses
  9049. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid, sd->itemgrouphealrate) / 100;
  9050. //Individual item bonuses.
  9051. for(const auto &it : sd->itemhealrate) {
  9052. if (it.id == itemid) {
  9053. bonus += bonus * it.val / 100;
  9054. break;
  9055. }
  9056. }
  9057. // Recovery Potion
  9058. if (sd->sc.getSCE(SC_INCHEALRATE))
  9059. bonus += bonus * sd->sc.getSCE(SC_INCHEALRATE)->val1 / 100;
  9060. // 2014 Halloween Event : Pumpkin Bonus
  9061. if (sd->sc.getSCE(SC_MTF_PUMPKIN)) {
  9062. if (itemid == ITEMID_PUMPKIN)
  9063. bonus += bonus * sd->sc.getSCE(SC_MTF_PUMPKIN)->val1 / 100;
  9064. else if (itemid == ITEMID_COOKIE_BAT)
  9065. bonus += sd->sc.getSCE(SC_MTF_PUMPKIN)->val2;
  9066. }
  9067. if (sd->sc.getSCE(SC_VITALIZE_POTION))
  9068. bonus += bonus * 10 / 100;
  9069. tmp = hp * bonus / 100; // Overflow check
  9070. if (bonus != 100 && tmp > hp)
  9071. hp = tmp;
  9072. }
  9073. if (sp) {
  9074. bonus = 100 + (sd->battle_status.int_ * 2) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9075. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9076. if (potion_flag == 2)
  9077. bonus += bonus * 50 / 100;
  9078. // All item bonuses.
  9079. bonus += sd->bonus.itemsphealrate2;
  9080. // Item Group bonuses
  9081. bonus += bonus * pc_get_itemgroup_bonus( sd, itemid, sd->itemgroupsphealrate ) / 100;
  9082. // Individual item bonuses.
  9083. for( const auto &it : sd->itemsphealrate ){
  9084. if( it.id == itemid ){
  9085. bonus += bonus * it.val / 100;
  9086. break;
  9087. }
  9088. }
  9089. tmp = sp * bonus / 100; // Overflow check
  9090. if (bonus != 100 && tmp > sp)
  9091. sp = tmp;
  9092. }
  9093. if (sd->sc.count) {
  9094. // Critical Wound and Death Hurt stack
  9095. if (sd->sc.getSCE(SC_CRITICALWOUND))
  9096. penalty += sd->sc.getSCE(SC_CRITICALWOUND)->val2;
  9097. if (sd->sc.getSCE(SC_DEATHHURT) && sd->sc.getSCE(SC_DEATHHURT)->val3 == 1)
  9098. penalty += 20;
  9099. if (sd->sc.getSCE(SC_NORECOVER_STATE))
  9100. penalty = 100;
  9101. if (sd->sc.getSCE(SC_VITALITYACTIVATION))
  9102. hp += hp / 2; // 1.5 times
  9103. if (sd->sc.getSCE(SC_WATER_INSIGNIA) && sd->sc.getSCE(SC_WATER_INSIGNIA)->val1 == 2) {
  9104. hp += hp / 10;
  9105. sp += sp / 10;
  9106. }
  9107. #ifdef RENEWAL
  9108. if (sd->sc.getSCE(SC_APPLEIDUN))
  9109. hp += sd->sc.getSCE(SC_APPLEIDUN)->val3 / 100;
  9110. #endif
  9111. if (penalty > 0) {
  9112. hp -= hp * penalty / 100;
  9113. sp -= sp * penalty / 100;
  9114. }
  9115. #ifdef RENEWAL
  9116. if (sd->sc.getSCE(SC_EXTREMITYFIST2))
  9117. sp = 0;
  9118. #endif
  9119. if (sd->sc.getSCE(SC_BITESCAR))
  9120. hp = 0;
  9121. }
  9122. return status_heal(&sd->bl, hp, sp, 1);
  9123. }
  9124. /*==========================================
  9125. * HP/SP Recovery
  9126. * Heal player hp nad/or sp by rate
  9127. *------------------------------------------*/
  9128. int pc_percentheal(map_session_data *sd,int hp,int sp)
  9129. {
  9130. nullpo_ret(sd);
  9131. if (hp > 100) hp = 100;
  9132. else if (hp <-100) hp = -100;
  9133. if (sp > 100) sp = 100;
  9134. else if (sp <-100) sp = -100;
  9135. if(hp >= 0 && sp >= 0) //Heal
  9136. return status_percent_heal(&sd->bl, hp, sp);
  9137. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  9138. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  9139. //Crossed signs
  9140. if(hp) {
  9141. if(hp > 0)
  9142. status_percent_heal(&sd->bl, hp, 0);
  9143. else
  9144. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  9145. }
  9146. if(sp) {
  9147. if(sp > 0)
  9148. status_percent_heal(&sd->bl, 0, sp);
  9149. else
  9150. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  9151. }
  9152. return 0;
  9153. }
  9154. static int jobchange_killclone(struct block_list *bl, va_list ap)
  9155. {
  9156. struct mob_data *md;
  9157. int flag;
  9158. md = (struct mob_data *)bl;
  9159. nullpo_ret(md);
  9160. flag = va_arg(ap, int);
  9161. if (md->master_id && md->special_state.clone && md->master_id == flag)
  9162. status_kill(&md->bl);
  9163. return 1;
  9164. }
  9165. /**
  9166. * Called when player changes job
  9167. * Rewrote to make it tidider [Celest]
  9168. * @param sd
  9169. * @param job JOB ID. See enum e_job
  9170. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  9171. * @return True if success, false if failed
  9172. **/
  9173. bool pc_jobchange(map_session_data *sd,int job, char upper)
  9174. {
  9175. int i, fame_flag = 0;
  9176. nullpo_retr(false,sd);
  9177. if (job < 0)
  9178. return false;
  9179. //Normalize job.
  9180. uint64 b_class = pc_jobid2mapid(job);
  9181. if (b_class == -1)
  9182. return false;
  9183. switch (upper) {
  9184. case 1:
  9185. b_class|= JOBL_UPPER;
  9186. break;
  9187. case 2:
  9188. b_class|= JOBL_BABY;
  9189. break;
  9190. }
  9191. //This will automatically adjust bard/dancer classes to the correct gender
  9192. //That is, if you try to jobchange into dancer, it will turn you to bard.
  9193. job = pc_mapid2jobid(b_class, sd->status.sex);
  9194. if (job == -1)
  9195. return false;
  9196. if ((unsigned short)b_class == sd->class_)
  9197. return false; //Nothing to change.
  9198. // If the job does not exist in the job db, dont allow changing to it
  9199. if( !job_db.exists( job ) ){
  9200. return false;
  9201. }
  9202. if( ( b_class&JOBL_FOURTH ) && !( sd->class_&JOBL_FOURTH ) ){
  9203. // Changing to 4th job
  9204. sd->change_level_4th = sd->status.job_level;
  9205. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ) , sd->change_level_4th );
  9206. }else if( ( b_class&JOBL_THIRD ) && !( sd->class_&JOBL_THIRD ) ){
  9207. // changing from 2nd to 3rd job
  9208. sd->change_level_3rd = sd->status.job_level;
  9209. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  9210. }else if( ( b_class&JOBL_2 ) && !( sd->class_&JOBL_2 ) ){
  9211. // changing from 1st to 2nd job
  9212. sd->change_level_2nd = sd->status.job_level;
  9213. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  9214. }
  9215. if(sd->cloneskill_idx > 0) {
  9216. pc_skill_plagiarism_reset(*sd, 1);
  9217. }
  9218. if(sd->reproduceskill_idx > 0) {
  9219. pc_skill_plagiarism_reset(*sd, 2);
  9220. }
  9221. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  9222. status_db.changeSkillTree(sd);
  9223. }
  9224. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  9225. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  9226. pc_resetfeel(sd);
  9227. pc_resethate(sd);
  9228. }
  9229. // Reset body style to 0 before changing job to avoid
  9230. // errors since not every job has a alternate outfit.
  9231. sd->status.body = 0;
  9232. clif_changelook(&sd->bl,LOOK_BODY2,0);
  9233. sd->status.class_ = job;
  9234. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  9235. uint64 previous_class = sd->class_;
  9236. sd->class_ = (unsigned short)b_class;
  9237. sd->status.job_level=1;
  9238. sd->status.job_exp=0;
  9239. if (sd->status.base_level > pc_maxbaselv(sd)) {
  9240. sd->status.base_level = pc_maxbaselv(sd);
  9241. sd->status.base_exp=0;
  9242. pc_resetstate(sd);
  9243. clif_updatestatus(sd,SP_STATUSPOINT);
  9244. clif_updatestatus(sd,SP_TRAITPOINT);
  9245. clif_updatestatus(sd,SP_BASELEVEL);
  9246. clif_updatestatus(sd,SP_BASEEXP);
  9247. clif_updatestatus(sd,SP_NEXTBASEEXP);
  9248. }
  9249. // Give or reduce transcendent status points
  9250. if( (b_class&JOBL_UPPER) && !(previous_class&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  9251. sd->status.status_point += battle_config.transcendent_status_points;
  9252. clif_updatestatus(sd,SP_STATUSPOINT);
  9253. }else if( !(b_class&JOBL_UPPER) && (previous_class&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  9254. if( sd->status.status_point < battle_config.transcendent_status_points ){
  9255. // The player already used his bonus points, so we have to reset his status points
  9256. pc_resetstate(sd);
  9257. }else{
  9258. sd->status.status_point -= battle_config.transcendent_status_points;
  9259. clif_updatestatus(sd,SP_STATUSPOINT);
  9260. }
  9261. }
  9262. // Give or reduce trait status points
  9263. if ((b_class & JOBL_FOURTH) && !(previous_class & JOBL_FOURTH)) {// Change to a 4th job.
  9264. sd->status.trait_point += battle_config.trait_points_job_change;
  9265. clif_updatestatus(sd, SP_TRAITPOINT);
  9266. clif_updatestatus(sd, SP_UPOW);
  9267. clif_updatestatus(sd, SP_USTA);
  9268. clif_updatestatus(sd, SP_UWIS);
  9269. clif_updatestatus(sd, SP_USPL);
  9270. clif_updatestatus(sd, SP_UCON);
  9271. clif_updatestatus(sd, SP_UCRT);
  9272. } else if (!(b_class & JOBL_FOURTH) && (previous_class & JOBL_FOURTH)) {// Change to a non 4th job.
  9273. if (sd->status.trait_point < battle_config.trait_points_job_change) {
  9274. // Player may have already used the trait status points. Force a reset.
  9275. pc_resetstate(sd);
  9276. } else {
  9277. sd->status.trait_point = 0;
  9278. clif_updatestatus(sd, SP_TRAITPOINT);
  9279. clif_updatestatus(sd, SP_UPOW);
  9280. clif_updatestatus(sd, SP_USTA);
  9281. clif_updatestatus(sd, SP_UWIS);
  9282. clif_updatestatus(sd, SP_USPL);
  9283. clif_updatestatus(sd, SP_UCON);
  9284. clif_updatestatus(sd, SP_UCRT);
  9285. }
  9286. }
  9287. clif_updatestatus(sd,SP_JOBLEVEL);
  9288. clif_updatestatus(sd,SP_JOBEXP);
  9289. clif_updatestatus(sd,SP_NEXTJOBEXP);
  9290. for(i=0;i<EQI_MAX;i++) {
  9291. if(sd->equip_index[i] >= 0)
  9292. if(pc_isequip(sd,sd->equip_index[i]))
  9293. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  9294. }
  9295. //Change look, if disguised, you need to undisguise
  9296. //to correctly calculate new job sprite without
  9297. if (sd->disguise)
  9298. pc_disguise(sd, 0);
  9299. status_set_viewdata(&sd->bl, job);
  9300. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  9301. #if PACKETVER >= 20151001
  9302. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  9303. #endif
  9304. if(sd->vd.cloth_color)
  9305. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9306. /*
  9307. if(sd->vd.body_style)
  9308. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9309. */
  9310. //Update skill tree.
  9311. pc_calc_skilltree(sd);
  9312. clif_skillinfoblock(sd);
  9313. if (sd->ed)
  9314. elemental_delete(sd->ed);
  9315. if (sd->state.vending)
  9316. vending_closevending(sd);
  9317. if (sd->state.buyingstore)
  9318. buyingstore_close(sd);
  9319. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  9320. //Remove peco/cart/falcon
  9321. i = sd->sc.option;
  9322. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  9323. i&=~OPTION_RIDING;
  9324. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  9325. i&=~OPTION_FALCON;
  9326. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  9327. i&=~OPTION_DRAGON;
  9328. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  9329. i&=~OPTION_WUGRIDER;
  9330. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  9331. i&=~OPTION_WUG;
  9332. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  9333. i&=~OPTION_MADOGEAR;
  9334. #ifndef NEW_CARTS
  9335. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  9336. i&=~OPTION_CART;
  9337. #else
  9338. if( sd->sc.getSCE(SC_PUSH_CART) && !pc_checkskill(sd, MC_PUSHCART) )
  9339. pc_setcart(sd, 0);
  9340. #endif
  9341. if(i != sd->sc.option)
  9342. pc_setoption(sd, i);
  9343. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  9344. hom_vaporize(sd, HOM_ST_ACTIVE);
  9345. if (sd->sc.getSCE(SC_SPRITEMABLE) && !pc_checkskill(sd, SU_SPRITEMABLE))
  9346. status_change_end(&sd->bl, SC_SPRITEMABLE);
  9347. if (sd->sc.getSCE(SC_SOULATTACK) && !pc_checkskill(sd, SU_SOULATTACK))
  9348. status_change_end(&sd->bl, SC_SOULATTACK);
  9349. if( sd->sc.getSCE(SC_SPIRIT) ){
  9350. status_change_end( &sd->bl, SC_SPIRIT );
  9351. }
  9352. if(sd->status.manner < 0)
  9353. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  9354. status_calc_pc(sd,SCO_FORCE);
  9355. pc_checkallowskill(sd);
  9356. pc_equiplookall(sd);
  9357. pc_show_questinfo(sd);
  9358. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  9359. if( sd->status.party_id ){
  9360. struct party_data* p;
  9361. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  9362. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  9363. if( i < MAX_PARTY ){
  9364. p->party.member[i].class_ = sd->status.class_;
  9365. clif_party_job_and_level( *sd );
  9366. }
  9367. }
  9368. }
  9369. chrif_save(sd, CSAVE_NORMAL);
  9370. //if you were previously famous, not anymore.
  9371. if (fame_flag)
  9372. chrif_buildfamelist();
  9373. else if (sd->status.fame > 0) {
  9374. //It may be that now they are famous?
  9375. switch (sd->class_&MAPID_UPPERMASK) {
  9376. case MAPID_BLACKSMITH:
  9377. case MAPID_ALCHEMIST:
  9378. case MAPID_TAEKWON:
  9379. chrif_buildfamelist();
  9380. break;
  9381. }
  9382. }
  9383. return true;
  9384. }
  9385. /*==========================================
  9386. * Tell client player sd has change equipement
  9387. *------------------------------------------*/
  9388. void pc_equiplookall(map_session_data *sd)
  9389. {
  9390. nullpo_retv(sd);
  9391. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  9392. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9393. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  9394. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  9395. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  9396. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  9397. }
  9398. /*==========================================
  9399. * Tell client player sd has change look (hair,equip...)
  9400. *------------------------------------------*/
  9401. void pc_changelook(map_session_data *sd,int type,int val) {
  9402. nullpo_retv(sd);
  9403. switch(type) {
  9404. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  9405. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  9406. if (sd->status.hair != val) {
  9407. sd->status.hair = val;
  9408. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9409. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9410. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  9411. }
  9412. break;
  9413. case LOOK_WEAPON:
  9414. sd->status.weapon = val;
  9415. break;
  9416. case LOOK_HEAD_BOTTOM:
  9417. sd->status.head_bottom = val;
  9418. sd->setlook_head_bottom = val;
  9419. break;
  9420. case LOOK_HEAD_TOP:
  9421. sd->status.head_top = val;
  9422. sd->setlook_head_top = val;
  9423. break;
  9424. case LOOK_HEAD_MID:
  9425. sd->status.head_mid = val;
  9426. sd->setlook_head_mid = val;
  9427. break;
  9428. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  9429. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  9430. if (sd->status.hair_color != val) {
  9431. sd->status.hair_color = val;
  9432. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9433. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9434. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  9435. }
  9436. break;
  9437. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  9438. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  9439. sd->status.clothes_color = val;
  9440. break;
  9441. case LOOK_SHIELD:
  9442. sd->status.shield = val;
  9443. break;
  9444. case LOOK_SHOES:
  9445. break;
  9446. case LOOK_ROBE:
  9447. sd->status.robe = val;
  9448. sd->setlook_robe = val;
  9449. break;
  9450. case LOOK_BODY2:
  9451. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  9452. sd->status.body = val;
  9453. break;
  9454. }
  9455. clif_changelook(&sd->bl, type, val);
  9456. }
  9457. /*==========================================
  9458. * Give an option (type) to player (sd) and display it to client
  9459. *------------------------------------------*/
  9460. void pc_setoption(map_session_data *sd,int type, int subtype)
  9461. {
  9462. int p_type, new_look=0;
  9463. nullpo_retv(sd);
  9464. p_type = sd->sc.option;
  9465. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  9466. sd->sc.option=type;
  9467. clif_changeoption(&sd->bl);
  9468. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9469. { // Mounting
  9470. clif_status_load(&sd->bl,EFST_RIDING,1);
  9471. status_calc_pc(sd,SCO_NONE);
  9472. }
  9473. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9474. { // Dismount
  9475. clif_status_load(&sd->bl,EFST_RIDING,0);
  9476. status_calc_pc(sd,SCO_NONE);
  9477. }
  9478. #ifndef NEW_CARTS
  9479. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  9480. clif_cartlist(sd);
  9481. clif_updatestatus(sd, SP_CARTINFO);
  9482. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9483. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  9484. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  9485. clif_clearcart(sd->fd);
  9486. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9487. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  9488. }
  9489. #endif
  9490. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  9491. clif_status_load(&sd->bl,EFST_FALCON,1);
  9492. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  9493. clif_status_load(&sd->bl,EFST_FALCON,0);
  9494. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  9495. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  9496. status_calc_pc(sd,SCO_NONE);
  9497. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  9498. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  9499. status_calc_pc(sd,SCO_NONE);
  9500. }
  9501. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  9502. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  9503. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  9504. status_change_end(&sd->bl, SC_MADOGEAR);
  9505. }
  9506. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  9507. new_look = JOB_STAR_GLADIATOR2;
  9508. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  9509. new_look = -1;
  9510. if (sd->disguise || !new_look)
  9511. return; //Disguises break sprite changes
  9512. if (new_look < 0) { //Restore normal look.
  9513. status_set_viewdata(&sd->bl, sd->status.class_);
  9514. new_look = sd->vd.class_;
  9515. }
  9516. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  9517. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  9518. if (sd->vd.cloth_color)
  9519. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9520. if( sd->vd.body_style )
  9521. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9522. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  9523. }
  9524. /**
  9525. * Give player a cart
  9526. * @param sd Player
  9527. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  9528. **/
  9529. bool pc_setcart(map_session_data *sd,int type) {
  9530. #ifndef NEW_CARTS
  9531. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  9532. int option;
  9533. #endif
  9534. nullpo_retr(false,sd);
  9535. if( type < 0 || type > MAX_CARTS )
  9536. return false;// Never trust the values sent by the client! [Skotlex]
  9537. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  9538. return false;// Push cart is required
  9539. #ifdef NEW_CARTS
  9540. switch( type ) {
  9541. case 0:
  9542. if( !sd->sc.getSCE(SC_PUSH_CART) )
  9543. return 0;
  9544. status_change_end(&sd->bl,SC_PUSH_CART);
  9545. clif_clearcart(sd->fd);
  9546. break;
  9547. default:/* everything else is an allowed ID so we can move on */
  9548. if( !sd->sc.getSCE(SC_PUSH_CART) ) { /* first time, so fill cart data */
  9549. clif_cartlist(sd);
  9550. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  9551. }
  9552. clif_updatestatus(sd, SP_CARTINFO);
  9553. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  9554. break;
  9555. }
  9556. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9557. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  9558. #else
  9559. // Update option
  9560. option = sd->sc.option;
  9561. option &= ~OPTION_CART;// clear cart bits
  9562. option |= cart[type]; // set cart
  9563. pc_setoption(sd, option);
  9564. #endif
  9565. return true;
  9566. }
  9567. /*==========================================
  9568. * Give player a falcon
  9569. *------------------------------------------*/
  9570. void pc_setfalcon(map_session_data* sd, int flag)
  9571. {
  9572. if( flag ){
  9573. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  9574. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  9575. } else if( pc_isfalcon(sd) ){
  9576. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  9577. }
  9578. }
  9579. /*==========================================
  9580. * Set player riding
  9581. *------------------------------------------*/
  9582. void pc_setriding(map_session_data* sd, int flag)
  9583. {
  9584. if( sd->sc.getSCE(SC_ALL_RIDING) )
  9585. return;
  9586. if( flag ){
  9587. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  9588. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  9589. } else if( pc_isriding(sd) ){
  9590. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  9591. }
  9592. }
  9593. /**
  9594. * Give player a mado
  9595. * @param sd: Player
  9596. * @param flag: Enable or disable mado
  9597. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  9598. */
  9599. void pc_setmadogear(map_session_data *sd, bool flag, e_mado_type type)
  9600. {
  9601. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  9602. return;
  9603. if (flag) {
  9604. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  9605. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  9606. } else if (pc_ismadogear(sd)) {
  9607. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  9608. }
  9609. }
  9610. /*==========================================
  9611. * Check if player can drop an item
  9612. *------------------------------------------*/
  9613. bool pc_candrop(map_session_data *sd, struct item *item)
  9614. {
  9615. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  9616. return false;
  9617. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  9618. return false;
  9619. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  9620. }
  9621. /*==========================================
  9622. * Read '@type' variables (temporary numeric char reg)
  9623. *------------------------------------------*/
  9624. int64 pc_readreg(map_session_data* sd, int64 reg)
  9625. {
  9626. return i64db_i64get(sd->regs.vars, reg);
  9627. }
  9628. /*==========================================
  9629. * Set '@type' variables (temporary numeric char reg)
  9630. *------------------------------------------*/
  9631. bool pc_setreg(map_session_data* sd, int64 reg, int64 val)
  9632. {
  9633. uint32 index = script_getvaridx(reg);
  9634. nullpo_retr(false, sd);
  9635. if( val ) {
  9636. i64db_i64put(sd->regs.vars, reg, val);
  9637. if( index )
  9638. script_array_update(&sd->regs, reg, false);
  9639. } else {
  9640. i64db_remove(sd->regs.vars, reg);
  9641. if( index )
  9642. script_array_update(&sd->regs, reg, true);
  9643. }
  9644. return true;
  9645. }
  9646. /*==========================================
  9647. * Read '@type$' variables (temporary string char reg)
  9648. *------------------------------------------*/
  9649. char* pc_readregstr(map_session_data* sd, int64 reg)
  9650. {
  9651. struct script_reg_str *p = NULL;
  9652. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9653. return p ? p->value : NULL;
  9654. }
  9655. /*==========================================
  9656. * Set '@type$' variables (temporary string char reg)
  9657. *------------------------------------------*/
  9658. bool pc_setregstr(map_session_data* sd, int64 reg, const char* str)
  9659. {
  9660. struct script_reg_str *p = NULL;
  9661. unsigned int index = script_getvaridx(reg);
  9662. DBData prev;
  9663. nullpo_retr(false, sd);
  9664. if( str[0] ) {
  9665. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9666. p->value = aStrdup(str);
  9667. p->flag.type = 1;
  9668. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9669. p = (struct script_reg_str *)db_data2ptr(&prev);
  9670. if( p->value )
  9671. aFree(p->value);
  9672. ers_free(str_reg_ers, p);
  9673. } else {
  9674. if( index )
  9675. script_array_update(&sd->regs, reg, false);
  9676. }
  9677. } else {
  9678. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  9679. p = (struct script_reg_str *)db_data2ptr(&prev);
  9680. if( p->value )
  9681. aFree(p->value);
  9682. ers_free(str_reg_ers, p);
  9683. if( index )
  9684. script_array_update(&sd->regs, reg, true);
  9685. }
  9686. }
  9687. return true;
  9688. }
  9689. /**
  9690. * Serves the following variable types:
  9691. * - 'type' (permanent numeric char reg)
  9692. * - '#type' (permanent numeric account reg)
  9693. * - '##type' (permanent numeric account reg2)
  9694. **/
  9695. int64 pc_readregistry(map_session_data *sd, int64 reg)
  9696. {
  9697. struct script_reg_num *p = NULL;
  9698. if (!sd->vars_ok) {
  9699. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9700. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9701. //intif->request_registry(sd,type==3?4:type);
  9702. set_eof(sd->fd);
  9703. return 0;
  9704. }
  9705. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  9706. return p ? p->value : 0;
  9707. }
  9708. /**
  9709. * Serves the following variable types:
  9710. * - 'type$' (permanent str char reg)
  9711. * - '#type$' (permanent str account reg)
  9712. * - '##type$' (permanent str account reg2)
  9713. **/
  9714. char* pc_readregistry_str(map_session_data *sd, int64 reg)
  9715. {
  9716. struct script_reg_str *p = NULL;
  9717. if (!sd->vars_ok) {
  9718. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9719. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9720. //intif->request_registry(sd,type==3?4:type);
  9721. set_eof(sd->fd);
  9722. return NULL;
  9723. }
  9724. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9725. return p ? p->value : NULL;
  9726. }
  9727. /**
  9728. * Serves the following variable types:
  9729. * - 'type' (permanent numeric char reg)
  9730. * - '#type' (permanent numeric account reg)
  9731. * - '##type' (permanent numeric account reg2)
  9732. **/
  9733. bool pc_setregistry(map_session_data *sd, int64 reg, int64 val)
  9734. {
  9735. struct script_reg_num *p = NULL;
  9736. const char *regname = get_str(script_getvarid(reg));
  9737. uint32 index = script_getvaridx(reg);
  9738. if ( !reg_load && !sd->vars_ok ) {
  9739. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  9740. return false;
  9741. }
  9742. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  9743. if( val ) {
  9744. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  9745. script_array_update(&sd->regs, reg, false);
  9746. p->value = val;
  9747. } else {
  9748. p->value = 0;
  9749. if( index )
  9750. script_array_update(&sd->regs, reg, true);
  9751. }
  9752. if (!reg_load)
  9753. p->flag.update = 1;/* either way, it will require either delete or replace */
  9754. } else if( val ) {
  9755. DBData prev;
  9756. if( index )
  9757. script_array_update(&sd->regs, reg, false);
  9758. p = ers_alloc(num_reg_ers, struct script_reg_num);
  9759. p->value = val;
  9760. if (!reg_load)
  9761. p->flag.update = 1;
  9762. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9763. p = (struct script_reg_num *)db_data2ptr(&prev);
  9764. ers_free(num_reg_ers, p);
  9765. }
  9766. }
  9767. if (!reg_load && p)
  9768. sd->vars_dirty = true;
  9769. return true;
  9770. }
  9771. /**
  9772. * Serves the following variable types:
  9773. * - 'type$' (permanent str char reg)
  9774. * - '#type$' (permanent str account reg)
  9775. * - '##type$' (permanent str account reg2)
  9776. **/
  9777. bool pc_setregistry_str(map_session_data *sd, int64 reg, const char *val)
  9778. {
  9779. struct script_reg_str *p = NULL;
  9780. const char *regname = get_str(script_getvarid(reg));
  9781. unsigned int index = script_getvaridx(reg);
  9782. size_t vlen = 0;
  9783. if (!reg_load && !sd->vars_ok) {
  9784. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  9785. return false;
  9786. }
  9787. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  9788. {
  9789. 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);
  9790. return false;
  9791. }
  9792. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  9793. if( val[0] ) {
  9794. if( p->value )
  9795. aFree(p->value);
  9796. else if ( index ) // an entry that was deleted, so we reset
  9797. script_array_update(&sd->regs, reg, false);
  9798. p->value = aStrdup(val);
  9799. } else {
  9800. if (p->value)
  9801. aFree(p->value);
  9802. p->value = NULL;
  9803. if( index )
  9804. script_array_update(&sd->regs, reg, true);
  9805. }
  9806. if( !reg_load )
  9807. p->flag.update = 1; // either way, it will require either delete or replace
  9808. } else if( val[0] ) {
  9809. DBData prev;
  9810. if( index )
  9811. script_array_update(&sd->regs, reg, false);
  9812. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9813. p->value = aStrdup(val);
  9814. if( !reg_load )
  9815. p->flag.update = 1;
  9816. p->flag.type = 1;
  9817. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  9818. p = (struct script_reg_str *)db_data2ptr(&prev);
  9819. if( p->value )
  9820. aFree(p->value);
  9821. ers_free(str_reg_ers, p);
  9822. }
  9823. }
  9824. if( !reg_load && p )
  9825. sd->vars_dirty = true;
  9826. return true;
  9827. }
  9828. /**
  9829. * Set value of player variable
  9830. * @param sd Player
  9831. * @param reg Variable name
  9832. * @param value
  9833. * @return True if success, false if failed.
  9834. **/
  9835. bool pc_setreg2(map_session_data *sd, const char *reg, int64 val) {
  9836. char prefix = reg[0];
  9837. nullpo_retr(false, sd);
  9838. if (reg[strlen(reg)-1] == '$') {
  9839. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9840. return false;
  9841. }
  9842. val = cap_value(val, INT_MIN, INT_MAX);
  9843. switch (prefix) {
  9844. case '.':
  9845. case '\'':
  9846. case '$':
  9847. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9848. return false;
  9849. case '@':
  9850. return pc_setreg(sd, add_str(reg), val);
  9851. case '#':
  9852. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  9853. default:
  9854. return pc_setglobalreg(sd, add_str(reg), val);
  9855. }
  9856. return false;
  9857. }
  9858. /**
  9859. * Get value of player variable
  9860. * @param sd Player
  9861. * @param reg Variable name
  9862. * @return Variable value or 0 if failed.
  9863. **/
  9864. int64 pc_readreg2(map_session_data *sd, const char *reg) {
  9865. char prefix = reg[0];
  9866. nullpo_ret(sd);
  9867. if (reg[strlen(reg)-1] == '$') {
  9868. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9869. return 0;
  9870. }
  9871. switch (prefix) {
  9872. case '.':
  9873. case '\'':
  9874. case '$':
  9875. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9876. return 0;
  9877. case '@':
  9878. return pc_readreg(sd, add_str(reg));
  9879. case '#':
  9880. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  9881. default:
  9882. return pc_readglobalreg(sd, add_str(reg));
  9883. }
  9884. return 0;
  9885. }
  9886. /*==========================================
  9887. * Exec eventtimer for player sd (retrieved from map_session (id))
  9888. *------------------------------------------*/
  9889. static TIMER_FUNC(pc_eventtimer){
  9890. map_session_data *sd=map_id2sd(id);
  9891. char *p = (char *)data;
  9892. int i;
  9893. if(sd==NULL)
  9894. return 0;
  9895. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  9896. if( i < MAX_EVENTTIMER )
  9897. {
  9898. sd->eventtimer[i] = INVALID_TIMER;
  9899. sd->eventcount--;
  9900. npc_event(sd,p,0);
  9901. }
  9902. else
  9903. ShowError("pc_eventtimer: no such event timer\n");
  9904. if (p) aFree(p);
  9905. return 0;
  9906. }
  9907. /*==========================================
  9908. * Add eventtimer for player sd ?
  9909. *------------------------------------------*/
  9910. bool pc_addeventtimer(map_session_data *sd,int tick,const char *name)
  9911. {
  9912. int i;
  9913. nullpo_retr(false,sd);
  9914. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  9915. if( i == MAX_EVENTTIMER )
  9916. return false;
  9917. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  9918. sd->eventcount++;
  9919. return true;
  9920. }
  9921. /*==========================================
  9922. * Del eventtimer for player sd ?
  9923. *------------------------------------------*/
  9924. bool pc_deleventtimer(map_session_data *sd,const char *name)
  9925. {
  9926. char* p = NULL;
  9927. int i;
  9928. nullpo_retr(false,sd);
  9929. if (sd->eventcount == 0)
  9930. return false;
  9931. // find the named event timer
  9932. ARR_FIND( 0, MAX_EVENTTIMER, i,
  9933. sd->eventtimer[i] != INVALID_TIMER &&
  9934. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  9935. strcmp(p, name) == 0
  9936. );
  9937. if( i == MAX_EVENTTIMER )
  9938. return false; // not found
  9939. delete_timer(sd->eventtimer[i],pc_eventtimer);
  9940. sd->eventtimer[i] = INVALID_TIMER;
  9941. if(sd->eventcount > 0)
  9942. sd->eventcount--;
  9943. aFree(p);
  9944. return true;
  9945. }
  9946. /*==========================================
  9947. * Update eventtimer count for player sd
  9948. *------------------------------------------*/
  9949. void pc_addeventtimercount(map_session_data *sd,const char *name,int tick)
  9950. {
  9951. int i;
  9952. nullpo_retv(sd);
  9953. for(i=0;i<MAX_EVENTTIMER;i++)
  9954. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  9955. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  9956. addtick_timer(sd->eventtimer[i],tick);
  9957. break;
  9958. }
  9959. }
  9960. /*==========================================
  9961. * Remove all eventtimer for player sd
  9962. *------------------------------------------*/
  9963. void pc_cleareventtimer(map_session_data *sd)
  9964. {
  9965. int i;
  9966. nullpo_retv(sd);
  9967. if (sd->eventcount == 0)
  9968. return;
  9969. for(i=0;i<MAX_EVENTTIMER;i++){
  9970. if( sd->eventtimer[i] != INVALID_TIMER ){
  9971. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  9972. delete_timer(sd->eventtimer[i],pc_eventtimer);
  9973. sd->eventtimer[i] = INVALID_TIMER;
  9974. if(sd->eventcount > 0) //avoid looping to max val
  9975. sd->eventcount--;
  9976. if (p) aFree(p);
  9977. }
  9978. }
  9979. }
  9980. /**
  9981. * Called when an item with combo is worn
  9982. * @param sd: Player data
  9983. * @param data: Item data
  9984. * @return Number of succeeded combo(s)
  9985. */
  9986. static int pc_checkcombo(map_session_data *sd, item_data *data) {
  9987. int success = 0;
  9988. for (const auto &item_combo : data->combos) {
  9989. bool do_continue = false;
  9990. // Ensure this isn't a duplicate combo
  9991. for (const auto player_combo : sd->combos) {
  9992. if (player_combo->id == item_combo->id) {
  9993. do_continue = true;
  9994. break;
  9995. }
  9996. }
  9997. // Combo already equipped
  9998. if (do_continue)
  9999. continue;
  10000. size_t nb_itemCombo = item_combo->nameid.size();
  10001. if (nb_itemCombo < 2) // A combo with less then 2 item?
  10002. continue;
  10003. struct s_itemchk {
  10004. int idx;
  10005. t_itemid nameid, card[MAX_SLOTS];
  10006. s_itemchk() : idx(0), nameid(0), card() {};
  10007. };
  10008. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  10009. size_t j;
  10010. unsigned int pos = 0;
  10011. for (j = 0; j < nb_itemCombo; j++) {
  10012. t_itemid id = item_combo->nameid[j];
  10013. bool found = false;
  10014. for (int16 k = 0; k < EQI_MAX; k++) {
  10015. short index = sd->equip_index[k];
  10016. if (index < 0)
  10017. continue;
  10018. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  10019. continue;
  10020. if (!sd->inventory_data[index])
  10021. continue;
  10022. if (itemdb_type(id) != IT_CARD) {
  10023. if (sd->inventory_data[index]->nameid != id)
  10024. continue;
  10025. if (j > 0) { // Check if this item not already used
  10026. do_continue = false;
  10027. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  10028. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  10029. do_continue = true;
  10030. break;
  10031. }
  10032. }
  10033. if (do_continue)
  10034. continue;
  10035. }
  10036. combo_idx[j].nameid = id;
  10037. combo_idx[j].idx = index;
  10038. pos |= sd->inventory.u.items_inventory[index].equip;
  10039. found = true;
  10040. break;
  10041. } else { // Cards and enchants
  10042. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  10043. continue;
  10044. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  10045. do_continue = false;
  10046. if (sd->inventory.u.items_inventory[index].card[z] != id)
  10047. continue;
  10048. if (j > 0) {
  10049. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  10050. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  10051. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  10052. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  10053. do_continue = true;
  10054. break;
  10055. }
  10056. }
  10057. }
  10058. }
  10059. }
  10060. if (do_continue)
  10061. continue;
  10062. combo_idx[j].nameid = id;
  10063. combo_idx[j].idx = index;
  10064. combo_idx[j].card[z] = id;
  10065. pos |= sd->inventory.u.items_inventory[index].equip;
  10066. found = true;
  10067. break;
  10068. }
  10069. }
  10070. }
  10071. if (!found)
  10072. break; // Unable to found all the IDs for this combo, return
  10073. }
  10074. // Broke out of the count loop without finding all IDs, move to the next combo
  10075. if (j < nb_itemCombo)
  10076. continue;
  10077. // All items in the combo are matching
  10078. auto entry = std::make_shared<s_combos>();
  10079. entry->bonus = item_combo->script;
  10080. entry->id = item_combo->id;
  10081. entry->pos = pos;
  10082. sd->combos.push_back(entry);
  10083. combo_idx.clear();
  10084. success++;
  10085. }
  10086. return success;
  10087. }
  10088. /**
  10089. * Called when an item with combo is removed
  10090. * @param sd: Player data
  10091. * @param data: Item data
  10092. * @return Number of removed combo(s)
  10093. */
  10094. static int pc_removecombo(map_session_data *sd, item_data *data ) {
  10095. if (sd->combos.empty())
  10096. return 0; // Nothing to do here, player has no combos
  10097. int retval = 0;
  10098. for (const auto &item_combo : data->combos) {
  10099. std::shared_ptr<s_combos> del_combo = nullptr;
  10100. // Check if this combo exists on this player
  10101. for (const auto &player_combo : sd->combos) {
  10102. if (player_combo->id == item_combo->id) {
  10103. del_combo = player_combo;
  10104. break;
  10105. }
  10106. }
  10107. // No match, skip this combo
  10108. if (del_combo == nullptr)
  10109. continue;
  10110. util::vector_erase_if_exists(sd->combos, del_combo);
  10111. retval++;
  10112. // Check if combo requirements still fit
  10113. if (pc_checkcombo(sd, data))
  10114. continue;
  10115. // It's empty, clear all the memory
  10116. if (sd->combos.empty()) {
  10117. sd->combos.clear();
  10118. return retval; // Return at this point as there are no more combos to check
  10119. }
  10120. }
  10121. return retval;
  10122. }
  10123. /**
  10124. * Load combo data(s) of player
  10125. * @param sd: Player data
  10126. * @return ret numbers of succeed combo
  10127. */
  10128. int pc_load_combo(map_session_data *sd) {
  10129. int ret = 0;
  10130. for (int16 i = 0; i < EQI_MAX; i++) {
  10131. item_data *id;
  10132. short idx = sd->equip_index[i];
  10133. if (idx < 0 || !(id = sd->inventory_data[idx]))
  10134. continue;
  10135. if (!id->combos.empty())
  10136. ret += pc_checkcombo(sd, id);
  10137. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  10138. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  10139. if (!sd->inventory.u.items_inventory[idx].card[j])
  10140. continue;
  10141. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[idx].card[j]);
  10142. if (data != nullptr) {
  10143. if (!data->combos.empty())
  10144. ret += pc_checkcombo(sd, data.get());
  10145. }
  10146. }
  10147. }
  10148. }
  10149. return ret;
  10150. }
  10151. /*==========================================
  10152. * Equip item on player sd at req_pos from inventory index n
  10153. * return: false - fail; true - success
  10154. *------------------------------------------*/
  10155. bool pc_equipitem(map_session_data *sd,short n,int req_pos,bool equipswitch)
  10156. {
  10157. int i, pos, flag = 0, iflag;
  10158. struct item_data *id;
  10159. uint8 res = ITEM_EQUIP_ACK_OK;
  10160. short* equip_index;
  10161. nullpo_retr(false,sd);
  10162. if( n < 0 || n >= MAX_INVENTORY ) {
  10163. if( equipswitch ){
  10164. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10165. }else{
  10166. // Does not deserve an answer... [Lemongrass]
  10167. //clif_equipitemack( sd, ITEM_EQUIP_ACK_FAIL, n );
  10168. }
  10169. return false;
  10170. }
  10171. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  10172. if( equipswitch ){
  10173. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10174. }else{
  10175. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n );
  10176. }
  10177. return false;
  10178. }
  10179. if (!(id = sd->inventory_data[n]))
  10180. return false;
  10181. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  10182. if(battle_config.battle_log && !equipswitch)
  10183. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  10184. if((res = pc_isequip(sd,n))) {
  10185. if( equipswitch ){
  10186. clif_equipswitch_add( sd, n, req_pos, res );
  10187. }else{
  10188. clif_equipitemack( *sd, res, n ); // fail
  10189. }
  10190. return false;
  10191. }
  10192. if( equipswitch && id->type == IT_AMMO ){
  10193. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10194. return false;
  10195. }
  10196. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  10197. if( equipswitch ){
  10198. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10199. }else{
  10200. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); // fail
  10201. }
  10202. return false;
  10203. }
  10204. if( sd->sc.count && (sd->sc.cant.equip || (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  10205. if( equipswitch ){
  10206. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10207. }else{
  10208. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); //Fail
  10209. }
  10210. return false;
  10211. }
  10212. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  10213. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  10214. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  10215. clif_notify_bindOnEquip( *sd, n );
  10216. }
  10217. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  10218. pos = req_pos&EQP_ACC;
  10219. if (pos == EQP_ACC) //User specified both slots.
  10220. pos = (equip_index[EQI_ACC_R] >= 0 && equip_index[EQI_ACC_L] < 0) ? EQP_ACC_L : EQP_ACC_R;
  10221. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  10222. if (!sd->inventory.u.items_inventory[n].card[i])
  10223. continue;
  10224. std::shared_ptr<item_data> card_data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10225. if (card_data) {
  10226. int card_pos = card_data->equip;
  10227. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  10228. pos = card_pos; // Use the card's equip position
  10229. break;
  10230. }
  10231. }
  10232. }
  10233. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  10234. pos = (req_pos&EQP_ARMS);
  10235. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  10236. #ifdef RENEWAL
  10237. pos = (equip_index[EQI_HAND_R] >= 0 && equip_index[EQI_HAND_L] < 0) ? EQP_HAND_L : EQP_HAND_R;
  10238. #else
  10239. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  10240. #endif
  10241. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  10242. pos = req_pos&EQP_SHADOW_ACC;
  10243. if (pos == EQP_SHADOW_ACC)
  10244. pos = (equip_index[EQI_SHADOW_ACC_L] >= 0 && equip_index[EQI_SHADOW_ACC_R] < 0) ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  10245. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  10246. pos = (req_pos&EQP_SHADOW_ARMS);
  10247. if( pos == EQP_SHADOW_ARMS )
  10248. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  10249. }
  10250. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  10251. //Update skill-block range database when weapon range changes. [Skotlex]
  10252. i = equip_index[EQI_HAND_R];
  10253. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  10254. flag = 1;
  10255. else
  10256. flag = id->range != sd->inventory_data[i]->range;
  10257. }
  10258. if( equipswitch ){
  10259. for( i = 0; i < EQI_MAX; i++ ){
  10260. if( pos&equip_bitmask[i] ){
  10261. // If there was already an item assigned to this slot
  10262. if( sd->equip_switch_index[i] >= 0 ){
  10263. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  10264. }
  10265. // Assign the new index to it
  10266. sd->equip_switch_index[i] = n;
  10267. }
  10268. }
  10269. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  10270. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  10271. return true;
  10272. }else{
  10273. for(i=0;i<EQI_MAX;i++) {
  10274. if(pos & equip_bitmask[i]) {
  10275. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  10276. pc_unequipitem(sd,sd->equip_index[i], 1 | 2 | 4);
  10277. sd->equip_index[i] = n;
  10278. }
  10279. }
  10280. pc_equipswitch_remove(sd, n);
  10281. if(pos==EQP_AMMO) {
  10282. clif_arrowequip(sd,n);
  10283. clif_arrow_fail(sd,3);
  10284. }
  10285. else
  10286. clif_equipitemack( *sd, ITEM_EQUIP_ACK_OK, n, pos );
  10287. sd->inventory.u.items_inventory[n].equip = pos;
  10288. }
  10289. if(pos & EQP_HAND_R) {
  10290. if(id)
  10291. sd->weapontype1 = id->subtype;
  10292. else
  10293. sd->weapontype1 = W_FIST;
  10294. pc_calcweapontype(sd);
  10295. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10296. }
  10297. if(pos & EQP_HAND_L) {
  10298. if(id) {
  10299. if(id->type == IT_WEAPON) {
  10300. sd->status.shield = W_FIST;
  10301. sd->weapontype2 = id->subtype;
  10302. }
  10303. else
  10304. if(id->type == IT_ARMOR) {
  10305. sd->status.shield = id->look;
  10306. sd->weapontype2 = W_FIST;
  10307. }
  10308. }
  10309. else
  10310. sd->status.shield = sd->weapontype2 = W_FIST;
  10311. pc_calcweapontype(sd);
  10312. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10313. }
  10314. if(pos & EQP_SHOES)
  10315. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10316. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  10317. short idx = sd->equip_index[EQI_AMMO];
  10318. if (idx >= 0) {
  10319. switch (sd->inventory_data[idx]->subtype) {
  10320. case AMMO_ARROW:
  10321. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  10322. pc_unequipitem(sd, idx, 2 | 4);
  10323. break;
  10324. case AMMO_BULLET:
  10325. case AMMO_SHELL:
  10326. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  10327. #ifdef RENEWAL
  10328. && id->subtype != W_GRENADE
  10329. #endif
  10330. )
  10331. pc_unequipitem(sd, idx, 2 | 4);
  10332. break;
  10333. #ifndef RENEWAL
  10334. case AMMO_GRENADE:
  10335. if (id->subtype != W_GRENADE)
  10336. pc_unequipitem(sd, idx, 2 | 4);
  10337. break;
  10338. #endif
  10339. }
  10340. }
  10341. }
  10342. pc_set_costume_view(sd);
  10343. pc_checkallowskill(sd); //Check if status changes should be halted.
  10344. iflag = sd->npc_item_flag;
  10345. // Check for combos (MUST be before status_calc_pc)
  10346. if (!id->combos.empty())
  10347. pc_checkcombo(sd, id);
  10348. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10349. ; // No cards
  10350. else {
  10351. for (i = 0; i < MAX_SLOTS; i++) {
  10352. if (!sd->inventory.u.items_inventory[n].card[i])
  10353. continue;
  10354. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10355. if (data != nullptr) {
  10356. if (!data->combos.empty())
  10357. pc_checkcombo(sd, data.get());
  10358. }
  10359. }
  10360. }
  10361. status_calc_pc(sd,SCO_NONE);
  10362. if (flag) //Update skill data
  10363. clif_skillinfoblock(sd);
  10364. //OnEquip script [Skotlex]
  10365. if (id) {
  10366. //only run the script if item isn't restricted
  10367. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  10368. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10369. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10370. ; //No cards
  10371. else {
  10372. for( i = 0; i < MAX_SLOTS; i++ ) {
  10373. if (!sd->inventory.u.items_inventory[n].card[i])
  10374. continue;
  10375. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10376. if ( data != nullptr ) {
  10377. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data.get(), sd->bl.m)))
  10378. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10379. }
  10380. }
  10381. }
  10382. }
  10383. sd->npc_item_flag = iflag;
  10384. return true;
  10385. }
  10386. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int position ){
  10387. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  10388. while( it != bonus.end() ){
  10389. std::shared_ptr<s_autobonus> b = *it;
  10390. if( ( b->pos & position ) != b->pos ){
  10391. it++;
  10392. continue;
  10393. }
  10394. it = bonus.erase( it );
  10395. }
  10396. }
  10397. /**
  10398. * Recalculate player status on unequip
  10399. * @param sd: Player data
  10400. * @param n: Item inventory index
  10401. * @param flag: Whether to recalculate a player's status or not
  10402. * @return True on success or false on failure
  10403. */
  10404. static void pc_unequipitem_sub(map_session_data *sd, int n, int flag) {
  10405. int i, iflag;
  10406. bool status_calc = false;
  10407. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  10408. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  10409. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  10410. sd->inventory.u.items_inventory[n].equip = 0;
  10411. if (!(flag & 4))
  10412. pc_checkallowskill(sd);
  10413. iflag = sd->npc_item_flag;
  10414. // Check for combos (MUST be before status_calc_pc)
  10415. if (sd->inventory_data[n]) {
  10416. if (!sd->inventory_data[n]->combos.empty()) {
  10417. if (pc_removecombo(sd, sd->inventory_data[n]))
  10418. status_calc = true;
  10419. }
  10420. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10421. ; // No cards
  10422. else {
  10423. for (i = 0; i < MAX_SLOTS; i++) {
  10424. if (!sd->inventory.u.items_inventory[n].card[i])
  10425. continue;
  10426. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10427. if (data != nullptr) {
  10428. if (!data->combos.empty()) {
  10429. if (pc_removecombo(sd, data.get()))
  10430. status_calc = true;
  10431. }
  10432. }
  10433. }
  10434. }
  10435. }
  10436. if (flag & 1 || status_calc) {
  10437. pc_checkallowskill(sd);
  10438. status_calc_pc(sd, SCO_FORCE);
  10439. }
  10440. if (sd->sc.getSCE(SC_SIGNUMCRUCIS) && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  10441. status_change_end(&sd->bl, SC_SIGNUMCRUCIS);
  10442. //OnUnEquip script [Skotlex]
  10443. if (sd->inventory_data[n]) {
  10444. if (sd->inventory_data[n]->unequip_script)
  10445. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10446. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10447. ; //No cards
  10448. else {
  10449. for (i = 0; i < MAX_SLOTS; i++) {
  10450. if (!sd->inventory.u.items_inventory[n].card[i])
  10451. continue;
  10452. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10453. if (data != nullptr) {
  10454. if (data->unequip_script)
  10455. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10456. }
  10457. }
  10458. }
  10459. }
  10460. sd->npc_item_flag = iflag;
  10461. }
  10462. /**
  10463. * Called when attempting to unequip an item from a player
  10464. * @param sd: Player data
  10465. * @param n: Item inventory index
  10466. * @param flag: Type of unequip
  10467. * 0 - only unequip
  10468. * 1 - calculate status after unequipping
  10469. * 2 - force unequip
  10470. * 4 - unequip by switching equipment
  10471. * @return True on success or false on failure
  10472. */
  10473. bool pc_unequipitem(map_session_data *sd, int n, int flag) {
  10474. int i, pos;
  10475. nullpo_retr(false,sd);
  10476. if (n < 0 || n >= MAX_INVENTORY) {
  10477. clif_unequipitemack(sd,0,0,0);
  10478. return false;
  10479. }
  10480. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  10481. clif_unequipitemack(sd,n,0,0);
  10482. return false; //Nothing to unequip
  10483. }
  10484. // status change that makes player cannot unequip equipment
  10485. if (!(flag&2) && sd->sc.count &&( sd->sc.cant.unequip ||
  10486. (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  10487. {
  10488. clif_unequipitemack(sd,n,0,0);
  10489. return false;
  10490. }
  10491. if (battle_config.battle_log)
  10492. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  10493. for(i = 0; i < EQI_MAX; i++) {
  10494. if (pos & equip_bitmask[i])
  10495. sd->equip_index[i] = -1;
  10496. }
  10497. if(pos & EQP_HAND_R) {
  10498. sd->weapontype1 = W_FIST;
  10499. sd->status.weapon = sd->weapontype2;
  10500. pc_calcweapontype(sd);
  10501. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10502. if( !battle_config.dancing_weaponswitch_fix )
  10503. status_change_end(&sd->bl, SC_DANCING); // Unequipping => stop dancing.
  10504. #ifdef RENEWAL
  10505. if (battle_config.switch_remove_edp&2) {
  10506. #else
  10507. if (battle_config.switch_remove_edp&1) {
  10508. #endif
  10509. status_change_end(&sd->bl, SC_EDP);
  10510. }
  10511. }
  10512. if(pos & EQP_HAND_L) {
  10513. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  10514. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  10515. sd->status.shield = sd->weapontype2 = W_FIST;
  10516. pc_calcweapontype(sd);
  10517. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10518. }
  10519. if(pos & EQP_SHOES)
  10520. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10521. clif_unequipitemack(sd,n,pos,1);
  10522. pc_set_costume_view(sd);
  10523. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIP });
  10524. // On weapon change (right and left hand)
  10525. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  10526. if (battle_config.ammo_unequip && !(flag & 4)) {
  10527. switch (sd->inventory_data[n]->subtype) {
  10528. case W_BOW:
  10529. case W_MUSICAL:
  10530. case W_WHIP:
  10531. case W_REVOLVER:
  10532. case W_RIFLE:
  10533. case W_GATLING:
  10534. case W_SHOTGUN:
  10535. case W_GRENADE: {
  10536. short idx = sd->equip_index[EQI_AMMO];
  10537. if (idx >= 0) {
  10538. sd->equip_index[EQI_AMMO] = -1;
  10539. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  10540. pc_unequipitem_sub(sd, idx, 0);
  10541. }
  10542. }
  10543. break;
  10544. }
  10545. }
  10546. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPWEAPON });
  10547. }
  10548. // On armor change
  10549. if (pos & EQP_ARMOR) {
  10550. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPARMOR });
  10551. }
  10552. // On equipment change
  10553. #ifndef RENEWAL
  10554. if (!(flag&4))
  10555. status_change_end(&sd->bl, SC_CONCENTRATION);
  10556. #endif
  10557. // On ammo change
  10558. 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_))
  10559. status_change_end(&sd->bl, SC_P_ALTER);
  10560. pc_unequipitem_sub(sd, n, flag);
  10561. return true;
  10562. }
  10563. int pc_equipswitch( map_session_data* sd, int index ){
  10564. // Get the target equip mask
  10565. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  10566. // Get the currently equipped item
  10567. short equippedItem = pc_checkequip( sd, position, true );
  10568. // No item equipped at the target
  10569. if( equippedItem == -1 ){
  10570. // Remove it from the equip switch
  10571. pc_equipswitch_remove( sd, index );
  10572. pc_equipitem( sd, index, position );
  10573. return position;
  10574. }else{
  10575. std::map<int, int> unequipped;
  10576. int unequipped_position = 0;
  10577. // Unequip all items that interfere
  10578. for( int i = 0; i < EQI_MAX; i++ ){
  10579. int unequip_index = sd->equip_index[i];
  10580. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  10581. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  10582. // Store the unequipped index and position mask for later
  10583. unequipped[unequip_index] = unequip_item->equip;
  10584. // Keep the position for later
  10585. unequipped_position |= unequip_item->equip;
  10586. // Unequip the item
  10587. pc_unequipitem( sd, unequip_index, 0 );
  10588. }
  10589. }
  10590. int all_position = position | unequipped_position;
  10591. // Equip everything that is hit by the mask
  10592. for( int i = 0; i < EQI_MAX; i++ ){
  10593. int exchange_index = sd->equip_switch_index[i];
  10594. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  10595. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  10596. // Store the target position
  10597. int exchange_position = exchange_item->equipSwitch;
  10598. // Remove the item from equip switch
  10599. pc_equipswitch_remove( sd, exchange_index );
  10600. // Equip the item at the destinated position
  10601. pc_equipitem( sd, exchange_index, exchange_position );
  10602. }
  10603. }
  10604. // Place all unequipped items into the equip switch window
  10605. for( std::pair<int, int> pair : unequipped ){
  10606. int unequipped_index = pair.first;
  10607. int unequipped_position = pair.second;
  10608. // Rebuild the index cache
  10609. for( int i = 0; i < EQI_MAX; i++ ){
  10610. if( unequipped_position & equip_bitmask[i] ){
  10611. sd->equip_switch_index[i] = unequipped_index;
  10612. }
  10613. }
  10614. // Set the correct position mask
  10615. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  10616. // Notify the client
  10617. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  10618. }
  10619. return all_position;
  10620. }
  10621. }
  10622. void pc_equipswitch_remove( map_session_data* sd, int index ){
  10623. struct item* item = &sd->inventory.u.items_inventory[index];
  10624. if( !item->equipSwitch ){
  10625. return;
  10626. }
  10627. for( int i = 0; i < EQI_MAX; i++ ){
  10628. // If a match is found
  10629. if( sd->equip_switch_index[i] == index ){
  10630. // Remove it from the slot
  10631. sd->equip_switch_index[i] = -1;
  10632. }
  10633. }
  10634. // Send out one packet for all slots using the current item's mask
  10635. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  10636. item->equipSwitch = 0;
  10637. }
  10638. /*==========================================
  10639. * Checking if player (sd) has an invalid item
  10640. * and is unequiped on map load (item_noequip)
  10641. *------------------------------------------*/
  10642. void pc_checkitem(map_session_data *sd) {
  10643. int i, calc_flag = 0;
  10644. struct item* it;
  10645. nullpo_retv(sd);
  10646. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  10647. return;
  10648. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  10649. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10650. it = &sd->inventory.u.items_inventory[i];
  10651. if( it->nameid == 0 )
  10652. continue;
  10653. if( !it->equip )
  10654. continue;
  10655. if( it->equip&~pc_equippoint(sd,i) ) {
  10656. pc_unequipitem(sd, i, 2);
  10657. calc_flag = 1;
  10658. continue;
  10659. }
  10660. if( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) {
  10661. pc_unequipitem(sd, i, 2);
  10662. calc_flag = 1;
  10663. continue;
  10664. }
  10665. }
  10666. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10667. it = &sd->inventory.u.items_inventory[i];
  10668. if( it->nameid == 0 )
  10669. continue;
  10670. if( !it->equipSwitch )
  10671. continue;
  10672. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  10673. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  10674. for( int j = 0; j < EQI_MAX; j++ ){
  10675. if( sd->equip_switch_index[j] == i ){
  10676. sd->equip_switch_index[j] = -1;
  10677. }
  10678. }
  10679. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  10680. continue;
  10681. }
  10682. }
  10683. if( calc_flag && sd->state.active ) {
  10684. pc_checkallowskill(sd);
  10685. status_calc_pc(sd,SCO_NONE);
  10686. }
  10687. }
  10688. /*==========================================
  10689. * Checks for unavailable items and removes them.
  10690. * @param sd: Player data
  10691. * @param type Forced check:
  10692. * 1 - Inventory
  10693. * 2 - Cart
  10694. * 4 - Storage
  10695. *------------------------------------------*/
  10696. void pc_check_available_item(map_session_data *sd, uint8 type)
  10697. {
  10698. int i;
  10699. t_itemid nameid;
  10700. char output[256];
  10701. nullpo_retv(sd);
  10702. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  10703. for(i = 0; i < MAX_INVENTORY; i++) {
  10704. nameid = sd->inventory.u.items_inventory[i].nameid;
  10705. if (!nameid)
  10706. continue;
  10707. if (!itemdb_available(nameid)) {
  10708. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  10709. clif_displaymessage(sd->fd, output);
  10710. 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);
  10711. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  10712. continue;
  10713. }
  10714. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  10715. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  10716. }
  10717. }
  10718. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  10719. for(i = 0; i < MAX_CART; i++) {
  10720. nameid = sd->cart.u.items_cart[i].nameid;
  10721. if (!nameid)
  10722. continue;
  10723. if (!itemdb_available(nameid)) {
  10724. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  10725. clif_displaymessage(sd->fd, output);
  10726. 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);
  10727. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  10728. continue;
  10729. }
  10730. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  10731. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  10732. }
  10733. }
  10734. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  10735. for(i = 0; i < sd->storage.max_amount; i++) {
  10736. nameid = sd->storage.u.items_storage[i].nameid;
  10737. if (!nameid)
  10738. continue;
  10739. if (!itemdb_available(nameid)) {
  10740. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  10741. clif_displaymessage(sd->fd, output);
  10742. 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);
  10743. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  10744. continue;
  10745. }
  10746. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  10747. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  10748. }
  10749. }
  10750. }
  10751. /*==========================================
  10752. * Update PVP rank for sd1 in cmp to sd2
  10753. *------------------------------------------*/
  10754. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  10755. {
  10756. map_session_data *sd1,*sd2;
  10757. sd1=(map_session_data *)bl;
  10758. sd2=va_arg(ap,map_session_data *);
  10759. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  10760. {// cannot register pvp rank for hidden GMs
  10761. return 0;
  10762. }
  10763. if( sd1->pvp_point > sd2->pvp_point )
  10764. sd2->pvp_rank++;
  10765. return 0;
  10766. }
  10767. /*==========================================
  10768. * Calculate new rank beetween all present players (map_foreachinallarea)
  10769. * and display result
  10770. *------------------------------------------*/
  10771. int pc_calc_pvprank(map_session_data *sd)
  10772. {
  10773. int old = sd->pvp_rank;
  10774. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10775. sd->pvp_rank=1;
  10776. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  10777. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  10778. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  10779. return sd->pvp_rank;
  10780. }
  10781. /*==========================================
  10782. * Calculate next sd ranking calculation from config
  10783. *------------------------------------------*/
  10784. TIMER_FUNC(pc_calc_pvprank_timer){
  10785. map_session_data *sd;
  10786. sd=map_id2sd(id);
  10787. if(sd==NULL)
  10788. return 0;
  10789. sd->pvp_timer = INVALID_TIMER;
  10790. if( pc_isinvisible(sd) )
  10791. {// do not calculate the pvp rank for a hidden GM
  10792. return 0;
  10793. }
  10794. if( pc_calc_pvprank(sd) > 0 )
  10795. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  10796. return 0;
  10797. }
  10798. /*==========================================
  10799. * Checking if sd is married
  10800. * Return:
  10801. * partner_id = yes
  10802. * 0 = no
  10803. *------------------------------------------*/
  10804. int pc_ismarried(map_session_data *sd)
  10805. {
  10806. if(sd == NULL)
  10807. return -1;
  10808. if(sd->status.partner_id > 0)
  10809. return sd->status.partner_id;
  10810. else
  10811. return 0;
  10812. }
  10813. /*==========================================
  10814. * Marry player sd to player dstsd
  10815. * Return:
  10816. * false = fail
  10817. * true = success
  10818. *------------------------------------------*/
  10819. bool pc_marriage(map_session_data *sd,map_session_data *dstsd)
  10820. {
  10821. if(sd == NULL || dstsd == NULL ||
  10822. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  10823. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  10824. return false;
  10825. sd->status.partner_id = dstsd->status.char_id;
  10826. dstsd->status.partner_id = sd->status.char_id;
  10827. achievement_update_objective(sd, AG_MARRY, 1, 1);
  10828. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  10829. return true;
  10830. }
  10831. /*==========================================
  10832. * Divorce sd from its partner
  10833. * Return:
  10834. * false = fail
  10835. * true = success
  10836. *------------------------------------------*/
  10837. bool pc_divorce(map_session_data *sd)
  10838. {
  10839. map_session_data *p_sd;
  10840. int i;
  10841. if( sd == NULL || !pc_ismarried(sd) )
  10842. return false;
  10843. if( !sd->status.partner_id )
  10844. return false; // Char is not married
  10845. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  10846. { // Lets char server do the divorce
  10847. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  10848. return false; // No char server connected
  10849. return true;
  10850. }
  10851. // Both players online, lets do the divorce manually
  10852. sd->status.partner_id = 0;
  10853. p_sd->status.partner_id = 0;
  10854. for( i = 0; i < MAX_INVENTORY; i++ )
  10855. {
  10856. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10857. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10858. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10859. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10860. }
  10861. clif_divorced(sd, p_sd->status.name);
  10862. clif_divorced(p_sd, sd->status.name);
  10863. return true;
  10864. }
  10865. /**
  10866. * Get the partner map_session_data of a player
  10867. * @param sd : the husband|wife session
  10868. * @return partner session or NULL
  10869. */
  10870. map_session_data *pc_get_partner(map_session_data *sd){
  10871. if (!sd || !pc_ismarried(sd))
  10872. return NULL;
  10873. return map_charid2sd(sd->status.partner_id);
  10874. }
  10875. /**
  10876. * Get the father map_session_data of a player
  10877. * @param sd : the baby session
  10878. * @return father session or NULL
  10879. */
  10880. map_session_data *pc_get_father (map_session_data *sd){
  10881. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  10882. return NULL;
  10883. return map_charid2sd(sd->status.father);
  10884. }
  10885. /**
  10886. * Get the mother map_session_data of a player
  10887. * @param sd : the baby session
  10888. * @return mother session or NULL
  10889. */
  10890. map_session_data *pc_get_mother (map_session_data *sd){
  10891. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  10892. return NULL;
  10893. return map_charid2sd(sd->status.mother);
  10894. }
  10895. /*==========================================
  10896. * Get sd children charid. (Need to be married)
  10897. *------------------------------------------*/
  10898. map_session_data *pc_get_child (map_session_data *sd)
  10899. {
  10900. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  10901. // charid2sd returns NULL if not found
  10902. return NULL;
  10903. return map_charid2sd(sd->status.child);
  10904. }
  10905. /*==========================================
  10906. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  10907. *------------------------------------------*/
  10908. void pc_bleeding (map_session_data *sd, t_tick diff_tick)
  10909. {
  10910. int hp = 0, sp = 0;
  10911. if( pc_isdead(sd) )
  10912. return;
  10913. if (sd->hp_loss.value) {
  10914. sd->hp_loss.tick += diff_tick;
  10915. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  10916. hp += sd->hp_loss.value;
  10917. sd->hp_loss.tick -= sd->hp_loss.rate;
  10918. }
  10919. if(hp >= sd->battle_status.hp)
  10920. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  10921. }
  10922. if (sd->sp_loss.value) {
  10923. sd->sp_loss.tick += diff_tick;
  10924. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  10925. sp += sd->sp_loss.value;
  10926. sd->sp_loss.tick -= sd->sp_loss.rate;
  10927. }
  10928. }
  10929. if (hp > 0 || sp > 0)
  10930. status_zap(&sd->bl, hp, sp);
  10931. }
  10932. //Character regen. Flag is used to know which types of regen can take place.
  10933. //&1: HP regen
  10934. //&2: SP regen
  10935. void pc_regen (map_session_data *sd, t_tick diff_tick)
  10936. {
  10937. int hp = 0, sp = 0;
  10938. if (sd->hp_regen.value) {
  10939. sd->hp_regen.tick += diff_tick;
  10940. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  10941. hp += sd->hp_regen.value;
  10942. sd->hp_regen.tick -= sd->hp_regen.rate;
  10943. }
  10944. }
  10945. if (sd->sp_regen.value) {
  10946. sd->sp_regen.tick += diff_tick;
  10947. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  10948. sp += sd->sp_regen.value;
  10949. sd->sp_regen.tick -= sd->sp_regen.rate;
  10950. }
  10951. }
  10952. if (sd->percent_hp_regen.value) {
  10953. sd->percent_hp_regen.tick += diff_tick;
  10954. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  10955. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  10956. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  10957. }
  10958. }
  10959. if (sd->percent_sp_regen.value) {
  10960. sd->percent_sp_regen.tick += diff_tick;
  10961. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  10962. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  10963. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  10964. }
  10965. }
  10966. if (hp > 0 || sp > 0)
  10967. status_heal(&sd->bl, hp, sp, 0);
  10968. }
  10969. /*==========================================
  10970. * Memo player sd savepoint. (map,x,y)
  10971. *------------------------------------------*/
  10972. void pc_setsavepoint(map_session_data *sd, short mapindex,int x,int y)
  10973. {
  10974. nullpo_retv(sd);
  10975. safestrncpy( sd->status.save_point.map, mapindex_id2name( mapindex ), sizeof( sd->status.save_point.map ) );
  10976. sd->status.save_point.x = x;
  10977. sd->status.save_point.y = y;
  10978. }
  10979. /*==========================================
  10980. * Save 1 player data at autosave interval
  10981. *------------------------------------------*/
  10982. static TIMER_FUNC(pc_autosave){
  10983. int interval;
  10984. struct s_mapiterator* iter;
  10985. map_session_data* sd;
  10986. static int last_save_id = 0, save_flag = 0;
  10987. if(save_flag == 2) //Someone was saved on last call, normal cycle
  10988. save_flag = 0;
  10989. else
  10990. save_flag = 1; //Noone was saved, so save first found char.
  10991. iter = mapit_getallusers();
  10992. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10993. {
  10994. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  10995. continue;
  10996. if(sd->bl.id == last_save_id && save_flag != 1) {
  10997. save_flag = 1;
  10998. continue;
  10999. }
  11000. if(save_flag != 1) //Not our turn to save yet.
  11001. continue;
  11002. //Save char.
  11003. last_save_id = sd->bl.id;
  11004. save_flag = 2;
  11005. if (pc_isvip(sd)) // Check if we're still VIP
  11006. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  11007. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  11008. break;
  11009. }
  11010. mapit_free(iter);
  11011. interval = autosave_interval/(map_usercount()+1);
  11012. if(interval < minsave_interval)
  11013. interval = minsave_interval;
  11014. add_timer(gettick()+interval,pc_autosave,0,0);
  11015. return 0;
  11016. }
  11017. static int pc_daynight_timer_sub(map_session_data *sd,va_list ap)
  11018. {
  11019. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  11020. { //Night/day state does not match.
  11021. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  11022. sd->state.night = night_flag;
  11023. return 1;
  11024. }
  11025. return 0;
  11026. }
  11027. /*================================================
  11028. * timer to do the day [Yor]
  11029. * data: 0 = called by timer, 1 = gmcommand/script
  11030. *------------------------------------------------*/
  11031. TIMER_FUNC(map_day_timer){
  11032. char tmp_soutput[1024];
  11033. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  11034. return 0;
  11035. if (!night_flag)
  11036. return 0; //Already day.
  11037. night_flag = 0; // 0=day, 1=night [Yor]
  11038. map_foreachpc(pc_daynight_timer_sub);
  11039. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  11040. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11041. return 0;
  11042. }
  11043. /*================================================
  11044. * timer to do the night [Yor]
  11045. * data: 0 = called by timer, 1 = gmcommand/script
  11046. *------------------------------------------------*/
  11047. TIMER_FUNC(map_night_timer){
  11048. char tmp_soutput[1024];
  11049. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  11050. return 0;
  11051. if (night_flag)
  11052. return 0; //Already nigth.
  11053. night_flag = 1; // 0=day, 1=night [Yor]
  11054. map_foreachpc(pc_daynight_timer_sub);
  11055. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  11056. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11057. return 0;
  11058. }
  11059. /**
  11060. * Attempt to stand up a player
  11061. * @param sd
  11062. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  11063. * @return True if success, Fals if failed
  11064. */
  11065. bool pc_setstand(map_session_data *sd, bool force){
  11066. nullpo_ret(sd);
  11067. // Cannot stand yet
  11068. // TODO: Move to SCS_NOSTAND [Cydh]
  11069. if (!force && (sd->sc.getSCE(SC_SITDOWN_FORCE) || sd->sc.getSCE(SC_BANANA_BOMB_SITDOWN)))
  11070. return false;
  11071. status_change_end(&sd->bl, SC_TENSIONRELAX);
  11072. clif_status_load(&sd->bl,EFST_SIT,0);
  11073. clif_standing(&sd->bl); //Inform area PC is standing
  11074. //Reset sitting tick.
  11075. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  11076. if( pc_isdead( sd ) ){
  11077. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11078. clif_party_dead( *sd );
  11079. }else{
  11080. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11081. }
  11082. return true;
  11083. }
  11084. /**
  11085. * Calculate Overheat value
  11086. * @param sd: Player data
  11087. * @param heat: Amount of Heat to adjust
  11088. **/
  11089. void pc_overheat(map_session_data &sd, int16 heat) {
  11090. status_change_entry *sce = sd.sc.getSCE(SC_OVERHEAT_LIMITPOINT);
  11091. if (sce) {
  11092. sce->val1 += heat;
  11093. sce->val1 = cap_value(sce->val1, 0, 1000);
  11094. if (heat < 0 && sce->val1 == 0) { // Cooling device used.
  11095. status_change_end(&sd.bl, SC_OVERHEAT_LIMITPOINT);
  11096. status_change_end(&sd.bl, SC_OVERHEAT);
  11097. }
  11098. } else if (heat > 0)
  11099. sc_start(&sd.bl, &sd.bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  11100. }
  11101. /**
  11102. * Check if player is autolooting given itemID.
  11103. */
  11104. bool pc_isautolooting(map_session_data *sd, t_itemid nameid)
  11105. {
  11106. uint8 i = 0;
  11107. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  11108. return true;
  11109. if (!sd->state.autolooting)
  11110. return false;
  11111. if (sd->state.autolooting)
  11112. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  11113. return (i != AUTOLOOTITEM_SIZE);
  11114. }
  11115. /**
  11116. * Checks if player can use @/#command
  11117. * @param sd Player map session data
  11118. * @param command Command name without @/# and params
  11119. * @param type is it atcommand or charcommand
  11120. */
  11121. bool pc_can_use_command( map_session_data *sd, const char *command, AtCommandType type ){
  11122. return sd->group->can_use_command( command, type );
  11123. }
  11124. bool pc_has_permission( map_session_data* sd, e_pc_permission permission ){
  11125. return sd->permissions.test( permission );
  11126. }
  11127. /**
  11128. * Checks if commands used by a player should be logged
  11129. * according to their group setting.
  11130. * @param sd Player map session data
  11131. */
  11132. bool pc_should_log_commands( map_session_data *sd ){
  11133. return sd->group->log_commands;
  11134. }
  11135. /**
  11136. * Spirit Charm expiration timer.
  11137. * @see TimerFunc
  11138. */
  11139. static TIMER_FUNC(pc_spiritcharm_timer){
  11140. map_session_data *sd;
  11141. int i;
  11142. if ((sd = (map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  11143. return 1;
  11144. if (sd->spiritcharm <= 0) {
  11145. 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);
  11146. sd->spiritcharm = 0;
  11147. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11148. return 0;
  11149. }
  11150. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  11151. if (i == sd->spiritcharm) {
  11152. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  11153. return 0;
  11154. }
  11155. sd->spiritcharm--;
  11156. if (i != sd->spiritcharm)
  11157. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  11158. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11159. if (sd->spiritcharm <= 0)
  11160. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11161. clif_spiritcharm(sd);
  11162. return 0;
  11163. }
  11164. /**
  11165. * Adds a spirit charm.
  11166. * @param sd: Target character
  11167. * @param interval: Duration
  11168. * @param max: Maximum amount of charms to add
  11169. * @param type: Charm type (@see spirit_charm_types)
  11170. */
  11171. void pc_addspiritcharm(map_session_data *sd, int interval, int max, int type)
  11172. {
  11173. int tid, i;
  11174. nullpo_retv(sd);
  11175. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  11176. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  11177. if (max > MAX_SPIRITCHARM)
  11178. max = MAX_SPIRITCHARM;
  11179. if (sd->spiritcharm < 0)
  11180. sd->spiritcharm = 0;
  11181. if (sd->spiritcharm && sd->spiritcharm >= max) {
  11182. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  11183. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  11184. sd->spiritcharm--;
  11185. if (sd->spiritcharm != 0)
  11186. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  11187. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11188. }
  11189. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  11190. 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);
  11191. if (i != sd->spiritcharm)
  11192. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  11193. sd->spiritcharm_timer[i] = tid;
  11194. sd->spiritcharm++;
  11195. sd->spiritcharm_type = type;
  11196. clif_spiritcharm(sd);
  11197. }
  11198. /**
  11199. * Removes one or more spirit charms.
  11200. * @param sd: The target character
  11201. * @param count: Amount of charms to remove
  11202. * @param type: Type of charm to remove
  11203. */
  11204. void pc_delspiritcharm(map_session_data *sd, int count, int type)
  11205. {
  11206. int i;
  11207. nullpo_retv(sd);
  11208. if (sd->spiritcharm_type != type)
  11209. return;
  11210. if (sd->spiritcharm <= 0) {
  11211. sd->spiritcharm = 0;
  11212. return;
  11213. }
  11214. if (count <= 0)
  11215. return;
  11216. if (count > sd->spiritcharm)
  11217. count = sd->spiritcharm;
  11218. sd->spiritcharm -= count;
  11219. if (count > MAX_SPIRITCHARM)
  11220. count = MAX_SPIRITCHARM;
  11221. for (i = 0; i < count; i++) {
  11222. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  11223. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  11224. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11225. }
  11226. }
  11227. for (i = count; i < MAX_SPIRITCHARM; i++) {
  11228. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  11229. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11230. }
  11231. if (sd->spiritcharm <= 0)
  11232. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11233. clif_spiritcharm(sd);
  11234. }
  11235. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  11236. /**
  11237. * Renewal EXP/Item Drop rate modifier based on level penalty
  11238. * @param level_diff: Monster and Player level difference
  11239. * @param mob_class: Monster class
  11240. * @param mode: Monster mode
  11241. * @param type: 1 - EXP, 2 - Item Drop
  11242. * @return Penalty rate
  11243. */
  11244. uint16 pc_level_penalty_mod( map_session_data* sd, e_penalty_type type, std::shared_ptr<s_mob_db> mob, mob_data* md ){
  11245. // No player was attached, we don't use any modifier (100 = rates are not touched)
  11246. if( sd == nullptr ){
  11247. return 100;
  11248. }
  11249. if ((type == PENALTY_DROP && map_getmapflag(sd->bl.m, MF_NORENEWALDROPPENALTY)) || (type == PENALTY_EXP && map_getmapflag(sd->bl.m, MF_NORENEWALEXPPENALTY))) {
  11250. return 100;
  11251. }
  11252. int monster_level;
  11253. if( md != nullptr ){
  11254. monster_level = md->level;
  11255. mob = md->db;
  11256. }else if( mob != nullptr ){
  11257. monster_level = mob->lv;
  11258. }else{
  11259. return 100;
  11260. }
  11261. if( ( type == PENALTY_DROP || type == PENALTY_MVP_DROP ) && status_has_mode( &mob->status, MD_FIXEDITEMDROP ) ){
  11262. return 100;
  11263. }
  11264. int level_difference = monster_level - sd->status.base_level;
  11265. std::shared_ptr<s_penalty> penalty = penalty_db.find( type );
  11266. if( penalty != nullptr ){
  11267. return penalty->rate[ level_difference + MAX_LEVEL - 1 ];
  11268. }else{
  11269. return 100;
  11270. }
  11271. }
  11272. #endif
  11273. int pc_split_str(char *str,char **val,int num)
  11274. {
  11275. int i;
  11276. for (i=0; i<num && str; i++){
  11277. val[i] = str;
  11278. str = strchr(str,',');
  11279. if (str && i<num-1) //Do not remove a trailing comma.
  11280. *str++=0;
  11281. }
  11282. return i;
  11283. }
  11284. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  11285. {
  11286. static int warning=0;
  11287. int i,j;
  11288. for (i=0; i<max; i++) {
  11289. double f;
  11290. if (!str) break;
  11291. f = atof(str);
  11292. if (f < 0)
  11293. val[i] = 0;
  11294. else if (f > UINT_MAX) {
  11295. val[i] = UINT_MAX;
  11296. if (!warning) {
  11297. warning = 1;
  11298. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  11299. }
  11300. } else
  11301. val[i] = (unsigned int)f;
  11302. str = strchr(str,sep);
  11303. if (str)
  11304. *str++=0;
  11305. }
  11306. //Zero up the remaining.
  11307. for(j=i; j < max; j++)
  11308. val[j] = 0;
  11309. return i;
  11310. }
  11311. std::shared_ptr<s_skill_tree_entry> SkillTreeDatabase::get_skill_data(int class_, uint16 skill_id) {
  11312. std::shared_ptr<s_skill_tree> tree = this->find(class_);
  11313. if (tree != nullptr)
  11314. return util::umap_find(tree->skills, skill_id);
  11315. return nullptr;
  11316. }
  11317. const std::string SkillTreeDatabase::getDefaultLocation() {
  11318. return std::string(db_path) + "/skill_tree.yml";
  11319. }
  11320. /**
  11321. * Reads and parses an entry from the skill_tree.
  11322. * @param node: YAML node containing the entry.
  11323. * @return count of successfully parsed rows
  11324. */
  11325. uint64 SkillTreeDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11326. std::string job_name;
  11327. if (!this->asString(node, "Job", job_name))
  11328. return 0;
  11329. int64 constant;
  11330. std::string job_name_constant = "JOB_" + job_name;
  11331. if (!script_get_constant(job_name_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11332. this->invalidWarning(node["Job"], "Invalid job %s.\n", job_name.c_str());
  11333. return 0;
  11334. }
  11335. uint16 job_id = static_cast<uint16>(constant);
  11336. std::shared_ptr<s_skill_tree> tree = this->find(job_id);
  11337. bool exists = tree != nullptr;
  11338. if (!exists)
  11339. tree = std::make_shared<s_skill_tree>();
  11340. if (this->nodeExists(node, "Inherit")) {
  11341. const ryml::NodeRef& InheritNode = node["Inherit"];
  11342. for (const auto &Inheritit : InheritNode) {
  11343. std::string inheritname;
  11344. c4::from_chars(Inheritit.key(), &inheritname);
  11345. std::string inheritname_constant = "JOB_" + inheritname;
  11346. if (!script_get_constant(inheritname_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11347. this->invalidWarning(InheritNode[Inheritit.key()], "Invalid job %s.\n", inheritname.c_str());
  11348. return 0;
  11349. }
  11350. bool active;
  11351. if (!this->asBool(InheritNode, inheritname, active))
  11352. return 0;
  11353. uint16 inherit_job = static_cast<uint16>(constant);
  11354. if (!active) {
  11355. if (exists)
  11356. util::vector_erase_if_exists(tree->inherit_job, inherit_job);
  11357. }
  11358. else {
  11359. if (!util::vector_exists(tree->inherit_job, inherit_job))
  11360. tree->inherit_job.push_back(inherit_job);
  11361. }
  11362. }
  11363. }
  11364. if (this->nodeExists(node, "Tree")) {
  11365. for (const auto &it : node["Tree"]) {
  11366. std::string skill_name;
  11367. if (!this->asString(it, "Name", skill_name))
  11368. return 0;
  11369. uint16 skill_id = skill_name2id(skill_name.c_str());
  11370. if (skill_id == 0) {
  11371. this->invalidWarning(it["Name"], "Invalid skill name \"%s\".\n", skill_name.c_str());
  11372. return 0;
  11373. }
  11374. if (!skill_get_index(skill_id)) {
  11375. this->invalidWarning(it["Name"], "Unable to load skill %s into job %hu's tree.\n", skill_name.c_str(), job_id);
  11376. return 0;
  11377. }
  11378. std::shared_ptr<s_skill_tree_entry> entry;
  11379. bool skill_exists = tree->skills.count(skill_id) > 0;
  11380. if (skill_exists)
  11381. entry = tree->skills[skill_id];
  11382. else
  11383. entry = std::make_shared<s_skill_tree_entry>();
  11384. entry->skill_id = skill_id;
  11385. uint16 max_lv;
  11386. if (!this->asUInt16(it, "MaxLevel", max_lv))
  11387. return 0;
  11388. if (max_lv > MAX_SKILL_LEVEL) {
  11389. this->invalidWarning(it["MaxLevel"], "MaxLevel exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11390. return 0;
  11391. }
  11392. uint16 skill_lv_max = skill_get_max(skill_id);
  11393. if (max_lv > skill_lv_max) {
  11394. 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);
  11395. max_lv = skill_lv_max;
  11396. }
  11397. // if (max_lv == 0) { // skill lvl 0 removed on loadingFinished (because of inherit)
  11398. // if (!skill_exists || entry->skill_id.erase(skill_id) == 0)
  11399. // 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());
  11400. // continue;
  11401. // }
  11402. entry->max_lv = max_lv;
  11403. if (this->nodeExists(it, "BaseLevel")) {
  11404. uint32 baselv;
  11405. if (!this->asUInt32(it, "BaseLevel", baselv))
  11406. return 0;
  11407. uint32 baselv_max = job_db.get_maxBaseLv(job_id);
  11408. if (baselv > baselv_max) {
  11409. this->invalidWarning(it["BaseLevel"], "Skill %hu's base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11410. skill_id, baselv, job_name.c_str(), baselv_max);
  11411. baselv = baselv_max;
  11412. }
  11413. entry->baselv = baselv;
  11414. } else {
  11415. if (!skill_exists)
  11416. entry->baselv = 0;
  11417. }
  11418. if (this->nodeExists(it, "JobLevel")) {
  11419. uint32 joblv;
  11420. if (!this->asUInt32(it, "JobLevel", joblv))
  11421. return 0;
  11422. uint32 joblv_max = job_db.get_maxJobLv(job_id);
  11423. if (joblv > joblv_max) {
  11424. this->invalidWarning(it["JobLevel"], "Skill %hu's job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11425. skill_id, joblv, job_name.c_str(), joblv_max);
  11426. joblv = joblv_max;
  11427. }
  11428. entry->joblv = joblv;
  11429. } else {
  11430. if (!skill_exists)
  11431. entry->joblv = 0;
  11432. }
  11433. if (this->nodeExists(it, "Requires")) {
  11434. for (const auto &Requiresit : it["Requires"]) {
  11435. if (!this->nodesExist(Requiresit, { "Name" }))
  11436. return 0;
  11437. std::string skill_name_req;
  11438. if (!this->asString(Requiresit, "Name", skill_name_req))
  11439. return 0;
  11440. uint16 skill_id_req = skill_name2id(skill_name_req.c_str());
  11441. if (skill_id_req == 0) {
  11442. this->invalidWarning(Requiresit["Name"], "Invalid skill name \"%s\".\n", skill_name_req.c_str());
  11443. return 0;
  11444. }
  11445. uint16 lv_req;
  11446. if (!this->asUInt16(Requiresit, "Level", lv_req))
  11447. return 0;
  11448. if (lv_req > MAX_SKILL_LEVEL) {
  11449. this->invalidWarning(Requiresit["Level"], "Level exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11450. return 0;
  11451. }
  11452. uint16 lv_req_max = skill_get_max(skill_id_req);
  11453. if (lv_req > lv_req_max) {
  11454. 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);
  11455. lv_req = lv_req_max;
  11456. }
  11457. if (lv_req == 0) {
  11458. if (entry->need.erase(skill_id_req) == 0)
  11459. 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());
  11460. continue;
  11461. }
  11462. entry->need[skill_id_req] = lv_req;
  11463. }
  11464. }
  11465. if (this->nodeExists(it, "Exclude")) {
  11466. bool exclude;
  11467. if (!this->asBool(it, "Exclude", exclude))
  11468. return 0;
  11469. entry->exclude_inherit = exclude;
  11470. } else {
  11471. if (!skill_exists)
  11472. entry->exclude_inherit = false;
  11473. }
  11474. if (!skill_exists)
  11475. tree->skills.insert({ skill_id, entry });
  11476. }
  11477. }
  11478. if (!exists)
  11479. this->put(job_id, tree);
  11480. return true;
  11481. }
  11482. void SkillTreeDatabase::loadingFinished() {
  11483. std::unordered_map<uint16, std::shared_ptr<s_skill_tree>> job_tree; // get the data from skill_tree_db before populate it
  11484. for (auto &data : *this) {
  11485. if (data.second->inherit_job.empty())
  11486. continue;
  11487. std::shared_ptr<s_skill_tree> skill_tree = std::make_shared<s_skill_tree>();
  11488. uint32 baselv_max = job_db.get_maxBaseLv(data.first);
  11489. uint32 joblv_max = job_db.get_maxJobLv(data.first);
  11490. for (const auto &inherit_job : data.second->inherit_job) {
  11491. std::shared_ptr<s_skill_tree> tree = this->find(inherit_job);
  11492. if (tree == nullptr || tree->skills.empty())
  11493. continue;
  11494. for (const auto &it : tree->skills) {
  11495. if (it.second->exclude_inherit)
  11496. continue;
  11497. if (data.second->skills.count(it.first) > 0) // skill already in the skill tree
  11498. continue;
  11499. if (skill_tree->skills.count(it.first) > 0) // replaced by the last inheritance
  11500. skill_tree->skills[it.first] = it.second;
  11501. else
  11502. skill_tree->skills.insert({ it.first, it.second });
  11503. std::shared_ptr<s_skill_tree_entry> skill = skill_tree->skills[it.first];
  11504. if (skill->baselv > baselv_max) {
  11505. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11506. skill_get_name(skill->skill_id), skill->skill_id, skill->baselv, job_name(data.first), baselv_max);
  11507. skill->baselv = baselv_max;
  11508. }
  11509. if (skill->joblv > joblv_max) {
  11510. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11511. skill_get_name(skill->skill_id), skill->skill_id, skill->joblv, job_name(data.first), joblv_max);
  11512. skill->joblv = joblv_max;
  11513. }
  11514. }
  11515. }
  11516. if (skill_tree != nullptr && !skill_tree->skills.empty())
  11517. job_tree.insert({ data.first, skill_tree });
  11518. }
  11519. if (!job_tree.empty()) {
  11520. for (auto &data : *this) {
  11521. if (job_tree.count(data.first) == 0)
  11522. continue;
  11523. data.second->skills.insert(job_tree[data.first]->skills.begin(), job_tree[data.first]->skills.end());
  11524. }
  11525. }
  11526. // remove skills with max_lv = 0
  11527. for (const auto &job : *this) {
  11528. if (job.second->skills.empty())
  11529. continue;
  11530. auto it = job.second->skills.begin();
  11531. while( it != job.second->skills.end() ){
  11532. if( it->second->max_lv == 0 ){
  11533. it = job.second->skills.erase( it );
  11534. }else{
  11535. it++;
  11536. }
  11537. }
  11538. }
  11539. TypesafeYamlDatabase::loadingFinished();
  11540. }
  11541. /**
  11542. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  11543. * @param level: Base level of player
  11544. * @param job_id: Job ID @see enum e_job
  11545. * @return base_hp
  11546. * @author [Cydh]
  11547. */
  11548. static unsigned int pc_calc_basehp(uint16 level, uint16 job_id) {
  11549. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11550. double base_hp = 35 + level * (job->hp_increase / 100.);
  11551. #ifndef RENEWAL
  11552. if (level >= 10 && (job_id == JOB_NINJA || job_id == JOB_GUNSLINGER))
  11553. base_hp += 90;
  11554. #endif
  11555. for (uint16 i = 2; i <= level; i++)
  11556. base_hp += floor(((job->hp_factor / 100.) * i) + 0.5); //Don't have round()
  11557. if (job_id == JOB_SUMMONER || job_id == JOB_SPIRIT_HANDLER)
  11558. base_hp += floor((base_hp / 2) + 0.5);
  11559. return (unsigned int)base_hp;
  11560. }
  11561. /**
  11562. * Calculates base sp of player.
  11563. * @param level: Base level of player
  11564. * @param job_id: Job ID @see enum e_job
  11565. * @return base_sp
  11566. * @author [Playtester]
  11567. */
  11568. static unsigned int pc_calc_basesp(uint16 level, uint16 job_id) {
  11569. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11570. double base_sp = 10 + floor(level * (job->sp_increase / 100.));
  11571. switch (job_id) {
  11572. case JOB_NINJA:
  11573. if (level >= 10)
  11574. base_sp -= 22;
  11575. else
  11576. base_sp = 11 + 3*level;
  11577. break;
  11578. case JOB_GUNSLINGER:
  11579. if (level > 10)
  11580. base_sp -= 18;
  11581. else
  11582. base_sp = 9 + 3*level;
  11583. break;
  11584. case JOB_SUMMONER:
  11585. case JOB_SPIRIT_HANDLER:
  11586. base_sp -= floor(base_sp / 2);
  11587. break;
  11588. }
  11589. return (unsigned int)base_sp;
  11590. }
  11591. const std::string JobDatabase::getDefaultLocation() {
  11592. return std::string(db_path) + "/job_stats.yml";
  11593. }
  11594. /**
  11595. * Reads and parses an entry from the job_db.
  11596. * @param node: YAML node containing the entry.
  11597. * @return count of successfully parsed rows
  11598. */
  11599. uint64 JobDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11600. if (this->nodeExists(node, "Jobs")) {
  11601. const ryml::NodeRef& jobsNode = node["Jobs"];
  11602. for (const auto &jobit : jobsNode) {
  11603. std::string job_name;
  11604. c4::from_chars(jobit.key(), &job_name);
  11605. std::string job_name_constant = "JOB_" + job_name;
  11606. int64 job_id;
  11607. if (!script_get_constant(job_name_constant.c_str(), &job_id)) {
  11608. this->invalidWarning(node["Job"], "Job %s does not exist.\n", job_name.c_str());
  11609. return 0;
  11610. }
  11611. std::shared_ptr<s_job_info> job = job_db.find(static_cast<uint16>(job_id));
  11612. bool exists = job != nullptr;
  11613. if (!exists) {
  11614. job = std::make_shared<s_job_info>();
  11615. job->job_bonus.resize(MAX_LEVEL);
  11616. std::fill(job->job_bonus.begin(), job->job_bonus.end(), std::array<uint16, PARAM_MAX> { 0 });
  11617. job->base_hp.resize(MAX_LEVEL);
  11618. std::fill(job->base_hp.begin(), job->base_hp.end(), 0);
  11619. job->base_sp.resize(MAX_LEVEL);
  11620. std::fill(job->base_sp.begin(), job->base_sp.end(), 0);
  11621. job->base_ap.resize(MAX_LEVEL);
  11622. std::fill(job->base_ap.begin(), job->base_ap.end(), 0);
  11623. }
  11624. if (this->nodeExists(node, "MaxWeight")) {
  11625. uint32 weight;
  11626. if (!this->asUInt32(node, "MaxWeight", weight))
  11627. return 0;
  11628. job->max_weight_base = weight;
  11629. } else {
  11630. if (!exists)
  11631. job->max_weight_base = 20000;
  11632. }
  11633. if (this->nodeExists(node, "HpFactor")) {
  11634. uint32 hp;
  11635. if (!this->asUInt32(node, "HpFactor", hp))
  11636. return 0;
  11637. job->hp_factor = hp;
  11638. } else {
  11639. if (!exists)
  11640. job->hp_factor = 0;
  11641. }
  11642. if (this->nodeExists(node, "HpIncrease")) {
  11643. uint32 hp;
  11644. if (!this->asUInt32(node, "HpIncrease", hp))
  11645. return 0;
  11646. job->hp_increase = hp;
  11647. } else {
  11648. if (!exists)
  11649. job->hp_increase = 500;
  11650. }
  11651. if (this->nodeExists(node, "SpIncrease")) {
  11652. uint32 sp;
  11653. if (!this->asUInt32(node, "SpIncrease", sp))
  11654. return 0;
  11655. job->sp_increase = sp;
  11656. } else {
  11657. if (!exists)
  11658. job->sp_increase = 100;
  11659. }
  11660. if (this->nodeExists(node, "BaseASPD")) {
  11661. const ryml::NodeRef& aspdNode = node["BaseASPD"];
  11662. uint8 max = MAX_WEAPON_TYPE;
  11663. #ifdef RENEWAL // Renewal adds an extra column for shields
  11664. max += 1;
  11665. #endif
  11666. if (!exists) {
  11667. job->aspd_base.resize(max);
  11668. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11669. }
  11670. for (const auto &aspdit : aspdNode) {
  11671. std::string weapon;
  11672. c4::from_chars(aspdit.key(), &weapon);
  11673. std::string weapon_constant = "W_" + weapon;
  11674. int64 constant;
  11675. if (!script_get_constant(weapon_constant.c_str(), &constant)) {
  11676. this->invalidWarning(aspdNode["BaseASPD"], "Unknown weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11677. continue;
  11678. }
  11679. if (constant < W_FIST || constant > max) {
  11680. this->invalidWarning(aspdNode["BaseASPD"], "Invalid weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11681. continue;
  11682. }
  11683. int16 aspd;
  11684. if (!this->asInt16(aspdNode, weapon.c_str(), aspd))
  11685. return 0;
  11686. job->aspd_base[static_cast<int16>(constant)] = aspd;
  11687. }
  11688. } else {
  11689. if (!exists) {
  11690. uint8 max = MAX_WEAPON_TYPE;
  11691. #ifdef RENEWAL // Renewal adds an extra column for shields
  11692. max += 1;
  11693. #endif
  11694. job->aspd_base.resize(max);
  11695. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11696. }
  11697. }
  11698. if (this->nodeExists(node, "MaxStats")) {
  11699. const ryml::NodeRef& statNode = node["MaxStats"];
  11700. for (const auto &statit : statNode) {
  11701. std::string stat;
  11702. c4::from_chars(statit.key(), &stat);
  11703. std::string stat_constant = "PARAM_" + stat;
  11704. int64 constant;
  11705. if (!script_get_constant(stat_constant.c_str(), &constant)) {
  11706. this->invalidWarning(statNode["Bonus"], "Unknown max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11707. continue;
  11708. }
  11709. if (constant < PARAM_STR || constant >= PARAM_MAX) {
  11710. this->invalidWarning(statNode["Bonus"], "Invalid max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11711. continue;
  11712. }
  11713. uint16 max;
  11714. if (!this->asUInt16(statNode, stat.c_str(), max))
  11715. return 0;
  11716. job->max_param[constant] = max;
  11717. }
  11718. }
  11719. if (this->nodeExists(node, "MaxBaseLevel")) {
  11720. uint16 level;
  11721. if (!this->asUInt16(node, "MaxBaseLevel", level))
  11722. return 0;
  11723. if (level == 0 || level > MAX_LEVEL) {
  11724. this->invalidWarning(node["MaxBaseLevel"], "MaxBaseLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11725. level = MAX_LEVEL;
  11726. }
  11727. job->max_base_level = level;
  11728. } else {
  11729. if (!exists)
  11730. job->max_base_level = MAX_LEVEL;
  11731. }
  11732. if (this->nodeExists(node, "BaseExp")) {
  11733. for (const ryml::NodeRef& bexpNode : node["BaseExp"]) {
  11734. uint16 level;
  11735. if (!this->asUInt16(bexpNode, "Level", level))
  11736. return 0;
  11737. if (level > job->max_base_level)
  11738. continue;
  11739. if (level == 0 || level > MAX_LEVEL) {
  11740. this->invalidWarning(bexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11741. return 0;
  11742. }
  11743. if (this->nodeExists(bexpNode, "Exp")) {
  11744. t_exp exp;
  11745. if (!this->asUInt64(bexpNode, "Exp", exp))
  11746. return 0;
  11747. job->base_exp[level - 1] = exp;
  11748. }
  11749. }
  11750. }
  11751. if (this->nodeExists(node, "MaxJobLevel")) {
  11752. uint16 level;
  11753. if (!this->asUInt16(node, "MaxJobLevel", level))
  11754. return 0;
  11755. if (level == 0 || level > MAX_LEVEL) {
  11756. this->invalidWarning(node["MaxJobLevel"], "MaxJobLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11757. level = MAX_LEVEL;
  11758. }
  11759. job->max_job_level = level;
  11760. } else {
  11761. if (!exists)
  11762. job->max_job_level = MAX_LEVEL;
  11763. }
  11764. if (this->nodeExists(node, "JobExp")) {
  11765. for (const ryml::NodeRef& jexpNode : node["JobExp"]) {
  11766. uint16 level;
  11767. if (!this->asUInt16(jexpNode, "Level", level))
  11768. return 0;
  11769. if (level > job->max_job_level)
  11770. continue;
  11771. if (level == 0 || level > MAX_LEVEL) {
  11772. this->invalidWarning(jexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11773. return 0;
  11774. }
  11775. if (this->nodeExists(jexpNode, "Exp")) {
  11776. t_exp exp;
  11777. if (!this->asUInt64(jexpNode, "Exp", exp))
  11778. return 0;
  11779. job->job_exp[level - 1] = exp;
  11780. }
  11781. }
  11782. }
  11783. if (this->nodeExists(node, "BonusStats")) {
  11784. const ryml::NodeRef& bonusNode = node["BonusStats"];
  11785. for (const ryml::NodeRef& levelNode : bonusNode) {
  11786. uint16 level;
  11787. if (!this->asUInt16(levelNode, "Level", level))
  11788. return 0;
  11789. if (level == 0 || level > MAX_LEVEL) {
  11790. this->invalidWarning(levelNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11791. return 0;
  11792. }
  11793. for (uint8 idx = PARAM_STR; idx < PARAM_MAX; idx++) {
  11794. if (this->nodeExists(levelNode, parameter_names[idx])) {
  11795. int16 change;
  11796. if (!this->asInt16(levelNode, parameter_names[idx], change))
  11797. return 0;
  11798. job->job_bonus[level - 1][idx] = change;
  11799. }
  11800. }
  11801. }
  11802. }
  11803. #ifdef HP_SP_TABLES
  11804. if (this->nodeExists(node, "BaseHp")) {
  11805. for (const ryml::NodeRef& bhpNode : node["BaseHp"]) {
  11806. uint16 level;
  11807. if (!this->asUInt16(bhpNode, "Level", level))
  11808. return 0;
  11809. if (level > job->max_base_level)
  11810. continue;
  11811. if (level == 0 || level > MAX_LEVEL) {
  11812. this->invalidWarning(bhpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11813. return 0;
  11814. }
  11815. if (this->nodeExists(bhpNode, "Hp")) {
  11816. uint32 points;
  11817. if (!this->asUInt32(bhpNode, "Hp", points))
  11818. return 0;
  11819. job->base_hp[level - 1] = points;
  11820. }
  11821. }
  11822. }
  11823. if (this->nodeExists(node, "BaseSp")) {
  11824. for (const ryml::NodeRef& bspNode : node["BaseSp"]) {
  11825. uint16 level;
  11826. if (!this->asUInt16(bspNode, "Level", level))
  11827. return 0;
  11828. if (level > job->max_base_level)
  11829. continue;
  11830. if (level == 0 || level > MAX_LEVEL) {
  11831. this->invalidWarning(bspNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11832. return 0;
  11833. }
  11834. if (this->nodeExists(bspNode, "Sp")) {
  11835. uint32 points;
  11836. if (!this->asUInt32(bspNode, "Sp", points))
  11837. return 0;
  11838. job->base_sp[level - 1] = points;
  11839. }
  11840. }
  11841. }
  11842. if (this->nodeExists(node, "BaseAp")) {
  11843. for (const ryml::NodeRef& bapNode : node["BaseAp"]) {
  11844. uint16 level;
  11845. if (!this->asUInt16(bapNode, "Level", level))
  11846. return 0;
  11847. if (level > job->max_base_level)
  11848. continue;
  11849. if (level == 0 || level > MAX_LEVEL) {
  11850. this->invalidWarning(bapNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11851. return 0;
  11852. }
  11853. if (this->nodeExists(bapNode, "Ap")) {
  11854. uint32 points;
  11855. if (!this->asUInt32(bapNode, "Ap", points))
  11856. return 0;
  11857. job->base_ap[level - 1] = points;
  11858. }
  11859. }
  11860. }
  11861. #endif
  11862. if (!exists)
  11863. this->put(static_cast<uint16>(job_id), job);
  11864. }
  11865. }
  11866. return 1;
  11867. }
  11868. void JobDatabase::loadingFinished() {
  11869. // Checking if all class have their data
  11870. for (auto &jobIt : *this) {
  11871. uint16 job_id = jobIt.first;
  11872. if (!pcdb_checkid(job_id))
  11873. continue;
  11874. 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)
  11875. continue; // Classes that do not need exp tables.
  11876. std::shared_ptr<s_job_info> job = jobIt.second;
  11877. uint16 maxBaseLv = job->max_base_level, maxJobLv = job->max_job_level;
  11878. if (!maxBaseLv)
  11879. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(job_id), job_id);
  11880. if (!maxJobLv)
  11881. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(job_id), job_id);
  11882. // Init and checking the empty value of Base HP/SP [Cydh]
  11883. if (job->base_hp.empty())
  11884. job->base_hp.resize(maxBaseLv);
  11885. for (uint16 j = 0; j < maxBaseLv; j++) {
  11886. if (job->base_hp[j] == 0)
  11887. job->base_hp[j] = pc_calc_basehp(j + 1, job_id);
  11888. }
  11889. if (job->base_sp.empty())
  11890. job->base_sp.resize(maxBaseLv);
  11891. for (uint16 j = 0; j < maxBaseLv; j++) {
  11892. if (job->base_sp[j] == 0)
  11893. job->base_sp[j] = pc_calc_basesp(j + 1, job_id);
  11894. }
  11895. // Resize to the maximum base level
  11896. if (job->base_hp.capacity() > maxBaseLv)
  11897. job->base_hp.erase(job->base_hp.begin() + maxBaseLv, job->base_hp.end());
  11898. if (job->base_sp.capacity() > maxBaseLv)
  11899. job->base_sp.erase(job->base_sp.begin() + maxBaseLv, job->base_sp.end());
  11900. if (job->base_ap.capacity() > maxBaseLv)
  11901. job->base_ap.erase(job->base_ap.begin() + maxBaseLv, job->base_ap.end());
  11902. // Resize to the maximum job level
  11903. if (job->job_bonus.capacity() > maxJobLv)
  11904. job->job_bonus.erase(job->job_bonus.begin() + maxJobLv, job->job_bonus.end());
  11905. for (uint16 parameter = PARAM_STR; parameter < PARAM_MAX; parameter++) {
  11906. // Store total
  11907. int16 current = 0;
  11908. for (uint16 job_level = 0; job_level < maxJobLv; job_level++) {
  11909. // Add the bonus from this job level
  11910. current += job->job_bonus[job_level][parameter];
  11911. // Set the new total on this job level
  11912. job->job_bonus[job_level][parameter] = current;
  11913. }
  11914. }
  11915. uint64 class_ = pc_jobid2mapid( job_id );
  11916. // Set normal status limits
  11917. uint16 max = battle_config.max_parameter;
  11918. do{
  11919. // Always check babies first
  11920. if( class_ & JOBL_BABY ){
  11921. if( class_ & JOBL_THIRD ){
  11922. max = battle_config.max_baby_third_parameter;
  11923. break;
  11924. }else{
  11925. max = battle_config.max_baby_parameter;
  11926. break;
  11927. }
  11928. }
  11929. // Summoner
  11930. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11931. max = battle_config.max_summoner_parameter;
  11932. break;
  11933. }
  11934. // Extended classes
  11935. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11936. max = battle_config.max_extended_parameter;
  11937. break;
  11938. }
  11939. if( class_ & JOBL_FOURTH ){
  11940. max = battle_config.max_fourth_parameter;
  11941. break;
  11942. }
  11943. // 3rd class
  11944. if( class_ & JOBL_THIRD ){
  11945. // Transcendent
  11946. if( class_ & JOBL_UPPER ){
  11947. max = battle_config.max_third_trans_parameter;
  11948. break;
  11949. }else{
  11950. max = battle_config.max_third_parameter;
  11951. break;
  11952. }
  11953. }
  11954. // Transcendent
  11955. if( class_ & JOBL_UPPER ){
  11956. max = battle_config.max_trans_parameter;
  11957. break;
  11958. }
  11959. }while( false );
  11960. for( uint16 parameter = PARAM_STR; parameter < PARAM_POW; parameter++ ){
  11961. // If it is not explicitly set in the database file
  11962. if( job->max_param[parameter] == 0 ){
  11963. job->max_param[parameter] = max;
  11964. }
  11965. }
  11966. // Set trait status limit
  11967. if( class_ & JOBL_FOURTH ){
  11968. max = battle_config.max_trait_parameter;
  11969. }else{
  11970. max = 0;
  11971. }
  11972. for( uint16 parameter = PARAM_POW; parameter < PARAM_MAX; parameter++ ){
  11973. // If it is not explicitly set in the database file
  11974. if( job->max_param[parameter] == 0 ){
  11975. job->max_param[parameter] = max;
  11976. }
  11977. }
  11978. }
  11979. TypesafeCachedYamlDatabase::loadingFinished();
  11980. }
  11981. /**
  11982. * Read job_noenter_map.txt
  11983. **/
  11984. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  11985. int class_ = -1;
  11986. int64 class_tmp;
  11987. if (ISDIGIT(str[0][0])) {
  11988. class_ = atoi(str[0]);
  11989. } else {
  11990. if (!script_get_constant(str[0], &class_tmp)) {
  11991. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  11992. return false;
  11993. }
  11994. class_ = static_cast<int>(class_tmp);
  11995. }
  11996. if (!pcdb_checkid(class_)) {
  11997. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  11998. return false;
  11999. }
  12000. std::shared_ptr<s_job_info> job = job_db.find(class_);
  12001. if (job == nullptr) {
  12002. ShowError("pc_readdb_job_noenter_map: Job %d data not initialized.\n", class_);
  12003. return false;
  12004. }
  12005. job->noenter_map.zone = atoi(str[1]);
  12006. job->noenter_map.group_lv = atoi(str[2]);
  12007. return true;
  12008. }
  12009. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  12010. return std::string(db_path) + "/statpoint.yml";
  12011. }
  12012. uint64 PlayerStatPointDatabase::parseBodyNode(const ryml::NodeRef& node) {
  12013. if (!this->nodesExist(node, { "Level", "Points" })) {
  12014. return 0;
  12015. }
  12016. uint16 level;
  12017. if (!this->asUInt16(node, "Level", level))
  12018. return 0;
  12019. uint32 point;
  12020. if (!this->asUInt32(node, "Points", point))
  12021. return 0;
  12022. if (level == 0) {
  12023. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  12024. return 0;
  12025. }
  12026. if (level > MAX_LEVEL) {
  12027. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  12028. return 0;
  12029. }
  12030. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12031. bool exists = entry != nullptr;
  12032. if( !exists ){
  12033. entry = std::make_shared<s_statpoint_entry>();
  12034. entry->level = level;
  12035. entry->statpoints = point;
  12036. }
  12037. if( this->nodeExists( node, "TraitPoints" ) ){
  12038. uint32 traitpoints;
  12039. if( !this->asUInt32( node, "TraitPoints", traitpoints ) ){
  12040. return 0;
  12041. }
  12042. entry->traitpoints = traitpoints;
  12043. }else{
  12044. if( !exists ){
  12045. entry->traitpoints = 0;
  12046. }
  12047. }
  12048. if( !exists ){
  12049. this->put( level, entry );
  12050. }
  12051. return 1;
  12052. }
  12053. /**
  12054. * Generate the remaining parts of the db if necessary.
  12055. */
  12056. void PlayerStatPointDatabase::loadingFinished(){
  12057. const uint16 trait_start_level = 200;
  12058. std::shared_ptr<s_statpoint_entry> level_one = this->find( 1 );
  12059. if( level_one == nullptr ){
  12060. if( battle_config.use_statpoint_table ){
  12061. ShowError( "Missing status points for Level 1\n" );
  12062. }
  12063. level_one = std::make_shared<s_statpoint_entry>();
  12064. level_one->level = 1;
  12065. level_one->statpoints = inter_config.start_status_points;
  12066. level_one->traitpoints = 0;
  12067. this->put( 1, level_one );
  12068. }else if( battle_config.use_statpoint_table ){
  12069. if( level_one->statpoints != inter_config.start_status_points ){
  12070. ShowError( "Status points for Level 1 (=%u) do not match inter_athena.conf value (=%u).\n", level_one->statpoints, inter_config.start_status_points );
  12071. level_one->statpoints = inter_config.start_status_points;
  12072. }
  12073. }else{
  12074. level_one->statpoints = inter_config.start_status_points;
  12075. level_one->traitpoints = 0;
  12076. }
  12077. std::shared_ptr<s_statpoint_entry> last_level = level_one;
  12078. for( uint16 level = 2; level <= MAX_LEVEL; level++ ){
  12079. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12080. bool exists = entry != nullptr;
  12081. if( !exists ){
  12082. entry = std::make_shared<s_statpoint_entry>();
  12083. entry->level = level;
  12084. this->put( level, entry );
  12085. }
  12086. if( !battle_config.use_statpoint_table || !exists ){
  12087. if( battle_config.use_statpoint_table ){
  12088. ShowError("Missing status points for Level %hu\n", level);
  12089. }
  12090. if( level <= trait_start_level ){
  12091. entry->statpoints = last_level->statpoints + ( ( level - 1 + 15 ) / 5 );
  12092. }else{
  12093. entry->statpoints = last_level->statpoints;
  12094. }
  12095. }
  12096. if( !battle_config.use_traitpoint_table || !exists ){
  12097. if( battle_config.use_traitpoint_table && level > trait_start_level ){
  12098. ShowError( "Missing trait points for Level %hu\n", level );
  12099. }
  12100. if( level > trait_start_level ){
  12101. entry->traitpoints = ( level - trait_start_level ) * 3 + ( level - trait_start_level ) / 5 * 4;
  12102. }else{
  12103. entry->traitpoints = 0;
  12104. }
  12105. }
  12106. // Store it for next iteration
  12107. last_level = entry;
  12108. }
  12109. TypesafeCachedYamlDatabase::loadingFinished();
  12110. }
  12111. /*==========================================
  12112. * pc DB reading.
  12113. * job_stats.yml - Job values
  12114. * skill_tree.txt - skill tree for every class
  12115. * attr_fix.yml - elemental adjustment table
  12116. *------------------------------------------*/
  12117. void pc_readdb(void) {
  12118. int i, s = 1;
  12119. const char* dbsubpath[] = {
  12120. "",
  12121. "/" DBIMPORT,
  12122. //add other path here
  12123. };
  12124. //reset
  12125. job_db.clear(); // job_info table
  12126. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  12127. penalty_db.load();
  12128. #endif
  12129. statpoint_db.clear();
  12130. job_db.load();
  12131. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  12132. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  12133. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  12134. char* dbsubpath1 = (char*)aMalloc(n1+1);
  12135. char* dbsubpath2 = (char*)aMalloc(n2+1);
  12136. if(i==0) {
  12137. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12138. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  12139. }
  12140. else {
  12141. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12142. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  12143. }
  12144. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  12145. aFree(dbsubpath1);
  12146. aFree(dbsubpath2);
  12147. }
  12148. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  12149. skill_tree_db.reload();
  12150. statpoint_db.load();
  12151. }
  12152. // Read MOTD on startup. [Valaris]
  12153. int pc_read_motd(void)
  12154. {
  12155. FILE* fp;
  12156. // clear old MOTD
  12157. memset(motd_text, 0, sizeof(motd_text));
  12158. // read current MOTD
  12159. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  12160. {
  12161. unsigned int entries = 0;
  12162. char buf[CHAT_SIZE_MAX];
  12163. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  12164. {
  12165. unsigned int lines = 0;
  12166. size_t len;
  12167. lines++;
  12168. if( buf[0] == '/' && buf[1] == '/' )
  12169. continue;
  12170. len = strlen(buf);
  12171. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  12172. len--;
  12173. if( len ) {
  12174. char * ptr;
  12175. buf[len] = 0;
  12176. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  12177. 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);
  12178. }
  12179. else {// empty line
  12180. buf[0] = ' ';
  12181. buf[1] = 0;
  12182. }
  12183. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  12184. entries++;
  12185. }
  12186. fclose(fp);
  12187. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  12188. }
  12189. else
  12190. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  12191. return 0;
  12192. }
  12193. void pc_itemcd_do(map_session_data *sd, bool load) {
  12194. int i,cursor = 0;
  12195. struct item_cd* cd = NULL;
  12196. if( load ) {
  12197. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  12198. // no item cooldown is associated with this character
  12199. return;
  12200. }
  12201. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12202. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  12203. sd->item_delay[cursor].tick = cd->tick[i];
  12204. sd->item_delay[cursor].nameid = cd->nameid[i];
  12205. cursor++;
  12206. }
  12207. }
  12208. idb_remove(itemcd_db,sd->status.char_id);
  12209. } else {
  12210. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  12211. // create a new skill cooldown object for map storage
  12212. CREATE( cd, struct item_cd, 1 );
  12213. idb_put( itemcd_db, sd->status.char_id, cd );
  12214. }
  12215. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12216. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  12217. cd->tick[cursor] = sd->item_delay[i].tick;
  12218. cd->nameid[cursor] = sd->item_delay[i].nameid;
  12219. cursor++;
  12220. }
  12221. }
  12222. }
  12223. return;
  12224. }
  12225. /**
  12226. * Add item delay to player's item delay data
  12227. * @param sd Player
  12228. * @param id Item data
  12229. * @param tick Current tick
  12230. * @param n Item index in inventory
  12231. * @return 0: No delay, can consume item.
  12232. * 1: Has delay, cancel consumption.
  12233. **/
  12234. uint8 pc_itemcd_add(map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12235. int i;
  12236. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  12237. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  12238. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  12239. if( i < MAX_ITEMDELAYS ) {
  12240. if( sd->item_delay[i].nameid ) {// found
  12241. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  12242. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  12243. char e_msg[CHAT_SIZE_MAX];
  12244. if( e_tick > 99 )
  12245. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  12246. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  12247. else
  12248. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  12249. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  12250. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  12251. return 1; // Delay has not expired yet
  12252. }
  12253. } else {// not yet used item (all slots are initially empty)
  12254. sd->item_delay[i].nameid = id->nameid;
  12255. }
  12256. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  12257. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  12258. } else {// should not happen
  12259. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  12260. }
  12261. //clean up used delays so we can give room for more
  12262. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12263. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  12264. sd->item_delay[i].tick = 0;
  12265. sd->item_delay[i].nameid = 0;
  12266. }
  12267. }
  12268. return 0;
  12269. }
  12270. /**
  12271. * Check if player has delay to reuse item
  12272. * @param sd Player
  12273. * @param id Item data
  12274. * @param tick Current tick
  12275. * @param n Item index in inventory
  12276. * @return 0: No delay, can consume item.
  12277. * 1: Has delay, cancel consumption.
  12278. **/
  12279. uint8 pc_itemcd_check(map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12280. status_change *sc = NULL;
  12281. nullpo_retr(0, sd);
  12282. nullpo_retr(0, id);
  12283. // Do normal delay assignment
  12284. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  12285. return pc_itemcd_add(sd, id, tick, n);
  12286. // Send reply of delay remains
  12287. if (sc->getSCE(id->delay.sc)) {
  12288. const struct TimerData *timer = get_timer(sc->getSCE(id->delay.sc)->timer);
  12289. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  12290. return 1;
  12291. }
  12292. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  12293. return 0;
  12294. }
  12295. /**
  12296. * Clear the dmglog data from player
  12297. * @param sd
  12298. * @param md
  12299. **/
  12300. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  12301. {
  12302. uint8 i;
  12303. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  12304. if (i < DAMAGELOG_SIZE) {
  12305. md->dmglog[i].id = 0;
  12306. md->dmglog[i].dmg = 0;
  12307. md->dmglog[i].flag = 0;
  12308. }
  12309. }
  12310. /**
  12311. * Add log to player's dmglog
  12312. * @param sd
  12313. * @param id Monster's GID
  12314. **/
  12315. void pc_damage_log_add(map_session_data *sd, int id)
  12316. {
  12317. uint8 i = 0;
  12318. if (!sd || !id)
  12319. return;
  12320. //Only store new data, don't need to renew the old one with same id
  12321. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  12322. if (i < DAMAGELOG_SIZE_PC)
  12323. return;
  12324. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12325. if (sd->dmglog[i] == 0) {
  12326. sd->dmglog[i] = id;
  12327. return;
  12328. }
  12329. }
  12330. }
  12331. /**
  12332. * Clear dmglog data from player
  12333. * @param sd
  12334. * @param id Monster's id
  12335. **/
  12336. void pc_damage_log_clear(map_session_data *sd, int id)
  12337. {
  12338. uint8 i;
  12339. struct mob_data *md = NULL;
  12340. if (!sd)
  12341. return;
  12342. if (!id) {
  12343. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12344. if( !sd->dmglog[i] ) //skip the empty value
  12345. continue;
  12346. if ((md = map_id2md(sd->dmglog[i])))
  12347. pc_clear_log_damage_sub(sd->status.char_id,md);
  12348. sd->dmglog[i] = 0;
  12349. }
  12350. }
  12351. else {
  12352. if ((md = map_id2md(id)))
  12353. pc_clear_log_damage_sub(sd->status.char_id,md);
  12354. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  12355. if (i < DAMAGELOG_SIZE_PC)
  12356. sd->dmglog[i] = 0;
  12357. }
  12358. }
  12359. /**
  12360. * Status change data arrived from char-server
  12361. * @param sd: Player data
  12362. */
  12363. void pc_scdata_received(map_session_data *sd) {
  12364. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  12365. clif_weight_limit( sd );
  12366. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) && pc_attendance_counter(sd) < 200 ){
  12367. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  12368. }
  12369. sd->state.pc_loaded = true;
  12370. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  12371. sd->state.connect_new = 1;
  12372. clif_parse_LoadEndAck(sd->fd, sd);
  12373. }
  12374. if (pc_iscarton(sd)) {
  12375. sd->cart_weight_max = 0; // Force a client refesh
  12376. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  12377. }
  12378. if (sd->sc.getSCE(SC_SOULENERGY))
  12379. sd->soulball = sd->sc.getSCE(SC_SOULENERGY)->val1;
  12380. }
  12381. /**
  12382. * Check player account expiration time and rental item expirations
  12383. * @param sd: Player data
  12384. */
  12385. void pc_check_expiration(map_session_data *sd) {
  12386. #ifndef ENABLE_SC_SAVING
  12387. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  12388. #endif
  12389. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  12390. time_t exp_time = sd->expiration_time;
  12391. char tmpstr[1024];
  12392. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  12393. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  12394. pc_expire_check(sd);
  12395. }
  12396. }
  12397. TIMER_FUNC(pc_expiration_timer){
  12398. map_session_data *sd = map_id2sd(id);
  12399. if( !sd ) return 0;
  12400. sd->expiration_tid = INVALID_TIMER;
  12401. if( sd->fd )
  12402. clif_authfail_fd(sd->fd,10);
  12403. map_quit(sd);
  12404. return 0;
  12405. }
  12406. TIMER_FUNC(pc_autotrade_timer){
  12407. map_session_data *sd = map_id2sd(id);
  12408. if (!sd)
  12409. return 0;
  12410. sd->autotrade_tid = INVALID_TIMER;
  12411. if (sd->state.autotrade&2)
  12412. vending_reopen(sd);
  12413. if (sd->state.autotrade&4)
  12414. buyingstore_reopen(sd);
  12415. if (!sd->vender_id && !sd->buyer_id) {
  12416. sd->state.autotrade = 0;
  12417. map_quit(sd);
  12418. }
  12419. return 0;
  12420. }
  12421. /* this timer exists only when a character with a expire timer > 24h is online */
  12422. /* it loops thru online players once an hour to check whether a new < 24h is available */
  12423. TIMER_FUNC(pc_global_expiration_timer){
  12424. struct s_mapiterator* iter;
  12425. map_session_data* sd;
  12426. iter = mapit_getallusers();
  12427. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  12428. if( sd->expiration_time )
  12429. pc_expire_check(sd);
  12430. mapit_free(iter);
  12431. return 0;
  12432. }
  12433. void pc_expire_check(map_session_data *sd) {
  12434. /* ongoing timer */
  12435. if( sd->expiration_tid != INVALID_TIMER )
  12436. return;
  12437. /* not within the next 24h, enable the global check */
  12438. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  12439. /* global check not running, enable */
  12440. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  12441. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  12442. return;
  12443. }
  12444. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  12445. }
  12446. /**
  12447. * Deposit some money to bank
  12448. * @param sd
  12449. * @param money Amount of money to deposit
  12450. **/
  12451. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(map_session_data *sd, int money) {
  12452. unsigned int limit_check = money + sd->bank_vault;
  12453. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  12454. return BDA_OVERFLOW;
  12455. } else if ( money > sd->status.zeny ) {
  12456. return BDA_NO_MONEY;
  12457. }
  12458. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  12459. return BDA_NO_MONEY;
  12460. sd->bank_vault += money;
  12461. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12462. if( save_settings&CHARSAVE_BANK )
  12463. chrif_save(sd, CSAVE_NORMAL);
  12464. return BDA_SUCCESS;
  12465. }
  12466. /**
  12467. * Withdraw money from bank
  12468. * @param sd
  12469. * @param money Amount of money that will be withdrawn
  12470. **/
  12471. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(map_session_data *sd, int money) {
  12472. unsigned int limit_check = money + sd->status.zeny;
  12473. if( money <= 0 ) {
  12474. return BWA_UNKNOWN_ERROR;
  12475. } else if ( money > sd->bank_vault ) {
  12476. return BWA_NO_MONEY;
  12477. } else if ( limit_check > MAX_ZENY ) {
  12478. /* no official response for this scenario exists. */
  12479. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  12480. return BWA_UNKNOWN_ERROR;
  12481. }
  12482. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  12483. return BWA_NO_MONEY;
  12484. sd->bank_vault -= money;
  12485. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12486. if( save_settings&CHARSAVE_BANK )
  12487. chrif_save(sd, CSAVE_NORMAL);
  12488. return BWA_SUCCESS;
  12489. }
  12490. /**
  12491. * Clear Crimson Marker data from caster
  12492. * @param sd: Player
  12493. **/
  12494. void pc_crimson_marker_clear(map_session_data *sd) {
  12495. uint8 i;
  12496. if (!sd)
  12497. return;
  12498. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  12499. struct block_list *bl = NULL;
  12500. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  12501. status_change_end(bl,SC_C_MARKER);
  12502. sd->c_marker[i] = 0;
  12503. }
  12504. }
  12505. /**
  12506. * Show version to player
  12507. * @param sd: Player
  12508. **/
  12509. void pc_show_version(map_session_data *sd) {
  12510. const char* svn = get_svn_revision();
  12511. char buf[CHAT_SIZE_MAX];
  12512. if( svn[0] != UNKNOWN_VERSION )
  12513. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  12514. else {
  12515. const char* git = get_git_hash();
  12516. if( git[0] != UNKNOWN_VERSION )
  12517. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  12518. else
  12519. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  12520. }
  12521. clif_displaymessage(sd->fd,buf);
  12522. }
  12523. /**
  12524. * Run bonus_script on player
  12525. * @param sd
  12526. * @author [Cydh]
  12527. **/
  12528. void pc_bonus_script(map_session_data *sd) {
  12529. t_tick now = gettick();
  12530. struct linkdb_node *node = NULL, *next = NULL;
  12531. if (!sd || !(node = sd->bonus_script.head))
  12532. return;
  12533. while (node) {
  12534. struct s_bonus_script_entry *entry = NULL;
  12535. next = node->next;
  12536. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  12537. // Only start timer for new bonus_script
  12538. if (entry->tid == INVALID_TIMER) {
  12539. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  12540. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  12541. entry->tick += now;
  12542. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  12543. }
  12544. if (entry->script)
  12545. run_script(entry->script, 0, sd->bl.id, 0);
  12546. else
  12547. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  12548. }
  12549. node = next;
  12550. }
  12551. }
  12552. /**
  12553. * Add bonus_script to player
  12554. * @param sd Player
  12555. * @param script_str Script string
  12556. * @param dur Duration in ms
  12557. * @param icon EFST
  12558. * @param flag Flags @see enum e_bonus_script_flags
  12559. * @param type 0 - None, 1 - Buff, 2 - Debuff
  12560. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  12561. * @author [Cydh]
  12562. **/
  12563. 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) {
  12564. struct script_code *script = NULL;
  12565. struct linkdb_node *node = NULL;
  12566. struct s_bonus_script_entry *entry = NULL;
  12567. if (!sd)
  12568. return NULL;
  12569. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  12570. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  12571. return NULL;
  12572. }
  12573. // Duplication checks
  12574. if ((node = sd->bonus_script.head)) {
  12575. while (node) {
  12576. entry = (struct s_bonus_script_entry *)node->data;
  12577. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  12578. t_tick newdur = gettick() + dur;
  12579. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  12580. settick_timer(entry->tid, newdur);
  12581. script_free_code(script);
  12582. return NULL;
  12583. }
  12584. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  12585. break;
  12586. else { // No duplicate bonus
  12587. script_free_code(script);
  12588. return NULL;
  12589. }
  12590. }
  12591. node = node->next;
  12592. }
  12593. }
  12594. CREATE(entry, struct s_bonus_script_entry, 1);
  12595. entry->script_buf = StringBuf_Malloc();
  12596. StringBuf_AppendStr(entry->script_buf, script_str);
  12597. entry->tid = INVALID_TIMER;
  12598. entry->flag = flag;
  12599. entry->icon = icon;
  12600. entry->tick = dur; // Use duration first, on run change to expire time
  12601. entry->type = type;
  12602. entry->script = script;
  12603. sd->bonus_script.count++;
  12604. return entry;
  12605. }
  12606. /**
  12607. * Remove bonus_script data from player
  12608. * @param sd: Target player
  12609. * @param list: Bonus script entry from player
  12610. * @author [Cydh]
  12611. **/
  12612. void pc_bonus_script_free_entry(map_session_data *sd, struct s_bonus_script_entry *entry) {
  12613. if (entry->tid != INVALID_TIMER)
  12614. delete_timer(entry->tid, pc_bonus_script_timer);
  12615. if (entry->script)
  12616. script_free_code(entry->script);
  12617. if (entry->script_buf)
  12618. StringBuf_Free(entry->script_buf);
  12619. if (sd) {
  12620. if (entry->icon != EFST_BLANK)
  12621. clif_status_load(&sd->bl, entry->icon, 0);
  12622. if (sd->bonus_script.count > 0)
  12623. sd->bonus_script.count--;
  12624. }
  12625. aFree(entry);
  12626. }
  12627. /**
  12628. * Do final process if no entry left
  12629. * @param sd
  12630. **/
  12631. static void inline pc_bonus_script_check_final(map_session_data *sd) {
  12632. if (sd->bonus_script.count == 0) {
  12633. if (sd->bonus_script.head && sd->bonus_script.head->data)
  12634. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  12635. linkdb_final(&sd->bonus_script.head);
  12636. }
  12637. }
  12638. /**
  12639. * Timer for bonus_script
  12640. * @param tid
  12641. * @param tick
  12642. * @param id
  12643. * @param data
  12644. * @author [Cydh]
  12645. **/
  12646. TIMER_FUNC(pc_bonus_script_timer){
  12647. map_session_data *sd;
  12648. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  12649. sd = map_id2sd(id);
  12650. if (!sd) {
  12651. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  12652. return 0;
  12653. }
  12654. if (tid == INVALID_TIMER)
  12655. return 0;
  12656. if (!sd->bonus_script.head || entry == NULL) {
  12657. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  12658. return 0;
  12659. }
  12660. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12661. pc_bonus_script_free_entry(sd, entry);
  12662. pc_bonus_script_check_final(sd);
  12663. status_calc_pc(sd,SCO_NONE);
  12664. return 0;
  12665. }
  12666. /**
  12667. * Check then clear all active timer(s) of bonus_script data from player based on reason
  12668. * @param sd: Target player
  12669. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  12670. * @author [Cydh]
  12671. **/
  12672. void pc_bonus_script_clear(map_session_data *sd, uint32 flag) {
  12673. struct linkdb_node *node = NULL;
  12674. uint16 count = 0;
  12675. if (!sd || !(node = sd->bonus_script.head))
  12676. return;
  12677. while (node) {
  12678. struct linkdb_node *next = node->next;
  12679. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  12680. if (entry && (
  12681. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  12682. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  12683. (flag&entry->flag) || // Matched flag
  12684. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  12685. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  12686. )
  12687. )
  12688. {
  12689. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12690. pc_bonus_script_free_entry(sd, entry);
  12691. count++;
  12692. }
  12693. node = next;
  12694. }
  12695. pc_bonus_script_check_final(sd);
  12696. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  12697. status_calc_pc(sd,SCO_NONE);
  12698. }
  12699. /** [Cydh]
  12700. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  12701. * @param sd: Target player
  12702. */
  12703. void pc_cell_basilica(map_session_data *sd) {
  12704. nullpo_retv(sd);
  12705. #ifdef RENEWAL
  12706. enum sc_type type = SC_BASILICA_CELL;
  12707. #else
  12708. enum sc_type type = SC_BASILICA;
  12709. #endif
  12710. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  12711. if (sd->sc.getSCE(type))
  12712. status_change_end(&sd->bl, type);
  12713. }
  12714. else if (!sd->sc.getSCE(type))
  12715. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  12716. }
  12717. /** [Cydh]
  12718. * Get maximum specified parameter for specified class
  12719. * @param sd: Player data
  12720. * @param param: Max parameter to check
  12721. * @return max_param
  12722. */
  12723. uint16 pc_maxparameter(map_session_data *sd, e_params param) {
  12724. nullpo_retr(0, sd);
  12725. std::shared_ptr<s_job_info> job = job_db.find(pc_mapid2jobid(sd->class_,sd->status.sex));
  12726. if( job == nullptr || param == PARAM_MAX ){
  12727. return 0;
  12728. }
  12729. return job->max_param[param];
  12730. }
  12731. /**
  12732. * Get max ASPD for player based on Class
  12733. * @param sd Player
  12734. * @return ASPD
  12735. */
  12736. short pc_maxaspd(map_session_data *sd) {
  12737. nullpo_ret(sd);
  12738. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  12739. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  12740. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  12741. battle_config.max_aspd ));
  12742. }
  12743. /**
  12744. * Calculates total item-group related bonuses for the given item
  12745. * @param sd Player
  12746. * @param nameid Item ID
  12747. * @return Heal rate
  12748. **/
  12749. short pc_get_itemgroup_bonus(map_session_data* sd, t_itemid nameid, std::vector<s_item_bonus>& bonuses) {
  12750. if (bonuses.empty())
  12751. return 0;
  12752. short bonus = 0;
  12753. for (const auto &it : bonuses) {
  12754. uint16 group_id = it.id;
  12755. if (group_id == 0)
  12756. continue;
  12757. if (itemdb_group.item_exists(group_id, nameid))
  12758. bonus += it.val;
  12759. }
  12760. return bonus;
  12761. }
  12762. /**
  12763. * Calculates total item-group related bonuses for the given item group
  12764. * @param sd Player
  12765. * @param group_id Item Group ID
  12766. * @return Heal rate
  12767. **/
  12768. short pc_get_itemgroup_bonus_group(map_session_data* sd, uint16 group_id, std::vector<s_item_bonus>& bonuses) {
  12769. if (bonuses.empty())
  12770. return 0;
  12771. for (const auto &it : bonuses) {
  12772. if (it.id == group_id)
  12773. return it.val;
  12774. }
  12775. return 0;
  12776. }
  12777. /**
  12778. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  12779. * @param eqi Item EQI of enum equip_index
  12780. * @param *equip_index Player's equip_index[]
  12781. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  12782. * @return True if item in same inventory index, False if doesn't
  12783. */
  12784. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  12785. if (index < 0 || index >= MAX_INVENTORY)
  12786. return true;
  12787. // Dual weapon checks
  12788. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  12789. return true;
  12790. // Headgear with Mid & Low location
  12791. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  12792. return true;
  12793. // Headgear with Top & Mid or Low location
  12794. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  12795. return true;
  12796. // Headgear with Mid & Low location
  12797. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  12798. return true;
  12799. // Headgear with Top & Mid or Low location
  12800. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  12801. return true;
  12802. return false;
  12803. }
  12804. /**
  12805. * Generate Unique item ID for player
  12806. * @param sd : Player
  12807. * @return A generated Unique item ID
  12808. */
  12809. uint64 pc_generate_unique_id(map_session_data *sd) {
  12810. nullpo_ret(sd);
  12811. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  12812. }
  12813. /**
  12814. * Validating skill from player after logged on
  12815. * @param sd
  12816. **/
  12817. void pc_validate_skill(map_session_data *sd) {
  12818. if (sd) {
  12819. uint16 i = 0, count = 0;
  12820. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  12821. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  12822. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  12823. for (i = 0; i < MAX_SKILL; i++) {
  12824. uint16 idx = 0;
  12825. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  12826. continue;
  12827. if ((idx = skill_get_index(tmp_skills[i].id))) {
  12828. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  12829. count++;
  12830. }
  12831. else
  12832. 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);
  12833. }
  12834. }
  12835. }
  12836. /**
  12837. * Show available NPC Quest / Event Icon Check [Kisuka]
  12838. * @param sd Player
  12839. **/
  12840. void pc_show_questinfo(map_session_data *sd) {
  12841. #if PACKETVER >= 20090218
  12842. nullpo_retv(sd);
  12843. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12844. return;
  12845. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12846. nullpo_retv(mapdata);
  12847. if (mapdata->qi_npc.empty())
  12848. return;
  12849. if (mapdata->qi_npc.size() != sd->qi_display.size())
  12850. return; // init was not called yet
  12851. for (int i = 0; i < mapdata->qi_npc.size(); i++) {
  12852. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  12853. if (!nd || nd->qi_data.empty())
  12854. continue;
  12855. bool show = false;
  12856. for (auto &qi : nd->qi_data) {
  12857. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  12858. show = true;
  12859. // Check if need to be displayed
  12860. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  12861. sd->qi_display[i].is_active = true;
  12862. sd->qi_display[i].icon = qi->icon;
  12863. sd->qi_display[i].color = qi->color;
  12864. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  12865. }
  12866. break;
  12867. }
  12868. }
  12869. if (show == false) {
  12870. // Check if need to be hide
  12871. if (sd->qi_display[i].is_active) {
  12872. sd->qi_display[i].is_active = false;
  12873. sd->qi_display[i].icon = QTYPE_NONE;
  12874. sd->qi_display[i].color = QMARK_NONE;
  12875. #if PACKETVER >= 20120410
  12876. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  12877. #else
  12878. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  12879. #endif
  12880. }
  12881. }
  12882. }
  12883. #endif
  12884. }
  12885. /**
  12886. * Reinit the questinfo for player when changing map
  12887. * @param sd Player
  12888. **/
  12889. void pc_show_questinfo_reinit(map_session_data *sd) {
  12890. #if PACKETVER >= 20090218
  12891. nullpo_retv(sd);
  12892. sd->qi_display.clear();
  12893. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12894. return;
  12895. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12896. nullpo_retv(mapdata);
  12897. if (mapdata->qi_npc.empty())
  12898. return;
  12899. sd->qi_display.reserve( mapdata->qi_npc.size() );
  12900. for( int i = 0; i < mapdata->qi_npc.size(); i++ ){
  12901. sd->qi_display.push_back( s_qi_display() );
  12902. }
  12903. #endif
  12904. }
  12905. /**
  12906. * Check if a job is allowed to enter the map
  12907. * @param jobid Job ID see enum e_job or sd->status.class_
  12908. * @param m ID -an index- for direct indexing map[] array
  12909. * @return 1 if job is allowed, 0 otherwise
  12910. **/
  12911. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  12912. // Map is other map server.
  12913. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  12914. if (m < 0)
  12915. return true;
  12916. struct map_data *mapdata = map_getmapdata(m);
  12917. if (!mapdata->cell)
  12918. return false;
  12919. if (!pcdb_checkid(jobid))
  12920. return false;
  12921. std::shared_ptr<s_job_info> job = job_db.find(jobid);
  12922. if (!job->noenter_map.zone || group_lv > job->noenter_map.group_lv)
  12923. return true;
  12924. if ((job->noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  12925. (job->noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  12926. (job->noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  12927. (job->noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  12928. (job->noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  12929. (job->noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  12930. )
  12931. return false;
  12932. return true;
  12933. }
  12934. /**
  12935. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  12936. * @param sd
  12937. **/
  12938. void pc_set_costume_view(map_session_data *sd) {
  12939. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  12940. struct item_data *id = NULL;
  12941. nullpo_retv(sd);
  12942. head_low = sd->status.head_bottom;
  12943. head_mid = sd->status.head_mid;
  12944. head_top = sd->status.head_top;
  12945. robe = sd->status.robe;
  12946. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  12947. //Added check to prevent sending the same look on multiple slots ->
  12948. //causes client to redraw item on top of itself. (suggested by Lupus)
  12949. // Normal headgear checks
  12950. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  12951. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  12952. sd->status.head_bottom = id->look;
  12953. else
  12954. sd->status.head_bottom = 0;
  12955. }
  12956. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  12957. if (!(id->equip&(EQP_HEAD_TOP)))
  12958. sd->status.head_mid = id->look;
  12959. else
  12960. sd->status.head_mid = 0;
  12961. }
  12962. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  12963. sd->status.head_top = id->look;
  12964. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  12965. sd->status.robe = id->look;
  12966. // Costumes check
  12967. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  12968. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  12969. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  12970. sd->status.head_bottom = id->look;
  12971. else
  12972. sd->status.head_bottom = 0;
  12973. }
  12974. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  12975. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  12976. sd->status.head_mid = id->look;
  12977. else
  12978. sd->status.head_mid = 0;
  12979. }
  12980. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  12981. sd->status.head_top = id->look;
  12982. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  12983. sd->status.robe = id->look;
  12984. }
  12985. if (sd->setlook_head_bottom)
  12986. sd->status.head_bottom = sd->setlook_head_bottom;
  12987. if (sd->setlook_head_mid)
  12988. sd->status.head_mid = sd->setlook_head_mid;
  12989. if (sd->setlook_head_top)
  12990. sd->status.head_top = sd->setlook_head_top;
  12991. if (sd->setlook_robe)
  12992. sd->status.robe = sd->setlook_robe;
  12993. if (head_low != sd->status.head_bottom)
  12994. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  12995. if (head_mid != sd->status.head_mid)
  12996. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  12997. if (head_top != sd->status.head_top)
  12998. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  12999. if (robe != sd->status.robe)
  13000. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  13001. }
  13002. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  13003. uint32 date = date_get(DT_YYYYMMDD);
  13004. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  13005. std::shared_ptr<s_attendance_period> period = pair.second;
  13006. if( period->start <= date && period->end >= date ){
  13007. return period;
  13008. }
  13009. }
  13010. return nullptr;
  13011. }
  13012. bool pc_attendance_enabled(){
  13013. // Check if the attendance feature is disabled
  13014. if( !battle_config.feature_attendance ){
  13015. return false;
  13016. }
  13017. // Check if there is a running attendance period
  13018. return pc_attendance_period() != nullptr;
  13019. }
  13020. static inline bool pc_attendance_rewarded_today( map_session_data* sd ){
  13021. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  13022. }
  13023. int32 pc_attendance_counter( map_session_data* sd ){
  13024. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13025. // No running attendance period
  13026. if( period == nullptr ){
  13027. return 0;
  13028. }
  13029. // Get the counter for the current period
  13030. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13031. // Check if we have a remaining counter from a previous period
  13032. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  13033. // Reset the counter to zero
  13034. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  13035. return 0;
  13036. }
  13037. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  13038. }
  13039. void pc_attendance_claim_reward( map_session_data* sd ){
  13040. // If the user's group does not have the permission
  13041. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  13042. return;
  13043. }
  13044. // Check if the attendance feature is disabled
  13045. if( !pc_attendance_enabled() ){
  13046. return;
  13047. }
  13048. // Check if the user already got his reward today
  13049. if( pc_attendance_rewarded_today( sd ) ){
  13050. return;
  13051. }
  13052. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13053. attendance_counter += 1;
  13054. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13055. if( period == nullptr ){
  13056. return;
  13057. }
  13058. if( period->rewards.size() < attendance_counter ){
  13059. return;
  13060. }
  13061. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  13062. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  13063. if( save_settings&CHARSAVE_ATTENDANCE )
  13064. chrif_save(sd, CSAVE_NORMAL);
  13065. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  13066. struct mail_message msg;
  13067. memset( &msg, 0, sizeof( struct mail_message ) );
  13068. msg.dest_id = sd->status.char_id;
  13069. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  13070. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  13071. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  13072. msg.item[0].nameid = reward->item_id;
  13073. msg.item[0].amount = reward->amount;
  13074. msg.item[0].identify = 1;
  13075. msg.status = MAIL_NEW;
  13076. msg.type = MAIL_INBOX_NORMAL;
  13077. msg.timestamp = time(NULL);
  13078. intif_Mail_send(0, &msg);
  13079. clif_attendence_response( sd, attendance_counter );
  13080. }
  13081. /**
  13082. * Send a player to jail and determine the location to send in jail.
  13083. * @param sd: Player data
  13084. * @param duration: Duration in minutes (default INT_MAX = infinite)
  13085. */
  13086. void pc_jail(map_session_data &sd, int32 duration) {
  13087. uint16 m_index = mapindex_name2id(MAP_JAIL);
  13088. int16 x, y;
  13089. switch (rnd() % 2) { // Jail Locations
  13090. case 0: // Jail #1
  13091. x = 49;
  13092. y = 75;
  13093. break;
  13094. default: // Default Jail
  13095. x = 24;
  13096. y = 75;
  13097. break;
  13098. }
  13099. duration = i32max(0, duration); // Can't be less than 0 seconds.
  13100. // If duration > 0 then triggered via jailfor which checks every minute.
  13101. // If duration == INT_MAX then triggered via jail for infinite duration.
  13102. // If duration == 0 then triggered via unjail and end status.
  13103. if (duration > 0)
  13104. sc_start4(nullptr, &sd.bl, SC_JAILED, 100, duration, m_index, x, y, 60000);
  13105. else
  13106. status_change_end(&sd.bl, SC_JAILED);
  13107. }
  13108. /**
  13109. * Determine the punishment type when failing macro checks.
  13110. * @param sd: Player data
  13111. * @param stype: Macro detection status type (for banning)
  13112. */
  13113. static void pc_macro_punishment(map_session_data &sd, e_macro_detect_status stype) {
  13114. int32 duration = 0;
  13115. // Determine if there's a unique duration
  13116. if (battle_config.macro_detection_punishment_time > 0) {
  13117. char time[13];
  13118. safesnprintf(time, 13, "+%dnm", battle_config.macro_detection_punishment_time);
  13119. duration = static_cast<int32>(solve_time(time));
  13120. }
  13121. // Delete the timer
  13122. if (sd.macro_detect.timer != INVALID_TIMER)
  13123. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13124. // Clear the macro detect data
  13125. sd.macro_detect = {};
  13126. sd.macro_detect.timer = INVALID_TIMER;
  13127. // Unblock all actions for the player
  13128. sd.state.block_action &= ~PCBLOCK_ALL;
  13129. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13130. if (battle_config.macro_detection_punishment == 0) { // Ban
  13131. clif_macro_detector_status(sd, stype);
  13132. 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);
  13133. } else { // Jail
  13134. // Send success to close the window without closing the client
  13135. clif_macro_detector_status(sd, MCD_GOOD);
  13136. pc_jail(sd, (duration == 0 ? INT_MAX : duration / 60));
  13137. }
  13138. }
  13139. /**
  13140. * Save a captcha image to memory via /macro_register.
  13141. * @param sd: Player data
  13142. * @param image_size: Captcha image size
  13143. * @param captcha_answer: Answer to captcha
  13144. */
  13145. void pc_macro_captcha_register(map_session_data &sd, uint16 image_size, char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13146. nullpo_retv(captcha_answer);
  13147. sd.captcha_upload.cd = nullptr;
  13148. sd.captcha_upload.upload_size = 0;
  13149. if (strlen(captcha_answer) < 4 || image_size == 0 || image_size > CAPTCHA_BMP_SIZE) {
  13150. clif_captcha_upload_request(sd); // Notify client of failure.
  13151. return;
  13152. }
  13153. std::shared_ptr<s_captcha_data> cd = std::make_shared<s_captcha_data>();
  13154. sd.captcha_upload.cd = cd;
  13155. cd->image_size = image_size;
  13156. safestrncpy(cd->captcha_answer, captcha_answer, sizeof(cd->captcha_answer));
  13157. memset(cd->image_data, 0, sizeof(cd->image_data));
  13158. // Request the image data from the client.
  13159. clif_captcha_upload_request(sd);
  13160. }
  13161. /**
  13162. * Save captcha image to server.
  13163. * @param sd: Player data
  13164. * @param captcha_key: Captcha ID
  13165. * @param upload_size: Captcha size
  13166. * @param upload_data: Image data
  13167. */
  13168. void pc_macro_captcha_register_upload(map_session_data &sd, uint16 upload_size, char *upload_data) {
  13169. nullpo_retv(upload_data);
  13170. memcpy(&sd.captcha_upload.cd->image_data[sd.captcha_upload.upload_size], upload_data, upload_size);
  13171. sd.captcha_upload.upload_size += upload_size;
  13172. // Notify that the image finished uploading.
  13173. if (sd.captcha_upload.upload_size == sd.captcha_upload.cd->image_size) {
  13174. // Tell the client that the upload was finished
  13175. clif_captcha_upload_end(sd);
  13176. // Look for a free key
  13177. uint16 index;
  13178. for (index = 0; index < UINT16_MAX; index++) {
  13179. if (!captcha_db.exists(index)) {
  13180. break;
  13181. }
  13182. }
  13183. if (index == UINT16_MAX) {
  13184. // no free key found...
  13185. sd.captcha_upload.cd = nullptr;
  13186. sd.captcha_upload.upload_size = 0;
  13187. return;
  13188. }
  13189. captcha_db.put(index, sd.captcha_upload.cd);
  13190. sd.captcha_upload.cd = nullptr;
  13191. sd.captcha_upload.upload_size = 0;
  13192. // TODO: write YAML and BMP file?
  13193. }
  13194. }
  13195. /**
  13196. * Timer attached to target player with attempts to confirm captcha.
  13197. */
  13198. TIMER_FUNC(pc_macro_detector_timeout) {
  13199. map_session_data *sd = map_id2sd(id);
  13200. nullpo_ret(sd);
  13201. // Remove the current timer
  13202. sd->macro_detect.timer = INVALID_TIMER;
  13203. // Deduct an answering attempt
  13204. sd->macro_detect.retry -= 1;
  13205. if (sd->macro_detect.retry == 0) {
  13206. // All attempts have been exhausted, punish the user
  13207. pc_macro_punishment(*sd, MCD_TIMEOUT);
  13208. } else {
  13209. // Update the client
  13210. clif_macro_detector_request_show(*sd);
  13211. // Start a new timer
  13212. sd->macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd->bl.id, 0);
  13213. }
  13214. return 0;
  13215. }
  13216. /**
  13217. * Check player's captcha answer.
  13218. * @param sd: Player data
  13219. * @param captcha_answer: Captcha answer entered by player
  13220. */
  13221. void pc_macro_detector_process_answer(map_session_data &sd, char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13222. nullpo_retv(captcha_answer);
  13223. const std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  13224. // Has no captcha request
  13225. if (cd == nullptr) {
  13226. return;
  13227. }
  13228. // Correct answer
  13229. if (strcmp(captcha_answer, cd->captcha_answer) == 0) {
  13230. // Delete the timer
  13231. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13232. // Clear the macro detect data
  13233. sd.macro_detect = {};
  13234. sd.macro_detect.timer = INVALID_TIMER;
  13235. // Unblock all actions for the player
  13236. sd.state.block_action &= ~PCBLOCK_ALL;
  13237. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13238. // Assign temporary macro variable to check failures
  13239. pc_setreg(&sd, add_str("@captcha_retries"), battle_config.macro_detection_retry - sd.macro_detect.retry);
  13240. // Grant bonuses via script
  13241. run_script(cd->bonus_script, 0, sd.bl.id, fake_nd->bl.id);
  13242. // Notify the client
  13243. clif_macro_detector_status(sd, MCD_GOOD);
  13244. } else {
  13245. // Deduct an answering attempt
  13246. sd.macro_detect.retry -= 1;
  13247. // All attempts have been exhausted, punish the user
  13248. if (sd.macro_detect.retry <= 0) {
  13249. pc_macro_punishment(sd, MCD_INCORRECT);
  13250. return;
  13251. }
  13252. // Incorrect response, update the client
  13253. clif_macro_detector_request_show(sd);
  13254. // Reset the timer
  13255. addtick_timer(sd.macro_detect.timer, gettick() + battle_config.macro_detection_timeout);
  13256. }
  13257. }
  13258. /**
  13259. * Determine if a player tries to log out during a captcha check.
  13260. * @param sd: Player data
  13261. */
  13262. void pc_macro_detector_disconnect(map_session_data &sd) {
  13263. // Delete the timeout timer
  13264. if (sd.macro_detect.timer != INVALID_TIMER) {
  13265. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13266. sd.macro_detect.timer = INVALID_TIMER;
  13267. }
  13268. // If the player disconnects before clearing the challenge the player is punished.
  13269. if (sd.macro_detect.retry != 0)
  13270. pc_macro_punishment(sd, MCD_TIMEOUT);
  13271. }
  13272. /**
  13273. * Save a list of players from an area select via /macro_detector.
  13274. */
  13275. int pc_macro_reporter_area_select_sub(block_list *bl, va_list ap) {
  13276. nullpo_retr(0, bl);
  13277. if (bl->type != BL_PC)
  13278. return 0;
  13279. std::vector<uint32> *aid_list = va_arg(ap, std::vector<uint32> *);
  13280. nullpo_ret(aid_list);
  13281. aid_list->push_back(bl->id);
  13282. return 0;
  13283. }
  13284. /**
  13285. * Area select via /macro_detector.
  13286. * @param sd: Player data
  13287. * @param x: X location
  13288. * @param y: Y location
  13289. * @param radius: Area
  13290. */
  13291. void pc_macro_reporter_area_select(map_session_data &sd, const int16 x, const int16 y, const int8 radius) {
  13292. std::vector<uint32> aid_list;
  13293. map_foreachinarea(pc_macro_reporter_area_select_sub, sd.bl.m, x - radius, y - radius, x + radius, y + radius, BL_PC, &aid_list);
  13294. clif_macro_reporter_select(sd, aid_list);
  13295. }
  13296. /**
  13297. * Send out captcha check to player.
  13298. * @param sd: Target player data
  13299. * @param reporter_account_id: Account ID of reporter
  13300. */
  13301. void pc_macro_reporter_process(map_session_data &sd, int32 reporter_account_id) {
  13302. if (captcha_db.empty())
  13303. return;
  13304. // Pick a random image from the database.
  13305. const std::shared_ptr<s_captcha_data> cd = captcha_db.random();
  13306. // Set macro detection data.
  13307. sd.macro_detect.cd = cd;
  13308. sd.macro_detect.reporter_aid = reporter_account_id;
  13309. sd.macro_detect.retry = battle_config.macro_detection_retry;
  13310. // Block all actions for the target player.
  13311. sd.state.block_action |= (PCBLOCK_ALL | PCBLOCK_IMMUNE);
  13312. // Open macro detect client side.
  13313. clif_macro_detector_request(sd);
  13314. // Start the timeout timer.
  13315. sd.macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd.bl.id, 0);
  13316. }
  13317. /**
  13318. * Parse a BMP image to memory.
  13319. * @param filepath: Image file location
  13320. * @param cd: Captcha data
  13321. */
  13322. bool pc_macro_read_captcha_db_loadbmp(const std::string &filepath, std::shared_ptr<s_captcha_data> cd) {
  13323. if (cd == nullptr)
  13324. return false;
  13325. FILE *fp = fopen(filepath.c_str(), "rb");
  13326. if (fp == nullptr) {
  13327. ShowError("%s: Failed to open file \"%s\"\n", __func__, filepath.c_str());
  13328. return false;
  13329. }
  13330. // Load the file data and verify magic
  13331. char bmp_data[CAPTCHA_BMP_SIZE];
  13332. if (fread(bmp_data, CAPTCHA_BMP_SIZE, 1, fp) != 1) {
  13333. ShowError("%s: Failed to read data from \"%s\"\n", __func__, filepath.c_str());
  13334. fclose(fp);
  13335. return false;
  13336. }
  13337. fclose(fp);
  13338. if (bmp_data[0] != 'B' || bmp_data[1] != 'M') {
  13339. ShowError("%s: Invalid BMP file header given at \"%s\"\n", __func__, filepath.c_str());
  13340. return false;
  13341. }
  13342. // Compress the data into the destination
  13343. unsigned long com_size = sizeof(cd->image_data);
  13344. encode_zip(cd->image_data, &com_size, bmp_data, CAPTCHA_BMP_SIZE);
  13345. cd->image_size = static_cast<int16>(com_size);
  13346. return true;
  13347. }
  13348. const std::string CaptchaDatabase::getDefaultLocation() {
  13349. return std::string(db_path) + "/captcha_db.yml";
  13350. }
  13351. /**
  13352. * Reads and parses an entry from the captcha_db.
  13353. * @param node: YAML node containing the entry.
  13354. * @return count of successfully parsed rows
  13355. */
  13356. uint64 CaptchaDatabase::parseBodyNode(const ryml::NodeRef &node) {
  13357. uint16 index;
  13358. if (!this->asUInt16(node, "Id", index))
  13359. return 0;
  13360. std::shared_ptr<s_captcha_data> cd = captcha_db.find(index);
  13361. bool exists = cd != nullptr;
  13362. if (!exists) {
  13363. if (!this->nodesExist(node, { "Filename", "Answer" }))
  13364. return 0;
  13365. cd = std::make_shared<s_captcha_data>();
  13366. cd->index = index;
  13367. }
  13368. if (this->nodeExists(node, "Filename")) {
  13369. std::string filename;
  13370. if (!this->asString(node, "Filename", filename))
  13371. return 0;
  13372. if (!pc_macro_read_captcha_db_loadbmp(filename, cd)) {
  13373. this->invalidWarning(node["Filename"], "Failed to parse BMP image, skipping...\n");
  13374. return 0;
  13375. }
  13376. }
  13377. if (this->nodeExists(node, "Answer")) {
  13378. std::string answer;
  13379. if (!this->asString(node, "Answer", answer))
  13380. return 0;
  13381. if (answer.length() < 4 || answer.length() > CAPTCHA_ANSWER_SIZE) {
  13382. this->invalidWarning(node["Answer"], "The captcha answer must be between 4~%d characters, skipping...", CAPTCHA_ANSWER_SIZE);
  13383. return 0;
  13384. }
  13385. safestrncpy(cd->captcha_answer, answer.c_str(), sizeof(cd->captcha_answer));
  13386. }
  13387. if (this->nodeExists(node, "Bonus")) {
  13388. std::string script;
  13389. if (!this->asString(node, "Bonus", script)) {
  13390. return 0;
  13391. }
  13392. if (cd->bonus_script) {
  13393. script_free_code(cd->bonus_script);
  13394. cd->bonus_script = nullptr;
  13395. }
  13396. cd->bonus_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Bonus"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  13397. } else {
  13398. if (!exists)
  13399. 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);
  13400. }
  13401. if (!exists)
  13402. captcha_db.put(index, cd);
  13403. return 1;
  13404. }
  13405. /*==========================================
  13406. * pc Init/Terminate
  13407. *------------------------------------------*/
  13408. void do_final_pc(void) {
  13409. db_destroy(itemcd_db);
  13410. do_final_pc_groups();
  13411. ers_destroy(pc_sc_display_ers);
  13412. ers_destroy(num_reg_ers);
  13413. ers_destroy(str_reg_ers);
  13414. attendance_db.clear();
  13415. reputation_db.clear();
  13416. #ifdef MAP_GENERATOR
  13417. reputationgroup_db.clear();
  13418. #endif
  13419. penalty_db.clear();
  13420. captcha_db.clear();
  13421. }
  13422. void do_init_pc(void) {
  13423. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  13424. pc_readdb();
  13425. pc_read_motd(); // Read MOTD [Valaris]
  13426. attendance_db.load();
  13427. reputation_db.load();
  13428. #ifdef MAP_GENERATOR
  13429. reputationgroup_db.load();
  13430. #endif
  13431. captcha_db.load();
  13432. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  13433. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  13434. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  13435. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  13436. add_timer_func_list(pc_autosave, "pc_autosave");
  13437. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  13438. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  13439. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  13440. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  13441. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  13442. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  13443. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  13444. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  13445. add_timer_func_list(pc_macro_detector_timeout, "pc_macro_detector_timeout");
  13446. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  13447. // 0=day, 1=night [Yor]
  13448. night_flag = battle_config.night_at_start ? 1 : 0;
  13449. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  13450. int day_duration = battle_config.day_duration;
  13451. int night_duration = battle_config.night_duration;
  13452. // add night/day timer [Yor]
  13453. add_timer_func_list(map_day_timer, "map_day_timer");
  13454. add_timer_func_list(map_night_timer, "map_night_timer");
  13455. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  13456. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  13457. }
  13458. do_init_pc_groups();
  13459. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  13460. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13461. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13462. ers_chunk_size(pc_sc_display_ers, 150);
  13463. ers_chunk_size(num_reg_ers, 300);
  13464. ers_chunk_size(str_reg_ers, 50);
  13465. }