pc.cpp 463 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481
  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. #include "../common/cbasetypes.hpp"
  8. #include "../common/core.hpp" // get_svn_revision()
  9. #include "../common/database.hpp"
  10. #include "../common/ers.hpp" // ers_destroy
  11. #include "../common/grfio.hpp"
  12. #include "../common/malloc.hpp"
  13. #include "../common/mmo.hpp" //NAME_LENGTH
  14. #include "../common/nullpo.hpp"
  15. #include "../common/random.hpp"
  16. #include "../common/showmsg.hpp"
  17. #include "../common/socket.hpp" // session[]
  18. #include "../common/strlib.hpp" // safestrncpy()
  19. #include "../common/timer.hpp"
  20. #include "../common/utilities.hpp"
  21. #include "../common/utils.hpp"
  22. #include "achievement.hpp"
  23. #include "atcommand.hpp" // get_atcommand_level()
  24. #include "battle.hpp" // battle_config
  25. #include "battleground.hpp"
  26. #include "buyingstore.hpp" // struct s_buyingstore
  27. #include "channel.hpp"
  28. #include "chat.hpp"
  29. #include "chrif.hpp"
  30. #include "clan.hpp"
  31. #include "clif.hpp"
  32. #include "date.hpp" // is_day_of_*()
  33. #include "duel.hpp"
  34. #include "elemental.hpp"
  35. #include "guild.hpp"
  36. #include "homunculus.hpp"
  37. #include "instance.hpp"
  38. #include "intif.hpp"
  39. #include "itemdb.hpp" // MAX_ITEMGROUP
  40. #include "log.hpp"
  41. #include "map.hpp"
  42. #include "mercenary.hpp"
  43. #include "mob.hpp"
  44. #include "npc.hpp"
  45. #include "party.hpp" // party_search()
  46. #include "pc_groups.hpp"
  47. #include "pet.hpp" // pet_unlocktarget()
  48. #include "quest.hpp"
  49. #include "skill.hpp" // skill_isCopyable()
  50. #include "script.hpp" // struct script_reg, struct script_regstr
  51. #include "searchstore.hpp" // struct s_search_store_info
  52. #include "status.hpp" // OPTION_*, struct weapon_atk
  53. #include "storage.hpp"
  54. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  55. #include "vending.hpp" // struct s_vending
  56. using namespace rathena;
  57. JobDatabase job_db;
  58. CaptchaDatabase captcha_db;
  59. const char *macro_allowed_answer_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  60. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  61. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
  62. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  63. PlayerStatPointDatabase statpoint_db;
  64. SkillTreeDatabase skill_tree_db;
  65. // timer for night.day implementation
  66. int day_timer_tid = INVALID_TIMER;
  67. int night_timer_tid = INVALID_TIMER;
  68. struct eri *pc_sc_display_ers = NULL;
  69. struct eri *num_reg_ers;
  70. struct eri *str_reg_ers;
  71. int pc_expiration_tid = INVALID_TIMER;
  72. struct fame_list smith_fame_list[MAX_FAME_LIST];
  73. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  74. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  75. const std::string AttendanceDatabase::getDefaultLocation(){
  76. return std::string(db_path) + "/attendance.yml";
  77. }
  78. /**
  79. * Reads and parses an entry from the attendance_db.
  80. * @param node: YAML node containing the entry.
  81. * @return count of successfully parsed rows
  82. */
  83. uint64 AttendanceDatabase::parseBodyNode(const ryml::NodeRef& node){
  84. uint32 start;
  85. if( !this->asUInt32( node, "Start", start ) ){
  86. return 0;
  87. }
  88. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  89. bool exists = attendance_period != nullptr;
  90. if( !exists ){
  91. if( !this->nodeExists( node, "End" ) ){
  92. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  93. return 0;
  94. }
  95. if( !this->nodeExists( node, "Rewards" ) ){
  96. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  97. return 0;
  98. }
  99. attendance_period = std::make_shared<s_attendance_period>();
  100. attendance_period->start = start;
  101. }
  102. // If it does not exist yet, we need to check it for sure
  103. bool requiresCollisionDetection = !exists;
  104. if( this->nodeExists( node, "End" ) ){
  105. uint32 end;
  106. if( !this->asUInt32( node, "End", end ) ){
  107. return 0;
  108. }
  109. // If the period is outdated already, we do not even bother parsing
  110. if( end < date_get( DT_YYYYMMDD ) ){
  111. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  112. return 0;
  113. }
  114. if( !exists || attendance_period->end != end ){
  115. requiresCollisionDetection = true;
  116. attendance_period->end = end;
  117. }
  118. }
  119. // Collision detection
  120. if( requiresCollisionDetection ){
  121. bool collision = false;
  122. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  123. std::shared_ptr<s_attendance_period> period = pair.second;
  124. if( exists && period->start == attendance_period->start ){
  125. // Dont compare to yourself
  126. continue;
  127. }
  128. // Check if start is inside another period
  129. if( period->start <= attendance_period->start && start <= period->end ){
  130. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  131. collision = true;
  132. break;
  133. }
  134. // Check if end is inside another period
  135. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  136. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  137. collision = true;
  138. break;
  139. }
  140. }
  141. if( collision ){
  142. return 0;
  143. }
  144. }
  145. if( this->nodeExists( node, "Rewards" ) ){
  146. const auto& rewardsNode = node["Rewards"];
  147. for( const auto& rewardNode : rewardsNode ){
  148. uint32 day;
  149. if( !this->asUInt32( rewardNode, "Day", day ) ){
  150. continue;
  151. }
  152. day -= 1;
  153. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  154. bool reward_exists = reward != nullptr;
  155. if( !reward_exists ){
  156. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  157. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  158. return 0;
  159. }
  160. reward = std::make_shared<s_attendance_reward>();
  161. }
  162. if( this->nodeExists( rewardNode, "ItemId" ) ){
  163. t_itemid item_id;
  164. if( !this->asUInt32( rewardNode, "ItemId", item_id ) ){
  165. continue;
  166. }
  167. if( !item_db.exists( item_id ) ){
  168. ShowError( "pc_attendance_load: Unknown item ID %u for day %d.\n", item_id, day + 1 );
  169. continue;
  170. }
  171. reward->item_id = item_id;
  172. }
  173. if( this->nodeExists( rewardNode, "Amount" ) ){
  174. uint16 amount;
  175. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  176. continue;
  177. }
  178. if( amount == 0 ){
  179. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  180. amount = 1;
  181. }else if( amount > MAX_AMOUNT ){
  182. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  183. amount = MAX_AMOUNT;
  184. }
  185. reward->amount = amount;
  186. }else{
  187. if( !reward_exists ){
  188. reward->amount = 1;
  189. }
  190. }
  191. if( !reward_exists ){
  192. attendance_period->rewards[day] = reward;
  193. }
  194. }
  195. bool missing_day = false;
  196. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  197. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  198. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  199. missing_day = true;
  200. break;
  201. }
  202. }
  203. if( missing_day ){
  204. return 0;
  205. }
  206. }
  207. if( !exists ){
  208. this->put( start, attendance_period );
  209. }
  210. return 1;
  211. }
  212. AttendanceDatabase attendance_db;
  213. const std::string ReputationDatabase::getDefaultLocation(){
  214. return std::string( db_path ) + "/reputation.yml";
  215. }
  216. uint64 ReputationDatabase::parseBodyNode( const ryml::NodeRef& node ){
  217. int64 id;
  218. if( !this->asInt64( node, "Id", id ) ){
  219. return 0;
  220. }
  221. std::shared_ptr<s_reputation> reputation = this->find( id );
  222. bool exists = reputation != nullptr;
  223. if( !exists ){
  224. if( !this->nodesExist( node, { "Name", "Variable" } ) ){
  225. return 0;
  226. }
  227. reputation = std::make_shared<s_reputation>();
  228. reputation->id = id;
  229. }
  230. if( this->nodeExists( node, "Name" ) ){
  231. std::string name;
  232. if( !this->asString( node, "Name", name ) ){
  233. return 0;
  234. }
  235. reputation->name = name;
  236. }
  237. if( this->nodeExists( node, "Variable" ) ){
  238. std::string variable;
  239. if( !this->asString( node, "Variable", variable ) ){
  240. return 0;
  241. }
  242. if( variable.length() > 32 ){
  243. this->invalidWarning( node, "Variable name \"%s\" exceeds maximum length 32.\n", variable.c_str() );
  244. return 0;
  245. }
  246. reputation->variable = variable;
  247. }
  248. if( this->nodeExists( node, "Minimum" ) ){
  249. int64 minimum;
  250. if( !this->asInt64( node, "Minimum", minimum ) ){
  251. return 0;
  252. }
  253. reputation->minimum = minimum;
  254. }else{
  255. if( !exists ){
  256. reputation->minimum = INT64_MIN;
  257. }
  258. }
  259. if( this->nodeExists( node, "Maximum" ) ){
  260. int64 maximum;
  261. if( !this->asInt64( node, "Maximum", maximum ) ){
  262. return 0;
  263. }
  264. reputation->maximum = maximum;
  265. }else{
  266. if( !exists ){
  267. reputation->maximum = INT64_MIN;
  268. }
  269. }
  270. if( !exists ){
  271. this->put( id, reputation );
  272. }
  273. return 1;
  274. }
  275. ReputationDatabase reputation_db;
  276. const std::string PenaltyDatabase::getDefaultLocation(){
  277. return std::string( db_path ) + "/level_penalty.yml";
  278. }
  279. uint64 PenaltyDatabase::parseBodyNode(const ryml::NodeRef& node){
  280. std::string type_constant;
  281. if( !this->asString( node, "Type", type_constant ) ){
  282. return 0;
  283. }
  284. int64 constant_value;
  285. if( !script_get_constant( ( "PENALTY_" + type_constant ).c_str(), &constant_value ) ){
  286. this->invalidWarning( node["Type"], "Unknown penalty type \"%s\".\n", type_constant.c_str() );
  287. return 0;
  288. }
  289. if( constant_value < PENALTY_NONE || constant_value > PENALTY_MAX ){
  290. this->invalidWarning( node["Type"], "Invalid penalty type \"%s\".\n", type_constant.c_str() );
  291. return 0;
  292. }
  293. e_penalty_type type = static_cast<e_penalty_type>( constant_value );
  294. std::shared_ptr<s_penalty> penalty = this->find( type );
  295. bool exists = penalty != nullptr;
  296. if( !exists ){
  297. penalty = std::make_shared<s_penalty>();
  298. penalty->type = type;
  299. for( int i = 0, max = ARRAYLENGTH( penalty->rate ); i < max; i++ ){
  300. penalty->rate[i] = UINT16_MAX;
  301. }
  302. }
  303. if( this->nodeExists( node, "LevelDifferences" ) ){
  304. for( const auto& levelNode : node["LevelDifferences"] ){
  305. if( !this->nodesExist( levelNode, { "Difference", "Rate" } ) ){
  306. return 0;
  307. }
  308. int32 difference;
  309. if( !this->asInt32( levelNode, "Difference", difference ) ){
  310. return 0;
  311. }
  312. if( std::abs( difference ) > MAX_LEVEL ){
  313. this->invalidWarning( levelNode["Difference"], "Level difference %d is bigger than maximum level %d.\n", difference, MAX_LEVEL );
  314. return 0;
  315. }
  316. uint16 rate;
  317. if( !this->asUInt16Rate( levelNode, "Rate", rate ) ){
  318. return 0;
  319. }
  320. penalty->rate[difference + MAX_LEVEL - 1] = rate;
  321. }
  322. }
  323. if( !exists ){
  324. this->put( type, penalty );
  325. }
  326. return 1;
  327. }
  328. void PenaltyDatabase::loadingFinished(){
  329. for( const auto& pair : *this ){
  330. for( int i = MAX_LEVEL - 1, max = ARRAYLENGTH( pair.second->rate ), last_rate = 100; i < max; i++ ){
  331. uint16 rate = pair.second->rate[i];
  332. // Check if it has been defined
  333. if( rate == UINT16_MAX ){
  334. pair.second->rate[i] = last_rate;
  335. }else{
  336. last_rate = rate;
  337. }
  338. }
  339. for( int i = MAX_LEVEL - 1, last_rate = 100; i >= 0; i-- ){
  340. uint16 rate = pair.second->rate[i];
  341. // Check if it has been defined
  342. if( rate == UINT16_MAX ){
  343. pair.second->rate[i] = last_rate;
  344. }else{
  345. last_rate = rate;
  346. }
  347. }
  348. }
  349. TypesafeYamlDatabase::loadingFinished();
  350. }
  351. PenaltyDatabase penalty_db;
  352. #define MOTD_LINE_SIZE 128
  353. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  354. bool reg_load;
  355. /**
  356. * Translation table from athena equip index to aegis bitmask
  357. */
  358. unsigned int equip_bitmask[EQI_MAX] = {
  359. EQP_ACC_L, // EQI_ACC_L
  360. EQP_ACC_R, // EQI_ACC_R
  361. EQP_SHOES, // EQI_SHOES
  362. EQP_GARMENT, // EQI_GARMENT
  363. EQP_HEAD_LOW, // EQI_HEAD_LOW
  364. EQP_HEAD_MID, // EQI_HEAD_MID
  365. EQP_HEAD_TOP, // EQI_HEAD_TOP
  366. EQP_ARMOR, // EQI_ARMOR
  367. EQP_HAND_L, // EQI_HAND_L
  368. EQP_HAND_R, // EQI_HAND_R
  369. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  370. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  371. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  372. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  373. EQP_AMMO, // EQI_AMMO
  374. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  375. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  376. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  377. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  378. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  379. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  380. };
  381. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  382. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  383. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  384. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  385. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  386. };
  387. void pc_set_reg_load( bool val ){
  388. reg_load = val;
  389. }
  390. /**
  391. * Item Cool Down Delay Saving
  392. * Struct item_cd is not a member of struct map_session_data
  393. * to keep cooldowns in memory between player log-ins.
  394. * All cooldowns are reset when server is restarted.
  395. **/
  396. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  397. struct item_cd {
  398. t_tick tick[MAX_ITEMDELAYS]; //tick
  399. t_itemid nameid[MAX_ITEMDELAYS]; //item id
  400. };
  401. /**
  402. * Converts a class to its array index for CLASS_COUNT defined arrays.
  403. * Note that it does not do a validity check for speed purposes, where parsing
  404. * player input make sure to use a pcdb_checkid first!
  405. * @param class_ Job ID see enum e_job
  406. * @return Class Index
  407. */
  408. int pc_class2idx(int class_) {
  409. if (class_ >= JOB_NOVICE_HIGH)
  410. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  411. return class_;
  412. }
  413. /**
  414. * Get player's group ID
  415. * @param sd
  416. * @return Group ID
  417. */
  418. int pc_get_group_id(struct map_session_data *sd) {
  419. return sd->group_id;
  420. }
  421. /** Get player's group Level
  422. * @param sd
  423. * @return Group Level
  424. */
  425. int pc_get_group_level(struct map_session_data *sd) {
  426. return sd->group->level;
  427. }
  428. /**
  429. * Remove a player from queue after timeout
  430. * @param tid: Timer ID
  431. * @param tick: Timer
  432. * @param id: ID
  433. * @return 0 on success or 1 otherwise
  434. */
  435. static TIMER_FUNC(pc_on_expire_active)
  436. {
  437. map_session_data *sd = (map_session_data *)data;
  438. nullpo_retr(1, sd);
  439. sd->tid_queue_active = INVALID_TIMER;
  440. bg_queue_leave(sd);
  441. clif_bg_queue_entry_init(sd);
  442. return 0;
  443. }
  444. /**
  445. * Function used to set timer externally
  446. * @param sd: Player data
  447. */
  448. void pc_set_bg_queue_timer(map_session_data *sd) {
  449. nullpo_retv(sd);
  450. if (sd->tid_queue_active != INVALID_TIMER) {
  451. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  452. sd->tid_queue_active = INVALID_TIMER;
  453. }
  454. sd->tid_queue_active = add_timer(gettick() + 20000, pc_on_expire_active, 0, (intptr_t)sd);
  455. }
  456. /**
  457. * Function used to delete timer externally
  458. * @param sd: Player data
  459. */
  460. void pc_delete_bg_queue_timer(map_session_data *sd) {
  461. nullpo_retv(sd);
  462. if (sd->tid_queue_active != INVALID_TIMER) {
  463. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  464. sd->tid_queue_active = INVALID_TIMER;
  465. }
  466. }
  467. static TIMER_FUNC(pc_invincible_timer){
  468. struct map_session_data *sd;
  469. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  470. return 1;
  471. if(sd->invincible_timer != tid){
  472. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  473. return 0;
  474. }
  475. sd->invincible_timer = INVALID_TIMER;
  476. skill_unit_move(&sd->bl,tick,1);
  477. return 0;
  478. }
  479. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  480. nullpo_retv(sd);
  481. if( sd->invincible_timer != INVALID_TIMER )
  482. delete_timer(sd->invincible_timer,pc_invincible_timer);
  483. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  484. }
  485. void pc_delinvincibletimer(struct map_session_data* sd)
  486. {
  487. nullpo_retv(sd);
  488. if( sd->invincible_timer != INVALID_TIMER )
  489. {
  490. delete_timer(sd->invincible_timer,pc_invincible_timer);
  491. sd->invincible_timer = INVALID_TIMER;
  492. skill_unit_move(&sd->bl,gettick(),1);
  493. }
  494. }
  495. static TIMER_FUNC(pc_spiritball_timer){
  496. struct map_session_data *sd;
  497. int i;
  498. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  499. return 1;
  500. if( sd->spiritball <= 0 )
  501. {
  502. 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);
  503. sd->spiritball = 0;
  504. return 0;
  505. }
  506. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  507. if( i == sd->spiritball )
  508. {
  509. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  510. return 0;
  511. }
  512. sd->spiritball--;
  513. if( i != sd->spiritball )
  514. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  515. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  516. clif_spiritball(&sd->bl);
  517. return 0;
  518. }
  519. /**
  520. * Adds a spiritball to player for 'interval' ms
  521. * @param sd
  522. * @param interval
  523. * @param max
  524. */
  525. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  526. {
  527. int tid;
  528. uint8 i;
  529. nullpo_retv(sd);
  530. if(max > MAX_SPIRITBALL)
  531. max = MAX_SPIRITBALL;
  532. if(sd->spiritball < 0)
  533. sd->spiritball = 0;
  534. if( sd->spiritball && sd->spiritball >= max )
  535. {
  536. if(sd->spirit_timer[0] != INVALID_TIMER)
  537. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  538. sd->spiritball--;
  539. if( sd->spiritball != 0 )
  540. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  541. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  542. }
  543. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  544. 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);
  545. if( i != sd->spiritball )
  546. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  547. sd->spirit_timer[i] = tid;
  548. sd->spiritball++;
  549. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  550. clif_millenniumshield(&sd->bl,sd->spiritball);
  551. else
  552. clif_spiritball(&sd->bl);
  553. }
  554. /**
  555. * Removes number of spiritball from player
  556. * @param sd
  557. * @param count
  558. * @param type 1 = doesn't give client effect
  559. */
  560. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  561. {
  562. uint8 i;
  563. nullpo_retv(sd);
  564. if(sd->spiritball <= 0) {
  565. sd->spiritball = 0;
  566. return;
  567. }
  568. if(count == 0)
  569. return;
  570. if(count > sd->spiritball)
  571. count = sd->spiritball;
  572. sd->spiritball -= count;
  573. if(count > MAX_SPIRITBALL)
  574. count = MAX_SPIRITBALL;
  575. for(i=0;i<count;i++) {
  576. if(sd->spirit_timer[i] != INVALID_TIMER) {
  577. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  578. sd->spirit_timer[i] = INVALID_TIMER;
  579. }
  580. }
  581. for(i=count;i<MAX_SPIRITBALL;i++) {
  582. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  583. sd->spirit_timer[i] = INVALID_TIMER;
  584. }
  585. if(!type) {
  586. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  587. clif_millenniumshield(&sd->bl,sd->spiritball);
  588. else
  589. clif_spiritball(&sd->bl);
  590. }
  591. }
  592. /**
  593. * Adds a soulball to player
  594. * @param sd: Player data
  595. * @param max: Max amount of soulballs
  596. */
  597. int pc_addsoulball(map_session_data *sd, int max)
  598. {
  599. nullpo_ret(sd);
  600. status_change *sc = status_get_sc(&sd->bl);
  601. if (sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  602. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, 0, skill_get_time2(SP_SOULCOLLECT, 1));
  603. sd->soulball = 0;
  604. }
  605. max = min(max, MAX_SOUL_BALL);
  606. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  607. if (sd->soulball && sd->soulball >= max)
  608. sd->soulball--;
  609. sd->soulball++;
  610. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, sd->soulball, skill_get_time2(SP_SOULCOLLECT, 1));
  611. clif_soulball(sd);
  612. return 0;
  613. }
  614. /**
  615. * Removes number of soulball from player
  616. * @param sd: Player data
  617. * @param count: Amount to remove
  618. * @param type: true = doesn't give client effect
  619. */
  620. int pc_delsoulball(map_session_data *sd, int count, bool type)
  621. {
  622. nullpo_ret(sd);
  623. if (count <= 0)
  624. return 0;
  625. status_change *sc = status_get_sc(&sd->bl);
  626. if (sd->soulball <= 0 || sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  627. sd->soulball = 0;
  628. }else{
  629. sd->soulball -= cap_value(count, 0, sd->soulball);
  630. if (sd->soulball == 0)
  631. status_change_end(&sd->bl, SC_SOULENERGY);
  632. else
  633. sc->data[SC_SOULENERGY]->val1 = sd->soulball;
  634. }
  635. if (!type)
  636. clif_soulball(sd);
  637. return 0;
  638. }
  639. /**
  640. * Adds servantballs to a player
  641. * @param sd: Player data
  642. * @param amount: Amount to add
  643. */
  644. void pc_addservantball( struct map_session_data& sd, int count ){
  645. sd.servantball = cap_value( sd.servantball + count, 0, MAX_SERVANTBALL );
  646. clif_servantball( sd );
  647. }
  648. /**
  649. * Removes number of servantballs from player
  650. * @param sd: Player data
  651. * @param count: Amount to remove
  652. */
  653. void pc_delservantball( struct map_session_data& sd, int count ){
  654. sd.servantball = cap_value( sd.servantball - count, 0, MAX_SERVANTBALL );
  655. clif_servantball( sd );
  656. }
  657. /**
  658. * Adds abyssballs to a player
  659. * @param sd: Player data
  660. * @param amount: Amount to add
  661. */
  662. void pc_addabyssball( struct map_session_data& sd, int count ){
  663. sd.abyssball = cap_value( sd.abyssball + count, 0, MAX_ABYSSBALL );
  664. clif_abyssball( sd );
  665. }
  666. /**
  667. * Removes number of abyssballs from player
  668. * @param sd: Player data
  669. * @param count: Amount to remove
  670. */
  671. void pc_delabyssball( struct map_session_data& sd, int count ){
  672. sd.abyssball = cap_value( sd.abyssball - count, 0, MAX_ABYSSBALL );
  673. clif_abyssball( sd );
  674. }
  675. /**
  676. * Increases a player's fame points and displays a notice to him
  677. * @param sd Player
  678. * @param count Fame point
  679. * @return true: on success, false: on error
  680. */
  681. bool pc_addfame(map_session_data &sd, int count)
  682. {
  683. enum e_rank ranktype;
  684. sd.status.fame += count;
  685. if (sd.status.fame > MAX_FAME)
  686. sd.status.fame = MAX_FAME;
  687. switch(sd.class_&MAPID_UPPERMASK){
  688. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  689. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  690. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  691. default:
  692. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd.status.class_), sd.status.class_ );
  693. return false;
  694. }
  695. clif_update_rankingpoint(sd, ranktype, count);
  696. chrif_updatefamelist(sd, ranktype);
  697. return true;
  698. }
  699. /**
  700. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  701. * @param sd
  702. * @param job Job use enum e_mapid
  703. * @return Rank
  704. */
  705. unsigned char pc_famerank(uint32 char_id, int job)
  706. {
  707. uint8 i;
  708. switch(job){
  709. case MAPID_BLACKSMITH: // Blacksmith
  710. for(i = 0; i < MAX_FAME_LIST; i++){
  711. if(smith_fame_list[i].id == char_id)
  712. return i + 1;
  713. }
  714. break;
  715. case MAPID_ALCHEMIST: // Alchemist
  716. for(i = 0; i < MAX_FAME_LIST; i++){
  717. if(chemist_fame_list[i].id == char_id)
  718. return i + 1;
  719. }
  720. break;
  721. case MAPID_TAEKWON: // Taekwon
  722. for(i = 0; i < MAX_FAME_LIST; i++){
  723. if(taekwon_fame_list[i].id == char_id)
  724. return i + 1;
  725. }
  726. break;
  727. }
  728. return 0;
  729. }
  730. /**
  731. * Restart player's HP & SP value
  732. * @param sd
  733. * @param type Restart type: 1 - Normal Resurection
  734. */
  735. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  736. struct status_data *status, *b_status;
  737. nullpo_retv(sd);
  738. b_status = &sd->base_status;
  739. status = &sd->battle_status;
  740. if (type&1) { //Normal resurrection
  741. status->hp = 1; //Otherwise status_heal may fail if dead.
  742. status_heal(&sd->bl, b_status->hp, 0, 1);
  743. if( status->sp < b_status->sp )
  744. status_set_sp(&sd->bl, b_status->sp, 1);
  745. } else { //Just for saving on the char-server (with values as if respawned)
  746. sd->status.hp = b_status->hp;
  747. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  748. sd->status.ap = (status->ap < b_status->ap)?b_status->ap:status->ap;
  749. }
  750. }
  751. /*==========================================
  752. Rental System
  753. *------------------------------------------*/
  754. /**
  755. * Ends a rental and removes the item/effect
  756. * @param tid: Tick ID
  757. * @param tick: Timer
  758. * @param id: Timer ID
  759. * @param data: Data
  760. * @return false - failure, true - success
  761. */
  762. TIMER_FUNC(pc_inventory_rental_end){
  763. struct map_session_data *sd = map_id2sd(id);
  764. if( sd == NULL )
  765. return 0;
  766. if( tid != sd->rental_timer ) {
  767. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  768. return 0;
  769. }
  770. pc_inventory_rentals(sd);
  771. return 1;
  772. }
  773. /**
  774. * Removes the rental timer from the player
  775. * @param sd: Player data
  776. */
  777. void pc_inventory_rental_clear(struct map_session_data *sd)
  778. {
  779. if( sd->rental_timer != INVALID_TIMER ) {
  780. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  781. sd->rental_timer = INVALID_TIMER;
  782. }
  783. }
  784. /**
  785. * Check for items in the player's inventory that are rental type
  786. * @param sd: Player data
  787. */
  788. void pc_inventory_rentals(struct map_session_data *sd)
  789. {
  790. int i, c = 0;
  791. unsigned int next_tick = UINT_MAX;
  792. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  793. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  794. continue; // Nothing here
  795. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  796. continue;
  797. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  798. if (sd->inventory_data[i]->unequip_script)
  799. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  800. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  801. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  802. } else {
  803. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  804. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  805. next_tick = umin(expire_tick * 1000U, next_tick);
  806. c++;
  807. }
  808. }
  809. 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
  810. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  811. else
  812. sd->rental_timer = INVALID_TIMER;
  813. }
  814. /**
  815. * Add a rental item to the player and adjusts the rental timer appropriately
  816. * @param sd: Player data
  817. * @param seconds: Rental time
  818. */
  819. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  820. {
  821. t_tick tick = seconds * 1000;
  822. if( sd == NULL )
  823. return;
  824. if( sd->rental_timer != INVALID_TIMER ) {
  825. const struct TimerData * td;
  826. td = get_timer(sd->rental_timer);
  827. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  828. pc_inventory_rental_clear(sd);
  829. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  830. }
  831. } else
  832. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  833. }
  834. /**
  835. * Check if the player can sell the current item
  836. * @param sd: map_session_data of the player
  837. * @param item: struct of the checking item
  838. * @param shoptype: NPC's sub type see enum npc_subtype
  839. * @return bool 'true' is sellable, 'false' otherwise
  840. */
  841. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  842. if (sd == NULL || item == NULL)
  843. return false;
  844. if (!pc_can_give_items(sd))
  845. return false;
  846. if (item->equip > 0 || item->amount < 0)
  847. return false;
  848. if (battle_config.hide_fav_sell && item->favorite)
  849. return false; //Cannot sell favs (optional config)
  850. if (!battle_config.rental_transaction && item->expire_time)
  851. return false; // Cannot Sell Rental Items
  852. if( item->equipSwitch ){
  853. return false;
  854. }
  855. if (itemdb_ishatched_egg(item))
  856. return false;
  857. switch (shoptype) {
  858. case NPCTYPE_SHOP:
  859. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  860. item->bound != BOUND_GUILD ||
  861. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  862. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  863. ))
  864. return true;
  865. break;
  866. case NPCTYPE_ITEMSHOP:
  867. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  868. item->bound != BOUND_GUILD ||
  869. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  870. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  871. ))
  872. return true;
  873. else if (!item->bound) {
  874. struct item_data *itd = itemdb_search(item->nameid);
  875. if (itd && itd->flag.trade_restriction.sell && battle_config.allow_bound_sell&ISR_SELLABLE)
  876. return true;
  877. }
  878. break;
  879. }
  880. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  881. return false;
  882. if (item->bound && !pc_can_give_bounded_items(sd))
  883. return false; // Don't allow sale of bound items
  884. return true;
  885. }
  886. /**
  887. * Determines if player can give / drop / trade / vend items
  888. */
  889. bool pc_can_give_items(struct map_session_data *sd)
  890. {
  891. return pc_has_permission(sd, PC_PERM_TRADE);
  892. }
  893. /**
  894. * Determines if player can give / drop / trade / vend bounded items
  895. */
  896. bool pc_can_give_bounded_items(struct map_session_data *sd)
  897. {
  898. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  899. }
  900. /**
  901. * Determine if an item in a player's inventory is tradeable based on several merits.
  902. * Checks for item_trade, bound, and rental restrictions.
  903. * @param sd: Player data
  904. * @param index: Item inventory index
  905. * @return True if the item can be traded or false otherwise
  906. */
  907. bool pc_can_trade_item(map_session_data *sd, int index) {
  908. if (sd && index >= 0) {
  909. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  910. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  911. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  912. );
  913. }
  914. return false;
  915. }
  916. /*==========================================
  917. * Prepares character for saving.
  918. * @param sd
  919. *------------------------------------------*/
  920. void pc_makesavestatus(struct map_session_data *sd) {
  921. nullpo_retv(sd);
  922. if(!battle_config.save_clothcolor)
  923. sd->status.clothes_color = 0;
  924. if(!battle_config.save_body_style)
  925. sd->status.body = 0;
  926. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  927. //status change load/saving. [Skotlex]
  928. #ifdef NEW_CARTS
  929. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  930. #else
  931. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  932. #endif
  933. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  934. if(pc_isdead(sd)){
  935. pc_setrestartvalue(sd, 0);
  936. } else {
  937. sd->status.hp = sd->battle_status.hp;
  938. sd->status.sp = sd->battle_status.sp;
  939. sd->status.ap = sd->battle_status.ap;
  940. }
  941. sd->status.last_point.map = sd->mapindex;
  942. sd->status.last_point.x = sd->bl.x;
  943. sd->status.last_point.y = sd->bl.y;
  944. return;
  945. }
  946. if(pc_isdead(sd)) {
  947. pc_setrestartvalue(sd, 0);
  948. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  949. } else {
  950. sd->status.hp = sd->battle_status.hp;
  951. sd->status.sp = sd->battle_status.sp;
  952. sd->status.ap = sd->battle_status.ap;
  953. sd->status.last_point.map = sd->mapindex;
  954. sd->status.last_point.x = sd->bl.x;
  955. sd->status.last_point.y = sd->bl.y;
  956. }
  957. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  958. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  959. if(mapdata->save.map)
  960. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  961. else
  962. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  963. }
  964. }
  965. /*==========================================
  966. * Off init ? Connection?
  967. *------------------------------------------*/
  968. void pc_setnewpc(struct map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, t_tick client_tick, int sex, int fd) {
  969. nullpo_retv(sd);
  970. sd->bl.id = account_id;
  971. sd->status.account_id = account_id;
  972. sd->status.char_id = char_id;
  973. sd->status.sex = sex;
  974. sd->login_id1 = login_id1;
  975. sd->login_id2 = 0; // at this point, we can not know the value :(
  976. sd->client_tick = client_tick;
  977. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  978. sd->bl.type = BL_PC;
  979. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  980. sd->canlog_tick = gettick();
  981. //Required to prevent homunculus copuing a base speed of 0.
  982. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  983. }
  984. /**
  985. * Get equip point for an equip
  986. * @param sd
  987. * @param id
  988. */
  989. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  990. int ep = 0;
  991. nullpo_ret(sd);
  992. nullpo_ret(id);
  993. if (!itemdb_isequip2(id))
  994. return 0; //Not equippable by players.
  995. ep = id->equip;
  996. if(id->subtype == W_DAGGER || id->subtype == W_1HSWORD || id->subtype == W_1HAXE) {
  997. 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]
  998. if (ep == EQP_WEAPON)
  999. return EQP_ARMS;
  1000. if (ep == EQP_SHADOW_WEAPON)
  1001. return EQP_SHADOW_ARMS;
  1002. }
  1003. }
  1004. return ep;
  1005. }
  1006. /**
  1007. * Get equip point for an equip
  1008. * @param sd
  1009. * @param n Equip index in inventory
  1010. */
  1011. int pc_equippoint(struct map_session_data *sd,int n){
  1012. nullpo_ret(sd);
  1013. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  1014. }
  1015. /**
  1016. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  1017. * @param sd : player session
  1018. * @return 0 sucess, 1:invalid sd
  1019. */
  1020. void pc_setinventorydata(struct map_session_data *sd)
  1021. {
  1022. uint8 i;
  1023. nullpo_retv(sd);
  1024. for(i = 0; i < MAX_INVENTORY; i++) {
  1025. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  1026. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  1027. }
  1028. }
  1029. /**
  1030. * 'Calculates' weapon type
  1031. * @param sd : Player
  1032. */
  1033. void pc_calcweapontype(struct map_session_data *sd)
  1034. {
  1035. nullpo_retv(sd);
  1036. // single-hand
  1037. if(sd->weapontype2 == W_FIST) {
  1038. sd->status.weapon = sd->weapontype1;
  1039. return;
  1040. }
  1041. if(sd->weapontype1 == W_FIST) {
  1042. sd->status.weapon = sd->weapontype2;
  1043. return;
  1044. }
  1045. // dual-wield
  1046. sd->status.weapon = W_FIST;
  1047. switch (sd->weapontype1){
  1048. case W_DAGGER:
  1049. switch (sd->weapontype2) {
  1050. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  1051. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  1052. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  1053. }
  1054. break;
  1055. case W_1HSWORD:
  1056. switch (sd->weapontype2) {
  1057. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  1058. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  1059. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  1060. }
  1061. break;
  1062. case W_1HAXE:
  1063. switch (sd->weapontype2) {
  1064. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  1065. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  1066. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  1067. }
  1068. }
  1069. // unknown, default to right hand type
  1070. if (!sd->status.weapon)
  1071. sd->status.weapon = sd->weapontype1;
  1072. }
  1073. /**
  1074. * Set equip index
  1075. * @param sd : Player
  1076. */
  1077. void pc_setequipindex(struct map_session_data *sd)
  1078. {
  1079. uint16 i;
  1080. nullpo_retv(sd);
  1081. for (i = 0; i < EQI_MAX; i++){
  1082. sd->equip_index[i] = -1;
  1083. sd->equip_switch_index[i] = -1;
  1084. }
  1085. for (i = 0; i < MAX_INVENTORY; i++) {
  1086. if (sd->inventory.u.items_inventory[i].nameid == 0)
  1087. continue;
  1088. if (sd->inventory.u.items_inventory[i].equip) {
  1089. uint8 j;
  1090. for (j = 0; j < EQI_MAX; j++)
  1091. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  1092. sd->equip_index[j] = i;
  1093. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  1094. if (sd->inventory_data[i])
  1095. sd->weapontype1 = sd->inventory_data[i]->subtype;
  1096. else
  1097. sd->weapontype1 = W_FIST;
  1098. }
  1099. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  1100. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  1101. sd->weapontype2 = sd->inventory_data[i]->subtype;
  1102. else
  1103. sd->weapontype2 = W_FIST;
  1104. }
  1105. }
  1106. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  1107. for (uint8 j = 0; j < EQI_MAX; j++) {
  1108. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  1109. sd->equip_switch_index[j] = i;
  1110. }
  1111. }
  1112. }
  1113. }
  1114. pc_calcweapontype(sd);
  1115. }
  1116. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  1117. //{
  1118. // int i;
  1119. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  1120. // std::shared_ptr<item_data> data;
  1121. //
  1122. // //Crafted/made/hatched items.
  1123. // if (itemdb_isspecial(item->card[0]))
  1124. // return 1;
  1125. //
  1126. // /* scan for enchant armor gems */
  1127. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  1128. // s = MAX_SLOTS - 1;
  1129. //
  1130. // ARR_FIND( 0, s, i, item->card[i] && (data = item_db.find(item->card[i])) != NULL && data->flag.no_equip&flag );
  1131. // return( i < s ) ? 0 : 1;
  1132. //}
  1133. /**
  1134. * Check if an item is equiped by player
  1135. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  1136. * @param sd : player session
  1137. * @param nameid : itemid
  1138. * @return 1:yes, 0:no
  1139. */
  1140. bool pc_isequipped(struct map_session_data *sd, t_itemid nameid)
  1141. {
  1142. uint8 i;
  1143. for( i = 0; i < EQI_MAX; i++ )
  1144. {
  1145. short index = sd->equip_index[i], j;
  1146. if( index < 0 )
  1147. continue;
  1148. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  1149. continue;
  1150. if( !sd->inventory_data[index] )
  1151. continue;
  1152. if( sd->inventory_data[index]->nameid == nameid )
  1153. return true;
  1154. for( j = 0; j < MAX_SLOTS; j++ ){
  1155. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  1156. return true;
  1157. }
  1158. }
  1159. return false;
  1160. }
  1161. /**
  1162. * Check adoption rules
  1163. * @param p1_sd: Player 1
  1164. * @param p2_sd: Player 2
  1165. * @param b_sd: Player that will be adopted
  1166. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  1167. * ADOPT_ALREADY_ADOPTED - Already adopted
  1168. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  1169. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  1170. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  1171. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  1172. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  1173. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  1174. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  1175. */
  1176. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1177. {
  1178. if( !p1_sd || !p2_sd || !b_sd )
  1179. return ADOPT_CHARACTER_NOT_FOUND;
  1180. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1181. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1182. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1183. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1184. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1185. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1186. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1187. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1188. // Parents need to have their ring equipped
  1189. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1190. return ADOPT_EQUIP_RINGS;
  1191. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1192. return ADOPT_EQUIP_RINGS;
  1193. // Already adopted a baby
  1194. if( p1_sd->status.child || p2_sd->status.child ) {
  1195. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1196. return ADOPT_MORE_CHILDREN;
  1197. }
  1198. // Parents need at least lvl 70 to adopt
  1199. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1200. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1201. return ADOPT_LEVEL_70;
  1202. }
  1203. if( b_sd->status.partner_id ) {
  1204. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1205. return ADOPT_MARRIED;
  1206. }
  1207. 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 ) )
  1208. return ADOPT_NOT_NOVICE;
  1209. return ADOPT_ALLOWED;
  1210. }
  1211. /*==========================================
  1212. * Adoption Process
  1213. *------------------------------------------*/
  1214. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1215. {
  1216. int job, joblevel;
  1217. t_exp jobexp;
  1218. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1219. return false;
  1220. // Preserve current job levels and progress
  1221. joblevel = b_sd->status.job_level;
  1222. jobexp = b_sd->status.job_exp;
  1223. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1224. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1225. { // Success, proceed to configure parents and baby skills
  1226. p1_sd->status.child = b_sd->status.char_id;
  1227. p2_sd->status.child = b_sd->status.char_id;
  1228. b_sd->status.father = p1_sd->status.char_id;
  1229. b_sd->status.mother = p2_sd->status.char_id;
  1230. // Restore progress
  1231. b_sd->status.job_level = joblevel;
  1232. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1233. b_sd->status.job_exp = jobexp;
  1234. clif_updatestatus(b_sd, SP_JOBEXP);
  1235. // Baby Skills
  1236. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1237. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1238. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1239. // Parents Skills
  1240. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1241. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1242. chrif_save(p1_sd, CSAVE_NORMAL);
  1243. chrif_save(p2_sd, CSAVE_NORMAL);
  1244. chrif_save(b_sd, CSAVE_NORMAL);
  1245. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1246. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1247. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1248. return true;
  1249. }
  1250. return false; // Job Change Fail
  1251. }
  1252. static bool pc_job_can_use_item( struct map_session_data* sd, struct item_data* item ){
  1253. nullpo_retr( false, sd );
  1254. nullpo_retr( false, item );
  1255. // Calculate the required bit to check
  1256. uint64 job = 1ULL << ( sd->class_ & MAPID_BASEMASK );
  1257. size_t index;
  1258. // 2-1
  1259. if( ( sd->class_ & JOBL_2_1 ) != 0 ){
  1260. index = 1;
  1261. // 2-2
  1262. }else if( ( sd->class_ & JOBL_2_2 ) != 0 ){
  1263. index = 2;
  1264. // Basejob
  1265. }else{
  1266. index = 0;
  1267. }
  1268. return ( item->class_base[index] & job ) != 0;
  1269. }
  1270. /*==========================================
  1271. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1272. Returns:
  1273. false : Cannot use/equip
  1274. true : Can use/equip
  1275. * Credits:
  1276. [Inkfish] for first idea
  1277. [Haru] for third-classes extension
  1278. [Cydh] finishing :D
  1279. *------------------------------------------*/
  1280. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  1281. while (1) {
  1282. 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)
  1283. break;
  1284. #ifndef RENEWAL
  1285. //allow third classes to use trans. class items
  1286. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1287. break;
  1288. //third-baby classes can use same item too
  1289. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1290. break;
  1291. //don't need to decide specific rules for third-classes?
  1292. //items for third classes can be used for all third classes
  1293. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_UPPER|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1294. break;
  1295. #else
  1296. //trans. classes (exl. third-trans.)
  1297. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1298. break;
  1299. //baby classes (exl. third-baby)
  1300. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1301. break;
  1302. //third classes (exl. third-trans. and baby-third and fourth)
  1303. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)) && !(sd->class_&JOBL_FOURTH))
  1304. break;
  1305. //trans-third classes (exl. fourth)
  1306. if (item->class_upper&ITEMJ_THIRD_UPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_FOURTH))
  1307. break;
  1308. //third-baby classes (exl. fourth)
  1309. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY && !(sd->class_&JOBL_FOURTH))
  1310. break;
  1311. //fourth classes
  1312. if (item->class_upper&ITEMJ_FOURTH && sd->class_&JOBL_FOURTH)
  1313. break;
  1314. #endif
  1315. return false;
  1316. }
  1317. return true;
  1318. }
  1319. /*=================================================
  1320. * Checks if the player can equip the item at index n in inventory.
  1321. * @param sd
  1322. * @param n Item index in inventory
  1323. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1324. *------------------------------------------------*/
  1325. uint8 pc_isequip(struct map_session_data *sd,int n)
  1326. {
  1327. struct item_data *item;
  1328. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1329. item = sd->inventory_data[n];
  1330. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1331. return ITEM_EQUIP_ACK_OK;
  1332. if(item == NULL)
  1333. return ITEM_EQUIP_ACK_FAIL;
  1334. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1335. return ITEM_EQUIP_ACK_FAILLEVEL;
  1336. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1337. return ITEM_EQUIP_ACK_FAILLEVEL;
  1338. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1339. return ITEM_EQUIP_ACK_FAIL;
  1340. //fail to equip if item is restricted
  1341. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1342. return ITEM_EQUIP_ACK_FAIL;
  1343. if (item->equip&EQP_AMMO) {
  1344. switch (item->subtype) {
  1345. case AMMO_ARROW:
  1346. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1347. clif_msg(sd, ITEM_NEED_BOW);
  1348. return ITEM_EQUIP_ACK_FAIL;
  1349. }
  1350. break;
  1351. case AMMO_DAGGER:
  1352. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1353. return ITEM_EQUIP_ACK_FAIL;
  1354. break;
  1355. case AMMO_BULLET:
  1356. case AMMO_SHELL:
  1357. 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
  1358. #ifdef RENEWAL
  1359. && sd->status.weapon != W_GRENADE
  1360. #endif
  1361. ) {
  1362. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1363. return ITEM_EQUIP_ACK_FAIL;
  1364. }
  1365. break;
  1366. #ifndef RENEWAL
  1367. case AMMO_GRENADE:
  1368. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1369. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1370. return ITEM_EQUIP_ACK_FAIL;
  1371. }
  1372. break;
  1373. #endif
  1374. case AMMO_CANNONBALL:
  1375. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1376. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1377. return ITEM_EQUIP_ACK_FAIL;
  1378. }
  1379. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1380. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1381. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1382. return ITEM_EQUIP_ACK_FAIL;
  1383. }
  1384. break;
  1385. }
  1386. }
  1387. if (sd->sc.count) {
  1388. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1389. return ITEM_EQUIP_ACK_FAIL;
  1390. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1391. return ITEM_EQUIP_ACK_FAIL;
  1392. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1393. return ITEM_EQUIP_ACK_FAIL;
  1394. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1395. return ITEM_EQUIP_ACK_FAIL;
  1396. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1397. return ITEM_EQUIP_ACK_FAIL;
  1398. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1399. return ITEM_EQUIP_ACK_FAIL;
  1400. if (item->equip & EQP_SHADOW_GEAR && sd->sc.data[SC_SHADOW_STRIP])
  1401. return ITEM_EQUIP_ACK_FAIL;
  1402. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1403. return ITEM_EQUIP_ACK_FAIL;
  1404. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1405. //Spirit of Super Novice equip bonuses. [Skotlex]
  1406. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1407. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1408. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->weapon_level == 4)
  1409. switch(item->subtype) { //In weapons, the look determines type of weapon.
  1410. case W_DAGGER: //All level 4 - Daggers
  1411. case W_1HSWORD: //All level 4 - 1H Swords
  1412. case W_1HAXE: //All level 4 - 1H Axes
  1413. case W_MACE: //All level 4 - 1H Maces
  1414. case W_STAFF: //All level 4 - 1H Staves
  1415. case W_2HSTAFF: //All level 4 - 2H Staves
  1416. return ITEM_EQUIP_ACK_OK;
  1417. }
  1418. }
  1419. }
  1420. //Not equipable by class. [Skotlex]
  1421. if (!pc_job_can_use_item(sd,item))
  1422. return ITEM_EQUIP_ACK_FAIL;
  1423. if (!pc_isItemClass(sd, item))
  1424. return ITEM_EQUIP_ACK_FAIL;
  1425. return ITEM_EQUIP_ACK_OK;
  1426. }
  1427. /*==========================================
  1428. * No problem with the session id
  1429. * set the status that has been sent from char server
  1430. *------------------------------------------*/
  1431. bool pc_authok(struct map_session_data *sd, uint32 login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
  1432. {
  1433. t_tick tick = gettick();
  1434. uint32 ip = session[sd->fd]->client_addr;
  1435. sd->login_id2 = login_id2;
  1436. sd->group_id = group_id;
  1437. /* load user permissions */
  1438. pc_group_pc_load(sd);
  1439. memcpy(&sd->status, st, sizeof(*st));
  1440. if (st->sex != sd->status.sex) {
  1441. clif_authfail_fd(sd->fd, 0);
  1442. return false;
  1443. }
  1444. //Set the map-server used job id. [Skotlex]
  1445. uint64 class_ = pc_jobid2mapid(sd->status.class_);
  1446. if (class_ == -1 || !job_db.exists(sd->status.class_)) { //Invalid class?
  1447. 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);
  1448. sd->status.class_ = JOB_NOVICE;
  1449. sd->class_ = MAPID_NOVICE;
  1450. } else
  1451. sd->class_ = class_;
  1452. // Checks and fixes to character status data, that are required
  1453. // in case of configuration change or stuff, which cannot be
  1454. // checked on char-server.
  1455. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1456. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1457. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1458. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1459. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1460. sd->state.connect_new = 1;
  1461. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1462. sd->invincible_timer = INVALID_TIMER;
  1463. sd->npc_timer_id = INVALID_TIMER;
  1464. sd->pvp_timer = INVALID_TIMER;
  1465. sd->expiration_tid = INVALID_TIMER;
  1466. sd->autotrade_tid = INVALID_TIMER;
  1467. sd->respawn_tid = INVALID_TIMER;
  1468. sd->tid_queue_active = INVALID_TIMER;
  1469. sd->macro_detect.timer = INVALID_TIMER;
  1470. sd->skill_keep_using.tid = INVALID_TIMER;
  1471. sd->skill_keep_using.skill_id = 0;
  1472. sd->skill_keep_using.level = 0;
  1473. sd->skill_keep_using.target = 0;
  1474. #ifdef SECURE_NPCTIMEOUT
  1475. // Initialize to defaults/expected
  1476. sd->npc_idle_timer = INVALID_TIMER;
  1477. sd->npc_idle_tick = tick;
  1478. sd->npc_idle_type = NPCT_INPUT;
  1479. sd->state.ignoretimeout = false;
  1480. #endif
  1481. sd->canuseitem_tick = tick;
  1482. sd->canusecashfood_tick = tick;
  1483. sd->canequip_tick = tick;
  1484. sd->cantalk_tick = tick;
  1485. sd->canskill_tick = tick;
  1486. sd->cansendmail_tick = tick;
  1487. sd->idletime = last_tick;
  1488. sd->regen.tick.hp = tick;
  1489. sd->regen.tick.sp = tick;
  1490. for(int i = 0; i < MAX_SPIRITBALL; i++)
  1491. sd->spirit_timer[i] = INVALID_TIMER;
  1492. if (battle_config.item_auto_get)
  1493. sd->state.autoloot = 10000;
  1494. if (battle_config.disp_experience)
  1495. sd->state.showexp = 1;
  1496. if (battle_config.disp_zeny)
  1497. sd->state.showzeny = 1;
  1498. #ifdef VIP_ENABLE
  1499. if (!battle_config.vip_disp_rate)
  1500. sd->vip.disableshowrate = 1;
  1501. #endif
  1502. if (!(battle_config.display_skill_fail&2))
  1503. sd->state.showdelay = 1;
  1504. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1505. memset(&sd->cart, 0, sizeof(struct s_storage));
  1506. memset(&sd->storage, 0, sizeof(struct s_storage));
  1507. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1508. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1509. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1510. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1511. sd->status.option &= ~OPTION_INVISIBLE;
  1512. }
  1513. status_change_init(&sd->bl);
  1514. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1515. unit_dataset(&sd->bl);
  1516. sd->guild_x = -1;
  1517. sd->guild_y = -1;
  1518. sd->delayed_damage = 0;
  1519. // Event Timers
  1520. for( int i = 0; i < MAX_EVENTTIMER; i++ )
  1521. sd->eventtimer[i] = INVALID_TIMER;
  1522. // Rental Timer
  1523. sd->rental_timer = INVALID_TIMER;
  1524. for( int i = 0; i < 3; i++ )
  1525. sd->hate_mob[i] = -1;
  1526. sd->quest_log = NULL;
  1527. sd->num_quests = 0;
  1528. sd->avail_quests = 0;
  1529. sd->save_quest = false;
  1530. sd->count_rewarp = 0;
  1531. sd->mail.pending_weight = 0;
  1532. sd->mail.pending_zeny = 0;
  1533. sd->mail.pending_slots = 0;
  1534. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1535. sd->regs.arrays = NULL;
  1536. sd->vars_dirty = false;
  1537. sd->vars_ok = false;
  1538. sd->vars_received = 0x0;
  1539. //warp player
  1540. enum e_setpos setpos_result = pc_setpos( sd, sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT );
  1541. if( setpos_result != SETPOS_OK ){
  1542. ShowError( "Last_point_map %s - id %d not found (error code %d)\n", mapindex_id2name(sd->status.last_point.map), sd->status.last_point.map, setpos_result );
  1543. // try warping to a default map instead (church graveyard)
  1544. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1545. // if we fail again
  1546. clif_authfail_fd(sd->fd, 0);
  1547. return false;
  1548. }
  1549. }
  1550. clif_inventory_expansion_info( sd );
  1551. clif_authok(sd);
  1552. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1553. sd->die_counter=-1;
  1554. //display login notice
  1555. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1556. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1557. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1558. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1559. sd->status.name, sd->status.account_id, sd->status.char_id,
  1560. CONVIP(ip), sd->group_id);
  1561. // Send friends list
  1562. clif_friendslist_send( *sd );
  1563. if( !changing_mapservers ) {
  1564. if (battle_config.display_version == 1)
  1565. pc_show_version(sd);
  1566. // Message of the Day [Valaris]
  1567. for(int i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1568. if (battle_config.motd_type)
  1569. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1570. else
  1571. clif_displaymessage(sd->fd, motd_text[i]);
  1572. }
  1573. if (expiration_time != 0)
  1574. sd->expiration_time = expiration_time;
  1575. /**
  1576. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1577. **/
  1578. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1579. }
  1580. pc_validate_skill(sd);
  1581. /* [Ind] */
  1582. sd->sc_display = NULL;
  1583. sd->sc_display_count = 0;
  1584. // Player has not yet received the CashShop list
  1585. sd->status.cashshop_sent = false;
  1586. sd->last_addeditem_index = -1;
  1587. sd->bonus_script.head = NULL;
  1588. sd->bonus_script.count = 0;
  1589. // Initialize BG queue
  1590. sd->bg_queue_id = 0;
  1591. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  1592. sd->hatEffects = {};
  1593. #endif
  1594. sd->catch_target_class = PET_CATCH_FAIL;
  1595. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1596. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1597. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1598. clif_updatestatus(sd, SP_BASEEXP);
  1599. }
  1600. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1601. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1602. clif_updatestatus(sd, SP_JOBEXP);
  1603. }
  1604. // Request all registries (auth is considered completed whence they arrive)
  1605. intif_request_registry(sd,7);
  1606. return true;
  1607. }
  1608. /*==========================================
  1609. * Closes a connection because it failed to be authenticated from the char server.
  1610. *------------------------------------------*/
  1611. void pc_authfail(struct map_session_data *sd)
  1612. {
  1613. clif_authfail_fd(sd->fd, 0);
  1614. return;
  1615. }
  1616. /**
  1617. * Player register a bl as hatred
  1618. * @param sd : player session
  1619. * @param pos : hate position [0;2]
  1620. * @param bl : target bl
  1621. * @return false:failed, true:success
  1622. */
  1623. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1624. {
  1625. int class_;
  1626. if (!sd || !bl || pos < 0 || pos > 2)
  1627. return false;
  1628. if (sd->hate_mob[pos] != -1)
  1629. { //Can't change hate targets.
  1630. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1631. return false;
  1632. }
  1633. class_ = status_get_class(bl);
  1634. if (!pcdb_checkid(class_)) {
  1635. unsigned int max_hp = status_get_max_hp(bl);
  1636. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1637. return false;
  1638. if (pos != status_get_size(bl))
  1639. return false; //Wrong size
  1640. }
  1641. sd->hate_mob[pos] = class_;
  1642. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1643. clif_hate_info(sd, pos, class_, 1);
  1644. return true;
  1645. }
  1646. /*==========================================
  1647. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1648. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1649. * See intif_parse_StorageReceived() for item operations [lighta]
  1650. *------------------------------------------*/
  1651. void pc_reg_received(struct map_session_data *sd)
  1652. {
  1653. uint8 i;
  1654. sd->vars_ok = true;
  1655. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1656. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1657. sd->change_level_4th = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE4TH_VAR)));
  1658. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1659. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1660. if (msg_checklangtype(sd->langtype,true) < 0)
  1661. sd->langtype = 0; //invalid langtype reset to default
  1662. // Cash shop
  1663. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1664. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1665. // Cooking Exp
  1666. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1667. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1668. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1669. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1670. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1671. }
  1672. if (battle_config.feature_banking)
  1673. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1674. if (battle_config.feature_roulette) {
  1675. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1676. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1677. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1678. }
  1679. sd->roulette.prizeIdx = -1;
  1680. //SG map and mob read [Komurka]
  1681. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1682. uint16 j;
  1683. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1684. sd->feel_map[i].index = j;
  1685. sd->feel_map[i].m = map_mapindex2mapid(j);
  1686. } else {
  1687. sd->feel_map[i].index = 0;
  1688. sd->feel_map[i].m = -1;
  1689. }
  1690. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1691. }
  1692. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1693. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1694. sd->cloneskill_idx = skill_get_index(skid);
  1695. if (sd->cloneskill_idx > 0) {
  1696. sd->status.skill[sd->cloneskill_idx].id = skid;
  1697. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1698. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1699. sd->status.skill[sd->cloneskill_idx].lv = i;
  1700. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1701. }
  1702. }
  1703. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1704. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1705. sd->reproduceskill_idx = skill_get_index(skid);
  1706. if (sd->reproduceskill_idx > 0) {
  1707. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1708. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1709. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1710. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1711. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1712. }
  1713. }
  1714. //Weird... maybe registries were reloaded?
  1715. if (sd->state.active)
  1716. return;
  1717. sd->state.active = 1;
  1718. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1719. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1720. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1721. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1722. // Restore IM_CHAR instance to the player
  1723. for (const auto &instance : instances) {
  1724. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1725. sd->instance_id = instance.first;
  1726. break;
  1727. }
  1728. }
  1729. if (sd->status.party_id > 0)
  1730. party_member_joined(sd);
  1731. if (sd->status.guild_id > 0)
  1732. guild_member_joined(sd);
  1733. if (sd->status.clan_id > 0)
  1734. clan_member_joined(sd);
  1735. #if !( PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410 )
  1736. // 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
  1737. clif_instance_info( *sd );
  1738. #endif
  1739. // pet
  1740. if (sd->status.pet_id > 0)
  1741. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1742. // Homunculus [albator]
  1743. if( sd->status.hom_id > 0 )
  1744. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1745. if( sd->status.mer_id > 0 )
  1746. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1747. if( sd->status.ele_id > 0 )
  1748. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1749. map_addiddb(&sd->bl);
  1750. map_delnickdb(sd->status.char_id, sd->status.name);
  1751. if (!chrif_auth_finished(sd))
  1752. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1753. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1754. chrif_bsdata_request(sd->status.char_id);
  1755. #ifdef VIP_ENABLE
  1756. sd->vip.time = 0;
  1757. sd->vip.enabled = 0;
  1758. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1759. #endif
  1760. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1761. intif_request_questlog(sd);
  1762. if (battle_config.feature_achievement) {
  1763. sd->achievement_data.total_score = 0;
  1764. sd->achievement_data.level = 0;
  1765. sd->achievement_data.save = false;
  1766. sd->achievement_data.count = 0;
  1767. sd->achievement_data.incompleteCount = 0;
  1768. sd->achievement_data.achievements = NULL;
  1769. intif_request_achievements(sd->status.char_id);
  1770. }
  1771. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1772. sd->state.connect_new = 1;
  1773. clif_parse_LoadEndAck(sd->fd, sd);
  1774. if( pc_isinvisible( sd ) ){
  1775. sd->vd.class_ = JT_INVISIBLE;
  1776. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1777. // decrement the number of pvp players on the map
  1778. map_getmapdata( sd->bl.m )->users_pvp--;
  1779. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1780. // unregister the player for ranking
  1781. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1782. sd->pvp_timer = INVALID_TIMER;
  1783. }
  1784. clif_changeoption( &sd->bl );
  1785. }
  1786. }
  1787. channel_autojoin(sd);
  1788. }
  1789. static int pc_calc_skillpoint(struct map_session_data* sd)
  1790. {
  1791. uint16 i, skill_point = 0;
  1792. nullpo_ret(sd);
  1793. for(i = 1; i < MAX_SKILL; i++) {
  1794. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1795. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1796. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1797. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1798. )
  1799. {
  1800. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1801. skill_point += sd->status.skill[i].lv;
  1802. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1803. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1804. }
  1805. }
  1806. }
  1807. return skill_point;
  1808. }
  1809. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1810. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1811. return false;
  1812. /**
  1813. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1814. * skill tree and since they have no icons they'll give resource errors
  1815. * Get ALL skills except npc/guild ones. [Skotlex]
  1816. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1817. **/
  1818. for (const auto &skill : skill_db) {
  1819. uint16 skill_id = skill.second->nameid;
  1820. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1821. continue;
  1822. switch (skill_id) {
  1823. case SM_SELFPROVOKE:
  1824. case AB_DUPLELIGHT_MELEE:
  1825. case AB_DUPLELIGHT_MAGIC:
  1826. case WL_CHAINLIGHTNING_ATK:
  1827. case WL_TETRAVORTEX_FIRE:
  1828. case WL_TETRAVORTEX_WATER:
  1829. case WL_TETRAVORTEX_WIND:
  1830. case WL_TETRAVORTEX_GROUND:
  1831. case WL_SUMMON_ATK_FIRE:
  1832. case WL_SUMMON_ATK_WIND:
  1833. case WL_SUMMON_ATK_WATER:
  1834. case WL_SUMMON_ATK_GROUND:
  1835. case LG_OVERBRAND_BRANDISH:
  1836. case LG_OVERBRAND_PLUSATK:
  1837. case WM_SEVERE_RAINSTORM_MELEE:
  1838. case RL_R_TRIP_PLUSATK:
  1839. case SG_DEVIL:
  1840. case MO_TRIPLEATTACK:
  1841. case RG_SNATCHER:
  1842. continue;
  1843. default:
  1844. {
  1845. uint8 lv = (uint8)skill_get_max(skill_id);
  1846. if (lv > 0) {
  1847. uint16 idx = skill_get_index(skill_id);
  1848. sd->status.skill[idx].id = skill_id;
  1849. if (addlv)
  1850. sd->status.skill[idx].lv = lv;
  1851. }
  1852. }
  1853. break;
  1854. }
  1855. }
  1856. return true;
  1857. }
  1858. /*==========================================
  1859. * Calculation of skill level.
  1860. * @param sd
  1861. *------------------------------------------*/
  1862. void pc_calc_skilltree(struct map_session_data *sd)
  1863. {
  1864. nullpo_retv(sd);
  1865. uint64 job = pc_calc_skilltree_normalize_job(sd);
  1866. int class_ = pc_mapid2jobid(job, sd->status.sex);
  1867. if( class_ == -1 )
  1868. { //Unable to normalize job??
  1869. 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 );
  1870. return;
  1871. }
  1872. uint16 job_id = class_;
  1873. class_ = pc_class2idx(class_);
  1874. for (const auto &skill : skill_db) {
  1875. uint16 skill_id = skill.second->nameid;
  1876. uint16 idx = skill_get_index(skill_id);
  1877. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1878. sd->status.skill[idx].id = 0; //First clear skills.
  1879. /* permanent skills that must be re-checked */
  1880. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  1881. if (skill_id == 0) {
  1882. sd->status.skill[idx].id = 0;
  1883. sd->status.skill[idx].lv = 0;
  1884. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1885. continue;
  1886. }
  1887. switch (skill_id) {
  1888. case NV_TRICKDEAD:
  1889. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1890. sd->status.skill[idx].id = 0;
  1891. sd->status.skill[idx].lv = 0;
  1892. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1893. }
  1894. break;
  1895. }
  1896. }
  1897. }
  1898. for (const auto &skill : skill_db) {
  1899. uint16 idx = skill_get_index(skill.second->nameid);
  1900. // Restore original level of skills after deleting earned skills.
  1901. 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 ) {
  1902. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1903. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1904. }
  1905. }
  1906. // Removes Taekwon Ranker skill bonus
  1907. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1908. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(JOB_TAEKWON);
  1909. if (tree != nullptr && !tree->skills.empty()) {
  1910. for (const auto &it : tree->skills) {
  1911. uint16 sk_idx = skill_get_index(it.first);
  1912. if (sk_idx == 0)
  1913. continue;
  1914. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1915. if (it.first == NV_BASIC || it.first == NV_FIRSTAID || it.first == WE_CALLBABY)
  1916. continue;
  1917. sd->status.skill[sk_idx].id = 0;
  1918. }
  1919. }
  1920. }
  1921. }
  1922. // Grant all skills
  1923. pc_grant_allskills(sd, false);
  1924. int flag;
  1925. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  1926. do {
  1927. flag = 0;
  1928. if (tree == nullptr || tree->skills.empty())
  1929. break;
  1930. for (const auto &skillsit : tree->skills) {
  1931. bool fail = false;
  1932. uint16 skid = skillsit.first;
  1933. uint16 sk_idx = skill_get_index(skid);
  1934. if (sd->status.skill[sk_idx].id)
  1935. continue; //Skill already known.
  1936. if (!battle_config.skillfree) {
  1937. // Checking required skills
  1938. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  1939. if (entry != nullptr && !entry->need.empty()) {
  1940. for (const auto &it : entry->need) {
  1941. uint16 sk_need_id = it.first;
  1942. uint16 sk_need_idx = skill_get_index(sk_need_id);
  1943. if (sk_need_idx > 0) {
  1944. uint16 sk_need = sk_need_id;
  1945. 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)
  1946. sk_need = 0; //Not learned.
  1947. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1948. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1949. else
  1950. sk_need = pc_checkskill(sd,sk_need_id);
  1951. if (sk_need < it.second) {
  1952. fail = true;
  1953. break;
  1954. }
  1955. }
  1956. }
  1957. }
  1958. if (sd->status.base_level < entry->baselv) { //We need to get the actual class in this case
  1959. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1960. c_ = pc_class2idx(c_);
  1961. if (class_ == c_ || (class_ != c_ && sd->status.base_level < entry->baselv))
  1962. fail = true; // base level requirement wasn't satisfied
  1963. }
  1964. if (sd->status.job_level < entry->joblv) { //We need to get the actual class in this case
  1965. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1966. c_ = pc_class2idx(c_);
  1967. if (class_ == c_ || (class_ != c_ && sd->status.job_level < entry->joblv))
  1968. fail = true; // job level requirement wasn't satisfied
  1969. }
  1970. }
  1971. if (!fail) {
  1972. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1973. if (!sd->status.skill[sk_idx].lv && (
  1974. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1975. skill->inf2[INF2_ISWEDDING] ||
  1976. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1977. ))
  1978. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1979. sd->status.skill[sk_idx].id = skid;
  1980. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1981. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1982. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1983. }
  1984. flag = 1; // skill list has changed, perform another pass
  1985. }
  1986. }
  1987. } while(flag);
  1988. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1989. uint16 skid = 0;
  1990. /* Taekwon Ranker Bonus Skill Tree
  1991. ============================================
  1992. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1993. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1994. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1995. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  1996. if (tree != nullptr && !tree->skills.empty()) {
  1997. for (const auto &it : tree->skills) {
  1998. skid = it.first;
  1999. uint16 sk_idx = skill_get_index(skid);
  2000. if (sk_idx == 0)
  2001. continue;
  2002. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  2003. continue; //Do not include Quest/Wedding skills.
  2004. if( sd->status.skill[sk_idx].id == 0 ) {
  2005. sd->status.skill[sk_idx].id = skid;
  2006. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  2007. } else if( skid != NV_BASIC )
  2008. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  2009. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  2010. }
  2011. }
  2012. }
  2013. // Enable Bard/Dancer spirit linked skills.
  2014. if (sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER) {
  2015. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  2016. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  2017. { BA_POEMBRAGI, DC_FORTUNEKISS },
  2018. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  2019. for (const auto &skill : linked_skills) {
  2020. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  2021. continue;
  2022. // Tag it as a non-savable, non-uppable, bonus skill
  2023. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  2024. }
  2025. }
  2026. }
  2027. //Checks if you can learn a new skill after having leveled up a skill.
  2028. static void pc_check_skilltree(struct map_session_data *sd)
  2029. {
  2030. int flag = 0;
  2031. if (battle_config.skillfree)
  2032. return; //Function serves no purpose if this is set
  2033. int c = pc_mapid2jobid( pc_calc_skilltree_normalize_job( sd ), sd->status.sex );
  2034. if (c == -1) { //Unable to normalize job??
  2035. 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);
  2036. return;
  2037. }
  2038. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(c);
  2039. if (tree == nullptr || tree->skills.empty())
  2040. return;
  2041. do {
  2042. flag = 0;
  2043. for (const auto &skillsit : tree->skills) {
  2044. uint16 skid = skillsit.first;
  2045. uint16 sk_idx = skill_get_index(skid);
  2046. bool fail = false;
  2047. if (sd->status.skill[sk_idx].id) //Already learned
  2048. continue;
  2049. // Checking required skills
  2050. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2051. if (entry != nullptr && !entry->need.empty()) {
  2052. for (const auto &it : entry->need) {
  2053. uint16 sk_need_id = it.first;
  2054. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2055. if (sk_need_id > 0) {
  2056. short sk_need = sk_need_id;
  2057. 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)
  2058. sk_need = 0; //Not learned.
  2059. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  2060. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2061. else
  2062. sk_need = pc_checkskill(sd,sk_need_id);
  2063. if (sk_need < it.second) {
  2064. fail = true;
  2065. break;
  2066. }
  2067. }
  2068. }
  2069. }
  2070. if( fail )
  2071. continue;
  2072. if (sd->status.base_level < entry->baselv || sd->status.job_level < entry->joblv)
  2073. continue;
  2074. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2075. if( !sd->status.skill[sk_idx].lv && (
  2076. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2077. skill->inf2[INF2_ISWEDDING] ||
  2078. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  2079. ) )
  2080. continue; //Cannot be learned via normal means.
  2081. sd->status.skill[sk_idx].id = skid;
  2082. flag = 1;
  2083. }
  2084. } while(flag);
  2085. }
  2086. // Make sure all the skills are in the correct condition
  2087. // before persisting to the backend.. [MouseJstr]
  2088. void pc_clean_skilltree(struct map_session_data *sd)
  2089. {
  2090. uint16 i;
  2091. for (i = 0; i < MAX_SKILL; i++){
  2092. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  2093. sd->status.skill[i].id = 0;
  2094. sd->status.skill[i].lv = 0;
  2095. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2096. }
  2097. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  2098. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  2099. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2100. }
  2101. }
  2102. }
  2103. uint64 pc_calc_skilltree_normalize_job_sub( struct map_session_data *sd ){
  2104. int skill_point = pc_calc_skillpoint( sd );
  2105. if( sd->class_ & MAPID_SUMMONER ){
  2106. // Novice's skill points for basic skill.
  2107. std::shared_ptr<s_job_info> summoner_job = job_db.find( JOB_SUMMONER );
  2108. int summoner_skills = summoner_job->max_job_level - 1;
  2109. if( skill_point < summoner_skills ){
  2110. return MAPID_SUMMONER;
  2111. }
  2112. skill_point -= summoner_skills;
  2113. }else{
  2114. // Novice's skill points for basic skill.
  2115. std::shared_ptr<s_job_info> novice_job = job_db.find( JOB_NOVICE );
  2116. int novice_skills = novice_job->max_job_level - 1;
  2117. if( skill_point < novice_skills ){
  2118. return MAPID_NOVICE;
  2119. }
  2120. skill_point -= novice_skills;
  2121. }
  2122. // 1st Class Job LV Check
  2123. if( sd->class_ & JOBL_2 && ( sd->class_ & MAPID_UPPERMASK ) != MAPID_SUPER_NOVICE ){
  2124. uint64 mapid_1st = sd->class_ & MAPID_BASEMASK;
  2125. int class_1st = pc_mapid2jobid( mapid_1st, sd->status.sex );
  2126. if( !sd->change_level_2nd ){
  2127. if( class_1st >= JOB_NOVICE ){
  2128. sd->change_level_2nd = job_db.find( class_1st )->max_job_level;
  2129. }else{
  2130. sd->change_level_2nd = 0;
  2131. }
  2132. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  2133. }
  2134. if( class_1st > 0 && skill_point < ( sd->change_level_2nd - 1 ) ){
  2135. return mapid_1st;
  2136. }
  2137. skill_point -= ( sd->change_level_2nd - 1 );
  2138. }
  2139. // 2nd Class Job LV Check
  2140. if( sd->class_ & JOBL_THIRD && (sd->class_ & MAPID_THIRDMASK) != MAPID_SUPER_NOVICE_E ){
  2141. uint64 mapid_2nd = sd->class_ & MAPID_UPPERMASK;
  2142. int class_2nd = pc_mapid2jobid( mapid_2nd, sd->status.sex );
  2143. if( !sd->change_level_3rd ){
  2144. if( class_2nd >= JOB_NOVICE ){
  2145. sd->change_level_3rd = job_db.find( class_2nd )->max_job_level;
  2146. }else{
  2147. sd->change_level_3rd = 0;
  2148. }
  2149. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  2150. }
  2151. if( class_2nd > 0 && skill_point < ( sd->change_level_3rd - 1 ) ){
  2152. return mapid_2nd;
  2153. }
  2154. skill_point -= ( sd->change_level_3rd - 1 );
  2155. }
  2156. // 3rd Class Job LV Check
  2157. if( sd->class_ & JOBL_FOURTH ){
  2158. uint64 mapid_3rd = sd->class_ & MAPID_THIRDMASK | JOBL_THIRD;
  2159. int class_3rd = pc_mapid2jobid( mapid_3rd, sd->status.sex );
  2160. if( !sd->change_level_4th ){
  2161. if( class_3rd >= JOB_NOVICE ){
  2162. sd->change_level_4th = job_db.find( class_3rd )->max_job_level;
  2163. }else{
  2164. sd->change_level_4th = 0;
  2165. }
  2166. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ), sd->change_level_4th );
  2167. }
  2168. if( class_3rd > 0 && skill_point < ( sd->change_level_4th - 1 ) ){
  2169. return mapid_3rd;
  2170. }
  2171. skill_point -= ( sd->change_level_4th - 1 );
  2172. }
  2173. return sd->class_;
  2174. }
  2175. uint64 pc_calc_skilltree_normalize_job( struct map_session_data *sd ){
  2176. if( !battle_config.skillup_limit || pc_has_permission( sd, PC_PERM_ALL_SKILL ) ){
  2177. return sd->class_;
  2178. }
  2179. uint64 c = pc_calc_skilltree_normalize_job_sub( sd );
  2180. // Special Masks
  2181. if( sd->class_&JOBL_UPPER ){
  2182. if( pc_mapid2jobid( c | JOBL_UPPER, sd->status.sex ) >= JOB_NOVICE ){
  2183. c |= JOBL_UPPER;// Rebirth Job
  2184. }
  2185. }
  2186. if( sd->class_&JOBL_BABY ){
  2187. if( pc_mapid2jobid( c | JOBL_BABY, sd->status.sex ) >= JOB_NOVICE ){
  2188. c |= JOBL_BABY;// Baby Job
  2189. }
  2190. }
  2191. return c;
  2192. }
  2193. /*==========================================
  2194. * Updates the weight status
  2195. *------------------------------------------
  2196. * 1: overweight 50% for pre-renewal and 70% for renewal
  2197. * 2: overweight 90%
  2198. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  2199. */
  2200. void pc_updateweightstatus(struct map_session_data *sd)
  2201. {
  2202. int old_overweight;
  2203. int new_overweight;
  2204. nullpo_retv(sd);
  2205. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  2206. #ifdef RENEWAL
  2207. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  2208. #else
  2209. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  2210. #endif
  2211. if( old_overweight == new_overweight )
  2212. return; // no change
  2213. // stop old status change
  2214. if( old_overweight == 1 )
  2215. status_change_end(&sd->bl, SC_WEIGHT50);
  2216. else if( old_overweight == 2 )
  2217. status_change_end(&sd->bl, SC_WEIGHT90);
  2218. // start new status change
  2219. if( new_overweight == 1 )
  2220. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  2221. else if( new_overweight == 2 )
  2222. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  2223. // update overweight status
  2224. sd->regen.state.overweight = new_overweight;
  2225. }
  2226. int pc_disguise(struct map_session_data *sd, int class_)
  2227. {
  2228. if (!class_ && !sd->disguise)
  2229. return 0;
  2230. if (class_ && sd->disguise == class_)
  2231. return 0;
  2232. if(pc_isinvisible(sd))
  2233. { //Character is invisible. Stealth class-change. [Skotlex]
  2234. sd->disguise = class_; //viewdata is set on uncloaking.
  2235. return 2;
  2236. }
  2237. if (sd->bl.prev != NULL) {
  2238. pc_stop_walking(sd, 0);
  2239. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  2240. }
  2241. if (!class_) {
  2242. sd->disguise = 0;
  2243. class_ = sd->status.class_;
  2244. } else
  2245. sd->disguise=class_;
  2246. status_set_viewdata(&sd->bl, class_);
  2247. clif_changeoption(&sd->bl);
  2248. if (sd->bl.prev != NULL) {
  2249. clif_spawn(&sd->bl);
  2250. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2251. { //It seems the cart info is lost on undisguise.
  2252. clif_cartlist(sd);
  2253. clif_updatestatus(sd,SP_CARTINFO);
  2254. }
  2255. if (sd->chatID) {
  2256. struct chat_data* cd;
  2257. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  2258. clif_dispchat(cd,0);
  2259. }
  2260. }
  2261. return 1;
  2262. }
  2263. /// Show error message
  2264. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2265. /// Check for valid Element, break & show error message if invalid Element
  2266. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2267. /// Check for valid Race, break & show error message if invalid Race
  2268. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2269. /// Check for valid Race2, break & show error message if invalid Race2
  2270. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2271. /// Check for valid Class, break & show error message if invalid Class
  2272. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2273. /// Check for valid Size, break & show error message if invalid Size
  2274. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2275. /// Check for valid SC, break & show error message if invalid SC
  2276. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2277. /**
  2278. * Add auto spell bonus for player while attacking/attacked
  2279. * @param spell: Spell array
  2280. * @param id: Skill to cast
  2281. * @param lv: Skill level
  2282. * @param rate: Success chance
  2283. * @param battle_flag: Battle flag
  2284. * @param card_id: Used to prevent card stacking
  2285. * @param flag: Flags used for extra arguments
  2286. * &0: forces the skill to be casted on self, rather than on the target
  2287. * &1: forces the skill to be casted on target, rather than self
  2288. * &2: random skill level in [1..lv] is chosen
  2289. */
  2290. 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)
  2291. {
  2292. if (spell.size() == MAX_PC_BONUS) {
  2293. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2294. return;
  2295. }
  2296. if (!rate)
  2297. return;
  2298. if (!(battle_flag&BF_RANGEMASK))
  2299. battle_flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2300. if (!(battle_flag&BF_WEAPONMASK))
  2301. battle_flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2302. if (!(battle_flag&BF_SKILLMASK)) {
  2303. if (battle_flag&(BF_MAGIC | BF_MISC))
  2304. battle_flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2305. if (battle_flag&BF_WEAPON)
  2306. battle_flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2307. }
  2308. for (auto &it : spell) {
  2309. 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) {
  2310. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2311. return;
  2312. it.rate = util::safe_addition_cap(it.rate, rate, (short)1000);
  2313. return;
  2314. }
  2315. }
  2316. struct s_autospell entry = {};
  2317. if (rate < -1000 || rate > 1000)
  2318. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -1000~1000 range, capping.\n", rate);
  2319. entry.id = id;
  2320. entry.lv = lv;
  2321. entry.rate = cap_value(rate, -1000, 1000);
  2322. entry.battle_flag = battle_flag;
  2323. entry.card_id = card_id;
  2324. entry.flag = flag;
  2325. spell.push_back(entry);
  2326. }
  2327. /**
  2328. * Add auto spell bonus for player while using skills
  2329. * @param spell: Spell array
  2330. * @param src_skill: Trigger skill
  2331. * @param id: Support or target type
  2332. * @param lv: Skill level
  2333. * @param rate: Success chance
  2334. * @param card_id: Used to prevent card stacking
  2335. * @param flag: Flags used for extra arguments
  2336. * &1: forces the skill to be casted on self, rather than on the target of skill
  2337. * &2: random skill level in [1..lv] is chosen
  2338. */
  2339. 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)
  2340. {
  2341. if (spell.size() == MAX_PC_BONUS) {
  2342. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2343. return;
  2344. }
  2345. if (!rate)
  2346. return;
  2347. struct s_autospell entry = {};
  2348. if (rate < -1000 || rate > 1000)
  2349. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -1000~1000 range, capping.\n", rate);
  2350. entry.trigger_skill = src_skill;
  2351. entry.id = id;
  2352. entry.lv = lv;
  2353. entry.rate = cap_value(rate, -1000, 1000);
  2354. entry.card_id = card_id;
  2355. entry.flag = flag;
  2356. spell.push_back(entry);
  2357. }
  2358. /**
  2359. * Add inflict effect bonus for player while attacking/attacked
  2360. * @param effect: Effect array
  2361. * @param sc: SC/Effect type
  2362. * @param rate: Success chance
  2363. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2364. * @param flag: Target flag
  2365. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2366. */
  2367. 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)
  2368. {
  2369. if (effect.size() == MAX_PC_BONUS) {
  2370. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2371. return;
  2372. }
  2373. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2374. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2375. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2376. flag |= ATF_TARGET; //Default target: enemy.
  2377. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2378. flag |= ATF_WEAPON; //Default type: weapon.
  2379. if (!duration)
  2380. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2381. for (auto &it : effect) {
  2382. if (it.sc == sc && it.flag == flag) {
  2383. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2384. it.arrow_rate = util::safe_addition_cap(it.arrow_rate, arrow_rate, (short)SHRT_MAX);
  2385. it.duration = umax(it.duration, duration);
  2386. return;
  2387. }
  2388. }
  2389. struct s_addeffect entry = {};
  2390. entry.sc = sc;
  2391. entry.rate = rate;
  2392. entry.arrow_rate = arrow_rate;
  2393. entry.flag = flag;
  2394. entry.duration = duration;
  2395. effect.push_back(entry);
  2396. }
  2397. /**
  2398. * Add inflict effect bonus for player while attacking using skill
  2399. * @param effect: Effect array
  2400. * @param sc: SC/Effect type
  2401. * @param rate: Success chance
  2402. * @param skill_id: Skill to cast
  2403. * @param target: Target type
  2404. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2405. */
  2406. 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)
  2407. {
  2408. if (effect.size() == MAX_PC_BONUS) {
  2409. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2410. return;
  2411. }
  2412. if (!duration)
  2413. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2414. for (auto &it : effect) {
  2415. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2416. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2417. it.duration = umax(it.duration, duration);
  2418. return;
  2419. }
  2420. }
  2421. struct s_addeffectonskill entry = {};
  2422. entry.sc = sc;
  2423. entry.rate = rate;
  2424. entry.skill_id = skill_id;
  2425. entry.target = target;
  2426. entry.duration = duration;
  2427. effect.push_back(entry);
  2428. }
  2429. /**
  2430. * Adjust/add drop rate modifier for player
  2431. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2432. * @param nameid: item id that will be dropped
  2433. * @param group: group id
  2434. * @param class_: target class
  2435. * @param race: target race. if < 0, means monster_id
  2436. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2437. */
  2438. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int class_, short race, int rate)
  2439. {
  2440. if (!nameid && !group) {
  2441. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2442. return;
  2443. }
  2444. if (!item_db.exists(nameid)) {
  2445. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2446. return;
  2447. }
  2448. if (group && !itemdb_group.exists(group)) {
  2449. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2450. return;
  2451. }
  2452. if (drop.size() == MAX_PC_BONUS) {
  2453. 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);
  2454. return;
  2455. }
  2456. //Apply config rate adjustment settings.
  2457. if (rate >= 0) { //Absolute drop.
  2458. if (battle_config.item_rate_adddrop != 100)
  2459. rate = rate*battle_config.item_rate_adddrop/100;
  2460. if (rate < battle_config.item_drop_adddrop_min)
  2461. rate = battle_config.item_drop_adddrop_min;
  2462. else if (rate > battle_config.item_drop_adddrop_max)
  2463. rate = battle_config.item_drop_adddrop_max;
  2464. } else { //Relative drop, max/min limits are applied at drop time.
  2465. if (battle_config.item_rate_adddrop != 100)
  2466. rate = rate*battle_config.item_rate_adddrop/100;
  2467. if (rate > -1)
  2468. rate = -1;
  2469. }
  2470. for (auto &it : drop) {
  2471. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2472. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2473. it.rate = util::safe_addition_cap(it.rate, rate, 10000);
  2474. return;
  2475. }
  2476. }
  2477. struct s_add_drop entry = {};
  2478. if (rate < -10000 || rate > 10000)
  2479. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2480. entry.nameid = nameid;
  2481. entry.group = group;
  2482. entry.race = race;
  2483. entry.class_ = class_;
  2484. entry.rate = cap_value(rate, -10000, 10000);
  2485. drop.push_back(entry);
  2486. }
  2487. s_autobonus::~s_autobonus(){
  2488. if( this->active != INVALID_TIMER ){
  2489. delete_timer( this->active, pc_endautobonus );
  2490. this->active = INVALID_TIMER;
  2491. }
  2492. if( this->bonus_script != nullptr ){
  2493. aFree( this->bonus_script );
  2494. this->bonus_script = nullptr;
  2495. }
  2496. if( this->other_script != nullptr ){
  2497. aFree( this->other_script );
  2498. this->other_script = nullptr;
  2499. }
  2500. }
  2501. /**
  2502. * Add autobonus to player when attacking/attacked
  2503. * @param bonus: Bonus array
  2504. * @param script: Script to execute
  2505. * @param rate: Success chance
  2506. * @param dur: Duration
  2507. * @param flag: Battle flag/skill
  2508. * @param other_script: Secondary script to execute
  2509. * @param pos: Item equip position
  2510. * @param onskill: Skill used to trigger autobonus
  2511. * @return True on success or false otherwise
  2512. */
  2513. 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){
  2514. // Check if the same bonus already exists
  2515. for( std::shared_ptr<s_autobonus> autobonus : bonus ){
  2516. // Compare based on position and bonus script
  2517. if( autobonus->pos == pos && strcmp( script, autobonus->bonus_script ) == 0 ){
  2518. return false;
  2519. }
  2520. }
  2521. if (bonus.size() == MAX_PC_BONUS) {
  2522. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2523. return false;
  2524. }
  2525. if (!onskill) {
  2526. if (!(flag&BF_RANGEMASK))
  2527. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2528. if (!(flag&BF_WEAPONMASK))
  2529. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2530. if (!(flag&BF_SKILLMASK)) {
  2531. if (flag&(BF_MAGIC | BF_MISC))
  2532. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2533. if (flag&BF_WEAPON)
  2534. flag |= BF_NORMAL | BF_SKILL;
  2535. }
  2536. }
  2537. std::shared_ptr<s_autobonus> entry = std::make_shared<s_autobonus>();
  2538. if (rate < -10000 || rate > 10000)
  2539. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2540. entry->rate = cap_value(rate, -10000, 10000);
  2541. entry->duration = dur;
  2542. entry->active = INVALID_TIMER;
  2543. entry->atk_type = flag;
  2544. entry->pos = pos;
  2545. entry->bonus_script = aStrdup(script);
  2546. entry->other_script = (other_script ? aStrdup(other_script) : NULL);
  2547. bonus.push_back(entry);
  2548. return true;
  2549. }
  2550. /**
  2551. * Remove an autobonus from player
  2552. * @param sd: Player data
  2553. * @param bonus: Autobonus array
  2554. * @param restore: Run script on clearing or not
  2555. */
  2556. void pc_delautobonus(struct map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> &bonus, bool restore){
  2557. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  2558. while( it != bonus.end() ){
  2559. std::shared_ptr<s_autobonus> b = *it;
  2560. if( b->active != INVALID_TIMER && restore && b->bonus_script != nullptr ){
  2561. unsigned int equip_pos_idx = 0;
  2562. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2563. for (uint8 j = 0; j < EQI_MAX; j++) {
  2564. if (sd.equip_index[j] >= 0)
  2565. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2566. }
  2567. if( ( equip_pos_idx&b->pos ) == b->pos ){
  2568. script_run_autobonus(b->bonus_script, &sd, b->pos);
  2569. }else{
  2570. // Not all required items equipped anymore
  2571. restore = false;
  2572. }
  2573. }
  2574. if( restore ){
  2575. it++;
  2576. continue;
  2577. }
  2578. it = bonus.erase(it);
  2579. }
  2580. }
  2581. /**
  2582. * Execute autobonus on player
  2583. * @param sd: Player data
  2584. * @param autobonus: Autobonus to run
  2585. */
  2586. void pc_exeautobonus(struct map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> *bonus, std::shared_ptr<s_autobonus> autobonus)
  2587. {
  2588. if (autobonus->active != INVALID_TIMER)
  2589. delete_timer(autobonus->active, pc_endautobonus);
  2590. if( autobonus->other_script )
  2591. {
  2592. int j;
  2593. unsigned int equip_pos_idx = 0;
  2594. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2595. for(j = 0; j < EQI_MAX; j++) {
  2596. if(sd.equip_index[j] >= 0)
  2597. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2598. }
  2599. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2600. script_run_autobonus(autobonus->other_script,&sd,autobonus->pos);
  2601. }
  2602. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd.bl.id, (intptr_t)bonus);
  2603. status_calc_pc(&sd,SCO_FORCE);
  2604. }
  2605. /**
  2606. * Remove autobonus timer from player
  2607. */
  2608. TIMER_FUNC(pc_endautobonus){
  2609. struct map_session_data *sd = map_id2sd(id);
  2610. std::vector<std::shared_ptr<s_autobonus>> *bonus = (std::vector<std::shared_ptr<s_autobonus>> *)data;
  2611. nullpo_ret(sd);
  2612. nullpo_ret(bonus);
  2613. for( std::shared_ptr<s_autobonus> autobonus : *bonus ){
  2614. if( autobonus->active == tid ){
  2615. autobonus->active = INVALID_TIMER;
  2616. break;
  2617. }
  2618. }
  2619. status_calc_pc(sd,SCO_FORCE);
  2620. return 0;
  2621. }
  2622. /**
  2623. * Add element bonus to player when attacking
  2624. * @param sd: Player data
  2625. * @param ele: Element to adjust
  2626. * @param rate: Success chance
  2627. * @param flag: Battle flag
  2628. */
  2629. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2630. {
  2631. nullpo_retv(sd);
  2632. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2633. if (wd->addele2.size() == MAX_PC_BONUS) {
  2634. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2635. return;
  2636. }
  2637. if (!(flag&BF_RANGEMASK))
  2638. flag |= BF_SHORT | BF_LONG;
  2639. if (!(flag&BF_WEAPONMASK))
  2640. flag |= BF_WEAPON;
  2641. if (!(flag&BF_SKILLMASK)) {
  2642. if (flag&(BF_MAGIC | BF_MISC))
  2643. flag |= BF_SKILL;
  2644. if (flag&BF_WEAPON)
  2645. flag |= BF_NORMAL | BF_SKILL;
  2646. }
  2647. for (auto &it : wd->addele2) {
  2648. if (it.ele == ele && it.flag == flag) {
  2649. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2650. return;
  2651. }
  2652. }
  2653. struct s_addele2 entry = {};
  2654. if (rate < -10000 || rate > 10000)
  2655. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2656. entry.ele = ele;
  2657. entry.rate = cap_value(rate, -10000, 10000);
  2658. entry.flag = flag;
  2659. wd->addele2.push_back(entry);
  2660. }
  2661. /**
  2662. * Reduce element bonus to player when attacking
  2663. * @param sd: Player data
  2664. * @param ele: Element to adjust
  2665. * @param rate: Success chance
  2666. * @param flag: Battle flag
  2667. */
  2668. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2669. {
  2670. nullpo_retv(sd);
  2671. if (sd->subele2.size() == MAX_PC_BONUS) {
  2672. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2673. return;
  2674. }
  2675. if (!(flag&BF_RANGEMASK))
  2676. flag |= BF_SHORT | BF_LONG;
  2677. if (!(flag&BF_WEAPONMASK))
  2678. flag |= BF_WEAPON;
  2679. if (!(flag&BF_SKILLMASK)) {
  2680. if (flag&(BF_MAGIC | BF_MISC))
  2681. flag |= BF_SKILL;
  2682. if (flag&BF_WEAPON)
  2683. flag |= BF_NORMAL | BF_SKILL;
  2684. }
  2685. for (auto &it : sd->subele2) {
  2686. if (it.ele == ele && it.flag == flag) {
  2687. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2688. return;
  2689. }
  2690. }
  2691. struct s_addele2 entry = {};
  2692. if (rate < -10000 || rate > 10000)
  2693. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2694. entry.ele = ele;
  2695. entry.rate = cap_value(rate, -10000, 10000);
  2696. entry.flag = flag;
  2697. sd->subele2.push_back(entry);
  2698. }
  2699. /**
  2700. * Adjust race damage to target when attacking
  2701. * @param sd: Player data
  2702. * @param race: Race to adjust
  2703. * @param rate: Success chance
  2704. * @param flag: Battle flag
  2705. */
  2706. static void pc_bonus_subrace(struct map_session_data* sd, unsigned char race, short rate, short flag)
  2707. {
  2708. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2709. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2710. return;
  2711. }
  2712. if (!(flag&BF_RANGEMASK))
  2713. flag |= BF_SHORT | BF_LONG;
  2714. if (!(flag&BF_WEAPONMASK))
  2715. flag |= BF_WEAPON;
  2716. if (!(flag&BF_SKILLMASK)) {
  2717. if (flag&(BF_MAGIC | BF_MISC))
  2718. flag |= BF_SKILL;
  2719. if (flag&BF_WEAPON)
  2720. flag |= BF_NORMAL | BF_SKILL;
  2721. }
  2722. for (auto &it : sd->subrace3) {
  2723. if (it.race == race && it.flag == flag) {
  2724. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2725. return;
  2726. }
  2727. }
  2728. struct s_addrace2 entry = {};
  2729. if (rate < -10000 || rate > 10000)
  2730. ShowWarning("pc_bonus_subrace: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2731. entry.race = race;
  2732. entry.rate = cap_value(rate, -10000, 10000);
  2733. entry.flag = flag;
  2734. sd->subrace3.push_back(entry);
  2735. }
  2736. /**
  2737. * General item bonus for player
  2738. * @param bonus: Bonus array
  2739. * @param id: Key
  2740. * @param val: Value
  2741. * @param cap_rate: If Value is a rate value that needs to be capped
  2742. */
  2743. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2744. {
  2745. for (auto &it : bonus) {
  2746. if (it.id == id) {
  2747. if (cap_rate)
  2748. it.val = util::safe_addition_cap(it.val, val, 10000);
  2749. else
  2750. it.val += val;
  2751. return;
  2752. }
  2753. }
  2754. struct s_item_bonus entry = {};
  2755. if (cap_rate && (val < -10000 || val > 10000)) {
  2756. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2757. val = cap_value(val, -10000, 10000);
  2758. }
  2759. entry.id = id;
  2760. entry.val = val;
  2761. bonus.push_back(entry);
  2762. }
  2763. /**
  2764. * Remove HP/SP to player when attacking
  2765. * @param bonus: Bonus array
  2766. * @param rate: Success chance
  2767. * @param per: Percentage of HP/SP to vanish
  2768. * @param flag: Battle flag
  2769. */
  2770. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2771. if (bonus.size() == MAX_PC_BONUS) {
  2772. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2773. return;
  2774. }
  2775. if (!(flag&BF_RANGEMASK))
  2776. flag |= BF_SHORT | BF_LONG;
  2777. if (!(flag&BF_WEAPONMASK))
  2778. flag |= BF_WEAPON;
  2779. if (!(flag&BF_SKILLMASK)) {
  2780. if (flag&(BF_MAGIC | BF_MISC))
  2781. flag |= BF_SKILL;
  2782. if (flag&BF_WEAPON)
  2783. flag |= BF_NORMAL | BF_SKILL;
  2784. }
  2785. for (auto &it : bonus) {
  2786. if (it.flag == flag) {
  2787. it.rate = util::safe_addition_cap(it.rate, rate, (int16)10000);
  2788. it.per += per;
  2789. return;
  2790. }
  2791. }
  2792. struct s_vanish_bonus entry = {};
  2793. if (rate < -10000 || rate > 10000)
  2794. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2795. entry.rate = cap_value(rate, -10000, 10000);
  2796. entry.per = per;
  2797. entry.flag = flag;
  2798. bonus.push_back(entry);
  2799. }
  2800. /*==========================================
  2801. * Add a bonus(type) to player sd
  2802. * format: bonus bBonusName,val;
  2803. * @param sd
  2804. * @param type Bonus type used by bBonusName
  2805. * @param val Value that usually for rate or fixed value
  2806. *------------------------------------------*/
  2807. void pc_bonus(struct map_session_data *sd,int type,int val)
  2808. {
  2809. struct status_data *status;
  2810. int bonus;
  2811. nullpo_retv(sd);
  2812. status = &sd->base_status;
  2813. switch(type){
  2814. case SP_STR:
  2815. case SP_AGI:
  2816. case SP_VIT:
  2817. case SP_INT:
  2818. case SP_DEX:
  2819. case SP_LUK:
  2820. if(sd->state.lr_flag != 2)
  2821. sd->indexed_bonus.param_bonus[type-SP_STR]+=val;
  2822. break;
  2823. case SP_POW:
  2824. case SP_STA:
  2825. case SP_WIS:
  2826. case SP_SPL:
  2827. case SP_CON:
  2828. case SP_CRT:
  2829. if (sd->state.lr_flag != 2)
  2830. sd->indexed_bonus.param_bonus[type - SP_POW + PARAM_POW] += val;
  2831. break;
  2832. case SP_ATK1:
  2833. if(!sd->state.lr_flag) {
  2834. bonus = status->rhw.atk + val;
  2835. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2836. }
  2837. else if(sd->state.lr_flag == 1) {
  2838. bonus = status->lhw.atk + val;
  2839. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2840. }
  2841. break;
  2842. case SP_ATK2:
  2843. if(!sd->state.lr_flag) {
  2844. bonus = status->rhw.atk2 + val;
  2845. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2846. }
  2847. else if(sd->state.lr_flag == 1) {
  2848. bonus = status->lhw.atk2 + val;
  2849. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2850. }
  2851. break;
  2852. case SP_BASE_ATK:
  2853. if(sd->state.lr_flag != 2) {
  2854. #ifdef RENEWAL
  2855. bonus = sd->bonus.eatk + val;
  2856. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2857. #else
  2858. bonus = status->batk + val;
  2859. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2860. #endif
  2861. }
  2862. break;
  2863. case SP_DEF1:
  2864. if(sd->state.lr_flag != 2) {
  2865. bonus = status->def + val;
  2866. #ifdef RENEWAL
  2867. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2868. #else
  2869. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2870. #endif
  2871. }
  2872. break;
  2873. case SP_DEF2:
  2874. if(sd->state.lr_flag != 2) {
  2875. bonus = status->def2 + val;
  2876. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2877. }
  2878. break;
  2879. case SP_MDEF1:
  2880. if(sd->state.lr_flag != 2) {
  2881. bonus = status->mdef + val;
  2882. #ifdef RENEWAL
  2883. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2884. #else
  2885. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2886. #endif
  2887. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2888. sd->bonus.shieldmdef += bonus;
  2889. }
  2890. }
  2891. break;
  2892. case SP_MDEF2:
  2893. if(sd->state.lr_flag != 2) {
  2894. bonus = status->mdef2 + val;
  2895. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2896. }
  2897. break;
  2898. case SP_HIT:
  2899. if(sd->state.lr_flag != 2) {
  2900. bonus = status->hit + val;
  2901. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2902. } else
  2903. sd->bonus.arrow_hit+=val;
  2904. break;
  2905. case SP_FLEE1:
  2906. if(sd->state.lr_flag != 2) {
  2907. bonus = status->flee + val;
  2908. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2909. }
  2910. break;
  2911. case SP_FLEE2:
  2912. if(sd->state.lr_flag != 2) {
  2913. bonus = status->flee2 + val*10;
  2914. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2915. }
  2916. break;
  2917. case SP_CRITICAL:
  2918. if(sd->state.lr_flag != 2) {
  2919. bonus = status->cri + val*10;
  2920. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2921. } else
  2922. sd->bonus.arrow_cri += val*10;
  2923. break;
  2924. case SP_PATK:
  2925. if (sd->state.lr_flag != 2) {
  2926. bonus = status->patk + val;
  2927. status->patk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2928. }
  2929. break;
  2930. case SP_SMATK:
  2931. if (sd->state.lr_flag != 2) {
  2932. bonus = status->smatk + val;
  2933. status->smatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2934. }
  2935. break;
  2936. case SP_RES:
  2937. if (sd->state.lr_flag != 2) {
  2938. bonus = status->res + val;
  2939. status->res = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2940. }
  2941. break;
  2942. case SP_MRES:
  2943. if (sd->state.lr_flag != 2) {
  2944. bonus = status->mres + val;
  2945. status->mres = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2946. }
  2947. break;
  2948. case SP_HPLUS:
  2949. if (sd->state.lr_flag != 2) {
  2950. bonus = status->hplus + val;
  2951. status->hplus = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2952. }
  2953. break;
  2954. case SP_CRATE:
  2955. if (sd->state.lr_flag != 2) {
  2956. bonus = status->crate + val;
  2957. status->crate = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2958. }
  2959. break;
  2960. case SP_ATKELE:
  2961. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2962. switch (sd->state.lr_flag)
  2963. {
  2964. case 2:
  2965. switch (sd->status.weapon) {
  2966. case W_BOW:
  2967. case W_REVOLVER:
  2968. case W_RIFLE:
  2969. case W_GATLING:
  2970. case W_SHOTGUN:
  2971. case W_GRENADE:
  2972. //Become weapon element.
  2973. status->rhw.ele=val;
  2974. break;
  2975. default: //Become arrow element.
  2976. sd->bonus.arrow_ele=val;
  2977. break;
  2978. }
  2979. break;
  2980. case 1:
  2981. status->lhw.ele=val;
  2982. break;
  2983. default:
  2984. status->rhw.ele=val;
  2985. break;
  2986. }
  2987. break;
  2988. case SP_DEFELE:
  2989. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2990. if(sd->state.lr_flag != 2)
  2991. status->def_ele=val;
  2992. break;
  2993. case SP_MAXHP:
  2994. if(sd->state.lr_flag == 2)
  2995. break;
  2996. sd->bonus.hp += val;
  2997. break;
  2998. case SP_MAXSP:
  2999. if(sd->state.lr_flag == 2)
  3000. break;
  3001. sd->bonus.sp += val;
  3002. break;
  3003. case SP_MAXAP:
  3004. if (sd->state.lr_flag == 2)
  3005. break;
  3006. sd->bonus.ap += val;
  3007. break;
  3008. case SP_MAXHPRATE:
  3009. if(sd->state.lr_flag != 2)
  3010. sd->hprate+=val;
  3011. break;
  3012. case SP_MAXSPRATE:
  3013. if(sd->state.lr_flag != 2)
  3014. sd->sprate+=val;
  3015. break;
  3016. case SP_MAXAPRATE:
  3017. if (sd->state.lr_flag != 2)
  3018. sd->aprate += val;
  3019. break;
  3020. case SP_SPRATE:
  3021. if(sd->state.lr_flag != 2)
  3022. sd->dsprate+=val;
  3023. break;
  3024. case SP_ATTACKRANGE:
  3025. switch (sd->state.lr_flag) {
  3026. case 2:
  3027. switch (sd->status.weapon) {
  3028. case W_BOW:
  3029. case W_REVOLVER:
  3030. case W_RIFLE:
  3031. case W_GATLING:
  3032. case W_SHOTGUN:
  3033. case W_GRENADE:
  3034. status->rhw.range += val;
  3035. }
  3036. break;
  3037. case 1:
  3038. status->lhw.range += val;
  3039. break;
  3040. default:
  3041. status->rhw.range += val;
  3042. break;
  3043. }
  3044. break;
  3045. case SP_SPEED_RATE: //Non stackable increase
  3046. if(sd->state.lr_flag != 2)
  3047. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  3048. break;
  3049. case SP_SPEED_ADDRATE: //Stackable increase
  3050. if(sd->state.lr_flag != 2)
  3051. sd->bonus.speed_add_rate -= val;
  3052. break;
  3053. case SP_ASPD: //Raw increase
  3054. if(sd->state.lr_flag != 2)
  3055. sd->bonus.aspd_add -= 10*val;
  3056. break;
  3057. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  3058. if(sd->state.lr_flag != 2)
  3059. #ifndef RENEWAL_ASPD
  3060. status->aspd_rate -= 10*val;
  3061. #else
  3062. status->aspd_rate2 += val;
  3063. #endif
  3064. break;
  3065. case SP_HP_RECOV_RATE:
  3066. if(sd->state.lr_flag != 2)
  3067. sd->hprecov_rate += val;
  3068. break;
  3069. case SP_SP_RECOV_RATE:
  3070. if(sd->state.lr_flag != 2)
  3071. sd->sprecov_rate += val;
  3072. break;
  3073. case SP_CRITICAL_DEF:
  3074. if(sd->state.lr_flag != 2)
  3075. sd->bonus.critical_def += val;
  3076. break;
  3077. case SP_NEAR_ATK_DEF:
  3078. if(sd->state.lr_flag != 2)
  3079. sd->bonus.near_attack_def_rate += val;
  3080. break;
  3081. case SP_LONG_ATK_DEF:
  3082. if(sd->state.lr_flag != 2)
  3083. sd->bonus.long_attack_def_rate += val;
  3084. break;
  3085. case SP_DOUBLE_RATE:
  3086. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  3087. sd->bonus.double_rate = val;
  3088. break;
  3089. case SP_DOUBLE_ADD_RATE:
  3090. if(sd->state.lr_flag == 0)
  3091. sd->bonus.double_add_rate += val;
  3092. break;
  3093. case SP_MATK_RATE:
  3094. if(sd->state.lr_flag != 2)
  3095. sd->matk_rate += val;
  3096. break;
  3097. case SP_IGNORE_DEF_ELE:
  3098. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  3099. if(!sd->state.lr_flag)
  3100. sd->right_weapon.ignore_def_ele |= 1<<val;
  3101. else if(sd->state.lr_flag == 1)
  3102. sd->left_weapon.ignore_def_ele |= 1<<val;
  3103. break;
  3104. case SP_IGNORE_DEF_RACE:
  3105. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  3106. if(!sd->state.lr_flag)
  3107. sd->right_weapon.ignore_def_race |= 1<<val;
  3108. else if(sd->state.lr_flag == 1)
  3109. sd->left_weapon.ignore_def_race |= 1<<val;
  3110. break;
  3111. case SP_IGNORE_DEF_CLASS:
  3112. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  3113. if(!sd->state.lr_flag)
  3114. sd->right_weapon.ignore_def_class |= 1<<val;
  3115. else if(sd->state.lr_flag == 1)
  3116. sd->left_weapon.ignore_def_class |= 1<<val;
  3117. break;
  3118. case SP_ATK_RATE:
  3119. if(sd->state.lr_flag != 2)
  3120. sd->bonus.atk_rate += val;
  3121. break;
  3122. case SP_MAGIC_ATK_DEF:
  3123. if(sd->state.lr_flag != 2)
  3124. sd->bonus.magic_def_rate += val;
  3125. break;
  3126. case SP_MISC_ATK_DEF:
  3127. if(sd->state.lr_flag != 2)
  3128. sd->bonus.misc_def_rate += val;
  3129. break;
  3130. case SP_IGNORE_MDEF_ELE:
  3131. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  3132. if(sd->state.lr_flag != 2)
  3133. sd->bonus.ignore_mdef_ele |= 1<<val;
  3134. break;
  3135. case SP_IGNORE_MDEF_RACE:
  3136. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  3137. if(sd->state.lr_flag != 2)
  3138. sd->bonus.ignore_mdef_race |= 1<<val;
  3139. break;
  3140. case SP_PERFECT_HIT_RATE:
  3141. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  3142. sd->bonus.perfect_hit = val;
  3143. break;
  3144. case SP_PERFECT_HIT_ADD_RATE:
  3145. if(sd->state.lr_flag != 2)
  3146. sd->bonus.perfect_hit_add += val;
  3147. break;
  3148. case SP_CRITICAL_RATE:
  3149. if(sd->state.lr_flag != 2)
  3150. sd->critical_rate+=val;
  3151. break;
  3152. case SP_DEF_RATIO_ATK_ELE:
  3153. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  3154. if(!sd->state.lr_flag)
  3155. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  3156. else if(sd->state.lr_flag == 1)
  3157. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  3158. break;
  3159. case SP_DEF_RATIO_ATK_RACE:
  3160. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  3161. if(!sd->state.lr_flag)
  3162. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  3163. else if(sd->state.lr_flag == 1)
  3164. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  3165. break;
  3166. case SP_DEF_RATIO_ATK_CLASS:
  3167. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  3168. if(!sd->state.lr_flag)
  3169. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  3170. else if(sd->state.lr_flag == 1)
  3171. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  3172. break;
  3173. case SP_HIT_RATE:
  3174. if(sd->state.lr_flag != 2)
  3175. sd->hit_rate += val;
  3176. break;
  3177. case SP_FLEE_RATE:
  3178. if(sd->state.lr_flag != 2)
  3179. sd->flee_rate += val;
  3180. break;
  3181. case SP_FLEE2_RATE:
  3182. if(sd->state.lr_flag != 2)
  3183. sd->flee2_rate += val;
  3184. break;
  3185. case SP_DEF_RATE:
  3186. if(sd->state.lr_flag != 2)
  3187. sd->def_rate += val;
  3188. break;
  3189. case SP_DEF2_RATE:
  3190. if(sd->state.lr_flag != 2)
  3191. sd->def2_rate += val;
  3192. break;
  3193. case SP_MDEF_RATE:
  3194. if(sd->state.lr_flag != 2)
  3195. sd->mdef_rate += val;
  3196. break;
  3197. case SP_MDEF2_RATE:
  3198. if(sd->state.lr_flag != 2)
  3199. sd->mdef2_rate += val;
  3200. break;
  3201. case SP_PATK_RATE:
  3202. if (sd->state.lr_flag != 2)
  3203. sd->patk_rate += val;
  3204. break;
  3205. case SP_SMATK_RATE:
  3206. if (sd->state.lr_flag != 2)
  3207. sd->smatk_rate += val;
  3208. break;
  3209. case SP_RES_RATE:
  3210. if (sd->state.lr_flag != 2)
  3211. sd->res_rate += val;
  3212. break;
  3213. case SP_MRES_RATE:
  3214. if (sd->state.lr_flag != 2)
  3215. sd->mres_rate += val;
  3216. break;
  3217. case SP_HPLUS_RATE:
  3218. if (sd->state.lr_flag != 2)
  3219. sd->hplus_rate += val;
  3220. break;
  3221. case SP_CRATE_RATE:
  3222. if (sd->state.lr_flag != 2)
  3223. sd->crate_rate += val;
  3224. break;
  3225. case SP_RESTART_FULL_RECOVER:
  3226. if(sd->state.lr_flag != 2)
  3227. sd->special_state.restart_full_recover = 1;
  3228. break;
  3229. case SP_NO_CASTCANCEL:
  3230. if(sd->state.lr_flag != 2)
  3231. sd->special_state.no_castcancel = 1;
  3232. break;
  3233. case SP_NO_CASTCANCEL2:
  3234. if(sd->state.lr_flag != 2)
  3235. sd->special_state.no_castcancel2 = 1;
  3236. break;
  3237. case SP_NO_SIZEFIX:
  3238. if(sd->state.lr_flag != 2)
  3239. sd->special_state.no_sizefix = 1;
  3240. break;
  3241. case SP_NO_MAGIC_DAMAGE:
  3242. if(sd->state.lr_flag == 2)
  3243. break;
  3244. val+= sd->special_state.no_magic_damage;
  3245. sd->special_state.no_magic_damage = cap_value(val,0,100);
  3246. break;
  3247. case SP_NO_WEAPON_DAMAGE:
  3248. if(sd->state.lr_flag == 2)
  3249. break;
  3250. val+= sd->special_state.no_weapon_damage;
  3251. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  3252. break;
  3253. case SP_NO_MISC_DAMAGE:
  3254. if(sd->state.lr_flag == 2)
  3255. break;
  3256. val+= sd->special_state.no_misc_damage;
  3257. sd->special_state.no_misc_damage = cap_value(val,0,100);
  3258. break;
  3259. case SP_NO_GEMSTONE:
  3260. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  3261. sd->special_state.no_gemstone = 1;
  3262. break;
  3263. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  3264. if(sd->state.lr_flag != 2) {
  3265. sd->special_state.intravision = 1;
  3266. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  3267. }
  3268. break;
  3269. case SP_NO_KNOCKBACK:
  3270. if(sd->state.lr_flag != 2)
  3271. sd->special_state.no_knockback = 1;
  3272. break;
  3273. case SP_SPLASH_RANGE:
  3274. if(sd->bonus.splash_range < val)
  3275. sd->bonus.splash_range = val;
  3276. break;
  3277. case SP_SPLASH_ADD_RANGE:
  3278. sd->bonus.splash_add_range += val;
  3279. break;
  3280. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  3281. if(sd->state.lr_flag != 2)
  3282. sd->bonus.short_weapon_damage_return += val;
  3283. break;
  3284. case SP_LONG_WEAPON_DAMAGE_RETURN:
  3285. if(sd->state.lr_flag != 2)
  3286. sd->bonus.long_weapon_damage_return += val;
  3287. break;
  3288. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  3289. if(sd->state.lr_flag != 2)
  3290. sd->bonus.magic_damage_return += val;
  3291. break;
  3292. case SP_REDUCE_DAMAGE_RETURN:
  3293. if (sd->state.lr_flag != 2)
  3294. sd->bonus.reduce_damage_return += val;
  3295. break;
  3296. case SP_ALL_STATS: // [Valaris]
  3297. if(sd->state.lr_flag!=2) {
  3298. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3299. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3300. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3301. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  3302. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3303. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  3304. }
  3305. break;
  3306. case SP_ALL_TRAIT_STATS:
  3307. if (sd->state.lr_flag != 2) {
  3308. sd->indexed_bonus.param_bonus[PARAM_POW] += val;
  3309. sd->indexed_bonus.param_bonus[PARAM_STA] += val;
  3310. sd->indexed_bonus.param_bonus[PARAM_WIS] += val;
  3311. sd->indexed_bonus.param_bonus[PARAM_SPL] += val;
  3312. sd->indexed_bonus.param_bonus[PARAM_CON] += val;
  3313. sd->indexed_bonus.param_bonus[PARAM_CRT] += val;
  3314. }
  3315. break;
  3316. case SP_AGI_VIT: // [Valaris]
  3317. if(sd->state.lr_flag!=2) {
  3318. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3319. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3320. }
  3321. break;
  3322. case SP_AGI_DEX_STR: // [Valaris]
  3323. if(sd->state.lr_flag!=2) {
  3324. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3325. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3326. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3327. }
  3328. break;
  3329. case SP_PERFECT_HIDE: // [Valaris]
  3330. if(sd->state.lr_flag!=2)
  3331. sd->special_state.perfect_hiding=1;
  3332. break;
  3333. case SP_UNBREAKABLE:
  3334. if(sd->state.lr_flag!=2)
  3335. sd->bonus.unbreakable += val;
  3336. break;
  3337. case SP_UNBREAKABLE_WEAPON:
  3338. if(sd->state.lr_flag != 2)
  3339. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3340. break;
  3341. case SP_UNBREAKABLE_ARMOR:
  3342. if(sd->state.lr_flag != 2)
  3343. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3344. break;
  3345. case SP_UNBREAKABLE_HELM:
  3346. if(sd->state.lr_flag != 2)
  3347. sd->bonus.unbreakable_equip |= EQP_HELM;
  3348. break;
  3349. case SP_UNBREAKABLE_SHIELD:
  3350. if(sd->state.lr_flag != 2)
  3351. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3352. break;
  3353. case SP_UNBREAKABLE_GARMENT:
  3354. if(sd->state.lr_flag != 2)
  3355. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3356. break;
  3357. case SP_UNBREAKABLE_SHOES:
  3358. if(sd->state.lr_flag != 2)
  3359. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3360. break;
  3361. case SP_CLASSCHANGE: // [Valaris]
  3362. if(sd->state.lr_flag !=2)
  3363. sd->bonus.classchange=val;
  3364. break;
  3365. case SP_SHORT_ATK_RATE:
  3366. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3367. sd->bonus.short_attack_atk_rate+=val;
  3368. break;
  3369. case SP_LONG_ATK_RATE:
  3370. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3371. sd->bonus.long_attack_atk_rate+=val;
  3372. break;
  3373. case SP_BREAK_WEAPON_RATE:
  3374. if(sd->state.lr_flag != 2)
  3375. sd->bonus.break_weapon_rate+=val;
  3376. break;
  3377. case SP_BREAK_ARMOR_RATE:
  3378. if(sd->state.lr_flag != 2)
  3379. sd->bonus.break_armor_rate+=val;
  3380. break;
  3381. case SP_ADD_STEAL_RATE:
  3382. if(sd->state.lr_flag != 2)
  3383. sd->bonus.add_steal_rate+=val;
  3384. break;
  3385. case SP_DELAYRATE:
  3386. if(sd->state.lr_flag != 2)
  3387. sd->bonus.delayrate -= val;
  3388. break;
  3389. case SP_CRIT_ATK_RATE:
  3390. if(sd->state.lr_flag != 2)
  3391. sd->bonus.crit_atk_rate += val;
  3392. break;
  3393. case SP_CRIT_DEF_RATE:
  3394. if(sd->state.lr_flag != 2)
  3395. sd->bonus.crit_def_rate += val;
  3396. break;
  3397. case SP_NO_REGEN:
  3398. if(sd->state.lr_flag != 2)
  3399. sd->regen.state.block|=val;
  3400. break;
  3401. case SP_UNSTRIPABLE_WEAPON:
  3402. if(sd->state.lr_flag != 2)
  3403. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3404. break;
  3405. case SP_UNSTRIPABLE:
  3406. case SP_UNSTRIPABLE_ARMOR:
  3407. if(sd->state.lr_flag != 2)
  3408. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3409. break;
  3410. case SP_UNSTRIPABLE_HELM:
  3411. if(sd->state.lr_flag != 2)
  3412. sd->bonus.unstripable_equip |= EQP_HELM;
  3413. break;
  3414. case SP_UNSTRIPABLE_SHIELD:
  3415. if(sd->state.lr_flag != 2)
  3416. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3417. break;
  3418. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3419. if(!sd->state.lr_flag) {
  3420. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3421. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3422. } else if(sd->state.lr_flag == 1) {
  3423. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3424. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3425. }
  3426. break;
  3427. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3428. if(!sd->state.lr_flag) {
  3429. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3430. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3431. } else if(sd->state.lr_flag == 1) {
  3432. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3433. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3434. }
  3435. break;
  3436. case SP_SP_GAIN_VALUE:
  3437. if(!sd->state.lr_flag)
  3438. sd->bonus.sp_gain_value += val;
  3439. break;
  3440. case SP_HP_GAIN_VALUE:
  3441. if(!sd->state.lr_flag)
  3442. sd->bonus.hp_gain_value += val;
  3443. break;
  3444. case SP_LONG_SP_GAIN_VALUE:
  3445. if(!sd->state.lr_flag)
  3446. sd->bonus.long_sp_gain_value += val;
  3447. case SP_LONG_HP_GAIN_VALUE:
  3448. if(!sd->state.lr_flag)
  3449. sd->bonus.long_hp_gain_value += val;
  3450. break;
  3451. case SP_MAGIC_SP_GAIN_VALUE:
  3452. if(!sd->state.lr_flag)
  3453. sd->bonus.magic_sp_gain_value += val;
  3454. break;
  3455. case SP_MAGIC_HP_GAIN_VALUE:
  3456. if(!sd->state.lr_flag)
  3457. sd->bonus.magic_hp_gain_value += val;
  3458. break;
  3459. case SP_ADD_HEAL_RATE:
  3460. if(sd->state.lr_flag != 2)
  3461. sd->bonus.add_heal_rate += val;
  3462. break;
  3463. case SP_ADD_HEAL2_RATE:
  3464. if(sd->state.lr_flag != 2)
  3465. sd->bonus.add_heal2_rate += val;
  3466. break;
  3467. case SP_ADD_ITEM_HEAL_RATE:
  3468. if(sd->state.lr_flag != 2)
  3469. sd->bonus.itemhealrate2 += val;
  3470. break;
  3471. case SP_EMATK:
  3472. if(sd->state.lr_flag != 2)
  3473. sd->bonus.ematk += val;
  3474. break;
  3475. case SP_ADD_VARIABLECAST:
  3476. if (sd->state.lr_flag != 2)
  3477. sd->bonus.add_varcast += val;
  3478. break;
  3479. #ifdef RENEWAL_CAST
  3480. case SP_FIXCASTRATE:
  3481. if(sd->state.lr_flag != 2)
  3482. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3483. break;
  3484. case SP_ADD_FIXEDCAST:
  3485. if(sd->state.lr_flag != 2)
  3486. sd->bonus.add_fixcast += val;
  3487. break;
  3488. case SP_CASTRATE:
  3489. case SP_VARCASTRATE:
  3490. if(sd->state.lr_flag != 2)
  3491. sd->bonus.varcastrate -= val;
  3492. break;
  3493. #else
  3494. case SP_ADD_FIXEDCAST:
  3495. case SP_FIXCASTRATE:
  3496. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3497. break;
  3498. case SP_VARCASTRATE:
  3499. case SP_CASTRATE:
  3500. if(sd->state.lr_flag != 2)
  3501. sd->castrate += val;
  3502. break;
  3503. #endif
  3504. case SP_ADDMAXWEIGHT:
  3505. if (sd->state.lr_flag != 2)
  3506. sd->add_max_weight += val;
  3507. break;
  3508. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3509. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3510. break;
  3511. case SP_ABSORB_DMG_MAXHP2:
  3512. sd->bonus.absorb_dmg_maxhp2 = max(sd->bonus.absorb_dmg_maxhp2, val);
  3513. break;
  3514. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3515. if (sd->state.lr_flag != 2)
  3516. sd->bonus.critical_rangeatk += val*10;
  3517. else
  3518. sd->bonus.arrow_cri += val*10;
  3519. break;
  3520. case SP_WEAPON_ATK_RATE:
  3521. if (sd->state.lr_flag != 2)
  3522. sd->bonus.weapon_atk_rate += val;
  3523. break;
  3524. case SP_WEAPON_MATK_RATE:
  3525. if (sd->state.lr_flag != 2)
  3526. sd->bonus.weapon_matk_rate += val;
  3527. break;
  3528. case SP_NO_MADO_FUEL:
  3529. if (sd->state.lr_flag != 2)
  3530. sd->special_state.no_mado_fuel = 1;
  3531. break;
  3532. case SP_NO_WALK_DELAY:
  3533. if (sd->state.lr_flag != 2)
  3534. sd->special_state.no_walk_delay = 1;
  3535. break;
  3536. case SP_ADD_ITEM_SPHEAL_RATE:
  3537. if(sd->state.lr_flag != 2)
  3538. sd->bonus.itemsphealrate2 += val;
  3539. break;
  3540. default:
  3541. if (current_equip_combo_pos > 0) {
  3542. 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);
  3543. }
  3544. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3545. 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);
  3546. }
  3547. else {
  3548. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3549. }
  3550. break;
  3551. }
  3552. }
  3553. /*==========================================
  3554. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3555. * format: bonus2 bBonusName,type2,val;
  3556. * @param sd
  3557. * @param type Bonus type used by bBonusName
  3558. * @param type2
  3559. * @param val Value that usually for rate or fixed value
  3560. *------------------------------------------*/
  3561. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3562. {
  3563. nullpo_retv(sd);
  3564. switch(type){
  3565. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3566. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3567. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3568. sd->right_weapon.addele[type2]+=val;
  3569. else if(sd->state.lr_flag == 1)
  3570. sd->left_weapon.addele[type2]+=val;
  3571. else if(sd->state.lr_flag == 2)
  3572. sd->indexed_bonus.arrow_addele[type2]+=val;
  3573. break;
  3574. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3575. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3576. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3577. sd->right_weapon.addrace[type2]+=val;
  3578. else if(sd->state.lr_flag == 1)
  3579. sd->left_weapon.addrace[type2]+=val;
  3580. else if(sd->state.lr_flag == 2)
  3581. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3582. break;
  3583. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3584. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3585. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3586. sd->right_weapon.addclass[type2]+=val;
  3587. else if(sd->state.lr_flag == 1)
  3588. sd->left_weapon.addclass[type2]+=val;
  3589. else if(sd->state.lr_flag == 2)
  3590. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3591. break;
  3592. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3593. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3594. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3595. sd->right_weapon.addsize[type2]+=val;
  3596. else if(sd->state.lr_flag == 1)
  3597. sd->left_weapon.addsize[type2]+=val;
  3598. else if(sd->state.lr_flag == 2)
  3599. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3600. break;
  3601. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3602. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3603. if(sd->state.lr_flag != 2)
  3604. sd->indexed_bonus.subele_script[type2] += val;
  3605. break;
  3606. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3607. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3608. if(sd->state.lr_flag != 2)
  3609. sd->indexed_bonus.subrace[type2]+=val;
  3610. break;
  3611. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3612. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3613. if(sd->state.lr_flag != 2)
  3614. sd->indexed_bonus.subclass[type2]+=val;
  3615. break;
  3616. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3617. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3618. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3619. break;
  3620. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3621. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3622. 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);
  3623. break;
  3624. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3625. if(sd->state.lr_flag == 2)
  3626. break;
  3627. if (sd->reseff.size() == MAX_PC_BONUS) {
  3628. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3629. break;
  3630. }
  3631. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3632. break;
  3633. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3634. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3635. if(sd->state.lr_flag != 2)
  3636. sd->indexed_bonus.magic_addele_script[type2] += val;
  3637. break;
  3638. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3639. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3640. if(sd->state.lr_flag != 2)
  3641. sd->indexed_bonus.magic_addrace[type2]+=val;
  3642. break;
  3643. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3644. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3645. if(sd->state.lr_flag != 2)
  3646. sd->indexed_bonus.magic_addclass[type2]+=val;
  3647. break;
  3648. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3649. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3650. if(sd->state.lr_flag != 2)
  3651. sd->indexed_bonus.magic_addsize[type2]+=val;
  3652. break;
  3653. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3654. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3655. if(sd->state.lr_flag != 2)
  3656. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3657. break;
  3658. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3659. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3660. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3661. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3662. break;
  3663. }
  3664. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3665. }
  3666. break;
  3667. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3668. if(sd->state.lr_flag == 2)
  3669. break;
  3670. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3671. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3672. break;
  3673. }
  3674. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3675. break;
  3676. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3677. if(sd->state.lr_flag == 2)
  3678. break;
  3679. if (sd->add_def.size() == MAX_PC_BONUS) {
  3680. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3681. break;
  3682. }
  3683. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3684. break;
  3685. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3686. if(sd->state.lr_flag == 2)
  3687. break;
  3688. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3689. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3690. break;
  3691. }
  3692. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3693. break;
  3694. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3695. if(!sd->state.lr_flag) {
  3696. sd->right_weapon.hp_drain_rate.rate += type2;
  3697. sd->right_weapon.hp_drain_rate.per += val;
  3698. }
  3699. else if(sd->state.lr_flag == 1) {
  3700. sd->left_weapon.hp_drain_rate.rate += type2;
  3701. sd->left_weapon.hp_drain_rate.per += val;
  3702. }
  3703. break;
  3704. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3705. if(!sd->state.lr_flag) {
  3706. sd->right_weapon.sp_drain_rate.rate += type2;
  3707. sd->right_weapon.sp_drain_rate.per += val;
  3708. }
  3709. else if(sd->state.lr_flag == 1) {
  3710. sd->left_weapon.sp_drain_rate.rate += type2;
  3711. sd->left_weapon.sp_drain_rate.per += val;
  3712. }
  3713. break;
  3714. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3715. if(sd->state.lr_flag != 2) {
  3716. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3717. }
  3718. break;
  3719. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3720. if(sd->state.lr_flag != 2) {
  3721. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3722. }
  3723. break;
  3724. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3725. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3726. sd->bonus.get_zeny_rate = val;
  3727. sd->bonus.get_zeny_num = type2;
  3728. }
  3729. break;
  3730. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3731. if(sd->state.lr_flag != 2) {
  3732. sd->bonus.get_zeny_rate += val;
  3733. sd->bonus.get_zeny_num += type2;
  3734. }
  3735. break;
  3736. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3737. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3738. if(sd->state.lr_flag == 2)
  3739. break;
  3740. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3741. sd->special_state.bonus_coma = 1;
  3742. break;
  3743. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3744. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3745. if(sd->state.lr_flag == 2)
  3746. break;
  3747. sd->indexed_bonus.weapon_coma_race[type2] += val;
  3748. sd->special_state.bonus_coma = 1;
  3749. break;
  3750. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3751. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3752. if(sd->state.lr_flag == 2)
  3753. break;
  3754. sd->indexed_bonus.weapon_coma_class[type2] += val;
  3755. sd->special_state.bonus_coma = 1;
  3756. break;
  3757. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3758. if(sd->state.lr_flag != 2)
  3759. sd->indexed_bonus.weapon_atk[type2]+=val;
  3760. break;
  3761. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3762. if(sd->state.lr_flag != 2)
  3763. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  3764. break;
  3765. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3766. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3767. if(sd->state.lr_flag != 2)
  3768. sd->indexed_bonus.critaddrace[type2] += val*10;
  3769. break;
  3770. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3771. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3772. if(sd->state.lr_flag != 2)
  3773. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3774. break;
  3775. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3776. if(sd->state.lr_flag == 2)
  3777. break;
  3778. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3779. 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);
  3780. break;
  3781. }
  3782. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3783. break;
  3784. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3785. if(sd->state.lr_flag == 2)
  3786. break;
  3787. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3788. 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);
  3789. break;
  3790. }
  3791. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3792. break;
  3793. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3794. if(sd->state.lr_flag == 2)
  3795. break;
  3796. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3797. 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);
  3798. break;
  3799. }
  3800. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3801. break;
  3802. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3803. if(sd->state.lr_flag == 2)
  3804. break;
  3805. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3806. 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);
  3807. break;
  3808. }
  3809. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3810. break;
  3811. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3812. if(sd->state.lr_flag != 2) {
  3813. sd->hp_loss.value = type2;
  3814. sd->hp_loss.rate = val;
  3815. }
  3816. break;
  3817. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3818. if(sd->state.lr_flag != 2) {
  3819. sd->hp_regen.value = type2;
  3820. sd->hp_regen.rate = val;
  3821. }
  3822. break;
  3823. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3824. if (sd->state.lr_flag != 2) {
  3825. sd->percent_hp_regen.value = type2;
  3826. sd->percent_hp_regen.rate = val;
  3827. }
  3828. break;
  3829. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3830. if (sd->state.lr_flag != 2) {
  3831. sd->percent_sp_regen.value = type2;
  3832. sd->percent_sp_regen.rate = val;
  3833. }
  3834. break;
  3835. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3836. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3837. if(sd->state.lr_flag != 2)
  3838. sd->right_weapon.addrace2[type2] += val;
  3839. else
  3840. sd->left_weapon.addrace2[type2] += val;
  3841. break;
  3842. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3843. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3844. if(sd->state.lr_flag != 2)
  3845. sd->indexed_bonus.subsize[type2]+=val;
  3846. break;
  3847. case SP_WEAPON_SUBSIZE: // bonus2 bWeaponSubSize,s,x;
  3848. PC_BONUS_CHK_SIZE(type2, SP_WEAPON_SUBSIZE);
  3849. if (sd->state.lr_flag != 2)
  3850. sd->indexed_bonus.weapon_subsize[type2] += val;
  3851. break;
  3852. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  3853. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  3854. if(sd->state.lr_flag != 2)
  3855. sd->indexed_bonus.magic_subsize[type2]+=val;
  3856. break;
  3857. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3858. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3859. if(sd->state.lr_flag != 2)
  3860. sd->indexed_bonus.subrace2[type2]+=val;
  3861. break;
  3862. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3863. if(sd->state.lr_flag == 2)
  3864. break;
  3865. if( !item_db.exists( type2 ) ){
  3866. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3867. break;
  3868. }
  3869. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3870. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3871. break;
  3872. }
  3873. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3874. break;
  3875. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3876. if (sd->state.lr_flag == 2)
  3877. break;
  3878. if (!type2 || !itemdb_group.exists(type2)) {
  3879. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3880. break;
  3881. }
  3882. if (sd->itemgrouphealrate.size() == MAX_PC_BONUS) {
  3883. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3884. break;
  3885. }
  3886. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3887. break;
  3888. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3889. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3890. if(sd->state.lr_flag != 2)
  3891. sd->indexed_bonus.expaddrace[type2]+=val;
  3892. break;
  3893. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3894. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3895. if(sd->state.lr_flag != 2)
  3896. sd->indexed_bonus.expaddclass[type2]+=val;
  3897. break;
  3898. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3899. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3900. if(sd->state.lr_flag != 2)
  3901. sd->indexed_bonus.sp_gain_race[type2]+=val;
  3902. break;
  3903. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3904. if (sd->state.lr_flag != 2)
  3905. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3906. break;
  3907. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3908. if (sd->state.lr_flag != 2)
  3909. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3910. break;
  3911. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3912. if(sd->state.lr_flag != 2) {
  3913. sd->sp_loss.value = type2;
  3914. sd->sp_loss.rate = val;
  3915. }
  3916. break;
  3917. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3918. if(sd->state.lr_flag != 2) {
  3919. sd->sp_regen.value = type2;
  3920. sd->sp_regen.rate = val;
  3921. }
  3922. break;
  3923. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3924. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3925. if(!sd->state.lr_flag) {
  3926. sd->right_weapon.hp_drain_race[type2] += val;
  3927. }
  3928. else if(sd->state.lr_flag == 1) {
  3929. sd->left_weapon.hp_drain_race[type2] += val;
  3930. }
  3931. break;
  3932. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3933. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3934. if(!sd->state.lr_flag) {
  3935. sd->right_weapon.sp_drain_race[type2] += val;
  3936. }
  3937. else if(sd->state.lr_flag == 1) {
  3938. sd->left_weapon.sp_drain_race[type2] += val;
  3939. }
  3940. break;
  3941. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3942. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3943. if(!sd->state.lr_flag) {
  3944. sd->right_weapon.hp_drain_class[type2] += val;
  3945. }
  3946. else if(sd->state.lr_flag == 1) {
  3947. sd->left_weapon.hp_drain_class[type2] += val;
  3948. }
  3949. break;
  3950. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3951. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3952. if(!sd->state.lr_flag) {
  3953. sd->right_weapon.sp_drain_class[type2] += val;
  3954. }
  3955. else if(sd->state.lr_flag == 1) {
  3956. sd->left_weapon.sp_drain_class[type2] += val;
  3957. }
  3958. break;
  3959. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3960. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3961. if(sd->state.lr_flag != 2)
  3962. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  3963. break;
  3964. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3965. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3966. if(sd->state.lr_flag != 2)
  3967. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  3968. break;
  3969. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3970. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3971. if(sd->state.lr_flag != 2)
  3972. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  3973. break;
  3974. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3975. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3976. if (sd->state.lr_flag != 2)
  3977. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  3978. break;
  3979. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3980. if(sd->state.lr_flag == 2)
  3981. break;
  3982. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3983. 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);
  3984. break;
  3985. }
  3986. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3987. break;
  3988. case SP_SKILL_DELAY:
  3989. if(sd->state.lr_flag == 2)
  3990. break;
  3991. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3992. 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);
  3993. break;
  3994. }
  3995. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3996. break;
  3997. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3998. if(sd->state.lr_flag == 2)
  3999. break;
  4000. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  4001. 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);
  4002. break;
  4003. }
  4004. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  4005. break;
  4006. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  4007. if (sd->state.lr_flag == 2)
  4008. break;
  4009. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  4010. 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);
  4011. break;
  4012. }
  4013. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  4014. break;
  4015. #ifdef RENEWAL_CAST
  4016. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4017. if(sd->state.lr_flag == 2)
  4018. break;
  4019. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  4020. 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);
  4021. break;
  4022. }
  4023. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  4024. break;
  4025. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4026. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4027. if(sd->state.lr_flag == 2)
  4028. break;
  4029. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  4030. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4031. break;
  4032. }
  4033. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  4034. break;
  4035. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4036. if(sd->state.lr_flag == 2)
  4037. break;
  4038. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  4039. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4040. break;
  4041. }
  4042. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  4043. break;
  4044. #else
  4045. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4046. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4047. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  4048. break;
  4049. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4050. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4051. if(sd->state.lr_flag == 2)
  4052. break;
  4053. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4054. 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);
  4055. break;
  4056. }
  4057. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  4058. break;
  4059. #endif
  4060. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  4061. if(sd->state.lr_flag == 2)
  4062. break;
  4063. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  4064. 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);
  4065. break;
  4066. }
  4067. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  4068. break;
  4069. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  4070. if (sd->subskill.size() == MAX_PC_BONUS) {
  4071. 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);
  4072. break;
  4073. }
  4074. pc_bonus_itembonus(sd->subskill, type2, val, false);
  4075. break;
  4076. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  4077. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  4078. sd->indexed_bonus.subdefele[type2] += val;
  4079. break;
  4080. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  4081. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  4082. sd->indexed_bonus.coma_class[type2] += val;
  4083. sd->special_state.bonus_coma = 1;
  4084. break;
  4085. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  4086. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  4087. sd->indexed_bonus.coma_race[type2] += val;
  4088. sd->special_state.bonus_coma = 1;
  4089. break;
  4090. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  4091. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  4092. if(sd->state.lr_flag != 2)
  4093. sd->indexed_bonus.magic_addrace2[type2] += val;
  4094. break;
  4095. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  4096. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  4097. if (sd->state.lr_flag != 2)
  4098. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  4099. break;
  4100. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  4101. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  4102. if (sd->state.lr_flag != 2)
  4103. sd->indexed_bonus.dropaddrace[type2] += val;
  4104. break;
  4105. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  4106. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  4107. if (sd->state.lr_flag != 2)
  4108. sd->indexed_bonus.dropaddclass[type2] += val;
  4109. break;
  4110. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  4111. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  4112. sd->indexed_bonus.magic_subdefele[type2] += val;
  4113. break;
  4114. case SP_ADD_ITEM_SPHEAL_RATE: // bonus2 bAddItemSPHealRate,iid,n;
  4115. if( sd->state.lr_flag == 2 ){
  4116. break;
  4117. }
  4118. if( !item_db.exists( type2 ) ){
  4119. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE Invalid item with id %d\n", type2 );
  4120. break;
  4121. }
  4122. if( sd->itemsphealrate.size() == MAX_PC_BONUS ){
  4123. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4124. break;
  4125. }
  4126. pc_bonus_itembonus( sd->itemsphealrate, type2, val, false );
  4127. break;
  4128. case SP_ADD_ITEMGROUP_SPHEAL_RATE: // bonus2 bAddItemGroupSPHealRate,ig,n;
  4129. if( sd->state.lr_flag == 2 ){
  4130. break;
  4131. }
  4132. if( !type2 || !itemdb_group.exists( type2 ) ){
  4133. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Invalid item group with id %d\n", type2 );
  4134. break;
  4135. }
  4136. if( sd->itemgroupsphealrate.size() == MAX_PC_BONUS ){
  4137. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4138. break;
  4139. }
  4140. pc_bonus_itembonus( sd->itemgroupsphealrate, type2, val, false );
  4141. break;
  4142. default:
  4143. if (current_equip_combo_pos > 0) {
  4144. 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);
  4145. }
  4146. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4147. 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);
  4148. }
  4149. else {
  4150. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  4151. }
  4152. break;
  4153. }
  4154. }
  4155. /**
  4156. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  4157. * @param sd
  4158. * @param type Bonus type used by bBonusName
  4159. * @param type2
  4160. * @param type3
  4161. * @param val Value that usually for rate or fixed value
  4162. */
  4163. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  4164. {
  4165. nullpo_retv(sd);
  4166. switch(type){
  4167. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  4168. if(sd->state.lr_flag != 2)
  4169. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  4170. break;
  4171. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  4172. if(sd->state.lr_flag != 2)
  4173. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  4174. break;
  4175. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  4176. if(sd->state.lr_flag != 2)
  4177. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  4178. break;
  4179. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  4180. if(sd->state.lr_flag != 2)
  4181. {
  4182. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4183. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4184. pc_bonus_autospell(sd->autospell, type2, type3, val, 0, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4185. }
  4186. break;
  4187. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  4188. if(sd->state.lr_flag != 2)
  4189. {
  4190. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4191. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4192. pc_bonus_autospell(sd->autospell2, type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4193. }
  4194. break;
  4195. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  4196. if (sd->state.lr_flag != 2)
  4197. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  4198. break;
  4199. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  4200. if (sd->state.lr_flag != 2)
  4201. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  4202. break;
  4203. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  4204. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4205. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  4206. break;
  4207. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  4208. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4209. if(sd->state.lr_flag != 2)
  4210. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  4211. break;
  4212. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  4213. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  4214. if( sd->state.lr_flag != 2 )
  4215. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  4216. break;
  4217. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  4218. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  4219. if (sd->state.lr_flag != 2)
  4220. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  4221. break;
  4222. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  4223. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  4224. if (sd->state.lr_flag != 2)
  4225. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  4226. break;
  4227. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  4228. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  4229. if (sd->state.lr_flag != 2)
  4230. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  4231. break;
  4232. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  4233. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  4234. if(sd->state.lr_flag != 2) {
  4235. sd->sp_vanish_race[type2].rate += type3;
  4236. sd->sp_vanish_race[type2].per += val;
  4237. }
  4238. break;
  4239. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  4240. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  4241. if(sd->state.lr_flag != 2) {
  4242. sd->hp_vanish_race[type2].rate += type3;
  4243. sd->hp_vanish_race[type2].per += val;
  4244. }
  4245. break;
  4246. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  4247. if(sd->state.lr_flag != 2) {
  4248. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  4249. }
  4250. break;
  4251. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  4252. if(sd->state.lr_flag != 2) {
  4253. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  4254. }
  4255. break;
  4256. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  4257. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  4258. if (sd->state.lr_flag == 2)
  4259. break;
  4260. //! CONFIRM: Is it not stackable? Does not check max or min value?
  4261. //if (type3 > sd->norecover_state_race[type2].rate) {
  4262. // sd->norecover_state_race[type2].rate = type3;
  4263. // sd->norecover_state_race[type2].tick = val;
  4264. // break;
  4265. //}
  4266. sd->norecover_state_race[type2].rate = type3;
  4267. sd->norecover_state_race[type2].tick = val;
  4268. break;
  4269. default:
  4270. if (current_equip_combo_pos > 0) {
  4271. 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);
  4272. }
  4273. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4274. 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);
  4275. }
  4276. else {
  4277. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  4278. }
  4279. break;
  4280. }
  4281. }
  4282. /**
  4283. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  4284. * @param sd
  4285. * @param type Bonus type used by bBonusName
  4286. * @param type2
  4287. * @param type3
  4288. * @param type4
  4289. * @param val Value that usually for rate or fixed value
  4290. */
  4291. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  4292. {
  4293. nullpo_retv(sd);
  4294. switch(type){
  4295. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4296. if(sd->state.lr_flag != 2)
  4297. pc_bonus_autospell(sd->autospell, type2, type3, type4, 0, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4298. break;
  4299. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4300. if(sd->state.lr_flag != 2)
  4301. pc_bonus_autospell(sd->autospell2, type2, type3, type4, BF_NORMAL|BF_SKILL, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4302. break;
  4303. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4304. if(sd->state.lr_flag != 2)
  4305. {
  4306. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4307. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4308. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, val, current_equip_card_id, target ? AUTOSPELL_FORCE_TARGET : AUTOSPELL_FORCE_SELF);
  4309. }
  4310. break;
  4311. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4312. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4313. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  4314. break;
  4315. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4316. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4317. if (sd->state.lr_flag != 2)
  4318. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4319. break;
  4320. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4321. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4322. if( sd->state.lr_flag != 2 )
  4323. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4324. break;
  4325. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4326. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4327. if(sd->state.lr_flag == 2)
  4328. break;
  4329. sd->def_set_race[type2].rate = type3;
  4330. sd->def_set_race[type2].tick = type4;
  4331. sd->def_set_race[type2].value = val;
  4332. break;
  4333. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4334. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4335. if(sd->state.lr_flag == 2)
  4336. break;
  4337. sd->mdef_set_race[type2].rate = type3;
  4338. sd->mdef_set_race[type2].tick = type4;
  4339. sd->mdef_set_race[type2].value = val;
  4340. break;
  4341. default:
  4342. if (current_equip_combo_pos > 0) {
  4343. 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);
  4344. }
  4345. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4346. 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);
  4347. }
  4348. else {
  4349. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4350. }
  4351. break;
  4352. }
  4353. }
  4354. /**
  4355. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4356. * @param sd
  4357. * @param type Bonus type used by bBonusName
  4358. * @param type2
  4359. * @param type3
  4360. * @param type4
  4361. * @param val Value that usually for rate or fixed value
  4362. */
  4363. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  4364. {
  4365. nullpo_retv(sd);
  4366. switch(type){
  4367. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4368. if(sd->state.lr_flag != 2)
  4369. pc_bonus_autospell(sd->autospell, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4370. break;
  4371. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4372. if(sd->state.lr_flag != 2)
  4373. pc_bonus_autospell(sd->autospell2, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4374. break;
  4375. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4376. if(sd->state.lr_flag != 2)
  4377. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4378. break;
  4379. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4380. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4381. if( sd->state.lr_flag != 2 )
  4382. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4383. break;
  4384. default:
  4385. if (current_equip_combo_pos > 0) {
  4386. 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);
  4387. }
  4388. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4389. 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);
  4390. }
  4391. else {
  4392. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4393. }
  4394. break;
  4395. }
  4396. }
  4397. /*==========================================
  4398. * Grants a player a given skill. Flag values are:
  4399. * 0 - Grant permanent skill to be bound to skill tree
  4400. * 1 - Grant an item skill (temporary)
  4401. * 2 - Like 1, except the level granted can stack with previously learned level.
  4402. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4403. *------------------------------------------*/
  4404. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  4405. uint16 idx = 0;
  4406. nullpo_ret(sd);
  4407. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4408. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4409. return false;
  4410. }
  4411. if (level > MAX_SKILL_LEVEL) {
  4412. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4413. return false;
  4414. }
  4415. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4416. 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);
  4417. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4418. }
  4419. switch (type) {
  4420. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4421. sd->status.skill[idx].id = skill_id;
  4422. sd->status.skill[idx].lv = level;
  4423. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4424. if (level == 0) { //Remove skill.
  4425. sd->status.skill[idx].id = 0;
  4426. clif_deleteskill(sd,skill_id);
  4427. } else
  4428. clif_addskill(sd,skill_id);
  4429. 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.
  4430. status_calc_pc(sd, SCO_NONE);
  4431. break;
  4432. case ADDSKILL_TEMP: //Item bonus skill.
  4433. if (sd->status.skill[idx].id != 0) {
  4434. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4435. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4436. } else {
  4437. sd->status.skill[idx].id = skill_id;
  4438. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4439. }
  4440. sd->status.skill[idx].lv = max(sd->status.skill[idx].lv, level);
  4441. break;
  4442. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4443. if (sd->status.skill[idx].id != 0) {
  4444. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4445. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4446. } else {
  4447. sd->status.skill[idx].id = skill_id;
  4448. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4449. }
  4450. sd->status.skill[idx].lv += level;
  4451. break;
  4452. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4453. sd->status.skill[idx].id = skill_id;
  4454. sd->status.skill[idx].lv = level;
  4455. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4456. if (level == 0) { //Remove skill.
  4457. sd->status.skill[idx].id = 0;
  4458. clif_deleteskill(sd,skill_id);
  4459. } else
  4460. clif_addskill(sd,skill_id);
  4461. 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.
  4462. status_calc_pc(sd, SCO_NONE);
  4463. break;
  4464. default:
  4465. return false;
  4466. }
  4467. return true;
  4468. }
  4469. /**
  4470. * Set's a player's plagiarized skill.
  4471. * @param sd: Player
  4472. * @param skill_id: Skill to be plagiarized
  4473. * @param skill_lv: Skill level to be plagiarized
  4474. * @return True on success or false otherwise
  4475. */
  4476. bool pc_skill_plagiarism(map_session_data &sd, uint16 skill_id, uint16 skill_lv)
  4477. {
  4478. skill_id = skill_dummy2skill_id(skill_id);
  4479. uint16 idx = skill_get_index(skill_id);
  4480. // Use skill index, avoiding out-of-bound array [Cydh]
  4481. if (idx == 0) {
  4482. ShowWarning("pc_skill_plagiarism: invalid skill idx 0 for skill %d.\n", skill_id);
  4483. return false;
  4484. }
  4485. skill_lv = cap_value(skill_lv, 1, skill_get_max(skill_id));
  4486. int type = skill_isCopyable(&sd, skill_id);
  4487. if (type == 1) {
  4488. pc_skill_plagiarism_reset(sd, type);
  4489. sd.cloneskill_idx = idx;
  4490. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), skill_id);
  4491. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), skill_lv);
  4492. } else if (type == 2) {
  4493. pc_skill_plagiarism_reset(sd, type);
  4494. sd.reproduceskill_idx = idx;
  4495. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), skill_id);
  4496. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), skill_lv);
  4497. } else {
  4498. ShowWarning("pc_skill_plagiarism: skill %d is not copyable.\n", skill_id);
  4499. return false;
  4500. }
  4501. sd.status.skill[idx].id = skill_id;
  4502. sd.status.skill[idx].lv = skill_lv;
  4503. sd.status.skill[idx].flag = SKILL_FLAG_PLAGIARIZED;
  4504. clif_addskill(&sd, skill_id);
  4505. return true;
  4506. }
  4507. /**
  4508. * Clear plagiarized skills from a player.
  4509. * @param sd: Player
  4510. * @param type: 1 for Plagiarism or 2 for Reproduce
  4511. * @return True on success or false otherwise
  4512. */
  4513. bool pc_skill_plagiarism_reset(map_session_data &sd, uint8 type)
  4514. {
  4515. uint16 idx;
  4516. if (type == 1)
  4517. idx = sd.cloneskill_idx;
  4518. else if (type == 2)
  4519. idx = sd.reproduceskill_idx;
  4520. else {
  4521. ShowError("pc_skill_plagiarism_reset: Unknown type %d.\n", type);
  4522. return false;
  4523. }
  4524. if (sd.status.skill[idx].flag == SKILL_FLAG_PLAGIARIZED) {
  4525. uint16 skill_id = sd.status.skill[idx].id;
  4526. sd.status.skill[idx].id = 0;
  4527. sd.status.skill[idx].lv = 0;
  4528. sd.status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4529. clif_deleteskill(&sd, skill_id);
  4530. if (type == 1) {
  4531. sd.cloneskill_idx = 0;
  4532. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  4533. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  4534. }
  4535. else if (type == 2) {
  4536. sd.reproduceskill_idx = 0;
  4537. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), 0);
  4538. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  4539. }
  4540. }
  4541. return true;
  4542. }
  4543. /*==========================================
  4544. * Append a card to an item ?
  4545. *------------------------------------------*/
  4546. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  4547. {
  4548. nullpo_ret(sd);
  4549. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4550. return 0;
  4551. }
  4552. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4553. return 0;
  4554. }
  4555. int i;
  4556. t_itemid nameid;
  4557. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4558. struct item_data* item_card = sd->inventory_data[idx_card];
  4559. if(item_eq == nullptr)
  4560. return 0; //Invalid item index.
  4561. if(item_card == nullptr)
  4562. return 0; //Invalid card index.
  4563. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4564. return 0; // target item missing
  4565. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4566. return 0; // target card missing
  4567. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4568. return 0; // only weapons and armor are allowed
  4569. if( item_card->type != IT_CARD )
  4570. return 0; // must be a card
  4571. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4572. return 0; // target must be identified
  4573. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4574. return 0; // card slots reserved for other purposes
  4575. if( (item_eq->equip & item_card->equip) == 0 )
  4576. return 0; // card cannot be compounded on this item type
  4577. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4578. return 0; // attempted to place shield card on left-hand weapon.
  4579. 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)) )
  4580. return 0; // specific accessory-card can only be inserted to specific accessory.
  4581. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4582. return 0; // item must be unequipped
  4583. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4584. if( i == item_eq->slots )
  4585. return 0; // no free slots
  4586. // remember the card id to insert
  4587. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4588. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4589. {// failed
  4590. clif_insert_card(sd,idx_equip,idx_card,1);
  4591. }
  4592. else
  4593. {// success
  4594. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4595. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4596. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4597. clif_insert_card(sd,idx_equip,idx_card,0);
  4598. }
  4599. return 0;
  4600. }
  4601. /**
  4602. * Returns the count of unidentified items with the option to identify too.
  4603. * @param sd: Player data
  4604. * @param identify_item: Whether or not to identify any unidentified items
  4605. * @return Unidentified items count
  4606. */
  4607. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  4608. {
  4609. int unidentified_count = 0;
  4610. for (int i = 0; i < MAX_INVENTORY; i++) {
  4611. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4612. if (identify_item == true) {
  4613. sd->inventory.u.items_inventory[i].identify = 1;
  4614. clif_item_identified(sd,i,0);
  4615. }
  4616. unidentified_count++;
  4617. }
  4618. }
  4619. return unidentified_count;
  4620. }
  4621. //
  4622. // Items
  4623. //
  4624. /*==========================================
  4625. * Update buying value by skills
  4626. *------------------------------------------*/
  4627. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4628. {
  4629. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4630. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4631. rate1 = 5+skill*2-((skill==10)? 1:0);
  4632. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4633. rate2 = 5+skill*4;
  4634. if(rate1 < rate2) rate1 = rate2;
  4635. if(rate1)
  4636. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4637. if(val < battle_config.min_shop_buy)
  4638. val = battle_config.min_shop_buy;
  4639. return val;
  4640. }
  4641. /*==========================================
  4642. * Update selling value by skills
  4643. *------------------------------------------*/
  4644. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4645. {
  4646. int skill,val = orig_value,rate = 0;
  4647. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4648. rate = 5+skill*2-((skill==10)? 1:0);
  4649. if(rate)
  4650. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4651. if (val < battle_config.min_shop_sell)
  4652. val = battle_config.min_shop_sell;
  4653. return val;
  4654. }
  4655. /*==========================================
  4656. * Checking if we have enough place on inventory for new item
  4657. * Make sure to take 30k as limit (for client I guess)
  4658. * @param sd
  4659. * @param nameid
  4660. * @param amount
  4661. * @return e_chkitem_result
  4662. *------------------------------------------*/
  4663. char pc_checkadditem(struct map_session_data *sd, t_itemid nameid, int amount)
  4664. {
  4665. int i;
  4666. struct item_data* data;
  4667. nullpo_ret(sd);
  4668. if(amount > MAX_AMOUNT)
  4669. return CHKADDITEM_OVERAMOUNT;
  4670. data = itemdb_search(nameid);
  4671. if(!itemdb_isstackable2(data))
  4672. return CHKADDITEM_NEW;
  4673. if( data->stack.inventory && amount > data->stack.amount )
  4674. return CHKADDITEM_OVERAMOUNT;
  4675. if (data->flag.guid)
  4676. return CHKADDITEM_NEW;
  4677. for(i=0;i<MAX_INVENTORY;i++){
  4678. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4679. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4680. 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 ) )
  4681. return CHKADDITEM_OVERAMOUNT;
  4682. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4683. if( i >= sd->status.inventory_slots ){
  4684. return CHKADDITEM_OVERAMOUNT;
  4685. }
  4686. return CHKADDITEM_EXIST;
  4687. }
  4688. }
  4689. return CHKADDITEM_NEW;
  4690. }
  4691. /*==========================================
  4692. * Return number of available place in inventory
  4693. * Each non stackable item will reduce place by 1
  4694. * @param sd
  4695. * @return Number of empty slots
  4696. *------------------------------------------*/
  4697. uint8 pc_inventoryblank(struct map_session_data *sd)
  4698. {
  4699. uint16 i;
  4700. uint8 b;
  4701. nullpo_ret(sd);
  4702. for(i = 0, b = 0; i < sd->status.inventory_slots; i++){
  4703. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4704. b++;
  4705. }
  4706. return b;
  4707. }
  4708. /**
  4709. * Attempts to remove zeny from player
  4710. * @param sd: Player
  4711. * @param zeny: Zeny removed
  4712. * @param type: Log type
  4713. * @param tsd: (optional) From who to log (if null take sd)
  4714. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4715. */
  4716. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4717. {
  4718. nullpo_retr(2,sd);
  4719. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4720. if( zeny < 0 )
  4721. {
  4722. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4723. return 1;
  4724. }
  4725. if( sd->status.zeny < zeny )
  4726. return 1; //Not enough.
  4727. sd->status.zeny -= zeny;
  4728. clif_updatestatus(sd,SP_ZENY);
  4729. if(!tsd) tsd = sd;
  4730. log_zeny(sd, type, tsd, -zeny);
  4731. if( zeny > 0 && sd->state.showzeny ) {
  4732. char output[255];
  4733. sprintf(output, "Removed %dz.", zeny);
  4734. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4735. }
  4736. return 0;
  4737. }
  4738. /**
  4739. * Attempts to give zeny to player
  4740. * @param sd: Player
  4741. * @param type: Log type
  4742. * @param tsd: (optional) From who to log (if null take sd)
  4743. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4744. */
  4745. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4746. {
  4747. nullpo_retr(-1,sd);
  4748. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4749. if( zeny < 0 )
  4750. {
  4751. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4752. return 1;
  4753. }
  4754. if( zeny > MAX_ZENY - sd->status.zeny )
  4755. zeny = MAX_ZENY - sd->status.zeny;
  4756. sd->status.zeny += zeny;
  4757. clif_updatestatus(sd,SP_ZENY);
  4758. if(!tsd) tsd = sd;
  4759. log_zeny(sd, type, tsd, zeny);
  4760. if( zeny > 0 && sd->state.showzeny ) {
  4761. char output[255];
  4762. sprintf(output, "Gained %dz.", zeny);
  4763. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4764. }
  4765. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4766. return 0;
  4767. }
  4768. /**
  4769. * Attempts to remove Cash Points from player
  4770. * @param sd: Player
  4771. * @param price: Total points (cash + kafra) the player has to pay
  4772. * @param points: Kafra points the player has to pay
  4773. * @param type: Log type
  4774. * @return -1: Not enough points, otherwise success (cash+points)
  4775. */
  4776. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4777. {
  4778. int cash;
  4779. nullpo_retr(-1,sd);
  4780. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4781. cash = price-points;
  4782. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4783. {
  4784. 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);
  4785. return -1;
  4786. }
  4787. if( cash ){
  4788. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4789. sd->cashPoints -= cash;
  4790. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4791. }
  4792. if( points ){
  4793. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4794. sd->kafraPoints -= points;
  4795. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4796. }
  4797. if( battle_config.cashshop_show_points )
  4798. {
  4799. char output[CHAT_SIZE_MAX];
  4800. 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.
  4801. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4802. }
  4803. return cash+points;
  4804. }
  4805. /**
  4806. * Attempts to give Cash Points to player
  4807. * @param sd: Player
  4808. * @param cash: Cash points the player gets
  4809. * @param points: Kafra points the player gets
  4810. * @param type: Log type
  4811. * @return -1: Error, otherwise success (cash or points)
  4812. */
  4813. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4814. {
  4815. char output[CHAT_SIZE_MAX];
  4816. nullpo_retr(-1,sd);
  4817. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4818. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4819. if( cash > 0 )
  4820. {
  4821. if( cash > MAX_ZENY-sd->cashPoints )
  4822. {
  4823. 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);
  4824. cash = MAX_ZENY-sd->cashPoints;
  4825. }
  4826. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4827. sd->cashPoints += cash;
  4828. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4829. if( battle_config.cashshop_show_points )
  4830. {
  4831. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4832. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4833. }
  4834. return cash;
  4835. }
  4836. if( points > 0 )
  4837. {
  4838. if( points > MAX_ZENY-sd->kafraPoints )
  4839. {
  4840. 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);
  4841. points = MAX_ZENY-sd->kafraPoints;
  4842. }
  4843. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4844. sd->kafraPoints += points;
  4845. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4846. if( battle_config.cashshop_show_points )
  4847. {
  4848. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4849. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4850. }
  4851. return points;
  4852. }
  4853. return -1; //shouldn't happen but just in case
  4854. }
  4855. /**
  4856. * Searching a specified itemid in inventory and return his stored index
  4857. * @param sd Player
  4858. * @param nameid Find this Item!
  4859. * @return Stored index in inventory, or -1 if not found.
  4860. **/
  4861. short pc_search_inventory(struct map_session_data *sd, t_itemid nameid) {
  4862. short i;
  4863. nullpo_retr(-1, sd);
  4864. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4865. return ( i < MAX_INVENTORY ) ? i : -1;
  4866. }
  4867. /** Attempt to add a new item to player inventory
  4868. * @param sd
  4869. * @param item_data
  4870. * @param amount
  4871. * @param log_type
  4872. * @return
  4873. * 0 = success
  4874. * 1 = invalid itemid not found or negative amount
  4875. * 2 = overweight
  4876. * 3 = ?
  4877. * 4 = no free place found
  4878. * 5 = max amount reached
  4879. * 6 = ?
  4880. * 7 = stack limitation
  4881. */
  4882. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4883. struct item_data *id;
  4884. int16 i;
  4885. unsigned int w;
  4886. nullpo_retr(ADDITEM_INVALID, sd);
  4887. nullpo_retr(ADDITEM_INVALID, item);
  4888. if( item->nameid == 0 || amount <= 0 )
  4889. return ADDITEM_INVALID;
  4890. if( amount > MAX_AMOUNT )
  4891. return ADDITEM_OVERAMOUNT;
  4892. id = itemdb_search(item->nameid);
  4893. if( id->stack.inventory && amount > id->stack.amount )
  4894. {// item stack limitation
  4895. return ADDITEM_STACKLIMIT;
  4896. }
  4897. w = id->weight*amount;
  4898. if(sd->weight + w > sd->max_weight)
  4899. return ADDITEM_OVERWEIGHT;
  4900. if (id->flag.guid && !item->unique_id)
  4901. item->unique_id = pc_generate_unique_id(sd);
  4902. // Stackable | Non Rental
  4903. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4904. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4905. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4906. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4907. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4908. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4909. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4910. 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 ) )
  4911. return ADDITEM_OVERAMOUNT;
  4912. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4913. if( i >= sd->status.inventory_slots ){
  4914. return ADDITEM_OVERAMOUNT;
  4915. }
  4916. sd->inventory.u.items_inventory[i].amount += amount;
  4917. clif_additem(sd,i,amount,0);
  4918. break;
  4919. }
  4920. }
  4921. }else{
  4922. i = MAX_INVENTORY;
  4923. }
  4924. if (i >= MAX_INVENTORY) {
  4925. i = pc_search_inventory(sd,0);
  4926. if( i < 0 )
  4927. return ADDITEM_OVERITEM;
  4928. if( i >= sd->status.inventory_slots ){
  4929. return ADDITEM_OVERITEM;
  4930. }
  4931. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4932. // clear equip and favorite fields first, just in case
  4933. if( item->equip )
  4934. sd->inventory.u.items_inventory[i].equip = 0;
  4935. if( item->favorite )
  4936. sd->inventory.u.items_inventory[i].favorite = 0;
  4937. if( item->equipSwitch )
  4938. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4939. sd->inventory.u.items_inventory[i].amount = amount;
  4940. sd->inventory_data[i] = id;
  4941. sd->last_addeditem_index = i;
  4942. if (!itemdb_isstackable2(id) || id->flag.guid)
  4943. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4944. clif_additem(sd,i,amount,0);
  4945. }
  4946. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4947. sd->weight += w;
  4948. clif_updatestatus(sd,SP_WEIGHT);
  4949. //Auto-equip
  4950. if(id->flag.autoequip)
  4951. pc_equipitem(sd, i, id->equip);
  4952. /* rental item check */
  4953. if( item->expire_time ) {
  4954. if( time(NULL) > item->expire_time ) {
  4955. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  4956. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4957. } else {
  4958. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4959. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4960. pc_inventory_rental_add(sd, seconds);
  4961. }
  4962. }
  4963. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4964. pc_show_questinfo(sd);
  4965. return ADDITEM_SUCCESS;
  4966. }
  4967. /*==========================================
  4968. * Remove an item at index n from inventory by amount.
  4969. * @param sd
  4970. * @param n Item index in inventory
  4971. * @param amount
  4972. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4973. * @param reason Delete reason
  4974. * @param log_type e_log_pick_type
  4975. * @return 1 - invalid itemid or negative amount; 0 - Success
  4976. *------------------------------------------*/
  4977. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4978. {
  4979. nullpo_retr(1, sd);
  4980. 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)
  4981. return 1;
  4982. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4983. sd->inventory.u.items_inventory[n].amount -= amount;
  4984. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4985. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4986. if(sd->inventory.u.items_inventory[n].equip)
  4987. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4988. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4989. sd->inventory_data[n] = NULL;
  4990. }
  4991. if(!(type&1))
  4992. clif_delitem(sd,n,amount,reason);
  4993. if(!(type&2))
  4994. clif_updatestatus(sd,SP_WEIGHT);
  4995. pc_show_questinfo(sd);
  4996. return 0;
  4997. }
  4998. /*==========================================
  4999. * Attempt to drop an item.
  5000. * @param sd
  5001. * @param n Item index in inventory
  5002. * @param amount Amount of item
  5003. * @return False = fail; True = success
  5004. *------------------------------------------*/
  5005. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  5006. {
  5007. nullpo_retr(1, sd);
  5008. if(n < 0 || n >= MAX_INVENTORY)
  5009. return false;
  5010. if(amount <= 0)
  5011. return false;
  5012. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  5013. sd->inventory.u.items_inventory[n].amount <= 0 ||
  5014. sd->inventory.u.items_inventory[n].amount < amount ||
  5015. sd->state.trading || sd->state.vending ||
  5016. !sd->inventory_data[n] //pc_delitem would fail on this case.
  5017. )
  5018. return false;
  5019. if( sd->inventory.u.items_inventory[n].equipSwitch )
  5020. return false;
  5021. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  5022. {
  5023. clif_displaymessage (sd->fd, msg_txt(sd,271));
  5024. return false; //Can't drop items in nodrop mapflag maps.
  5025. }
  5026. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  5027. {
  5028. clif_displaymessage (sd->fd, msg_txt(sd,263));
  5029. return false;
  5030. }
  5031. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  5032. return false;
  5033. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  5034. clif_dropitem(sd, n, amount);
  5035. return true;
  5036. }
  5037. /*==========================================
  5038. * Attempt to pick up an item.
  5039. * @param sd
  5040. * @param fitem Item that will be picked
  5041. * @return False = fail; True = success
  5042. *------------------------------------------*/
  5043. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  5044. {
  5045. int flag = 0;
  5046. t_tick tick = gettick();
  5047. struct party_data *p = NULL;
  5048. nullpo_ret(sd);
  5049. nullpo_ret(fitem);
  5050. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  5051. return false; // Distance is too far
  5052. if (sd->sc.cant.pickup)
  5053. return false;
  5054. if (sd->status.party_id)
  5055. p = party_search(sd->status.party_id);
  5056. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  5057. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  5058. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  5059. if (!(p && p->party.item&1 &&
  5060. first_sd && first_sd->status.party_id == sd->status.party_id
  5061. ))
  5062. return false;
  5063. }
  5064. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  5065. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  5066. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  5067. if (!(p && p->party.item&1 &&
  5068. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5069. (second_sd && second_sd->status.party_id == sd->status.party_id))
  5070. ))
  5071. return false;
  5072. }
  5073. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  5074. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  5075. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  5076. if(!(p && p->party.item&1 &&
  5077. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5078. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  5079. (third_sd && third_sd->status.party_id == sd->status.party_id))
  5080. ))
  5081. return false;
  5082. }
  5083. }
  5084. }
  5085. }
  5086. //This function takes care of giving the item to whoever should have it, considering party-share options.
  5087. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  5088. clif_additem(sd,0,0,flag);
  5089. return true;
  5090. }
  5091. //Display pickup animation.
  5092. pc_stop_attack(sd);
  5093. clif_takeitem(&sd->bl,&fitem->bl);
  5094. if (fitem->mob_id &&
  5095. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  5096. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  5097. )
  5098. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  5099. map_clearflooritem(&fitem->bl);
  5100. return true;
  5101. }
  5102. /*==========================================
  5103. * Check if item is usable.
  5104. * Return:
  5105. * false = no
  5106. * true = yes
  5107. *------------------------------------------*/
  5108. bool pc_isUseitem(struct map_session_data *sd,int n)
  5109. {
  5110. struct item_data *item;
  5111. t_itemid nameid;
  5112. nullpo_ret(sd);
  5113. item = sd->inventory_data[n];
  5114. nameid = sd->inventory.u.items_inventory[n].nameid;
  5115. if( item == NULL )
  5116. return false;
  5117. //Not consumable item
  5118. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  5119. return false;
  5120. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  5121. return true;
  5122. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5123. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  5124. return false;
  5125. //Prevent mass item usage. [Skotlex]
  5126. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  5127. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  5128. )
  5129. return false;
  5130. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  5131. clif_msg(sd,ITEM_NOUSE_SITTING);
  5132. return false; // You cannot use this item while sitting.
  5133. }
  5134. if (sd->state.storage_flag && item->type != IT_CASH) {
  5135. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  5136. return false; // You cannot use this item while storage is open.
  5137. }
  5138. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  5139. return false;
  5140. switch( nameid ) {
  5141. case ITEMID_WING_OF_FLY:
  5142. case ITEMID_GIANT_FLY_WING:
  5143. case ITEMID_N_FLY_WING:
  5144. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  5145. clif_skill_teleportmessage(sd,0);
  5146. return false;
  5147. }
  5148. if (nameid == ITEMID_GIANT_FLY_WING) {
  5149. struct party_data *pd = party_search(sd->status.party_id);
  5150. if (pd) {
  5151. int i;
  5152. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  5153. if (i == MAX_PARTY) { // User is not party leader
  5154. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  5155. break;
  5156. }
  5157. 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));
  5158. if (i == MAX_PARTY) { // No party members found on same map
  5159. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  5160. break;
  5161. }
  5162. } else {
  5163. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  5164. break;
  5165. }
  5166. }
  5167. // Fall through
  5168. case ITEMID_WING_OF_BUTTERFLY:
  5169. case ITEMID_N_BUTTERFLY_WING:
  5170. case ITEMID_DUN_TELE_SCROLL1:
  5171. case ITEMID_DUN_TELE_SCROLL2:
  5172. case ITEMID_DUN_TELE_SCROLL3:
  5173. case ITEMID_WOB_RUNE:
  5174. case ITEMID_WOB_SCHWALTZ:
  5175. case ITEMID_WOB_RACHEL:
  5176. case ITEMID_WOB_LOCAL:
  5177. case ITEMID_SIEGE_TELEPORT_SCROLL:
  5178. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  5179. clif_displaymessage(sd->fd, msg_txt(sd,663));
  5180. return false;
  5181. }
  5182. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  5183. return false;
  5184. break;
  5185. case ITEMID_MERCENARY_RED_POTION:
  5186. case ITEMID_MERCENARY_BLUE_POTION:
  5187. case ITEMID_M_CENTER_POTION:
  5188. case ITEMID_M_AWAKENING_POTION:
  5189. case ITEMID_M_BERSERK_POTION:
  5190. if( sd->md == NULL || sd->md->db == NULL )
  5191. return false;
  5192. if( sd->md->sc.cant.consume )
  5193. return false;
  5194. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  5195. return false;
  5196. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  5197. return false;
  5198. break;
  5199. case ITEMID_NEURALIZER:
  5200. if( !mapdata->flag[MF_RESET] )
  5201. return false;
  5202. break;
  5203. }
  5204. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  5205. return false; // Mercenary Scrolls
  5206. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  5207. if( pc_is90overweight(sd) ) {
  5208. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  5209. return false;
  5210. }
  5211. if( !pc_inventoryblank(sd) ) {
  5212. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  5213. return false;
  5214. }
  5215. }
  5216. //Gender check
  5217. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  5218. return false;
  5219. //Required level check
  5220. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  5221. return false;
  5222. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  5223. return false;
  5224. //Not equipable by class. [Skotlex]
  5225. if (!pc_job_can_use_item(sd,item))
  5226. return false;
  5227. if (sd->sc.cant.consume)
  5228. return false;
  5229. if (!pc_isItemClass(sd,item))
  5230. return false;
  5231. //Dead Branch items
  5232. if( item->flag.dead_branch )
  5233. log_branch(sd);
  5234. return true;
  5235. }
  5236. /*==========================================
  5237. * Last checks to use an item.
  5238. * Return:
  5239. * 0 = fail
  5240. * 1 = success
  5241. *------------------------------------------*/
  5242. int pc_useitem(struct map_session_data *sd,int n)
  5243. {
  5244. t_tick tick = gettick();
  5245. int amount;
  5246. t_itemid nameid;
  5247. struct script_code *script;
  5248. struct item item;
  5249. struct item_data *id;
  5250. nullpo_ret(sd);
  5251. if (sd->state.mail_writing)
  5252. return 0;
  5253. if (sd->npc_id) {
  5254. if (sd->progressbar.npc_id) {
  5255. clif_progressbar_abort(sd);
  5256. return 0; // First item use attempt cancels the progress bar
  5257. }
  5258. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  5259. #ifdef RENEWAL
  5260. clif_msg( sd, WORK_IN_PROGRESS );
  5261. #endif
  5262. return 0;
  5263. }
  5264. }
  5265. item = sd->inventory.u.items_inventory[n];
  5266. id = sd->inventory_data[n];
  5267. if (item.nameid == 0 || item.amount <= 0)
  5268. return 0;
  5269. if( !pc_isUseitem(sd,n) )
  5270. return 0;
  5271. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  5272. nameid = id->nameid;
  5273. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  5274. return 0;
  5275. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  5276. if( id->flag.delay_consume > 0 ) {
  5277. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  5278. return 0;
  5279. else if( pc_issit(sd) )
  5280. return 0;
  5281. }
  5282. //Since most delay-consume items involve using a "skill-type" target cursor,
  5283. //perform a skill-use check before going through. [Skotlex]
  5284. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  5285. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  5286. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  5287. return 0;
  5288. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  5289. return 0;
  5290. /* on restricted maps the item is consumed but the effect is not used */
  5291. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  5292. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  5293. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  5294. clif_useitemack(sd,n,item.amount-1,true);
  5295. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  5296. }
  5297. return 0;/* regardless, effect is not run */
  5298. }
  5299. if (pet_db_search(id->nameid, PET_CATCH) != nullptr && map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  5300. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  5301. return 0;
  5302. }
  5303. sd->itemid = item.nameid;
  5304. sd->itemindex = n;
  5305. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  5306. sd->catch_target_class = PET_CATCH_FAIL;
  5307. amount = item.amount;
  5308. script = id->script;
  5309. //Check if the item is to be consumed immediately [Skotlex]
  5310. if (id->flag.delay_consume > 0)
  5311. clif_useitemack(sd, n, amount, true);
  5312. else
  5313. {
  5314. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  5315. {
  5316. clif_useitemack(sd, n, amount - 1, true);
  5317. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  5318. }
  5319. else
  5320. clif_useitemack(sd, n, 0, false);
  5321. }
  5322. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  5323. potion_flag = 2; // Famous player's potions have 50% more efficiency
  5324. //Update item use time.
  5325. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  5326. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  5327. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  5328. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  5329. potion_flag = 0;
  5330. return 1;
  5331. }
  5332. /**
  5333. * Add item on cart for given index.
  5334. * @param sd
  5335. * @param item
  5336. * @param amount
  5337. * @param log_type
  5338. * @return See pc.hpp::e_additem_result
  5339. */
  5340. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  5341. {
  5342. struct item_data *data;
  5343. int i,w;
  5344. nullpo_retr(ADDITEM_INVALID, sd);
  5345. nullpo_retr(ADDITEM_INVALID, item);
  5346. if(item->nameid == 0 || amount <= 0)
  5347. return ADDITEM_INVALID;
  5348. if (itemdb_ishatched_egg(item))
  5349. return ADDITEM_INVALID;
  5350. data = itemdb_search(item->nameid);
  5351. if( data->stack.cart && amount > data->stack.amount )
  5352. {// item stack limitation
  5353. return ADDITEM_STACKLIMIT;
  5354. }
  5355. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  5356. { // Check item trade restrictions [Skotlex]
  5357. clif_displaymessage (sd->fd, msg_txt(sd,264));
  5358. return ADDITEM_INVALID;
  5359. }
  5360. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  5361. return ADDITEM_OVERWEIGHT;
  5362. i = MAX_CART;
  5363. if( itemdb_isstackable2(data) && !item->expire_time )
  5364. {
  5365. for (i = 0; i < MAX_CART; i++) {
  5366. if (sd->cart.u.items_cart[i].nameid == item->nameid
  5367. && sd->cart.u.items_cart[i].bound == item->bound
  5368. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5369. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5370. )
  5371. break;
  5372. }
  5373. }
  5374. if( i < MAX_CART )
  5375. {// item already in cart, stack it
  5376. 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 ) )
  5377. return ADDITEM_OVERAMOUNT; // no slot
  5378. sd->cart.u.items_cart[i].amount += amount;
  5379. clif_cart_additem(sd,i,amount);
  5380. }
  5381. else
  5382. {// item not stackable or not present, add it
  5383. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5384. if( i == MAX_CART )
  5385. return ADDITEM_OVERAMOUNT; // no slot
  5386. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5387. sd->cart.u.items_cart[i].id = 0;
  5388. sd->cart.u.items_cart[i].amount = amount;
  5389. sd->cart_num++;
  5390. clif_cart_additem(sd,i,amount);
  5391. }
  5392. sd->cart.u.items_cart[i].favorite = 0; // clear
  5393. sd->cart.u.items_cart[i].equipSwitch = 0;
  5394. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5395. sd->cart_weight += w;
  5396. clif_updatestatus(sd,SP_CARTINFO);
  5397. return ADDITEM_SUCCESS;
  5398. }
  5399. /*==========================================
  5400. * Delete item on cart for given index.
  5401. *------------------------------------------*/
  5402. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  5403. {
  5404. nullpo_retv(sd);
  5405. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5406. sd->cart.u.items_cart[n].amount < amount)
  5407. return;
  5408. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5409. sd->cart.u.items_cart[n].amount -= amount;
  5410. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5411. if(sd->cart.u.items_cart[n].amount <= 0) {
  5412. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5413. sd->cart_num--;
  5414. }
  5415. if(!type) {
  5416. clif_cart_delitem(sd,n,amount);
  5417. clif_updatestatus(sd,SP_CARTINFO);
  5418. }
  5419. }
  5420. /*==========================================
  5421. * Transfer item from inventory to cart.
  5422. *------------------------------------------*/
  5423. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  5424. {
  5425. nullpo_retv(sd);
  5426. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5427. return;
  5428. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5429. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5430. return;
  5431. if( item_data->equipSwitch ){
  5432. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  5433. return;
  5434. }
  5435. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5436. if (flag == ADDITEM_SUCCESS)
  5437. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5438. else {
  5439. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  5440. clif_additem(sd, idx, amount, 0);
  5441. clif_delitem(sd, idx, amount, 0);
  5442. }
  5443. }
  5444. /*==========================================
  5445. * Get number of item in cart.
  5446. * Return:
  5447. -1 = itemid not found or no amount found
  5448. x = remaining itemid on cart after get
  5449. *------------------------------------------*/
  5450. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  5451. {
  5452. struct item* item_data;
  5453. nullpo_retr(-1, sd);
  5454. item_data = &sd->cart.u.items_cart[idx];
  5455. if( item_data->nameid == 0 || item_data->amount == 0 )
  5456. return -1;
  5457. return item_data->amount - amount;
  5458. }
  5459. /*==========================================
  5460. * Retrieve an item at index idx from cart.
  5461. *------------------------------------------*/
  5462. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  5463. {
  5464. nullpo_retv(sd);
  5465. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5466. return;
  5467. struct item *item_data=&sd->cart.u.items_cart[idx];
  5468. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5469. return;
  5470. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5471. if (flag == ADDITEM_SUCCESS)
  5472. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5473. else {
  5474. clif_cart_delitem(sd, idx, amount);
  5475. clif_additem(sd, idx, amount, flag);
  5476. clif_cart_additem(sd, idx, amount);
  5477. }
  5478. }
  5479. /*==========================================
  5480. * Bound Item Check
  5481. * Type:
  5482. * 1 Account Bound
  5483. * 2 Guild Bound
  5484. * 3 Party Bound
  5485. * 4 Character Bound
  5486. *------------------------------------------*/
  5487. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  5488. {
  5489. int i = 0, j = 0;
  5490. for(i = 0; i < MAX_INVENTORY; i++) {
  5491. 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) {
  5492. idxlist[j] = i;
  5493. j++;
  5494. }
  5495. }
  5496. return j;
  5497. }
  5498. /*==========================================
  5499. * Display item stolen msg to player sd
  5500. *------------------------------------------*/
  5501. int pc_show_steal(struct block_list *bl,va_list ap)
  5502. {
  5503. struct map_session_data *sd;
  5504. t_itemid itemid;
  5505. char output[100];
  5506. sd=va_arg(ap,struct map_session_data *);
  5507. itemid=va_arg(ap,int);
  5508. std::shared_ptr<item_data> id = item_db.find(itemid);
  5509. if(id == nullptr)
  5510. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5511. else
  5512. sprintf(output,"%s stole %s.",sd->status.name,id->ename.c_str());
  5513. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  5514. return 0;
  5515. }
  5516. /**
  5517. * Steal an item from bl (mob).
  5518. * @param sd: Player data
  5519. * @param bl: Object to steal from
  5520. * @param skill_lv: Level of skill used
  5521. * @return True on success or false otherwise
  5522. */
  5523. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5524. {
  5525. int i;
  5526. t_itemid itemid;
  5527. double rate;
  5528. unsigned char flag = 0;
  5529. struct status_data *sd_status, *md_status;
  5530. struct mob_data *md;
  5531. if(!sd || !bl || bl->type!=BL_MOB)
  5532. return false;
  5533. md = (TBL_MOB *)bl;
  5534. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5535. return false;
  5536. sd_status= status_get_status_data(&sd->bl);
  5537. md_status= status_get_status_data(bl);
  5538. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5539. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5540. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5541. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5542. ) { //Can't steal from
  5543. md->state.steal_flag = UCHAR_MAX;
  5544. return false;
  5545. }
  5546. // base skill success chance (percentual)
  5547. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5548. rate += sd->bonus.add_steal_rate;
  5549. if( rate < 1
  5550. #ifdef RENEWAL
  5551. || rnd()%100 >= rate
  5552. #endif
  5553. )
  5554. return false;
  5555. // Try dropping one item, in the order from first to last possible slot.
  5556. // Droprate is affected by the skill success rate.
  5557. for( i = 0; i < MAX_MOB_DROP; i++ )
  5558. if( item_db.exists(md->db->dropitem[i].nameid) && !md->db->dropitem[i].steal_protected && rnd() % 10000 < md->db->dropitem[i].rate
  5559. #ifndef RENEWAL
  5560. * rate/100.
  5561. #endif
  5562. )
  5563. break;
  5564. if( i == MAX_MOB_DROP )
  5565. return false;
  5566. itemid = md->db->dropitem[i].nameid;
  5567. struct item tmp_item = {};
  5568. tmp_item.nameid = itemid;
  5569. tmp_item.amount = 1;
  5570. tmp_item.identify = itemdb_isidentified(itemid);
  5571. if( battle_config.skill_steal_random_options ){
  5572. mob_setdropitem_option( &tmp_item, &md->db->dropitem[i] );
  5573. }
  5574. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5575. //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?
  5576. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5577. if(flag) { //Failed to steal due to overweight
  5578. clif_additem(sd,0,0,flag);
  5579. return false;
  5580. }
  5581. if(battle_config.show_steal_in_same_party)
  5582. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5583. //Logs items, Stolen from mobs [Lupus]
  5584. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5585. //A Rare Steal Global Announce by Lupus
  5586. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5587. struct item_data *i_data;
  5588. char message[128];
  5589. i_data = itemdb_search(itemid);
  5590. 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);
  5591. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5592. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5593. }
  5594. return true;
  5595. }
  5596. /*==========================================
  5597. * Stole zeny from bl (mob)
  5598. * return
  5599. * 0 = fail
  5600. * 1 = success
  5601. *------------------------------------------*/
  5602. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  5603. {
  5604. int rate, target_lv;
  5605. struct mob_data *md;
  5606. if(!sd || !target || target->type != BL_MOB)
  5607. return 0;
  5608. md = (TBL_MOB*)target;
  5609. target_lv = status_get_lv(target);
  5610. if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || md->sc.data[SC_HANDICAPSTATE_FROSTBITE] ||
  5611. md->sc.data[SC_HANDICAPSTATE_SWOONING] || md->sc.data[SC_HANDICAPSTATE_LIGHTNINGSTRIKE] || md->sc.data[SC_HANDICAPSTATE_CRYSTALLIZATION] ||
  5612. status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5613. return 0;
  5614. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5615. if(rnd()%1000 < rate)
  5616. {
  5617. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5618. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5619. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5620. md->state.steal_coin_flag = 1;
  5621. return 1;
  5622. }
  5623. return 0;
  5624. }
  5625. /*==========================================
  5626. * Set's a player position.
  5627. * @param sd
  5628. * @param mapindex
  5629. * @param x
  5630. * @param y
  5631. * @param clrtype
  5632. * @return SETPOS_OK Success
  5633. * SETPOS_MAPINDEX Invalid map index
  5634. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5635. * SETPOS_AUTOTRADE Player is in autotrade state
  5636. *------------------------------------------*/
  5637. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5638. {
  5639. nullpo_retr(SETPOS_OK,sd);
  5640. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5641. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5642. return SETPOS_MAPINDEX;
  5643. }
  5644. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5645. return SETPOS_AUTOTRADE;
  5646. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5647. pc_setstand(sd, true);
  5648. pc_setrestartvalue(sd,1);
  5649. }
  5650. int16 m = map_mapindex2mapid(mapindex);
  5651. struct map_data *mapdata = map_getmapdata(m);
  5652. status_change *sc = status_get_sc(&sd->bl);
  5653. sd->state.changemap = (sd->mapindex != mapindex);
  5654. sd->state.warping = 1;
  5655. sd->state.workinprogress = WIP_DISABLE_NONE;
  5656. sd->state.mail_writing = false;
  5657. sd->state.refineui_open = false;
  5658. if( sd->state.changemap ) { // Misc map-changing settings
  5659. int curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5660. if (curr_map_instance_id != new_map_instance_id) {
  5661. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5662. instance_delusers(curr_map_instance_id);
  5663. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5664. }
  5665. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5666. instance_addusers(new_map_instance_id);
  5667. }
  5668. if (sd->bg_id && mapdata && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5669. bg_team_leave(sd, false, true);
  5670. sd->state.pmap = sd->bl.m;
  5671. if (sc && sc->count) { // Cancel some map related stuff.
  5672. if (sc->cant.warp)
  5673. return SETPOS_MAPINDEX; // You may not get out!
  5674. for (const auto &it : status_db) {
  5675. if (sc->data[it.first]) {
  5676. if (it.second->flag[SCF_REMOVEONMAPWARP])
  5677. status_change_end(&sd->bl, static_cast<sc_type>(it.first));
  5678. if (it.second->flag[SCF_RESTARTONMAPWARP] && it.second->skill_id > 0) {
  5679. status_change_entry *sce = sd->sc.data[it.first];
  5680. if (sce->timer != INVALID_TIMER)
  5681. delete_timer(sce->timer, status_change_timer);
  5682. sce->timer = add_timer(gettick() + skill_get_time(it.second->skill_id, sce->val1), status_change_timer, sd->bl.id, it.first);
  5683. }
  5684. }
  5685. }
  5686. }
  5687. for(int i = 0; i < EQI_MAX; i++ ) {
  5688. if( sd->equip_index[i] >= 0 )
  5689. if( pc_isequip(sd,sd->equip_index[i]) )
  5690. pc_unequipitem(sd,sd->equip_index[i],2);
  5691. }
  5692. if (battle_config.clear_unit_onwarp&BL_PC)
  5693. skill_clear_unitgroup(&sd->bl);
  5694. if( battle_config.loose_ap_on_map && mapdata_flag_vs( mapdata ) ){
  5695. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  5696. }
  5697. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5698. guild_send_dot_remove(sd);
  5699. bg_send_dot_remove(sd);
  5700. if (sd->regen.state.gc)
  5701. sd->regen.state.gc = 0;
  5702. if (mapdata) {
  5703. // make sure vending is allowed here
  5704. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5705. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5706. vending_closevending(sd);
  5707. }
  5708. // make sure buyingstore is allowed here
  5709. if (sd->state.buyingstore && mapdata->flag[MF_NOBUYINGSTORE]) {
  5710. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5711. buyingstore_close(sd);
  5712. }
  5713. }
  5714. channel_pcquit(sd,4); //quit map chan
  5715. // Remove Cloaked NPC if changing to another map
  5716. for (auto it = sd->cloaked_npc.begin(); it != sd->cloaked_npc.end(); ++it) {
  5717. block_list *npc_bl = map_id2bl(*it);
  5718. if (npc_bl && npc_bl->m != m) {
  5719. sd->cloaked_npc.erase(it);
  5720. break;
  5721. }
  5722. }
  5723. }
  5724. if( m < 0 )
  5725. {
  5726. uint32 ip;
  5727. uint16 port;
  5728. struct script_state *st;
  5729. //if can't find any map-servers, just abort setting position.
  5730. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5731. return SETPOS_NO_MAPSERVER;
  5732. if (sd->npc_id){
  5733. npc_event_dequeue(sd,false);
  5734. st = sd->st;
  5735. }else{
  5736. st = nullptr;
  5737. }
  5738. if (sd->bg_id) // Switching map servers, remove from bg
  5739. bg_team_leave(sd, false, true);
  5740. if (sd->state.vending) // Stop vending
  5741. vending_closevending(sd);
  5742. if (sd->state.buyingstore) // Stop buyingstore
  5743. buyingstore_close(sd);
  5744. npc_script_event(sd, NPCE_LOGOUT);
  5745. //remove from map, THEN change x/y coordinates
  5746. unit_remove_map_pc(sd,clrtype);
  5747. sd->mapindex = mapindex;
  5748. sd->bl.x=x;
  5749. sd->bl.y=y;
  5750. pc_clean_skilltree(sd);
  5751. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5752. chrif_changemapserver(sd, ip, (short)port);
  5753. //Free session data from this map server [Kevin]
  5754. unit_free_pc(sd);
  5755. if( st ){
  5756. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5757. st->state = END;
  5758. }
  5759. return SETPOS_OK;
  5760. }
  5761. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5762. {
  5763. 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);
  5764. x = y = 0; // make it random
  5765. }
  5766. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5767. int c=0;
  5768. do {
  5769. x = rnd()%(mapdata->xs-2)+1;
  5770. y = rnd()%(mapdata->ys-2)+1;
  5771. c++;
  5772. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5773. 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));
  5774. return SETPOS_OK; //preventing warp
  5775. //break; //allow warp anyway
  5776. }
  5777. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5778. }
  5779. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5780. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5781. vending_closevending(sd);
  5782. }
  5783. if (sd->state.buyingstore && map_getcell(m, x, y, CELL_CHKNOBUYINGSTORE)) {
  5784. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  5785. buyingstore_close(sd);
  5786. }
  5787. if(sd->bl.prev != NULL){
  5788. unit_remove_map_pc(sd,clrtype);
  5789. clif_changemap(sd,m,x,y); // [MouseJstr]
  5790. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5791. sd->state.rewarp = 1;
  5792. sd->mapindex = mapindex;
  5793. sd->bl.m = m;
  5794. sd->bl.x = sd->ud.to_x = x;
  5795. sd->bl.y = sd->ud.to_y = y;
  5796. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5797. { // Increased guild castle regen [Valaris]
  5798. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  5799. if(gc && gc->guild_id == sd->status.guild_id)
  5800. sd->regen.state.gc = 1;
  5801. }
  5802. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5803. {
  5804. sd->pd->bl.m = m;
  5805. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5806. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5807. sd->pd->ud.dir = sd->ud.dir;
  5808. }
  5809. if( hom_is_active(sd->hd) )
  5810. {
  5811. sd->hd->bl.m = m;
  5812. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5813. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5814. sd->hd->ud.dir = sd->ud.dir;
  5815. }
  5816. if( sd->md )
  5817. {
  5818. sd->md->bl.m = m;
  5819. sd->md->bl.x = sd->md->ud.to_x = x;
  5820. sd->md->bl.y = sd->md->ud.to_y = y;
  5821. sd->md->ud.dir = sd->ud.dir;
  5822. }
  5823. if( sd->ed ) {
  5824. sd->ed->bl.m = m;
  5825. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5826. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5827. sd->ed->ud.dir = sd->ud.dir;
  5828. }
  5829. pc_cell_basilica(sd);
  5830. //check if we gonna be rewarped [lighta]
  5831. if(npc_check_areanpc(1,m,x,y,0)){
  5832. sd->count_rewarp++;
  5833. }
  5834. else
  5835. sd->count_rewarp = 0;
  5836. if (sd->state.vending)
  5837. vending_update(*sd);
  5838. if (sd->state.buyingstore)
  5839. buyingstore_update(*sd);
  5840. return SETPOS_OK;
  5841. }
  5842. /*==========================================
  5843. * Warp player sd to random location on current map.
  5844. * May fail if no walkable cell found (1000 attempts).
  5845. * Return:
  5846. * 0 = Success
  5847. * 1,2,3 = Fail
  5848. *------------------------------------------*/
  5849. char pc_randomwarp(struct map_session_data *sd, clr_type type, bool ignore_mapflag)
  5850. {
  5851. int x,y,i=0;
  5852. nullpo_ret(sd);
  5853. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5854. if (mapdata->flag[MF_NOTELEPORT] && !ignore_mapflag) //Teleport forbidden
  5855. return 3;
  5856. do {
  5857. x = rnd()%(mapdata->xs-2)+1;
  5858. y = rnd()%(mapdata->ys-2)+1;
  5859. } 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);
  5860. if (i < 1000)
  5861. return pc_setpos(sd,mapdata->index,x,y,type);
  5862. return 3;
  5863. }
  5864. /*==========================================
  5865. * Records a memo point at sd's current position
  5866. * pos - entry to replace, (-1: shift oldest entry out)
  5867. *------------------------------------------*/
  5868. bool pc_memo(struct map_session_data* sd, int pos)
  5869. {
  5870. int skill;
  5871. nullpo_ret(sd);
  5872. // check mapflags
  5873. 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) ) {
  5874. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5875. return false;
  5876. }
  5877. // check inputs
  5878. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5879. return false; // invalid input
  5880. // check required skill level
  5881. skill = pc_checkskill(sd, AL_WARP);
  5882. if( skill < 1 ) {
  5883. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5884. return false;
  5885. }
  5886. if( skill < 2 || skill - 2 < pos ) {
  5887. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5888. return false;
  5889. }
  5890. if( pos == -1 )
  5891. {
  5892. uint8 i;
  5893. // prevent memo-ing the same map multiple times
  5894. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5895. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5896. pos = 0;
  5897. }
  5898. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5899. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5900. return false;
  5901. }
  5902. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5903. sd->status.memo_point[pos].x = sd->bl.x;
  5904. sd->status.memo_point[pos].y = sd->bl.y;
  5905. clif_skill_memomessage(sd, 0);
  5906. return true;
  5907. }
  5908. //
  5909. // Skills
  5910. //
  5911. /**
  5912. * Get the skill current cooldown for player.
  5913. * (get the db base cooldown for skill + player specific cooldown)
  5914. * @param sd : player pointer
  5915. * @param id : skill id
  5916. * @param lv : skill lv
  5917. * @return player skill cooldown
  5918. */
  5919. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5920. if (skill_id == SJ_NOVAEXPLOSING) {
  5921. struct status_change *sc = status_get_sc(&sd->bl);
  5922. if (sc && sc->data[SC_DIMENSION])
  5923. return 0;
  5924. }
  5925. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  5926. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  5927. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5928. for (auto &it : sd->skillcooldown) {
  5929. if (it.id == skill_id) {
  5930. cooldown += it.val;
  5931. break;
  5932. }
  5933. }
  5934. return max(0, cooldown);
  5935. }
  5936. /*==========================================
  5937. * Return player sd skill_lv learned for given skill
  5938. *------------------------------------------*/
  5939. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5940. {
  5941. uint16 idx = 0;
  5942. if (sd == NULL)
  5943. return 0;
  5944. #ifdef RENEWAL
  5945. if ((idx = skill_get_index(skill_id)) == 0) {
  5946. #else
  5947. if( ( idx = skill_db.get_index( skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__ ) ) == 0 ){
  5948. if( skill_id >= RK_ENCHANTBLADE ){
  5949. // Silently fail for now -> future update planned
  5950. return 0;
  5951. }
  5952. #endif
  5953. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5954. return 0;
  5955. }
  5956. if (SKILL_CHK_GUILD(skill_id) ) {
  5957. struct guild *g;
  5958. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5959. return guild_checkskill(g,skill_id);
  5960. return 0;
  5961. }
  5962. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5963. }
  5964. /**
  5965. * Returns the flag of the given skill (when learned).
  5966. * @param sd: Player data
  5967. * @param skill_id: Skill to lookup
  5968. * @return Skill flag type
  5969. */
  5970. e_skill_flag pc_checkskill_flag(map_session_data &sd, uint16 skill_id) {
  5971. uint16 idx;
  5972. #ifdef RENEWAL
  5973. if ((idx = skill_get_index(skill_id)) == 0) {
  5974. #else
  5975. if ((idx = skill_db.get_index(skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__)) == 0) {
  5976. if (skill_id >= RK_ENCHANTBLADE) {
  5977. // Silently fail for now -> future update planned
  5978. return SKILL_FLAG_NONE;
  5979. }
  5980. #endif
  5981. ShowError("pc_checkskill_flag: Invalid skill id %d (char_id=%d).\n", skill_id, sd.status.char_id);
  5982. return SKILL_FLAG_NONE;
  5983. }
  5984. 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;
  5985. }
  5986. /**
  5987. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  5988. * @param sd: Player data
  5989. * @param type: Summoner Power Type
  5990. * @return Skill points invested
  5991. */
  5992. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  5993. if (sd == nullptr)
  5994. return 0;
  5995. uint8 count = 0;
  5996. switch (type) {
  5997. case SUMMONER_POWER_SEA:
  5998. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  5999. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  6000. break;
  6001. case SUMMONER_POWER_LAND:
  6002. 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) +
  6003. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  6004. break;
  6005. case SUMMONER_POWER_LIFE:
  6006. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  6007. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  6008. break;
  6009. }
  6010. return count;
  6011. }
  6012. /**
  6013. * Checks for Imperial Guard's passive skills.
  6014. * @param sd: Player data
  6015. * @param flag:
  6016. * Flag&1 = IG_SHIELD_MASTERY
  6017. * Flag&2 = IG_SPEAR_SWORD_M
  6018. */
  6019. uint8 pc_checkskill_imperial_guard(struct map_session_data *sd, short flag)
  6020. {
  6021. nullpo_retr(0, sd);
  6022. uint8 count = 0;
  6023. if (flag&1 && sd->status.shield > 0)
  6024. count += pc_checkskill(sd, IG_SHIELD_MASTERY);
  6025. if (flag&2 && (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_1HSPEAR || sd->status.weapon == W_2HSPEAR))
  6026. count += pc_checkskill(sd, IG_SPEAR_SWORD_M);
  6027. return count;
  6028. }
  6029. /**
  6030. * Check if we still have the correct weapon to continue the skill (actually status)
  6031. * If not ending it
  6032. * @param sd
  6033. * @return 0:error, 1:check done
  6034. */
  6035. static void pc_checkallowskill(struct map_session_data *sd)
  6036. {
  6037. nullpo_retv(sd);
  6038. if(!sd->sc.count)
  6039. return;
  6040. for (const auto &it : status_db) {
  6041. sc_type status = it.second->type;
  6042. std::bitset<SCF_MAX> flag = it.second->flag;
  6043. if (flag[SCF_REQUIREWEAPON]) { // Skills requiring specific weapon types
  6044. if (status == SC_DANCING && !battle_config.dancing_weaponswitch_fix)
  6045. continue;
  6046. if (sd->sc.data[status] && !pc_check_weapontype(sd, skill_get_weapontype(it.second->skill_id)))
  6047. status_change_end(&sd->bl, status);
  6048. }
  6049. if (flag[SCF_REQUIRESHIELD]) { // Skills requiring a shield
  6050. if (sd->sc.data[status] && sd->status.shield <= 0)
  6051. status_change_end(&sd->bl, status);
  6052. }
  6053. }
  6054. }
  6055. /*==========================================
  6056. * Return equipped index of item on player sd at pos
  6057. * Return
  6058. * -1 : Nothing equipped
  6059. * idx : (this index could be used in inventory to found item_data)
  6060. *------------------------------------------*/
  6061. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  6062. {
  6063. uint8 i;
  6064. nullpo_retr(-1, sd);
  6065. for(i=0;i<EQI_MAX;i++){
  6066. if(pos & equip_bitmask[i]){
  6067. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  6068. // Check all if any match is found
  6069. continue;
  6070. }
  6071. return sd->equip_index[i];
  6072. }
  6073. }
  6074. return -1;
  6075. }
  6076. /*==========================================
  6077. * Check if sd has nameid equipped somewhere
  6078. * @sd : the player session
  6079. * @nameid : id of the item to check
  6080. * @min : : see pc.hpp enum equip_index from ? to @max
  6081. * @max : see pc.hpp enum equip_index for @min to ?
  6082. * -return true,false
  6083. *------------------------------------------*/
  6084. bool pc_checkequip2(struct map_session_data *sd, t_itemid nameid, int min, int max)
  6085. {
  6086. int i;
  6087. for(i = min; i < max; i++) {
  6088. if(equip_bitmask[i]) {
  6089. int idx = sd->equip_index[i];
  6090. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  6091. return true;
  6092. }
  6093. }
  6094. return false;
  6095. }
  6096. /*==========================================
  6097. * Convert's from the client's lame Job ID system
  6098. * to the map server's 'makes sense' system. [Skotlex]
  6099. *------------------------------------------*/
  6100. uint64 pc_jobid2mapid(unsigned short b_class)
  6101. {
  6102. switch(b_class)
  6103. {
  6104. //Novice And 1-1 Jobs
  6105. case JOB_NOVICE: return MAPID_NOVICE;
  6106. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  6107. case JOB_MAGE: return MAPID_MAGE;
  6108. case JOB_ARCHER: return MAPID_ARCHER;
  6109. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  6110. case JOB_MERCHANT: return MAPID_MERCHANT;
  6111. case JOB_THIEF: return MAPID_THIEF;
  6112. case JOB_TAEKWON: return MAPID_TAEKWON;
  6113. case JOB_WEDDING: return MAPID_WEDDING;
  6114. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  6115. case JOB_NINJA: return MAPID_NINJA;
  6116. case JOB_XMAS: return MAPID_XMAS;
  6117. case JOB_SUMMER: return MAPID_SUMMER;
  6118. case JOB_HANBOK: return MAPID_HANBOK;
  6119. case JOB_GANGSI: return MAPID_GANGSI;
  6120. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  6121. case JOB_SUMMER2: return MAPID_SUMMER2;
  6122. //2-1 Jobs
  6123. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  6124. case JOB_KNIGHT: return MAPID_KNIGHT;
  6125. case JOB_WIZARD: return MAPID_WIZARD;
  6126. case JOB_HUNTER: return MAPID_HUNTER;
  6127. case JOB_PRIEST: return MAPID_PRIEST;
  6128. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  6129. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  6130. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  6131. case JOB_KAGEROU:
  6132. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  6133. case JOB_REBELLION: return MAPID_REBELLION;
  6134. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  6135. //2-2 Jobs
  6136. case JOB_CRUSADER: return MAPID_CRUSADER;
  6137. case JOB_SAGE: return MAPID_SAGE;
  6138. case JOB_BARD:
  6139. case JOB_DANCER: return MAPID_BARDDANCER;
  6140. case JOB_MONK: return MAPID_MONK;
  6141. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  6142. case JOB_ROGUE: return MAPID_ROGUE;
  6143. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  6144. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  6145. //Trans Novice And Trans 1-1 Jobs
  6146. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  6147. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  6148. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  6149. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  6150. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  6151. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  6152. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  6153. //Trans 2-1 Jobs
  6154. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  6155. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  6156. case JOB_SNIPER: return MAPID_SNIPER;
  6157. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  6158. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  6159. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  6160. //Trans 2-2 Jobs
  6161. case JOB_PALADIN: return MAPID_PALADIN;
  6162. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  6163. case JOB_CLOWN:
  6164. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  6165. case JOB_CHAMPION: return MAPID_CHAMPION;
  6166. case JOB_CREATOR: return MAPID_CREATOR;
  6167. case JOB_STALKER: return MAPID_STALKER;
  6168. //Baby Novice And Baby 1-1 Jobs
  6169. case JOB_BABY: return MAPID_BABY;
  6170. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  6171. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  6172. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  6173. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  6174. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  6175. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  6176. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  6177. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  6178. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  6179. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  6180. //Baby 2-1 Jobs
  6181. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  6182. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  6183. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  6184. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  6185. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  6186. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  6187. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  6188. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  6189. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  6190. case JOB_BABY_KAGEROU:
  6191. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  6192. //Baby 2-2 Jobs
  6193. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  6194. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  6195. case JOB_BABY_BARD:
  6196. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  6197. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  6198. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  6199. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  6200. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  6201. //3-1 Jobs
  6202. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  6203. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  6204. case JOB_WARLOCK: return MAPID_WARLOCK;
  6205. case JOB_RANGER: return MAPID_RANGER;
  6206. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  6207. case JOB_MECHANIC: return MAPID_MECHANIC;
  6208. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  6209. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  6210. //3-2 Jobs
  6211. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  6212. case JOB_SORCERER: return MAPID_SORCERER;
  6213. case JOB_MINSTREL:
  6214. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  6215. case JOB_SURA: return MAPID_SURA;
  6216. case JOB_GENETIC: return MAPID_GENETIC;
  6217. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  6218. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  6219. //Trans 3-1 Jobs
  6220. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  6221. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  6222. case JOB_RANGER_T: return MAPID_RANGER_T;
  6223. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  6224. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  6225. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  6226. //Trans 3-2 Jobs
  6227. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  6228. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  6229. case JOB_MINSTREL_T:
  6230. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  6231. case JOB_SURA_T: return MAPID_SURA_T;
  6232. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  6233. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  6234. //Baby 3-1 Jobs
  6235. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  6236. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  6237. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  6238. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  6239. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  6240. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  6241. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  6242. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  6243. //Baby 3-2 Jobs
  6244. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  6245. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  6246. case JOB_BABY_MINSTREL:
  6247. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  6248. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  6249. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  6250. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  6251. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  6252. //Doram Jobs
  6253. case JOB_SUMMONER: return MAPID_SUMMONER;
  6254. case JOB_SPIRIT_HANDLER: return MAPID_SPIRIT_HANDLER;
  6255. //4-1 Jobs
  6256. case JOB_HYPER_NOVICE: return MAPID_HYPER_NOVICE;
  6257. case JOB_DRAGON_KNIGHT: return MAPID_DRAGON_KNIGHT;
  6258. case JOB_ARCH_MAGE: return MAPID_ARCH_MAGE;
  6259. case JOB_WINDHAWK: return MAPID_WINDHAWK;
  6260. case JOB_CARDINAL: return MAPID_CARDINAL;
  6261. case JOB_MEISTER: return MAPID_MEISTER;
  6262. case JOB_SHADOW_CROSS: return MAPID_SHADOW_CROSS;
  6263. case JOB_SKY_EMPEROR: return MAPID_SKY_EMPEROR;
  6264. case JOB_NIGHT_WATCH: return MAPID_NIGHT_WATCH;
  6265. case JOB_SHINKIRO:
  6266. case JOB_SHIRANUI: return MAPID_SHINKIRO_SHIRANUI;
  6267. //4-2 Jobs
  6268. case JOB_IMPERIAL_GUARD: return MAPID_IMPERIAL_GUARD;
  6269. case JOB_ELEMENTAL_MASTER: return MAPID_ELEMENTAL_MASTER;
  6270. case JOB_INQUISITOR: return MAPID_INQUISITOR;
  6271. case JOB_TROUBADOUR:
  6272. case JOB_TROUVERE: return MAPID_TROUBADOURTROUVERE;
  6273. case JOB_BIOLO: return MAPID_BIOLO;
  6274. case JOB_ABYSS_CHASER: return MAPID_ABYSS_CHASER;
  6275. case JOB_SOUL_ASCETIC: return MAPID_SOUL_ASCETIC;
  6276. //Unknown
  6277. default:
  6278. return -1;
  6279. }
  6280. }
  6281. //Reverts the map-style class id to the client-style one.
  6282. int pc_mapid2jobid(uint64 class_, int sex)
  6283. {
  6284. switch(class_) {
  6285. //Novice And 1-1 Jobs
  6286. case MAPID_NOVICE: return JOB_NOVICE;
  6287. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  6288. case MAPID_MAGE: return JOB_MAGE;
  6289. case MAPID_ARCHER: return JOB_ARCHER;
  6290. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  6291. case MAPID_MERCHANT: return JOB_MERCHANT;
  6292. case MAPID_THIEF: return JOB_THIEF;
  6293. case MAPID_TAEKWON: return JOB_TAEKWON;
  6294. case MAPID_WEDDING: return JOB_WEDDING;
  6295. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  6296. case MAPID_NINJA: return JOB_NINJA;
  6297. case MAPID_XMAS: return JOB_XMAS;
  6298. case MAPID_SUMMER: return JOB_SUMMER;
  6299. case MAPID_HANBOK: return JOB_HANBOK;
  6300. case MAPID_GANGSI: return JOB_GANGSI;
  6301. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  6302. case MAPID_SUMMER2: return JOB_SUMMER2;
  6303. //2-1 Jobs
  6304. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  6305. case MAPID_KNIGHT: return JOB_KNIGHT;
  6306. case MAPID_WIZARD: return JOB_WIZARD;
  6307. case MAPID_HUNTER: return JOB_HUNTER;
  6308. case MAPID_PRIEST: return JOB_PRIEST;
  6309. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  6310. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  6311. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  6312. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  6313. case MAPID_REBELLION: return JOB_REBELLION;
  6314. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  6315. //2-2 Jobs
  6316. case MAPID_CRUSADER: return JOB_CRUSADER;
  6317. case MAPID_SAGE: return JOB_SAGE;
  6318. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  6319. case MAPID_MONK: return JOB_MONK;
  6320. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  6321. case MAPID_ROGUE: return JOB_ROGUE;
  6322. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  6323. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  6324. //Trans Novice And Trans 2-1 Jobs
  6325. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  6326. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  6327. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  6328. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  6329. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  6330. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  6331. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  6332. //Trans 2-1 Jobs
  6333. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  6334. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  6335. case MAPID_SNIPER: return JOB_SNIPER;
  6336. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  6337. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  6338. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  6339. //Trans 2-2 Jobs
  6340. case MAPID_PALADIN: return JOB_PALADIN;
  6341. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  6342. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  6343. case MAPID_CHAMPION: return JOB_CHAMPION;
  6344. case MAPID_CREATOR: return JOB_CREATOR;
  6345. case MAPID_STALKER: return JOB_STALKER;
  6346. //Baby Novice And Baby 1-1 Jobs
  6347. case MAPID_BABY: return JOB_BABY;
  6348. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  6349. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  6350. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  6351. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  6352. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  6353. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  6354. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  6355. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  6356. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  6357. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  6358. //Baby 2-1 Jobs
  6359. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  6360. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  6361. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  6362. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  6363. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  6364. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  6365. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  6366. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  6367. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  6368. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  6369. //Baby 2-2 Jobs
  6370. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  6371. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  6372. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  6373. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  6374. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  6375. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  6376. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  6377. //3-1 Jobs
  6378. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  6379. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  6380. case MAPID_WARLOCK: return JOB_WARLOCK;
  6381. case MAPID_RANGER: return JOB_RANGER;
  6382. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  6383. case MAPID_MECHANIC: return JOB_MECHANIC;
  6384. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  6385. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  6386. //3-2 Jobs
  6387. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  6388. case MAPID_SORCERER: return JOB_SORCERER;
  6389. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  6390. case MAPID_SURA: return JOB_SURA;
  6391. case MAPID_GENETIC: return JOB_GENETIC;
  6392. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  6393. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  6394. //Trans 3-1 Jobs
  6395. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  6396. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  6397. case MAPID_RANGER_T: return JOB_RANGER_T;
  6398. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  6399. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  6400. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  6401. //Trans 3-2 Jobs
  6402. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  6403. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  6404. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  6405. case MAPID_SURA_T: return JOB_SURA_T;
  6406. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  6407. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  6408. //Baby 3-1 Jobs
  6409. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  6410. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  6411. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  6412. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  6413. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  6414. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  6415. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  6416. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  6417. //Baby 3-2 Jobs
  6418. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  6419. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  6420. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  6421. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  6422. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  6423. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  6424. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  6425. //Doram Jobs
  6426. case MAPID_SUMMONER: return JOB_SUMMONER;
  6427. case MAPID_SPIRIT_HANDLER: return JOB_SPIRIT_HANDLER;
  6428. //4-1 Jobs
  6429. case MAPID_HYPER_NOVICE: return JOB_HYPER_NOVICE;
  6430. case MAPID_DRAGON_KNIGHT: return JOB_DRAGON_KNIGHT;
  6431. case MAPID_ARCH_MAGE: return JOB_ARCH_MAGE;
  6432. case MAPID_WINDHAWK: return JOB_WINDHAWK;
  6433. case MAPID_CARDINAL: return JOB_CARDINAL;
  6434. case MAPID_MEISTER: return JOB_MEISTER;
  6435. case MAPID_SHADOW_CROSS: return JOB_SHADOW_CROSS;
  6436. case MAPID_SKY_EMPEROR: return JOB_SKY_EMPEROR;
  6437. case MAPID_NIGHT_WATCH: return JOB_NIGHT_WATCH;
  6438. case MAPID_SHINKIRO_SHIRANUI: return sex?JOB_SHINKIRO:JOB_SHIRANUI;
  6439. //4-2 Jobs
  6440. case MAPID_IMPERIAL_GUARD: return JOB_IMPERIAL_GUARD;
  6441. case MAPID_ELEMENTAL_MASTER: return JOB_ELEMENTAL_MASTER;
  6442. case MAPID_INQUISITOR: return JOB_INQUISITOR;
  6443. case MAPID_TROUBADOURTROUVERE: return sex?JOB_TROUBADOUR:JOB_TROUVERE;
  6444. case MAPID_BIOLO: return JOB_BIOLO;
  6445. case MAPID_ABYSS_CHASER: return JOB_ABYSS_CHASER;
  6446. case MAPID_SOUL_ASCETIC: return JOB_SOUL_ASCETIC;
  6447. //Unknown
  6448. default:
  6449. return -1;
  6450. }
  6451. }
  6452. /*====================================================
  6453. * This function return the name of the job (by [Yor])
  6454. *----------------------------------------------------*/
  6455. const char* job_name(int class_)
  6456. {
  6457. switch (class_) {
  6458. case JOB_NOVICE:
  6459. case JOB_SWORDMAN:
  6460. case JOB_MAGE:
  6461. case JOB_ARCHER:
  6462. case JOB_ACOLYTE:
  6463. case JOB_MERCHANT:
  6464. case JOB_THIEF:
  6465. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  6466. case JOB_KNIGHT:
  6467. case JOB_PRIEST:
  6468. case JOB_WIZARD:
  6469. case JOB_BLACKSMITH:
  6470. case JOB_HUNTER:
  6471. case JOB_ASSASSIN:
  6472. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  6473. case JOB_KNIGHT2:
  6474. return msg_txt(NULL,557);
  6475. case JOB_CRUSADER:
  6476. case JOB_MONK:
  6477. case JOB_SAGE:
  6478. case JOB_ROGUE:
  6479. case JOB_ALCHEMIST:
  6480. case JOB_BARD:
  6481. case JOB_DANCER:
  6482. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  6483. case JOB_CRUSADER2:
  6484. return msg_txt(NULL,563);
  6485. case JOB_WEDDING:
  6486. case JOB_SUPER_NOVICE:
  6487. case JOB_GUNSLINGER:
  6488. case JOB_NINJA:
  6489. case JOB_XMAS:
  6490. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  6491. case JOB_SUMMER:
  6492. case JOB_SUMMER2:
  6493. return msg_txt(NULL,621);
  6494. case JOB_HANBOK:
  6495. return msg_txt(NULL,694);
  6496. case JOB_OKTOBERFEST:
  6497. return msg_txt(NULL,696);
  6498. case JOB_NOVICE_HIGH:
  6499. case JOB_SWORDMAN_HIGH:
  6500. case JOB_MAGE_HIGH:
  6501. case JOB_ARCHER_HIGH:
  6502. case JOB_ACOLYTE_HIGH:
  6503. case JOB_MERCHANT_HIGH:
  6504. case JOB_THIEF_HIGH:
  6505. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  6506. case JOB_LORD_KNIGHT:
  6507. case JOB_HIGH_PRIEST:
  6508. case JOB_HIGH_WIZARD:
  6509. case JOB_WHITESMITH:
  6510. case JOB_SNIPER:
  6511. case JOB_ASSASSIN_CROSS:
  6512. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  6513. case JOB_LORD_KNIGHT2:
  6514. return msg_txt(NULL,582);
  6515. case JOB_PALADIN:
  6516. case JOB_CHAMPION:
  6517. case JOB_PROFESSOR:
  6518. case JOB_STALKER:
  6519. case JOB_CREATOR:
  6520. case JOB_CLOWN:
  6521. case JOB_GYPSY:
  6522. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  6523. case JOB_PALADIN2:
  6524. return msg_txt(NULL,588);
  6525. case JOB_BABY:
  6526. case JOB_BABY_SWORDMAN:
  6527. case JOB_BABY_MAGE:
  6528. case JOB_BABY_ARCHER:
  6529. case JOB_BABY_ACOLYTE:
  6530. case JOB_BABY_MERCHANT:
  6531. case JOB_BABY_THIEF:
  6532. return msg_txt(NULL,595 - JOB_BABY + class_);
  6533. case JOB_BABY_KNIGHT:
  6534. case JOB_BABY_PRIEST:
  6535. case JOB_BABY_WIZARD:
  6536. case JOB_BABY_BLACKSMITH:
  6537. case JOB_BABY_HUNTER:
  6538. case JOB_BABY_ASSASSIN:
  6539. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  6540. case JOB_BABY_KNIGHT2:
  6541. return msg_txt(NULL,602);
  6542. case JOB_BABY_CRUSADER:
  6543. case JOB_BABY_MONK:
  6544. case JOB_BABY_SAGE:
  6545. case JOB_BABY_ROGUE:
  6546. case JOB_BABY_ALCHEMIST:
  6547. case JOB_BABY_BARD:
  6548. case JOB_BABY_DANCER:
  6549. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  6550. case JOB_BABY_CRUSADER2:
  6551. return msg_txt(NULL,608);
  6552. case JOB_SUPER_BABY:
  6553. return msg_txt(NULL,615);
  6554. case JOB_TAEKWON:
  6555. return msg_txt(NULL,616);
  6556. case JOB_STAR_GLADIATOR:
  6557. case JOB_STAR_GLADIATOR2:
  6558. return msg_txt(NULL,617);
  6559. case JOB_SOUL_LINKER:
  6560. return msg_txt(NULL,618);
  6561. case JOB_GANGSI:
  6562. case JOB_DEATH_KNIGHT:
  6563. case JOB_DARK_COLLECTOR:
  6564. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  6565. case JOB_RUNE_KNIGHT:
  6566. case JOB_WARLOCK:
  6567. case JOB_RANGER:
  6568. case JOB_ARCH_BISHOP:
  6569. case JOB_MECHANIC:
  6570. case JOB_GUILLOTINE_CROSS:
  6571. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  6572. case JOB_RUNE_KNIGHT_T:
  6573. case JOB_WARLOCK_T:
  6574. case JOB_RANGER_T:
  6575. case JOB_ARCH_BISHOP_T:
  6576. case JOB_MECHANIC_T:
  6577. case JOB_GUILLOTINE_CROSS_T:
  6578. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  6579. case JOB_ROYAL_GUARD:
  6580. case JOB_SORCERER:
  6581. case JOB_MINSTREL:
  6582. case JOB_WANDERER:
  6583. case JOB_SURA:
  6584. case JOB_GENETIC:
  6585. case JOB_SHADOW_CHASER:
  6586. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  6587. case JOB_ROYAL_GUARD_T:
  6588. case JOB_SORCERER_T:
  6589. case JOB_MINSTREL_T:
  6590. case JOB_WANDERER_T:
  6591. case JOB_SURA_T:
  6592. case JOB_GENETIC_T:
  6593. case JOB_SHADOW_CHASER_T:
  6594. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  6595. case JOB_RUNE_KNIGHT2:
  6596. case JOB_RUNE_KNIGHT_T2:
  6597. return msg_txt(NULL,625);
  6598. case JOB_ROYAL_GUARD2:
  6599. case JOB_ROYAL_GUARD_T2:
  6600. return msg_txt(NULL,631);
  6601. case JOB_RANGER2:
  6602. case JOB_RANGER_T2:
  6603. return msg_txt(NULL,627);
  6604. case JOB_MECHANIC2:
  6605. case JOB_MECHANIC_T2:
  6606. return msg_txt(NULL,629);
  6607. case JOB_BABY_RUNE_KNIGHT:
  6608. case JOB_BABY_WARLOCK:
  6609. case JOB_BABY_RANGER:
  6610. case JOB_BABY_ARCH_BISHOP:
  6611. case JOB_BABY_MECHANIC:
  6612. case JOB_BABY_GUILLOTINE_CROSS:
  6613. case JOB_BABY_ROYAL_GUARD:
  6614. case JOB_BABY_SORCERER:
  6615. case JOB_BABY_MINSTREL:
  6616. case JOB_BABY_WANDERER:
  6617. case JOB_BABY_SURA:
  6618. case JOB_BABY_GENETIC:
  6619. case JOB_BABY_SHADOW_CHASER:
  6620. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6621. case JOB_BABY_RUNE_KNIGHT2:
  6622. return msg_txt(NULL,638);
  6623. case JOB_BABY_ROYAL_GUARD2:
  6624. return msg_txt(NULL,644);
  6625. case JOB_BABY_RANGER2:
  6626. return msg_txt(NULL,640);
  6627. case JOB_BABY_MECHANIC2:
  6628. return msg_txt(NULL,642);
  6629. case JOB_SUPER_NOVICE_E:
  6630. case JOB_SUPER_BABY_E:
  6631. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  6632. case JOB_KAGEROU:
  6633. case JOB_OBORO:
  6634. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  6635. case JOB_REBELLION:
  6636. return msg_txt(NULL,695);
  6637. case JOB_SUMMONER:
  6638. return msg_txt(NULL,697);
  6639. case JOB_BABY_SUMMONER:
  6640. return msg_txt(NULL,698);
  6641. case JOB_BABY_NINJA:
  6642. return msg_txt(NULL,699);
  6643. case JOB_BABY_KAGEROU:
  6644. case JOB_BABY_OBORO:
  6645. case JOB_BABY_TAEKWON:
  6646. case JOB_BABY_STAR_GLADIATOR:
  6647. case JOB_BABY_SOUL_LINKER:
  6648. case JOB_BABY_GUNSLINGER:
  6649. case JOB_BABY_REBELLION:
  6650. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  6651. case JOB_BABY_STAR_GLADIATOR2:
  6652. return msg_txt(NULL,756);
  6653. case JOB_STAR_EMPEROR:
  6654. case JOB_SOUL_REAPER:
  6655. case JOB_BABY_STAR_EMPEROR:
  6656. case JOB_BABY_SOUL_REAPER:
  6657. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  6658. case JOB_STAR_EMPEROR2:
  6659. return msg_txt(NULL,782);
  6660. case JOB_BABY_STAR_EMPEROR2:
  6661. return msg_txt(NULL,784);
  6662. case JOB_DRAGON_KNIGHT:
  6663. case JOB_MEISTER:
  6664. case JOB_SHADOW_CROSS:
  6665. case JOB_ARCH_MAGE:
  6666. case JOB_CARDINAL:
  6667. case JOB_WINDHAWK:
  6668. case JOB_IMPERIAL_GUARD:
  6669. case JOB_BIOLO:
  6670. case JOB_ABYSS_CHASER:
  6671. case JOB_ELEMENTAL_MASTER:
  6672. case JOB_INQUISITOR:
  6673. case JOB_TROUBADOUR:
  6674. case JOB_TROUVERE:
  6675. return msg_txt( nullptr, 800 - JOB_DRAGON_KNIGHT + class_ );
  6676. case JOB_WINDHAWK2:
  6677. return msg_txt( nullptr, 805);
  6678. case JOB_MEISTER2:
  6679. return msg_txt( nullptr, 801 );
  6680. case JOB_DRAGON_KNIGHT2:
  6681. return msg_txt( nullptr, 800 );
  6682. case JOB_IMPERIAL_GUARD2:
  6683. return msg_txt( nullptr, 806 );
  6684. case JOB_SKY_EMPEROR:
  6685. case JOB_SOUL_ASCETIC:
  6686. case JOB_SHINKIRO:
  6687. case JOB_SHIRANUI:
  6688. case JOB_NIGHT_WATCH:
  6689. case JOB_HYPER_NOVICE:
  6690. case JOB_SPIRIT_HANDLER:
  6691. return msg_txt( nullptr, 813 - JOB_SKY_EMPEROR + class_ );
  6692. case JOB_SKY_EMPEROR2:
  6693. return msg_txt( nullptr, 813 );
  6694. default:
  6695. return msg_txt(NULL,655);
  6696. }
  6697. }
  6698. /*====================================================
  6699. * Timered function to make id follow a target.
  6700. * @id = bl.id (player only atm)
  6701. * target is define in sd->followtarget (bl.id)
  6702. * used by pc_follow
  6703. *----------------------------------------------------*/
  6704. TIMER_FUNC(pc_follow_timer){
  6705. struct map_session_data *sd;
  6706. struct block_list *tbl;
  6707. sd = map_id2sd(id);
  6708. nullpo_ret(sd);
  6709. if (sd->followtimer != tid){
  6710. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6711. sd->followtimer = INVALID_TIMER;
  6712. return 0;
  6713. }
  6714. sd->followtimer = INVALID_TIMER;
  6715. tbl = map_id2bl(sd->followtarget);
  6716. if (tbl == NULL || pc_isdead(sd))
  6717. {
  6718. pc_stop_following(sd);
  6719. return 0;
  6720. }
  6721. // either player or target is currently detached from map blocks (could be teleporting),
  6722. // but still connected to this map, so we'll just increment the timer and check back later
  6723. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  6724. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6725. {
  6726. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  6727. if (!check_distance_bl(&sd->bl, tbl, 5))
  6728. unit_walktobl(&sd->bl, tbl, 5, 0);
  6729. } else
  6730. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  6731. }
  6732. sd->followtimer = add_timer(
  6733. tick + 1000, // increase time a bit to loosen up map's load
  6734. pc_follow_timer, sd->bl.id, 0);
  6735. return 0;
  6736. }
  6737. int pc_stop_following (struct map_session_data *sd)
  6738. {
  6739. nullpo_ret(sd);
  6740. if (sd->followtimer != INVALID_TIMER) {
  6741. delete_timer(sd->followtimer,pc_follow_timer);
  6742. sd->followtimer = INVALID_TIMER;
  6743. }
  6744. sd->followtarget = -1;
  6745. sd->ud.target_to = 0;
  6746. unit_stop_walking(&sd->bl, 1);
  6747. return 0;
  6748. }
  6749. int pc_follow(struct map_session_data *sd,int target_id)
  6750. {
  6751. struct block_list *bl = map_id2bl(target_id);
  6752. if (bl == NULL /*|| bl->type != BL_PC*/)
  6753. return 1;
  6754. if (sd->followtimer != INVALID_TIMER)
  6755. pc_stop_following(sd);
  6756. sd->followtarget = target_id;
  6757. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6758. return 0;
  6759. }
  6760. int pc_checkbaselevelup(struct map_session_data *sd) {
  6761. t_exp next = pc_nextbaseexp(sd);
  6762. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  6763. return 0;
  6764. uint32 base_level = sd->status.base_level;
  6765. do {
  6766. sd->status.base_exp -= next;
  6767. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6768. 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 )
  6769. sd->status.base_exp = next-1;
  6770. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level);
  6771. sd->status.trait_point += statpoint_db.pc_gets_trait_point(sd->status.base_level);
  6772. sd->status.base_level++;
  6773. if( pc_is_maxbaselv(sd) ){
  6774. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  6775. break;
  6776. }
  6777. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  6778. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  6779. status_calc_pet(sd->pd,SCO_NONE);
  6780. clif_updatestatus(sd,SP_STATUSPOINT);
  6781. clif_updatestatus(sd,SP_TRAITPOINT);
  6782. clif_updatestatus(sd,SP_BASELEVEL);
  6783. clif_updatestatus(sd,SP_BASEEXP);
  6784. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6785. status_calc_pc(sd,SCO_FORCE);
  6786. status_percent_heal(&sd->bl,100,100);
  6787. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6788. for (const auto &status : status_db) {
  6789. if (status.second->flag[SCF_SUPERNOVICEANGEL])
  6790. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 1, skill_get_time(status.second->skill_id, 1));
  6791. }
  6792. if (sd->state.snovice_dead_flag)
  6793. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6794. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6795. for (const auto &status : status_db) {
  6796. if (status.second->flag[SCF_TAEKWONANGEL])
  6797. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 10, 600000);
  6798. }
  6799. }
  6800. clif_misceffect(&sd->bl,0);
  6801. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6802. if(sd->status.party_id)
  6803. party_send_levelup(sd);
  6804. pc_baselevelchanged(sd);
  6805. for (; base_level <= sd->status.base_level; base_level++) {
  6806. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  6807. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  6808. }
  6809. return 1;
  6810. }
  6811. void pc_baselevelchanged(struct map_session_data *sd) {
  6812. uint8 i;
  6813. for( i = 0; i < EQI_MAX; i++ ) {
  6814. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  6815. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6816. pc_unequipitem(sd, sd->equip_index[i], 3);
  6817. }
  6818. }
  6819. pc_show_questinfo(sd);
  6820. }
  6821. int pc_checkjoblevelup(struct map_session_data *sd)
  6822. {
  6823. t_exp next = pc_nextjobexp(sd);
  6824. nullpo_ret(sd);
  6825. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6826. return 0;
  6827. uint32 job_level = sd->status.job_level;
  6828. do {
  6829. sd->status.job_exp -= next;
  6830. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6831. 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 )
  6832. sd->status.job_exp = next-1;
  6833. sd->status.job_level ++;
  6834. sd->status.skill_point ++;
  6835. if( pc_is_maxjoblv(sd) ){
  6836. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6837. break;
  6838. }
  6839. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6840. clif_updatestatus(sd,SP_JOBLEVEL);
  6841. clif_updatestatus(sd,SP_JOBEXP);
  6842. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6843. clif_updatestatus(sd,SP_SKILLPOINT);
  6844. status_calc_pc(sd,SCO_FORCE);
  6845. clif_misceffect(&sd->bl,1);
  6846. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6847. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6848. npc_script_event(sd, NPCE_JOBLVUP);
  6849. for (; job_level <= sd->status.job_level; job_level++)
  6850. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  6851. pc_show_questinfo(sd);
  6852. return 1;
  6853. }
  6854. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6855. * @param sd Player
  6856. * @param base_exp Base EXP before peronal bonuses
  6857. * @param job_exp Job EXP before peronal bonuses
  6858. * @param src Block list that affecting the exp calculation
  6859. */
  6860. static void pc_calcexp(struct map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  6861. {
  6862. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6863. if (src) {
  6864. struct status_data *status = status_get_status_data(src);
  6865. if( sd->indexed_bonus.expaddrace[status->race] )
  6866. bonus += sd->indexed_bonus.expaddrace[status->race];
  6867. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  6868. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  6869. if( sd->indexed_bonus.expaddclass[status->class_] )
  6870. bonus += sd->indexed_bonus.expaddclass[status->class_];
  6871. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  6872. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  6873. if (battle_config.pk_mode &&
  6874. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6875. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6876. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6877. vip_bonus_base = battle_config.vip_base_exp_increase;
  6878. vip_bonus_job = battle_config.vip_job_exp_increase;
  6879. }
  6880. }
  6881. // Give EXPBOOST for quests even if src is NULL.
  6882. if (sd->sc.data[SC_EXPBOOST]) {
  6883. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6884. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6885. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6886. }
  6887. if (*base_exp) {
  6888. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  6889. *base_exp = cap_value(exp, 1, MAX_EXP);
  6890. }
  6891. // Give JEXPBOOST for quests even if src is NULL.
  6892. if (sd->sc.data[SC_JEXPBOOST])
  6893. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6894. if (*job_exp) {
  6895. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  6896. *job_exp = cap_value(exp, 1, MAX_EXP);
  6897. }
  6898. return;
  6899. }
  6900. /**
  6901. * Show EXP gained by player in percentage by @showexp
  6902. * @param sd Player
  6903. * @param base_exp Base EXP gained/loss
  6904. * @param next_base_exp Base EXP needed for next base level
  6905. * @param job_exp Job EXP gained/loss
  6906. * @param next_job_exp Job EXP needed for next job level
  6907. * @param lost True:EXP penalty, lose EXP
  6908. **/
  6909. void pc_gainexp_disp(struct map_session_data *sd, t_exp base_exp, t_exp next_base_exp, t_exp job_exp, t_exp next_job_exp, bool lost) {
  6910. char output[CHAT_SIZE_MAX];
  6911. nullpo_retv(sd);
  6912. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6913. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6914. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6915. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6916. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6917. }
  6918. /**
  6919. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6920. * @param sd Player
  6921. * @param src EXP source
  6922. * @param base_exp Base EXP gained
  6923. * @param base_exp Job EXP gained
  6924. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6925. * @return
  6926. **/
  6927. void pc_gainexp(struct map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  6928. {
  6929. t_exp nextb = 0, nextj = 0;
  6930. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6931. nullpo_retv(sd);
  6932. if(sd->bl.prev == NULL || pc_isdead(sd))
  6933. return;
  6934. if (!(exp_flag&2)) {
  6935. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6936. return; // no exp on pvp maps
  6937. if (sd->status.guild_id>0)
  6938. base_exp -= guild_payexp(sd,base_exp);
  6939. }
  6940. flag = ((base_exp) ? 1 : 0) |
  6941. ((job_exp) ? 2 : 0) |
  6942. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6943. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6944. if (!(exp_flag&2))
  6945. pc_calcexp(sd, &base_exp, &job_exp, src);
  6946. nextb = pc_nextbaseexp(sd);
  6947. nextj = pc_nextjobexp(sd);
  6948. if (flag&4){
  6949. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6950. base_exp = 0;
  6951. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6952. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6953. }
  6954. if (flag&8){
  6955. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6956. job_exp = 0;
  6957. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6958. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6959. }
  6960. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6961. //Note that this value should never be greater than the original
  6962. //therefore no overflow checks are needed. [Skotlex]
  6963. if (nextb > 0) {
  6964. float nextbp = (float) base_exp / (float) nextb;
  6965. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6966. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6967. }
  6968. if (nextj > 0) {
  6969. float nextjp = (float) job_exp / (float) nextj;
  6970. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6971. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6972. }
  6973. }
  6974. // Give EXP for Base Level
  6975. if (base_exp) {
  6976. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  6977. if (!pc_checkbaselevelup(sd))
  6978. clif_updatestatus(sd,SP_BASEEXP);
  6979. }
  6980. // Give EXP for Job Level
  6981. if (job_exp) {
  6982. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  6983. if (!pc_checkjoblevelup(sd))
  6984. clif_updatestatus(sd,SP_JOBEXP);
  6985. }
  6986. if (flag&1)
  6987. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6988. if (flag&2)
  6989. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6990. if (sd->state.showexp && (base_exp || job_exp))
  6991. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6992. }
  6993. /**
  6994. * Lost Base/Job EXP from a player
  6995. * @param sd Player
  6996. * @param base_exp Base EXP lost
  6997. * @param job_exp Job EXP lost
  6998. **/
  6999. void pc_lostexp(struct map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  7000. nullpo_retv(sd);
  7001. if (base_exp) {
  7002. base_exp = u64min(sd->status.base_exp, base_exp);
  7003. sd->status.base_exp -= base_exp;
  7004. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  7005. clif_updatestatus(sd, SP_BASEEXP);
  7006. }
  7007. if (job_exp) {
  7008. job_exp = u64min(sd->status.job_exp, job_exp);
  7009. sd->status.job_exp -= job_exp;
  7010. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  7011. clif_updatestatus(sd, SP_JOBEXP);
  7012. }
  7013. if (sd->state.showexp && (base_exp || job_exp))
  7014. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  7015. }
  7016. /**
  7017. * Returns max base level for this character's class.
  7018. * @param job_id: Player's class
  7019. * @return Max Base Level
  7020. */
  7021. uint32 JobDatabase::get_maxBaseLv(uint16 job_id) {
  7022. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7023. return job ? job->max_base_level : 0;
  7024. }
  7025. /**
  7026. * Returns max base level for this character.
  7027. * @param sd Player
  7028. * @return Max Base Level
  7029. **/
  7030. unsigned int pc_maxbaselv(struct map_session_data *sd){
  7031. return job_db.get_maxBaseLv(sd->status.class_);
  7032. }
  7033. /**
  7034. * Returns max job level for this character's class.
  7035. * @param job_id: Player's class
  7036. * @return Max Job Level
  7037. */
  7038. uint32 JobDatabase::get_maxJobLv(uint16 job_id) {
  7039. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7040. return job ? job->max_job_level : 0;
  7041. }
  7042. /**
  7043. * Returns max job level for this character.
  7044. * @param sd Player
  7045. * @return Max Job Level
  7046. **/
  7047. unsigned int pc_maxjoblv(struct map_session_data *sd){
  7048. return job_db.get_maxJobLv(sd->status.class_);
  7049. }
  7050. /**
  7051. * Check if player is reached max base level
  7052. * @param sd
  7053. * @return True if reached max level
  7054. **/
  7055. bool pc_is_maxbaselv(struct map_session_data *sd) {
  7056. nullpo_retr(false, sd);
  7057. return (sd->status.base_level >= pc_maxbaselv(sd));
  7058. }
  7059. /**
  7060. * Check if player is reached max base level
  7061. * @param sd
  7062. * @return True if reached max level
  7063. **/
  7064. bool pc_is_maxjoblv(struct map_session_data *sd) {
  7065. nullpo_retr(false, sd);
  7066. return (sd->status.job_level >= pc_maxjoblv(sd));
  7067. }
  7068. /**
  7069. * Returns base experience for this character's class.
  7070. * @param job_id: Player's class
  7071. * @param level: Player's level
  7072. * @return Base EXP
  7073. */
  7074. t_exp JobDatabase::get_baseExp(uint16 job_id, uint32 level) {
  7075. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7076. return job ? job->base_exp[level - 1] : 0;
  7077. }
  7078. /**
  7079. * Base exp needed for player to level up.
  7080. * @param sd
  7081. * @return Base EXP needed for next base level
  7082. **/
  7083. t_exp pc_nextbaseexp(struct map_session_data *sd){
  7084. nullpo_ret(sd);
  7085. if (sd->status.base_level == 0) // Is this something that possible?
  7086. return 0;
  7087. if (pc_is_maxbaselv(sd))
  7088. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  7089. return static_cast<t_exp>(job_db.get_baseExp(sd->status.class_, sd->status.base_level));
  7090. }
  7091. /**
  7092. * Returns job experience for this character's class.
  7093. * @param job_id: Player's class
  7094. * @param level: Player's level
  7095. * @return Job EXP
  7096. */
  7097. t_exp JobDatabase::get_jobExp(uint16 job_id, uint32 level) {
  7098. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7099. return job ? job->job_exp[level - 1] : 0;
  7100. }
  7101. /**
  7102. * Job exp needed for player to level up.
  7103. * @param sd
  7104. * @return Job EXP needed for next job level
  7105. **/
  7106. t_exp pc_nextjobexp(struct map_session_data *sd){
  7107. nullpo_ret(sd);
  7108. if (sd->status.job_level == 0) // Is this something that possible?
  7109. return 0;
  7110. if (pc_is_maxjoblv(sd))
  7111. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  7112. return static_cast<t_exp>(job_db.get_jobExp(sd->status.class_, sd->status.job_level));
  7113. }
  7114. /**
  7115. * Returns max weight base for this character's class.
  7116. * @param job_id: Player's class
  7117. * @return Max weight base
  7118. */
  7119. int32 JobDatabase::get_maxWeight(uint16 job_id) {
  7120. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7121. return job ? job->max_weight_base : 0;
  7122. }
  7123. /// Returns the value of the specified stat.
  7124. int pc_getstat(map_session_data *sd, int type)
  7125. {
  7126. nullpo_retr(-1, sd);
  7127. switch( type ) {
  7128. case SP_STR: return sd->status.str;
  7129. case SP_AGI: return sd->status.agi;
  7130. case SP_VIT: return sd->status.vit;
  7131. case SP_INT: return sd->status.int_;
  7132. case SP_DEX: return sd->status.dex;
  7133. case SP_LUK: return sd->status.luk;
  7134. case SP_POW: return sd->status.pow;
  7135. case SP_STA: return sd->status.sta;
  7136. case SP_WIS: return sd->status.wis;
  7137. case SP_SPL: return sd->status.spl;
  7138. case SP_CON: return sd->status.con;
  7139. case SP_CRT: return sd->status.crt;
  7140. default:
  7141. return -1;
  7142. }
  7143. }
  7144. /// Sets the specified stat to the specified value.
  7145. /// Returns the new value.
  7146. int pc_setstat(struct map_session_data* sd, int type, int val)
  7147. {
  7148. nullpo_retr(-1, sd);
  7149. switch( type ) {
  7150. case SP_STR: sd->status.str = val; break;
  7151. case SP_AGI: sd->status.agi = val; break;
  7152. case SP_VIT: sd->status.vit = val; break;
  7153. case SP_INT: sd->status.int_ = val; break;
  7154. case SP_DEX: sd->status.dex = val; break;
  7155. case SP_LUK: sd->status.luk = val; break;
  7156. case SP_POW: sd->status.pow = val; break;
  7157. case SP_STA: sd->status.sta = val; break;
  7158. case SP_WIS: sd->status.wis = val; break;
  7159. case SP_SPL: sd->status.spl = val; break;
  7160. case SP_CON: sd->status.con = val; break;
  7161. case SP_CRT: sd->status.crt = val; break;
  7162. default:
  7163. return -1;
  7164. }
  7165. return val;
  7166. }
  7167. /**
  7168. * Gets the total number of status points at the provided level.
  7169. * @param level: Player base level.
  7170. * @return Total number of status points at specific base level.
  7171. */
  7172. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  7173. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7174. if( entry != nullptr ){
  7175. return entry->statpoints;
  7176. }else{
  7177. return 0;
  7178. }
  7179. }
  7180. /**
  7181. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  7182. * @param level: Player base level.
  7183. * @param table: Use table value or formula.
  7184. * @return Status points at specific base level.
  7185. */
  7186. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  7187. uint32 next_level = this->get_table_point( level + 1 );
  7188. uint32 current_level = this->get_table_point( level );
  7189. if( next_level > current_level ){
  7190. return next_level - current_level;
  7191. }else{
  7192. return 0;
  7193. }
  7194. }
  7195. /**
  7196. * Gets the total number of trait points at the provided level.
  7197. * @param level: Player base level.
  7198. * @return Total number of trait points at specific base level.
  7199. */
  7200. uint32 PlayerStatPointDatabase::get_trait_table_point(uint16 level) {
  7201. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7202. if( entry != nullptr ){
  7203. return entry->traitpoints;
  7204. }else{
  7205. return 0;
  7206. }
  7207. }
  7208. /**
  7209. * Calculates the number of trait points PC gets when leveling up (from level to level+1)
  7210. * @param level: Player base level.
  7211. * @param table: Use table value or formula.
  7212. * @return Trait points at specific base level.
  7213. */
  7214. uint32 PlayerStatPointDatabase::pc_gets_trait_point(uint16 level) {
  7215. uint32 next_level = this->get_trait_table_point( level + 1 );
  7216. uint32 current_level = this->get_trait_table_point( level );
  7217. if( next_level > current_level ){
  7218. return next_level - current_level;
  7219. }else{
  7220. return 0;
  7221. }
  7222. }
  7223. #ifdef RENEWAL_STAT
  7224. /// Renewal status point cost formula
  7225. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  7226. #else
  7227. /// Pre-Renewal status point cost formula
  7228. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  7229. #endif
  7230. /// Returns the number of stat points needed to change the specified stat by val.
  7231. /// If val is negative, returns the number of stat points that would be needed to
  7232. /// raise the specified stat from (current value - val) to current value.
  7233. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  7234. {
  7235. int low, high, sp = 0, max = 0;
  7236. if ( val == 0 )
  7237. return 0;
  7238. low = pc_getstat(sd,type);
  7239. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  7240. if ( low >= max && val > 0 )
  7241. return 0; // Official servers show '0' when max is reached
  7242. high = low + val;
  7243. if ( val < 0 )
  7244. SWAP(low, high);
  7245. for ( ; low < high; low++ )
  7246. sp += PC_STATUS_POINT_COST(low);
  7247. return sp;
  7248. }
  7249. /**
  7250. * Returns the value the specified stat can be increased by with the current
  7251. * amount of available status points for the current character's class.
  7252. *
  7253. * @param sd The target character.
  7254. * @param type Stat to verify.
  7255. * @return Maximum value the stat could grow by.
  7256. */
  7257. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  7258. {
  7259. int base, final_val, status_points, max_param;
  7260. nullpo_ret(sd);
  7261. base = final_val = pc_getstat(sd, type);
  7262. status_points = sd->status.status_point;
  7263. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  7264. while (final_val <= max_param && status_points >= 0) {
  7265. status_points -= PC_STATUS_POINT_COST(final_val);
  7266. final_val++;
  7267. }
  7268. final_val--;
  7269. return (final_val > base ? final_val-base : 0);
  7270. }
  7271. /**
  7272. * Raises a stat by the specified amount.
  7273. *
  7274. * Obeys max_parameter limits.
  7275. * Subtracts status points according to the cost of the increased stat points.
  7276. *
  7277. * @param sd The target character.
  7278. * @param type The stat to change (see enum _sp)
  7279. * @param increase The stat increase (strictly positive) amount.
  7280. * @retval true if the stat was increased by any amount.
  7281. * @retval false if there were no changes.
  7282. */
  7283. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  7284. {
  7285. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  7286. nullpo_ret(sd);
  7287. // check conditions
  7288. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  7289. clif_statusupack(sd, type, 0, 0);
  7290. return false;
  7291. }
  7292. // check limits
  7293. current = pc_getstat(sd, type);
  7294. max_increase = pc_maxparameterincrease(sd, type);
  7295. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7296. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  7297. clif_statusupack(sd, type, 0, 0);
  7298. return false;
  7299. }
  7300. // check status points
  7301. needed_points = pc_need_status_point(sd, type, increase);
  7302. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  7303. clif_statusupack(sd, type, 0, 0);
  7304. return false;
  7305. }
  7306. // set new values
  7307. final_value = pc_setstat(sd, type, current + increase);
  7308. sd->status.status_point -= needed_points;
  7309. status_calc_pc(sd,SCO_NONE);
  7310. // update increase cost indicator
  7311. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7312. // update statpoint count
  7313. clif_updatestatus(sd, SP_STATUSPOINT);
  7314. // update stat value
  7315. clif_statusupack(sd, type, 1, final_value); // required
  7316. if( final_value > 255 )
  7317. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7318. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7319. return true;
  7320. }
  7321. /**
  7322. * Raises a stat by the specified amount.
  7323. *
  7324. * Obeys max_parameter limits.
  7325. * Does not subtract status points for the cost of the modified stat points.
  7326. *
  7327. * @param sd The target character.
  7328. * @param type The stat to change (see enum _sp)
  7329. * @param val The stat increase (or decrease) amount.
  7330. * @return the stat increase amount.
  7331. * @retval 0 if no changes were made.
  7332. */
  7333. int pc_statusup2(struct map_session_data* sd, int type, int val)
  7334. {
  7335. int max, need;
  7336. nullpo_ret(sd);
  7337. if( type < SP_STR || type > SP_LUK )
  7338. {
  7339. clif_statusupack(sd,type,0,0);
  7340. return 0;
  7341. }
  7342. need = pc_need_status_point(sd,type,1);
  7343. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  7344. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  7345. status_calc_pc(sd,SCO_NONE);
  7346. // update increase cost indicator
  7347. if( need != pc_need_status_point(sd,type,1) )
  7348. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7349. // update stat value
  7350. clif_statusupack(sd,type,1,val); // required
  7351. if( val > 255 )
  7352. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  7353. return val;
  7354. }
  7355. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  7356. /// If val is negative, returns the number of trait stat points that would be needed to
  7357. /// raise the specified trait stat from (current value - val) to current value.
  7358. int pc_need_trait_point(struct map_session_data* sd, int type, int val)
  7359. {
  7360. nullpo_retr(0, sd);
  7361. if (val == 0 || type < SP_POW || type > SP_CRT)
  7362. return 0;
  7363. int low = pc_getstat(sd, type);
  7364. int max = pc_maxparameter(sd, (e_params)(PARAM_POW + type - SP_POW));
  7365. if (low >= max && val > 0)
  7366. return 0; // Official servers show '0' when max is reached
  7367. int high = low + val, sp = 0;
  7368. if (val < 0)
  7369. SWAP(low, high);
  7370. for (; low < high; low++)
  7371. sp += 1;
  7372. return sp;
  7373. }
  7374. /**
  7375. * Returns the value the specified trait stat can be increased by with the current
  7376. * amount of available trait status points for the current character's class.
  7377. *
  7378. * @param sd The target character.
  7379. * @param type Trait stat to verify.
  7380. * @return Maximum value the stat could grow by.
  7381. */
  7382. int pc_maxtraitparameterincrease(struct map_session_data* sd, int type)
  7383. {
  7384. nullpo_ret(sd);
  7385. if( type < SP_POW || type > SP_CRT ){
  7386. return 0;
  7387. }
  7388. int base, final_val = pc_getstat(sd, type);
  7389. int trait_points = sd->status.trait_point;
  7390. int max_param = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW));
  7391. base = final_val;
  7392. while (final_val <= max_param && trait_points >= 0) {
  7393. trait_points -= 1;
  7394. final_val++;
  7395. }
  7396. final_val--;
  7397. return (final_val > base ? final_val - base : 0);
  7398. }
  7399. /**
  7400. * Raises a trait stat by the specified amount.
  7401. *
  7402. * Obeys max_traitparameter limits.
  7403. * Subtracts trait status points according to the cost of the increased trait stat points.
  7404. *
  7405. * @param sd The target character.
  7406. * @param type The stat to change (see enum _sp)
  7407. * @param increase The stat increase (strictly positive) amount.
  7408. * @retval true if the trait stat was increased by any amount.
  7409. * @retval false if there were no changes.
  7410. */
  7411. bool pc_traitstatusup(struct map_session_data* sd, int type, int increase)
  7412. {
  7413. nullpo_ret(sd);
  7414. // check conditions
  7415. if (type < SP_POW || type > SP_CRT || increase <= 0) {
  7416. clif_statusupack(sd, type, 0, 0);
  7417. return false;
  7418. }
  7419. // check limits
  7420. int current = pc_getstat(sd, type);
  7421. int max_increase = pc_maxtraitparameterincrease(sd, type);
  7422. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7423. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW))) {
  7424. clif_statusupack(sd, type, 0, 0);
  7425. return false;
  7426. }
  7427. // check status points
  7428. int needed_points = pc_need_trait_point(sd, type, increase);
  7429. if (needed_points < 0 || needed_points > sd->status.trait_point) { // Sanity check
  7430. clif_statusupack(sd, type, 0, 0);
  7431. return false;
  7432. }
  7433. // set new values
  7434. int final_value = pc_setstat(sd, type, current + increase);
  7435. sd->status.trait_point -= needed_points;
  7436. status_calc_pc(sd, SCO_NONE);
  7437. // update increase cost indicator
  7438. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7439. // update statpoint count
  7440. clif_updatestatus(sd, SP_TRAITPOINT);
  7441. // update stat value
  7442. clif_statusupack(sd, type, 1, final_value); // required
  7443. if (final_value > 255)
  7444. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7445. //achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7446. return true;
  7447. }
  7448. /**
  7449. * Raises a trait stat by the specified amount.
  7450. *
  7451. * Obeys max_trait_parameter limits.
  7452. * Does not subtract status points for the cost of the modified stat points.
  7453. *
  7454. * @param sd The target character.
  7455. * @param type The stat to change (see enum _sp)
  7456. * @param val The stat increase (or decrease) amount.
  7457. * @return the stat increase amount.
  7458. * @retval 0 if no changes were made.
  7459. */
  7460. int pc_traitstatusup2(struct map_session_data* sd, int type, int val)
  7461. {
  7462. nullpo_ret(sd);
  7463. if (type < SP_POW || type > SP_CRT) {
  7464. clif_statusupack(sd, type, 0, 0);
  7465. return 0;
  7466. }
  7467. int need = pc_need_trait_point(sd, type, 1);
  7468. int max = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW)); // set new value
  7469. val = pc_setstat(sd, type, cap_value(pc_getstat(sd, type) + val, 0, max));
  7470. status_calc_pc(sd, SCO_NONE);
  7471. // update increase cost indicator
  7472. if (need != pc_need_trait_point(sd, type, 1))
  7473. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7474. // update stat value
  7475. clif_statusupack(sd, type, 1, val); // required
  7476. if (val > 255)
  7477. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7478. return val;
  7479. }
  7480. /*==========================================
  7481. * Update skill_lv for player sd
  7482. * Skill point allocation
  7483. *------------------------------------------*/
  7484. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  7485. {
  7486. uint16 idx = skill_get_index(skill_id);
  7487. nullpo_retv(sd);
  7488. if (!idx) {
  7489. if (skill_id)
  7490. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  7491. return;
  7492. }
  7493. // Level up guild skill
  7494. if (SKILL_CHK_GUILD(skill_id)) {
  7495. guild_skillup(sd, skill_id);
  7496. return;
  7497. }
  7498. // Level up homunculus skill
  7499. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  7500. hom_skillup(sd->hd, skill_id);
  7501. return;
  7502. }
  7503. else {
  7504. if( sd->status.skill_point > 0 &&
  7505. sd->status.skill[idx].id &&
  7506. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  7507. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  7508. {
  7509. int lv, range, upgradable;
  7510. sd->status.skill[idx].lv++;
  7511. sd->status.skill_point--;
  7512. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  7513. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  7514. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  7515. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  7516. else
  7517. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  7518. lv = sd->status.skill[idx].lv;
  7519. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  7520. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  7521. clif_skillup(sd,skill_id,lv,range,upgradable);
  7522. clif_updatestatus(sd,SP_SKILLPOINT);
  7523. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  7524. clif_updatestatus(sd,SP_CARTINFO);
  7525. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  7526. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7527. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  7528. clif_skillinfoblock(sd);
  7529. }
  7530. //else
  7531. // 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);
  7532. }
  7533. }
  7534. /*==========================================
  7535. * /allskill
  7536. *------------------------------------------*/
  7537. int pc_allskillup(struct map_session_data *sd)
  7538. {
  7539. int i;
  7540. nullpo_ret(sd);
  7541. for (i = 0; i < MAX_SKILL; i++) {
  7542. 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) {
  7543. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  7544. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  7545. if (sd->status.skill[i].lv == 0)
  7546. sd->status.skill[i].id = 0;
  7547. }
  7548. }
  7549. if (!pc_grant_allskills(sd, true)) {
  7550. uint16 sk_id;
  7551. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(sd->status.class_);
  7552. if (tree != nullptr && !tree->skills.empty()) {
  7553. for (const auto &skillsit : tree->skills) {
  7554. sk_id = skillsit.first;
  7555. uint16 sk_idx = skill_get_index(sk_id);
  7556. if (sk_idx == 0)
  7557. continue;
  7558. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  7559. if (
  7560. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  7561. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  7562. sk_id == SG_DEVIL
  7563. )
  7564. continue; //Cannot be learned normally.
  7565. sd->status.skill[sk_idx].id = sk_id;
  7566. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  7567. }
  7568. }
  7569. }
  7570. status_calc_pc(sd,SCO_NONE);
  7571. //Required because if you could level up all skills previously,
  7572. //the update will not be sent as only the lv variable changes.
  7573. clif_skillinfoblock(sd);
  7574. return 0;
  7575. }
  7576. /*==========================================
  7577. * /resetlvl
  7578. *------------------------------------------*/
  7579. int pc_resetlvl(struct map_session_data* sd,int type)
  7580. {
  7581. int i;
  7582. nullpo_ret(sd);
  7583. if (type != 3) //Also reset skills
  7584. pc_resetskill(sd, 0);
  7585. if(type == 1){
  7586. sd->status.skill_point=0;
  7587. sd->status.trait_point = 0;
  7588. sd->status.base_level=1;
  7589. sd->status.job_level=1;
  7590. sd->status.base_exp=0;
  7591. sd->status.job_exp=0;
  7592. if(sd->sc.option !=0)
  7593. sd->sc.option = 0;
  7594. sd->status.str=1;
  7595. sd->status.agi=1;
  7596. sd->status.vit=1;
  7597. sd->status.int_=1;
  7598. sd->status.dex=1;
  7599. sd->status.luk=1;
  7600. sd->status.pow=0;
  7601. sd->status.sta=0;
  7602. sd->status.wis=0;
  7603. sd->status.spl=0;
  7604. sd->status.con=0;
  7605. sd->status.crt=0;
  7606. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  7607. sd->status.status_point=100; // not 88 [celest]
  7608. // give platinum skills upon changing
  7609. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  7610. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  7611. }
  7612. }
  7613. if(type == 2){
  7614. sd->status.skill_point=0;
  7615. sd->status.base_level=1;
  7616. sd->status.job_level=1;
  7617. sd->status.base_exp=0;
  7618. sd->status.job_exp=0;
  7619. }
  7620. if(type == 3){
  7621. sd->status.base_level=1;
  7622. sd->status.base_exp=0;
  7623. }
  7624. if(type == 4){
  7625. sd->status.job_level=1;
  7626. sd->status.job_exp=0;
  7627. }
  7628. clif_updatestatus(sd,SP_STATUSPOINT);
  7629. clif_updatestatus(sd,SP_TRAITPOINT);
  7630. clif_updatestatus(sd,SP_STR);
  7631. clif_updatestatus(sd,SP_AGI);
  7632. clif_updatestatus(sd,SP_VIT);
  7633. clif_updatestatus(sd,SP_INT);
  7634. clif_updatestatus(sd,SP_DEX);
  7635. clif_updatestatus(sd,SP_LUK);
  7636. clif_updatestatus(sd,SP_POW);
  7637. clif_updatestatus(sd,SP_STA);
  7638. clif_updatestatus(sd,SP_WIS);
  7639. clif_updatestatus(sd,SP_SPL);
  7640. clif_updatestatus(sd,SP_CON);
  7641. clif_updatestatus(sd,SP_CRT);
  7642. clif_updatestatus(sd,SP_BASELEVEL);
  7643. clif_updatestatus(sd,SP_JOBLEVEL);
  7644. clif_updatestatus(sd,SP_STATUSPOINT);
  7645. clif_updatestatus(sd,SP_BASEEXP);
  7646. clif_updatestatus(sd,SP_JOBEXP);
  7647. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7648. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7649. clif_updatestatus(sd,SP_SKILLPOINT);
  7650. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7651. clif_updatestatus(sd,SP_UAGI);
  7652. clif_updatestatus(sd,SP_UVIT);
  7653. clif_updatestatus(sd,SP_UINT);
  7654. clif_updatestatus(sd,SP_UDEX);
  7655. clif_updatestatus(sd,SP_ULUK); // End Addition
  7656. clif_updatestatus(sd,SP_UPOW);
  7657. clif_updatestatus(sd,SP_USTA);
  7658. clif_updatestatus(sd,SP_UWIS);
  7659. clif_updatestatus(sd,SP_USPL);
  7660. clif_updatestatus(sd,SP_UCON);
  7661. clif_updatestatus(sd,SP_UCRT);
  7662. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  7663. if(sd->equip_index[i] >= 0)
  7664. if(pc_isequip(sd,sd->equip_index[i]))
  7665. pc_unequipitem(sd,sd->equip_index[i],2);
  7666. }
  7667. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  7668. party_send_levelup(sd);
  7669. status_calc_pc(sd, SCO_FORCE);
  7670. clif_skillinfoblock(sd);
  7671. return 0;
  7672. }
  7673. /*==========================================
  7674. * /resetstate
  7675. *------------------------------------------*/
  7676. int pc_resetstate(struct map_session_data* sd)
  7677. {
  7678. nullpo_ret(sd);
  7679. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  7680. 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",
  7681. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  7682. sd->status.base_level = pc_maxbaselv( sd );
  7683. clif_updatestatus( sd, SP_BASELEVEL );
  7684. }
  7685. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  7686. sd->status.trait_point = statpoint_db.get_trait_table_point(sd->status.base_level);
  7687. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  7688. sd->status.status_point += battle_config.transcendent_status_points;
  7689. }
  7690. if ((sd->class_&JOBL_FOURTH) != 0) {
  7691. sd->status.trait_point += battle_config.trait_points_job_change;
  7692. }
  7693. pc_setstat(sd, SP_STR, 1);
  7694. pc_setstat(sd, SP_AGI, 1);
  7695. pc_setstat(sd, SP_VIT, 1);
  7696. pc_setstat(sd, SP_INT, 1);
  7697. pc_setstat(sd, SP_DEX, 1);
  7698. pc_setstat(sd, SP_LUK, 1);
  7699. pc_setstat(sd, SP_POW, 0);
  7700. pc_setstat(sd, SP_STA, 0);
  7701. pc_setstat(sd, SP_WIS, 0);
  7702. pc_setstat(sd, SP_SPL, 0);
  7703. pc_setstat(sd, SP_CON, 0);
  7704. pc_setstat(sd, SP_CRT, 0);
  7705. clif_updatestatus(sd,SP_STR);
  7706. clif_updatestatus(sd,SP_AGI);
  7707. clif_updatestatus(sd,SP_VIT);
  7708. clif_updatestatus(sd,SP_INT);
  7709. clif_updatestatus(sd,SP_DEX);
  7710. clif_updatestatus(sd,SP_LUK);
  7711. clif_updatestatus(sd,SP_POW);
  7712. clif_updatestatus(sd,SP_STA);
  7713. clif_updatestatus(sd,SP_WIS);
  7714. clif_updatestatus(sd,SP_SPL);
  7715. clif_updatestatus(sd,SP_CON);
  7716. clif_updatestatus(sd,SP_CRT);
  7717. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7718. clif_updatestatus(sd,SP_UAGI);
  7719. clif_updatestatus(sd,SP_UVIT);
  7720. clif_updatestatus(sd,SP_UINT);
  7721. clif_updatestatus(sd,SP_UDEX);
  7722. clif_updatestatus(sd,SP_ULUK); // End Addition
  7723. clif_updatestatus(sd,SP_UPOW);
  7724. clif_updatestatus(sd,SP_USTA);
  7725. clif_updatestatus(sd,SP_UWIS);
  7726. clif_updatestatus(sd,SP_USPL);
  7727. clif_updatestatus(sd,SP_UCON);
  7728. clif_updatestatus(sd,SP_UCRT);
  7729. clif_updatestatus(sd,SP_STATUSPOINT);
  7730. clif_updatestatus(sd,SP_TRAITPOINT);
  7731. if( sd->mission_mobid ) { //bugreport:2200
  7732. sd->mission_mobid = 0;
  7733. sd->mission_count = 0;
  7734. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  7735. }
  7736. status_calc_pc(sd, SCO_NONE);
  7737. return 1;
  7738. }
  7739. /*==========================================
  7740. * /resetskill
  7741. * if flag&1, perform block resync and status_calc call.
  7742. * if flag&2, just count total amount of skill points used by player, do not really reset.
  7743. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  7744. *------------------------------------------*/
  7745. int pc_resetskill(struct map_session_data* sd, int flag)
  7746. {
  7747. int i, skill_point=0;
  7748. nullpo_ret(sd);
  7749. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  7750. return 0;
  7751. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  7752. /**
  7753. * 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)
  7754. **/
  7755. if( pc_is_taekwon_ranker(sd) )
  7756. return 0;
  7757. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7758. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  7759. i = sd->sc.option;
  7760. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  7761. i &= ~OPTION_RIDING;
  7762. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  7763. i &= ~OPTION_FALCON;
  7764. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  7765. i &= ~OPTION_DRAGON;
  7766. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  7767. i &= ~OPTION_WUG;
  7768. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  7769. i &= ~OPTION_WUGRIDER;
  7770. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  7771. i &= ~OPTION_MADOGEAR;
  7772. #ifndef NEW_CARTS
  7773. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  7774. i &= ~OPTION_CART;
  7775. #else
  7776. if( sd->sc.data[SC_PUSH_CART] )
  7777. pc_setcart(sd, 0);
  7778. #endif
  7779. if( i != sd->sc.option )
  7780. pc_setoption(sd, i);
  7781. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  7782. hom_vaporize(sd, HOM_ST_ACTIVE);
  7783. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  7784. status_change_end(&sd->bl, SC_SPRITEMABLE);
  7785. if (sd->sc.data[SC_SOULATTACK] && pc_checkskill(sd, SU_SOULATTACK))
  7786. status_change_end(&sd->bl, SC_SOULATTACK);
  7787. }
  7788. for (const auto &skill : skill_db) {
  7789. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  7790. uint8 lv = sd->status.skill[idx].lv;
  7791. if (lv == 0 || skill_id == 0)
  7792. continue;
  7793. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  7794. continue;
  7795. // Don't reset trick dead if not a novice/baby
  7796. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7797. {
  7798. sd->status.skill[idx].lv = 0;
  7799. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7800. continue;
  7801. }
  7802. // do not reset basic skill
  7803. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7804. continue;
  7805. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  7806. continue;
  7807. if( flag&4 && !skill_ischangesex(skill_id) )
  7808. continue;
  7809. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  7810. { //Only handle quest skills in a special way when you can't learn them manually
  7811. if( battle_config.quest_skill_reset && !(flag&2) )
  7812. { //Wipe them
  7813. sd->status.skill[idx].lv = 0;
  7814. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7815. }
  7816. continue;
  7817. }
  7818. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  7819. skill_point += lv;
  7820. else
  7821. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  7822. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  7823. if( !(flag&2) )
  7824. {// reset
  7825. sd->status.skill[idx].lv = 0;
  7826. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7827. }
  7828. }
  7829. if( flag&2 || !skill_point ) return skill_point;
  7830. sd->status.skill_point += skill_point;
  7831. if (flag&1) {
  7832. clif_updatestatus(sd,SP_SKILLPOINT);
  7833. clif_skillinfoblock(sd);
  7834. status_calc_pc(sd, SCO_FORCE);
  7835. }
  7836. return skill_point;
  7837. }
  7838. /*==========================================
  7839. * /resetfeel [Komurka]
  7840. *------------------------------------------*/
  7841. int pc_resetfeel(struct map_session_data* sd)
  7842. {
  7843. int i;
  7844. nullpo_ret(sd);
  7845. for (i=0; i<MAX_PC_FEELHATE; i++)
  7846. {
  7847. sd->feel_map[i].m = -1;
  7848. sd->feel_map[i].index = 0;
  7849. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  7850. }
  7851. return 0;
  7852. }
  7853. int pc_resethate(struct map_session_data* sd)
  7854. {
  7855. int i;
  7856. nullpo_ret(sd);
  7857. for (i=0; i<MAX_PC_FEELHATE; i++)
  7858. {
  7859. sd->hate_mob[i] = -1;
  7860. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  7861. }
  7862. return 0;
  7863. }
  7864. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  7865. {
  7866. int bonus = 0;
  7867. nullpo_ret(sd);
  7868. skill_id = skill_dummy2skill_id(skill_id);
  7869. for (auto &it : sd->skillatk) {
  7870. if (it.id == skill_id) {
  7871. bonus += it.val;
  7872. break;
  7873. }
  7874. }
  7875. return bonus;
  7876. }
  7877. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  7878. {
  7879. int bonus = 0;
  7880. nullpo_ret(sd);
  7881. skill_id = skill_dummy2skill_id(skill_id);
  7882. for (auto &it : sd->subskill) {
  7883. if (it.id == skill_id) {
  7884. bonus += it.val;
  7885. break;
  7886. }
  7887. }
  7888. return bonus;
  7889. }
  7890. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  7891. int bonus = sd->bonus.add_heal_rate;
  7892. nullpo_ret(sd);
  7893. skill_id = skill_dummy2skill_id(skill_id);
  7894. if( bonus ) {
  7895. switch( skill_id ) {
  7896. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  7897. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  7898. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  7899. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  7900. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  7901. case AB_CHEAL: if (!(battle_config.skill_add_heal_rate & 32)) bonus = 0; break;
  7902. case AB_HIGHNESSHEAL: if (!(battle_config.skill_add_heal_rate & 64)) bonus = 0; break;
  7903. case CD_MEDIALE_VOTUM: if (!(battle_config.skill_add_heal_rate & 128)) bonus = 0; break;
  7904. case CD_DILECTIO_HEAL: if (!(battle_config.skill_add_heal_rate & 256)) bonus = 0; break;
  7905. }
  7906. }
  7907. for (auto &it : sd->skillheal) {
  7908. if (it.id == skill_id) {
  7909. bonus += it.val;
  7910. break;
  7911. }
  7912. }
  7913. return bonus;
  7914. }
  7915. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  7916. int bonus = sd->bonus.add_heal2_rate;
  7917. skill_id = skill_dummy2skill_id(skill_id);
  7918. for (auto &it : sd->skillheal2) {
  7919. if (it.id == skill_id) {
  7920. bonus += it.val;
  7921. break;
  7922. }
  7923. }
  7924. return bonus;
  7925. }
  7926. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  7927. {
  7928. if( !pc_isdead(sd) )
  7929. return; // not applicable
  7930. if( sd->bg_id && bg_member_respawn(sd) )
  7931. return; // member revived by battleground
  7932. pc_setstand(sd, true);
  7933. pc_setrestartvalue(sd,3);
  7934. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  7935. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  7936. }
  7937. static TIMER_FUNC(pc_respawn_timer){
  7938. struct map_session_data *sd = map_id2sd(id);
  7939. if( sd != NULL )
  7940. {
  7941. sd->pvp_point=0;
  7942. sd->respawn_tid = INVALID_TIMER;
  7943. pc_respawn(sd,CLR_OUTSIGHT);
  7944. }
  7945. return 0;
  7946. }
  7947. /*==========================================
  7948. * Invoked when a player has received damage
  7949. *------------------------------------------*/
  7950. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp, unsigned int ap)
  7951. {
  7952. if (ap) clif_updatestatus(sd,SP_AP);
  7953. if (sp) clif_updatestatus(sd,SP_SP);
  7954. if (hp) clif_updatestatus(sd,SP_HP);
  7955. else return;
  7956. if (!src)
  7957. return;
  7958. if( pc_issit(sd) ) {
  7959. pc_setstand(sd, true);
  7960. skill_sit(sd,0);
  7961. }
  7962. if (sd->progressbar.npc_id)
  7963. clif_progressbar_abort(sd);
  7964. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  7965. pet_target_check(sd->pd,src,1);
  7966. if( sd->status.ele_id > 0 )
  7967. elemental_set_target(sd,src);
  7968. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  7969. sd->canlog_tick = gettick();
  7970. }
  7971. TIMER_FUNC(pc_close_npc_timer){
  7972. TBL_PC *sd = map_id2sd(id);
  7973. if(sd) pc_close_npc(sd,data);
  7974. return 0;
  7975. }
  7976. /**
  7977. * Method to properly close a NPC for player and clear anything related.
  7978. * @param sd: Player attached
  7979. * @param flag: Method of closure
  7980. * 1: Produce a close button and end the NPC
  7981. * 2: End the NPC (best for no dialog windows)
  7982. */
  7983. void pc_close_npc(struct map_session_data *sd,int flag)
  7984. {
  7985. nullpo_retv(sd);
  7986. if (sd->npc_id || sd->npc_shopid) {
  7987. if (sd->state.using_fake_npc) {
  7988. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  7989. sd->state.using_fake_npc = 0;
  7990. }
  7991. if (sd->st) {
  7992. if(sd->st->state == RUN){ //wait ending code execution
  7993. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  7994. return;
  7995. }
  7996. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  7997. sd->st->mes_active = 0;
  7998. }
  7999. sd->state.menu_or_input = 0;
  8000. sd->npc_menu = 0;
  8001. sd->npc_shopid = 0;
  8002. #ifdef SECURE_NPCTIMEOUT
  8003. if( sd->npc_idle_timer != INVALID_TIMER ){
  8004. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  8005. sd->npc_idle_timer = INVALID_TIMER;
  8006. }
  8007. #endif
  8008. if (sd->st) {
  8009. if (sd->st->state == CLOSE) {
  8010. clif_scriptclose(sd, sd->npc_id);
  8011. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  8012. sd->st->state = END; // Force to end now
  8013. }
  8014. if (sd->st->state == END) { // free attached scripts that are waiting
  8015. script_free_state(sd->st);
  8016. sd->st = NULL;
  8017. sd->npc_id = 0;
  8018. }
  8019. }
  8020. }
  8021. }
  8022. /*==========================================
  8023. * Invoked when a player has negative current hp
  8024. *------------------------------------------*/
  8025. int pc_dead(struct map_session_data *sd,struct block_list *src)
  8026. {
  8027. int i=0,k=0;
  8028. t_tick tick = gettick();
  8029. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8030. // Activate Steel body if a super novice dies at 99+% exp [celest]
  8031. // Super Novices have no kill or die functions attached when saved by their angel
  8032. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  8033. t_exp exp = pc_nextbaseexp(sd);
  8034. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  8035. sd->state.snovice_dead_flag = 1;
  8036. pc_setrestartvalue(sd,1);
  8037. status_percent_heal(&sd->bl, 100, 100);
  8038. clif_resurrection(&sd->bl, 1);
  8039. if(battle_config.pc_invincible_time)
  8040. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8041. sc_start(&sd->bl,&sd->bl,SC_STEELBODY,100,5,skill_get_time(MO_STEELBODY,5));
  8042. if(mapdata_flag_gvg2(mapdata))
  8043. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  8044. return 0;
  8045. }
  8046. }
  8047. for(k = 0; k < MAX_DEVOTION; k++) {
  8048. if (sd->devotion[k]){
  8049. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  8050. if (devsd)
  8051. status_change_end(&devsd->bl, SC_DEVOTION);
  8052. sd->devotion[k] = 0;
  8053. }
  8054. }
  8055. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  8056. if (sd->stellar_mark[k]) {
  8057. struct map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  8058. if (smarksd)
  8059. status_change_end(&smarksd->bl, SC_FLASHKICK);
  8060. sd->stellar_mark[k] = 0;
  8061. }
  8062. }
  8063. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  8064. if (sd->united_soul[k]) {
  8065. struct map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  8066. if (usoulsd)
  8067. status_change_end(&usoulsd->bl, SC_SOULUNITY);
  8068. sd->united_soul[k] = 0;
  8069. }
  8070. }
  8071. for (k = 0; k < MAX_SERVANT_SIGN; k++) {
  8072. if (sd->servant_sign[k]) {
  8073. struct map_session_data *ssignsd = map_id2sd(sd->servant_sign[k]);
  8074. if (ssignsd)
  8075. status_change_end(&ssignsd->bl, SC_SERVANT_SIGN);
  8076. sd->servant_sign[k] = 0;
  8077. }
  8078. }
  8079. if(sd->shadowform_id) { //if we were target of shadowform
  8080. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM);
  8081. sd->shadowform_id = 0; //should be remove on status end anyway
  8082. }
  8083. if(sd->status.pet_id > 0 && sd->pd) {
  8084. struct pet_data *pd = sd->pd;
  8085. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  8086. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  8087. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  8088. pet_set_intimate(pd, PET_INTIMATE_NONE);
  8089. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  8090. }
  8091. if( sd->pd->target_id ) // Unlock all targets...
  8092. pet_unlocktarget(sd->pd);
  8093. }
  8094. 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)
  8095. hom_vaporize(sd, HOM_ST_ACTIVE);
  8096. if( sd->md )
  8097. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  8098. if( sd->ed )
  8099. elemental_delete(sd->ed);
  8100. // Leave duel if you die [LuzZza]
  8101. if(battle_config.duel_autoleave_when_die) {
  8102. if(sd->duel_group > 0)
  8103. duel_leave(sd->duel_group, sd);
  8104. if(sd->duel_invite > 0)
  8105. duel_reject(sd->duel_invite, sd);
  8106. }
  8107. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  8108. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  8109. sd->skill_keep_using.tid = INVALID_TIMER;
  8110. }
  8111. pc_close_npc(sd,2); //close npc if we were using one
  8112. /* e.g. not killed thru pc_damage */
  8113. if( pc_issit(sd) ) {
  8114. clif_status_load(&sd->bl,EFST_SIT,0);
  8115. }
  8116. pc_setdead(sd);
  8117. clif_party_dead( *sd );
  8118. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  8119. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  8120. if (battle_config.loose_ap_on_death == 1)
  8121. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  8122. //Reset menu skills/item skills
  8123. if ((sd->skillitem) != 0)
  8124. sd->skillitem = sd->skillitemlv = 0;
  8125. if ((sd->menuskill_id) != 0)
  8126. sd->menuskill_id = sd->menuskill_val = 0;
  8127. //Reset ticks.
  8128. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  8129. if ( sd->spiritball !=0 )
  8130. pc_delspiritball(sd,sd->spiritball,0);
  8131. if (sd->soulball != 0)
  8132. pc_delsoulball(sd, sd->soulball, false);
  8133. if (sd->servantball != 0)
  8134. pc_delservantball( *sd, sd->servantball );
  8135. if (sd->abyssball != 0)
  8136. pc_delabyssball( *sd, sd->abyssball );
  8137. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  8138. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  8139. if (src)
  8140. switch (src->type) {
  8141. case BL_MOB:
  8142. {
  8143. struct mob_data *md=(struct mob_data *)src;
  8144. if(md->target_id==sd->bl.id)
  8145. mob_unlocktarget(md,tick);
  8146. if(battle_config.mobs_level_up && md->status.hp &&
  8147. (unsigned int)md->level < pc_maxbaselv(sd) &&
  8148. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  8149. ) { // monster level up [Valaris]
  8150. clif_misceffect(&md->bl,0);
  8151. md->level++;
  8152. status_calc_mob(md, SCO_NONE);
  8153. status_percent_heal(src,10,0);
  8154. if( battle_config.show_mob_info&4 )
  8155. {// update name with new level
  8156. clif_name_area(&md->bl);
  8157. }
  8158. }
  8159. src = battle_get_master(src); // Maybe Player Summon
  8160. }
  8161. break;
  8162. case BL_PET: //Pass on to master...
  8163. case BL_HOM:
  8164. case BL_MER:
  8165. src = battle_get_master(src);
  8166. break;
  8167. }
  8168. if (src && src->type == BL_PC) {
  8169. struct map_session_data *ssd = (struct map_session_data *)src;
  8170. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  8171. npc_script_event(ssd, NPCE_KILLPC);
  8172. if (battle_config.pk_mode&2) {
  8173. ssd->status.manner -= 5;
  8174. if(ssd->status.manner < 0)
  8175. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  8176. #if 0
  8177. // PK/Karma system code (not enabled yet) [celest]
  8178. // originally from Kade Online, so i don't know if any of these is correct ^^;
  8179. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  8180. // karma going down = more 'good' / more honourable.
  8181. // The Karma System way...
  8182. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  8183. sd->status.karma--;
  8184. ssd->status.karma--;
  8185. }
  8186. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  8187. ssd->status.karma++;
  8188. // or the PK System way...
  8189. if (sd->status.karma > 0) // player killed is dishonourable?
  8190. ssd->status.karma--; // honour points earned
  8191. sd->status.karma++; // honour points lost
  8192. // To-do: Receive exp on certain occasions
  8193. #endif
  8194. }
  8195. }
  8196. if(battle_config.bone_drop==2
  8197. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  8198. {
  8199. struct item item_tmp;
  8200. memset(&item_tmp,0,sizeof(item_tmp));
  8201. item_tmp.nameid=ITEMID_SKULL_;
  8202. item_tmp.identify=1;
  8203. item_tmp.card[0]=CARD0_CREATE;
  8204. item_tmp.card[1]=0;
  8205. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  8206. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  8207. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  8208. }
  8209. //Remove bonus_script when dead
  8210. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  8211. // changed penalty options, added death by player if pk_mode [Valaris]
  8212. if(battle_config.death_penalty_type
  8213. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  8214. && !sd->sc.cant.deathpenalty
  8215. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  8216. {
  8217. t_exp base_penalty = 0;
  8218. t_exp job_penalty = 0;
  8219. uint32 zeny_penalty = 0;
  8220. if (pc_isvip(sd)) { // EXP penalty for VIP
  8221. base_penalty = battle_config.vip_exp_penalty_base;
  8222. job_penalty = battle_config.vip_exp_penalty_job;
  8223. zeny_penalty = battle_config.vip_zeny_penalty;
  8224. } else {
  8225. base_penalty = battle_config.death_penalty_base;
  8226. job_penalty = battle_config.death_penalty_job;
  8227. zeny_penalty = battle_config.zeny_penalty;
  8228. }
  8229. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  8230. switch (battle_config.death_penalty_type) {
  8231. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  8232. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  8233. }
  8234. if (base_penalty){ //recheck after altering to speedup
  8235. if (battle_config.pk_mode && src && src->type==BL_PC)
  8236. base_penalty *= 2;
  8237. base_penalty = u64min(sd->status.base_exp, base_penalty);
  8238. }
  8239. }
  8240. else
  8241. base_penalty = 0;
  8242. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  8243. switch (battle_config.death_penalty_type) {
  8244. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  8245. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  8246. }
  8247. if (job_penalty) {
  8248. if (battle_config.pk_mode && src && src->type==BL_PC)
  8249. job_penalty *= 2;
  8250. job_penalty = u64min(sd->status.job_exp, job_penalty);
  8251. }
  8252. }
  8253. else
  8254. job_penalty = 0;
  8255. if (base_penalty || job_penalty) {
  8256. short insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  8257. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  8258. pc_lostexp(sd, base_penalty, job_penalty);
  8259. }
  8260. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  8261. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  8262. if(zeny_penalty)
  8263. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  8264. }
  8265. }
  8266. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  8267. for (const auto &it : mapdata->drop_list) {
  8268. int id = it.drop_id, per = it.drop_per;
  8269. enum e_nightmare_drop_type type = it.drop_type;
  8270. if(id == 0)
  8271. continue;
  8272. if(id == -1){
  8273. int eq_num=0,eq_n[MAX_INVENTORY];
  8274. memset(eq_n,0,sizeof(eq_n));
  8275. for(i=0;i<MAX_INVENTORY;i++) {
  8276. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8277. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8278. || type&NMDT_ALL)
  8279. {
  8280. int l;
  8281. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  8282. if( l < MAX_INVENTORY )
  8283. eq_n[l] = i;
  8284. eq_num++;
  8285. }
  8286. }
  8287. if(eq_num > 0){
  8288. int n = eq_n[rnd()%eq_num];
  8289. if(rnd()%10000 < per) {
  8290. if(sd->inventory.u.items_inventory[n].equip)
  8291. pc_unequipitem(sd,n,3);
  8292. pc_dropitem(sd,n,1);
  8293. }
  8294. }
  8295. }
  8296. else if(id > 0) {
  8297. for(i=0;i<MAX_INVENTORY;i++){
  8298. if(sd->inventory.u.items_inventory[i].nameid == id
  8299. && rnd()%10000 < per
  8300. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8301. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8302. || type&NMDT_ALL) ){
  8303. if(sd->inventory.u.items_inventory[i].equip)
  8304. pc_unequipitem(sd,i,3);
  8305. pc_dropitem(sd,i,1);
  8306. break;
  8307. }
  8308. }
  8309. }
  8310. }
  8311. }
  8312. // pvp
  8313. // disable certain pvp functions on pk_mode [Valaris]
  8314. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  8315. sd->pvp_point -= 5;
  8316. sd->pvp_lost++;
  8317. if( src && src->type == BL_PC ) {
  8318. struct map_session_data *ssd = (struct map_session_data *)src;
  8319. ssd->pvp_point++;
  8320. ssd->pvp_won++;
  8321. }
  8322. if( sd->pvp_point < 0 ) {
  8323. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  8324. return 1|8;
  8325. }
  8326. }
  8327. //GvG
  8328. if( mapdata_flag_gvg2(mapdata) ) {
  8329. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  8330. return 1|8;
  8331. }
  8332. else if( sd->bg_id ) {
  8333. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  8334. if (bg) {
  8335. if (bg->cemetery.map > 0) { // Respawn by BG
  8336. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  8337. return 1|8;
  8338. }
  8339. }
  8340. }
  8341. //Reset "can log out" tick.
  8342. if( battle_config.prevent_logout )
  8343. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  8344. return 1;
  8345. }
  8346. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp, unsigned int ap) {
  8347. if(hp) clif_updatestatus(sd,SP_HP);
  8348. if(sp) clif_updatestatus(sd,SP_SP);
  8349. if(ap) clif_updatestatus(sd,SP_AP);
  8350. pc_setstand(sd, true);
  8351. if(battle_config.pc_invincible_time > 0)
  8352. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8353. if( sd->state.gmaster_flag ) {
  8354. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  8355. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  8356. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  8357. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  8358. }
  8359. }
  8360. bool pc_revive_item(struct map_session_data *sd) {
  8361. nullpo_retr(false, sd);
  8362. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  8363. return false;
  8364. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  8365. return false;
  8366. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  8367. uint8 hp = 100, sp = 100;
  8368. if (item_position < 0) {
  8369. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  8370. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  8371. sp = 0;
  8372. }
  8373. else
  8374. return false;
  8375. }
  8376. if (!status_revive(&sd->bl, hp, sp))
  8377. return false;
  8378. if (item_position < 0)
  8379. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE);
  8380. else
  8381. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  8382. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  8383. return true;
  8384. }
  8385. // script
  8386. //
  8387. /*==========================================
  8388. * script reading pc status registry
  8389. *------------------------------------------*/
  8390. int64 pc_readparam(struct map_session_data* sd,int64 type)
  8391. {
  8392. int64 val = 0;
  8393. nullpo_ret(sd);
  8394. switch(type) {
  8395. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  8396. case SP_STATUSPOINT: val = sd->status.status_point; break;
  8397. case SP_TRAITPOINT: val = sd->status.trait_point; break;
  8398. case SP_ZENY: val = sd->status.zeny; break;
  8399. case SP_BASELEVEL: val = sd->status.base_level; break;
  8400. case SP_JOBLEVEL: val = sd->status.job_level; break;
  8401. case SP_CLASS: val = sd->status.class_; break;
  8402. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  8403. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  8404. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  8405. case SP_SEX: val = sd->status.sex; break;
  8406. case SP_WEIGHT: val = sd->weight; break;
  8407. case SP_MAXWEIGHT: val = sd->max_weight; break;
  8408. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  8409. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  8410. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  8411. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  8412. case SP_HP: val = sd->battle_status.hp; break;
  8413. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  8414. case SP_SP: val = sd->battle_status.sp; break;
  8415. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  8416. case SP_AP: val = sd->battle_status.ap; break;
  8417. case SP_MAXAP: val = sd->battle_status.max_ap; break;
  8418. case SP_STR: val = sd->status.str; break;
  8419. case SP_AGI: val = sd->status.agi; break;
  8420. case SP_VIT: val = sd->status.vit; break;
  8421. case SP_INT: val = sd->status.int_; break;
  8422. case SP_DEX: val = sd->status.dex; break;
  8423. case SP_LUK: val = sd->status.luk; break;
  8424. case SP_POW: val = sd->status.pow; break;
  8425. case SP_STA: val = sd->status.sta; break;
  8426. case SP_WIS: val = sd->status.wis; break;
  8427. case SP_SPL: val = sd->status.spl; break;
  8428. case SP_CON: val = sd->status.con; break;
  8429. case SP_CRT: val = sd->status.crt; break;
  8430. case SP_KARMA: val = sd->status.karma; break;
  8431. case SP_MANNER: val = sd->status.manner; break;
  8432. case SP_FAME: val = sd->status.fame; break;
  8433. case SP_KILLERRID: val = sd->killerrid; break;
  8434. case SP_KILLEDRID: val = sd->killedrid; break;
  8435. case SP_KILLEDGID: val = sd->killedgid; break;
  8436. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  8437. case SP_CHARMOVE: val = sd->status.character_moves; break;
  8438. case SP_CHARRENAME: val = sd->status.rename; break;
  8439. case SP_CHARFONT: val = sd->status.font; break;
  8440. case SP_BANK_VAULT: val = sd->bank_vault; break;
  8441. case SP_CASHPOINTS: val = sd->cashPoints; break;
  8442. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  8443. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  8444. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  8445. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  8446. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  8447. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  8448. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  8449. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  8450. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  8451. case SP_BASE_ATK:
  8452. #ifdef RENEWAL
  8453. val = sd->bonus.eatk;
  8454. #else
  8455. val = sd->battle_status.batk;
  8456. #endif
  8457. break;
  8458. case SP_DEF1: val = sd->battle_status.def; break;
  8459. case SP_DEF2: val = sd->battle_status.def2; break;
  8460. case SP_MDEF1: val = sd->battle_status.mdef; break;
  8461. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  8462. case SP_HIT: val = sd->battle_status.hit; break;
  8463. case SP_FLEE1: val = sd->battle_status.flee; break;
  8464. case SP_FLEE2: val = sd->battle_status.flee2; break;
  8465. case SP_PATK: val = sd->battle_status.patk; break;
  8466. case SP_SMATK: val = sd->battle_status.smatk; break;
  8467. case SP_RES: val = sd->battle_status.res; break;
  8468. case SP_MRES: val = sd->battle_status.mres; break;
  8469. case SP_HPLUS: val = sd->battle_status.hplus; break;
  8470. case SP_CRATE: val = sd->battle_status.crate; break;
  8471. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  8472. case SP_MAXHPRATE: val = sd->hprate; break;
  8473. case SP_MAXSPRATE: val = sd->sprate; break;
  8474. case SP_MAXAPRATE: val = sd->aprate; break;
  8475. case SP_SPRATE: val = sd->dsprate; break;
  8476. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  8477. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  8478. case SP_ASPD_RATE:
  8479. #ifndef RENEWAL_ASPD
  8480. val = sd->battle_status.aspd_rate;
  8481. #else
  8482. val = sd->battle_status.aspd_rate2;
  8483. #endif
  8484. break;
  8485. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  8486. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  8487. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  8488. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  8489. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  8490. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  8491. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  8492. case SP_MATK_RATE: val = sd->matk_rate; break;
  8493. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  8494. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  8495. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  8496. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  8497. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  8498. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  8499. case SP_HIT_RATE: val = sd->hit_rate; break;
  8500. case SP_FLEE_RATE: val = sd->flee_rate; break;
  8501. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  8502. case SP_DEF_RATE: val = sd->def_rate; break;
  8503. case SP_DEF2_RATE: val = sd->def2_rate; break;
  8504. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  8505. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  8506. case SP_PATK_RATE: val = sd->patk_rate; break;
  8507. case SP_SMATK_RATE: val = sd->smatk_rate; break;
  8508. case SP_RES_RATE: val = sd->res_rate; break;
  8509. case SP_MRES_RATE: val = sd->mres_rate; break;
  8510. case SP_HPLUS_RATE: val = sd->hplus_rate; break;
  8511. case SP_CRATE_RATE: val = sd->crate_rate; break;
  8512. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  8513. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  8514. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  8515. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  8516. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  8517. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  8518. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  8519. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  8520. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  8521. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  8522. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  8523. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  8524. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  8525. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  8526. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  8527. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  8528. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  8529. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  8530. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  8531. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  8532. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  8533. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  8534. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  8535. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  8536. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  8537. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  8538. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  8539. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  8540. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  8541. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  8542. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  8543. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  8544. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  8545. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  8546. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  8547. case SP_UNSTRIPABLE:
  8548. case SP_UNSTRIPABLE_ARMOR:
  8549. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  8550. break;
  8551. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  8552. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  8553. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  8554. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  8555. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  8556. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  8557. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  8558. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  8559. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  8560. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  8561. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  8562. case SP_EMATK: val = sd->bonus.ematk; break;
  8563. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  8564. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  8565. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  8566. case SP_CASTRATE:
  8567. case SP_VARCASTRATE:
  8568. #ifdef RENEWAL_CAST
  8569. val = sd->bonus.varcastrate; break;
  8570. #else
  8571. val = sd->castrate; break;
  8572. #endif
  8573. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  8574. case SP_ADD_ITEM_SPHEAL_RATE: val = sd->bonus.itemsphealrate2; break;
  8575. default:
  8576. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  8577. return -1;
  8578. }
  8579. return val;
  8580. }
  8581. /*==========================================
  8582. * script set pc status registry
  8583. *------------------------------------------*/
  8584. bool pc_setparam(struct map_session_data *sd,int64 type,int64 val_tmp)
  8585. {
  8586. nullpo_retr(false,sd);
  8587. int val = static_cast<unsigned int>(val_tmp);
  8588. switch(type){
  8589. case SP_BASELEVEL:
  8590. if (val > pc_maxbaselv(sd)) //Capping to max
  8591. val = pc_maxbaselv(sd);
  8592. if (val > sd->status.base_level) {
  8593. for( int i = 0; i < (int)( val - sd->status.base_level ); i++ ){
  8594. sd->status.status_point += statpoint_db.pc_gets_status_point( sd->status.base_level + i );
  8595. sd->status.trait_point += statpoint_db.pc_gets_trait_point( sd->status.base_level + i );
  8596. }
  8597. }
  8598. sd->status.base_level = val;
  8599. sd->status.base_exp = 0;
  8600. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  8601. clif_updatestatus(sd, SP_NEXTBASEEXP);
  8602. clif_updatestatus(sd, SP_STATUSPOINT);
  8603. clif_updatestatus(sd, SP_TRAITPOINT);
  8604. clif_updatestatus(sd, SP_BASEEXP);
  8605. status_calc_pc(sd, SCO_FORCE);
  8606. if(sd->status.party_id)
  8607. party_send_levelup(sd);
  8608. break;
  8609. case SP_JOBLEVEL:
  8610. if (val >= sd->status.job_level) {
  8611. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  8612. sd->status.skill_point += val - sd->status.job_level;
  8613. clif_updatestatus(sd, SP_SKILLPOINT);
  8614. }
  8615. sd->status.job_level = val;
  8616. sd->status.job_exp = 0;
  8617. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  8618. clif_updatestatus(sd, SP_NEXTJOBEXP);
  8619. clif_updatestatus(sd, SP_JOBEXP);
  8620. status_calc_pc(sd, SCO_FORCE);
  8621. break;
  8622. case SP_SKILLPOINT:
  8623. sd->status.skill_point = val;
  8624. break;
  8625. case SP_STATUSPOINT:
  8626. sd->status.status_point = val;
  8627. break;
  8628. case SP_TRAITPOINT:
  8629. sd->status.trait_point = val;
  8630. break;
  8631. case SP_ZENY:
  8632. if( val < 0 )
  8633. return false;// can't set negative zeny
  8634. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  8635. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  8636. break;
  8637. case SP_BASEEXP:
  8638. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  8639. if (val_tmp < sd->status.base_exp) // Lost
  8640. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  8641. else // Gained
  8642. pc_gainexp(sd, NULL, val_tmp - sd->status.base_exp, 0, 2);
  8643. return true;
  8644. case SP_JOBEXP:
  8645. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  8646. if (val_tmp < sd->status.job_exp) // Lost
  8647. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  8648. else // Gained
  8649. pc_gainexp(sd, NULL, 0, val_tmp - sd->status.job_exp, 2);
  8650. return true;
  8651. case SP_SEX:
  8652. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  8653. break;
  8654. case SP_WEIGHT:
  8655. sd->weight = val;
  8656. break;
  8657. case SP_MAXWEIGHT:
  8658. sd->max_weight = val;
  8659. break;
  8660. case SP_HP:
  8661. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  8662. break;
  8663. case SP_MAXHP:
  8664. if (sd->status.base_level < 100)
  8665. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  8666. else if (sd->status.base_level < 151)
  8667. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  8668. else
  8669. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  8670. if( sd->battle_status.max_hp < sd->battle_status.hp )
  8671. {
  8672. sd->battle_status.hp = sd->battle_status.max_hp;
  8673. clif_updatestatus(sd, SP_HP);
  8674. }
  8675. break;
  8676. case SP_SP:
  8677. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  8678. break;
  8679. case SP_MAXSP:
  8680. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  8681. if( sd->battle_status.max_sp < sd->battle_status.sp )
  8682. {
  8683. sd->battle_status.sp = sd->battle_status.max_sp;
  8684. clif_updatestatus(sd, SP_SP);
  8685. }
  8686. break;
  8687. case SP_AP:
  8688. sd->battle_status.ap = cap_value(val, 0, (int)sd->battle_status.max_ap);
  8689. break;
  8690. case SP_MAXAP:
  8691. sd->battle_status.max_ap = cap_value(val, 1, battle_config.max_ap);
  8692. if (sd->battle_status.max_ap < sd->battle_status.ap) {
  8693. sd->battle_status.ap = sd->battle_status.max_ap;
  8694. clif_updatestatus(sd, SP_AP);
  8695. }
  8696. break;
  8697. case SP_STR:
  8698. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  8699. break;
  8700. case SP_AGI:
  8701. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  8702. break;
  8703. case SP_VIT:
  8704. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  8705. break;
  8706. case SP_INT:
  8707. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  8708. break;
  8709. case SP_DEX:
  8710. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  8711. break;
  8712. case SP_LUK:
  8713. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  8714. break;
  8715. case SP_POW:
  8716. sd->status.pow = cap_value(val, 0, pc_maxparameter(sd,PARAM_POW));
  8717. break;
  8718. case SP_STA:
  8719. sd->status.sta = cap_value(val, 0, pc_maxparameter(sd,PARAM_STA));
  8720. break;
  8721. case SP_WIS:
  8722. sd->status.wis = cap_value(val, 0, pc_maxparameter(sd,PARAM_WIS));
  8723. break;
  8724. case SP_SPL:
  8725. sd->status.spl = cap_value(val, 0, pc_maxparameter(sd,PARAM_SPL));
  8726. break;
  8727. case SP_CON:
  8728. sd->status.con = cap_value(val, 0, pc_maxparameter(sd,PARAM_CON));
  8729. break;
  8730. case SP_CRT:
  8731. sd->status.crt = cap_value(val, 0, pc_maxparameter(sd,PARAM_CRT));
  8732. break;
  8733. case SP_KARMA:
  8734. sd->status.karma = val;
  8735. break;
  8736. case SP_MANNER:
  8737. sd->status.manner = val;
  8738. if( val < 0 )
  8739. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  8740. else {
  8741. status_change_end(&sd->bl, SC_NOCHAT);
  8742. clif_manner_message(sd, 5);
  8743. }
  8744. return true; // status_change_start/status_change_end already sends packets warning the client
  8745. case SP_FAME:
  8746. sd->status.fame = val;
  8747. break;
  8748. case SP_KILLERRID:
  8749. sd->killerrid = val;
  8750. return true;
  8751. case SP_KILLEDRID:
  8752. sd->killedrid = val;
  8753. return true;
  8754. case SP_KILLEDGID:
  8755. sd->killedgid = val;
  8756. return true;
  8757. case SP_CHARMOVE:
  8758. sd->status.character_moves = val;
  8759. return true;
  8760. case SP_CHARRENAME:
  8761. sd->status.rename = val;
  8762. return true;
  8763. case SP_CHARFONT:
  8764. sd->status.font = val;
  8765. clif_font(sd);
  8766. return true;
  8767. case SP_BANK_VAULT:
  8768. if (val < 0)
  8769. return false;
  8770. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  8771. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  8772. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  8773. return true;
  8774. case SP_ROULETTE_BRONZE:
  8775. sd->roulette_point.bronze = val;
  8776. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  8777. return true;
  8778. case SP_ROULETTE_SILVER:
  8779. sd->roulette_point.silver = val;
  8780. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  8781. return true;
  8782. case SP_ROULETTE_GOLD:
  8783. sd->roulette_point.gold = val;
  8784. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  8785. return true;
  8786. case SP_CASHPOINTS:
  8787. if (val < 0)
  8788. return false;
  8789. if (!sd->state.connect_new)
  8790. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  8791. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  8792. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  8793. return true;
  8794. case SP_KAFRAPOINTS:
  8795. if (val < 0)
  8796. return false;
  8797. if (!sd->state.connect_new)
  8798. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  8799. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  8800. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  8801. return true;
  8802. case SP_PCDIECOUNTER:
  8803. if (val < 0)
  8804. return false;
  8805. if (sd->die_counter == val)
  8806. return true;
  8807. sd->die_counter = val;
  8808. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  8809. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  8810. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  8811. return true;
  8812. case SP_COOKMASTERY:
  8813. if (sd->cook_mastery == val)
  8814. return true;
  8815. val = cap_value(val, 0, 1999);
  8816. sd->cook_mastery = val;
  8817. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  8818. return true;
  8819. default:
  8820. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  8821. return false;
  8822. }
  8823. clif_updatestatus(sd,static_cast<int>(type));
  8824. return true;
  8825. }
  8826. /*==========================================
  8827. * HP/SP/AP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  8828. *------------------------------------------*/
  8829. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, unsigned int ap, int type)
  8830. {// Is there going to be a effect for gaining AP soon??? [Rytech]
  8831. nullpo_retv(sd);
  8832. if (type&2) {
  8833. if (hp || type&4) {
  8834. clif_heal(sd->fd,SP_HP,hp);
  8835. clif_update_hp(*sd);
  8836. }
  8837. if (sp)
  8838. clif_heal(sd->fd,SP_SP,sp);
  8839. if (ap)
  8840. clif_heal(sd->fd,SP_AP,ap);
  8841. } else {
  8842. if(hp)
  8843. clif_updatestatus(sd,SP_HP);
  8844. if(sp)
  8845. clif_updatestatus(sd,SP_SP);
  8846. if (ap)
  8847. clif_updatestatus(sd,SP_AP);
  8848. }
  8849. return;
  8850. }
  8851. /**
  8852. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  8853. * @param sd: Player data
  8854. * @param itemid: Item ID
  8855. * @param hp: HP to heal
  8856. * @param sp: SP to heal
  8857. * @return Amount healed to an object
  8858. */
  8859. int pc_itemheal(struct map_session_data *sd, t_itemid itemid, int hp, int sp)
  8860. {
  8861. int bonus, tmp, penalty = 0;
  8862. if (hp) {
  8863. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  8864. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  8865. if (potion_flag == 2) {
  8866. bonus += bonus * 50 / 100;
  8867. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  8868. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  8869. }
  8870. //All item bonuses.
  8871. bonus += sd->bonus.itemhealrate2;
  8872. //Item Group bonuses
  8873. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid, sd->itemgrouphealrate) / 100;
  8874. //Individual item bonuses.
  8875. for(const auto &it : sd->itemhealrate) {
  8876. if (it.id == itemid) {
  8877. bonus += bonus * it.val / 100;
  8878. break;
  8879. }
  8880. }
  8881. // Recovery Potion
  8882. if (sd->sc.data[SC_INCHEALRATE])
  8883. bonus += bonus * sd->sc.data[SC_INCHEALRATE]->val1 / 100;
  8884. // 2014 Halloween Event : Pumpkin Bonus
  8885. if (sd->sc.data[SC_MTF_PUMPKIN]) {
  8886. if (itemid == ITEMID_PUMPKIN)
  8887. bonus += bonus * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100;
  8888. else if (itemid == ITEMID_COOKIE_BAT)
  8889. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val2;
  8890. }
  8891. if (sd->sc.data[SC_VITALIZE_POTION])
  8892. bonus += bonus * 10 / 100;
  8893. tmp = hp * bonus / 100; // Overflow check
  8894. if (bonus != 100 && tmp > hp)
  8895. hp = tmp;
  8896. }
  8897. if (sp) {
  8898. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  8899. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  8900. if (potion_flag == 2)
  8901. bonus += bonus * 50 / 100;
  8902. // All item bonuses.
  8903. bonus += sd->bonus.itemsphealrate2;
  8904. // Item Group bonuses
  8905. bonus += bonus * pc_get_itemgroup_bonus( sd, itemid, sd->itemgroupsphealrate ) / 100;
  8906. // Individual item bonuses.
  8907. for( const auto &it : sd->itemsphealrate ){
  8908. if( it.id == itemid ){
  8909. bonus += bonus * it.val / 100;
  8910. break;
  8911. }
  8912. }
  8913. tmp = sp * bonus / 100; // Overflow check
  8914. if (bonus != 100 && tmp > sp)
  8915. sp = tmp;
  8916. }
  8917. if (sd->sc.count) {
  8918. // Critical Wound and Death Hurt stack
  8919. if (sd->sc.data[SC_CRITICALWOUND])
  8920. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  8921. if (sd->sc.data[SC_DEATHHURT] && sd->sc.data[SC_DEATHHURT]->val3 == 1)
  8922. penalty += 20;
  8923. if (sd->sc.data[SC_NORECOVER_STATE])
  8924. penalty = 100;
  8925. if (sd->sc.data[SC_VITALITYACTIVATION])
  8926. hp += hp / 2; // 1.5 times
  8927. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  8928. hp += hp / 10;
  8929. sp += sp / 10;
  8930. }
  8931. #ifdef RENEWAL
  8932. if (sd->sc.data[SC_APPLEIDUN])
  8933. hp += sd->sc.data[SC_APPLEIDUN]->val3 / 100;
  8934. #endif
  8935. if (penalty > 0) {
  8936. hp -= hp * penalty / 100;
  8937. sp -= sp * penalty / 100;
  8938. }
  8939. #ifdef RENEWAL
  8940. if (sd->sc.data[SC_EXTREMITYFIST2])
  8941. sp = 0;
  8942. #endif
  8943. if (sd->sc.data[SC_BITESCAR])
  8944. hp = 0;
  8945. }
  8946. return status_heal(&sd->bl, hp, sp, 1);
  8947. }
  8948. /*==========================================
  8949. * HP/SP Recovery
  8950. * Heal player hp nad/or sp by rate
  8951. *------------------------------------------*/
  8952. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  8953. {
  8954. nullpo_ret(sd);
  8955. if (hp > 100) hp = 100;
  8956. else if (hp <-100) hp = -100;
  8957. if (sp > 100) sp = 100;
  8958. else if (sp <-100) sp = -100;
  8959. if(hp >= 0 && sp >= 0) //Heal
  8960. return status_percent_heal(&sd->bl, hp, sp);
  8961. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  8962. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  8963. //Crossed signs
  8964. if(hp) {
  8965. if(hp > 0)
  8966. status_percent_heal(&sd->bl, hp, 0);
  8967. else
  8968. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  8969. }
  8970. if(sp) {
  8971. if(sp > 0)
  8972. status_percent_heal(&sd->bl, 0, sp);
  8973. else
  8974. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  8975. }
  8976. return 0;
  8977. }
  8978. static int jobchange_killclone(struct block_list *bl, va_list ap)
  8979. {
  8980. struct mob_data *md;
  8981. int flag;
  8982. md = (struct mob_data *)bl;
  8983. nullpo_ret(md);
  8984. flag = va_arg(ap, int);
  8985. if (md->master_id && md->special_state.clone && md->master_id == flag)
  8986. status_kill(&md->bl);
  8987. return 1;
  8988. }
  8989. /**
  8990. * Called when player changes job
  8991. * Rewrote to make it tidider [Celest]
  8992. * @param sd
  8993. * @param job JOB ID. See enum e_job
  8994. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  8995. * @return True if success, false if failed
  8996. **/
  8997. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  8998. {
  8999. int i, fame_flag = 0;
  9000. nullpo_retr(false,sd);
  9001. if (job < 0)
  9002. return false;
  9003. //Normalize job.
  9004. uint64 b_class = pc_jobid2mapid(job);
  9005. if (b_class == -1)
  9006. return false;
  9007. switch (upper) {
  9008. case 1:
  9009. b_class|= JOBL_UPPER;
  9010. break;
  9011. case 2:
  9012. b_class|= JOBL_BABY;
  9013. break;
  9014. }
  9015. //This will automatically adjust bard/dancer classes to the correct gender
  9016. //That is, if you try to jobchange into dancer, it will turn you to bard.
  9017. job = pc_mapid2jobid(b_class, sd->status.sex);
  9018. if (job == -1)
  9019. return false;
  9020. if ((unsigned short)b_class == sd->class_)
  9021. return false; //Nothing to change.
  9022. // If the job does not exist in the job db, dont allow changing to it
  9023. if( !job_db.exists( job ) ){
  9024. return false;
  9025. }
  9026. if( ( b_class&JOBL_FOURTH ) && !( sd->class_&JOBL_FOURTH ) ){
  9027. // Changing to 4th job
  9028. sd->change_level_4th = sd->status.job_level;
  9029. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ) , sd->change_level_4th );
  9030. }else if( ( b_class&JOBL_THIRD ) && !( sd->class_&JOBL_THIRD ) ){
  9031. // changing from 2nd to 3rd job
  9032. sd->change_level_3rd = sd->status.job_level;
  9033. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  9034. }else if( ( b_class&JOBL_2 ) && !( sd->class_&JOBL_2 ) ){
  9035. // changing from 1st to 2nd job
  9036. sd->change_level_2nd = sd->status.job_level;
  9037. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  9038. }
  9039. if(sd->cloneskill_idx > 0) {
  9040. pc_skill_plagiarism_reset(*sd, 1);
  9041. }
  9042. if(sd->reproduceskill_idx > 0) {
  9043. pc_skill_plagiarism_reset(*sd, 2);
  9044. }
  9045. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  9046. status_db.changeSkillTree(sd);
  9047. }
  9048. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  9049. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  9050. pc_resetfeel(sd);
  9051. pc_resethate(sd);
  9052. }
  9053. // Reset body style to 0 before changing job to avoid
  9054. // errors since not every job has a alternate outfit.
  9055. sd->status.body = 0;
  9056. clif_changelook(&sd->bl,LOOK_BODY2,0);
  9057. sd->status.class_ = job;
  9058. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  9059. uint64 previous_class = sd->class_;
  9060. sd->class_ = (unsigned short)b_class;
  9061. sd->status.job_level=1;
  9062. sd->status.job_exp=0;
  9063. if (sd->status.base_level > pc_maxbaselv(sd)) {
  9064. sd->status.base_level = pc_maxbaselv(sd);
  9065. sd->status.base_exp=0;
  9066. pc_resetstate(sd);
  9067. clif_updatestatus(sd,SP_STATUSPOINT);
  9068. clif_updatestatus(sd,SP_TRAITPOINT);
  9069. clif_updatestatus(sd,SP_BASELEVEL);
  9070. clif_updatestatus(sd,SP_BASEEXP);
  9071. clif_updatestatus(sd,SP_NEXTBASEEXP);
  9072. }
  9073. // Give or reduce transcendent status points
  9074. if( (b_class&JOBL_UPPER) && !(previous_class&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  9075. sd->status.status_point += battle_config.transcendent_status_points;
  9076. clif_updatestatus(sd,SP_STATUSPOINT);
  9077. }else if( !(b_class&JOBL_UPPER) && (previous_class&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  9078. if( sd->status.status_point < battle_config.transcendent_status_points ){
  9079. // The player already used his bonus points, so we have to reset his status points
  9080. pc_resetstate(sd);
  9081. }else{
  9082. sd->status.status_point -= battle_config.transcendent_status_points;
  9083. clif_updatestatus(sd,SP_STATUSPOINT);
  9084. }
  9085. }
  9086. // Give or reduce trait status points
  9087. if ((b_class & JOBL_FOURTH) && !(previous_class & JOBL_FOURTH)) {// Change to a 4th job.
  9088. sd->status.trait_point += battle_config.trait_points_job_change;
  9089. clif_updatestatus(sd, SP_TRAITPOINT);
  9090. clif_updatestatus(sd, SP_UPOW);
  9091. clif_updatestatus(sd, SP_USTA);
  9092. clif_updatestatus(sd, SP_UWIS);
  9093. clif_updatestatus(sd, SP_USPL);
  9094. clif_updatestatus(sd, SP_UCON);
  9095. clif_updatestatus(sd, SP_UCRT);
  9096. } else if (!(b_class & JOBL_FOURTH) && (previous_class & JOBL_FOURTH)) {// Change to a non 4th job.
  9097. if (sd->status.trait_point < battle_config.trait_points_job_change) {
  9098. // Player may have already used the trait status points. Force a reset.
  9099. pc_resetstate(sd);
  9100. } else {
  9101. sd->status.trait_point = 0;
  9102. clif_updatestatus(sd, SP_TRAITPOINT);
  9103. clif_updatestatus(sd, SP_UPOW);
  9104. clif_updatestatus(sd, SP_USTA);
  9105. clif_updatestatus(sd, SP_UWIS);
  9106. clif_updatestatus(sd, SP_USPL);
  9107. clif_updatestatus(sd, SP_UCON);
  9108. clif_updatestatus(sd, SP_UCRT);
  9109. }
  9110. }
  9111. clif_updatestatus(sd,SP_JOBLEVEL);
  9112. clif_updatestatus(sd,SP_JOBEXP);
  9113. clif_updatestatus(sd,SP_NEXTJOBEXP);
  9114. for(i=0;i<EQI_MAX;i++) {
  9115. if(sd->equip_index[i] >= 0)
  9116. if(pc_isequip(sd,sd->equip_index[i]))
  9117. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  9118. }
  9119. //Change look, if disguised, you need to undisguise
  9120. //to correctly calculate new job sprite without
  9121. if (sd->disguise)
  9122. pc_disguise(sd, 0);
  9123. status_set_viewdata(&sd->bl, job);
  9124. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  9125. #if PACKETVER >= 20151001
  9126. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  9127. #endif
  9128. if(sd->vd.cloth_color)
  9129. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9130. /*
  9131. if(sd->vd.body_style)
  9132. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9133. */
  9134. //Update skill tree.
  9135. pc_calc_skilltree(sd);
  9136. clif_skillinfoblock(sd);
  9137. if (sd->ed)
  9138. elemental_delete(sd->ed);
  9139. if (sd->state.vending)
  9140. vending_closevending(sd);
  9141. if (sd->state.buyingstore)
  9142. buyingstore_close(sd);
  9143. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  9144. //Remove peco/cart/falcon
  9145. i = sd->sc.option;
  9146. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  9147. i&=~OPTION_RIDING;
  9148. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  9149. i&=~OPTION_FALCON;
  9150. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  9151. i&=~OPTION_DRAGON;
  9152. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  9153. i&=~OPTION_WUGRIDER;
  9154. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  9155. i&=~OPTION_WUG;
  9156. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  9157. i&=~OPTION_MADOGEAR;
  9158. #ifndef NEW_CARTS
  9159. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  9160. i&=~OPTION_CART;
  9161. #else
  9162. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  9163. pc_setcart(sd, 0);
  9164. #endif
  9165. if(i != sd->sc.option)
  9166. pc_setoption(sd, i);
  9167. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  9168. hom_vaporize(sd, HOM_ST_ACTIVE);
  9169. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  9170. status_change_end(&sd->bl, SC_SPRITEMABLE);
  9171. if (sd->sc.data[SC_SOULATTACK] && !pc_checkskill(sd, SU_SOULATTACK))
  9172. status_change_end(&sd->bl, SC_SOULATTACK);
  9173. if( sd->sc.data[SC_SPIRIT] ){
  9174. status_change_end( &sd->bl, SC_SPIRIT );
  9175. }
  9176. if(sd->status.manner < 0)
  9177. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  9178. status_calc_pc(sd,SCO_FORCE);
  9179. pc_checkallowskill(sd);
  9180. pc_equiplookall(sd);
  9181. pc_show_questinfo(sd);
  9182. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  9183. if( sd->status.party_id ){
  9184. struct party_data* p;
  9185. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  9186. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  9187. if( i < MAX_PARTY ){
  9188. p->party.member[i].class_ = sd->status.class_;
  9189. clif_party_job_and_level( *sd );
  9190. }
  9191. }
  9192. }
  9193. chrif_save(sd, CSAVE_NORMAL);
  9194. //if you were previously famous, not anymore.
  9195. if (fame_flag)
  9196. chrif_buildfamelist();
  9197. else if (sd->status.fame > 0) {
  9198. //It may be that now they are famous?
  9199. switch (sd->class_&MAPID_UPPERMASK) {
  9200. case MAPID_BLACKSMITH:
  9201. case MAPID_ALCHEMIST:
  9202. case MAPID_TAEKWON:
  9203. chrif_buildfamelist();
  9204. break;
  9205. }
  9206. }
  9207. return true;
  9208. }
  9209. /*==========================================
  9210. * Tell client player sd has change equipement
  9211. *------------------------------------------*/
  9212. void pc_equiplookall(struct map_session_data *sd)
  9213. {
  9214. nullpo_retv(sd);
  9215. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  9216. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9217. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  9218. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  9219. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  9220. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  9221. }
  9222. /*==========================================
  9223. * Tell client player sd has change look (hair,equip...)
  9224. *------------------------------------------*/
  9225. void pc_changelook(struct map_session_data *sd,int type,int val) {
  9226. nullpo_retv(sd);
  9227. switch(type) {
  9228. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  9229. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  9230. if (sd->status.hair != val) {
  9231. sd->status.hair = val;
  9232. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9233. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9234. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  9235. }
  9236. break;
  9237. case LOOK_WEAPON:
  9238. sd->status.weapon = val;
  9239. break;
  9240. case LOOK_HEAD_BOTTOM:
  9241. sd->status.head_bottom = val;
  9242. sd->setlook_head_bottom = val;
  9243. break;
  9244. case LOOK_HEAD_TOP:
  9245. sd->status.head_top = val;
  9246. sd->setlook_head_top = val;
  9247. break;
  9248. case LOOK_HEAD_MID:
  9249. sd->status.head_mid = val;
  9250. sd->setlook_head_mid = val;
  9251. break;
  9252. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  9253. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  9254. if (sd->status.hair_color != val) {
  9255. sd->status.hair_color = val;
  9256. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9257. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9258. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  9259. }
  9260. break;
  9261. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  9262. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  9263. sd->status.clothes_color = val;
  9264. break;
  9265. case LOOK_SHIELD:
  9266. sd->status.shield = val;
  9267. break;
  9268. case LOOK_SHOES:
  9269. break;
  9270. case LOOK_ROBE:
  9271. sd->status.robe = val;
  9272. sd->setlook_robe = val;
  9273. break;
  9274. case LOOK_BODY2:
  9275. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  9276. sd->status.body = val;
  9277. break;
  9278. }
  9279. clif_changelook(&sd->bl, type, val);
  9280. }
  9281. /*==========================================
  9282. * Give an option (type) to player (sd) and display it to client
  9283. *------------------------------------------*/
  9284. void pc_setoption(struct map_session_data *sd,int type, int subtype)
  9285. {
  9286. int p_type, new_look=0;
  9287. nullpo_retv(sd);
  9288. p_type = sd->sc.option;
  9289. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  9290. sd->sc.option=type;
  9291. clif_changeoption(&sd->bl);
  9292. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9293. { // Mounting
  9294. clif_status_load(&sd->bl,EFST_RIDING,1);
  9295. status_calc_pc(sd,SCO_NONE);
  9296. }
  9297. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9298. { // Dismount
  9299. clif_status_load(&sd->bl,EFST_RIDING,0);
  9300. status_calc_pc(sd,SCO_NONE);
  9301. }
  9302. #ifndef NEW_CARTS
  9303. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  9304. clif_cartlist(sd);
  9305. clif_updatestatus(sd, SP_CARTINFO);
  9306. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9307. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  9308. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  9309. clif_clearcart(sd->fd);
  9310. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9311. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  9312. }
  9313. #endif
  9314. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  9315. clif_status_load(&sd->bl,EFST_FALCON,1);
  9316. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  9317. clif_status_load(&sd->bl,EFST_FALCON,0);
  9318. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  9319. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  9320. status_calc_pc(sd,SCO_NONE);
  9321. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  9322. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  9323. status_calc_pc(sd,SCO_NONE);
  9324. }
  9325. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  9326. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  9327. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  9328. status_change_end(&sd->bl, SC_MADOGEAR);
  9329. }
  9330. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  9331. new_look = JOB_STAR_GLADIATOR2;
  9332. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  9333. new_look = -1;
  9334. if (sd->disguise || !new_look)
  9335. return; //Disguises break sprite changes
  9336. if (new_look < 0) { //Restore normal look.
  9337. status_set_viewdata(&sd->bl, sd->status.class_);
  9338. new_look = sd->vd.class_;
  9339. }
  9340. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  9341. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  9342. if (sd->vd.cloth_color)
  9343. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9344. if( sd->vd.body_style )
  9345. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9346. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  9347. }
  9348. /**
  9349. * Give player a cart
  9350. * @param sd Player
  9351. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  9352. **/
  9353. bool pc_setcart(struct map_session_data *sd,int type) {
  9354. #ifndef NEW_CARTS
  9355. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  9356. int option;
  9357. #endif
  9358. nullpo_retr(false,sd);
  9359. if( type < 0 || type > MAX_CARTS )
  9360. return false;// Never trust the values sent by the client! [Skotlex]
  9361. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  9362. return false;// Push cart is required
  9363. #ifdef NEW_CARTS
  9364. switch( type ) {
  9365. case 0:
  9366. if( !sd->sc.data[SC_PUSH_CART] )
  9367. return 0;
  9368. status_change_end(&sd->bl,SC_PUSH_CART);
  9369. clif_clearcart(sd->fd);
  9370. break;
  9371. default:/* everything else is an allowed ID so we can move on */
  9372. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  9373. clif_cartlist(sd);
  9374. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  9375. }
  9376. clif_updatestatus(sd, SP_CARTINFO);
  9377. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  9378. break;
  9379. }
  9380. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9381. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  9382. #else
  9383. // Update option
  9384. option = sd->sc.option;
  9385. option &= ~OPTION_CART;// clear cart bits
  9386. option |= cart[type]; // set cart
  9387. pc_setoption(sd, option);
  9388. #endif
  9389. return true;
  9390. }
  9391. /*==========================================
  9392. * Give player a falcon
  9393. *------------------------------------------*/
  9394. void pc_setfalcon(struct map_session_data* sd, int flag)
  9395. {
  9396. if( flag ){
  9397. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  9398. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  9399. } else if( pc_isfalcon(sd) ){
  9400. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  9401. }
  9402. }
  9403. /*==========================================
  9404. * Set player riding
  9405. *------------------------------------------*/
  9406. void pc_setriding(struct map_session_data* sd, int flag)
  9407. {
  9408. if( sd->sc.data[SC_ALL_RIDING] )
  9409. return;
  9410. if( flag ){
  9411. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  9412. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  9413. } else if( pc_isriding(sd) ){
  9414. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  9415. }
  9416. }
  9417. /**
  9418. * Give player a mado
  9419. * @param sd: Player
  9420. * @param flag: Enable or disable mado
  9421. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  9422. */
  9423. void pc_setmadogear(struct map_session_data *sd, bool flag, e_mado_type type)
  9424. {
  9425. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  9426. return;
  9427. if (flag) {
  9428. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  9429. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  9430. } else if (pc_ismadogear(sd)) {
  9431. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  9432. }
  9433. }
  9434. /*==========================================
  9435. * Check if player can drop an item
  9436. *------------------------------------------*/
  9437. bool pc_candrop(struct map_session_data *sd, struct item *item)
  9438. {
  9439. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  9440. return false;
  9441. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  9442. return false;
  9443. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  9444. }
  9445. /*==========================================
  9446. * Read '@type' variables (temporary numeric char reg)
  9447. *------------------------------------------*/
  9448. int64 pc_readreg(struct map_session_data* sd, int64 reg)
  9449. {
  9450. return i64db_i64get(sd->regs.vars, reg);
  9451. }
  9452. /*==========================================
  9453. * Set '@type' variables (temporary numeric char reg)
  9454. *------------------------------------------*/
  9455. bool pc_setreg(struct map_session_data* sd, int64 reg, int64 val)
  9456. {
  9457. uint32 index = script_getvaridx(reg);
  9458. nullpo_retr(false, sd);
  9459. if( val ) {
  9460. i64db_i64put(sd->regs.vars, reg, val);
  9461. if( index )
  9462. script_array_update(&sd->regs, reg, false);
  9463. } else {
  9464. i64db_remove(sd->regs.vars, reg);
  9465. if( index )
  9466. script_array_update(&sd->regs, reg, true);
  9467. }
  9468. return true;
  9469. }
  9470. /*==========================================
  9471. * Read '@type$' variables (temporary string char reg)
  9472. *------------------------------------------*/
  9473. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  9474. {
  9475. struct script_reg_str *p = NULL;
  9476. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9477. return p ? p->value : NULL;
  9478. }
  9479. /*==========================================
  9480. * Set '@type$' variables (temporary string char reg)
  9481. *------------------------------------------*/
  9482. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  9483. {
  9484. struct script_reg_str *p = NULL;
  9485. unsigned int index = script_getvaridx(reg);
  9486. DBData prev;
  9487. nullpo_retr(false, sd);
  9488. if( str[0] ) {
  9489. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9490. p->value = aStrdup(str);
  9491. p->flag.type = 1;
  9492. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9493. p = (struct script_reg_str *)db_data2ptr(&prev);
  9494. if( p->value )
  9495. aFree(p->value);
  9496. ers_free(str_reg_ers, p);
  9497. } else {
  9498. if( index )
  9499. script_array_update(&sd->regs, reg, false);
  9500. }
  9501. } else {
  9502. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  9503. p = (struct script_reg_str *)db_data2ptr(&prev);
  9504. if( p->value )
  9505. aFree(p->value);
  9506. ers_free(str_reg_ers, p);
  9507. if( index )
  9508. script_array_update(&sd->regs, reg, true);
  9509. }
  9510. }
  9511. return true;
  9512. }
  9513. /**
  9514. * Serves the following variable types:
  9515. * - 'type' (permanent numeric char reg)
  9516. * - '#type' (permanent numeric account reg)
  9517. * - '##type' (permanent numeric account reg2)
  9518. **/
  9519. int64 pc_readregistry(struct map_session_data *sd, int64 reg)
  9520. {
  9521. struct script_reg_num *p = NULL;
  9522. if (!sd->vars_ok) {
  9523. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9524. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9525. //intif->request_registry(sd,type==3?4:type);
  9526. set_eof(sd->fd);
  9527. return 0;
  9528. }
  9529. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  9530. return p ? p->value : 0;
  9531. }
  9532. /**
  9533. * Serves the following variable types:
  9534. * - 'type$' (permanent str char reg)
  9535. * - '#type$' (permanent str account reg)
  9536. * - '##type$' (permanent str account reg2)
  9537. **/
  9538. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  9539. {
  9540. struct script_reg_str *p = NULL;
  9541. if (!sd->vars_ok) {
  9542. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9543. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9544. //intif->request_registry(sd,type==3?4:type);
  9545. set_eof(sd->fd);
  9546. return NULL;
  9547. }
  9548. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9549. return p ? p->value : NULL;
  9550. }
  9551. /**
  9552. * Serves the following variable types:
  9553. * - 'type' (permanent numeric char reg)
  9554. * - '#type' (permanent numeric account reg)
  9555. * - '##type' (permanent numeric account reg2)
  9556. **/
  9557. bool pc_setregistry(struct map_session_data *sd, int64 reg, int64 val)
  9558. {
  9559. struct script_reg_num *p = NULL;
  9560. const char *regname = get_str(script_getvarid(reg));
  9561. uint32 index = script_getvaridx(reg);
  9562. if ( !reg_load && !sd->vars_ok ) {
  9563. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  9564. return false;
  9565. }
  9566. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  9567. if( val ) {
  9568. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  9569. script_array_update(&sd->regs, reg, false);
  9570. p->value = val;
  9571. } else {
  9572. p->value = 0;
  9573. if( index )
  9574. script_array_update(&sd->regs, reg, true);
  9575. }
  9576. if (!reg_load)
  9577. p->flag.update = 1;/* either way, it will require either delete or replace */
  9578. } else if( val ) {
  9579. DBData prev;
  9580. if( index )
  9581. script_array_update(&sd->regs, reg, false);
  9582. p = ers_alloc(num_reg_ers, struct script_reg_num);
  9583. p->value = val;
  9584. if (!reg_load)
  9585. p->flag.update = 1;
  9586. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9587. p = (struct script_reg_num *)db_data2ptr(&prev);
  9588. ers_free(num_reg_ers, p);
  9589. }
  9590. }
  9591. if (!reg_load && p)
  9592. sd->vars_dirty = true;
  9593. return true;
  9594. }
  9595. /**
  9596. * Serves the following variable types:
  9597. * - 'type$' (permanent str char reg)
  9598. * - '#type$' (permanent str account reg)
  9599. * - '##type$' (permanent str account reg2)
  9600. **/
  9601. bool pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  9602. {
  9603. struct script_reg_str *p = NULL;
  9604. const char *regname = get_str(script_getvarid(reg));
  9605. unsigned int index = script_getvaridx(reg);
  9606. size_t vlen = 0;
  9607. if (!reg_load && !sd->vars_ok) {
  9608. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  9609. return false;
  9610. }
  9611. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  9612. {
  9613. 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);
  9614. return false;
  9615. }
  9616. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  9617. if( val[0] ) {
  9618. if( p->value )
  9619. aFree(p->value);
  9620. else if ( index ) // an entry that was deleted, so we reset
  9621. script_array_update(&sd->regs, reg, false);
  9622. p->value = aStrdup(val);
  9623. } else {
  9624. if (p->value)
  9625. aFree(p->value);
  9626. p->value = NULL;
  9627. if( index )
  9628. script_array_update(&sd->regs, reg, true);
  9629. }
  9630. if( !reg_load )
  9631. p->flag.update = 1; // either way, it will require either delete or replace
  9632. } else if( val[0] ) {
  9633. DBData prev;
  9634. if( index )
  9635. script_array_update(&sd->regs, reg, false);
  9636. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9637. p->value = aStrdup(val);
  9638. if( !reg_load )
  9639. p->flag.update = 1;
  9640. p->flag.type = 1;
  9641. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  9642. p = (struct script_reg_str *)db_data2ptr(&prev);
  9643. if( p->value )
  9644. aFree(p->value);
  9645. ers_free(str_reg_ers, p);
  9646. }
  9647. }
  9648. if( !reg_load && p )
  9649. sd->vars_dirty = true;
  9650. return true;
  9651. }
  9652. /**
  9653. * Set value of player variable
  9654. * @param sd Player
  9655. * @param reg Variable name
  9656. * @param value
  9657. * @return True if success, false if failed.
  9658. **/
  9659. bool pc_setreg2(struct map_session_data *sd, const char *reg, int64 val) {
  9660. char prefix = reg[0];
  9661. nullpo_retr(false, sd);
  9662. if (reg[strlen(reg)-1] == '$') {
  9663. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9664. return false;
  9665. }
  9666. val = cap_value(val, INT_MIN, INT_MAX);
  9667. switch (prefix) {
  9668. case '.':
  9669. case '\'':
  9670. case '$':
  9671. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9672. return false;
  9673. case '@':
  9674. return pc_setreg(sd, add_str(reg), val);
  9675. case '#':
  9676. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  9677. default:
  9678. return pc_setglobalreg(sd, add_str(reg), val);
  9679. }
  9680. return false;
  9681. }
  9682. /**
  9683. * Get value of player variable
  9684. * @param sd Player
  9685. * @param reg Variable name
  9686. * @return Variable value or 0 if failed.
  9687. **/
  9688. int64 pc_readreg2(struct map_session_data *sd, const char *reg) {
  9689. char prefix = reg[0];
  9690. nullpo_ret(sd);
  9691. if (reg[strlen(reg)-1] == '$') {
  9692. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9693. return 0;
  9694. }
  9695. switch (prefix) {
  9696. case '.':
  9697. case '\'':
  9698. case '$':
  9699. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9700. return 0;
  9701. case '@':
  9702. return pc_readreg(sd, add_str(reg));
  9703. case '#':
  9704. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  9705. default:
  9706. return pc_readglobalreg(sd, add_str(reg));
  9707. }
  9708. return 0;
  9709. }
  9710. /*==========================================
  9711. * Exec eventtimer for player sd (retrieved from map_session (id))
  9712. *------------------------------------------*/
  9713. static TIMER_FUNC(pc_eventtimer){
  9714. struct map_session_data *sd=map_id2sd(id);
  9715. char *p = (char *)data;
  9716. int i;
  9717. if(sd==NULL)
  9718. return 0;
  9719. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  9720. if( i < MAX_EVENTTIMER )
  9721. {
  9722. sd->eventtimer[i] = INVALID_TIMER;
  9723. sd->eventcount--;
  9724. npc_event(sd,p,0);
  9725. }
  9726. else
  9727. ShowError("pc_eventtimer: no such event timer\n");
  9728. if (p) aFree(p);
  9729. return 0;
  9730. }
  9731. /*==========================================
  9732. * Add eventtimer for player sd ?
  9733. *------------------------------------------*/
  9734. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  9735. {
  9736. int i;
  9737. nullpo_retr(false,sd);
  9738. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  9739. if( i == MAX_EVENTTIMER )
  9740. return false;
  9741. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  9742. sd->eventcount++;
  9743. return true;
  9744. }
  9745. /*==========================================
  9746. * Del eventtimer for player sd ?
  9747. *------------------------------------------*/
  9748. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  9749. {
  9750. char* p = NULL;
  9751. int i;
  9752. nullpo_retr(false,sd);
  9753. if (sd->eventcount == 0)
  9754. return false;
  9755. // find the named event timer
  9756. ARR_FIND( 0, MAX_EVENTTIMER, i,
  9757. sd->eventtimer[i] != INVALID_TIMER &&
  9758. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  9759. strcmp(p, name) == 0
  9760. );
  9761. if( i == MAX_EVENTTIMER )
  9762. return false; // not found
  9763. delete_timer(sd->eventtimer[i],pc_eventtimer);
  9764. sd->eventtimer[i] = INVALID_TIMER;
  9765. if(sd->eventcount > 0)
  9766. sd->eventcount--;
  9767. aFree(p);
  9768. return true;
  9769. }
  9770. /*==========================================
  9771. * Update eventtimer count for player sd
  9772. *------------------------------------------*/
  9773. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  9774. {
  9775. int i;
  9776. nullpo_retv(sd);
  9777. for(i=0;i<MAX_EVENTTIMER;i++)
  9778. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  9779. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  9780. addtick_timer(sd->eventtimer[i],tick);
  9781. break;
  9782. }
  9783. }
  9784. /*==========================================
  9785. * Remove all eventtimer for player sd
  9786. *------------------------------------------*/
  9787. void pc_cleareventtimer(struct map_session_data *sd)
  9788. {
  9789. int i;
  9790. nullpo_retv(sd);
  9791. if (sd->eventcount == 0)
  9792. return;
  9793. for(i=0;i<MAX_EVENTTIMER;i++){
  9794. if( sd->eventtimer[i] != INVALID_TIMER ){
  9795. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  9796. delete_timer(sd->eventtimer[i],pc_eventtimer);
  9797. sd->eventtimer[i] = INVALID_TIMER;
  9798. if(sd->eventcount > 0) //avoid looping to max val
  9799. sd->eventcount--;
  9800. if (p) aFree(p);
  9801. }
  9802. }
  9803. }
  9804. /**
  9805. * Called when an item with combo is worn
  9806. * @param sd: Player data
  9807. * @param data: Item data
  9808. * @return Number of succeeded combo(s)
  9809. */
  9810. static int pc_checkcombo(struct map_session_data *sd, item_data *data) {
  9811. int success = 0;
  9812. for (const auto &item_combo : data->combos) {
  9813. bool do_continue = false;
  9814. // Ensure this isn't a duplicate combo
  9815. for (const auto player_combo : sd->combos) {
  9816. if (player_combo->id == item_combo->id) {
  9817. do_continue = true;
  9818. break;
  9819. }
  9820. }
  9821. // Combo already equipped
  9822. if (do_continue)
  9823. continue;
  9824. size_t nb_itemCombo = item_combo->nameid.size();
  9825. if (nb_itemCombo < 2) // A combo with less then 2 item?
  9826. continue;
  9827. struct s_itemchk {
  9828. int idx;
  9829. t_itemid nameid, card[MAX_SLOTS];
  9830. s_itemchk() : idx(0), nameid(0), card() {};
  9831. };
  9832. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  9833. size_t j;
  9834. unsigned int pos = 0;
  9835. for (j = 0; j < nb_itemCombo; j++) {
  9836. t_itemid id = item_combo->nameid[j];
  9837. bool found = false;
  9838. for (int16 k = 0; k < EQI_MAX; k++) {
  9839. short index = sd->equip_index[k];
  9840. if (index < 0)
  9841. continue;
  9842. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  9843. continue;
  9844. if (!sd->inventory_data[index])
  9845. continue;
  9846. if (itemdb_type(id) != IT_CARD) {
  9847. if (sd->inventory_data[index]->nameid != id)
  9848. continue;
  9849. if (j > 0) { // Check if this item not already used
  9850. do_continue = false;
  9851. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  9852. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  9853. do_continue = true;
  9854. break;
  9855. }
  9856. }
  9857. if (do_continue)
  9858. continue;
  9859. }
  9860. combo_idx[j].nameid = id;
  9861. combo_idx[j].idx = index;
  9862. pos |= sd->inventory.u.items_inventory[index].equip;
  9863. found = true;
  9864. break;
  9865. } else { // Cards and enchants
  9866. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  9867. continue;
  9868. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  9869. do_continue = false;
  9870. if (sd->inventory.u.items_inventory[index].card[z] != id)
  9871. continue;
  9872. if (j > 0) {
  9873. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  9874. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  9875. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  9876. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  9877. do_continue = true;
  9878. break;
  9879. }
  9880. }
  9881. }
  9882. }
  9883. }
  9884. if (do_continue)
  9885. continue;
  9886. combo_idx[j].nameid = id;
  9887. combo_idx[j].idx = index;
  9888. combo_idx[j].card[z] = id;
  9889. pos |= sd->inventory.u.items_inventory[index].equip;
  9890. found = true;
  9891. break;
  9892. }
  9893. }
  9894. }
  9895. if (!found)
  9896. break; // Unable to found all the IDs for this combo, return
  9897. }
  9898. // Broke out of the count loop without finding all IDs, move to the next combo
  9899. if (j < nb_itemCombo)
  9900. continue;
  9901. // All items in the combo are matching
  9902. auto entry = std::make_shared<s_combos>();
  9903. entry->bonus = item_combo->script;
  9904. entry->id = item_combo->id;
  9905. entry->pos = pos;
  9906. sd->combos.push_back(entry);
  9907. combo_idx.clear();
  9908. success++;
  9909. }
  9910. return success;
  9911. }
  9912. /**
  9913. * Called when an item with combo is removed
  9914. * @param sd: Player data
  9915. * @param data: Item data
  9916. * @return Number of removed combo(s)
  9917. */
  9918. static int pc_removecombo(struct map_session_data *sd, item_data *data ) {
  9919. if (sd->combos.empty())
  9920. return 0; // Nothing to do here, player has no combos
  9921. int retval = 0;
  9922. for (const auto &item_combo : data->combos) {
  9923. std::shared_ptr<s_combos> del_combo = nullptr;
  9924. // Check if this combo exists on this player
  9925. for (const auto &player_combo : sd->combos) {
  9926. if (player_combo->id == item_combo->id) {
  9927. del_combo = player_combo;
  9928. break;
  9929. }
  9930. }
  9931. // No match, skip this combo
  9932. if (del_combo == nullptr)
  9933. continue;
  9934. util::vector_erase_if_exists(sd->combos, del_combo);
  9935. retval++;
  9936. // Check if combo requirements still fit
  9937. if (pc_checkcombo(sd, data))
  9938. continue;
  9939. // It's empty, clear all the memory
  9940. if (sd->combos.empty()) {
  9941. sd->combos.clear();
  9942. return retval; // Return at this point as there are no more combos to check
  9943. }
  9944. }
  9945. return retval;
  9946. }
  9947. /**
  9948. * Load combo data(s) of player
  9949. * @param sd: Player data
  9950. * @return ret numbers of succeed combo
  9951. */
  9952. int pc_load_combo(struct map_session_data *sd) {
  9953. int ret = 0;
  9954. for (int16 i = 0; i < EQI_MAX; i++) {
  9955. item_data *id;
  9956. short idx = sd->equip_index[i];
  9957. if (idx < 0 || !(id = sd->inventory_data[idx]))
  9958. continue;
  9959. if (!id->combos.empty())
  9960. ret += pc_checkcombo(sd, id);
  9961. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  9962. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  9963. if (!sd->inventory.u.items_inventory[idx].card[j])
  9964. continue;
  9965. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[idx].card[j]);
  9966. if (data != nullptr) {
  9967. if (!data->combos.empty())
  9968. ret += pc_checkcombo(sd, data.get());
  9969. }
  9970. }
  9971. }
  9972. }
  9973. return ret;
  9974. }
  9975. /*==========================================
  9976. * Equip item on player sd at req_pos from inventory index n
  9977. * return: false - fail; true - success
  9978. *------------------------------------------*/
  9979. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  9980. {
  9981. int i, pos, flag = 0, iflag;
  9982. struct item_data *id;
  9983. uint8 res = ITEM_EQUIP_ACK_OK;
  9984. short* equip_index;
  9985. nullpo_retr(false,sd);
  9986. if( n < 0 || n >= MAX_INVENTORY ) {
  9987. if( equipswitch ){
  9988. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9989. }else{
  9990. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  9991. }
  9992. return false;
  9993. }
  9994. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  9995. if( equipswitch ){
  9996. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9997. }else{
  9998. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  9999. }
  10000. return false;
  10001. }
  10002. if (!(id = sd->inventory_data[n]))
  10003. return false;
  10004. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  10005. if(battle_config.battle_log && !equipswitch)
  10006. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  10007. if((res = pc_isequip(sd,n))) {
  10008. if( equipswitch ){
  10009. clif_equipswitch_add( sd, n, req_pos, res );
  10010. }else{
  10011. clif_equipitemack(sd,n,0,res); // fail
  10012. }
  10013. return false;
  10014. }
  10015. if( equipswitch && id->type == IT_AMMO ){
  10016. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10017. return false;
  10018. }
  10019. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  10020. if( equipswitch ){
  10021. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10022. }else{
  10023. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  10024. }
  10025. return false;
  10026. }
  10027. if( sd->sc.count && (sd->sc.cant.equip || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  10028. if( equipswitch ){
  10029. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10030. }else{
  10031. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  10032. }
  10033. return false;
  10034. }
  10035. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  10036. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  10037. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  10038. clif_notify_bindOnEquip(sd,n);
  10039. }
  10040. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  10041. pos = req_pos&EQP_ACC;
  10042. if (pos == EQP_ACC) //User specified both slots.
  10043. pos = (equip_index[EQI_ACC_R] >= 0 && equip_index[EQI_ACC_L] < 0) ? EQP_ACC_L : EQP_ACC_R;
  10044. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  10045. if (!sd->inventory.u.items_inventory[n].card[i])
  10046. continue;
  10047. std::shared_ptr<item_data> card_data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10048. if (card_data) {
  10049. int card_pos = card_data->equip;
  10050. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  10051. pos = card_pos; // Use the card's equip position
  10052. break;
  10053. }
  10054. }
  10055. }
  10056. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  10057. pos = (req_pos&EQP_ARMS);
  10058. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  10059. #ifdef RENEWAL
  10060. pos = (equip_index[EQI_HAND_R] >= 0 && equip_index[EQI_HAND_L] < 0) ? EQP_HAND_L : EQP_HAND_R;
  10061. #else
  10062. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  10063. #endif
  10064. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  10065. pos = req_pos&EQP_SHADOW_ACC;
  10066. if (pos == EQP_SHADOW_ACC)
  10067. pos = (equip_index[EQI_SHADOW_ACC_L] >= 0 && equip_index[EQI_SHADOW_ACC_R] < 0) ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  10068. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  10069. pos = (req_pos&EQP_SHADOW_ARMS);
  10070. if( pos == EQP_SHADOW_ARMS )
  10071. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  10072. }
  10073. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  10074. //Update skill-block range database when weapon range changes. [Skotlex]
  10075. i = equip_index[EQI_HAND_R];
  10076. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  10077. flag = 1;
  10078. else
  10079. flag = id->range != sd->inventory_data[i]->range;
  10080. }
  10081. if( equipswitch ){
  10082. for( i = 0; i < EQI_MAX; i++ ){
  10083. if( pos&equip_bitmask[i] ){
  10084. // If there was already an item assigned to this slot
  10085. if( sd->equip_switch_index[i] >= 0 ){
  10086. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  10087. }
  10088. // Assign the new index to it
  10089. sd->equip_switch_index[i] = n;
  10090. }
  10091. }
  10092. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  10093. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  10094. return true;
  10095. }else{
  10096. for(i=0;i<EQI_MAX;i++) {
  10097. if(pos & equip_bitmask[i]) {
  10098. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  10099. pc_unequipitem(sd,sd->equip_index[i], 1 | 2 | 4);
  10100. sd->equip_index[i] = n;
  10101. }
  10102. }
  10103. pc_equipswitch_remove(sd, n);
  10104. if(pos==EQP_AMMO) {
  10105. clif_arrowequip(sd,n);
  10106. clif_arrow_fail(sd,3);
  10107. }
  10108. else
  10109. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  10110. sd->inventory.u.items_inventory[n].equip = pos;
  10111. }
  10112. if(pos & EQP_HAND_R) {
  10113. if(id)
  10114. sd->weapontype1 = id->subtype;
  10115. else
  10116. sd->weapontype1 = W_FIST;
  10117. pc_calcweapontype(sd);
  10118. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10119. }
  10120. if(pos & EQP_HAND_L) {
  10121. if(id) {
  10122. if(id->type == IT_WEAPON) {
  10123. sd->status.shield = W_FIST;
  10124. sd->weapontype2 = id->subtype;
  10125. }
  10126. else
  10127. if(id->type == IT_ARMOR) {
  10128. sd->status.shield = id->look;
  10129. sd->weapontype2 = W_FIST;
  10130. }
  10131. }
  10132. else
  10133. sd->status.shield = sd->weapontype2 = W_FIST;
  10134. pc_calcweapontype(sd);
  10135. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10136. }
  10137. if(pos & EQP_SHOES)
  10138. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10139. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  10140. short idx = sd->equip_index[EQI_AMMO];
  10141. if (idx >= 0) {
  10142. switch (sd->inventory_data[idx]->subtype) {
  10143. case AMMO_ARROW:
  10144. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  10145. pc_unequipitem(sd, idx, 2 | 4);
  10146. break;
  10147. case AMMO_BULLET:
  10148. case AMMO_SHELL:
  10149. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  10150. #ifdef RENEWAL
  10151. && id->subtype != W_GRENADE
  10152. #endif
  10153. )
  10154. pc_unequipitem(sd, idx, 2 | 4);
  10155. break;
  10156. #ifndef RENEWAL
  10157. case AMMO_GRENADE:
  10158. if (id->subtype != W_GRENADE)
  10159. pc_unequipitem(sd, idx, 2 | 4);
  10160. break;
  10161. #endif
  10162. }
  10163. }
  10164. }
  10165. pc_set_costume_view(sd);
  10166. pc_checkallowskill(sd); //Check if status changes should be halted.
  10167. iflag = sd->npc_item_flag;
  10168. // Check for combos (MUST be before status_calc_pc)
  10169. if (!id->combos.empty())
  10170. pc_checkcombo(sd, id);
  10171. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10172. ; // No cards
  10173. else {
  10174. for (i = 0; i < MAX_SLOTS; i++) {
  10175. if (!sd->inventory.u.items_inventory[n].card[i])
  10176. continue;
  10177. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10178. if (data != nullptr) {
  10179. if (!data->combos.empty())
  10180. pc_checkcombo(sd, data.get());
  10181. }
  10182. }
  10183. }
  10184. status_calc_pc(sd,SCO_NONE);
  10185. if (flag) //Update skill data
  10186. clif_skillinfoblock(sd);
  10187. //OnEquip script [Skotlex]
  10188. if (id) {
  10189. //only run the script if item isn't restricted
  10190. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  10191. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10192. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10193. ; //No cards
  10194. else {
  10195. for( i = 0; i < MAX_SLOTS; i++ ) {
  10196. if (!sd->inventory.u.items_inventory[n].card[i])
  10197. continue;
  10198. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10199. if ( data != nullptr ) {
  10200. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data.get(), sd->bl.m)))
  10201. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10202. }
  10203. }
  10204. }
  10205. }
  10206. sd->npc_item_flag = iflag;
  10207. return true;
  10208. }
  10209. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int position ){
  10210. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  10211. while( it != bonus.end() ){
  10212. std::shared_ptr<s_autobonus> b = *it;
  10213. if( ( b->pos & position ) != b->pos ){
  10214. it++;
  10215. continue;
  10216. }
  10217. it = bonus.erase( it );
  10218. }
  10219. }
  10220. /**
  10221. * Recalculate player status on unequip
  10222. * @param sd: Player data
  10223. * @param n: Item inventory index
  10224. * @param flag: Whether to recalculate a player's status or not
  10225. * @return True on success or false on failure
  10226. */
  10227. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  10228. int i, iflag;
  10229. bool status_calc = false;
  10230. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  10231. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  10232. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  10233. sd->inventory.u.items_inventory[n].equip = 0;
  10234. if (!(flag & 4))
  10235. pc_checkallowskill(sd);
  10236. iflag = sd->npc_item_flag;
  10237. // Check for combos (MUST be before status_calc_pc)
  10238. if (sd->inventory_data[n]) {
  10239. if (!sd->inventory_data[n]->combos.empty()) {
  10240. if (pc_removecombo(sd, sd->inventory_data[n]))
  10241. status_calc = true;
  10242. }
  10243. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10244. ; // No cards
  10245. else {
  10246. for (i = 0; i < MAX_SLOTS; i++) {
  10247. if (!sd->inventory.u.items_inventory[n].card[i])
  10248. continue;
  10249. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10250. if (data != nullptr) {
  10251. if (!data->combos.empty()) {
  10252. if (pc_removecombo(sd, data.get()))
  10253. status_calc = true;
  10254. }
  10255. }
  10256. }
  10257. }
  10258. }
  10259. if (flag & 1 || status_calc) {
  10260. pc_checkallowskill(sd);
  10261. status_calc_pc(sd, SCO_FORCE);
  10262. }
  10263. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  10264. status_change_end(&sd->bl, SC_SIGNUMCRUCIS);
  10265. //OnUnEquip script [Skotlex]
  10266. if (sd->inventory_data[n]) {
  10267. if (sd->inventory_data[n]->unequip_script)
  10268. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10269. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10270. ; //No cards
  10271. else {
  10272. for (i = 0; i < MAX_SLOTS; i++) {
  10273. if (!sd->inventory.u.items_inventory[n].card[i])
  10274. continue;
  10275. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10276. if (data != nullptr) {
  10277. if (data->unequip_script)
  10278. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10279. }
  10280. }
  10281. }
  10282. }
  10283. sd->npc_item_flag = iflag;
  10284. }
  10285. /**
  10286. * Called when attempting to unequip an item from a player
  10287. * @param sd: Player data
  10288. * @param n: Item inventory index
  10289. * @param flag: Type of unequip
  10290. * 0 - only unequip
  10291. * 1 - calculate status after unequipping
  10292. * 2 - force unequip
  10293. * 4 - unequip by switching equipment
  10294. * @return True on success or false on failure
  10295. */
  10296. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  10297. int i, pos;
  10298. nullpo_retr(false,sd);
  10299. if (n < 0 || n >= MAX_INVENTORY) {
  10300. clif_unequipitemack(sd,0,0,0);
  10301. return false;
  10302. }
  10303. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  10304. clif_unequipitemack(sd,n,0,0);
  10305. return false; //Nothing to unequip
  10306. }
  10307. // status change that makes player cannot unequip equipment
  10308. if (!(flag&2) && sd->sc.count &&( sd->sc.cant.unequip ||
  10309. (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  10310. {
  10311. clif_unequipitemack(sd,n,0,0);
  10312. return false;
  10313. }
  10314. if (battle_config.battle_log)
  10315. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  10316. for(i = 0; i < EQI_MAX; i++) {
  10317. if (pos & equip_bitmask[i])
  10318. sd->equip_index[i] = -1;
  10319. }
  10320. if(pos & EQP_HAND_R) {
  10321. sd->weapontype1 = W_FIST;
  10322. sd->status.weapon = sd->weapontype2;
  10323. pc_calcweapontype(sd);
  10324. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10325. if( !battle_config.dancing_weaponswitch_fix )
  10326. status_change_end(&sd->bl, SC_DANCING); // Unequipping => stop dancing.
  10327. #ifdef RENEWAL
  10328. if (battle_config.switch_remove_edp&2) {
  10329. #else
  10330. if (battle_config.switch_remove_edp&1) {
  10331. #endif
  10332. status_change_end(&sd->bl, SC_EDP);
  10333. }
  10334. }
  10335. if(pos & EQP_HAND_L) {
  10336. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  10337. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  10338. sd->status.shield = sd->weapontype2 = W_FIST;
  10339. pc_calcweapontype(sd);
  10340. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10341. }
  10342. if(pos & EQP_SHOES)
  10343. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10344. clif_unequipitemack(sd,n,pos,1);
  10345. pc_set_costume_view(sd);
  10346. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIP });
  10347. // On weapon change (right and left hand)
  10348. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  10349. if (battle_config.ammo_unequip && !(flag & 4)) {
  10350. switch (sd->inventory_data[n]->subtype) {
  10351. case W_BOW:
  10352. case W_MUSICAL:
  10353. case W_WHIP:
  10354. case W_REVOLVER:
  10355. case W_RIFLE:
  10356. case W_GATLING:
  10357. case W_SHOTGUN:
  10358. case W_GRENADE: {
  10359. short idx = sd->equip_index[EQI_AMMO];
  10360. if (idx >= 0) {
  10361. sd->equip_index[EQI_AMMO] = -1;
  10362. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  10363. pc_unequipitem_sub(sd, idx, 0);
  10364. }
  10365. }
  10366. break;
  10367. }
  10368. }
  10369. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPWEAPON });
  10370. }
  10371. // On armor change
  10372. if (pos & EQP_ARMOR) {
  10373. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPARMOR });
  10374. }
  10375. // On equipment change
  10376. #ifndef RENEWAL
  10377. if (!(flag&4))
  10378. status_change_end(&sd->bl, SC_CONCENTRATION);
  10379. #endif
  10380. // On ammo change
  10381. 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_))
  10382. status_change_end(&sd->bl, SC_P_ALTER);
  10383. pc_unequipitem_sub(sd, n, flag);
  10384. return true;
  10385. }
  10386. int pc_equipswitch( struct map_session_data* sd, int index ){
  10387. // Get the target equip mask
  10388. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  10389. // Get the currently equipped item
  10390. short equippedItem = pc_checkequip( sd, position, true );
  10391. // No item equipped at the target
  10392. if( equippedItem == -1 ){
  10393. // Remove it from the equip switch
  10394. pc_equipswitch_remove( sd, index );
  10395. pc_equipitem( sd, index, position );
  10396. return position;
  10397. }else{
  10398. std::map<int, int> unequipped;
  10399. int unequipped_position = 0;
  10400. // Unequip all items that interfere
  10401. for( int i = 0; i < EQI_MAX; i++ ){
  10402. int unequip_index = sd->equip_index[i];
  10403. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  10404. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  10405. // Store the unequipped index and position mask for later
  10406. unequipped[unequip_index] = unequip_item->equip;
  10407. // Keep the position for later
  10408. unequipped_position |= unequip_item->equip;
  10409. // Unequip the item
  10410. pc_unequipitem( sd, unequip_index, 0 );
  10411. }
  10412. }
  10413. int all_position = position | unequipped_position;
  10414. // Equip everything that is hit by the mask
  10415. for( int i = 0; i < EQI_MAX; i++ ){
  10416. int exchange_index = sd->equip_switch_index[i];
  10417. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  10418. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  10419. // Store the target position
  10420. int exchange_position = exchange_item->equipSwitch;
  10421. // Remove the item from equip switch
  10422. pc_equipswitch_remove( sd, exchange_index );
  10423. // Equip the item at the destinated position
  10424. pc_equipitem( sd, exchange_index, exchange_position );
  10425. }
  10426. }
  10427. // Place all unequipped items into the equip switch window
  10428. for( std::pair<int, int> pair : unequipped ){
  10429. int unequipped_index = pair.first;
  10430. int unequipped_position = pair.second;
  10431. // Rebuild the index cache
  10432. for( int i = 0; i < EQI_MAX; i++ ){
  10433. if( unequipped_position & equip_bitmask[i] ){
  10434. sd->equip_switch_index[i] = unequipped_index;
  10435. }
  10436. }
  10437. // Set the correct position mask
  10438. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  10439. // Notify the client
  10440. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  10441. }
  10442. return all_position;
  10443. }
  10444. }
  10445. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  10446. struct item* item = &sd->inventory.u.items_inventory[index];
  10447. if( !item->equipSwitch ){
  10448. return;
  10449. }
  10450. for( int i = 0; i < EQI_MAX; i++ ){
  10451. // If a match is found
  10452. if( sd->equip_switch_index[i] == index ){
  10453. // Remove it from the slot
  10454. sd->equip_switch_index[i] = -1;
  10455. }
  10456. }
  10457. // Send out one packet for all slots using the current item's mask
  10458. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  10459. item->equipSwitch = 0;
  10460. }
  10461. /*==========================================
  10462. * Checking if player (sd) has an invalid item
  10463. * and is unequiped on map load (item_noequip)
  10464. *------------------------------------------*/
  10465. void pc_checkitem(struct map_session_data *sd) {
  10466. int i, calc_flag = 0;
  10467. struct item* it;
  10468. nullpo_retv(sd);
  10469. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  10470. return;
  10471. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  10472. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10473. it = &sd->inventory.u.items_inventory[i];
  10474. if( it->nameid == 0 )
  10475. continue;
  10476. if( !it->equip )
  10477. continue;
  10478. if( it->equip&~pc_equippoint(sd,i) ) {
  10479. pc_unequipitem(sd, i, 2);
  10480. calc_flag = 1;
  10481. continue;
  10482. }
  10483. 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) ) {
  10484. pc_unequipitem(sd, i, 2);
  10485. calc_flag = 1;
  10486. continue;
  10487. }
  10488. }
  10489. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10490. it = &sd->inventory.u.items_inventory[i];
  10491. if( it->nameid == 0 )
  10492. continue;
  10493. if( !it->equipSwitch )
  10494. continue;
  10495. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  10496. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  10497. for( int j = 0; j < EQI_MAX; j++ ){
  10498. if( sd->equip_switch_index[j] == i ){
  10499. sd->equip_switch_index[j] = -1;
  10500. }
  10501. }
  10502. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  10503. continue;
  10504. }
  10505. }
  10506. if( calc_flag && sd->state.active ) {
  10507. pc_checkallowskill(sd);
  10508. status_calc_pc(sd,SCO_NONE);
  10509. }
  10510. }
  10511. /*==========================================
  10512. * Checks for unavailable items and removes them.
  10513. * @param sd: Player data
  10514. * @param type Forced check:
  10515. * 1 - Inventory
  10516. * 2 - Cart
  10517. * 4 - Storage
  10518. *------------------------------------------*/
  10519. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  10520. {
  10521. int i;
  10522. t_itemid nameid;
  10523. char output[256];
  10524. nullpo_retv(sd);
  10525. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  10526. for(i = 0; i < MAX_INVENTORY; i++) {
  10527. nameid = sd->inventory.u.items_inventory[i].nameid;
  10528. if (!nameid)
  10529. continue;
  10530. if (!itemdb_available(nameid)) {
  10531. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  10532. clif_displaymessage(sd->fd, output);
  10533. 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);
  10534. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  10535. continue;
  10536. }
  10537. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  10538. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  10539. }
  10540. }
  10541. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  10542. for(i = 0; i < MAX_CART; i++) {
  10543. nameid = sd->cart.u.items_cart[i].nameid;
  10544. if (!nameid)
  10545. continue;
  10546. if (!itemdb_available(nameid)) {
  10547. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  10548. clif_displaymessage(sd->fd, output);
  10549. 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);
  10550. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  10551. continue;
  10552. }
  10553. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  10554. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  10555. }
  10556. }
  10557. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  10558. for(i = 0; i < sd->storage.max_amount; i++) {
  10559. nameid = sd->storage.u.items_storage[i].nameid;
  10560. if (!nameid)
  10561. continue;
  10562. if (!itemdb_available(nameid)) {
  10563. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  10564. clif_displaymessage(sd->fd, output);
  10565. 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);
  10566. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  10567. continue;
  10568. }
  10569. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  10570. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  10571. }
  10572. }
  10573. }
  10574. /*==========================================
  10575. * Update PVP rank for sd1 in cmp to sd2
  10576. *------------------------------------------*/
  10577. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  10578. {
  10579. struct map_session_data *sd1,*sd2;
  10580. sd1=(struct map_session_data *)bl;
  10581. sd2=va_arg(ap,struct map_session_data *);
  10582. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  10583. {// cannot register pvp rank for hidden GMs
  10584. return 0;
  10585. }
  10586. if( sd1->pvp_point > sd2->pvp_point )
  10587. sd2->pvp_rank++;
  10588. return 0;
  10589. }
  10590. /*==========================================
  10591. * Calculate new rank beetween all present players (map_foreachinallarea)
  10592. * and display result
  10593. *------------------------------------------*/
  10594. int pc_calc_pvprank(struct map_session_data *sd)
  10595. {
  10596. int old = sd->pvp_rank;
  10597. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10598. sd->pvp_rank=1;
  10599. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  10600. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  10601. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  10602. return sd->pvp_rank;
  10603. }
  10604. /*==========================================
  10605. * Calculate next sd ranking calculation from config
  10606. *------------------------------------------*/
  10607. TIMER_FUNC(pc_calc_pvprank_timer){
  10608. struct map_session_data *sd;
  10609. sd=map_id2sd(id);
  10610. if(sd==NULL)
  10611. return 0;
  10612. sd->pvp_timer = INVALID_TIMER;
  10613. if( pc_isinvisible(sd) )
  10614. {// do not calculate the pvp rank for a hidden GM
  10615. return 0;
  10616. }
  10617. if( pc_calc_pvprank(sd) > 0 )
  10618. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  10619. return 0;
  10620. }
  10621. /*==========================================
  10622. * Checking if sd is married
  10623. * Return:
  10624. * partner_id = yes
  10625. * 0 = no
  10626. *------------------------------------------*/
  10627. int pc_ismarried(struct map_session_data *sd)
  10628. {
  10629. if(sd == NULL)
  10630. return -1;
  10631. if(sd->status.partner_id > 0)
  10632. return sd->status.partner_id;
  10633. else
  10634. return 0;
  10635. }
  10636. /*==========================================
  10637. * Marry player sd to player dstsd
  10638. * Return:
  10639. * false = fail
  10640. * true = success
  10641. *------------------------------------------*/
  10642. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  10643. {
  10644. if(sd == NULL || dstsd == NULL ||
  10645. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  10646. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  10647. return false;
  10648. sd->status.partner_id = dstsd->status.char_id;
  10649. dstsd->status.partner_id = sd->status.char_id;
  10650. achievement_update_objective(sd, AG_MARRY, 1, 1);
  10651. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  10652. return true;
  10653. }
  10654. /*==========================================
  10655. * Divorce sd from its partner
  10656. * Return:
  10657. * false = fail
  10658. * true = success
  10659. *------------------------------------------*/
  10660. bool pc_divorce(struct map_session_data *sd)
  10661. {
  10662. struct map_session_data *p_sd;
  10663. int i;
  10664. if( sd == NULL || !pc_ismarried(sd) )
  10665. return false;
  10666. if( !sd->status.partner_id )
  10667. return false; // Char is not married
  10668. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  10669. { // Lets char server do the divorce
  10670. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  10671. return false; // No char server connected
  10672. return true;
  10673. }
  10674. // Both players online, lets do the divorce manually
  10675. sd->status.partner_id = 0;
  10676. p_sd->status.partner_id = 0;
  10677. for( i = 0; i < MAX_INVENTORY; i++ )
  10678. {
  10679. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10680. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10681. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10682. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10683. }
  10684. clif_divorced(sd, p_sd->status.name);
  10685. clif_divorced(p_sd, sd->status.name);
  10686. return true;
  10687. }
  10688. /**
  10689. * Get the partner map_session_data of a player
  10690. * @param sd : the husband|wife session
  10691. * @return partner session or NULL
  10692. */
  10693. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  10694. if (!sd || !pc_ismarried(sd))
  10695. return NULL;
  10696. return map_charid2sd(sd->status.partner_id);
  10697. }
  10698. /**
  10699. * Get the father map_session_data of a player
  10700. * @param sd : the baby session
  10701. * @return father session or NULL
  10702. */
  10703. struct map_session_data *pc_get_father (struct map_session_data *sd){
  10704. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  10705. return NULL;
  10706. return map_charid2sd(sd->status.father);
  10707. }
  10708. /**
  10709. * Get the mother map_session_data of a player
  10710. * @param sd : the baby session
  10711. * @return mother session or NULL
  10712. */
  10713. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  10714. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  10715. return NULL;
  10716. return map_charid2sd(sd->status.mother);
  10717. }
  10718. /*==========================================
  10719. * Get sd children charid. (Need to be married)
  10720. *------------------------------------------*/
  10721. struct map_session_data *pc_get_child (struct map_session_data *sd)
  10722. {
  10723. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  10724. // charid2sd returns NULL if not found
  10725. return NULL;
  10726. return map_charid2sd(sd->status.child);
  10727. }
  10728. /*==========================================
  10729. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  10730. *------------------------------------------*/
  10731. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  10732. {
  10733. int hp = 0, sp = 0;
  10734. if( pc_isdead(sd) )
  10735. return;
  10736. if (sd->hp_loss.value) {
  10737. sd->hp_loss.tick += diff_tick;
  10738. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  10739. hp += sd->hp_loss.value;
  10740. sd->hp_loss.tick -= sd->hp_loss.rate;
  10741. }
  10742. if(hp >= sd->battle_status.hp)
  10743. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  10744. }
  10745. if (sd->sp_loss.value) {
  10746. sd->sp_loss.tick += diff_tick;
  10747. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  10748. sp += sd->sp_loss.value;
  10749. sd->sp_loss.tick -= sd->sp_loss.rate;
  10750. }
  10751. }
  10752. if (hp > 0 || sp > 0)
  10753. status_zap(&sd->bl, hp, sp);
  10754. }
  10755. //Character regen. Flag is used to know which types of regen can take place.
  10756. //&1: HP regen
  10757. //&2: SP regen
  10758. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  10759. {
  10760. int hp = 0, sp = 0;
  10761. if (sd->hp_regen.value) {
  10762. sd->hp_regen.tick += diff_tick;
  10763. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  10764. hp += sd->hp_regen.value;
  10765. sd->hp_regen.tick -= sd->hp_regen.rate;
  10766. }
  10767. }
  10768. if (sd->sp_regen.value) {
  10769. sd->sp_regen.tick += diff_tick;
  10770. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  10771. sp += sd->sp_regen.value;
  10772. sd->sp_regen.tick -= sd->sp_regen.rate;
  10773. }
  10774. }
  10775. if (sd->percent_hp_regen.value) {
  10776. sd->percent_hp_regen.tick += diff_tick;
  10777. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  10778. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  10779. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  10780. }
  10781. }
  10782. if (sd->percent_sp_regen.value) {
  10783. sd->percent_sp_regen.tick += diff_tick;
  10784. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  10785. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  10786. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  10787. }
  10788. }
  10789. if (hp > 0 || sp > 0)
  10790. status_heal(&sd->bl, hp, sp, 0);
  10791. }
  10792. /*==========================================
  10793. * Memo player sd savepoint. (map,x,y)
  10794. *------------------------------------------*/
  10795. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  10796. {
  10797. nullpo_retv(sd);
  10798. sd->status.save_point.map = mapindex;
  10799. sd->status.save_point.x = x;
  10800. sd->status.save_point.y = y;
  10801. }
  10802. /*==========================================
  10803. * Save 1 player data at autosave interval
  10804. *------------------------------------------*/
  10805. static TIMER_FUNC(pc_autosave){
  10806. int interval;
  10807. struct s_mapiterator* iter;
  10808. struct map_session_data* sd;
  10809. static int last_save_id = 0, save_flag = 0;
  10810. if(save_flag == 2) //Someone was saved on last call, normal cycle
  10811. save_flag = 0;
  10812. else
  10813. save_flag = 1; //Noone was saved, so save first found char.
  10814. iter = mapit_getallusers();
  10815. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10816. {
  10817. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  10818. continue;
  10819. if(sd->bl.id == last_save_id && save_flag != 1) {
  10820. save_flag = 1;
  10821. continue;
  10822. }
  10823. if(save_flag != 1) //Not our turn to save yet.
  10824. continue;
  10825. //Save char.
  10826. last_save_id = sd->bl.id;
  10827. save_flag = 2;
  10828. if (pc_isvip(sd)) // Check if we're still VIP
  10829. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  10830. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  10831. break;
  10832. }
  10833. mapit_free(iter);
  10834. interval = autosave_interval/(map_usercount()+1);
  10835. if(interval < minsave_interval)
  10836. interval = minsave_interval;
  10837. add_timer(gettick()+interval,pc_autosave,0,0);
  10838. return 0;
  10839. }
  10840. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  10841. {
  10842. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  10843. { //Night/day state does not match.
  10844. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  10845. sd->state.night = night_flag;
  10846. return 1;
  10847. }
  10848. return 0;
  10849. }
  10850. /*================================================
  10851. * timer to do the day [Yor]
  10852. * data: 0 = called by timer, 1 = gmcommand/script
  10853. *------------------------------------------------*/
  10854. TIMER_FUNC(map_day_timer){
  10855. char tmp_soutput[1024];
  10856. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  10857. return 0;
  10858. if (!night_flag)
  10859. return 0; //Already day.
  10860. night_flag = 0; // 0=day, 1=night [Yor]
  10861. map_foreachpc(pc_daynight_timer_sub);
  10862. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  10863. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  10864. return 0;
  10865. }
  10866. /*================================================
  10867. * timer to do the night [Yor]
  10868. * data: 0 = called by timer, 1 = gmcommand/script
  10869. *------------------------------------------------*/
  10870. TIMER_FUNC(map_night_timer){
  10871. char tmp_soutput[1024];
  10872. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  10873. return 0;
  10874. if (night_flag)
  10875. return 0; //Already nigth.
  10876. night_flag = 1; // 0=day, 1=night [Yor]
  10877. map_foreachpc(pc_daynight_timer_sub);
  10878. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  10879. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  10880. return 0;
  10881. }
  10882. /**
  10883. * Attempt to stand up a player
  10884. * @param sd
  10885. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  10886. * @return True if success, Fals if failed
  10887. */
  10888. bool pc_setstand(struct map_session_data *sd, bool force){
  10889. nullpo_ret(sd);
  10890. // Cannot stand yet
  10891. // TODO: Move to SCS_NOSTAND [Cydh]
  10892. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  10893. return false;
  10894. status_change_end(&sd->bl, SC_TENSIONRELAX);
  10895. clif_status_load(&sd->bl,EFST_SIT,0);
  10896. clif_standing(&sd->bl); //Inform area PC is standing
  10897. //Reset sitting tick.
  10898. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  10899. if( pc_isdead( sd ) ){
  10900. sd->state.dead_sit = sd->vd.dead_sit = 0;
  10901. clif_party_dead( *sd );
  10902. }else{
  10903. sd->state.dead_sit = sd->vd.dead_sit = 0;
  10904. }
  10905. return true;
  10906. }
  10907. /**
  10908. * Calculate Overheat value
  10909. * @param sd: Player data
  10910. * @param heat: Amount of Heat to adjust
  10911. **/
  10912. void pc_overheat(map_session_data &sd, int16 heat) {
  10913. status_change_entry *sce = sd.sc.data[SC_OVERHEAT_LIMITPOINT];
  10914. if (sce) {
  10915. sce->val1 += heat;
  10916. sce->val1 = cap_value(sce->val1, 0, 1000);
  10917. if (heat < 0 && sce->val1 == 0) { // Cooling device used.
  10918. status_change_end(&sd.bl, SC_OVERHEAT_LIMITPOINT);
  10919. status_change_end(&sd.bl, SC_OVERHEAT);
  10920. }
  10921. } else if (heat > 0)
  10922. sc_start(&sd.bl, &sd.bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  10923. }
  10924. /**
  10925. * Check if player is autolooting given itemID.
  10926. */
  10927. bool pc_isautolooting(struct map_session_data *sd, t_itemid nameid)
  10928. {
  10929. uint8 i = 0;
  10930. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  10931. return true;
  10932. if (!sd->state.autolooting)
  10933. return false;
  10934. if (sd->state.autolooting)
  10935. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  10936. return (i != AUTOLOOTITEM_SIZE);
  10937. }
  10938. /**
  10939. * Checks if player can use @/#command
  10940. * @param sd Player map session data
  10941. * @param command Command name without @/# and params
  10942. * @param type is it atcommand or charcommand
  10943. */
  10944. bool pc_can_use_command( struct map_session_data *sd, const char *command, AtCommandType type ){
  10945. return sd->group->can_use_command( command, type );
  10946. }
  10947. bool pc_has_permission( struct map_session_data* sd, e_pc_permission permission ){
  10948. return sd->permissions.test( permission );
  10949. }
  10950. /**
  10951. * Checks if commands used by a player should be logged
  10952. * according to their group setting.
  10953. * @param sd Player map session data
  10954. */
  10955. bool pc_should_log_commands( struct map_session_data *sd ){
  10956. return sd->group->log_commands;
  10957. }
  10958. /**
  10959. * Spirit Charm expiration timer.
  10960. * @see TimerFunc
  10961. */
  10962. static TIMER_FUNC(pc_spiritcharm_timer){
  10963. struct map_session_data *sd;
  10964. int i;
  10965. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  10966. return 1;
  10967. if (sd->spiritcharm <= 0) {
  10968. 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);
  10969. sd->spiritcharm = 0;
  10970. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10971. return 0;
  10972. }
  10973. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  10974. if (i == sd->spiritcharm) {
  10975. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  10976. return 0;
  10977. }
  10978. sd->spiritcharm--;
  10979. if (i != sd->spiritcharm)
  10980. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  10981. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10982. if (sd->spiritcharm <= 0)
  10983. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10984. clif_spiritcharm(sd);
  10985. return 0;
  10986. }
  10987. /**
  10988. * Adds a spirit charm.
  10989. * @param sd: Target character
  10990. * @param interval: Duration
  10991. * @param max: Maximum amount of charms to add
  10992. * @param type: Charm type (@see spirit_charm_types)
  10993. */
  10994. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  10995. {
  10996. int tid, i;
  10997. nullpo_retv(sd);
  10998. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  10999. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  11000. if (max > MAX_SPIRITCHARM)
  11001. max = MAX_SPIRITCHARM;
  11002. if (sd->spiritcharm < 0)
  11003. sd->spiritcharm = 0;
  11004. if (sd->spiritcharm && sd->spiritcharm >= max) {
  11005. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  11006. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  11007. sd->spiritcharm--;
  11008. if (sd->spiritcharm != 0)
  11009. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  11010. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11011. }
  11012. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  11013. 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);
  11014. if (i != sd->spiritcharm)
  11015. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  11016. sd->spiritcharm_timer[i] = tid;
  11017. sd->spiritcharm++;
  11018. sd->spiritcharm_type = type;
  11019. clif_spiritcharm(sd);
  11020. }
  11021. /**
  11022. * Removes one or more spirit charms.
  11023. * @param sd: The target character
  11024. * @param count: Amount of charms to remove
  11025. * @param type: Type of charm to remove
  11026. */
  11027. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  11028. {
  11029. int i;
  11030. nullpo_retv(sd);
  11031. if (sd->spiritcharm_type != type)
  11032. return;
  11033. if (sd->spiritcharm <= 0) {
  11034. sd->spiritcharm = 0;
  11035. return;
  11036. }
  11037. if (count <= 0)
  11038. return;
  11039. if (count > sd->spiritcharm)
  11040. count = sd->spiritcharm;
  11041. sd->spiritcharm -= count;
  11042. if (count > MAX_SPIRITCHARM)
  11043. count = MAX_SPIRITCHARM;
  11044. for (i = 0; i < count; i++) {
  11045. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  11046. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  11047. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11048. }
  11049. }
  11050. for (i = count; i < MAX_SPIRITCHARM; i++) {
  11051. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  11052. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11053. }
  11054. if (sd->spiritcharm <= 0)
  11055. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11056. clif_spiritcharm(sd);
  11057. }
  11058. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  11059. /**
  11060. * Renewal EXP/Item Drop rate modifier based on level penalty
  11061. * @param level_diff: Monster and Player level difference
  11062. * @param mob_class: Monster class
  11063. * @param mode: Monster mode
  11064. * @param type: 1 - EXP, 2 - Item Drop
  11065. * @return Penalty rate
  11066. */
  11067. uint16 pc_level_penalty_mod( struct map_session_data* sd, e_penalty_type type, std::shared_ptr<s_mob_db> mob, mob_data* md ){
  11068. // No player was attached, we don't use any modifier (100 = rates are not touched)
  11069. if( sd == nullptr ){
  11070. return 100;
  11071. }
  11072. if ((type == PENALTY_DROP && map_getmapflag(sd->bl.m, MF_NORENEWALDROPPENALTY)) || (type == PENALTY_EXP && map_getmapflag(sd->bl.m, MF_NORENEWALEXPPENALTY))) {
  11073. return 100;
  11074. }
  11075. int monster_level;
  11076. if( md != nullptr ){
  11077. monster_level = md->level;
  11078. mob = md->db;
  11079. }else if( mob != nullptr ){
  11080. monster_level = mob->lv;
  11081. }else{
  11082. return 100;
  11083. }
  11084. if( ( type == PENALTY_DROP || type == PENALTY_MVP_DROP ) && status_has_mode( &mob->status, MD_FIXEDITEMDROP ) ){
  11085. return 100;
  11086. }
  11087. int level_difference = monster_level - sd->status.base_level;
  11088. std::shared_ptr<s_penalty> penalty = penalty_db.find( type );
  11089. if( penalty != nullptr ){
  11090. return penalty->rate[ level_difference + MAX_LEVEL - 1 ];
  11091. }else{
  11092. return 100;
  11093. }
  11094. }
  11095. #endif
  11096. int pc_split_str(char *str,char **val,int num)
  11097. {
  11098. int i;
  11099. for (i=0; i<num && str; i++){
  11100. val[i] = str;
  11101. str = strchr(str,',');
  11102. if (str && i<num-1) //Do not remove a trailing comma.
  11103. *str++=0;
  11104. }
  11105. return i;
  11106. }
  11107. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  11108. {
  11109. static int warning=0;
  11110. int i,j;
  11111. for (i=0; i<max; i++) {
  11112. double f;
  11113. if (!str) break;
  11114. f = atof(str);
  11115. if (f < 0)
  11116. val[i] = 0;
  11117. else if (f > UINT_MAX) {
  11118. val[i] = UINT_MAX;
  11119. if (!warning) {
  11120. warning = 1;
  11121. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  11122. }
  11123. } else
  11124. val[i] = (unsigned int)f;
  11125. str = strchr(str,sep);
  11126. if (str)
  11127. *str++=0;
  11128. }
  11129. //Zero up the remaining.
  11130. for(j=i; j < max; j++)
  11131. val[j] = 0;
  11132. return i;
  11133. }
  11134. std::shared_ptr<s_skill_tree_entry> SkillTreeDatabase::get_skill_data(int class_, uint16 skill_id) {
  11135. std::shared_ptr<s_skill_tree> tree = this->find(class_);
  11136. if (tree != nullptr)
  11137. return util::umap_find(tree->skills, skill_id);
  11138. return nullptr;
  11139. }
  11140. const std::string SkillTreeDatabase::getDefaultLocation() {
  11141. return std::string(db_path) + "/skill_tree.yml";
  11142. }
  11143. /**
  11144. * Reads and parses an entry from the skill_tree.
  11145. * @param node: YAML node containing the entry.
  11146. * @return count of successfully parsed rows
  11147. */
  11148. uint64 SkillTreeDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11149. std::string job_name;
  11150. if (!this->asString(node, "Job", job_name))
  11151. return 0;
  11152. int64 constant;
  11153. std::string job_name_constant = "JOB_" + job_name;
  11154. if (!script_get_constant(job_name_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11155. this->invalidWarning(node["Job"], "Invalid job %s.\n", job_name.c_str());
  11156. return 0;
  11157. }
  11158. uint16 job_id = static_cast<uint16>(constant);
  11159. std::shared_ptr<s_skill_tree> tree = this->find(job_id);
  11160. bool exists = tree != nullptr;
  11161. if (!exists)
  11162. tree = std::make_shared<s_skill_tree>();
  11163. if (this->nodeExists(node, "Inherit")) {
  11164. const ryml::NodeRef& InheritNode = node["Inherit"];
  11165. for (const auto &Inheritit : InheritNode) {
  11166. std::string inheritname;
  11167. c4::from_chars(Inheritit.key(), &inheritname);
  11168. std::string inheritname_constant = "JOB_" + inheritname;
  11169. if (!script_get_constant(inheritname_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11170. this->invalidWarning(InheritNode[Inheritit.key()], "Invalid job %s.\n", inheritname.c_str());
  11171. return 0;
  11172. }
  11173. bool active;
  11174. if (!this->asBool(InheritNode, inheritname, active))
  11175. return 0;
  11176. uint16 inherit_job = static_cast<uint16>(constant);
  11177. if (!active) {
  11178. if (exists)
  11179. util::vector_erase_if_exists(tree->inherit_job, inherit_job);
  11180. }
  11181. else {
  11182. if (!util::vector_exists(tree->inherit_job, inherit_job))
  11183. tree->inherit_job.push_back(inherit_job);
  11184. }
  11185. }
  11186. }
  11187. if (this->nodeExists(node, "Tree")) {
  11188. for (const auto &it : node["Tree"]) {
  11189. std::string skill_name;
  11190. if (!this->asString(it, "Name", skill_name))
  11191. return 0;
  11192. uint16 skill_id = skill_name2id(skill_name.c_str());
  11193. if (skill_id == 0) {
  11194. this->invalidWarning(it["Name"], "Invalid skill name \"%s\".\n", skill_name.c_str());
  11195. return 0;
  11196. }
  11197. if (!skill_get_index(skill_id)) {
  11198. this->invalidWarning(it["Name"], "Unable to load skill %s into job %hu's tree.\n", skill_name.c_str(), job_id);
  11199. return 0;
  11200. }
  11201. std::shared_ptr<s_skill_tree_entry> entry;
  11202. bool skill_exists = tree->skills.count(skill_id) > 0;
  11203. if (skill_exists)
  11204. entry = tree->skills[skill_id];
  11205. else
  11206. entry = std::make_shared<s_skill_tree_entry>();
  11207. entry->skill_id = skill_id;
  11208. uint16 max_lv;
  11209. if (!this->asUInt16(it, "MaxLevel", max_lv))
  11210. return 0;
  11211. if (max_lv > MAX_SKILL_LEVEL) {
  11212. this->invalidWarning(it["MaxLevel"], "MaxLevel exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11213. return 0;
  11214. }
  11215. uint16 skill_lv_max = skill_get_max(skill_id);
  11216. if (max_lv > skill_lv_max) {
  11217. 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);
  11218. max_lv = skill_lv_max;
  11219. }
  11220. // if (max_lv == 0) { // skill lvl 0 removed on loadingFinished (because of inherit)
  11221. // if (!skill_exists || entry->skill_id.erase(skill_id) == 0)
  11222. // 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());
  11223. // continue;
  11224. // }
  11225. entry->max_lv = max_lv;
  11226. if (this->nodeExists(it, "BaseLevel")) {
  11227. uint32 baselv;
  11228. if (!this->asUInt32(it, "BaseLevel", baselv))
  11229. return 0;
  11230. uint32 baselv_max = job_db.get_maxBaseLv(job_id);
  11231. if (baselv > baselv_max) {
  11232. this->invalidWarning(it["BaseLevel"], "Skill %hu's base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11233. skill_id, baselv, job_name.c_str(), baselv_max);
  11234. baselv = baselv_max;
  11235. }
  11236. entry->baselv = baselv;
  11237. } else {
  11238. if (!skill_exists)
  11239. entry->baselv = 0;
  11240. }
  11241. if (this->nodeExists(it, "JobLevel")) {
  11242. uint32 joblv;
  11243. if (!this->asUInt32(it, "JobLevel", joblv))
  11244. return 0;
  11245. uint32 joblv_max = job_db.get_maxJobLv(job_id);
  11246. if (joblv > joblv_max) {
  11247. this->invalidWarning(it["JobLevel"], "Skill %hu's job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11248. skill_id, joblv, job_name.c_str(), joblv_max);
  11249. joblv = joblv_max;
  11250. }
  11251. entry->joblv = joblv;
  11252. } else {
  11253. if (!skill_exists)
  11254. entry->joblv = 0;
  11255. }
  11256. if (this->nodeExists(it, "Requires")) {
  11257. for (const auto &Requiresit : it["Requires"]) {
  11258. if (!this->nodesExist(Requiresit, { "Name" }))
  11259. return 0;
  11260. std::string skill_name_req;
  11261. if (!this->asString(Requiresit, "Name", skill_name_req))
  11262. return 0;
  11263. uint16 skill_id_req = skill_name2id(skill_name_req.c_str());
  11264. if (skill_id_req == 0) {
  11265. this->invalidWarning(Requiresit["Name"], "Invalid skill name \"%s\".\n", skill_name_req.c_str());
  11266. return 0;
  11267. }
  11268. uint16 lv_req;
  11269. if (!this->asUInt16(Requiresit, "Level", lv_req))
  11270. return 0;
  11271. if (lv_req > MAX_SKILL_LEVEL) {
  11272. this->invalidWarning(Requiresit["Level"], "Level exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11273. return 0;
  11274. }
  11275. uint16 lv_req_max = skill_get_max(skill_id_req);
  11276. if (lv_req > lv_req_max) {
  11277. 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);
  11278. lv_req = lv_req_max;
  11279. }
  11280. if (lv_req == 0) {
  11281. if (entry->need.erase(skill_id_req) == 0)
  11282. 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());
  11283. continue;
  11284. }
  11285. entry->need[skill_id_req] = lv_req;
  11286. }
  11287. }
  11288. if (this->nodeExists(it, "Exclude")) {
  11289. bool exclude;
  11290. if (!this->asBool(it, "Exclude", exclude))
  11291. return 0;
  11292. entry->exclude_inherit = exclude;
  11293. } else {
  11294. if (!skill_exists)
  11295. entry->exclude_inherit = false;
  11296. }
  11297. if (!skill_exists)
  11298. tree->skills.insert({ skill_id, entry });
  11299. }
  11300. }
  11301. if (!exists)
  11302. this->put(job_id, tree);
  11303. return true;
  11304. }
  11305. void SkillTreeDatabase::loadingFinished() {
  11306. std::unordered_map<uint16, std::shared_ptr<s_skill_tree>> job_tree; // get the data from skill_tree_db before populate it
  11307. for (auto &data : *this) {
  11308. if (data.second->inherit_job.empty())
  11309. continue;
  11310. std::shared_ptr<s_skill_tree> skill_tree = std::make_shared<s_skill_tree>();
  11311. uint32 baselv_max = job_db.get_maxBaseLv(data.first);
  11312. uint32 joblv_max = job_db.get_maxJobLv(data.first);
  11313. for (const auto &inherit_job : data.second->inherit_job) {
  11314. std::shared_ptr<s_skill_tree> tree = this->find(inherit_job);
  11315. if (tree == nullptr || tree->skills.empty())
  11316. continue;
  11317. for (const auto &it : tree->skills) {
  11318. if (it.second->exclude_inherit)
  11319. continue;
  11320. if (data.second->skills.count(it.first) > 0) // skill already in the skill tree
  11321. continue;
  11322. if (skill_tree->skills.count(it.first) > 0) // replaced by the last inheritance
  11323. skill_tree->skills[it.first] = it.second;
  11324. else
  11325. skill_tree->skills.insert({ it.first, it.second });
  11326. std::shared_ptr<s_skill_tree_entry> skill = skill_tree->skills[it.first];
  11327. if (skill->baselv > baselv_max) {
  11328. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11329. skill_get_name(skill->skill_id), skill->skill_id, skill->baselv, job_name(data.first), baselv_max);
  11330. skill->baselv = baselv_max;
  11331. }
  11332. if (skill->joblv > joblv_max) {
  11333. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11334. skill_get_name(skill->skill_id), skill->skill_id, skill->joblv, job_name(data.first), joblv_max);
  11335. skill->joblv = joblv_max;
  11336. }
  11337. }
  11338. }
  11339. if (skill_tree != nullptr && !skill_tree->skills.empty())
  11340. job_tree.insert({ data.first, skill_tree });
  11341. }
  11342. if (!job_tree.empty()) {
  11343. for (auto &data : *this) {
  11344. if (job_tree.count(data.first) == 0)
  11345. continue;
  11346. data.second->skills.insert(job_tree[data.first]->skills.begin(), job_tree[data.first]->skills.end());
  11347. }
  11348. }
  11349. // remove skills with max_lv = 0
  11350. for (const auto &job : *this) {
  11351. if (job.second->skills.empty())
  11352. continue;
  11353. auto it = job.second->skills.begin();
  11354. while( it != job.second->skills.end() ){
  11355. if( it->second->max_lv == 0 ){
  11356. it = job.second->skills.erase( it );
  11357. }else{
  11358. it++;
  11359. }
  11360. }
  11361. }
  11362. TypesafeYamlDatabase::loadingFinished();
  11363. }
  11364. /**
  11365. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  11366. * @param level: Base level of player
  11367. * @param job_id: Job ID @see enum e_job
  11368. * @return base_hp
  11369. * @author [Cydh]
  11370. */
  11371. static unsigned int pc_calc_basehp(uint16 level, uint16 job_id) {
  11372. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11373. double base_hp = 35 + level * (job->hp_increase / 100.);
  11374. #ifndef RENEWAL
  11375. if (level >= 10 && (job_id == JOB_NINJA || job_id == JOB_GUNSLINGER))
  11376. base_hp += 90;
  11377. #endif
  11378. for (uint16 i = 2; i <= level; i++)
  11379. base_hp += floor(((job->hp_factor / 100.) * i) + 0.5); //Don't have round()
  11380. if (job_id == JOB_SUMMONER || job_id == JOB_SPIRIT_HANDLER)
  11381. base_hp += floor((base_hp / 2) + 0.5);
  11382. return (unsigned int)base_hp;
  11383. }
  11384. /**
  11385. * Calculates base sp of player.
  11386. * @param level: Base level of player
  11387. * @param job_id: Job ID @see enum e_job
  11388. * @return base_sp
  11389. * @author [Playtester]
  11390. */
  11391. static unsigned int pc_calc_basesp(uint16 level, uint16 job_id) {
  11392. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11393. double base_sp = 10 + floor(level * (job->sp_increase / 100.));
  11394. switch (job_id) {
  11395. case JOB_NINJA:
  11396. if (level >= 10)
  11397. base_sp -= 22;
  11398. else
  11399. base_sp = 11 + 3*level;
  11400. break;
  11401. case JOB_GUNSLINGER:
  11402. if (level > 10)
  11403. base_sp -= 18;
  11404. else
  11405. base_sp = 9 + 3*level;
  11406. break;
  11407. case JOB_SUMMONER:
  11408. case JOB_SPIRIT_HANDLER:
  11409. base_sp -= floor(base_sp / 2);
  11410. break;
  11411. }
  11412. return (unsigned int)base_sp;
  11413. }
  11414. const std::string JobDatabase::getDefaultLocation() {
  11415. return std::string(db_path) + "/job_stats.yml";
  11416. }
  11417. /**
  11418. * Reads and parses an entry from the job_db.
  11419. * @param node: YAML node containing the entry.
  11420. * @return count of successfully parsed rows
  11421. */
  11422. uint64 JobDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11423. if (this->nodeExists(node, "Jobs")) {
  11424. const ryml::NodeRef& jobsNode = node["Jobs"];
  11425. for (const auto &jobit : jobsNode) {
  11426. std::string job_name;
  11427. c4::from_chars(jobit.key(), &job_name);
  11428. std::string job_name_constant = "JOB_" + job_name;
  11429. int64 job_id;
  11430. if (!script_get_constant(job_name_constant.c_str(), &job_id)) {
  11431. this->invalidWarning(node["Job"], "Job %s does not exist.\n", job_name.c_str());
  11432. return 0;
  11433. }
  11434. std::shared_ptr<s_job_info> job = job_db.find(static_cast<uint16>(job_id));
  11435. bool exists = job != nullptr;
  11436. if (!exists) {
  11437. job = std::make_shared<s_job_info>();
  11438. job->job_bonus.resize(MAX_LEVEL);
  11439. std::fill(job->job_bonus.begin(), job->job_bonus.end(), std::array<uint16, PARAM_MAX> { 0 });
  11440. job->base_hp.resize(MAX_LEVEL);
  11441. std::fill(job->base_hp.begin(), job->base_hp.end(), 0);
  11442. job->base_sp.resize(MAX_LEVEL);
  11443. std::fill(job->base_sp.begin(), job->base_sp.end(), 0);
  11444. job->base_ap.resize(MAX_LEVEL);
  11445. std::fill(job->base_ap.begin(), job->base_ap.end(), 0);
  11446. }
  11447. if (this->nodeExists(node, "MaxWeight")) {
  11448. uint32 weight;
  11449. if (!this->asUInt32(node, "MaxWeight", weight))
  11450. return 0;
  11451. job->max_weight_base = weight;
  11452. } else {
  11453. if (!exists)
  11454. job->max_weight_base = 20000;
  11455. }
  11456. if (this->nodeExists(node, "HpFactor")) {
  11457. uint32 hp;
  11458. if (!this->asUInt32(node, "HpFactor", hp))
  11459. return 0;
  11460. job->hp_factor = hp;
  11461. } else {
  11462. if (!exists)
  11463. job->hp_factor = 0;
  11464. }
  11465. if (this->nodeExists(node, "HpIncrease")) {
  11466. uint32 hp;
  11467. if (!this->asUInt32(node, "HpIncrease", hp))
  11468. return 0;
  11469. job->hp_increase = hp;
  11470. } else {
  11471. if (!exists)
  11472. job->hp_increase = 500;
  11473. }
  11474. if (this->nodeExists(node, "SpIncrease")) {
  11475. uint32 sp;
  11476. if (!this->asUInt32(node, "SpIncrease", sp))
  11477. return 0;
  11478. job->sp_increase = sp;
  11479. } else {
  11480. if (!exists)
  11481. job->sp_increase = 100;
  11482. }
  11483. if (this->nodeExists(node, "BaseASPD")) {
  11484. const ryml::NodeRef& aspdNode = node["BaseASPD"];
  11485. uint8 max = MAX_WEAPON_TYPE;
  11486. #ifdef RENEWAL // Renewal adds an extra column for shields
  11487. max += 1;
  11488. #endif
  11489. if (!exists) {
  11490. job->aspd_base.resize(max);
  11491. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11492. }
  11493. for (const auto &aspdit : aspdNode) {
  11494. std::string weapon;
  11495. c4::from_chars(aspdit.key(), &weapon);
  11496. std::string weapon_constant = "W_" + weapon;
  11497. int64 constant;
  11498. if (!script_get_constant(weapon_constant.c_str(), &constant)) {
  11499. this->invalidWarning(aspdNode["BaseASPD"], "Unknown weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11500. continue;
  11501. }
  11502. if (constant < W_FIST || constant > max) {
  11503. this->invalidWarning(aspdNode["BaseASPD"], "Invalid weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11504. continue;
  11505. }
  11506. int16 aspd;
  11507. if (!this->asInt16(aspdNode, weapon.c_str(), aspd))
  11508. return 0;
  11509. job->aspd_base[static_cast<int16>(constant)] = aspd;
  11510. }
  11511. } else {
  11512. if (!exists) {
  11513. uint8 max = MAX_WEAPON_TYPE;
  11514. #ifdef RENEWAL // Renewal adds an extra column for shields
  11515. max += 1;
  11516. #endif
  11517. job->aspd_base.resize(max);
  11518. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11519. }
  11520. }
  11521. if (this->nodeExists(node, "MaxStats")) {
  11522. const ryml::NodeRef& statNode = node["MaxStats"];
  11523. for (const auto &statit : statNode) {
  11524. std::string stat;
  11525. c4::from_chars(statit.key(), &stat);
  11526. std::string stat_constant = "PARAM_" + stat;
  11527. int64 constant;
  11528. if (!script_get_constant(stat_constant.c_str(), &constant)) {
  11529. this->invalidWarning(statNode["Bonus"], "Unknown max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11530. continue;
  11531. }
  11532. if (constant < PARAM_STR || constant >= PARAM_MAX) {
  11533. this->invalidWarning(statNode["Bonus"], "Invalid max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11534. continue;
  11535. }
  11536. uint16 max;
  11537. if (!this->asUInt16(statNode, stat.c_str(), max))
  11538. return 0;
  11539. job->max_param[constant] = max;
  11540. }
  11541. }
  11542. if (this->nodeExists(node, "MaxBaseLevel")) {
  11543. uint16 level;
  11544. if (!this->asUInt16(node, "MaxBaseLevel", level))
  11545. return 0;
  11546. if (level == 0 || level > MAX_LEVEL) {
  11547. this->invalidWarning(node["MaxBaseLevel"], "MaxBaseLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11548. level = MAX_LEVEL;
  11549. }
  11550. job->max_base_level = level;
  11551. } else {
  11552. if (!exists)
  11553. job->max_base_level = MAX_LEVEL;
  11554. }
  11555. if (this->nodeExists(node, "BaseExp")) {
  11556. for (const ryml::NodeRef& bexpNode : node["BaseExp"]) {
  11557. uint16 level;
  11558. if (!this->asUInt16(bexpNode, "Level", level))
  11559. return 0;
  11560. if (level > job->max_base_level)
  11561. continue;
  11562. if (level == 0 || level > MAX_LEVEL) {
  11563. this->invalidWarning(bexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11564. return 0;
  11565. }
  11566. if (this->nodeExists(bexpNode, "Exp")) {
  11567. t_exp exp;
  11568. if (!this->asUInt64(bexpNode, "Exp", exp))
  11569. return 0;
  11570. job->base_exp[level - 1] = exp;
  11571. }
  11572. }
  11573. }
  11574. if (this->nodeExists(node, "MaxJobLevel")) {
  11575. uint16 level;
  11576. if (!this->asUInt16(node, "MaxJobLevel", level))
  11577. return 0;
  11578. if (level == 0 || level > MAX_LEVEL) {
  11579. this->invalidWarning(node["MaxJobLevel"], "MaxJobLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11580. level = MAX_LEVEL;
  11581. }
  11582. job->max_job_level = level;
  11583. } else {
  11584. if (!exists)
  11585. job->max_job_level = MAX_LEVEL;
  11586. }
  11587. if (this->nodeExists(node, "JobExp")) {
  11588. for (const ryml::NodeRef& jexpNode : node["JobExp"]) {
  11589. uint16 level;
  11590. if (!this->asUInt16(jexpNode, "Level", level))
  11591. return 0;
  11592. if (level > job->max_job_level)
  11593. continue;
  11594. if (level == 0 || level > MAX_LEVEL) {
  11595. this->invalidWarning(jexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11596. return 0;
  11597. }
  11598. if (this->nodeExists(jexpNode, "Exp")) {
  11599. t_exp exp;
  11600. if (!this->asUInt64(jexpNode, "Exp", exp))
  11601. return 0;
  11602. job->job_exp[level - 1] = exp;
  11603. }
  11604. }
  11605. }
  11606. if (this->nodeExists(node, "BonusStats")) {
  11607. const ryml::NodeRef& bonusNode = node["BonusStats"];
  11608. for (const ryml::NodeRef& levelNode : bonusNode) {
  11609. uint16 level;
  11610. if (!this->asUInt16(levelNode, "Level", level))
  11611. return 0;
  11612. if (level == 0 || level > MAX_LEVEL) {
  11613. this->invalidWarning(levelNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11614. return 0;
  11615. }
  11616. for (uint8 idx = PARAM_STR; idx < PARAM_MAX; idx++) {
  11617. if (this->nodeExists(levelNode, parameter_names[idx])) {
  11618. int16 change;
  11619. if (!this->asInt16(levelNode, parameter_names[idx], change))
  11620. return 0;
  11621. job->job_bonus[level - 1][idx] = change;
  11622. }
  11623. }
  11624. }
  11625. }
  11626. #ifdef HP_SP_TABLES
  11627. if (this->nodeExists(node, "BaseHp")) {
  11628. for (const ryml::NodeRef& bhpNode : node["BaseHp"]) {
  11629. uint16 level;
  11630. if (!this->asUInt16(bhpNode, "Level", level))
  11631. return 0;
  11632. if (level > job->max_base_level)
  11633. continue;
  11634. if (level == 0 || level > MAX_LEVEL) {
  11635. this->invalidWarning(bhpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11636. return 0;
  11637. }
  11638. if (this->nodeExists(bhpNode, "Hp")) {
  11639. uint32 points;
  11640. if (!this->asUInt32(bhpNode, "Hp", points))
  11641. return 0;
  11642. job->base_hp[level - 1] = points;
  11643. }
  11644. }
  11645. }
  11646. if (this->nodeExists(node, "BaseSp")) {
  11647. for (const ryml::NodeRef& bspNode : node["BaseSp"]) {
  11648. uint16 level;
  11649. if (!this->asUInt16(bspNode, "Level", level))
  11650. return 0;
  11651. if (level > job->max_base_level)
  11652. continue;
  11653. if (level == 0 || level > MAX_LEVEL) {
  11654. this->invalidWarning(bspNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11655. return 0;
  11656. }
  11657. if (this->nodeExists(bspNode, "Sp")) {
  11658. uint32 points;
  11659. if (!this->asUInt32(bspNode, "Sp", points))
  11660. return 0;
  11661. job->base_sp[level - 1] = points;
  11662. }
  11663. }
  11664. }
  11665. if (this->nodeExists(node, "BaseAp")) {
  11666. for (const ryml::NodeRef& bapNode : node["BaseAp"]) {
  11667. uint16 level;
  11668. if (!this->asUInt16(bapNode, "Level", level))
  11669. return 0;
  11670. if (level > job->max_base_level)
  11671. continue;
  11672. if (level == 0 || level > MAX_LEVEL) {
  11673. this->invalidWarning(bapNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11674. return 0;
  11675. }
  11676. if (this->nodeExists(bapNode, "Ap")) {
  11677. uint32 points;
  11678. if (!this->asUInt32(bapNode, "Ap", points))
  11679. return 0;
  11680. job->base_ap[level - 1] = points;
  11681. }
  11682. }
  11683. }
  11684. #endif
  11685. if (!exists)
  11686. this->put(static_cast<uint16>(job_id), job);
  11687. }
  11688. }
  11689. return 1;
  11690. }
  11691. void JobDatabase::loadingFinished() {
  11692. // Checking if all class have their data
  11693. for (auto &jobIt : *this) {
  11694. uint16 job_id = jobIt.first;
  11695. if (!pcdb_checkid(job_id))
  11696. continue;
  11697. 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)
  11698. continue; // Classes that do not need exp tables.
  11699. std::shared_ptr<s_job_info> job = jobIt.second;
  11700. uint16 maxBaseLv = job->max_base_level, maxJobLv = job->max_job_level;
  11701. if (!maxBaseLv)
  11702. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(job_id), job_id);
  11703. if (!maxJobLv)
  11704. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(job_id), job_id);
  11705. // Init and checking the empty value of Base HP/SP [Cydh]
  11706. if (job->base_hp.empty())
  11707. job->base_hp.resize(maxBaseLv);
  11708. for (uint16 j = 0; j < maxBaseLv; j++) {
  11709. if (job->base_hp[j] == 0)
  11710. job->base_hp[j] = pc_calc_basehp(j + 1, job_id);
  11711. }
  11712. if (job->base_sp.empty())
  11713. job->base_sp.resize(maxBaseLv);
  11714. for (uint16 j = 0; j < maxBaseLv; j++) {
  11715. if (job->base_sp[j] == 0)
  11716. job->base_sp[j] = pc_calc_basesp(j + 1, job_id);
  11717. }
  11718. // Resize to the maximum base level
  11719. if (job->base_hp.capacity() > maxBaseLv)
  11720. job->base_hp.erase(job->base_hp.begin() + maxBaseLv, job->base_hp.end());
  11721. if (job->base_sp.capacity() > maxBaseLv)
  11722. job->base_sp.erase(job->base_sp.begin() + maxBaseLv, job->base_sp.end());
  11723. if (job->base_ap.capacity() > maxBaseLv)
  11724. job->base_ap.erase(job->base_ap.begin() + maxBaseLv, job->base_ap.end());
  11725. // Resize to the maximum job level
  11726. if (job->job_bonus.capacity() > maxJobLv)
  11727. job->job_bonus.erase(job->job_bonus.begin() + maxJobLv, job->job_bonus.end());
  11728. for (uint16 parameter = PARAM_STR; parameter < PARAM_MAX; parameter++) {
  11729. // Store total
  11730. int16 current = 0;
  11731. for (uint16 job_level = 0; job_level < maxJobLv; job_level++) {
  11732. // Add the bonus from this job level
  11733. current += job->job_bonus[job_level][parameter];
  11734. // Set the new total on this job level
  11735. job->job_bonus[job_level][parameter] = current;
  11736. }
  11737. }
  11738. uint64 class_ = pc_jobid2mapid( job_id );
  11739. // Set normal status limits
  11740. uint16 max = battle_config.max_parameter;
  11741. do{
  11742. // Always check babies first
  11743. if( class_ & JOBL_BABY ){
  11744. if( class_ & JOBL_THIRD ){
  11745. max = battle_config.max_baby_third_parameter;
  11746. break;
  11747. }else{
  11748. max = battle_config.max_baby_parameter;
  11749. break;
  11750. }
  11751. }
  11752. // Summoner
  11753. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11754. max = battle_config.max_summoner_parameter;
  11755. break;
  11756. }
  11757. // Extended classes
  11758. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11759. max = battle_config.max_extended_parameter;
  11760. break;
  11761. }
  11762. if( class_ & JOBL_FOURTH ){
  11763. max = battle_config.max_fourth_parameter;
  11764. break;
  11765. }
  11766. // 3rd class
  11767. if( class_ & JOBL_THIRD ){
  11768. // Transcendent
  11769. if( class_ & JOBL_UPPER ){
  11770. max = battle_config.max_third_trans_parameter;
  11771. break;
  11772. }else{
  11773. max = battle_config.max_third_parameter;
  11774. break;
  11775. }
  11776. }
  11777. // Transcendent
  11778. if( class_ & JOBL_UPPER ){
  11779. max = battle_config.max_trans_parameter;
  11780. break;
  11781. }
  11782. }while( false );
  11783. for( uint16 parameter = PARAM_STR; parameter < PARAM_POW; parameter++ ){
  11784. // If it is not explicitly set in the database file
  11785. if( job->max_param[parameter] == 0 ){
  11786. job->max_param[parameter] = max;
  11787. }
  11788. }
  11789. // Set trait status limit
  11790. if( class_ & JOBL_FOURTH ){
  11791. max = battle_config.max_trait_parameter;
  11792. }else{
  11793. max = 0;
  11794. }
  11795. for( uint16 parameter = PARAM_POW; parameter < PARAM_MAX; parameter++ ){
  11796. // If it is not explicitly set in the database file
  11797. if( job->max_param[parameter] == 0 ){
  11798. job->max_param[parameter] = max;
  11799. }
  11800. }
  11801. }
  11802. TypesafeCachedYamlDatabase::loadingFinished();
  11803. }
  11804. /**
  11805. * Read job_noenter_map.txt
  11806. **/
  11807. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  11808. int class_ = -1;
  11809. int64 class_tmp;
  11810. if (ISDIGIT(str[0][0])) {
  11811. class_ = atoi(str[0]);
  11812. } else {
  11813. if (!script_get_constant(str[0], &class_tmp)) {
  11814. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  11815. return false;
  11816. }
  11817. class_ = static_cast<int>(class_tmp);
  11818. }
  11819. if (!pcdb_checkid(class_)) {
  11820. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  11821. return false;
  11822. }
  11823. std::shared_ptr<s_job_info> job = job_db.find(class_);
  11824. if (job == nullptr) {
  11825. ShowError("pc_readdb_job_noenter_map: Job %d data not initialized.\n", class_);
  11826. return false;
  11827. }
  11828. job->noenter_map.zone = atoi(str[1]);
  11829. job->noenter_map.group_lv = atoi(str[2]);
  11830. return true;
  11831. }
  11832. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  11833. return std::string(db_path) + "/statpoint.yml";
  11834. }
  11835. uint64 PlayerStatPointDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11836. if (!this->nodesExist(node, { "Level", "Points" })) {
  11837. return 0;
  11838. }
  11839. uint16 level;
  11840. if (!this->asUInt16(node, "Level", level))
  11841. return 0;
  11842. uint32 point;
  11843. if (!this->asUInt32(node, "Points", point))
  11844. return 0;
  11845. if (level == 0) {
  11846. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  11847. return 0;
  11848. }
  11849. if (level > MAX_LEVEL) {
  11850. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  11851. return 0;
  11852. }
  11853. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  11854. bool exists = entry != nullptr;
  11855. if( !exists ){
  11856. entry = std::make_shared<s_statpoint_entry>();
  11857. entry->level = level;
  11858. entry->statpoints = point;
  11859. }
  11860. if( this->nodeExists( node, "TraitPoints" ) ){
  11861. uint32 traitpoints;
  11862. if( !this->asUInt32( node, "TraitPoints", traitpoints ) ){
  11863. return 0;
  11864. }
  11865. entry->traitpoints = traitpoints;
  11866. }else{
  11867. if( !exists ){
  11868. entry->traitpoints = 0;
  11869. }
  11870. }
  11871. if( !exists ){
  11872. this->put( level, entry );
  11873. }
  11874. return 1;
  11875. }
  11876. /**
  11877. * Generate the remaining parts of the db if necessary.
  11878. */
  11879. void PlayerStatPointDatabase::loadingFinished(){
  11880. const uint16 trait_start_level = 200;
  11881. std::shared_ptr<s_statpoint_entry> level_one = this->find( 1 );
  11882. if( level_one == nullptr ){
  11883. if( battle_config.use_statpoint_table ){
  11884. ShowError( "Missing status points for Level 1\n" );
  11885. }
  11886. level_one = std::make_shared<s_statpoint_entry>();
  11887. level_one->level = 1;
  11888. level_one->statpoints = inter_config.start_status_points;
  11889. level_one->traitpoints = 0;
  11890. this->put( 1, level_one );
  11891. }else if( battle_config.use_statpoint_table ){
  11892. if( level_one->statpoints != inter_config.start_status_points ){
  11893. ShowError( "Status points for Level 1 (=%u) do not match inter_athena.conf value (=%u).\n", level_one->statpoints, inter_config.start_status_points );
  11894. level_one->statpoints = inter_config.start_status_points;
  11895. }
  11896. }else{
  11897. level_one->statpoints = inter_config.start_status_points;
  11898. level_one->traitpoints = 0;
  11899. }
  11900. std::shared_ptr<s_statpoint_entry> last_level = level_one;
  11901. for( uint16 level = 2; level <= MAX_LEVEL; level++ ){
  11902. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  11903. bool exists = entry != nullptr;
  11904. if( !exists ){
  11905. entry = std::make_shared<s_statpoint_entry>();
  11906. entry->level = level;
  11907. this->put( level, entry );
  11908. }
  11909. if( !battle_config.use_statpoint_table || !exists ){
  11910. if( battle_config.use_statpoint_table ){
  11911. ShowError("Missing status points for Level %hu\n", level);
  11912. }
  11913. if( level <= trait_start_level ){
  11914. entry->statpoints = last_level->statpoints + ( ( level - 1 + 15 ) / 5 );
  11915. }else{
  11916. entry->statpoints = last_level->statpoints;
  11917. }
  11918. }
  11919. if( !battle_config.use_traitpoint_table || !exists ){
  11920. if( battle_config.use_traitpoint_table && level > trait_start_level ){
  11921. ShowError( "Missing trait points for Level %hu\n", level );
  11922. }
  11923. if( level > trait_start_level ){
  11924. entry->traitpoints = ( level - trait_start_level ) * 3 + ( level - trait_start_level ) / 5 * 4;
  11925. }else{
  11926. entry->traitpoints = 0;
  11927. }
  11928. }
  11929. // Store it for next iteration
  11930. last_level = entry;
  11931. }
  11932. TypesafeCachedYamlDatabase::loadingFinished();
  11933. }
  11934. /*==========================================
  11935. * pc DB reading.
  11936. * job_stats.yml - Job values
  11937. * skill_tree.txt - skill tree for every class
  11938. * attr_fix.yml - elemental adjustment table
  11939. *------------------------------------------*/
  11940. void pc_readdb(void) {
  11941. int i, s = 1;
  11942. const char* dbsubpath[] = {
  11943. "",
  11944. "/" DBIMPORT,
  11945. //add other path here
  11946. };
  11947. //reset
  11948. job_db.clear(); // job_info table
  11949. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  11950. penalty_db.load();
  11951. #endif
  11952. statpoint_db.clear();
  11953. job_db.load();
  11954. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  11955. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  11956. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  11957. char* dbsubpath1 = (char*)aMalloc(n1+1);
  11958. char* dbsubpath2 = (char*)aMalloc(n2+1);
  11959. if(i==0) {
  11960. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  11961. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  11962. }
  11963. else {
  11964. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  11965. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  11966. }
  11967. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  11968. aFree(dbsubpath1);
  11969. aFree(dbsubpath2);
  11970. }
  11971. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  11972. skill_tree_db.reload();
  11973. statpoint_db.load();
  11974. }
  11975. // Read MOTD on startup. [Valaris]
  11976. int pc_read_motd(void)
  11977. {
  11978. FILE* fp;
  11979. // clear old MOTD
  11980. memset(motd_text, 0, sizeof(motd_text));
  11981. // read current MOTD
  11982. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  11983. {
  11984. unsigned int entries = 0;
  11985. char buf[CHAT_SIZE_MAX];
  11986. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  11987. {
  11988. unsigned int lines = 0;
  11989. size_t len;
  11990. lines++;
  11991. if( buf[0] == '/' && buf[1] == '/' )
  11992. continue;
  11993. len = strlen(buf);
  11994. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  11995. len--;
  11996. if( len ) {
  11997. char * ptr;
  11998. buf[len] = 0;
  11999. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  12000. 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);
  12001. }
  12002. else {// empty line
  12003. buf[0] = ' ';
  12004. buf[1] = 0;
  12005. }
  12006. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  12007. entries++;
  12008. }
  12009. fclose(fp);
  12010. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  12011. }
  12012. else
  12013. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  12014. return 0;
  12015. }
  12016. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  12017. int i,cursor = 0;
  12018. struct item_cd* cd = NULL;
  12019. if( load ) {
  12020. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  12021. // no item cooldown is associated with this character
  12022. return;
  12023. }
  12024. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12025. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  12026. sd->item_delay[cursor].tick = cd->tick[i];
  12027. sd->item_delay[cursor].nameid = cd->nameid[i];
  12028. cursor++;
  12029. }
  12030. }
  12031. idb_remove(itemcd_db,sd->status.char_id);
  12032. } else {
  12033. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  12034. // create a new skill cooldown object for map storage
  12035. CREATE( cd, struct item_cd, 1 );
  12036. idb_put( itemcd_db, sd->status.char_id, cd );
  12037. }
  12038. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12039. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  12040. cd->tick[cursor] = sd->item_delay[i].tick;
  12041. cd->nameid[cursor] = sd->item_delay[i].nameid;
  12042. cursor++;
  12043. }
  12044. }
  12045. }
  12046. return;
  12047. }
  12048. /**
  12049. * Add item delay to player's item delay data
  12050. * @param sd Player
  12051. * @param id Item data
  12052. * @param tick Current tick
  12053. * @param n Item index in inventory
  12054. * @return 0: No delay, can consume item.
  12055. * 1: Has delay, cancel consumption.
  12056. **/
  12057. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12058. int i;
  12059. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  12060. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  12061. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  12062. if( i < MAX_ITEMDELAYS ) {
  12063. if( sd->item_delay[i].nameid ) {// found
  12064. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  12065. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  12066. char e_msg[CHAT_SIZE_MAX];
  12067. if( e_tick > 99 )
  12068. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  12069. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  12070. else
  12071. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  12072. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  12073. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  12074. return 1; // Delay has not expired yet
  12075. }
  12076. } else {// not yet used item (all slots are initially empty)
  12077. sd->item_delay[i].nameid = id->nameid;
  12078. }
  12079. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  12080. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  12081. } else {// should not happen
  12082. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  12083. }
  12084. //clean up used delays so we can give room for more
  12085. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12086. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  12087. sd->item_delay[i].tick = 0;
  12088. sd->item_delay[i].nameid = 0;
  12089. }
  12090. }
  12091. return 0;
  12092. }
  12093. /**
  12094. * Check if player has delay to reuse item
  12095. * @param sd Player
  12096. * @param id Item data
  12097. * @param tick Current tick
  12098. * @param n Item index in inventory
  12099. * @return 0: No delay, can consume item.
  12100. * 1: Has delay, cancel consumption.
  12101. **/
  12102. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12103. struct status_change *sc = NULL;
  12104. nullpo_retr(0, sd);
  12105. nullpo_retr(0, id);
  12106. // Do normal delay assignment
  12107. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  12108. return pc_itemcd_add(sd, id, tick, n);
  12109. // Send reply of delay remains
  12110. if (sc->data[id->delay.sc]) {
  12111. const struct TimerData *timer = get_timer(sc->data[id->delay.sc]->timer);
  12112. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  12113. return 1;
  12114. }
  12115. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  12116. return 0;
  12117. }
  12118. /**
  12119. * Clear the dmglog data from player
  12120. * @param sd
  12121. * @param md
  12122. **/
  12123. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  12124. {
  12125. uint8 i;
  12126. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  12127. if (i < DAMAGELOG_SIZE) {
  12128. md->dmglog[i].id = 0;
  12129. md->dmglog[i].dmg = 0;
  12130. md->dmglog[i].flag = 0;
  12131. }
  12132. }
  12133. /**
  12134. * Add log to player's dmglog
  12135. * @param sd
  12136. * @param id Monster's GID
  12137. **/
  12138. void pc_damage_log_add(struct map_session_data *sd, int id)
  12139. {
  12140. uint8 i = 0;
  12141. if (!sd || !id)
  12142. return;
  12143. //Only store new data, don't need to renew the old one with same id
  12144. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  12145. if (i < DAMAGELOG_SIZE_PC)
  12146. return;
  12147. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12148. if (sd->dmglog[i] == 0) {
  12149. sd->dmglog[i] = id;
  12150. return;
  12151. }
  12152. }
  12153. }
  12154. /**
  12155. * Clear dmglog data from player
  12156. * @param sd
  12157. * @param id Monster's id
  12158. **/
  12159. void pc_damage_log_clear(struct map_session_data *sd, int id)
  12160. {
  12161. uint8 i;
  12162. struct mob_data *md = NULL;
  12163. if (!sd)
  12164. return;
  12165. if (!id) {
  12166. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12167. if( !sd->dmglog[i] ) //skip the empty value
  12168. continue;
  12169. if ((md = map_id2md(sd->dmglog[i])))
  12170. pc_clear_log_damage_sub(sd->status.char_id,md);
  12171. sd->dmglog[i] = 0;
  12172. }
  12173. }
  12174. else {
  12175. if ((md = map_id2md(id)))
  12176. pc_clear_log_damage_sub(sd->status.char_id,md);
  12177. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  12178. if (i < DAMAGELOG_SIZE_PC)
  12179. sd->dmglog[i] = 0;
  12180. }
  12181. }
  12182. /**
  12183. * Status change data arrived from char-server
  12184. * @param sd: Player data
  12185. */
  12186. void pc_scdata_received(struct map_session_data *sd) {
  12187. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  12188. clif_weight_limit( sd );
  12189. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  12190. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  12191. }
  12192. sd->state.pc_loaded = true;
  12193. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  12194. sd->state.connect_new = 1;
  12195. clif_parse_LoadEndAck(sd->fd, sd);
  12196. }
  12197. if (pc_iscarton(sd)) {
  12198. sd->cart_weight_max = 0; // Force a client refesh
  12199. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  12200. }
  12201. if (sd->sc.data[SC_SOULENERGY])
  12202. sd->soulball = sd->sc.data[SC_SOULENERGY]->val1;
  12203. }
  12204. /**
  12205. * Check player account expiration time and rental item expirations
  12206. * @param sd: Player data
  12207. */
  12208. void pc_check_expiration(struct map_session_data *sd) {
  12209. #ifndef ENABLE_SC_SAVING
  12210. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  12211. #endif
  12212. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  12213. time_t exp_time = sd->expiration_time;
  12214. char tmpstr[1024];
  12215. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  12216. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  12217. pc_expire_check(sd);
  12218. }
  12219. }
  12220. TIMER_FUNC(pc_expiration_timer){
  12221. struct map_session_data *sd = map_id2sd(id);
  12222. if( !sd ) return 0;
  12223. sd->expiration_tid = INVALID_TIMER;
  12224. if( sd->fd )
  12225. clif_authfail_fd(sd->fd,10);
  12226. map_quit(sd);
  12227. return 0;
  12228. }
  12229. TIMER_FUNC(pc_autotrade_timer){
  12230. struct map_session_data *sd = map_id2sd(id);
  12231. if (!sd)
  12232. return 0;
  12233. sd->autotrade_tid = INVALID_TIMER;
  12234. if (sd->state.autotrade&2)
  12235. vending_reopen(sd);
  12236. if (sd->state.autotrade&4)
  12237. buyingstore_reopen(sd);
  12238. if (!sd->vender_id && !sd->buyer_id) {
  12239. sd->state.autotrade = 0;
  12240. map_quit(sd);
  12241. }
  12242. return 0;
  12243. }
  12244. /* this timer exists only when a character with a expire timer > 24h is online */
  12245. /* it loops thru online players once an hour to check whether a new < 24h is available */
  12246. TIMER_FUNC(pc_global_expiration_timer){
  12247. struct s_mapiterator* iter;
  12248. struct map_session_data* sd;
  12249. iter = mapit_getallusers();
  12250. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  12251. if( sd->expiration_time )
  12252. pc_expire_check(sd);
  12253. mapit_free(iter);
  12254. return 0;
  12255. }
  12256. void pc_expire_check(struct map_session_data *sd) {
  12257. /* ongoing timer */
  12258. if( sd->expiration_tid != INVALID_TIMER )
  12259. return;
  12260. /* not within the next 24h, enable the global check */
  12261. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  12262. /* global check not running, enable */
  12263. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  12264. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  12265. return;
  12266. }
  12267. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  12268. }
  12269. /**
  12270. * Deposit some money to bank
  12271. * @param sd
  12272. * @param money Amount of money to deposit
  12273. **/
  12274. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  12275. unsigned int limit_check = money + sd->bank_vault;
  12276. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  12277. return BDA_OVERFLOW;
  12278. } else if ( money > sd->status.zeny ) {
  12279. return BDA_NO_MONEY;
  12280. }
  12281. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  12282. return BDA_NO_MONEY;
  12283. sd->bank_vault += money;
  12284. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12285. if( save_settings&CHARSAVE_BANK )
  12286. chrif_save(sd, CSAVE_NORMAL);
  12287. return BDA_SUCCESS;
  12288. }
  12289. /**
  12290. * Withdraw money from bank
  12291. * @param sd
  12292. * @param money Amount of money that will be withdrawn
  12293. **/
  12294. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  12295. unsigned int limit_check = money + sd->status.zeny;
  12296. if( money <= 0 ) {
  12297. return BWA_UNKNOWN_ERROR;
  12298. } else if ( money > sd->bank_vault ) {
  12299. return BWA_NO_MONEY;
  12300. } else if ( limit_check > MAX_ZENY ) {
  12301. /* no official response for this scenario exists. */
  12302. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  12303. return BWA_UNKNOWN_ERROR;
  12304. }
  12305. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  12306. return BWA_NO_MONEY;
  12307. sd->bank_vault -= money;
  12308. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12309. if( save_settings&CHARSAVE_BANK )
  12310. chrif_save(sd, CSAVE_NORMAL);
  12311. return BWA_SUCCESS;
  12312. }
  12313. /**
  12314. * Clear Crimson Marker data from caster
  12315. * @param sd: Player
  12316. **/
  12317. void pc_crimson_marker_clear(struct map_session_data *sd) {
  12318. uint8 i;
  12319. if (!sd)
  12320. return;
  12321. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  12322. struct block_list *bl = NULL;
  12323. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  12324. status_change_end(bl,SC_C_MARKER);
  12325. sd->c_marker[i] = 0;
  12326. }
  12327. }
  12328. /**
  12329. * Show version to player
  12330. * @param sd: Player
  12331. **/
  12332. void pc_show_version(struct map_session_data *sd) {
  12333. const char* svn = get_svn_revision();
  12334. char buf[CHAT_SIZE_MAX];
  12335. if( svn[0] != UNKNOWN_VERSION )
  12336. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  12337. else {
  12338. const char* git = get_git_hash();
  12339. if( git[0] != UNKNOWN_VERSION )
  12340. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  12341. else
  12342. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  12343. }
  12344. clif_displaymessage(sd->fd,buf);
  12345. }
  12346. /**
  12347. * Run bonus_script on player
  12348. * @param sd
  12349. * @author [Cydh]
  12350. **/
  12351. void pc_bonus_script(struct map_session_data *sd) {
  12352. t_tick now = gettick();
  12353. struct linkdb_node *node = NULL, *next = NULL;
  12354. if (!sd || !(node = sd->bonus_script.head))
  12355. return;
  12356. while (node) {
  12357. struct s_bonus_script_entry *entry = NULL;
  12358. next = node->next;
  12359. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  12360. // Only start timer for new bonus_script
  12361. if (entry->tid == INVALID_TIMER) {
  12362. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  12363. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  12364. entry->tick += now;
  12365. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  12366. }
  12367. if (entry->script)
  12368. run_script(entry->script, 0, sd->bl.id, 0);
  12369. else
  12370. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  12371. }
  12372. node = next;
  12373. }
  12374. }
  12375. /**
  12376. * Add bonus_script to player
  12377. * @param sd Player
  12378. * @param script_str Script string
  12379. * @param dur Duration in ms
  12380. * @param icon EFST
  12381. * @param flag Flags @see enum e_bonus_script_flags
  12382. * @param type 0 - None, 1 - Buff, 2 - Debuff
  12383. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  12384. * @author [Cydh]
  12385. **/
  12386. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, t_tick dur, enum efst_type icon, uint16 flag, uint8 type) {
  12387. struct script_code *script = NULL;
  12388. struct linkdb_node *node = NULL;
  12389. struct s_bonus_script_entry *entry = NULL;
  12390. if (!sd)
  12391. return NULL;
  12392. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  12393. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  12394. return NULL;
  12395. }
  12396. // Duplication checks
  12397. if ((node = sd->bonus_script.head)) {
  12398. while (node) {
  12399. entry = (struct s_bonus_script_entry *)node->data;
  12400. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  12401. t_tick newdur = gettick() + dur;
  12402. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  12403. settick_timer(entry->tid, newdur);
  12404. script_free_code(script);
  12405. return NULL;
  12406. }
  12407. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  12408. break;
  12409. else { // No duplicate bonus
  12410. script_free_code(script);
  12411. return NULL;
  12412. }
  12413. }
  12414. node = node->next;
  12415. }
  12416. }
  12417. CREATE(entry, struct s_bonus_script_entry, 1);
  12418. entry->script_buf = StringBuf_Malloc();
  12419. StringBuf_AppendStr(entry->script_buf, script_str);
  12420. entry->tid = INVALID_TIMER;
  12421. entry->flag = flag;
  12422. entry->icon = icon;
  12423. entry->tick = dur; // Use duration first, on run change to expire time
  12424. entry->type = type;
  12425. entry->script = script;
  12426. sd->bonus_script.count++;
  12427. return entry;
  12428. }
  12429. /**
  12430. * Remove bonus_script data from player
  12431. * @param sd: Target player
  12432. * @param list: Bonus script entry from player
  12433. * @author [Cydh]
  12434. **/
  12435. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  12436. if (entry->tid != INVALID_TIMER)
  12437. delete_timer(entry->tid, pc_bonus_script_timer);
  12438. if (entry->script)
  12439. script_free_code(entry->script);
  12440. if (entry->script_buf)
  12441. StringBuf_Free(entry->script_buf);
  12442. if (sd) {
  12443. if (entry->icon != EFST_BLANK)
  12444. clif_status_load(&sd->bl, entry->icon, 0);
  12445. if (sd->bonus_script.count > 0)
  12446. sd->bonus_script.count--;
  12447. }
  12448. aFree(entry);
  12449. }
  12450. /**
  12451. * Do final process if no entry left
  12452. * @param sd
  12453. **/
  12454. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  12455. if (sd->bonus_script.count == 0) {
  12456. if (sd->bonus_script.head && sd->bonus_script.head->data)
  12457. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  12458. linkdb_final(&sd->bonus_script.head);
  12459. }
  12460. }
  12461. /**
  12462. * Timer for bonus_script
  12463. * @param tid
  12464. * @param tick
  12465. * @param id
  12466. * @param data
  12467. * @author [Cydh]
  12468. **/
  12469. TIMER_FUNC(pc_bonus_script_timer){
  12470. struct map_session_data *sd;
  12471. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  12472. sd = map_id2sd(id);
  12473. if (!sd) {
  12474. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  12475. return 0;
  12476. }
  12477. if (tid == INVALID_TIMER)
  12478. return 0;
  12479. if (!sd->bonus_script.head || entry == NULL) {
  12480. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  12481. return 0;
  12482. }
  12483. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12484. pc_bonus_script_free_entry(sd, entry);
  12485. pc_bonus_script_check_final(sd);
  12486. status_calc_pc(sd,SCO_NONE);
  12487. return 0;
  12488. }
  12489. /**
  12490. * Check then clear all active timer(s) of bonus_script data from player based on reason
  12491. * @param sd: Target player
  12492. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  12493. * @author [Cydh]
  12494. **/
  12495. void pc_bonus_script_clear(struct map_session_data *sd, uint32 flag) {
  12496. struct linkdb_node *node = NULL;
  12497. uint16 count = 0;
  12498. if (!sd || !(node = sd->bonus_script.head))
  12499. return;
  12500. while (node) {
  12501. struct linkdb_node *next = node->next;
  12502. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  12503. if (entry && (
  12504. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  12505. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  12506. (flag&entry->flag) || // Matched flag
  12507. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  12508. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  12509. )
  12510. )
  12511. {
  12512. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12513. pc_bonus_script_free_entry(sd, entry);
  12514. count++;
  12515. }
  12516. node = next;
  12517. }
  12518. pc_bonus_script_check_final(sd);
  12519. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  12520. status_calc_pc(sd,SCO_NONE);
  12521. }
  12522. /** [Cydh]
  12523. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  12524. * @param sd: Target player
  12525. */
  12526. void pc_cell_basilica(struct map_session_data *sd) {
  12527. nullpo_retv(sd);
  12528. #ifdef RENEWAL
  12529. enum sc_type type = SC_BASILICA_CELL;
  12530. #else
  12531. enum sc_type type = SC_BASILICA;
  12532. #endif
  12533. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  12534. if (sd->sc.data[type])
  12535. status_change_end(&sd->bl, type);
  12536. }
  12537. else if (!sd->sc.data[type])
  12538. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  12539. }
  12540. /** [Cydh]
  12541. * Get maximum specified parameter for specified class
  12542. * @param sd: Player data
  12543. * @param param: Max parameter to check
  12544. * @return max_param
  12545. */
  12546. uint16 pc_maxparameter(struct map_session_data *sd, e_params param) {
  12547. nullpo_retr(0, sd);
  12548. std::shared_ptr<s_job_info> job = job_db.find(pc_mapid2jobid(sd->class_,sd->status.sex));
  12549. if( job == nullptr || param == PARAM_MAX ){
  12550. return 0;
  12551. }
  12552. return job->max_param[param];
  12553. }
  12554. /**
  12555. * Get max ASPD for player based on Class
  12556. * @param sd Player
  12557. * @return ASPD
  12558. */
  12559. short pc_maxaspd(struct map_session_data *sd) {
  12560. nullpo_ret(sd);
  12561. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  12562. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  12563. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  12564. battle_config.max_aspd ));
  12565. }
  12566. /**
  12567. * Calculates total item-group related bonuses for the given item
  12568. * @param sd Player
  12569. * @param nameid Item ID
  12570. * @return Heal rate
  12571. **/
  12572. short pc_get_itemgroup_bonus(struct map_session_data* sd, t_itemid nameid, std::vector<s_item_bonus>& bonuses) {
  12573. if (bonuses.empty())
  12574. return 0;
  12575. short bonus = 0;
  12576. for (const auto &it : bonuses) {
  12577. uint16 group_id = it.id;
  12578. if (group_id == 0)
  12579. continue;
  12580. if (itemdb_group.item_exists(group_id, nameid))
  12581. bonus += it.val;
  12582. }
  12583. return bonus;
  12584. }
  12585. /**
  12586. * Calculates total item-group related bonuses for the given item group
  12587. * @param sd Player
  12588. * @param group_id Item Group ID
  12589. * @return Heal rate
  12590. **/
  12591. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id, std::vector<s_item_bonus>& bonuses) {
  12592. if (bonuses.empty())
  12593. return 0;
  12594. for (const auto &it : bonuses) {
  12595. if (it.id == group_id)
  12596. return it.val;
  12597. }
  12598. return 0;
  12599. }
  12600. /**
  12601. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  12602. * @param eqi Item EQI of enum equip_index
  12603. * @param *equip_index Player's equip_index[]
  12604. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  12605. * @return True if item in same inventory index, False if doesn't
  12606. */
  12607. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  12608. if (index < 0 || index >= MAX_INVENTORY)
  12609. return true;
  12610. // Dual weapon checks
  12611. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  12612. return true;
  12613. // Headgear with Mid & Low location
  12614. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  12615. return true;
  12616. // Headgear with Top & Mid or Low location
  12617. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  12618. return true;
  12619. // Headgear with Mid & Low location
  12620. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  12621. return true;
  12622. // Headgear with Top & Mid or Low location
  12623. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  12624. return true;
  12625. return false;
  12626. }
  12627. /**
  12628. * Generate Unique item ID for player
  12629. * @param sd : Player
  12630. * @return A generated Unique item ID
  12631. */
  12632. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  12633. nullpo_ret(sd);
  12634. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  12635. }
  12636. /**
  12637. * Validating skill from player after logged on
  12638. * @param sd
  12639. **/
  12640. void pc_validate_skill(struct map_session_data *sd) {
  12641. if (sd) {
  12642. uint16 i = 0, count = 0;
  12643. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  12644. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  12645. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  12646. for (i = 0; i < MAX_SKILL; i++) {
  12647. uint16 idx = 0;
  12648. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  12649. continue;
  12650. if ((idx = skill_get_index(tmp_skills[i].id))) {
  12651. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  12652. count++;
  12653. }
  12654. else
  12655. 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);
  12656. }
  12657. }
  12658. }
  12659. /**
  12660. * Show available NPC Quest / Event Icon Check [Kisuka]
  12661. * @param sd Player
  12662. **/
  12663. void pc_show_questinfo(struct map_session_data *sd) {
  12664. #if PACKETVER >= 20090218
  12665. nullpo_retv(sd);
  12666. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12667. return;
  12668. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12669. nullpo_retv(mapdata);
  12670. if (mapdata->qi_npc.empty())
  12671. return;
  12672. if (mapdata->qi_npc.size() != sd->qi_display.size())
  12673. return; // init was not called yet
  12674. for (int i = 0; i < mapdata->qi_npc.size(); i++) {
  12675. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  12676. if (!nd || nd->qi_data.empty())
  12677. continue;
  12678. bool show = false;
  12679. for (auto &qi : nd->qi_data) {
  12680. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  12681. show = true;
  12682. // Check if need to be displayed
  12683. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  12684. sd->qi_display[i].is_active = true;
  12685. sd->qi_display[i].icon = qi->icon;
  12686. sd->qi_display[i].color = qi->color;
  12687. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  12688. }
  12689. break;
  12690. }
  12691. }
  12692. if (show == false) {
  12693. // Check if need to be hide
  12694. if (sd->qi_display[i].is_active) {
  12695. sd->qi_display[i].is_active = false;
  12696. sd->qi_display[i].icon = QTYPE_NONE;
  12697. sd->qi_display[i].color = QMARK_NONE;
  12698. #if PACKETVER >= 20120410
  12699. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  12700. #else
  12701. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  12702. #endif
  12703. }
  12704. }
  12705. }
  12706. #endif
  12707. }
  12708. /**
  12709. * Reinit the questinfo for player when changing map
  12710. * @param sd Player
  12711. **/
  12712. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  12713. #if PACKETVER >= 20090218
  12714. nullpo_retv(sd);
  12715. sd->qi_display.clear();
  12716. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12717. return;
  12718. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12719. nullpo_retv(mapdata);
  12720. if (mapdata->qi_npc.empty())
  12721. return;
  12722. sd->qi_display.reserve( mapdata->qi_npc.size() );
  12723. for( int i = 0; i < mapdata->qi_npc.size(); i++ ){
  12724. sd->qi_display.push_back( s_qi_display() );
  12725. }
  12726. #endif
  12727. }
  12728. /**
  12729. * Check if a job is allowed to enter the map
  12730. * @param jobid Job ID see enum e_job or sd->status.class_
  12731. * @param m ID -an index- for direct indexing map[] array
  12732. * @return 1 if job is allowed, 0 otherwise
  12733. **/
  12734. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  12735. // Map is other map server.
  12736. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  12737. if (m < 0)
  12738. return true;
  12739. struct map_data *mapdata = map_getmapdata(m);
  12740. if (!mapdata->cell)
  12741. return false;
  12742. if (!pcdb_checkid(jobid))
  12743. return false;
  12744. std::shared_ptr<s_job_info> job = job_db.find(jobid);
  12745. if (!job->noenter_map.zone || group_lv > job->noenter_map.group_lv)
  12746. return true;
  12747. if ((job->noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  12748. (job->noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  12749. (job->noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  12750. (job->noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  12751. (job->noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  12752. (job->noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  12753. )
  12754. return false;
  12755. return true;
  12756. }
  12757. /**
  12758. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  12759. * @param sd
  12760. **/
  12761. void pc_set_costume_view(struct map_session_data *sd) {
  12762. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  12763. struct item_data *id = NULL;
  12764. nullpo_retv(sd);
  12765. head_low = sd->status.head_bottom;
  12766. head_mid = sd->status.head_mid;
  12767. head_top = sd->status.head_top;
  12768. robe = sd->status.robe;
  12769. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  12770. //Added check to prevent sending the same look on multiple slots ->
  12771. //causes client to redraw item on top of itself. (suggested by Lupus)
  12772. // Normal headgear checks
  12773. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  12774. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  12775. sd->status.head_bottom = id->look;
  12776. else
  12777. sd->status.head_bottom = 0;
  12778. }
  12779. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  12780. if (!(id->equip&(EQP_HEAD_TOP)))
  12781. sd->status.head_mid = id->look;
  12782. else
  12783. sd->status.head_mid = 0;
  12784. }
  12785. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  12786. sd->status.head_top = id->look;
  12787. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  12788. sd->status.robe = id->look;
  12789. // Costumes check
  12790. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  12791. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  12792. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  12793. sd->status.head_bottom = id->look;
  12794. else
  12795. sd->status.head_bottom = 0;
  12796. }
  12797. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  12798. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  12799. sd->status.head_mid = id->look;
  12800. else
  12801. sd->status.head_mid = 0;
  12802. }
  12803. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  12804. sd->status.head_top = id->look;
  12805. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  12806. sd->status.robe = id->look;
  12807. }
  12808. if (sd->setlook_head_bottom)
  12809. sd->status.head_bottom = sd->setlook_head_bottom;
  12810. if (sd->setlook_head_mid)
  12811. sd->status.head_mid = sd->setlook_head_mid;
  12812. if (sd->setlook_head_top)
  12813. sd->status.head_top = sd->setlook_head_top;
  12814. if (sd->setlook_robe)
  12815. sd->status.robe = sd->setlook_robe;
  12816. if (head_low != sd->status.head_bottom)
  12817. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  12818. if (head_mid != sd->status.head_mid)
  12819. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  12820. if (head_top != sd->status.head_top)
  12821. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  12822. if (robe != sd->status.robe)
  12823. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  12824. }
  12825. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  12826. uint32 date = date_get(DT_YYYYMMDD);
  12827. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  12828. std::shared_ptr<s_attendance_period> period = pair.second;
  12829. if( period->start <= date && period->end >= date ){
  12830. return period;
  12831. }
  12832. }
  12833. return nullptr;
  12834. }
  12835. bool pc_attendance_enabled(){
  12836. // Check if the attendance feature is disabled
  12837. if( !battle_config.feature_attendance ){
  12838. return false;
  12839. }
  12840. // Check if there is a running attendance period
  12841. return pc_attendance_period() != nullptr;
  12842. }
  12843. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  12844. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  12845. }
  12846. int32 pc_attendance_counter( struct map_session_data* sd ){
  12847. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  12848. // No running attendance period
  12849. if( period == nullptr ){
  12850. return 0;
  12851. }
  12852. // Get the counter for the current period
  12853. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  12854. // Check if we have a remaining counter from a previous period
  12855. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  12856. // Reset the counter to zero
  12857. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  12858. return 0;
  12859. }
  12860. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  12861. }
  12862. void pc_attendance_claim_reward( struct map_session_data* sd ){
  12863. // If the user's group does not have the permission
  12864. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  12865. return;
  12866. }
  12867. // Check if the attendance feature is disabled
  12868. if( !pc_attendance_enabled() ){
  12869. return;
  12870. }
  12871. // Check if the user already got his reward today
  12872. if( pc_attendance_rewarded_today( sd ) ){
  12873. return;
  12874. }
  12875. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  12876. attendance_counter += 1;
  12877. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  12878. if( period == nullptr ){
  12879. return;
  12880. }
  12881. if( period->rewards.size() < attendance_counter ){
  12882. return;
  12883. }
  12884. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  12885. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  12886. if( save_settings&CHARSAVE_ATTENDANCE )
  12887. chrif_save(sd, CSAVE_NORMAL);
  12888. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  12889. struct mail_message msg;
  12890. memset( &msg, 0, sizeof( struct mail_message ) );
  12891. msg.dest_id = sd->status.char_id;
  12892. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  12893. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  12894. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  12895. msg.item[0].nameid = reward->item_id;
  12896. msg.item[0].amount = reward->amount;
  12897. msg.item[0].identify = 1;
  12898. msg.status = MAIL_NEW;
  12899. msg.type = MAIL_INBOX_NORMAL;
  12900. msg.timestamp = time(NULL);
  12901. intif_Mail_send(0, &msg);
  12902. clif_attendence_response( sd, attendance_counter );
  12903. }
  12904. /**
  12905. * Save a captcha image to memory via /macro_register.
  12906. * @param sd: Player data
  12907. * @param image_size: Captcha image size
  12908. * @param captcha_answer: Answer to captcha
  12909. */
  12910. void pc_macro_captcha_register(map_session_data &sd, uint16 image_size, char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  12911. nullpo_retv(captcha_answer);
  12912. sd.captcha_upload.cd = nullptr;
  12913. sd.captcha_upload.upload_size = 0;
  12914. if (strlen(captcha_answer) < 4 || image_size == 0 || image_size > CAPTCHA_BMP_SIZE) {
  12915. clif_captcha_upload_request(sd); // Notify client of failure.
  12916. return;
  12917. }
  12918. std::shared_ptr<s_captcha_data> cd = std::make_shared<s_captcha_data>();
  12919. sd.captcha_upload.cd = cd;
  12920. cd->image_size = image_size;
  12921. safestrncpy(cd->captcha_answer, captcha_answer, sizeof(cd->captcha_answer));
  12922. memset(cd->image_data, 0, sizeof(cd->image_data));
  12923. // Request the image data from the client.
  12924. clif_captcha_upload_request(sd);
  12925. }
  12926. /**
  12927. * Save captcha image to server.
  12928. * @param sd: Player data
  12929. * @param captcha_key: Captcha ID
  12930. * @param upload_size: Captcha size
  12931. * @param upload_data: Image data
  12932. */
  12933. void pc_macro_captcha_register_upload(map_session_data &sd, uint16 upload_size, char *upload_data) {
  12934. nullpo_retv(upload_data);
  12935. memcpy(&sd.captcha_upload.cd->image_data[sd.captcha_upload.upload_size], upload_data, upload_size);
  12936. sd.captcha_upload.upload_size += upload_size;
  12937. // Notify that the image finished uploading.
  12938. if (sd.captcha_upload.upload_size == sd.captcha_upload.cd->image_size) {
  12939. // Tell the client that the upload was finished
  12940. clif_captcha_upload_end(sd);
  12941. // Look for a free key
  12942. uint16 index;
  12943. for (index = 0; index < UINT16_MAX; index++) {
  12944. if (!captcha_db.exists(index)) {
  12945. break;
  12946. }
  12947. }
  12948. if (index == UINT16_MAX) {
  12949. // no free key found...
  12950. sd.captcha_upload.cd = nullptr;
  12951. sd.captcha_upload.upload_size = 0;
  12952. return;
  12953. }
  12954. captcha_db.put(index, sd.captcha_upload.cd);
  12955. sd.captcha_upload.cd = nullptr;
  12956. sd.captcha_upload.upload_size = 0;
  12957. // TODO: write YAML and BMP file?
  12958. }
  12959. }
  12960. /**
  12961. * Timer attached to target player with attempts to confirm captcha.
  12962. */
  12963. TIMER_FUNC(pc_macro_detector_timeout) {
  12964. map_session_data *sd = map_id2sd(id);
  12965. nullpo_ret(sd);
  12966. // Remove the current timer
  12967. sd->macro_detect.timer = INVALID_TIMER;
  12968. // Deduct an answering attempt
  12969. sd->macro_detect.retry -= 1;
  12970. if (sd->macro_detect.retry == 0) {
  12971. // All attempts have been exhausted block the user
  12972. clif_macro_detector_status(*sd, MCD_TIMEOUT);
  12973. chrif_req_login_operation(sd->macro_detect.reporter_aid, sd->status.name, CHRIF_OP_LOGIN_BLOCK, 0, 0, 0);
  12974. } else {
  12975. // Update the client
  12976. clif_macro_detector_request_show(*sd);
  12977. // Start a new timer
  12978. sd->macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd->bl.id, 0);
  12979. }
  12980. return 0;
  12981. }
  12982. /**
  12983. * Check player's captcha answer.
  12984. * @param sd: Player data
  12985. * @param captcha_answer: Captcha answer entered by player
  12986. */
  12987. void pc_macro_detector_process_answer(map_session_data &sd, char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  12988. nullpo_retv(captcha_answer);
  12989. const std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  12990. // Has no captcha request
  12991. if (cd == nullptr) {
  12992. return;
  12993. }
  12994. // Correct answer
  12995. if (strcmp(captcha_answer, cd->captcha_answer) == 0) {
  12996. // Delete the timer
  12997. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  12998. // Clear the macro detect data
  12999. sd.macro_detect = {};
  13000. sd.macro_detect.timer = INVALID_TIMER;
  13001. // Unblock all actions for the player
  13002. sd.state.block_action &= ~PCBLOCK_ALL;
  13003. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13004. // Assign temporary macro variable to check failures
  13005. pc_setreg(&sd, add_str("@captcha_retries"), battle_config.macro_detection_retry - sd.macro_detect.retry);
  13006. // Grant bonuses via script
  13007. run_script(cd->bonus_script, 0, sd.bl.id, fake_nd->bl.id);
  13008. // Notify the client
  13009. clif_macro_detector_status(sd, MCD_GOOD);
  13010. } else {
  13011. // Deduct an answering attempt
  13012. sd.macro_detect.retry -= 1;
  13013. // All attempts have been exhausted block the user
  13014. if (sd.macro_detect.retry <= 0) {
  13015. clif_macro_detector_status(sd, MCD_INCORRECT);
  13016. chrif_req_login_operation(sd.macro_detect.reporter_aid, sd.status.name, CHRIF_OP_LOGIN_BLOCK, 0, 0, 0);
  13017. return;
  13018. }
  13019. // Incorrect response, update the client
  13020. clif_macro_detector_request_show(sd);
  13021. // Reset the timer
  13022. addtick_timer(sd.macro_detect.timer, gettick() + battle_config.macro_detection_timeout);
  13023. }
  13024. }
  13025. /**
  13026. * Determine if a player tries to log out during a captcha check.
  13027. * @param sd: Player data
  13028. */
  13029. void pc_macro_detector_disconnect(map_session_data &sd) {
  13030. // Delete the timeout timer
  13031. if (sd.macro_detect.timer != INVALID_TIMER) {
  13032. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13033. sd.macro_detect.timer = INVALID_TIMER;
  13034. }
  13035. // If the player disconnects before clearing the challenge the account is banned.
  13036. if (sd.macro_detect.retry != 0)
  13037. chrif_req_login_operation(sd.macro_detect.reporter_aid, sd.status.name, CHRIF_OP_LOGIN_BLOCK, 0, 0, 0);
  13038. }
  13039. /**
  13040. * Save a list of players from an area select via /macro_detector.
  13041. */
  13042. int pc_macro_reporter_area_select_sub(block_list *bl, va_list ap) {
  13043. nullpo_retr(0, bl);
  13044. if (bl->type != BL_PC)
  13045. return 0;
  13046. std::vector<uint32> *aid_list = va_arg(ap, std::vector<uint32> *);
  13047. nullpo_ret(aid_list);
  13048. aid_list->push_back(bl->id);
  13049. return 0;
  13050. }
  13051. /**
  13052. * Area select via /macro_detector.
  13053. * @param sd: Player data
  13054. * @param x: X location
  13055. * @param y: Y location
  13056. * @param radius: Area
  13057. */
  13058. void pc_macro_reporter_area_select(map_session_data &sd, const int16 x, const int16 y, const int8 radius) {
  13059. std::vector<uint32> aid_list;
  13060. map_foreachinarea(pc_macro_reporter_area_select_sub, sd.bl.m, x - radius, y - radius, x + radius, y + radius, BL_PC, &aid_list);
  13061. clif_macro_reporter_select(sd, aid_list);
  13062. }
  13063. /**
  13064. * Send out captcha check to player.
  13065. * @param ssd: Source player data
  13066. * @param tsd: Target player data
  13067. */
  13068. void pc_macro_reporter_process(map_session_data &ssd, map_session_data &tsd) {
  13069. if (captcha_db.empty())
  13070. return;
  13071. // Pick a random image from the database.
  13072. const std::shared_ptr<s_captcha_data> cd = captcha_db.random();
  13073. // Set macro detection data.
  13074. tsd.macro_detect.cd = cd;
  13075. tsd.macro_detect.reporter_aid = ssd.status.account_id;
  13076. tsd.macro_detect.retry = battle_config.macro_detection_retry;
  13077. // Block all actions for the target player.
  13078. tsd.state.block_action |= (PCBLOCK_ALL | PCBLOCK_IMMUNE);
  13079. // Open macro detect client side.
  13080. clif_macro_detector_request(tsd);
  13081. // Start the timeout timer.
  13082. tsd.macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, tsd.bl.id, 0);
  13083. }
  13084. /**
  13085. * Parse a BMP image to memory.
  13086. * @param filepath: Image file location
  13087. * @param cd: Captcha data
  13088. */
  13089. bool pc_macro_read_captcha_db_loadbmp(const std::string &filepath, std::shared_ptr<s_captcha_data> cd) {
  13090. if (cd == nullptr)
  13091. return false;
  13092. FILE *fp = fopen(filepath.c_str(), "rb");
  13093. if (fp == nullptr) {
  13094. ShowError("%s: Failed to open file \"%s\"\n", __func__, filepath.c_str());
  13095. return false;
  13096. }
  13097. // Load the file data and verify magic
  13098. char bmp_data[CAPTCHA_BMP_SIZE];
  13099. if (fread(bmp_data, CAPTCHA_BMP_SIZE, 1, fp) != 1) {
  13100. ShowError("%s: Failed to read data from \"%s\"\n", __func__, filepath.c_str());
  13101. fclose(fp);
  13102. return false;
  13103. }
  13104. fclose(fp);
  13105. if (bmp_data[0] != 'B' || bmp_data[1] != 'M') {
  13106. ShowError("%s: Invalid BMP file header given at \"%s\"\n", __func__, filepath.c_str());
  13107. return false;
  13108. }
  13109. // Compress the data into the destination
  13110. unsigned long com_size = sizeof(cd->image_data);
  13111. encode_zip(cd->image_data, &com_size, bmp_data, CAPTCHA_BMP_SIZE);
  13112. cd->image_size = static_cast<int16>(com_size);
  13113. return true;
  13114. }
  13115. const std::string CaptchaDatabase::getDefaultLocation() {
  13116. return std::string(db_path) + "/captcha_db.yml";
  13117. }
  13118. /**
  13119. * Reads and parses an entry from the captcha_db.
  13120. * @param node: YAML node containing the entry.
  13121. * @return count of successfully parsed rows
  13122. */
  13123. uint64 CaptchaDatabase::parseBodyNode(const ryml::NodeRef &node) {
  13124. uint16 index;
  13125. if (!this->asUInt16(node, "Id", index))
  13126. return 0;
  13127. std::shared_ptr<s_captcha_data> cd = captcha_db.find(index);
  13128. bool exists = cd != nullptr;
  13129. if (!exists) {
  13130. if (!this->nodesExist(node, { "Filename", "Answer" }))
  13131. return 0;
  13132. cd = std::make_shared<s_captcha_data>();
  13133. cd->index = index;
  13134. }
  13135. if (this->nodeExists(node, "Filename")) {
  13136. std::string filename;
  13137. if (!this->asString(node, "Filename", filename))
  13138. return 0;
  13139. if (!pc_macro_read_captcha_db_loadbmp(filename, cd)) {
  13140. this->invalidWarning(node["Filename"], "Failed to parse BMP image, skipping...\n");
  13141. return 0;
  13142. }
  13143. }
  13144. if (this->nodeExists(node, "Answer")) {
  13145. std::string answer;
  13146. if (!this->asString(node, "Answer", answer))
  13147. return 0;
  13148. if (answer.length() < 4 || answer.length() > CAPTCHA_ANSWER_SIZE) {
  13149. this->invalidWarning(node["Answer"], "The captcha answer must be between 4~%d characters, skipping...", CAPTCHA_ANSWER_SIZE);
  13150. return 0;
  13151. }
  13152. safestrncpy(cd->captcha_answer, answer.c_str(), sizeof(cd->captcha_answer));
  13153. }
  13154. if (this->nodeExists(node, "Bonus")) {
  13155. std::string script;
  13156. if (!this->asString(node, "Bonus", script)) {
  13157. return 0;
  13158. }
  13159. if (cd->bonus_script) {
  13160. script_free_code(cd->bonus_script);
  13161. cd->bonus_script = nullptr;
  13162. }
  13163. cd->bonus_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Bonus"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  13164. } else {
  13165. if (!exists)
  13166. 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);
  13167. }
  13168. if (!exists)
  13169. captcha_db.put(index, cd);
  13170. return 1;
  13171. }
  13172. /*==========================================
  13173. * pc Init/Terminate
  13174. *------------------------------------------*/
  13175. void do_final_pc(void) {
  13176. db_destroy(itemcd_db);
  13177. do_final_pc_groups();
  13178. ers_destroy(pc_sc_display_ers);
  13179. ers_destroy(num_reg_ers);
  13180. ers_destroy(str_reg_ers);
  13181. attendance_db.clear();
  13182. reputation_db.clear();
  13183. penalty_db.clear();
  13184. captcha_db.clear();
  13185. }
  13186. void do_init_pc(void) {
  13187. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  13188. pc_readdb();
  13189. pc_read_motd(); // Read MOTD [Valaris]
  13190. attendance_db.load();
  13191. reputation_db.load();
  13192. captcha_db.load();
  13193. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  13194. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  13195. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  13196. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  13197. add_timer_func_list(pc_autosave, "pc_autosave");
  13198. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  13199. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  13200. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  13201. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  13202. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  13203. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  13204. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  13205. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  13206. add_timer_func_list(pc_macro_detector_timeout, "pc_macro_detector_timeout");
  13207. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  13208. // 0=day, 1=night [Yor]
  13209. night_flag = battle_config.night_at_start ? 1 : 0;
  13210. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  13211. int day_duration = battle_config.day_duration;
  13212. int night_duration = battle_config.night_duration;
  13213. // add night/day timer [Yor]
  13214. add_timer_func_list(map_day_timer, "map_day_timer");
  13215. add_timer_func_list(map_night_timer, "map_night_timer");
  13216. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  13217. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  13218. }
  13219. do_init_pc_groups();
  13220. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  13221. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13222. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13223. ers_chunk_size(pc_sc_display_ers, 150);
  13224. ers_chunk_size(num_reg_ers, 300);
  13225. ers_chunk_size(str_reg_ers, 50);
  13226. }