pc.cpp 470 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pc.hpp"
  4. #include <map>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #ifdef MAP_GENERATOR
  8. #include <fstream>
  9. #include <iostream>
  10. #include <nlohmann/json.hpp>
  11. #endif
  12. #include "../common/cbasetypes.hpp"
  13. #include "../common/core.hpp" // get_svn_revision()
  14. #include "../common/database.hpp"
  15. #include "../common/ers.hpp" // ers_destroy
  16. #include "../common/grfio.hpp"
  17. #include "../common/malloc.hpp"
  18. #include "../common/mmo.hpp" //NAME_LENGTH
  19. #include "../common/nullpo.hpp"
  20. #include "../common/random.hpp"
  21. #include "../common/showmsg.hpp"
  22. #include "../common/socket.hpp" // session[]
  23. #include "../common/strlib.hpp" // safestrncpy()
  24. #include "../common/timer.hpp"
  25. #include "../common/utilities.hpp"
  26. #include "../common/utils.hpp"
  27. #include "achievement.hpp"
  28. #include "atcommand.hpp" // get_atcommand_level()
  29. #include "battle.hpp" // battle_config
  30. #include "battleground.hpp"
  31. #include "buyingstore.hpp" // struct s_buyingstore
  32. #include "channel.hpp"
  33. #include "chat.hpp"
  34. #include "chrif.hpp"
  35. #include "clan.hpp"
  36. #include "clif.hpp"
  37. #include "date.hpp" // is_day_of_*()
  38. #include "duel.hpp"
  39. #include "elemental.hpp"
  40. #include "guild.hpp"
  41. #include "homunculus.hpp"
  42. #include "instance.hpp"
  43. #include "intif.hpp"
  44. #include "itemdb.hpp" // MAX_ITEMGROUP
  45. #include "log.hpp"
  46. #include "map.hpp"
  47. #include "mercenary.hpp"
  48. #include "mob.hpp"
  49. #include "npc.hpp"
  50. #include "party.hpp" // party_search()
  51. #include "pc_groups.hpp"
  52. #include "pet.hpp" // pet_unlocktarget()
  53. #include "quest.hpp"
  54. #include "skill.hpp" // skill_isCopyable()
  55. #include "script.hpp" // struct script_reg, struct script_regstr
  56. #include "searchstore.hpp" // struct s_search_store_info
  57. #include "status.hpp" // OPTION_*, struct weapon_atk
  58. #include "storage.hpp"
  59. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  60. #include "vending.hpp" // struct s_vending
  61. using namespace rathena;
  62. JobDatabase job_db;
  63. CaptchaDatabase captcha_db;
  64. const char *macro_allowed_answer_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  65. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  66. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
  67. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  68. PlayerStatPointDatabase statpoint_db;
  69. SkillTreeDatabase skill_tree_db;
  70. // timer for night.day implementation
  71. int day_timer_tid = INVALID_TIMER;
  72. int night_timer_tid = INVALID_TIMER;
  73. struct eri *pc_sc_display_ers = NULL;
  74. struct eri *num_reg_ers;
  75. struct eri *str_reg_ers;
  76. int pc_expiration_tid = INVALID_TIMER;
  77. struct fame_list smith_fame_list[MAX_FAME_LIST];
  78. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  79. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  80. const std::string AttendanceDatabase::getDefaultLocation(){
  81. return std::string(db_path) + "/attendance.yml";
  82. }
  83. /**
  84. * Reads and parses an entry from the attendance_db.
  85. * @param node: YAML node containing the entry.
  86. * @return count of successfully parsed rows
  87. */
  88. uint64 AttendanceDatabase::parseBodyNode(const ryml::NodeRef& node){
  89. uint32 start;
  90. if( !this->asUInt32( node, "Start", start ) ){
  91. return 0;
  92. }
  93. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  94. bool exists = attendance_period != nullptr;
  95. if( !exists ){
  96. if( !this->nodeExists( node, "End" ) ){
  97. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  98. return 0;
  99. }
  100. if( !this->nodeExists( node, "Rewards" ) ){
  101. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  102. return 0;
  103. }
  104. attendance_period = std::make_shared<s_attendance_period>();
  105. attendance_period->start = start;
  106. }
  107. // If it does not exist yet, we need to check it for sure
  108. bool requiresCollisionDetection = !exists;
  109. if( this->nodeExists( node, "End" ) ){
  110. uint32 end;
  111. if( !this->asUInt32( node, "End", end ) ){
  112. return 0;
  113. }
  114. // If the period is outdated already, we do not even bother parsing
  115. if( end < date_get( DT_YYYYMMDD ) ){
  116. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  117. return 0;
  118. }
  119. if( !exists || attendance_period->end != end ){
  120. requiresCollisionDetection = true;
  121. attendance_period->end = end;
  122. }
  123. }
  124. // Collision detection
  125. if( requiresCollisionDetection ){
  126. bool collision = false;
  127. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  128. std::shared_ptr<s_attendance_period> period = pair.second;
  129. if( exists && period->start == attendance_period->start ){
  130. // Dont compare to yourself
  131. continue;
  132. }
  133. // Check if start is inside another period
  134. if( period->start <= attendance_period->start && start <= period->end ){
  135. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  136. collision = true;
  137. break;
  138. }
  139. // Check if end is inside another period
  140. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  141. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  142. collision = true;
  143. break;
  144. }
  145. }
  146. if( collision ){
  147. return 0;
  148. }
  149. }
  150. if( this->nodeExists( node, "Rewards" ) ){
  151. const auto& rewardsNode = node["Rewards"];
  152. for( const auto& rewardNode : rewardsNode ){
  153. uint32 day;
  154. if( !this->asUInt32( rewardNode, "Day", day ) ){
  155. continue;
  156. }
  157. day -= 1;
  158. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  159. bool reward_exists = reward != nullptr;
  160. if( !reward_exists ){
  161. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  162. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  163. return 0;
  164. }
  165. reward = std::make_shared<s_attendance_reward>();
  166. }
  167. if( this->nodeExists( rewardNode, "ItemId" ) ){
  168. t_itemid item_id;
  169. if( !this->asUInt32( rewardNode, "ItemId", item_id ) ){
  170. continue;
  171. }
  172. if( !item_db.exists( item_id ) ){
  173. ShowError( "pc_attendance_load: Unknown item ID %u for day %d.\n", item_id, day + 1 );
  174. continue;
  175. }
  176. reward->item_id = item_id;
  177. }
  178. if( this->nodeExists( rewardNode, "Amount" ) ){
  179. uint16 amount;
  180. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  181. continue;
  182. }
  183. if( amount == 0 ){
  184. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  185. amount = 1;
  186. }else if( amount > MAX_AMOUNT ){
  187. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  188. amount = MAX_AMOUNT;
  189. }
  190. reward->amount = amount;
  191. }else{
  192. if( !reward_exists ){
  193. reward->amount = 1;
  194. }
  195. }
  196. if( !reward_exists ){
  197. attendance_period->rewards[day] = reward;
  198. }
  199. }
  200. bool missing_day = false;
  201. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  202. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  203. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  204. missing_day = true;
  205. break;
  206. }
  207. }
  208. if( missing_day ){
  209. return 0;
  210. }
  211. }
  212. if( !exists ){
  213. this->put( start, attendance_period );
  214. }
  215. return 1;
  216. }
  217. AttendanceDatabase attendance_db;
  218. const std::string ReputationDatabase::getDefaultLocation(){
  219. return std::string( db_path ) + "/reputation.yml";
  220. }
  221. uint64 ReputationDatabase::parseBodyNode( const ryml::NodeRef& node ){
  222. int64 id;
  223. if( !this->asInt64( node, "Id", id ) ){
  224. return 0;
  225. }
  226. std::shared_ptr<s_reputation> reputation = this->find( id );
  227. bool exists = reputation != nullptr;
  228. if( !exists ){
  229. if( !this->nodesExist( node, { "Name", "Variable" } ) ){
  230. return 0;
  231. }
  232. reputation = std::make_shared<s_reputation>();
  233. reputation->id = id;
  234. }
  235. if( this->nodeExists( node, "Name" ) ){
  236. std::string name;
  237. if( !this->asString( node, "Name", name ) ){
  238. return 0;
  239. }
  240. reputation->name = name;
  241. }
  242. if( this->nodeExists( node, "Variable" ) ){
  243. std::string variable;
  244. if( !this->asString( node, "Variable", variable ) ){
  245. return 0;
  246. }
  247. if( variable.length() > 32 ){
  248. this->invalidWarning( node, "Variable name \"%s\" exceeds maximum length 32.\n", variable.c_str() );
  249. return 0;
  250. }
  251. reputation->variable = variable;
  252. }
  253. if( this->nodeExists( node, "Minimum" ) ){
  254. int64 minimum;
  255. if( !this->asInt64( node, "Minimum", minimum ) ){
  256. return 0;
  257. }
  258. reputation->minimum = minimum;
  259. }else{
  260. if( !exists ){
  261. reputation->minimum = INT64_MIN;
  262. }
  263. }
  264. if( this->nodeExists( node, "Maximum" ) ){
  265. int64 maximum;
  266. if( !this->asInt64( node, "Maximum", maximum ) ){
  267. return 0;
  268. }
  269. reputation->maximum = maximum;
  270. }else{
  271. if( !exists ){
  272. reputation->maximum = INT64_MIN;
  273. }
  274. }
  275. #ifdef MAP_GENERATOR
  276. if (this->nodeExists(node, "Visibility")) {
  277. std::string visibility;
  278. if (!this->asString(node, "Visibility", visibility)) {
  279. return 0;
  280. }
  281. if (visibility == "Always")
  282. reputation->visibility = s_reputation::e_visibility::ALWAYS;
  283. else if (visibility == "Never")
  284. reputation->visibility = s_reputation::e_visibility::NEVER;
  285. else if (visibility == "Exist")
  286. reputation->visibility = s_reputation::e_visibility::EXIST;
  287. else {
  288. this->invalidWarning(node, "Visibility \"%s\" unknown.\n", visibility.c_str());
  289. return 0;
  290. }
  291. } else {
  292. if (!exists) {
  293. reputation->visibility = s_reputation::e_visibility::ALWAYS;
  294. }
  295. }
  296. #endif
  297. if( !exists ){
  298. this->put( id, reputation );
  299. }
  300. return 1;
  301. }
  302. ReputationDatabase reputation_db;
  303. const std::string ReputationGroupDatabase::getDefaultLocation() {
  304. return std::string(db_path) + "/reputation_group.yml";
  305. }
  306. uint64 ReputationGroupDatabase::parseBodyNode(const ryml::NodeRef& node) {
  307. int64 id;
  308. if (!this->asInt64(node, "Id", id)) {
  309. return 0;
  310. }
  311. std::shared_ptr<s_reputationgroup> group = this->find(id);
  312. bool exists = group != nullptr;
  313. if (!exists) {
  314. if (!this->nodesExist(node, {"Name", "ReputeList"})) {
  315. return 0;
  316. }
  317. group = std::make_shared<s_reputationgroup>();
  318. group->id = id;
  319. }
  320. if (this->nodeExists(node, "Name")) {
  321. std::string name;
  322. if (!this->asString(node, "Name", name)) {
  323. return 0;
  324. }
  325. group->name = name;
  326. }
  327. if (this->nodeExists(node, "ScriptName")) {
  328. std::string script_name;
  329. if (!this->asString(node, "ScriptName", script_name)) {
  330. return 0;
  331. }
  332. group->script_name = script_name;
  333. }
  334. if (this->nodeExists(node, "ReputeList")) {
  335. const auto& reputelist = node[c4::to_csubstr("ReputeList")];
  336. for (const auto& repute : reputelist) {
  337. int64 repute_id;
  338. try {
  339. repute >> repute_id;
  340. } catch (std::runtime_error const&) {
  341. this->invalidWarning(node, "Value \"%s\" cannot be parsed as int64.\n", repute.val().str);
  342. continue;
  343. }
  344. if (!reputation_db.find(repute_id)) {
  345. this->invalidWarning(node, "Reputation id %lld does not exist in reputation_db!\n", repute_id);
  346. continue;
  347. }
  348. group->reputations.push_back(repute_id);
  349. }
  350. }
  351. if (!exists) {
  352. this->put(id, group);
  353. }
  354. return 1;
  355. }
  356. ReputationGroupDatabase reputationgroup_db;
  357. void pc_reputation_generate() {
  358. #ifdef MAP_GENERATOR
  359. const std::string filePrefix = "generated/clientside/data/contentdata/";
  360. auto reputeInfo = nlohmann::json::object();
  361. for (const auto& pair : reputation_db) {
  362. auto id = pair.first;
  363. auto rep = pair.second;
  364. nlohmann::json node;
  365. switch (rep->visibility) {
  366. case s_reputation::e_visibility::ALWAYS:
  367. node["Invisible"] = "VISIBLE_TRUE";
  368. break;
  369. case s_reputation::e_visibility::NEVER:
  370. node["Invisible"] = "VISIBLE_FALSE";
  371. break;
  372. case s_reputation::e_visibility::EXIST:
  373. node["Invisible"] = "VISIBLE_EXIST";
  374. break;
  375. }
  376. node["MaxPoint_Negative"] = std::abs(rep->minimum);
  377. node["MaxPoint_Positive"] = std::abs(rep->maximum);
  378. node["Name"] = rep->name;
  379. reputeInfo[std::to_string(id)] = node;
  380. }
  381. auto j = nlohmann::json::object();
  382. j["reputeInfo"] = reputeInfo;
  383. // std::cout << j.dump(2) << "\n";
  384. auto bson = nlohmann::json::to_bson(j);
  385. auto reputation_file = std::ofstream(filePrefix + "./reputeinfodata.bson", std::ios::binary);
  386. if (!reputation_file) {
  387. ShowError("Failed to create reputation file.\n");
  388. ShowError("Maybe the file directory \"%s\" does not exist?\n", filePrefix.c_str());
  389. ShowInfo("Create the directory and rerun map-server-generator\n");
  390. exit(1);
  391. }
  392. reputation_file.write((const char *)&bson[0], bson.size());
  393. auto reputeGroupInfo = nlohmann::json::object();
  394. for (const auto& pair : reputationgroup_db) {
  395. auto id = pair.first;
  396. auto group = pair.second;
  397. nlohmann::json node;
  398. node["ID"] = group->script_name;
  399. node["Name"] = group->name;
  400. node["ReputeList"] = group->reputations;
  401. reputeGroupInfo[std::to_string(id)] = node;
  402. }
  403. j = nlohmann::json::object();
  404. j["ReputeGroup"] = reputeGroupInfo;
  405. // std::cout << j.dump(2) << "\n";
  406. bson = nlohmann::json::to_bson(j);
  407. auto reputation_group_file = std::ofstream(filePrefix + "./reputegroupdata.bson", std::ios::binary);
  408. if (!reputation_group_file) {
  409. ShowError("Failed to create reputation group file.\n");
  410. ShowError("Maybe the file directory \"%s\" does not exist?\n", filePrefix.c_str());
  411. ShowInfo("Create the directory and rerun map-server-generator\n");
  412. exit(1);
  413. }
  414. reputation_group_file.write((const char *)&bson[0], bson.size());
  415. #endif
  416. }
  417. const std::string PenaltyDatabase::getDefaultLocation(){
  418. return std::string( db_path ) + "/level_penalty.yml";
  419. }
  420. uint64 PenaltyDatabase::parseBodyNode(const ryml::NodeRef& node){
  421. std::string type_constant;
  422. if( !this->asString( node, "Type", type_constant ) ){
  423. return 0;
  424. }
  425. int64 constant_value;
  426. if( !script_get_constant( ( "PENALTY_" + type_constant ).c_str(), &constant_value ) ){
  427. this->invalidWarning( node["Type"], "Unknown penalty type \"%s\".\n", type_constant.c_str() );
  428. return 0;
  429. }
  430. if( constant_value < PENALTY_NONE || constant_value > PENALTY_MAX ){
  431. this->invalidWarning( node["Type"], "Invalid penalty type \"%s\".\n", type_constant.c_str() );
  432. return 0;
  433. }
  434. e_penalty_type type = static_cast<e_penalty_type>( constant_value );
  435. std::shared_ptr<s_penalty> penalty = this->find( type );
  436. bool exists = penalty != nullptr;
  437. if( !exists ){
  438. penalty = std::make_shared<s_penalty>();
  439. penalty->type = type;
  440. for( int i = 0, max = ARRAYLENGTH( penalty->rate ); i < max; i++ ){
  441. penalty->rate[i] = UINT16_MAX;
  442. }
  443. }
  444. if( this->nodeExists( node, "LevelDifferences" ) ){
  445. for( const auto& levelNode : node["LevelDifferences"] ){
  446. if( !this->nodesExist( levelNode, { "Difference", "Rate" } ) ){
  447. return 0;
  448. }
  449. int32 difference;
  450. if( !this->asInt32( levelNode, "Difference", difference ) ){
  451. return 0;
  452. }
  453. if( std::abs( difference ) > MAX_LEVEL ){
  454. this->invalidWarning( levelNode["Difference"], "Level difference %d is bigger than maximum level %d.\n", difference, MAX_LEVEL );
  455. return 0;
  456. }
  457. uint16 rate;
  458. if( !this->asUInt16Rate( levelNode, "Rate", rate ) ){
  459. return 0;
  460. }
  461. penalty->rate[difference + MAX_LEVEL - 1] = rate;
  462. }
  463. }
  464. if( !exists ){
  465. this->put( type, penalty );
  466. }
  467. return 1;
  468. }
  469. void PenaltyDatabase::loadingFinished(){
  470. for( const auto& pair : *this ){
  471. for( int i = MAX_LEVEL - 1, max = ARRAYLENGTH( pair.second->rate ), last_rate = 100; i < max; i++ ){
  472. uint16 rate = pair.second->rate[i];
  473. // Check if it has been defined
  474. if( rate == UINT16_MAX ){
  475. pair.second->rate[i] = last_rate;
  476. }else{
  477. last_rate = rate;
  478. }
  479. }
  480. for( int i = MAX_LEVEL - 1, last_rate = 100; i >= 0; i-- ){
  481. uint16 rate = pair.second->rate[i];
  482. // Check if it has been defined
  483. if( rate == UINT16_MAX ){
  484. pair.second->rate[i] = last_rate;
  485. }else{
  486. last_rate = rate;
  487. }
  488. }
  489. }
  490. TypesafeYamlDatabase::loadingFinished();
  491. }
  492. PenaltyDatabase penalty_db;
  493. #define MOTD_LINE_SIZE 128
  494. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  495. bool reg_load;
  496. /**
  497. * Translation table from athena equip index to aegis bitmask
  498. */
  499. unsigned int equip_bitmask[EQI_MAX] = {
  500. EQP_ACC_L, // EQI_ACC_L
  501. EQP_ACC_R, // EQI_ACC_R
  502. EQP_SHOES, // EQI_SHOES
  503. EQP_GARMENT, // EQI_GARMENT
  504. EQP_HEAD_LOW, // EQI_HEAD_LOW
  505. EQP_HEAD_MID, // EQI_HEAD_MID
  506. EQP_HEAD_TOP, // EQI_HEAD_TOP
  507. EQP_ARMOR, // EQI_ARMOR
  508. EQP_HAND_L, // EQI_HAND_L
  509. EQP_HAND_R, // EQI_HAND_R
  510. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  511. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  512. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  513. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  514. EQP_AMMO, // EQI_AMMO
  515. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  516. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  517. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  518. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  519. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  520. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  521. };
  522. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  523. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  524. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  525. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  526. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  527. };
  528. void pc_set_reg_load( bool val ){
  529. reg_load = val;
  530. }
  531. /**
  532. * Item Cool Down Delay Saving
  533. * Struct item_cd is not a member of struct map_session_data
  534. * to keep cooldowns in memory between player log-ins.
  535. * All cooldowns are reset when server is restarted.
  536. **/
  537. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  538. struct item_cd {
  539. t_tick tick[MAX_ITEMDELAYS]; //tick
  540. t_itemid nameid[MAX_ITEMDELAYS]; //item id
  541. };
  542. /**
  543. * Converts a class to its array index for CLASS_COUNT defined arrays.
  544. * Note that it does not do a validity check for speed purposes, where parsing
  545. * player input make sure to use a pcdb_checkid first!
  546. * @param class_ Job ID see enum e_job
  547. * @return Class Index
  548. */
  549. int pc_class2idx(int class_) {
  550. if (class_ >= JOB_NOVICE_HIGH)
  551. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  552. return class_;
  553. }
  554. /**
  555. * Get player's group ID
  556. * @param sd
  557. * @return Group ID
  558. */
  559. int pc_get_group_id(struct map_session_data *sd) {
  560. return sd->group_id;
  561. }
  562. /** Get player's group Level
  563. * @param sd
  564. * @return Group Level
  565. */
  566. int pc_get_group_level(struct map_session_data *sd) {
  567. return sd->group->level;
  568. }
  569. /**
  570. * Remove a player from queue after timeout
  571. * @param tid: Timer ID
  572. * @param tick: Timer
  573. * @param id: ID
  574. * @return 0 on success or 1 otherwise
  575. */
  576. static TIMER_FUNC(pc_on_expire_active)
  577. {
  578. map_session_data *sd = (map_session_data *)data;
  579. nullpo_retr(1, sd);
  580. sd->tid_queue_active = INVALID_TIMER;
  581. bg_queue_leave(sd);
  582. clif_bg_queue_entry_init(sd);
  583. return 0;
  584. }
  585. /**
  586. * Function used to set timer externally
  587. * @param sd: Player data
  588. */
  589. void pc_set_bg_queue_timer(map_session_data *sd) {
  590. nullpo_retv(sd);
  591. if (sd->tid_queue_active != INVALID_TIMER) {
  592. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  593. sd->tid_queue_active = INVALID_TIMER;
  594. }
  595. sd->tid_queue_active = add_timer(gettick() + 20000, pc_on_expire_active, 0, (intptr_t)sd);
  596. }
  597. /**
  598. * Function used to delete timer externally
  599. * @param sd: Player data
  600. */
  601. void pc_delete_bg_queue_timer(map_session_data *sd) {
  602. nullpo_retv(sd);
  603. if (sd->tid_queue_active != INVALID_TIMER) {
  604. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  605. sd->tid_queue_active = INVALID_TIMER;
  606. }
  607. }
  608. static TIMER_FUNC(pc_invincible_timer){
  609. struct map_session_data *sd;
  610. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  611. return 1;
  612. if(sd->invincible_timer != tid){
  613. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  614. return 0;
  615. }
  616. sd->invincible_timer = INVALID_TIMER;
  617. skill_unit_move(&sd->bl,tick,1);
  618. return 0;
  619. }
  620. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  621. nullpo_retv(sd);
  622. if( sd->invincible_timer != INVALID_TIMER )
  623. delete_timer(sd->invincible_timer,pc_invincible_timer);
  624. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  625. }
  626. void pc_delinvincibletimer(struct map_session_data* sd)
  627. {
  628. nullpo_retv(sd);
  629. if( sd->invincible_timer != INVALID_TIMER )
  630. {
  631. delete_timer(sd->invincible_timer,pc_invincible_timer);
  632. sd->invincible_timer = INVALID_TIMER;
  633. skill_unit_move(&sd->bl,gettick(),1);
  634. }
  635. }
  636. static TIMER_FUNC(pc_spiritball_timer){
  637. struct map_session_data *sd;
  638. int i;
  639. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  640. return 1;
  641. if( sd->spiritball <= 0 )
  642. {
  643. ShowError("pc_spiritball_timer: %d spiritball's available. (aid=%d cid=%d tid=%d)\n", sd->spiritball, sd->status.account_id, sd->status.char_id, tid);
  644. sd->spiritball = 0;
  645. return 0;
  646. }
  647. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  648. if( i == sd->spiritball )
  649. {
  650. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  651. return 0;
  652. }
  653. sd->spiritball--;
  654. if( i != sd->spiritball )
  655. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  656. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  657. clif_spiritball(&sd->bl);
  658. return 0;
  659. }
  660. /**
  661. * Adds a spiritball to player for 'interval' ms
  662. * @param sd
  663. * @param interval
  664. * @param max
  665. */
  666. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  667. {
  668. int tid;
  669. uint8 i;
  670. nullpo_retv(sd);
  671. if(max > MAX_SPIRITBALL)
  672. max = MAX_SPIRITBALL;
  673. if(sd->spiritball < 0)
  674. sd->spiritball = 0;
  675. if( sd->spiritball && sd->spiritball >= max )
  676. {
  677. if(sd->spirit_timer[0] != INVALID_TIMER)
  678. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  679. sd->spiritball--;
  680. if( sd->spiritball != 0 )
  681. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  682. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  683. }
  684. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  685. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spirit_timer[i])->tick) < 0);
  686. if( i != sd->spiritball )
  687. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  688. sd->spirit_timer[i] = tid;
  689. sd->spiritball++;
  690. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  691. clif_millenniumshield(&sd->bl,sd->spiritball);
  692. else
  693. clif_spiritball(&sd->bl);
  694. }
  695. /**
  696. * Removes number of spiritball from player
  697. * @param sd
  698. * @param count
  699. * @param type 1 = doesn't give client effect
  700. */
  701. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  702. {
  703. uint8 i;
  704. nullpo_retv(sd);
  705. if(sd->spiritball <= 0) {
  706. sd->spiritball = 0;
  707. return;
  708. }
  709. if(count == 0)
  710. return;
  711. if(count > sd->spiritball)
  712. count = sd->spiritball;
  713. sd->spiritball -= count;
  714. if(count > MAX_SPIRITBALL)
  715. count = MAX_SPIRITBALL;
  716. for(i=0;i<count;i++) {
  717. if(sd->spirit_timer[i] != INVALID_TIMER) {
  718. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  719. sd->spirit_timer[i] = INVALID_TIMER;
  720. }
  721. }
  722. for(i=count;i<MAX_SPIRITBALL;i++) {
  723. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  724. sd->spirit_timer[i] = INVALID_TIMER;
  725. }
  726. if(!type) {
  727. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  728. clif_millenniumshield(&sd->bl,sd->spiritball);
  729. else
  730. clif_spiritball(&sd->bl);
  731. }
  732. }
  733. /**
  734. * Adds a soulball to player
  735. * @param sd: Player data
  736. * @param max: Max amount of soulballs
  737. */
  738. int pc_addsoulball(map_session_data *sd, int max)
  739. {
  740. nullpo_ret(sd);
  741. status_change *sc = status_get_sc(&sd->bl);
  742. if (sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  743. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, 0, skill_get_time2(SP_SOULCOLLECT, 1));
  744. sd->soulball = 0;
  745. }
  746. max = min(max, MAX_SOUL_BALL);
  747. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  748. if (sd->soulball && sd->soulball >= max)
  749. sd->soulball--;
  750. sd->soulball++;
  751. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, sd->soulball, skill_get_time2(SP_SOULCOLLECT, 1));
  752. clif_soulball(sd);
  753. return 0;
  754. }
  755. /**
  756. * Removes number of soulball from player
  757. * @param sd: Player data
  758. * @param count: Amount to remove
  759. * @param type: true = doesn't give client effect
  760. */
  761. int pc_delsoulball(map_session_data *sd, int count, bool type)
  762. {
  763. nullpo_ret(sd);
  764. if (count <= 0)
  765. return 0;
  766. status_change *sc = status_get_sc(&sd->bl);
  767. if (sd->soulball <= 0 || sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  768. sd->soulball = 0;
  769. }else{
  770. sd->soulball -= cap_value(count, 0, sd->soulball);
  771. if (sd->soulball == 0)
  772. status_change_end(&sd->bl, SC_SOULENERGY);
  773. else
  774. sc->data[SC_SOULENERGY]->val1 = sd->soulball;
  775. }
  776. if (!type)
  777. clif_soulball(sd);
  778. return 0;
  779. }
  780. /**
  781. * Adds servantballs to a player
  782. * @param sd: Player data
  783. * @param amount: Amount to add
  784. */
  785. void pc_addservantball( struct map_session_data& sd, int count ){
  786. sd.servantball = cap_value( sd.servantball + count, 0, MAX_SERVANTBALL );
  787. clif_servantball( sd );
  788. }
  789. /**
  790. * Removes number of servantballs from player
  791. * @param sd: Player data
  792. * @param count: Amount to remove
  793. */
  794. void pc_delservantball( struct map_session_data& sd, int count ){
  795. sd.servantball = cap_value( sd.servantball - count, 0, MAX_SERVANTBALL );
  796. clif_servantball( sd );
  797. }
  798. /**
  799. * Adds abyssballs to a player
  800. * @param sd: Player data
  801. * @param amount: Amount to add
  802. */
  803. void pc_addabyssball( struct map_session_data& sd, int count ){
  804. sd.abyssball = cap_value( sd.abyssball + count, 0, MAX_ABYSSBALL );
  805. clif_abyssball( sd );
  806. }
  807. /**
  808. * Removes number of abyssballs from player
  809. * @param sd: Player data
  810. * @param count: Amount to remove
  811. */
  812. void pc_delabyssball( struct map_session_data& sd, int count ){
  813. sd.abyssball = cap_value( sd.abyssball - count, 0, MAX_ABYSSBALL );
  814. clif_abyssball( sd );
  815. }
  816. /**
  817. * Increases a player's fame points and displays a notice to him
  818. * @param sd Player
  819. * @param count Fame point
  820. * @return true: on success, false: on error
  821. */
  822. bool pc_addfame(map_session_data &sd, int count)
  823. {
  824. enum e_rank ranktype;
  825. sd.status.fame += count;
  826. if (sd.status.fame > MAX_FAME)
  827. sd.status.fame = MAX_FAME;
  828. switch(sd.class_&MAPID_UPPERMASK){
  829. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  830. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  831. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  832. default:
  833. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd.status.class_), sd.status.class_ );
  834. return false;
  835. }
  836. clif_update_rankingpoint(sd, ranktype, count);
  837. chrif_updatefamelist(sd, ranktype);
  838. return true;
  839. }
  840. /**
  841. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  842. * @param sd
  843. * @param job Job use enum e_mapid
  844. * @return Rank
  845. */
  846. unsigned char pc_famerank(uint32 char_id, int job)
  847. {
  848. uint8 i;
  849. switch(job){
  850. case MAPID_BLACKSMITH: // Blacksmith
  851. for(i = 0; i < MAX_FAME_LIST; i++){
  852. if(smith_fame_list[i].id == char_id)
  853. return i + 1;
  854. }
  855. break;
  856. case MAPID_ALCHEMIST: // Alchemist
  857. for(i = 0; i < MAX_FAME_LIST; i++){
  858. if(chemist_fame_list[i].id == char_id)
  859. return i + 1;
  860. }
  861. break;
  862. case MAPID_TAEKWON: // Taekwon
  863. for(i = 0; i < MAX_FAME_LIST; i++){
  864. if(taekwon_fame_list[i].id == char_id)
  865. return i + 1;
  866. }
  867. break;
  868. }
  869. return 0;
  870. }
  871. /**
  872. * Restart player's HP & SP value
  873. * @param sd
  874. * @param type Restart type: 1 - Normal Resurection
  875. */
  876. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  877. struct status_data *status, *b_status;
  878. nullpo_retv(sd);
  879. b_status = &sd->base_status;
  880. status = &sd->battle_status;
  881. if (type&1) { //Normal resurrection
  882. status->hp = 1; //Otherwise status_heal may fail if dead.
  883. status_heal(&sd->bl, b_status->hp, 0, 1);
  884. if( status->sp < b_status->sp )
  885. status_set_sp(&sd->bl, b_status->sp, 1);
  886. } else { //Just for saving on the char-server (with values as if respawned)
  887. sd->status.hp = b_status->hp;
  888. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  889. sd->status.ap = (status->ap < b_status->ap)?b_status->ap:status->ap;
  890. }
  891. }
  892. /*==========================================
  893. Rental System
  894. *------------------------------------------*/
  895. /**
  896. * Ends a rental and removes the item/effect
  897. * @param tid: Tick ID
  898. * @param tick: Timer
  899. * @param id: Timer ID
  900. * @param data: Data
  901. * @return false - failure, true - success
  902. */
  903. TIMER_FUNC(pc_inventory_rental_end){
  904. struct map_session_data *sd = map_id2sd(id);
  905. if( sd == NULL )
  906. return 0;
  907. if( tid != sd->rental_timer ) {
  908. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  909. return 0;
  910. }
  911. pc_inventory_rentals(sd);
  912. return 1;
  913. }
  914. /**
  915. * Removes the rental timer from the player
  916. * @param sd: Player data
  917. */
  918. void pc_inventory_rental_clear(struct map_session_data *sd)
  919. {
  920. if( sd->rental_timer != INVALID_TIMER ) {
  921. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  922. sd->rental_timer = INVALID_TIMER;
  923. }
  924. }
  925. /**
  926. * Check for items in the player's inventory that are rental type
  927. * @param sd: Player data
  928. */
  929. void pc_inventory_rentals(struct map_session_data *sd)
  930. {
  931. int i, c = 0;
  932. unsigned int next_tick = UINT_MAX;
  933. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  934. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  935. continue; // Nothing here
  936. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  937. continue;
  938. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  939. if (sd->inventory_data[i]->unequip_script)
  940. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  941. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  942. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  943. } else {
  944. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  945. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  946. next_tick = umin(expire_tick * 1000U, next_tick);
  947. c++;
  948. }
  949. }
  950. if( c > 0 ) // min(next_tick,3600000) 1 hour each timer to keep announcing to the owner, and to avoid a but with rental time > 15 days
  951. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  952. else
  953. sd->rental_timer = INVALID_TIMER;
  954. }
  955. /**
  956. * Add a rental item to the player and adjusts the rental timer appropriately
  957. * @param sd: Player data
  958. * @param seconds: Rental time
  959. */
  960. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  961. {
  962. t_tick tick = seconds * 1000;
  963. if( sd == NULL )
  964. return;
  965. if( sd->rental_timer != INVALID_TIMER ) {
  966. const struct TimerData * td;
  967. td = get_timer(sd->rental_timer);
  968. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  969. pc_inventory_rental_clear(sd);
  970. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  971. }
  972. } else
  973. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  974. }
  975. /**
  976. * Check if the player can sell the current item
  977. * @param sd: map_session_data of the player
  978. * @param item: struct of the checking item
  979. * @param shoptype: NPC's sub type see enum npc_subtype
  980. * @return bool 'true' is sellable, 'false' otherwise
  981. */
  982. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  983. if (sd == NULL || item == NULL)
  984. return false;
  985. if (!pc_can_give_items(sd))
  986. return false;
  987. if (item->equip > 0 || item->amount < 0)
  988. return false;
  989. if (battle_config.hide_fav_sell && item->favorite)
  990. return false; //Cannot sell favs (optional config)
  991. if (!battle_config.rental_transaction && item->expire_time)
  992. return false; // Cannot Sell Rental Items
  993. if( item->equipSwitch ){
  994. return false;
  995. }
  996. if (itemdb_ishatched_egg(item))
  997. return false;
  998. switch (shoptype) {
  999. case NPCTYPE_SHOP:
  1000. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  1001. item->bound != BOUND_GUILD ||
  1002. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  1003. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  1004. ))
  1005. return true;
  1006. break;
  1007. case NPCTYPE_ITEMSHOP:
  1008. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  1009. item->bound != BOUND_GUILD ||
  1010. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  1011. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  1012. ))
  1013. return true;
  1014. else if (!item->bound) {
  1015. struct item_data *itd = itemdb_search(item->nameid);
  1016. if (itd && itd->flag.trade_restriction.sell && battle_config.allow_bound_sell&ISR_SELLABLE)
  1017. return true;
  1018. }
  1019. break;
  1020. }
  1021. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  1022. return false;
  1023. if (item->bound && !pc_can_give_bounded_items(sd))
  1024. return false; // Don't allow sale of bound items
  1025. return true;
  1026. }
  1027. /**
  1028. * Determines if player can give / drop / trade / vend items
  1029. */
  1030. bool pc_can_give_items(struct map_session_data *sd)
  1031. {
  1032. return pc_has_permission(sd, PC_PERM_TRADE);
  1033. }
  1034. /**
  1035. * Determines if player can give / drop / trade / vend bounded items
  1036. */
  1037. bool pc_can_give_bounded_items(struct map_session_data *sd)
  1038. {
  1039. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  1040. }
  1041. /**
  1042. * Determine if an item in a player's inventory is tradeable based on several merits.
  1043. * Checks for item_trade, bound, and rental restrictions.
  1044. * @param sd: Player data
  1045. * @param index: Item inventory index
  1046. * @return True if the item can be traded or false otherwise
  1047. */
  1048. bool pc_can_trade_item(map_session_data *sd, int index) {
  1049. if (sd && index >= 0) {
  1050. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  1051. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  1052. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  1053. );
  1054. }
  1055. return false;
  1056. }
  1057. /*==========================================
  1058. * Prepares character for saving.
  1059. * @param sd
  1060. *------------------------------------------*/
  1061. void pc_makesavestatus(struct map_session_data *sd) {
  1062. nullpo_retv(sd);
  1063. if(!battle_config.save_clothcolor)
  1064. sd->status.clothes_color = 0;
  1065. if(!battle_config.save_body_style)
  1066. sd->status.body = 0;
  1067. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  1068. //status change load/saving. [Skotlex]
  1069. #ifdef NEW_CARTS
  1070. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1071. #else
  1072. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1073. #endif
  1074. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  1075. if(pc_isdead(sd)){
  1076. pc_setrestartvalue(sd, 0);
  1077. } else {
  1078. sd->status.hp = sd->battle_status.hp;
  1079. sd->status.sp = sd->battle_status.sp;
  1080. sd->status.ap = sd->battle_status.ap;
  1081. }
  1082. sd->status.last_point.map = sd->mapindex;
  1083. sd->status.last_point.x = sd->bl.x;
  1084. sd->status.last_point.y = sd->bl.y;
  1085. return;
  1086. }
  1087. if(pc_isdead(sd)) {
  1088. pc_setrestartvalue(sd, 0);
  1089. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  1090. } else {
  1091. sd->status.hp = sd->battle_status.hp;
  1092. sd->status.sp = sd->battle_status.sp;
  1093. sd->status.ap = sd->battle_status.ap;
  1094. sd->status.last_point.map = sd->mapindex;
  1095. sd->status.last_point.x = sd->bl.x;
  1096. sd->status.last_point.y = sd->bl.y;
  1097. }
  1098. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  1099. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  1100. if(mapdata->save.map)
  1101. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  1102. else
  1103. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  1104. }
  1105. }
  1106. /*==========================================
  1107. * Off init ? Connection?
  1108. *------------------------------------------*/
  1109. 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) {
  1110. nullpo_retv(sd);
  1111. sd->bl.id = account_id;
  1112. sd->status.account_id = account_id;
  1113. sd->status.char_id = char_id;
  1114. sd->status.sex = sex;
  1115. sd->login_id1 = login_id1;
  1116. sd->login_id2 = 0; // at this point, we can not know the value :(
  1117. sd->client_tick = client_tick;
  1118. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  1119. sd->bl.type = BL_PC;
  1120. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  1121. sd->canlog_tick = gettick();
  1122. //Required to prevent homunculus copuing a base speed of 0.
  1123. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  1124. }
  1125. /**
  1126. * Get equip point for an equip
  1127. * @param sd
  1128. * @param id
  1129. */
  1130. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  1131. int ep = 0;
  1132. nullpo_ret(sd);
  1133. nullpo_ret(id);
  1134. if (!itemdb_isequip2(id))
  1135. return 0; //Not equippable by players.
  1136. ep = id->equip;
  1137. if(id->subtype == W_DAGGER || id->subtype == W_1HSWORD || id->subtype == W_1HAXE) {
  1138. 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]
  1139. if (ep == EQP_WEAPON)
  1140. return EQP_ARMS;
  1141. if (ep == EQP_SHADOW_WEAPON)
  1142. return EQP_SHADOW_ARMS;
  1143. }
  1144. }
  1145. return ep;
  1146. }
  1147. /**
  1148. * Get equip point for an equip
  1149. * @param sd
  1150. * @param n Equip index in inventory
  1151. */
  1152. int pc_equippoint(struct map_session_data *sd,int n){
  1153. nullpo_ret(sd);
  1154. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  1155. }
  1156. /**
  1157. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  1158. * @param sd : player session
  1159. * @return 0 sucess, 1:invalid sd
  1160. */
  1161. void pc_setinventorydata(struct map_session_data *sd)
  1162. {
  1163. uint8 i;
  1164. nullpo_retv(sd);
  1165. for(i = 0; i < MAX_INVENTORY; i++) {
  1166. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  1167. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  1168. }
  1169. }
  1170. /**
  1171. * 'Calculates' weapon type
  1172. * @param sd : Player
  1173. */
  1174. void pc_calcweapontype(struct map_session_data *sd)
  1175. {
  1176. nullpo_retv(sd);
  1177. // single-hand
  1178. if(sd->weapontype2 == W_FIST) {
  1179. sd->status.weapon = sd->weapontype1;
  1180. return;
  1181. }
  1182. if(sd->weapontype1 == W_FIST) {
  1183. sd->status.weapon = sd->weapontype2;
  1184. return;
  1185. }
  1186. // dual-wield
  1187. sd->status.weapon = W_FIST;
  1188. switch (sd->weapontype1){
  1189. case W_DAGGER:
  1190. switch (sd->weapontype2) {
  1191. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  1192. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  1193. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  1194. }
  1195. break;
  1196. case W_1HSWORD:
  1197. switch (sd->weapontype2) {
  1198. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  1199. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  1200. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  1201. }
  1202. break;
  1203. case W_1HAXE:
  1204. switch (sd->weapontype2) {
  1205. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  1206. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  1207. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  1208. }
  1209. }
  1210. // unknown, default to right hand type
  1211. if (!sd->status.weapon)
  1212. sd->status.weapon = sd->weapontype1;
  1213. }
  1214. /**
  1215. * Set equip index
  1216. * @param sd : Player
  1217. */
  1218. void pc_setequipindex(struct map_session_data *sd)
  1219. {
  1220. uint16 i;
  1221. nullpo_retv(sd);
  1222. for (i = 0; i < EQI_MAX; i++){
  1223. sd->equip_index[i] = -1;
  1224. sd->equip_switch_index[i] = -1;
  1225. }
  1226. for (i = 0; i < MAX_INVENTORY; i++) {
  1227. if (sd->inventory.u.items_inventory[i].nameid == 0)
  1228. continue;
  1229. if (sd->inventory.u.items_inventory[i].equip) {
  1230. uint8 j;
  1231. for (j = 0; j < EQI_MAX; j++)
  1232. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  1233. sd->equip_index[j] = i;
  1234. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  1235. if (sd->inventory_data[i])
  1236. sd->weapontype1 = sd->inventory_data[i]->subtype;
  1237. else
  1238. sd->weapontype1 = W_FIST;
  1239. }
  1240. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  1241. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  1242. sd->weapontype2 = sd->inventory_data[i]->subtype;
  1243. else
  1244. sd->weapontype2 = W_FIST;
  1245. }
  1246. }
  1247. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  1248. for (uint8 j = 0; j < EQI_MAX; j++) {
  1249. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  1250. sd->equip_switch_index[j] = i;
  1251. }
  1252. }
  1253. }
  1254. }
  1255. pc_calcweapontype(sd);
  1256. }
  1257. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  1258. //{
  1259. // int i;
  1260. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  1261. // std::shared_ptr<item_data> data;
  1262. //
  1263. // //Crafted/made/hatched items.
  1264. // if (itemdb_isspecial(item->card[0]))
  1265. // return 1;
  1266. //
  1267. // /* scan for enchant armor gems */
  1268. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  1269. // s = MAX_SLOTS - 1;
  1270. //
  1271. // ARR_FIND( 0, s, i, item->card[i] && (data = item_db.find(item->card[i])) != NULL && data->flag.no_equip&flag );
  1272. // return( i < s ) ? 0 : 1;
  1273. //}
  1274. /**
  1275. * Check if an item is equiped by player
  1276. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  1277. * @param sd : player session
  1278. * @param nameid : itemid
  1279. * @return 1:yes, 0:no
  1280. */
  1281. bool pc_isequipped(struct map_session_data *sd, t_itemid nameid)
  1282. {
  1283. uint8 i;
  1284. for( i = 0; i < EQI_MAX; i++ )
  1285. {
  1286. short index = sd->equip_index[i], j;
  1287. if( index < 0 )
  1288. continue;
  1289. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  1290. continue;
  1291. if( !sd->inventory_data[index] )
  1292. continue;
  1293. if( sd->inventory_data[index]->nameid == nameid )
  1294. return true;
  1295. for( j = 0; j < MAX_SLOTS; j++ ){
  1296. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  1297. return true;
  1298. }
  1299. }
  1300. return false;
  1301. }
  1302. /**
  1303. * Check adoption rules
  1304. * @param p1_sd: Player 1
  1305. * @param p2_sd: Player 2
  1306. * @param b_sd: Player that will be adopted
  1307. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  1308. * ADOPT_ALREADY_ADOPTED - Already adopted
  1309. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  1310. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  1311. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  1312. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  1313. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  1314. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  1315. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  1316. */
  1317. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1318. {
  1319. if( !p1_sd || !p2_sd || !b_sd )
  1320. return ADOPT_CHARACTER_NOT_FOUND;
  1321. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1322. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1323. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1324. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1325. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1326. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1327. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1328. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1329. // Parents need to have their ring equipped
  1330. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1331. return ADOPT_EQUIP_RINGS;
  1332. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1333. return ADOPT_EQUIP_RINGS;
  1334. // Already adopted a baby
  1335. if( p1_sd->status.child || p2_sd->status.child ) {
  1336. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1337. return ADOPT_MORE_CHILDREN;
  1338. }
  1339. // Parents need at least lvl 70 to adopt
  1340. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1341. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1342. return ADOPT_LEVEL_70;
  1343. }
  1344. if( b_sd->status.partner_id ) {
  1345. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1346. return ADOPT_MARRIED;
  1347. }
  1348. 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 ) )
  1349. return ADOPT_NOT_NOVICE;
  1350. return ADOPT_ALLOWED;
  1351. }
  1352. /*==========================================
  1353. * Adoption Process
  1354. *------------------------------------------*/
  1355. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1356. {
  1357. int job, joblevel;
  1358. t_exp jobexp;
  1359. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1360. return false;
  1361. // Preserve current job levels and progress
  1362. joblevel = b_sd->status.job_level;
  1363. jobexp = b_sd->status.job_exp;
  1364. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1365. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1366. { // Success, proceed to configure parents and baby skills
  1367. p1_sd->status.child = b_sd->status.char_id;
  1368. p2_sd->status.child = b_sd->status.char_id;
  1369. b_sd->status.father = p1_sd->status.char_id;
  1370. b_sd->status.mother = p2_sd->status.char_id;
  1371. // Restore progress
  1372. b_sd->status.job_level = joblevel;
  1373. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1374. b_sd->status.job_exp = jobexp;
  1375. clif_updatestatus(b_sd, SP_JOBEXP);
  1376. // Baby Skills
  1377. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1378. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1379. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1380. // Parents Skills
  1381. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1382. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1383. chrif_save(p1_sd, CSAVE_NORMAL);
  1384. chrif_save(p2_sd, CSAVE_NORMAL);
  1385. chrif_save(b_sd, CSAVE_NORMAL);
  1386. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1387. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1388. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1389. return true;
  1390. }
  1391. return false; // Job Change Fail
  1392. }
  1393. static bool pc_job_can_use_item( struct map_session_data* sd, struct item_data* item ){
  1394. nullpo_retr( false, sd );
  1395. nullpo_retr( false, item );
  1396. // Calculate the required bit to check
  1397. uint64 job = 1ULL << ( sd->class_ & MAPID_BASEMASK );
  1398. size_t index;
  1399. // 2-1
  1400. if( ( sd->class_ & JOBL_2_1 ) != 0 ){
  1401. index = 1;
  1402. // 2-2
  1403. }else if( ( sd->class_ & JOBL_2_2 ) != 0 ){
  1404. index = 2;
  1405. // Basejob
  1406. }else{
  1407. index = 0;
  1408. }
  1409. return ( item->class_base[index] & job ) != 0;
  1410. }
  1411. /*==========================================
  1412. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1413. Returns:
  1414. false : Cannot use/equip
  1415. true : Can use/equip
  1416. * Credits:
  1417. [Inkfish] for first idea
  1418. [Haru] for third-classes extension
  1419. [Cydh] finishing :D
  1420. *------------------------------------------*/
  1421. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  1422. while (1) {
  1423. 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)
  1424. break;
  1425. #ifndef RENEWAL
  1426. //allow third classes to use trans. class items
  1427. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1428. break;
  1429. //third-baby classes can use same item too
  1430. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1431. break;
  1432. //don't need to decide specific rules for third-classes?
  1433. //items for third classes can be used for all third classes
  1434. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_UPPER|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1435. break;
  1436. #else
  1437. //trans. classes (exl. third-trans.)
  1438. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1439. break;
  1440. //baby classes (exl. third-baby)
  1441. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1442. break;
  1443. //third classes (exl. third-trans. and baby-third and fourth)
  1444. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)) && !(sd->class_&JOBL_FOURTH))
  1445. break;
  1446. //trans-third classes (exl. fourth)
  1447. if (item->class_upper&ITEMJ_THIRD_UPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_FOURTH))
  1448. break;
  1449. //third-baby classes (exl. fourth)
  1450. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY && !(sd->class_&JOBL_FOURTH))
  1451. break;
  1452. //fourth classes
  1453. if (item->class_upper&ITEMJ_FOURTH && sd->class_&JOBL_FOURTH)
  1454. break;
  1455. #endif
  1456. return false;
  1457. }
  1458. return true;
  1459. }
  1460. /*=================================================
  1461. * Checks if the player can equip the item at index n in inventory.
  1462. * @param sd
  1463. * @param n Item index in inventory
  1464. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1465. *------------------------------------------------*/
  1466. uint8 pc_isequip(struct map_session_data *sd,int n)
  1467. {
  1468. struct item_data *item;
  1469. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1470. item = sd->inventory_data[n];
  1471. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1472. return ITEM_EQUIP_ACK_OK;
  1473. if(item == NULL)
  1474. return ITEM_EQUIP_ACK_FAIL;
  1475. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1476. return ITEM_EQUIP_ACK_FAILLEVEL;
  1477. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1478. return ITEM_EQUIP_ACK_FAILLEVEL;
  1479. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1480. return ITEM_EQUIP_ACK_FAIL;
  1481. //fail to equip if item is restricted
  1482. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1483. return ITEM_EQUIP_ACK_FAIL;
  1484. if (item->equip&EQP_AMMO) {
  1485. switch (item->subtype) {
  1486. case AMMO_ARROW:
  1487. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1488. clif_msg(sd, ITEM_NEED_BOW);
  1489. return ITEM_EQUIP_ACK_FAIL;
  1490. }
  1491. break;
  1492. case AMMO_DAGGER:
  1493. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1494. return ITEM_EQUIP_ACK_FAIL;
  1495. break;
  1496. case AMMO_BULLET:
  1497. case AMMO_SHELL:
  1498. 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
  1499. #ifdef RENEWAL
  1500. && sd->status.weapon != W_GRENADE
  1501. #endif
  1502. ) {
  1503. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1504. return ITEM_EQUIP_ACK_FAIL;
  1505. }
  1506. break;
  1507. #ifndef RENEWAL
  1508. case AMMO_GRENADE:
  1509. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1510. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1511. return ITEM_EQUIP_ACK_FAIL;
  1512. }
  1513. break;
  1514. #endif
  1515. case AMMO_CANNONBALL:
  1516. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1517. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1518. return ITEM_EQUIP_ACK_FAIL;
  1519. }
  1520. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1521. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1522. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1523. return ITEM_EQUIP_ACK_FAIL;
  1524. }
  1525. break;
  1526. }
  1527. }
  1528. if (sd->sc.count) {
  1529. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1530. return ITEM_EQUIP_ACK_FAIL;
  1531. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1532. return ITEM_EQUIP_ACK_FAIL;
  1533. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1534. return ITEM_EQUIP_ACK_FAIL;
  1535. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1536. return ITEM_EQUIP_ACK_FAIL;
  1537. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1538. return ITEM_EQUIP_ACK_FAIL;
  1539. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1540. return ITEM_EQUIP_ACK_FAIL;
  1541. if (item->equip & EQP_SHADOW_GEAR && sd->sc.data[SC_SHADOW_STRIP])
  1542. return ITEM_EQUIP_ACK_FAIL;
  1543. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1544. return ITEM_EQUIP_ACK_FAIL;
  1545. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1546. //Spirit of Super Novice equip bonuses. [Skotlex]
  1547. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1548. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1549. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->weapon_level == 4)
  1550. switch(item->subtype) { //In weapons, the look determines type of weapon.
  1551. case W_DAGGER: //All level 4 - Daggers
  1552. case W_1HSWORD: //All level 4 - 1H Swords
  1553. case W_1HAXE: //All level 4 - 1H Axes
  1554. case W_MACE: //All level 4 - 1H Maces
  1555. case W_STAFF: //All level 4 - 1H Staves
  1556. case W_2HSTAFF: //All level 4 - 2H Staves
  1557. return ITEM_EQUIP_ACK_OK;
  1558. }
  1559. }
  1560. }
  1561. //Not equipable by class. [Skotlex]
  1562. if (!pc_job_can_use_item(sd,item))
  1563. return ITEM_EQUIP_ACK_FAIL;
  1564. if (!pc_isItemClass(sd, item))
  1565. return ITEM_EQUIP_ACK_FAIL;
  1566. return ITEM_EQUIP_ACK_OK;
  1567. }
  1568. /*==========================================
  1569. * No problem with the session id
  1570. * set the status that has been sent from char server
  1571. *------------------------------------------*/
  1572. 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)
  1573. {
  1574. t_tick tick = gettick();
  1575. uint32 ip = session[sd->fd]->client_addr;
  1576. sd->login_id2 = login_id2;
  1577. sd->group_id = group_id;
  1578. /* load user permissions */
  1579. pc_group_pc_load(sd);
  1580. memcpy(&sd->status, st, sizeof(*st));
  1581. if (st->sex != sd->status.sex) {
  1582. clif_authfail_fd(sd->fd, 0);
  1583. return false;
  1584. }
  1585. //Set the map-server used job id. [Skotlex]
  1586. uint64 class_ = pc_jobid2mapid(sd->status.class_);
  1587. if (class_ == -1 || !job_db.exists(sd->status.class_)) { //Invalid class?
  1588. 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);
  1589. sd->status.class_ = JOB_NOVICE;
  1590. sd->class_ = MAPID_NOVICE;
  1591. } else
  1592. sd->class_ = class_;
  1593. // Checks and fixes to character status data, that are required
  1594. // in case of configuration change or stuff, which cannot be
  1595. // checked on char-server.
  1596. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1597. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1598. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1599. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1600. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1601. sd->state.connect_new = 1;
  1602. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1603. sd->invincible_timer = INVALID_TIMER;
  1604. sd->npc_timer_id = INVALID_TIMER;
  1605. sd->pvp_timer = INVALID_TIMER;
  1606. sd->expiration_tid = INVALID_TIMER;
  1607. sd->autotrade_tid = INVALID_TIMER;
  1608. sd->respawn_tid = INVALID_TIMER;
  1609. sd->tid_queue_active = INVALID_TIMER;
  1610. sd->macro_detect.timer = INVALID_TIMER;
  1611. sd->skill_keep_using.tid = INVALID_TIMER;
  1612. sd->skill_keep_using.skill_id = 0;
  1613. sd->skill_keep_using.level = 0;
  1614. sd->skill_keep_using.target = 0;
  1615. #ifdef SECURE_NPCTIMEOUT
  1616. // Initialize to defaults/expected
  1617. sd->npc_idle_timer = INVALID_TIMER;
  1618. sd->npc_idle_tick = tick;
  1619. sd->npc_idle_type = NPCT_INPUT;
  1620. sd->state.ignoretimeout = false;
  1621. #endif
  1622. sd->canuseitem_tick = tick;
  1623. sd->canusecashfood_tick = tick;
  1624. sd->canequip_tick = tick;
  1625. sd->cantalk_tick = tick;
  1626. sd->canskill_tick = tick;
  1627. sd->cansendmail_tick = tick;
  1628. sd->idletime = last_tick;
  1629. sd->regen.tick.hp = tick;
  1630. sd->regen.tick.sp = tick;
  1631. for(int i = 0; i < MAX_SPIRITBALL; i++)
  1632. sd->spirit_timer[i] = INVALID_TIMER;
  1633. if (battle_config.item_auto_get)
  1634. sd->state.autoloot = 10000;
  1635. if (battle_config.disp_experience)
  1636. sd->state.showexp = 1;
  1637. if (battle_config.disp_zeny)
  1638. sd->state.showzeny = 1;
  1639. #ifdef VIP_ENABLE
  1640. if (!battle_config.vip_disp_rate)
  1641. sd->vip.disableshowrate = 1;
  1642. #endif
  1643. if (!(battle_config.display_skill_fail&2))
  1644. sd->state.showdelay = 1;
  1645. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1646. memset(&sd->cart, 0, sizeof(struct s_storage));
  1647. memset(&sd->storage, 0, sizeof(struct s_storage));
  1648. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1649. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1650. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1651. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1652. sd->status.option &= ~OPTION_INVISIBLE;
  1653. }
  1654. status_change_init(&sd->bl);
  1655. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1656. unit_dataset(&sd->bl);
  1657. sd->guild_x = -1;
  1658. sd->guild_y = -1;
  1659. sd->delayed_damage = 0;
  1660. // Event Timers
  1661. for( int i = 0; i < MAX_EVENTTIMER; i++ )
  1662. sd->eventtimer[i] = INVALID_TIMER;
  1663. // Rental Timer
  1664. sd->rental_timer = INVALID_TIMER;
  1665. for( int i = 0; i < 3; i++ )
  1666. sd->hate_mob[i] = -1;
  1667. sd->quest_log = NULL;
  1668. sd->num_quests = 0;
  1669. sd->avail_quests = 0;
  1670. sd->save_quest = false;
  1671. sd->count_rewarp = 0;
  1672. sd->mail.pending_weight = 0;
  1673. sd->mail.pending_zeny = 0;
  1674. sd->mail.pending_slots = 0;
  1675. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1676. sd->regs.arrays = NULL;
  1677. sd->vars_dirty = false;
  1678. sd->vars_ok = false;
  1679. sd->vars_received = 0x0;
  1680. //warp player
  1681. 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 );
  1682. if( setpos_result != SETPOS_OK ){
  1683. 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 );
  1684. // try warping to a default map instead (church graveyard)
  1685. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1686. // if we fail again
  1687. clif_authfail_fd(sd->fd, 0);
  1688. return false;
  1689. }
  1690. }
  1691. clif_inventory_expansion_info( sd );
  1692. clif_authok(sd);
  1693. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1694. sd->die_counter=-1;
  1695. //display login notice
  1696. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1697. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1698. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1699. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1700. sd->status.name, sd->status.account_id, sd->status.char_id,
  1701. CONVIP(ip), sd->group_id);
  1702. // Send friends list
  1703. clif_friendslist_send( *sd );
  1704. if( !changing_mapservers ) {
  1705. if (battle_config.display_version == 1)
  1706. pc_show_version(sd);
  1707. // Message of the Day [Valaris]
  1708. for(int i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1709. if (battle_config.motd_type)
  1710. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1711. else
  1712. clif_displaymessage(sd->fd, motd_text[i]);
  1713. }
  1714. if (expiration_time != 0)
  1715. sd->expiration_time = expiration_time;
  1716. /**
  1717. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1718. **/
  1719. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1720. }
  1721. pc_validate_skill(sd);
  1722. /* [Ind] */
  1723. sd->sc_display = NULL;
  1724. sd->sc_display_count = 0;
  1725. // Player has not yet received the CashShop list
  1726. sd->status.cashshop_sent = false;
  1727. sd->last_addeditem_index = -1;
  1728. sd->bonus_script.head = NULL;
  1729. sd->bonus_script.count = 0;
  1730. // Initialize BG queue
  1731. sd->bg_queue_id = 0;
  1732. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  1733. sd->hatEffects = {};
  1734. #endif
  1735. sd->catch_target_class = PET_CATCH_FAIL;
  1736. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1737. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1738. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1739. clif_updatestatus(sd, SP_BASEEXP);
  1740. }
  1741. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1742. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1743. clif_updatestatus(sd, SP_JOBEXP);
  1744. }
  1745. // Request all registries (auth is considered completed whence they arrive)
  1746. intif_request_registry(sd,7);
  1747. return true;
  1748. }
  1749. /*==========================================
  1750. * Closes a connection because it failed to be authenticated from the char server.
  1751. *------------------------------------------*/
  1752. void pc_authfail(struct map_session_data *sd)
  1753. {
  1754. clif_authfail_fd(sd->fd, 0);
  1755. return;
  1756. }
  1757. /**
  1758. * Player register a bl as hatred
  1759. * @param sd : player session
  1760. * @param pos : hate position [0;2]
  1761. * @param bl : target bl
  1762. * @return false:failed, true:success
  1763. */
  1764. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1765. {
  1766. int class_;
  1767. if (!sd || !bl || pos < 0 || pos > 2)
  1768. return false;
  1769. if (sd->hate_mob[pos] != -1)
  1770. { //Can't change hate targets.
  1771. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1772. return false;
  1773. }
  1774. class_ = status_get_class(bl);
  1775. if (!pcdb_checkid(class_)) {
  1776. unsigned int max_hp = status_get_max_hp(bl);
  1777. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1778. return false;
  1779. if (pos != status_get_size(bl))
  1780. return false; //Wrong size
  1781. }
  1782. sd->hate_mob[pos] = class_;
  1783. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1784. clif_hate_info(sd, pos, class_, 1);
  1785. return true;
  1786. }
  1787. /*==========================================
  1788. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1789. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1790. * See intif_parse_StorageReceived() for item operations [lighta]
  1791. *------------------------------------------*/
  1792. void pc_reg_received(struct map_session_data *sd)
  1793. {
  1794. uint8 i;
  1795. sd->vars_ok = true;
  1796. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1797. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1798. sd->change_level_4th = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE4TH_VAR)));
  1799. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1800. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1801. if (msg_checklangtype(sd->langtype,true) < 0)
  1802. sd->langtype = 0; //invalid langtype reset to default
  1803. // Cash shop
  1804. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1805. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1806. // Cooking Exp
  1807. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1808. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1809. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1810. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1811. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1812. }
  1813. if (battle_config.feature_banking)
  1814. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1815. if (battle_config.feature_roulette) {
  1816. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1817. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1818. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1819. }
  1820. sd->roulette.prizeIdx = -1;
  1821. //SG map and mob read [Komurka]
  1822. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1823. uint16 j;
  1824. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1825. sd->feel_map[i].index = j;
  1826. sd->feel_map[i].m = map_mapindex2mapid(j);
  1827. } else {
  1828. sd->feel_map[i].index = 0;
  1829. sd->feel_map[i].m = -1;
  1830. }
  1831. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1832. }
  1833. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1834. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1835. sd->cloneskill_idx = skill_get_index(skid);
  1836. if (sd->cloneskill_idx > 0) {
  1837. sd->status.skill[sd->cloneskill_idx].id = skid;
  1838. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1839. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1840. sd->status.skill[sd->cloneskill_idx].lv = i;
  1841. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1842. }
  1843. }
  1844. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1845. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1846. sd->reproduceskill_idx = skill_get_index(skid);
  1847. if (sd->reproduceskill_idx > 0) {
  1848. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1849. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1850. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1851. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1852. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1853. }
  1854. }
  1855. //Weird... maybe registries were reloaded?
  1856. if (sd->state.active)
  1857. return;
  1858. sd->state.active = 1;
  1859. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1860. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1861. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1862. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1863. // Restore IM_CHAR instance to the player
  1864. for (const auto &instance : instances) {
  1865. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1866. sd->instance_id = instance.first;
  1867. break;
  1868. }
  1869. }
  1870. if (sd->status.party_id > 0)
  1871. party_member_joined(sd);
  1872. if (sd->status.guild_id > 0)
  1873. guild_member_joined(sd);
  1874. if (sd->status.clan_id > 0)
  1875. clan_member_joined(sd);
  1876. #if !( PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410 )
  1877. // 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
  1878. clif_instance_info( *sd );
  1879. #endif
  1880. // pet
  1881. if (sd->status.pet_id > 0)
  1882. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1883. // Homunculus [albator]
  1884. if( sd->status.hom_id > 0 )
  1885. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1886. if( sd->status.mer_id > 0 )
  1887. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1888. if( sd->status.ele_id > 0 )
  1889. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1890. map_addiddb(&sd->bl);
  1891. map_delnickdb(sd->status.char_id, sd->status.name);
  1892. if (!chrif_auth_finished(sd))
  1893. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1894. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1895. chrif_bsdata_request(sd->status.char_id);
  1896. #ifdef VIP_ENABLE
  1897. sd->vip.time = 0;
  1898. sd->vip.enabled = 0;
  1899. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1900. #endif
  1901. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1902. intif_request_questlog(sd);
  1903. if (battle_config.feature_achievement) {
  1904. sd->achievement_data.total_score = 0;
  1905. sd->achievement_data.level = 0;
  1906. sd->achievement_data.save = false;
  1907. sd->achievement_data.count = 0;
  1908. sd->achievement_data.incompleteCount = 0;
  1909. sd->achievement_data.achievements = NULL;
  1910. intif_request_achievements(sd->status.char_id);
  1911. }
  1912. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1913. sd->state.connect_new = 1;
  1914. clif_parse_LoadEndAck(sd->fd, sd);
  1915. if( pc_isinvisible( sd ) ){
  1916. sd->vd.class_ = JT_INVISIBLE;
  1917. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1918. // decrement the number of pvp players on the map
  1919. map_getmapdata( sd->bl.m )->users_pvp--;
  1920. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1921. // unregister the player for ranking
  1922. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1923. sd->pvp_timer = INVALID_TIMER;
  1924. }
  1925. clif_changeoption( &sd->bl );
  1926. }
  1927. }
  1928. channel_autojoin(sd);
  1929. }
  1930. static int pc_calc_skillpoint(struct map_session_data* sd)
  1931. {
  1932. uint16 i, skill_point = 0;
  1933. nullpo_ret(sd);
  1934. for(i = 1; i < MAX_SKILL; i++) {
  1935. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1936. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1937. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1938. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1939. )
  1940. {
  1941. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1942. skill_point += sd->status.skill[i].lv;
  1943. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1944. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1945. }
  1946. }
  1947. }
  1948. return skill_point;
  1949. }
  1950. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1951. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1952. return false;
  1953. /**
  1954. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1955. * skill tree and since they have no icons they'll give resource errors
  1956. * Get ALL skills except npc/guild ones. [Skotlex]
  1957. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1958. **/
  1959. for (const auto &skill : skill_db) {
  1960. uint16 skill_id = skill.second->nameid;
  1961. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1962. continue;
  1963. switch (skill_id) {
  1964. case SM_SELFPROVOKE:
  1965. case AB_DUPLELIGHT_MELEE:
  1966. case AB_DUPLELIGHT_MAGIC:
  1967. case WL_CHAINLIGHTNING_ATK:
  1968. case WL_TETRAVORTEX_FIRE:
  1969. case WL_TETRAVORTEX_WATER:
  1970. case WL_TETRAVORTEX_WIND:
  1971. case WL_TETRAVORTEX_GROUND:
  1972. case WL_SUMMON_ATK_FIRE:
  1973. case WL_SUMMON_ATK_WIND:
  1974. case WL_SUMMON_ATK_WATER:
  1975. case WL_SUMMON_ATK_GROUND:
  1976. case LG_OVERBRAND_BRANDISH:
  1977. case LG_OVERBRAND_PLUSATK:
  1978. case WM_SEVERE_RAINSTORM_MELEE:
  1979. case RL_R_TRIP_PLUSATK:
  1980. case SG_DEVIL:
  1981. case MO_TRIPLEATTACK:
  1982. case RG_SNATCHER:
  1983. continue;
  1984. default:
  1985. {
  1986. uint8 lv = (uint8)skill_get_max(skill_id);
  1987. if (lv > 0) {
  1988. uint16 idx = skill_get_index(skill_id);
  1989. sd->status.skill[idx].id = skill_id;
  1990. if (addlv)
  1991. sd->status.skill[idx].lv = lv;
  1992. }
  1993. }
  1994. break;
  1995. }
  1996. }
  1997. return true;
  1998. }
  1999. /*==========================================
  2000. * Calculation of skill level.
  2001. * @param sd
  2002. *------------------------------------------*/
  2003. void pc_calc_skilltree(struct map_session_data *sd)
  2004. {
  2005. nullpo_retv(sd);
  2006. uint64 job = pc_calc_skilltree_normalize_job(sd);
  2007. int class_ = pc_mapid2jobid(job, sd->status.sex);
  2008. if( class_ == -1 )
  2009. { //Unable to normalize job??
  2010. 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 );
  2011. return;
  2012. }
  2013. uint16 job_id = class_;
  2014. class_ = pc_class2idx(class_);
  2015. for (const auto &skill : skill_db) {
  2016. uint16 skill_id = skill.second->nameid;
  2017. uint16 idx = skill_get_index(skill_id);
  2018. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  2019. sd->status.skill[idx].id = 0; //First clear skills.
  2020. /* permanent skills that must be re-checked */
  2021. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  2022. if (skill_id == 0) {
  2023. sd->status.skill[idx].id = 0;
  2024. sd->status.skill[idx].lv = 0;
  2025. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2026. continue;
  2027. }
  2028. switch (skill_id) {
  2029. case NV_TRICKDEAD:
  2030. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  2031. sd->status.skill[idx].id = 0;
  2032. sd->status.skill[idx].lv = 0;
  2033. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2034. }
  2035. break;
  2036. }
  2037. }
  2038. }
  2039. for (const auto &skill : skill_db) {
  2040. uint16 idx = skill_get_index(skill.second->nameid);
  2041. // Restore original level of skills after deleting earned skills.
  2042. 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 ) {
  2043. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2044. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2045. }
  2046. }
  2047. // Removes Taekwon Ranker skill bonus
  2048. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  2049. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(JOB_TAEKWON);
  2050. if (tree != nullptr && !tree->skills.empty()) {
  2051. for (const auto &it : tree->skills) {
  2052. uint16 sk_idx = skill_get_index(it.first);
  2053. if (sk_idx == 0)
  2054. continue;
  2055. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  2056. if (it.first == NV_BASIC || it.first == NV_FIRSTAID || it.first == WE_CALLBABY)
  2057. continue;
  2058. sd->status.skill[sk_idx].id = 0;
  2059. }
  2060. }
  2061. }
  2062. }
  2063. // Grant all skills
  2064. pc_grant_allskills(sd, false);
  2065. int flag;
  2066. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2067. do {
  2068. flag = 0;
  2069. if (tree == nullptr || tree->skills.empty())
  2070. break;
  2071. for (const auto &skillsit : tree->skills) {
  2072. bool fail = false;
  2073. uint16 skid = skillsit.first;
  2074. uint16 sk_idx = skill_get_index(skid);
  2075. if (sd->status.skill[sk_idx].id)
  2076. continue; //Skill already known.
  2077. if (!battle_config.skillfree) {
  2078. // Checking required skills
  2079. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2080. if (entry != nullptr && !entry->need.empty()) {
  2081. for (const auto &it : entry->need) {
  2082. uint16 sk_need_id = it.first;
  2083. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2084. if (sk_need_idx > 0) {
  2085. uint16 sk_need = sk_need_id;
  2086. 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)
  2087. sk_need = 0; //Not learned.
  2088. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  2089. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2090. else
  2091. sk_need = pc_checkskill(sd,sk_need_id);
  2092. if (sk_need < it.second) {
  2093. fail = true;
  2094. break;
  2095. }
  2096. }
  2097. }
  2098. }
  2099. if (sd->status.base_level < entry->baselv) { //We need to get the actual class in this case
  2100. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2101. c_ = pc_class2idx(c_);
  2102. if (class_ == c_ || (class_ != c_ && sd->status.base_level < entry->baselv))
  2103. fail = true; // base level requirement wasn't satisfied
  2104. }
  2105. if (sd->status.job_level < entry->joblv) { //We need to get the actual class in this case
  2106. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2107. c_ = pc_class2idx(c_);
  2108. if (class_ == c_ || (class_ != c_ && sd->status.job_level < entry->joblv))
  2109. fail = true; // job level requirement wasn't satisfied
  2110. }
  2111. }
  2112. if (!fail) {
  2113. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2114. if (!sd->status.skill[sk_idx].lv && (
  2115. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2116. skill->inf2[INF2_ISWEDDING] ||
  2117. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  2118. ))
  2119. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  2120. sd->status.skill[sk_idx].id = skid;
  2121. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  2122. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  2123. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  2124. }
  2125. flag = 1; // skill list has changed, perform another pass
  2126. }
  2127. }
  2128. } while(flag);
  2129. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  2130. uint16 skid = 0;
  2131. /* Taekwon Ranker Bonus Skill Tree
  2132. ============================================
  2133. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  2134. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  2135. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  2136. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2137. if (tree != nullptr && !tree->skills.empty()) {
  2138. for (const auto &it : tree->skills) {
  2139. skid = it.first;
  2140. uint16 sk_idx = skill_get_index(skid);
  2141. if (sk_idx == 0)
  2142. continue;
  2143. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  2144. continue; //Do not include Quest/Wedding skills.
  2145. if( sd->status.skill[sk_idx].id == 0 ) {
  2146. sd->status.skill[sk_idx].id = skid;
  2147. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  2148. } else if( skid != NV_BASIC )
  2149. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  2150. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  2151. }
  2152. }
  2153. }
  2154. // Enable Bard/Dancer spirit linked skills.
  2155. if (sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER) {
  2156. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  2157. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  2158. { BA_POEMBRAGI, DC_FORTUNEKISS },
  2159. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  2160. for (const auto &skill : linked_skills) {
  2161. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  2162. continue;
  2163. // Tag it as a non-savable, non-uppable, bonus skill
  2164. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  2165. }
  2166. }
  2167. }
  2168. //Checks if you can learn a new skill after having leveled up a skill.
  2169. static void pc_check_skilltree(struct map_session_data *sd)
  2170. {
  2171. int flag = 0;
  2172. if (battle_config.skillfree)
  2173. return; //Function serves no purpose if this is set
  2174. int c = pc_mapid2jobid( pc_calc_skilltree_normalize_job( sd ), sd->status.sex );
  2175. if (c == -1) { //Unable to normalize job??
  2176. 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);
  2177. return;
  2178. }
  2179. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(c);
  2180. if (tree == nullptr || tree->skills.empty())
  2181. return;
  2182. do {
  2183. flag = 0;
  2184. for (const auto &skillsit : tree->skills) {
  2185. uint16 skid = skillsit.first;
  2186. uint16 sk_idx = skill_get_index(skid);
  2187. bool fail = false;
  2188. if (sd->status.skill[sk_idx].id) //Already learned
  2189. continue;
  2190. // Checking required skills
  2191. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2192. if (entry != nullptr && !entry->need.empty()) {
  2193. for (const auto &it : entry->need) {
  2194. uint16 sk_need_id = it.first;
  2195. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2196. if (sk_need_id > 0) {
  2197. short sk_need = sk_need_id;
  2198. 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)
  2199. sk_need = 0; //Not learned.
  2200. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  2201. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2202. else
  2203. sk_need = pc_checkskill(sd,sk_need_id);
  2204. if (sk_need < it.second) {
  2205. fail = true;
  2206. break;
  2207. }
  2208. }
  2209. }
  2210. }
  2211. if( fail )
  2212. continue;
  2213. if (sd->status.base_level < entry->baselv || sd->status.job_level < entry->joblv)
  2214. continue;
  2215. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2216. if( !sd->status.skill[sk_idx].lv && (
  2217. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2218. skill->inf2[INF2_ISWEDDING] ||
  2219. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  2220. ) )
  2221. continue; //Cannot be learned via normal means.
  2222. sd->status.skill[sk_idx].id = skid;
  2223. flag = 1;
  2224. }
  2225. } while(flag);
  2226. }
  2227. // Make sure all the skills are in the correct condition
  2228. // before persisting to the backend.. [MouseJstr]
  2229. void pc_clean_skilltree(struct map_session_data *sd)
  2230. {
  2231. uint16 i;
  2232. for (i = 0; i < MAX_SKILL; i++){
  2233. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  2234. sd->status.skill[i].id = 0;
  2235. sd->status.skill[i].lv = 0;
  2236. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2237. }
  2238. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  2239. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  2240. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2241. }
  2242. }
  2243. }
  2244. uint64 pc_calc_skilltree_normalize_job_sub( struct map_session_data *sd ){
  2245. int skill_point = pc_calc_skillpoint( sd );
  2246. if( sd->class_ & MAPID_SUMMONER ){
  2247. // Novice's skill points for basic skill.
  2248. std::shared_ptr<s_job_info> summoner_job = job_db.find( JOB_SUMMONER );
  2249. int summoner_skills = summoner_job->max_job_level - 1;
  2250. if( skill_point < summoner_skills ){
  2251. return MAPID_SUMMONER;
  2252. }
  2253. skill_point -= summoner_skills;
  2254. }else{
  2255. // Novice's skill points for basic skill.
  2256. std::shared_ptr<s_job_info> novice_job = job_db.find( JOB_NOVICE );
  2257. int novice_skills = novice_job->max_job_level - 1;
  2258. if( skill_point < novice_skills ){
  2259. return MAPID_NOVICE;
  2260. }
  2261. skill_point -= novice_skills;
  2262. }
  2263. // 1st Class Job LV Check
  2264. if( sd->class_ & JOBL_2 && ( sd->class_ & MAPID_UPPERMASK ) != MAPID_SUPER_NOVICE ){
  2265. uint64 mapid_1st = sd->class_ & MAPID_BASEMASK;
  2266. int class_1st = pc_mapid2jobid( mapid_1st, sd->status.sex );
  2267. if( !sd->change_level_2nd ){
  2268. if( class_1st >= JOB_NOVICE ){
  2269. sd->change_level_2nd = job_db.find( class_1st )->max_job_level;
  2270. }else{
  2271. sd->change_level_2nd = 0;
  2272. }
  2273. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  2274. }
  2275. if( class_1st > 0 && skill_point < ( sd->change_level_2nd - 1 ) ){
  2276. return mapid_1st;
  2277. }
  2278. skill_point -= ( sd->change_level_2nd - 1 );
  2279. }
  2280. // 2nd Class Job LV Check
  2281. if( sd->class_ & JOBL_THIRD && (sd->class_ & MAPID_THIRDMASK) != MAPID_SUPER_NOVICE_E ){
  2282. uint64 mapid_2nd = sd->class_ & MAPID_UPPERMASK;
  2283. int class_2nd = pc_mapid2jobid( mapid_2nd, sd->status.sex );
  2284. if( !sd->change_level_3rd ){
  2285. if( class_2nd >= JOB_NOVICE ){
  2286. sd->change_level_3rd = job_db.find( class_2nd )->max_job_level;
  2287. }else{
  2288. sd->change_level_3rd = 0;
  2289. }
  2290. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  2291. }
  2292. if( class_2nd > 0 && skill_point < ( sd->change_level_3rd - 1 ) ){
  2293. return mapid_2nd;
  2294. }
  2295. skill_point -= ( sd->change_level_3rd - 1 );
  2296. }
  2297. // 3rd Class Job LV Check
  2298. if( sd->class_ & JOBL_FOURTH ){
  2299. uint64 mapid_3rd = sd->class_ & MAPID_THIRDMASK | JOBL_THIRD;
  2300. int class_3rd = pc_mapid2jobid( mapid_3rd, sd->status.sex );
  2301. if( !sd->change_level_4th ){
  2302. if( class_3rd >= JOB_NOVICE ){
  2303. sd->change_level_4th = job_db.find( class_3rd )->max_job_level;
  2304. }else{
  2305. sd->change_level_4th = 0;
  2306. }
  2307. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ), sd->change_level_4th );
  2308. }
  2309. if( class_3rd > 0 && skill_point < ( sd->change_level_4th - 1 ) ){
  2310. return mapid_3rd;
  2311. }
  2312. skill_point -= ( sd->change_level_4th - 1 );
  2313. }
  2314. return sd->class_;
  2315. }
  2316. uint64 pc_calc_skilltree_normalize_job( struct map_session_data *sd ){
  2317. if( !battle_config.skillup_limit || pc_has_permission( sd, PC_PERM_ALL_SKILL ) ){
  2318. return sd->class_;
  2319. }
  2320. uint64 c = pc_calc_skilltree_normalize_job_sub( sd );
  2321. // Special Masks
  2322. if( sd->class_&JOBL_UPPER ){
  2323. if( pc_mapid2jobid( c | JOBL_UPPER, sd->status.sex ) >= JOB_NOVICE ){
  2324. c |= JOBL_UPPER;// Rebirth Job
  2325. }
  2326. }
  2327. if( sd->class_&JOBL_BABY ){
  2328. if( pc_mapid2jobid( c | JOBL_BABY, sd->status.sex ) >= JOB_NOVICE ){
  2329. c |= JOBL_BABY;// Baby Job
  2330. }
  2331. }
  2332. return c;
  2333. }
  2334. /*==========================================
  2335. * Updates the weight status
  2336. *------------------------------------------
  2337. * 1: overweight 50% for pre-renewal and 70% for renewal
  2338. * 2: overweight 90%
  2339. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  2340. */
  2341. void pc_updateweightstatus(struct map_session_data *sd)
  2342. {
  2343. int old_overweight;
  2344. int new_overweight;
  2345. nullpo_retv(sd);
  2346. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  2347. #ifdef RENEWAL
  2348. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  2349. #else
  2350. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  2351. #endif
  2352. if( old_overweight == new_overweight )
  2353. return; // no change
  2354. // stop old status change
  2355. if( old_overweight == 1 )
  2356. status_change_end(&sd->bl, SC_WEIGHT50);
  2357. else if( old_overweight == 2 )
  2358. status_change_end(&sd->bl, SC_WEIGHT90);
  2359. // start new status change
  2360. if( new_overweight == 1 )
  2361. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  2362. else if( new_overweight == 2 )
  2363. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  2364. // update overweight status
  2365. sd->regen.state.overweight = new_overweight;
  2366. }
  2367. int pc_disguise(struct map_session_data *sd, int class_)
  2368. {
  2369. if (!class_ && !sd->disguise)
  2370. return 0;
  2371. if (class_ && sd->disguise == class_)
  2372. return 0;
  2373. if(pc_isinvisible(sd))
  2374. { //Character is invisible. Stealth class-change. [Skotlex]
  2375. sd->disguise = class_; //viewdata is set on uncloaking.
  2376. return 2;
  2377. }
  2378. if (sd->bl.prev != NULL) {
  2379. pc_stop_walking(sd, 0);
  2380. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  2381. }
  2382. if (!class_) {
  2383. sd->disguise = 0;
  2384. class_ = sd->status.class_;
  2385. } else
  2386. sd->disguise=class_;
  2387. status_set_viewdata(&sd->bl, class_);
  2388. clif_changeoption(&sd->bl);
  2389. if (sd->bl.prev != NULL) {
  2390. clif_spawn(&sd->bl);
  2391. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2392. { //It seems the cart info is lost on undisguise.
  2393. clif_cartlist(sd);
  2394. clif_updatestatus(sd,SP_CARTINFO);
  2395. }
  2396. if (sd->chatID) {
  2397. struct chat_data* cd;
  2398. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  2399. clif_dispchat(cd,0);
  2400. }
  2401. }
  2402. return 1;
  2403. }
  2404. /// Show error message
  2405. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2406. /// Check for valid Element, break & show error message if invalid Element
  2407. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2408. /// Check for valid Race, break & show error message if invalid Race
  2409. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2410. /// Check for valid Race2, break & show error message if invalid Race2
  2411. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2412. /// Check for valid Class, break & show error message if invalid Class
  2413. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2414. /// Check for valid Size, break & show error message if invalid Size
  2415. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2416. /// Check for valid SC, break & show error message if invalid SC
  2417. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2418. /**
  2419. * Add auto spell bonus for player while attacking/attacked
  2420. * @param spell: Spell array
  2421. * @param id: Skill to cast
  2422. * @param lv: Skill level
  2423. * @param rate: Success chance
  2424. * @param battle_flag: Battle flag
  2425. * @param card_id: Used to prevent card stacking
  2426. * @param flag: Flags used for extra arguments
  2427. * &0: forces the skill to be casted on self, rather than on the target
  2428. * &1: forces the skill to be casted on target, rather than self
  2429. * &2: random skill level in [1..lv] is chosen
  2430. */
  2431. 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)
  2432. {
  2433. if (spell.size() == MAX_PC_BONUS) {
  2434. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2435. return;
  2436. }
  2437. if (!rate)
  2438. return;
  2439. if (!(battle_flag&BF_RANGEMASK))
  2440. battle_flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2441. if (!(battle_flag&BF_WEAPONMASK))
  2442. battle_flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2443. if (!(battle_flag&BF_SKILLMASK)) {
  2444. if (battle_flag&(BF_MAGIC | BF_MISC))
  2445. battle_flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2446. if (battle_flag&BF_WEAPON)
  2447. battle_flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2448. }
  2449. for (auto &it : spell) {
  2450. 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) {
  2451. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2452. return;
  2453. it.rate = util::safe_addition_cap(it.rate, rate, (short)1000);
  2454. return;
  2455. }
  2456. }
  2457. struct s_autospell entry = {};
  2458. if (rate < -1000 || rate > 1000)
  2459. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -1000~1000 range, capping.\n", rate);
  2460. entry.id = id;
  2461. entry.lv = lv;
  2462. entry.rate = cap_value(rate, -1000, 1000);
  2463. entry.battle_flag = battle_flag;
  2464. entry.card_id = card_id;
  2465. entry.flag = flag;
  2466. spell.push_back(entry);
  2467. }
  2468. /**
  2469. * Add auto spell bonus for player while using skills
  2470. * @param spell: Spell array
  2471. * @param src_skill: Trigger skill
  2472. * @param id: Support or target type
  2473. * @param lv: Skill level
  2474. * @param rate: Success chance
  2475. * @param card_id: Used to prevent card stacking
  2476. * @param flag: Flags used for extra arguments
  2477. * &1: forces the skill to be casted on self, rather than on the target of skill
  2478. * &2: random skill level in [1..lv] is chosen
  2479. */
  2480. 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)
  2481. {
  2482. if (spell.size() == MAX_PC_BONUS) {
  2483. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2484. return;
  2485. }
  2486. if (!rate)
  2487. return;
  2488. struct s_autospell entry = {};
  2489. if (rate < -1000 || rate > 1000)
  2490. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -1000~1000 range, capping.\n", rate);
  2491. entry.trigger_skill = src_skill;
  2492. entry.id = id;
  2493. entry.lv = lv;
  2494. entry.rate = cap_value(rate, -1000, 1000);
  2495. entry.card_id = card_id;
  2496. entry.flag = flag;
  2497. spell.push_back(entry);
  2498. }
  2499. /**
  2500. * Add inflict effect bonus for player while attacking/attacked
  2501. * @param effect: Effect array
  2502. * @param sc: SC/Effect type
  2503. * @param rate: Success chance
  2504. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2505. * @param flag: Target flag
  2506. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2507. */
  2508. 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)
  2509. {
  2510. if (effect.size() == MAX_PC_BONUS) {
  2511. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2512. return;
  2513. }
  2514. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2515. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2516. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2517. flag |= ATF_TARGET; //Default target: enemy.
  2518. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2519. flag |= ATF_WEAPON; //Default type: weapon.
  2520. if (!duration)
  2521. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2522. for (auto &it : effect) {
  2523. if (it.sc == sc && it.flag == flag) {
  2524. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2525. it.arrow_rate = util::safe_addition_cap(it.arrow_rate, arrow_rate, (short)SHRT_MAX);
  2526. it.duration = umax(it.duration, duration);
  2527. return;
  2528. }
  2529. }
  2530. struct s_addeffect entry = {};
  2531. entry.sc = sc;
  2532. entry.rate = rate;
  2533. entry.arrow_rate = arrow_rate;
  2534. entry.flag = flag;
  2535. entry.duration = duration;
  2536. effect.push_back(entry);
  2537. }
  2538. /**
  2539. * Add inflict effect bonus for player while attacking using skill
  2540. * @param effect: Effect array
  2541. * @param sc: SC/Effect type
  2542. * @param rate: Success chance
  2543. * @param skill_id: Skill to cast
  2544. * @param target: Target type
  2545. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2546. */
  2547. 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)
  2548. {
  2549. if (effect.size() == MAX_PC_BONUS) {
  2550. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2551. return;
  2552. }
  2553. if (!duration)
  2554. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2555. for (auto &it : effect) {
  2556. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2557. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2558. it.duration = umax(it.duration, duration);
  2559. return;
  2560. }
  2561. }
  2562. struct s_addeffectonskill entry = {};
  2563. entry.sc = sc;
  2564. entry.rate = rate;
  2565. entry.skill_id = skill_id;
  2566. entry.target = target;
  2567. entry.duration = duration;
  2568. effect.push_back(entry);
  2569. }
  2570. /**
  2571. * Adjust/add drop rate modifier for player
  2572. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2573. * @param nameid: item id that will be dropped
  2574. * @param group: group id
  2575. * @param class_: target class
  2576. * @param race: target race. if < 0, means monster_id
  2577. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2578. */
  2579. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int class_, short race, int rate)
  2580. {
  2581. if (!nameid && !group) {
  2582. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2583. return;
  2584. }
  2585. if (!item_db.exists(nameid)) {
  2586. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2587. return;
  2588. }
  2589. if (group && !itemdb_group.exists(group)) {
  2590. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2591. return;
  2592. }
  2593. if (drop.size() == MAX_PC_BONUS) {
  2594. 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);
  2595. return;
  2596. }
  2597. //Apply config rate adjustment settings.
  2598. if (rate >= 0) { //Absolute drop.
  2599. if (battle_config.item_rate_adddrop != 100)
  2600. rate = rate*battle_config.item_rate_adddrop/100;
  2601. if (rate < battle_config.item_drop_adddrop_min)
  2602. rate = battle_config.item_drop_adddrop_min;
  2603. else if (rate > battle_config.item_drop_adddrop_max)
  2604. rate = battle_config.item_drop_adddrop_max;
  2605. } else { //Relative drop, max/min limits are applied at drop time.
  2606. if (battle_config.item_rate_adddrop != 100)
  2607. rate = rate*battle_config.item_rate_adddrop/100;
  2608. if (rate > -1)
  2609. rate = -1;
  2610. }
  2611. for (auto &it : drop) {
  2612. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2613. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2614. it.rate = util::safe_addition_cap(it.rate, rate, 10000);
  2615. return;
  2616. }
  2617. }
  2618. struct s_add_drop entry = {};
  2619. if (rate < -10000 || rate > 10000)
  2620. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2621. entry.nameid = nameid;
  2622. entry.group = group;
  2623. entry.race = race;
  2624. entry.class_ = class_;
  2625. entry.rate = cap_value(rate, -10000, 10000);
  2626. drop.push_back(entry);
  2627. }
  2628. s_autobonus::~s_autobonus(){
  2629. if( this->active != INVALID_TIMER ){
  2630. delete_timer( this->active, pc_endautobonus );
  2631. this->active = INVALID_TIMER;
  2632. }
  2633. if( this->bonus_script != nullptr ){
  2634. aFree( this->bonus_script );
  2635. this->bonus_script = nullptr;
  2636. }
  2637. if( this->other_script != nullptr ){
  2638. aFree( this->other_script );
  2639. this->other_script = nullptr;
  2640. }
  2641. }
  2642. /**
  2643. * Add autobonus to player when attacking/attacked
  2644. * @param bonus: Bonus array
  2645. * @param script: Script to execute
  2646. * @param rate: Success chance
  2647. * @param dur: Duration
  2648. * @param flag: Battle flag/skill
  2649. * @param other_script: Secondary script to execute
  2650. * @param pos: Item equip position
  2651. * @param onskill: Skill used to trigger autobonus
  2652. * @return True on success or false otherwise
  2653. */
  2654. 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){
  2655. // Check if the same bonus already exists
  2656. for( std::shared_ptr<s_autobonus> autobonus : bonus ){
  2657. // Compare based on position and bonus script
  2658. if( autobonus->pos == pos && strcmp( script, autobonus->bonus_script ) == 0 ){
  2659. return false;
  2660. }
  2661. }
  2662. if (bonus.size() == MAX_PC_BONUS) {
  2663. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2664. return false;
  2665. }
  2666. if (!onskill) {
  2667. if (!(flag&BF_RANGEMASK))
  2668. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2669. if (!(flag&BF_WEAPONMASK))
  2670. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2671. if (!(flag&BF_SKILLMASK)) {
  2672. if (flag&(BF_MAGIC | BF_MISC))
  2673. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2674. if (flag&BF_WEAPON)
  2675. flag |= BF_NORMAL | BF_SKILL;
  2676. }
  2677. }
  2678. std::shared_ptr<s_autobonus> entry = std::make_shared<s_autobonus>();
  2679. if (rate < -10000 || rate > 10000)
  2680. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2681. entry->rate = cap_value(rate, -10000, 10000);
  2682. entry->duration = dur;
  2683. entry->active = INVALID_TIMER;
  2684. entry->atk_type = flag;
  2685. entry->pos = pos;
  2686. entry->bonus_script = aStrdup(script);
  2687. entry->other_script = (other_script ? aStrdup(other_script) : NULL);
  2688. bonus.push_back(entry);
  2689. return true;
  2690. }
  2691. /**
  2692. * Remove an autobonus from player
  2693. * @param sd: Player data
  2694. * @param bonus: Autobonus array
  2695. * @param restore: Run script on clearing or not
  2696. */
  2697. void pc_delautobonus(struct map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> &bonus, bool restore){
  2698. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  2699. while( it != bonus.end() ){
  2700. std::shared_ptr<s_autobonus> b = *it;
  2701. if( b->active != INVALID_TIMER && restore && b->bonus_script != nullptr ){
  2702. unsigned int equip_pos_idx = 0;
  2703. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2704. for (uint8 j = 0; j < EQI_MAX; j++) {
  2705. if (sd.equip_index[j] >= 0)
  2706. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2707. }
  2708. if( ( equip_pos_idx&b->pos ) == b->pos ){
  2709. script_run_autobonus(b->bonus_script, &sd, b->pos);
  2710. }else{
  2711. // Not all required items equipped anymore
  2712. restore = false;
  2713. }
  2714. }
  2715. if( restore ){
  2716. it++;
  2717. continue;
  2718. }
  2719. it = bonus.erase(it);
  2720. }
  2721. }
  2722. /**
  2723. * Execute autobonus on player
  2724. * @param sd: Player data
  2725. * @param autobonus: Autobonus to run
  2726. */
  2727. void pc_exeautobonus(struct map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> *bonus, std::shared_ptr<s_autobonus> autobonus)
  2728. {
  2729. if (autobonus->active != INVALID_TIMER)
  2730. delete_timer(autobonus->active, pc_endautobonus);
  2731. if( autobonus->other_script )
  2732. {
  2733. int j;
  2734. unsigned int equip_pos_idx = 0;
  2735. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2736. for(j = 0; j < EQI_MAX; j++) {
  2737. if(sd.equip_index[j] >= 0)
  2738. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2739. }
  2740. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2741. script_run_autobonus(autobonus->other_script,&sd,autobonus->pos);
  2742. }
  2743. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd.bl.id, (intptr_t)bonus);
  2744. status_calc_pc(&sd,SCO_FORCE);
  2745. }
  2746. /**
  2747. * Remove autobonus timer from player
  2748. */
  2749. TIMER_FUNC(pc_endautobonus){
  2750. struct map_session_data *sd = map_id2sd(id);
  2751. std::vector<std::shared_ptr<s_autobonus>> *bonus = (std::vector<std::shared_ptr<s_autobonus>> *)data;
  2752. nullpo_ret(sd);
  2753. nullpo_ret(bonus);
  2754. for( std::shared_ptr<s_autobonus> autobonus : *bonus ){
  2755. if( autobonus->active == tid ){
  2756. autobonus->active = INVALID_TIMER;
  2757. break;
  2758. }
  2759. }
  2760. status_calc_pc(sd,SCO_FORCE);
  2761. return 0;
  2762. }
  2763. /**
  2764. * Add element bonus to player when attacking
  2765. * @param sd: Player data
  2766. * @param ele: Element to adjust
  2767. * @param rate: Success chance
  2768. * @param flag: Battle flag
  2769. */
  2770. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2771. {
  2772. nullpo_retv(sd);
  2773. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2774. if (wd->addele2.size() == MAX_PC_BONUS) {
  2775. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2776. return;
  2777. }
  2778. if (!(flag&BF_RANGEMASK))
  2779. flag |= BF_SHORT | BF_LONG;
  2780. if (!(flag&BF_WEAPONMASK))
  2781. flag |= BF_WEAPON;
  2782. if (!(flag&BF_SKILLMASK)) {
  2783. if (flag&(BF_MAGIC | BF_MISC))
  2784. flag |= BF_SKILL;
  2785. if (flag&BF_WEAPON)
  2786. flag |= BF_NORMAL | BF_SKILL;
  2787. }
  2788. for (auto &it : wd->addele2) {
  2789. if (it.ele == ele && it.flag == flag) {
  2790. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2791. return;
  2792. }
  2793. }
  2794. struct s_addele2 entry = {};
  2795. if (rate < -10000 || rate > 10000)
  2796. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2797. entry.ele = ele;
  2798. entry.rate = cap_value(rate, -10000, 10000);
  2799. entry.flag = flag;
  2800. wd->addele2.push_back(entry);
  2801. }
  2802. /**
  2803. * Reduce element bonus to player when attacking
  2804. * @param sd: Player data
  2805. * @param ele: Element to adjust
  2806. * @param rate: Success chance
  2807. * @param flag: Battle flag
  2808. */
  2809. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2810. {
  2811. nullpo_retv(sd);
  2812. if (sd->subele2.size() == MAX_PC_BONUS) {
  2813. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2814. return;
  2815. }
  2816. if (!(flag&BF_RANGEMASK))
  2817. flag |= BF_SHORT | BF_LONG;
  2818. if (!(flag&BF_WEAPONMASK))
  2819. flag |= BF_WEAPON;
  2820. if (!(flag&BF_SKILLMASK)) {
  2821. if (flag&(BF_MAGIC | BF_MISC))
  2822. flag |= BF_SKILL;
  2823. if (flag&BF_WEAPON)
  2824. flag |= BF_NORMAL | BF_SKILL;
  2825. }
  2826. for (auto &it : sd->subele2) {
  2827. if (it.ele == ele && it.flag == flag) {
  2828. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2829. return;
  2830. }
  2831. }
  2832. struct s_addele2 entry = {};
  2833. if (rate < -10000 || rate > 10000)
  2834. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2835. entry.ele = ele;
  2836. entry.rate = cap_value(rate, -10000, 10000);
  2837. entry.flag = flag;
  2838. sd->subele2.push_back(entry);
  2839. }
  2840. /**
  2841. * Adjust race damage to target when attacking
  2842. * @param sd: Player data
  2843. * @param race: Race to adjust
  2844. * @param rate: Success chance
  2845. * @param flag: Battle flag
  2846. */
  2847. static void pc_bonus_subrace(struct map_session_data* sd, unsigned char race, short rate, short flag)
  2848. {
  2849. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2850. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2851. return;
  2852. }
  2853. if (!(flag&BF_RANGEMASK))
  2854. flag |= BF_SHORT | BF_LONG;
  2855. if (!(flag&BF_WEAPONMASK))
  2856. flag |= BF_WEAPON;
  2857. if (!(flag&BF_SKILLMASK)) {
  2858. if (flag&(BF_MAGIC | BF_MISC))
  2859. flag |= BF_SKILL;
  2860. if (flag&BF_WEAPON)
  2861. flag |= BF_NORMAL | BF_SKILL;
  2862. }
  2863. for (auto &it : sd->subrace3) {
  2864. if (it.race == race && it.flag == flag) {
  2865. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2866. return;
  2867. }
  2868. }
  2869. struct s_addrace2 entry = {};
  2870. if (rate < -10000 || rate > 10000)
  2871. ShowWarning("pc_bonus_subrace: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2872. entry.race = race;
  2873. entry.rate = cap_value(rate, -10000, 10000);
  2874. entry.flag = flag;
  2875. sd->subrace3.push_back(entry);
  2876. }
  2877. /**
  2878. * General item bonus for player
  2879. * @param bonus: Bonus array
  2880. * @param id: Key
  2881. * @param val: Value
  2882. * @param cap_rate: If Value is a rate value that needs to be capped
  2883. */
  2884. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2885. {
  2886. for (auto &it : bonus) {
  2887. if (it.id == id) {
  2888. if (cap_rate)
  2889. it.val = util::safe_addition_cap(it.val, val, 10000);
  2890. else
  2891. it.val += val;
  2892. return;
  2893. }
  2894. }
  2895. struct s_item_bonus entry = {};
  2896. if (cap_rate && (val < -10000 || val > 10000)) {
  2897. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2898. val = cap_value(val, -10000, 10000);
  2899. }
  2900. entry.id = id;
  2901. entry.val = val;
  2902. bonus.push_back(entry);
  2903. }
  2904. /**
  2905. * Remove HP/SP to player when attacking
  2906. * @param bonus: Bonus array
  2907. * @param rate: Success chance
  2908. * @param per: Percentage of HP/SP to vanish
  2909. * @param flag: Battle flag
  2910. */
  2911. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2912. if (bonus.size() == MAX_PC_BONUS) {
  2913. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2914. return;
  2915. }
  2916. if (!(flag&BF_RANGEMASK))
  2917. flag |= BF_SHORT | BF_LONG;
  2918. if (!(flag&BF_WEAPONMASK))
  2919. flag |= BF_WEAPON;
  2920. if (!(flag&BF_SKILLMASK)) {
  2921. if (flag&(BF_MAGIC | BF_MISC))
  2922. flag |= BF_SKILL;
  2923. if (flag&BF_WEAPON)
  2924. flag |= BF_NORMAL | BF_SKILL;
  2925. }
  2926. for (auto &it : bonus) {
  2927. if (it.flag == flag) {
  2928. it.rate = util::safe_addition_cap(it.rate, rate, (int16)10000);
  2929. it.per += per;
  2930. return;
  2931. }
  2932. }
  2933. struct s_vanish_bonus entry = {};
  2934. if (rate < -10000 || rate > 10000)
  2935. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2936. entry.rate = cap_value(rate, -10000, 10000);
  2937. entry.per = per;
  2938. entry.flag = flag;
  2939. bonus.push_back(entry);
  2940. }
  2941. /*==========================================
  2942. * Add a bonus(type) to player sd
  2943. * format: bonus bBonusName,val;
  2944. * @param sd
  2945. * @param type Bonus type used by bBonusName
  2946. * @param val Value that usually for rate or fixed value
  2947. *------------------------------------------*/
  2948. void pc_bonus(struct map_session_data *sd,int type,int val)
  2949. {
  2950. struct status_data *status;
  2951. int bonus;
  2952. nullpo_retv(sd);
  2953. status = &sd->base_status;
  2954. switch(type){
  2955. case SP_STR:
  2956. case SP_AGI:
  2957. case SP_VIT:
  2958. case SP_INT:
  2959. case SP_DEX:
  2960. case SP_LUK:
  2961. if(sd->state.lr_flag != 2)
  2962. sd->indexed_bonus.param_bonus[type-SP_STR]+=val;
  2963. break;
  2964. case SP_POW:
  2965. case SP_STA:
  2966. case SP_WIS:
  2967. case SP_SPL:
  2968. case SP_CON:
  2969. case SP_CRT:
  2970. if (sd->state.lr_flag != 2)
  2971. sd->indexed_bonus.param_bonus[type - SP_POW + PARAM_POW] += val;
  2972. break;
  2973. case SP_ATK1:
  2974. if(!sd->state.lr_flag) {
  2975. bonus = status->rhw.atk + val;
  2976. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2977. }
  2978. else if(sd->state.lr_flag == 1) {
  2979. bonus = status->lhw.atk + val;
  2980. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2981. }
  2982. break;
  2983. case SP_ATK2:
  2984. if(!sd->state.lr_flag) {
  2985. bonus = status->rhw.atk2 + val;
  2986. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2987. }
  2988. else if(sd->state.lr_flag == 1) {
  2989. bonus = status->lhw.atk2 + val;
  2990. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2991. }
  2992. break;
  2993. case SP_BASE_ATK:
  2994. if(sd->state.lr_flag != 2) {
  2995. #ifdef RENEWAL
  2996. bonus = sd->bonus.eatk + val;
  2997. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2998. #else
  2999. bonus = status->batk + val;
  3000. status->batk = cap_value(bonus, 0, USHRT_MAX);
  3001. #endif
  3002. }
  3003. break;
  3004. case SP_DEF1:
  3005. if(sd->state.lr_flag != 2) {
  3006. bonus = status->def + val;
  3007. #ifdef RENEWAL
  3008. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3009. #else
  3010. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3011. #endif
  3012. }
  3013. break;
  3014. case SP_DEF2:
  3015. if(sd->state.lr_flag != 2) {
  3016. bonus = status->def2 + val;
  3017. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3018. }
  3019. break;
  3020. case SP_MDEF1:
  3021. if(sd->state.lr_flag != 2) {
  3022. bonus = status->mdef + val;
  3023. #ifdef RENEWAL
  3024. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3025. #else
  3026. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3027. #endif
  3028. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  3029. sd->bonus.shieldmdef += bonus;
  3030. }
  3031. }
  3032. break;
  3033. case SP_MDEF2:
  3034. if(sd->state.lr_flag != 2) {
  3035. bonus = status->mdef2 + val;
  3036. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3037. }
  3038. break;
  3039. case SP_HIT:
  3040. if(sd->state.lr_flag != 2) {
  3041. bonus = status->hit + val;
  3042. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3043. } else
  3044. sd->bonus.arrow_hit+=val;
  3045. break;
  3046. case SP_FLEE1:
  3047. if(sd->state.lr_flag != 2) {
  3048. bonus = status->flee + val;
  3049. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3050. }
  3051. break;
  3052. case SP_FLEE2:
  3053. if(sd->state.lr_flag != 2) {
  3054. bonus = status->flee2 + val*10;
  3055. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3056. }
  3057. break;
  3058. case SP_CRITICAL:
  3059. if(sd->state.lr_flag != 2) {
  3060. bonus = status->cri + val*10;
  3061. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3062. } else
  3063. sd->bonus.arrow_cri += val*10;
  3064. break;
  3065. case SP_PATK:
  3066. if (sd->state.lr_flag != 2) {
  3067. bonus = status->patk + val;
  3068. status->patk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3069. }
  3070. break;
  3071. case SP_SMATK:
  3072. if (sd->state.lr_flag != 2) {
  3073. bonus = status->smatk + val;
  3074. status->smatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3075. }
  3076. break;
  3077. case SP_RES:
  3078. if (sd->state.lr_flag != 2) {
  3079. bonus = status->res + val;
  3080. status->res = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3081. }
  3082. break;
  3083. case SP_MRES:
  3084. if (sd->state.lr_flag != 2) {
  3085. bonus = status->mres + val;
  3086. status->mres = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3087. }
  3088. break;
  3089. case SP_HPLUS:
  3090. if (sd->state.lr_flag != 2) {
  3091. bonus = status->hplus + val;
  3092. status->hplus = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3093. }
  3094. break;
  3095. case SP_CRATE:
  3096. if (sd->state.lr_flag != 2) {
  3097. bonus = status->crate + val;
  3098. status->crate = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3099. }
  3100. break;
  3101. case SP_ATKELE:
  3102. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  3103. switch (sd->state.lr_flag)
  3104. {
  3105. case 2:
  3106. switch (sd->status.weapon) {
  3107. case W_BOW:
  3108. case W_REVOLVER:
  3109. case W_RIFLE:
  3110. case W_GATLING:
  3111. case W_SHOTGUN:
  3112. case W_GRENADE:
  3113. //Become weapon element.
  3114. status->rhw.ele=val;
  3115. break;
  3116. default: //Become arrow element.
  3117. sd->bonus.arrow_ele=val;
  3118. break;
  3119. }
  3120. break;
  3121. case 1:
  3122. status->lhw.ele=val;
  3123. break;
  3124. default:
  3125. status->rhw.ele=val;
  3126. break;
  3127. }
  3128. break;
  3129. case SP_DEFELE:
  3130. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  3131. if(sd->state.lr_flag != 2)
  3132. status->def_ele=val;
  3133. break;
  3134. case SP_MAXHP:
  3135. if(sd->state.lr_flag == 2)
  3136. break;
  3137. sd->bonus.hp += val;
  3138. break;
  3139. case SP_MAXSP:
  3140. if(sd->state.lr_flag == 2)
  3141. break;
  3142. sd->bonus.sp += val;
  3143. break;
  3144. case SP_MAXAP:
  3145. if (sd->state.lr_flag == 2)
  3146. break;
  3147. sd->bonus.ap += val;
  3148. break;
  3149. case SP_MAXHPRATE:
  3150. if(sd->state.lr_flag != 2)
  3151. sd->hprate+=val;
  3152. break;
  3153. case SP_MAXSPRATE:
  3154. if(sd->state.lr_flag != 2)
  3155. sd->sprate+=val;
  3156. break;
  3157. case SP_MAXAPRATE:
  3158. if (sd->state.lr_flag != 2)
  3159. sd->aprate += val;
  3160. break;
  3161. case SP_SPRATE:
  3162. if(sd->state.lr_flag != 2)
  3163. sd->dsprate+=val;
  3164. break;
  3165. case SP_ATTACKRANGE:
  3166. switch (sd->state.lr_flag) {
  3167. case 2:
  3168. switch (sd->status.weapon) {
  3169. case W_BOW:
  3170. case W_REVOLVER:
  3171. case W_RIFLE:
  3172. case W_GATLING:
  3173. case W_SHOTGUN:
  3174. case W_GRENADE:
  3175. status->rhw.range += val;
  3176. }
  3177. break;
  3178. case 1:
  3179. status->lhw.range += val;
  3180. break;
  3181. default:
  3182. status->rhw.range += val;
  3183. break;
  3184. }
  3185. break;
  3186. case SP_SPEED_RATE: //Non stackable increase
  3187. if(sd->state.lr_flag != 2)
  3188. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  3189. break;
  3190. case SP_SPEED_ADDRATE: //Stackable increase
  3191. if(sd->state.lr_flag != 2)
  3192. sd->bonus.speed_add_rate -= val;
  3193. break;
  3194. case SP_ASPD: //Raw increase
  3195. if(sd->state.lr_flag != 2)
  3196. sd->bonus.aspd_add -= 10*val;
  3197. break;
  3198. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  3199. if(sd->state.lr_flag != 2)
  3200. #ifndef RENEWAL_ASPD
  3201. status->aspd_rate -= 10*val;
  3202. #else
  3203. status->aspd_rate2 += val;
  3204. #endif
  3205. break;
  3206. case SP_HP_RECOV_RATE:
  3207. if(sd->state.lr_flag != 2)
  3208. sd->hprecov_rate += val;
  3209. break;
  3210. case SP_SP_RECOV_RATE:
  3211. if(sd->state.lr_flag != 2)
  3212. sd->sprecov_rate += val;
  3213. break;
  3214. case SP_CRITICAL_DEF:
  3215. if(sd->state.lr_flag != 2)
  3216. sd->bonus.critical_def += val;
  3217. break;
  3218. case SP_NEAR_ATK_DEF:
  3219. if(sd->state.lr_flag != 2)
  3220. sd->bonus.near_attack_def_rate += val;
  3221. break;
  3222. case SP_LONG_ATK_DEF:
  3223. if(sd->state.lr_flag != 2)
  3224. sd->bonus.long_attack_def_rate += val;
  3225. break;
  3226. case SP_DOUBLE_RATE:
  3227. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  3228. sd->bonus.double_rate = val;
  3229. break;
  3230. case SP_DOUBLE_ADD_RATE:
  3231. if(sd->state.lr_flag == 0)
  3232. sd->bonus.double_add_rate += val;
  3233. break;
  3234. case SP_MATK_RATE:
  3235. if(sd->state.lr_flag != 2)
  3236. sd->matk_rate += val;
  3237. break;
  3238. case SP_IGNORE_DEF_ELE:
  3239. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  3240. if(!sd->state.lr_flag)
  3241. sd->right_weapon.ignore_def_ele |= 1<<val;
  3242. else if(sd->state.lr_flag == 1)
  3243. sd->left_weapon.ignore_def_ele |= 1<<val;
  3244. break;
  3245. case SP_IGNORE_DEF_RACE:
  3246. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  3247. if(!sd->state.lr_flag)
  3248. sd->right_weapon.ignore_def_race |= 1<<val;
  3249. else if(sd->state.lr_flag == 1)
  3250. sd->left_weapon.ignore_def_race |= 1<<val;
  3251. break;
  3252. case SP_IGNORE_DEF_CLASS:
  3253. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  3254. if(!sd->state.lr_flag)
  3255. sd->right_weapon.ignore_def_class |= 1<<val;
  3256. else if(sd->state.lr_flag == 1)
  3257. sd->left_weapon.ignore_def_class |= 1<<val;
  3258. break;
  3259. case SP_ATK_RATE:
  3260. if(sd->state.lr_flag != 2)
  3261. sd->bonus.atk_rate += val;
  3262. break;
  3263. case SP_MAGIC_ATK_DEF:
  3264. if(sd->state.lr_flag != 2)
  3265. sd->bonus.magic_def_rate += val;
  3266. break;
  3267. case SP_MISC_ATK_DEF:
  3268. if(sd->state.lr_flag != 2)
  3269. sd->bonus.misc_def_rate += val;
  3270. break;
  3271. case SP_IGNORE_MDEF_ELE:
  3272. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  3273. if(sd->state.lr_flag != 2)
  3274. sd->bonus.ignore_mdef_ele |= 1<<val;
  3275. break;
  3276. case SP_IGNORE_MDEF_RACE:
  3277. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  3278. if(sd->state.lr_flag != 2)
  3279. sd->bonus.ignore_mdef_race |= 1<<val;
  3280. break;
  3281. case SP_PERFECT_HIT_RATE:
  3282. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  3283. sd->bonus.perfect_hit = val;
  3284. break;
  3285. case SP_PERFECT_HIT_ADD_RATE:
  3286. if(sd->state.lr_flag != 2)
  3287. sd->bonus.perfect_hit_add += val;
  3288. break;
  3289. case SP_CRITICAL_RATE:
  3290. if(sd->state.lr_flag != 2)
  3291. sd->critical_rate+=val;
  3292. break;
  3293. case SP_DEF_RATIO_ATK_ELE:
  3294. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  3295. if(!sd->state.lr_flag)
  3296. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  3297. else if(sd->state.lr_flag == 1)
  3298. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  3299. break;
  3300. case SP_DEF_RATIO_ATK_RACE:
  3301. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  3302. if(!sd->state.lr_flag)
  3303. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  3304. else if(sd->state.lr_flag == 1)
  3305. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  3306. break;
  3307. case SP_DEF_RATIO_ATK_CLASS:
  3308. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  3309. if(!sd->state.lr_flag)
  3310. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  3311. else if(sd->state.lr_flag == 1)
  3312. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  3313. break;
  3314. case SP_HIT_RATE:
  3315. if(sd->state.lr_flag != 2)
  3316. sd->hit_rate += val;
  3317. break;
  3318. case SP_FLEE_RATE:
  3319. if(sd->state.lr_flag != 2)
  3320. sd->flee_rate += val;
  3321. break;
  3322. case SP_FLEE2_RATE:
  3323. if(sd->state.lr_flag != 2)
  3324. sd->flee2_rate += val;
  3325. break;
  3326. case SP_DEF_RATE:
  3327. if(sd->state.lr_flag != 2)
  3328. sd->def_rate += val;
  3329. break;
  3330. case SP_DEF2_RATE:
  3331. if(sd->state.lr_flag != 2)
  3332. sd->def2_rate += val;
  3333. break;
  3334. case SP_MDEF_RATE:
  3335. if(sd->state.lr_flag != 2)
  3336. sd->mdef_rate += val;
  3337. break;
  3338. case SP_MDEF2_RATE:
  3339. if(sd->state.lr_flag != 2)
  3340. sd->mdef2_rate += val;
  3341. break;
  3342. case SP_PATK_RATE:
  3343. if (sd->state.lr_flag != 2)
  3344. sd->patk_rate += val;
  3345. break;
  3346. case SP_SMATK_RATE:
  3347. if (sd->state.lr_flag != 2)
  3348. sd->smatk_rate += val;
  3349. break;
  3350. case SP_RES_RATE:
  3351. if (sd->state.lr_flag != 2)
  3352. sd->res_rate += val;
  3353. break;
  3354. case SP_MRES_RATE:
  3355. if (sd->state.lr_flag != 2)
  3356. sd->mres_rate += val;
  3357. break;
  3358. case SP_HPLUS_RATE:
  3359. if (sd->state.lr_flag != 2)
  3360. sd->hplus_rate += val;
  3361. break;
  3362. case SP_CRATE_RATE:
  3363. if (sd->state.lr_flag != 2)
  3364. sd->crate_rate += val;
  3365. break;
  3366. case SP_RESTART_FULL_RECOVER:
  3367. if(sd->state.lr_flag != 2)
  3368. sd->special_state.restart_full_recover = 1;
  3369. break;
  3370. case SP_NO_CASTCANCEL:
  3371. if(sd->state.lr_flag != 2)
  3372. sd->special_state.no_castcancel = 1;
  3373. break;
  3374. case SP_NO_CASTCANCEL2:
  3375. if(sd->state.lr_flag != 2)
  3376. sd->special_state.no_castcancel2 = 1;
  3377. break;
  3378. case SP_NO_SIZEFIX:
  3379. if(sd->state.lr_flag != 2)
  3380. sd->special_state.no_sizefix = 1;
  3381. break;
  3382. case SP_NO_MAGIC_DAMAGE:
  3383. if(sd->state.lr_flag == 2)
  3384. break;
  3385. val+= sd->special_state.no_magic_damage;
  3386. sd->special_state.no_magic_damage = cap_value(val,0,100);
  3387. break;
  3388. case SP_NO_WEAPON_DAMAGE:
  3389. if(sd->state.lr_flag == 2)
  3390. break;
  3391. val+= sd->special_state.no_weapon_damage;
  3392. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  3393. break;
  3394. case SP_NO_MISC_DAMAGE:
  3395. if(sd->state.lr_flag == 2)
  3396. break;
  3397. val+= sd->special_state.no_misc_damage;
  3398. sd->special_state.no_misc_damage = cap_value(val,0,100);
  3399. break;
  3400. case SP_NO_GEMSTONE:
  3401. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  3402. sd->special_state.no_gemstone = 1;
  3403. break;
  3404. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  3405. if(sd->state.lr_flag != 2) {
  3406. sd->special_state.intravision = 1;
  3407. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  3408. }
  3409. break;
  3410. case SP_NO_KNOCKBACK:
  3411. if(sd->state.lr_flag != 2)
  3412. sd->special_state.no_knockback = 1;
  3413. break;
  3414. case SP_SPLASH_RANGE:
  3415. if(sd->bonus.splash_range < val)
  3416. sd->bonus.splash_range = val;
  3417. break;
  3418. case SP_SPLASH_ADD_RANGE:
  3419. sd->bonus.splash_add_range += val;
  3420. break;
  3421. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  3422. if(sd->state.lr_flag != 2)
  3423. sd->bonus.short_weapon_damage_return += val;
  3424. break;
  3425. case SP_LONG_WEAPON_DAMAGE_RETURN:
  3426. if(sd->state.lr_flag != 2)
  3427. sd->bonus.long_weapon_damage_return += val;
  3428. break;
  3429. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  3430. if(sd->state.lr_flag != 2)
  3431. sd->bonus.magic_damage_return += val;
  3432. break;
  3433. case SP_REDUCE_DAMAGE_RETURN:
  3434. if (sd->state.lr_flag != 2)
  3435. sd->bonus.reduce_damage_return += val;
  3436. break;
  3437. case SP_ALL_STATS: // [Valaris]
  3438. if(sd->state.lr_flag!=2) {
  3439. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3440. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3441. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3442. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  3443. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3444. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  3445. }
  3446. break;
  3447. case SP_ALL_TRAIT_STATS:
  3448. if (sd->state.lr_flag != 2) {
  3449. sd->indexed_bonus.param_bonus[PARAM_POW] += val;
  3450. sd->indexed_bonus.param_bonus[PARAM_STA] += val;
  3451. sd->indexed_bonus.param_bonus[PARAM_WIS] += val;
  3452. sd->indexed_bonus.param_bonus[PARAM_SPL] += val;
  3453. sd->indexed_bonus.param_bonus[PARAM_CON] += val;
  3454. sd->indexed_bonus.param_bonus[PARAM_CRT] += val;
  3455. }
  3456. break;
  3457. case SP_AGI_VIT: // [Valaris]
  3458. if(sd->state.lr_flag!=2) {
  3459. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3460. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3461. }
  3462. break;
  3463. case SP_AGI_DEX_STR: // [Valaris]
  3464. if(sd->state.lr_flag!=2) {
  3465. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3466. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3467. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3468. }
  3469. break;
  3470. case SP_PERFECT_HIDE: // [Valaris]
  3471. if(sd->state.lr_flag!=2)
  3472. sd->special_state.perfect_hiding=1;
  3473. break;
  3474. case SP_UNBREAKABLE:
  3475. if(sd->state.lr_flag!=2)
  3476. sd->bonus.unbreakable += val;
  3477. break;
  3478. case SP_UNBREAKABLE_WEAPON:
  3479. if(sd->state.lr_flag != 2)
  3480. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3481. break;
  3482. case SP_UNBREAKABLE_ARMOR:
  3483. if(sd->state.lr_flag != 2)
  3484. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3485. break;
  3486. case SP_UNBREAKABLE_HELM:
  3487. if(sd->state.lr_flag != 2)
  3488. sd->bonus.unbreakable_equip |= EQP_HELM;
  3489. break;
  3490. case SP_UNBREAKABLE_SHIELD:
  3491. if(sd->state.lr_flag != 2)
  3492. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3493. break;
  3494. case SP_UNBREAKABLE_GARMENT:
  3495. if(sd->state.lr_flag != 2)
  3496. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3497. break;
  3498. case SP_UNBREAKABLE_SHOES:
  3499. if(sd->state.lr_flag != 2)
  3500. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3501. break;
  3502. case SP_CLASSCHANGE: // [Valaris]
  3503. if(sd->state.lr_flag !=2)
  3504. sd->bonus.classchange=val;
  3505. break;
  3506. case SP_SHORT_ATK_RATE:
  3507. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3508. sd->bonus.short_attack_atk_rate+=val;
  3509. break;
  3510. case SP_LONG_ATK_RATE:
  3511. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3512. sd->bonus.long_attack_atk_rate+=val;
  3513. break;
  3514. case SP_BREAK_WEAPON_RATE:
  3515. if(sd->state.lr_flag != 2)
  3516. sd->bonus.break_weapon_rate+=val;
  3517. break;
  3518. case SP_BREAK_ARMOR_RATE:
  3519. if(sd->state.lr_flag != 2)
  3520. sd->bonus.break_armor_rate+=val;
  3521. break;
  3522. case SP_ADD_STEAL_RATE:
  3523. if(sd->state.lr_flag != 2)
  3524. sd->bonus.add_steal_rate+=val;
  3525. break;
  3526. case SP_DELAYRATE:
  3527. if(sd->state.lr_flag != 2)
  3528. sd->bonus.delayrate -= val;
  3529. break;
  3530. case SP_CRIT_ATK_RATE:
  3531. if(sd->state.lr_flag != 2)
  3532. sd->bonus.crit_atk_rate += val;
  3533. break;
  3534. case SP_CRIT_DEF_RATE:
  3535. if(sd->state.lr_flag != 2)
  3536. sd->bonus.crit_def_rate += val;
  3537. break;
  3538. case SP_NO_REGEN:
  3539. if(sd->state.lr_flag != 2)
  3540. sd->regen.state.block|=val;
  3541. break;
  3542. case SP_UNSTRIPABLE_WEAPON:
  3543. if(sd->state.lr_flag != 2)
  3544. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3545. break;
  3546. case SP_UNSTRIPABLE:
  3547. case SP_UNSTRIPABLE_ARMOR:
  3548. if(sd->state.lr_flag != 2)
  3549. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3550. break;
  3551. case SP_UNSTRIPABLE_HELM:
  3552. if(sd->state.lr_flag != 2)
  3553. sd->bonus.unstripable_equip |= EQP_HELM;
  3554. break;
  3555. case SP_UNSTRIPABLE_SHIELD:
  3556. if(sd->state.lr_flag != 2)
  3557. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3558. break;
  3559. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3560. if(!sd->state.lr_flag) {
  3561. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3562. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3563. } else if(sd->state.lr_flag == 1) {
  3564. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3565. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3566. }
  3567. break;
  3568. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3569. if(!sd->state.lr_flag) {
  3570. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3571. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3572. } else if(sd->state.lr_flag == 1) {
  3573. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3574. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3575. }
  3576. break;
  3577. case SP_SP_GAIN_VALUE:
  3578. if(!sd->state.lr_flag)
  3579. sd->bonus.sp_gain_value += val;
  3580. break;
  3581. case SP_HP_GAIN_VALUE:
  3582. if(!sd->state.lr_flag)
  3583. sd->bonus.hp_gain_value += val;
  3584. break;
  3585. case SP_LONG_SP_GAIN_VALUE:
  3586. if(!sd->state.lr_flag)
  3587. sd->bonus.long_sp_gain_value += val;
  3588. case SP_LONG_HP_GAIN_VALUE:
  3589. if(!sd->state.lr_flag)
  3590. sd->bonus.long_hp_gain_value += val;
  3591. break;
  3592. case SP_MAGIC_SP_GAIN_VALUE:
  3593. if(!sd->state.lr_flag)
  3594. sd->bonus.magic_sp_gain_value += val;
  3595. break;
  3596. case SP_MAGIC_HP_GAIN_VALUE:
  3597. if(!sd->state.lr_flag)
  3598. sd->bonus.magic_hp_gain_value += val;
  3599. break;
  3600. case SP_ADD_HEAL_RATE:
  3601. if(sd->state.lr_flag != 2)
  3602. sd->bonus.add_heal_rate += val;
  3603. break;
  3604. case SP_ADD_HEAL2_RATE:
  3605. if(sd->state.lr_flag != 2)
  3606. sd->bonus.add_heal2_rate += val;
  3607. break;
  3608. case SP_ADD_ITEM_HEAL_RATE:
  3609. if(sd->state.lr_flag != 2)
  3610. sd->bonus.itemhealrate2 += val;
  3611. break;
  3612. case SP_EMATK:
  3613. if(sd->state.lr_flag != 2)
  3614. sd->bonus.ematk += val;
  3615. break;
  3616. case SP_ADD_VARIABLECAST:
  3617. if (sd->state.lr_flag != 2)
  3618. sd->bonus.add_varcast += val;
  3619. break;
  3620. #ifdef RENEWAL_CAST
  3621. case SP_FIXCASTRATE:
  3622. if(sd->state.lr_flag != 2)
  3623. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3624. break;
  3625. case SP_ADD_FIXEDCAST:
  3626. if(sd->state.lr_flag != 2)
  3627. sd->bonus.add_fixcast += val;
  3628. break;
  3629. case SP_CASTRATE:
  3630. case SP_VARCASTRATE:
  3631. if(sd->state.lr_flag != 2)
  3632. sd->bonus.varcastrate -= val;
  3633. break;
  3634. #else
  3635. case SP_ADD_FIXEDCAST:
  3636. case SP_FIXCASTRATE:
  3637. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3638. break;
  3639. case SP_VARCASTRATE:
  3640. case SP_CASTRATE:
  3641. if(sd->state.lr_flag != 2)
  3642. sd->castrate += val;
  3643. break;
  3644. #endif
  3645. case SP_ADDMAXWEIGHT:
  3646. if (sd->state.lr_flag != 2)
  3647. sd->add_max_weight += val;
  3648. break;
  3649. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3650. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3651. break;
  3652. case SP_ABSORB_DMG_MAXHP2:
  3653. sd->bonus.absorb_dmg_maxhp2 = max(sd->bonus.absorb_dmg_maxhp2, val);
  3654. break;
  3655. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3656. if (sd->state.lr_flag != 2)
  3657. sd->bonus.critical_rangeatk += val*10;
  3658. else
  3659. sd->bonus.arrow_cri += val*10;
  3660. break;
  3661. case SP_WEAPON_ATK_RATE:
  3662. if (sd->state.lr_flag != 2)
  3663. sd->bonus.weapon_atk_rate += val;
  3664. break;
  3665. case SP_WEAPON_MATK_RATE:
  3666. if (sd->state.lr_flag != 2)
  3667. sd->bonus.weapon_matk_rate += val;
  3668. break;
  3669. case SP_NO_MADO_FUEL:
  3670. if (sd->state.lr_flag != 2)
  3671. sd->special_state.no_mado_fuel = 1;
  3672. break;
  3673. case SP_NO_WALK_DELAY:
  3674. if (sd->state.lr_flag != 2)
  3675. sd->special_state.no_walk_delay = 1;
  3676. break;
  3677. case SP_ADD_ITEM_SPHEAL_RATE:
  3678. if(sd->state.lr_flag != 2)
  3679. sd->bonus.itemsphealrate2 += val;
  3680. break;
  3681. default:
  3682. if (current_equip_combo_pos > 0) {
  3683. 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);
  3684. }
  3685. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3686. 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);
  3687. }
  3688. else {
  3689. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3690. }
  3691. break;
  3692. }
  3693. }
  3694. /*==========================================
  3695. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3696. * format: bonus2 bBonusName,type2,val;
  3697. * @param sd
  3698. * @param type Bonus type used by bBonusName
  3699. * @param type2
  3700. * @param val Value that usually for rate or fixed value
  3701. *------------------------------------------*/
  3702. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3703. {
  3704. nullpo_retv(sd);
  3705. switch(type){
  3706. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3707. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3708. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3709. sd->right_weapon.addele[type2]+=val;
  3710. else if(sd->state.lr_flag == 1)
  3711. sd->left_weapon.addele[type2]+=val;
  3712. else if(sd->state.lr_flag == 2)
  3713. sd->indexed_bonus.arrow_addele[type2]+=val;
  3714. break;
  3715. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3716. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3717. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3718. sd->right_weapon.addrace[type2]+=val;
  3719. else if(sd->state.lr_flag == 1)
  3720. sd->left_weapon.addrace[type2]+=val;
  3721. else if(sd->state.lr_flag == 2)
  3722. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3723. break;
  3724. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3725. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3726. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3727. sd->right_weapon.addclass[type2]+=val;
  3728. else if(sd->state.lr_flag == 1)
  3729. sd->left_weapon.addclass[type2]+=val;
  3730. else if(sd->state.lr_flag == 2)
  3731. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3732. break;
  3733. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3734. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3735. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3736. sd->right_weapon.addsize[type2]+=val;
  3737. else if(sd->state.lr_flag == 1)
  3738. sd->left_weapon.addsize[type2]+=val;
  3739. else if(sd->state.lr_flag == 2)
  3740. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3741. break;
  3742. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3743. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3744. if(sd->state.lr_flag != 2)
  3745. sd->indexed_bonus.subele_script[type2] += val;
  3746. break;
  3747. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3748. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3749. if(sd->state.lr_flag != 2)
  3750. sd->indexed_bonus.subrace[type2]+=val;
  3751. break;
  3752. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3753. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3754. if(sd->state.lr_flag != 2)
  3755. sd->indexed_bonus.subclass[type2]+=val;
  3756. break;
  3757. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3758. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3759. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3760. break;
  3761. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3762. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3763. 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);
  3764. break;
  3765. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3766. if(sd->state.lr_flag == 2)
  3767. break;
  3768. if (sd->reseff.size() == MAX_PC_BONUS) {
  3769. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3770. break;
  3771. }
  3772. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3773. break;
  3774. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3775. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3776. if(sd->state.lr_flag != 2)
  3777. sd->indexed_bonus.magic_addele_script[type2] += val;
  3778. break;
  3779. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3780. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3781. if(sd->state.lr_flag != 2)
  3782. sd->indexed_bonus.magic_addrace[type2]+=val;
  3783. break;
  3784. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3785. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3786. if(sd->state.lr_flag != 2)
  3787. sd->indexed_bonus.magic_addclass[type2]+=val;
  3788. break;
  3789. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3790. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3791. if(sd->state.lr_flag != 2)
  3792. sd->indexed_bonus.magic_addsize[type2]+=val;
  3793. break;
  3794. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3795. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3796. if(sd->state.lr_flag != 2)
  3797. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3798. break;
  3799. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3800. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3801. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3802. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3803. break;
  3804. }
  3805. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3806. }
  3807. break;
  3808. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3809. if(sd->state.lr_flag == 2)
  3810. break;
  3811. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3812. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3813. break;
  3814. }
  3815. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3816. break;
  3817. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3818. if(sd->state.lr_flag == 2)
  3819. break;
  3820. if (sd->add_def.size() == MAX_PC_BONUS) {
  3821. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3822. break;
  3823. }
  3824. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3825. break;
  3826. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3827. if(sd->state.lr_flag == 2)
  3828. break;
  3829. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3830. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3831. break;
  3832. }
  3833. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3834. break;
  3835. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3836. if(!sd->state.lr_flag) {
  3837. sd->right_weapon.hp_drain_rate.rate += type2;
  3838. sd->right_weapon.hp_drain_rate.per += val;
  3839. }
  3840. else if(sd->state.lr_flag == 1) {
  3841. sd->left_weapon.hp_drain_rate.rate += type2;
  3842. sd->left_weapon.hp_drain_rate.per += val;
  3843. }
  3844. break;
  3845. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3846. if(!sd->state.lr_flag) {
  3847. sd->right_weapon.sp_drain_rate.rate += type2;
  3848. sd->right_weapon.sp_drain_rate.per += val;
  3849. }
  3850. else if(sd->state.lr_flag == 1) {
  3851. sd->left_weapon.sp_drain_rate.rate += type2;
  3852. sd->left_weapon.sp_drain_rate.per += val;
  3853. }
  3854. break;
  3855. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3856. if(sd->state.lr_flag != 2) {
  3857. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3858. }
  3859. break;
  3860. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3861. if(sd->state.lr_flag != 2) {
  3862. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3863. }
  3864. break;
  3865. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3866. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3867. sd->bonus.get_zeny_rate = val;
  3868. sd->bonus.get_zeny_num = type2;
  3869. }
  3870. break;
  3871. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3872. if(sd->state.lr_flag != 2) {
  3873. sd->bonus.get_zeny_rate += val;
  3874. sd->bonus.get_zeny_num += type2;
  3875. }
  3876. break;
  3877. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3878. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3879. if(sd->state.lr_flag == 2)
  3880. break;
  3881. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3882. sd->special_state.bonus_coma = 1;
  3883. break;
  3884. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3885. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3886. if(sd->state.lr_flag == 2)
  3887. break;
  3888. sd->indexed_bonus.weapon_coma_race[type2] += val;
  3889. sd->special_state.bonus_coma = 1;
  3890. break;
  3891. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3892. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3893. if(sd->state.lr_flag == 2)
  3894. break;
  3895. sd->indexed_bonus.weapon_coma_class[type2] += val;
  3896. sd->special_state.bonus_coma = 1;
  3897. break;
  3898. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3899. if(sd->state.lr_flag != 2)
  3900. sd->indexed_bonus.weapon_atk[type2]+=val;
  3901. break;
  3902. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3903. if(sd->state.lr_flag != 2)
  3904. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  3905. break;
  3906. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3907. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3908. if(sd->state.lr_flag != 2)
  3909. sd->indexed_bonus.critaddrace[type2] += val*10;
  3910. break;
  3911. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3912. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3913. if(sd->state.lr_flag != 2)
  3914. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3915. break;
  3916. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3917. if(sd->state.lr_flag == 2)
  3918. break;
  3919. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3920. 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);
  3921. break;
  3922. }
  3923. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3924. break;
  3925. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3926. if(sd->state.lr_flag == 2)
  3927. break;
  3928. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3929. 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);
  3930. break;
  3931. }
  3932. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3933. break;
  3934. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3935. if(sd->state.lr_flag == 2)
  3936. break;
  3937. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3938. 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);
  3939. break;
  3940. }
  3941. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3942. break;
  3943. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3944. if(sd->state.lr_flag == 2)
  3945. break;
  3946. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3947. 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);
  3948. break;
  3949. }
  3950. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3951. break;
  3952. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3953. if(sd->state.lr_flag != 2) {
  3954. sd->hp_loss.value = type2;
  3955. sd->hp_loss.rate = val;
  3956. }
  3957. break;
  3958. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3959. if(sd->state.lr_flag != 2) {
  3960. sd->hp_regen.value = type2;
  3961. sd->hp_regen.rate = val;
  3962. }
  3963. break;
  3964. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3965. if (sd->state.lr_flag != 2) {
  3966. sd->percent_hp_regen.value = type2;
  3967. sd->percent_hp_regen.rate = val;
  3968. }
  3969. break;
  3970. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3971. if (sd->state.lr_flag != 2) {
  3972. sd->percent_sp_regen.value = type2;
  3973. sd->percent_sp_regen.rate = val;
  3974. }
  3975. break;
  3976. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3977. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3978. if(sd->state.lr_flag != 2)
  3979. sd->right_weapon.addrace2[type2] += val;
  3980. else
  3981. sd->left_weapon.addrace2[type2] += val;
  3982. break;
  3983. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3984. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3985. if(sd->state.lr_flag != 2)
  3986. sd->indexed_bonus.subsize[type2]+=val;
  3987. break;
  3988. case SP_WEAPON_SUBSIZE: // bonus2 bWeaponSubSize,s,x;
  3989. PC_BONUS_CHK_SIZE(type2, SP_WEAPON_SUBSIZE);
  3990. if (sd->state.lr_flag != 2)
  3991. sd->indexed_bonus.weapon_subsize[type2] += val;
  3992. break;
  3993. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  3994. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  3995. if(sd->state.lr_flag != 2)
  3996. sd->indexed_bonus.magic_subsize[type2]+=val;
  3997. break;
  3998. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3999. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  4000. if(sd->state.lr_flag != 2)
  4001. sd->indexed_bonus.subrace2[type2]+=val;
  4002. break;
  4003. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  4004. if(sd->state.lr_flag == 2)
  4005. break;
  4006. if( !item_db.exists( type2 ) ){
  4007. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  4008. break;
  4009. }
  4010. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  4011. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4012. break;
  4013. }
  4014. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  4015. break;
  4016. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  4017. if (sd->state.lr_flag == 2)
  4018. break;
  4019. if (!type2 || !itemdb_group.exists(type2)) {
  4020. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  4021. break;
  4022. }
  4023. if (sd->itemgrouphealrate.size() == MAX_PC_BONUS) {
  4024. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4025. break;
  4026. }
  4027. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  4028. break;
  4029. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  4030. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  4031. if(sd->state.lr_flag != 2)
  4032. sd->indexed_bonus.expaddrace[type2]+=val;
  4033. break;
  4034. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  4035. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  4036. if(sd->state.lr_flag != 2)
  4037. sd->indexed_bonus.expaddclass[type2]+=val;
  4038. break;
  4039. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  4040. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  4041. if(sd->state.lr_flag != 2)
  4042. sd->indexed_bonus.sp_gain_race[type2]+=val;
  4043. break;
  4044. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  4045. if (sd->state.lr_flag != 2)
  4046. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  4047. break;
  4048. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  4049. if (sd->state.lr_flag != 2)
  4050. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  4051. break;
  4052. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  4053. if(sd->state.lr_flag != 2) {
  4054. sd->sp_loss.value = type2;
  4055. sd->sp_loss.rate = val;
  4056. }
  4057. break;
  4058. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  4059. if(sd->state.lr_flag != 2) {
  4060. sd->sp_regen.value = type2;
  4061. sd->sp_regen.rate = val;
  4062. }
  4063. break;
  4064. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  4065. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  4066. if(!sd->state.lr_flag) {
  4067. sd->right_weapon.hp_drain_race[type2] += val;
  4068. }
  4069. else if(sd->state.lr_flag == 1) {
  4070. sd->left_weapon.hp_drain_race[type2] += val;
  4071. }
  4072. break;
  4073. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  4074. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  4075. if(!sd->state.lr_flag) {
  4076. sd->right_weapon.sp_drain_race[type2] += val;
  4077. }
  4078. else if(sd->state.lr_flag == 1) {
  4079. sd->left_weapon.sp_drain_race[type2] += val;
  4080. }
  4081. break;
  4082. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  4083. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  4084. if(!sd->state.lr_flag) {
  4085. sd->right_weapon.hp_drain_class[type2] += val;
  4086. }
  4087. else if(sd->state.lr_flag == 1) {
  4088. sd->left_weapon.hp_drain_class[type2] += val;
  4089. }
  4090. break;
  4091. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  4092. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  4093. if(!sd->state.lr_flag) {
  4094. sd->right_weapon.sp_drain_class[type2] += val;
  4095. }
  4096. else if(sd->state.lr_flag == 1) {
  4097. sd->left_weapon.sp_drain_class[type2] += val;
  4098. }
  4099. break;
  4100. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  4101. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  4102. if(sd->state.lr_flag != 2)
  4103. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  4104. break;
  4105. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  4106. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  4107. if(sd->state.lr_flag != 2)
  4108. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  4109. break;
  4110. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  4111. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  4112. if(sd->state.lr_flag != 2)
  4113. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  4114. break;
  4115. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  4116. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  4117. if (sd->state.lr_flag != 2)
  4118. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  4119. break;
  4120. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  4121. if(sd->state.lr_flag == 2)
  4122. break;
  4123. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  4124. 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);
  4125. break;
  4126. }
  4127. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  4128. break;
  4129. case SP_SKILL_DELAY:
  4130. if(sd->state.lr_flag == 2)
  4131. break;
  4132. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  4133. 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);
  4134. break;
  4135. }
  4136. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  4137. break;
  4138. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  4139. if(sd->state.lr_flag == 2)
  4140. break;
  4141. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  4142. 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);
  4143. break;
  4144. }
  4145. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  4146. break;
  4147. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  4148. if (sd->state.lr_flag == 2)
  4149. break;
  4150. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  4151. 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);
  4152. break;
  4153. }
  4154. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  4155. break;
  4156. #ifdef RENEWAL_CAST
  4157. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4158. if(sd->state.lr_flag == 2)
  4159. break;
  4160. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  4161. 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);
  4162. break;
  4163. }
  4164. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  4165. break;
  4166. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4167. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4168. if(sd->state.lr_flag == 2)
  4169. break;
  4170. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  4171. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4172. break;
  4173. }
  4174. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  4175. break;
  4176. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4177. if(sd->state.lr_flag == 2)
  4178. break;
  4179. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  4180. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4181. break;
  4182. }
  4183. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  4184. break;
  4185. #else
  4186. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4187. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4188. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  4189. break;
  4190. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4191. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4192. if(sd->state.lr_flag == 2)
  4193. break;
  4194. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4195. 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);
  4196. break;
  4197. }
  4198. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  4199. break;
  4200. #endif
  4201. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  4202. if(sd->state.lr_flag == 2)
  4203. break;
  4204. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  4205. 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);
  4206. break;
  4207. }
  4208. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  4209. break;
  4210. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  4211. if (sd->subskill.size() == MAX_PC_BONUS) {
  4212. 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);
  4213. break;
  4214. }
  4215. pc_bonus_itembonus(sd->subskill, type2, val, false);
  4216. break;
  4217. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  4218. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  4219. sd->indexed_bonus.subdefele[type2] += val;
  4220. break;
  4221. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  4222. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  4223. sd->indexed_bonus.coma_class[type2] += val;
  4224. sd->special_state.bonus_coma = 1;
  4225. break;
  4226. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  4227. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  4228. sd->indexed_bonus.coma_race[type2] += val;
  4229. sd->special_state.bonus_coma = 1;
  4230. break;
  4231. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  4232. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  4233. if(sd->state.lr_flag != 2)
  4234. sd->indexed_bonus.magic_addrace2[type2] += val;
  4235. break;
  4236. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  4237. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  4238. if (sd->state.lr_flag != 2)
  4239. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  4240. break;
  4241. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  4242. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  4243. if (sd->state.lr_flag != 2)
  4244. sd->indexed_bonus.dropaddrace[type2] += val;
  4245. break;
  4246. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  4247. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  4248. if (sd->state.lr_flag != 2)
  4249. sd->indexed_bonus.dropaddclass[type2] += val;
  4250. break;
  4251. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  4252. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  4253. sd->indexed_bonus.magic_subdefele[type2] += val;
  4254. break;
  4255. case SP_ADD_ITEM_SPHEAL_RATE: // bonus2 bAddItemSPHealRate,iid,n;
  4256. if( sd->state.lr_flag == 2 ){
  4257. break;
  4258. }
  4259. if( !item_db.exists( type2 ) ){
  4260. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE Invalid item with id %d\n", type2 );
  4261. break;
  4262. }
  4263. if( sd->itemsphealrate.size() == MAX_PC_BONUS ){
  4264. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4265. break;
  4266. }
  4267. pc_bonus_itembonus( sd->itemsphealrate, type2, val, false );
  4268. break;
  4269. case SP_ADD_ITEMGROUP_SPHEAL_RATE: // bonus2 bAddItemGroupSPHealRate,ig,n;
  4270. if( sd->state.lr_flag == 2 ){
  4271. break;
  4272. }
  4273. if( !type2 || !itemdb_group.exists( type2 ) ){
  4274. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Invalid item group with id %d\n", type2 );
  4275. break;
  4276. }
  4277. if( sd->itemgroupsphealrate.size() == MAX_PC_BONUS ){
  4278. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4279. break;
  4280. }
  4281. pc_bonus_itembonus( sd->itemgroupsphealrate, type2, val, false );
  4282. break;
  4283. default:
  4284. if (current_equip_combo_pos > 0) {
  4285. 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);
  4286. }
  4287. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4288. 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);
  4289. }
  4290. else {
  4291. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  4292. }
  4293. break;
  4294. }
  4295. }
  4296. /**
  4297. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  4298. * @param sd
  4299. * @param type Bonus type used by bBonusName
  4300. * @param type2
  4301. * @param type3
  4302. * @param val Value that usually for rate or fixed value
  4303. */
  4304. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  4305. {
  4306. nullpo_retv(sd);
  4307. switch(type){
  4308. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  4309. if(sd->state.lr_flag != 2)
  4310. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  4311. break;
  4312. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  4313. if(sd->state.lr_flag != 2)
  4314. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  4315. break;
  4316. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  4317. if(sd->state.lr_flag != 2)
  4318. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  4319. break;
  4320. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  4321. if(sd->state.lr_flag != 2)
  4322. {
  4323. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4324. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4325. pc_bonus_autospell(sd->autospell, type2, type3, val, 0, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4326. }
  4327. break;
  4328. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  4329. if(sd->state.lr_flag != 2)
  4330. {
  4331. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4332. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4333. pc_bonus_autospell(sd->autospell2, type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4334. }
  4335. break;
  4336. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  4337. if (sd->state.lr_flag != 2)
  4338. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  4339. break;
  4340. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  4341. if (sd->state.lr_flag != 2)
  4342. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  4343. break;
  4344. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  4345. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4346. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  4347. break;
  4348. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  4349. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4350. if(sd->state.lr_flag != 2)
  4351. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  4352. break;
  4353. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  4354. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  4355. if( sd->state.lr_flag != 2 )
  4356. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  4357. break;
  4358. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  4359. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  4360. if (sd->state.lr_flag != 2)
  4361. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  4362. break;
  4363. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  4364. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  4365. if (sd->state.lr_flag != 2)
  4366. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  4367. break;
  4368. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  4369. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  4370. if (sd->state.lr_flag != 2)
  4371. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  4372. break;
  4373. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  4374. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  4375. if(sd->state.lr_flag != 2) {
  4376. sd->sp_vanish_race[type2].rate += type3;
  4377. sd->sp_vanish_race[type2].per += val;
  4378. }
  4379. break;
  4380. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  4381. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  4382. if(sd->state.lr_flag != 2) {
  4383. sd->hp_vanish_race[type2].rate += type3;
  4384. sd->hp_vanish_race[type2].per += val;
  4385. }
  4386. break;
  4387. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  4388. if(sd->state.lr_flag != 2) {
  4389. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  4390. }
  4391. break;
  4392. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  4393. if(sd->state.lr_flag != 2) {
  4394. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  4395. }
  4396. break;
  4397. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  4398. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  4399. if (sd->state.lr_flag == 2)
  4400. break;
  4401. //! CONFIRM: Is it not stackable? Does not check max or min value?
  4402. //if (type3 > sd->norecover_state_race[type2].rate) {
  4403. // sd->norecover_state_race[type2].rate = type3;
  4404. // sd->norecover_state_race[type2].tick = val;
  4405. // break;
  4406. //}
  4407. sd->norecover_state_race[type2].rate = type3;
  4408. sd->norecover_state_race[type2].tick = val;
  4409. break;
  4410. default:
  4411. if (current_equip_combo_pos > 0) {
  4412. 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);
  4413. }
  4414. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4415. 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);
  4416. }
  4417. else {
  4418. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  4419. }
  4420. break;
  4421. }
  4422. }
  4423. /**
  4424. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  4425. * @param sd
  4426. * @param type Bonus type used by bBonusName
  4427. * @param type2
  4428. * @param type3
  4429. * @param type4
  4430. * @param val Value that usually for rate or fixed value
  4431. */
  4432. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  4433. {
  4434. nullpo_retv(sd);
  4435. switch(type){
  4436. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4437. if(sd->state.lr_flag != 2)
  4438. pc_bonus_autospell(sd->autospell, type2, type3, type4, 0, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4439. break;
  4440. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4441. if(sd->state.lr_flag != 2)
  4442. pc_bonus_autospell(sd->autospell2, type2, type3, type4, BF_NORMAL|BF_SKILL, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4443. break;
  4444. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4445. if(sd->state.lr_flag != 2)
  4446. {
  4447. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4448. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4449. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, val, current_equip_card_id, target ? AUTOSPELL_FORCE_TARGET : AUTOSPELL_FORCE_SELF);
  4450. }
  4451. break;
  4452. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4453. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4454. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  4455. break;
  4456. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4457. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4458. if (sd->state.lr_flag != 2)
  4459. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4460. break;
  4461. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4462. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4463. if( sd->state.lr_flag != 2 )
  4464. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4465. break;
  4466. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4467. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4468. if(sd->state.lr_flag == 2)
  4469. break;
  4470. sd->def_set_race[type2].rate = type3;
  4471. sd->def_set_race[type2].tick = type4;
  4472. sd->def_set_race[type2].value = val;
  4473. break;
  4474. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4475. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4476. if(sd->state.lr_flag == 2)
  4477. break;
  4478. sd->mdef_set_race[type2].rate = type3;
  4479. sd->mdef_set_race[type2].tick = type4;
  4480. sd->mdef_set_race[type2].value = val;
  4481. break;
  4482. default:
  4483. if (current_equip_combo_pos > 0) {
  4484. 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);
  4485. }
  4486. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4487. 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);
  4488. }
  4489. else {
  4490. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4491. }
  4492. break;
  4493. }
  4494. }
  4495. /**
  4496. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4497. * @param sd
  4498. * @param type Bonus type used by bBonusName
  4499. * @param type2
  4500. * @param type3
  4501. * @param type4
  4502. * @param val Value that usually for rate or fixed value
  4503. */
  4504. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  4505. {
  4506. nullpo_retv(sd);
  4507. switch(type){
  4508. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4509. if(sd->state.lr_flag != 2)
  4510. pc_bonus_autospell(sd->autospell, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4511. break;
  4512. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4513. if(sd->state.lr_flag != 2)
  4514. pc_bonus_autospell(sd->autospell2, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4515. break;
  4516. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4517. if(sd->state.lr_flag != 2)
  4518. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4519. break;
  4520. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4521. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4522. if( sd->state.lr_flag != 2 )
  4523. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4524. break;
  4525. default:
  4526. if (current_equip_combo_pos > 0) {
  4527. 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);
  4528. }
  4529. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4530. 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);
  4531. }
  4532. else {
  4533. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4534. }
  4535. break;
  4536. }
  4537. }
  4538. /*==========================================
  4539. * Grants a player a given skill. Flag values are:
  4540. * 0 - Grant permanent skill to be bound to skill tree
  4541. * 1 - Grant an item skill (temporary)
  4542. * 2 - Like 1, except the level granted can stack with previously learned level.
  4543. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4544. *------------------------------------------*/
  4545. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  4546. uint16 idx = 0;
  4547. nullpo_ret(sd);
  4548. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4549. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4550. return false;
  4551. }
  4552. if (level > MAX_SKILL_LEVEL) {
  4553. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4554. return false;
  4555. }
  4556. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4557. 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);
  4558. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4559. }
  4560. switch (type) {
  4561. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4562. sd->status.skill[idx].id = skill_id;
  4563. sd->status.skill[idx].lv = level;
  4564. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4565. if (level == 0) { //Remove skill.
  4566. sd->status.skill[idx].id = 0;
  4567. clif_deleteskill(sd,skill_id);
  4568. } else
  4569. clif_addskill(sd,skill_id);
  4570. 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.
  4571. status_calc_pc(sd, SCO_NONE);
  4572. break;
  4573. case ADDSKILL_TEMP: //Item bonus skill.
  4574. if (sd->status.skill[idx].id != 0) {
  4575. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4576. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4577. } else {
  4578. sd->status.skill[idx].id = skill_id;
  4579. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4580. }
  4581. sd->status.skill[idx].lv = max(sd->status.skill[idx].lv, level);
  4582. break;
  4583. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4584. if (sd->status.skill[idx].id != 0) {
  4585. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4586. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4587. } else {
  4588. sd->status.skill[idx].id = skill_id;
  4589. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4590. }
  4591. sd->status.skill[idx].lv += level;
  4592. break;
  4593. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4594. sd->status.skill[idx].id = skill_id;
  4595. sd->status.skill[idx].lv = level;
  4596. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4597. if (level == 0) { //Remove skill.
  4598. sd->status.skill[idx].id = 0;
  4599. clif_deleteskill(sd,skill_id);
  4600. } else
  4601. clif_addskill(sd,skill_id);
  4602. 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.
  4603. status_calc_pc(sd, SCO_NONE);
  4604. break;
  4605. default:
  4606. return false;
  4607. }
  4608. return true;
  4609. }
  4610. /**
  4611. * Set's a player's plagiarized skill.
  4612. * @param sd: Player
  4613. * @param skill_id: Skill to be plagiarized
  4614. * @param skill_lv: Skill level to be plagiarized
  4615. * @return True on success or false otherwise
  4616. */
  4617. bool pc_skill_plagiarism(map_session_data &sd, uint16 skill_id, uint16 skill_lv)
  4618. {
  4619. skill_id = skill_dummy2skill_id(skill_id);
  4620. uint16 idx = skill_get_index(skill_id);
  4621. // Use skill index, avoiding out-of-bound array [Cydh]
  4622. if (idx == 0) {
  4623. ShowWarning("pc_skill_plagiarism: invalid skill idx 0 for skill %d.\n", skill_id);
  4624. return false;
  4625. }
  4626. skill_lv = cap_value(skill_lv, 1, skill_get_max(skill_id));
  4627. int type = skill_isCopyable(&sd, skill_id);
  4628. if (type == 1) {
  4629. pc_skill_plagiarism_reset(sd, type);
  4630. sd.cloneskill_idx = idx;
  4631. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), skill_id);
  4632. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), skill_lv);
  4633. } else if (type == 2) {
  4634. pc_skill_plagiarism_reset(sd, type);
  4635. sd.reproduceskill_idx = idx;
  4636. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), skill_id);
  4637. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), skill_lv);
  4638. } else {
  4639. ShowWarning("pc_skill_plagiarism: skill %d is not copyable.\n", skill_id);
  4640. return false;
  4641. }
  4642. sd.status.skill[idx].id = skill_id;
  4643. sd.status.skill[idx].lv = static_cast<uint8>(skill_lv);
  4644. sd.status.skill[idx].flag = SKILL_FLAG_PLAGIARIZED;
  4645. clif_addskill(&sd, skill_id);
  4646. return true;
  4647. }
  4648. /**
  4649. * Clear plagiarized skills from a player.
  4650. * @param sd: Player
  4651. * @param type: 1 for Plagiarism or 2 for Reproduce
  4652. * @return True on success or false otherwise
  4653. */
  4654. bool pc_skill_plagiarism_reset(map_session_data &sd, uint8 type)
  4655. {
  4656. uint16 idx;
  4657. if (type == 1)
  4658. idx = sd.cloneskill_idx;
  4659. else if (type == 2)
  4660. idx = sd.reproduceskill_idx;
  4661. else {
  4662. ShowError("pc_skill_plagiarism_reset: Unknown type %d.\n", type);
  4663. return false;
  4664. }
  4665. if (sd.status.skill[idx].flag == SKILL_FLAG_PLAGIARIZED) {
  4666. uint16 skill_id = sd.status.skill[idx].id;
  4667. sd.status.skill[idx].id = 0;
  4668. sd.status.skill[idx].lv = 0;
  4669. sd.status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4670. clif_deleteskill(&sd, skill_id);
  4671. if (type == 1) {
  4672. sd.cloneskill_idx = 0;
  4673. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  4674. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  4675. }
  4676. else if (type == 2) {
  4677. sd.reproduceskill_idx = 0;
  4678. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), 0);
  4679. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  4680. }
  4681. }
  4682. return true;
  4683. }
  4684. /*==========================================
  4685. * Append a card to an item ?
  4686. *------------------------------------------*/
  4687. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  4688. {
  4689. nullpo_ret(sd);
  4690. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4691. return 0;
  4692. }
  4693. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4694. return 0;
  4695. }
  4696. int i;
  4697. t_itemid nameid;
  4698. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4699. struct item_data* item_card = sd->inventory_data[idx_card];
  4700. if(item_eq == nullptr)
  4701. return 0; //Invalid item index.
  4702. if(item_card == nullptr)
  4703. return 0; //Invalid card index.
  4704. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4705. return 0; // target item missing
  4706. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4707. return 0; // target card missing
  4708. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4709. return 0; // only weapons and armor are allowed
  4710. if( item_card->type != IT_CARD )
  4711. return 0; // must be a card
  4712. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4713. return 0; // target must be identified
  4714. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4715. return 0; // card slots reserved for other purposes
  4716. if( (item_eq->equip & item_card->equip) == 0 )
  4717. return 0; // card cannot be compounded on this item type
  4718. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4719. return 0; // attempted to place shield card on left-hand weapon.
  4720. 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)) )
  4721. return 0; // specific accessory-card can only be inserted to specific accessory.
  4722. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4723. return 0; // item must be unequipped
  4724. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4725. if( i == item_eq->slots )
  4726. return 0; // no free slots
  4727. // remember the card id to insert
  4728. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4729. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4730. {// failed
  4731. clif_insert_card(sd,idx_equip,idx_card,1);
  4732. }
  4733. else
  4734. {// success
  4735. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4736. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4737. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4738. clif_insert_card(sd,idx_equip,idx_card,0);
  4739. }
  4740. return 0;
  4741. }
  4742. /**
  4743. * Returns the count of unidentified items with the option to identify too.
  4744. * @param sd: Player data
  4745. * @param identify_item: Whether or not to identify any unidentified items
  4746. * @return Unidentified items count
  4747. */
  4748. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  4749. {
  4750. int unidentified_count = 0;
  4751. for (int i = 0; i < MAX_INVENTORY; i++) {
  4752. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4753. if (identify_item == true) {
  4754. sd->inventory.u.items_inventory[i].identify = 1;
  4755. clif_item_identified(sd,i,0);
  4756. }
  4757. unidentified_count++;
  4758. }
  4759. }
  4760. return unidentified_count;
  4761. }
  4762. //
  4763. // Items
  4764. //
  4765. /*==========================================
  4766. * Update buying value by skills
  4767. *------------------------------------------*/
  4768. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4769. {
  4770. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4771. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4772. rate1 = 5+skill*2-((skill==10)? 1:0);
  4773. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4774. rate2 = 5+skill*4;
  4775. if(rate1 < rate2) rate1 = rate2;
  4776. if(rate1)
  4777. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4778. if(val < battle_config.min_shop_buy)
  4779. val = battle_config.min_shop_buy;
  4780. return val;
  4781. }
  4782. /*==========================================
  4783. * Update selling value by skills
  4784. *------------------------------------------*/
  4785. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4786. {
  4787. int skill,val = orig_value,rate = 0;
  4788. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4789. rate = 5+skill*2-((skill==10)? 1:0);
  4790. if(rate)
  4791. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4792. if (val < battle_config.min_shop_sell)
  4793. val = battle_config.min_shop_sell;
  4794. return val;
  4795. }
  4796. /*==========================================
  4797. * Checking if we have enough place on inventory for new item
  4798. * Make sure to take 30k as limit (for client I guess)
  4799. * @param sd
  4800. * @param nameid
  4801. * @param amount
  4802. * @return e_chkitem_result
  4803. *------------------------------------------*/
  4804. char pc_checkadditem(struct map_session_data *sd, t_itemid nameid, int amount)
  4805. {
  4806. int i;
  4807. struct item_data* data;
  4808. nullpo_ret(sd);
  4809. if(amount > MAX_AMOUNT)
  4810. return CHKADDITEM_OVERAMOUNT;
  4811. data = itemdb_search(nameid);
  4812. if(!itemdb_isstackable2(data))
  4813. return CHKADDITEM_NEW;
  4814. if( data->stack.inventory && amount > data->stack.amount )
  4815. return CHKADDITEM_OVERAMOUNT;
  4816. if (data->flag.guid)
  4817. return CHKADDITEM_NEW;
  4818. for(i=0;i<MAX_INVENTORY;i++){
  4819. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4820. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4821. 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 ) )
  4822. return CHKADDITEM_OVERAMOUNT;
  4823. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4824. if( i >= sd->status.inventory_slots ){
  4825. return CHKADDITEM_OVERAMOUNT;
  4826. }
  4827. return CHKADDITEM_EXIST;
  4828. }
  4829. }
  4830. return CHKADDITEM_NEW;
  4831. }
  4832. /*==========================================
  4833. * Return number of available place in inventory
  4834. * Each non stackable item will reduce place by 1
  4835. * @param sd
  4836. * @return Number of empty slots
  4837. *------------------------------------------*/
  4838. uint8 pc_inventoryblank(struct map_session_data *sd)
  4839. {
  4840. uint16 i;
  4841. uint8 b;
  4842. nullpo_ret(sd);
  4843. for(i = 0, b = 0; i < sd->status.inventory_slots; i++){
  4844. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4845. b++;
  4846. }
  4847. return b;
  4848. }
  4849. /**
  4850. * Attempts to remove zeny from player
  4851. * @param sd: Player
  4852. * @param zeny: Zeny removed
  4853. * @param type: Log type
  4854. * @param tsd: (optional) From who to log (if null take sd)
  4855. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4856. */
  4857. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4858. {
  4859. nullpo_retr(2,sd);
  4860. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4861. if( zeny < 0 )
  4862. {
  4863. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4864. return 1;
  4865. }
  4866. if( sd->status.zeny < zeny )
  4867. return 1; //Not enough.
  4868. sd->status.zeny -= zeny;
  4869. clif_updatestatus(sd,SP_ZENY);
  4870. if(!tsd) tsd = sd;
  4871. log_zeny(sd, type, tsd, -zeny);
  4872. if( zeny > 0 && sd->state.showzeny ) {
  4873. char output[255];
  4874. sprintf(output, "Removed %dz.", zeny);
  4875. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4876. }
  4877. return 0;
  4878. }
  4879. /**
  4880. * Attempts to give zeny to player
  4881. * @param sd: Player
  4882. * @param type: Log type
  4883. * @param tsd: (optional) From who to log (if null take sd)
  4884. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4885. */
  4886. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4887. {
  4888. nullpo_retr(-1,sd);
  4889. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4890. if( zeny < 0 )
  4891. {
  4892. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4893. return 1;
  4894. }
  4895. if( zeny > MAX_ZENY - sd->status.zeny )
  4896. zeny = MAX_ZENY - sd->status.zeny;
  4897. sd->status.zeny += zeny;
  4898. clif_updatestatus(sd,SP_ZENY);
  4899. if(!tsd) tsd = sd;
  4900. log_zeny(sd, type, tsd, zeny);
  4901. if( zeny > 0 && sd->state.showzeny ) {
  4902. char output[255];
  4903. sprintf(output, "Gained %dz.", zeny);
  4904. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4905. }
  4906. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4907. return 0;
  4908. }
  4909. /**
  4910. * Attempts to remove Cash Points from player
  4911. * @param sd: Player
  4912. * @param price: Total points (cash + kafra) the player has to pay
  4913. * @param points: Kafra points the player has to pay
  4914. * @param type: Log type
  4915. * @return -1: Not enough points, otherwise success (cash+points)
  4916. */
  4917. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4918. {
  4919. int cash;
  4920. nullpo_retr(-1,sd);
  4921. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4922. cash = price-points;
  4923. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4924. {
  4925. 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);
  4926. return -1;
  4927. }
  4928. if( cash ){
  4929. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4930. sd->cashPoints -= cash;
  4931. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4932. }
  4933. if( points ){
  4934. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4935. sd->kafraPoints -= points;
  4936. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4937. }
  4938. if( battle_config.cashshop_show_points )
  4939. {
  4940. char output[CHAT_SIZE_MAX];
  4941. 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.
  4942. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4943. }
  4944. return cash+points;
  4945. }
  4946. /**
  4947. * Attempts to give Cash Points to player
  4948. * @param sd: Player
  4949. * @param cash: Cash points the player gets
  4950. * @param points: Kafra points the player gets
  4951. * @param type: Log type
  4952. * @return -1: Error, otherwise success (cash or points)
  4953. */
  4954. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4955. {
  4956. char output[CHAT_SIZE_MAX];
  4957. nullpo_retr(-1,sd);
  4958. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4959. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4960. if( cash > 0 )
  4961. {
  4962. if( cash > MAX_ZENY-sd->cashPoints )
  4963. {
  4964. 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);
  4965. cash = MAX_ZENY-sd->cashPoints;
  4966. }
  4967. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4968. sd->cashPoints += cash;
  4969. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4970. if( battle_config.cashshop_show_points )
  4971. {
  4972. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4973. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4974. }
  4975. return cash;
  4976. }
  4977. if( points > 0 )
  4978. {
  4979. if( points > MAX_ZENY-sd->kafraPoints )
  4980. {
  4981. 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);
  4982. points = MAX_ZENY-sd->kafraPoints;
  4983. }
  4984. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4985. sd->kafraPoints += points;
  4986. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4987. if( battle_config.cashshop_show_points )
  4988. {
  4989. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4990. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4991. }
  4992. return points;
  4993. }
  4994. return -1; //shouldn't happen but just in case
  4995. }
  4996. /**
  4997. * Searching a specified itemid in inventory and return his stored index
  4998. * @param sd Player
  4999. * @param nameid Find this Item!
  5000. * @return Stored index in inventory, or -1 if not found.
  5001. **/
  5002. short pc_search_inventory(struct map_session_data *sd, t_itemid nameid) {
  5003. short i;
  5004. nullpo_retr(-1, sd);
  5005. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  5006. return ( i < MAX_INVENTORY ) ? i : -1;
  5007. }
  5008. /** Attempt to add a new item to player inventory
  5009. * @param sd
  5010. * @param item_data
  5011. * @param amount
  5012. * @param log_type
  5013. * @return
  5014. * 0 = success
  5015. * 1 = invalid itemid not found or negative amount
  5016. * 2 = overweight
  5017. * 3 = ?
  5018. * 4 = no free place found
  5019. * 5 = max amount reached
  5020. * 6 = ?
  5021. * 7 = stack limitation
  5022. */
  5023. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  5024. struct item_data *id;
  5025. int16 i;
  5026. unsigned int w;
  5027. nullpo_retr(ADDITEM_INVALID, sd);
  5028. nullpo_retr(ADDITEM_INVALID, item);
  5029. if( item->nameid == 0 || amount <= 0 )
  5030. return ADDITEM_INVALID;
  5031. if( amount > MAX_AMOUNT )
  5032. return ADDITEM_OVERAMOUNT;
  5033. id = itemdb_search(item->nameid);
  5034. if( id->stack.inventory && amount > id->stack.amount )
  5035. {// item stack limitation
  5036. return ADDITEM_STACKLIMIT;
  5037. }
  5038. w = id->weight*amount;
  5039. if(sd->weight + w > sd->max_weight)
  5040. return ADDITEM_OVERWEIGHT;
  5041. if (id->flag.guid && !item->unique_id)
  5042. item->unique_id = pc_generate_unique_id(sd);
  5043. // Stackable | Non Rental
  5044. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  5045. for( i = 0; i < MAX_INVENTORY; i++ ) {
  5046. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  5047. sd->inventory.u.items_inventory[i].bound == item->bound &&
  5048. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  5049. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  5050. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  5051. 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 ) )
  5052. return ADDITEM_OVERAMOUNT;
  5053. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  5054. if( i >= sd->status.inventory_slots ){
  5055. return ADDITEM_OVERAMOUNT;
  5056. }
  5057. sd->inventory.u.items_inventory[i].amount += amount;
  5058. clif_additem(sd,i,amount,0);
  5059. break;
  5060. }
  5061. }
  5062. }else{
  5063. i = MAX_INVENTORY;
  5064. }
  5065. if (i >= MAX_INVENTORY) {
  5066. i = pc_search_inventory(sd,0);
  5067. if( i < 0 )
  5068. return ADDITEM_OVERITEM;
  5069. if( i >= sd->status.inventory_slots ){
  5070. return ADDITEM_OVERITEM;
  5071. }
  5072. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  5073. // clear equip and favorite fields first, just in case
  5074. if( item->equip )
  5075. sd->inventory.u.items_inventory[i].equip = 0;
  5076. if( item->favorite )
  5077. sd->inventory.u.items_inventory[i].favorite = 0;
  5078. if( item->equipSwitch )
  5079. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  5080. sd->inventory.u.items_inventory[i].amount = amount;
  5081. sd->inventory_data[i] = id;
  5082. sd->last_addeditem_index = i;
  5083. if (!itemdb_isstackable2(id) || id->flag.guid)
  5084. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  5085. clif_additem(sd,i,amount,0);
  5086. }
  5087. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  5088. sd->weight += w;
  5089. clif_updatestatus(sd,SP_WEIGHT);
  5090. //Auto-equip
  5091. if(id->flag.autoequip)
  5092. pc_equipitem(sd, i, id->equip);
  5093. /* rental item check */
  5094. if( item->expire_time ) {
  5095. if( time(NULL) > item->expire_time ) {
  5096. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  5097. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  5098. } else {
  5099. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  5100. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  5101. pc_inventory_rental_add(sd, seconds);
  5102. }
  5103. }
  5104. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  5105. pc_show_questinfo(sd);
  5106. return ADDITEM_SUCCESS;
  5107. }
  5108. /*==========================================
  5109. * Remove an item at index n from inventory by amount.
  5110. * @param sd
  5111. * @param n Item index in inventory
  5112. * @param amount
  5113. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  5114. * @param reason Delete reason
  5115. * @param log_type e_log_pick_type
  5116. * @return 1 - invalid itemid or negative amount; 0 - Success
  5117. *------------------------------------------*/
  5118. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  5119. {
  5120. nullpo_retr(1, sd);
  5121. 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)
  5122. return 1;
  5123. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  5124. sd->inventory.u.items_inventory[n].amount -= amount;
  5125. sd->weight -= sd->inventory_data[n]->weight*amount ;
  5126. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  5127. if(sd->inventory.u.items_inventory[n].equip)
  5128. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  5129. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  5130. sd->inventory_data[n] = NULL;
  5131. }
  5132. if(!(type&1))
  5133. clif_delitem(sd,n,amount,reason);
  5134. if(!(type&2))
  5135. clif_updatestatus(sd,SP_WEIGHT);
  5136. pc_show_questinfo(sd);
  5137. return 0;
  5138. }
  5139. /*==========================================
  5140. * Attempt to drop an item.
  5141. * @param sd
  5142. * @param n Item index in inventory
  5143. * @param amount Amount of item
  5144. * @return False = fail; True = success
  5145. *------------------------------------------*/
  5146. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  5147. {
  5148. nullpo_retr(1, sd);
  5149. if(n < 0 || n >= MAX_INVENTORY)
  5150. return false;
  5151. if(amount <= 0)
  5152. return false;
  5153. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  5154. sd->inventory.u.items_inventory[n].amount <= 0 ||
  5155. sd->inventory.u.items_inventory[n].amount < amount ||
  5156. sd->state.trading || sd->state.vending ||
  5157. !sd->inventory_data[n] //pc_delitem would fail on this case.
  5158. )
  5159. return false;
  5160. if( sd->inventory.u.items_inventory[n].equipSwitch )
  5161. return false;
  5162. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  5163. {
  5164. clif_displaymessage (sd->fd, msg_txt(sd,271));
  5165. return false; //Can't drop items in nodrop mapflag maps.
  5166. }
  5167. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  5168. {
  5169. clif_displaymessage (sd->fd, msg_txt(sd,263));
  5170. return false;
  5171. }
  5172. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  5173. return false;
  5174. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  5175. clif_dropitem(sd, n, amount);
  5176. return true;
  5177. }
  5178. /*==========================================
  5179. * Attempt to pick up an item.
  5180. * @param sd
  5181. * @param fitem Item that will be picked
  5182. * @return False = fail; True = success
  5183. *------------------------------------------*/
  5184. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  5185. {
  5186. int flag = 0;
  5187. t_tick tick = gettick();
  5188. struct party_data *p = NULL;
  5189. nullpo_ret(sd);
  5190. nullpo_ret(fitem);
  5191. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  5192. return false; // Distance is too far
  5193. if (sd->sc.cant.pickup)
  5194. return false;
  5195. if (sd->status.party_id)
  5196. p = party_search(sd->status.party_id);
  5197. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  5198. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  5199. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  5200. if (!(p && p->party.item&1 &&
  5201. first_sd && first_sd->status.party_id == sd->status.party_id
  5202. ))
  5203. return false;
  5204. }
  5205. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  5206. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  5207. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  5208. if (!(p && p->party.item&1 &&
  5209. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5210. (second_sd && second_sd->status.party_id == sd->status.party_id))
  5211. ))
  5212. return false;
  5213. }
  5214. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  5215. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  5216. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  5217. if(!(p && p->party.item&1 &&
  5218. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5219. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  5220. (third_sd && third_sd->status.party_id == sd->status.party_id))
  5221. ))
  5222. return false;
  5223. }
  5224. }
  5225. }
  5226. }
  5227. //This function takes care of giving the item to whoever should have it, considering party-share options.
  5228. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  5229. clif_additem(sd,0,0,flag);
  5230. return true;
  5231. }
  5232. //Display pickup animation.
  5233. pc_stop_attack(sd);
  5234. clif_takeitem(&sd->bl,&fitem->bl);
  5235. if (fitem->mob_id &&
  5236. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  5237. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  5238. )
  5239. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  5240. map_clearflooritem(&fitem->bl);
  5241. return true;
  5242. }
  5243. /*==========================================
  5244. * Check if item is usable.
  5245. * Return:
  5246. * false = no
  5247. * true = yes
  5248. *------------------------------------------*/
  5249. bool pc_isUseitem(struct map_session_data *sd,int n)
  5250. {
  5251. struct item_data *item;
  5252. t_itemid nameid;
  5253. nullpo_ret(sd);
  5254. item = sd->inventory_data[n];
  5255. nameid = sd->inventory.u.items_inventory[n].nameid;
  5256. if( item == NULL )
  5257. return false;
  5258. //Not consumable item
  5259. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  5260. return false;
  5261. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  5262. return true;
  5263. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5264. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  5265. return false;
  5266. //Prevent mass item usage. [Skotlex]
  5267. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  5268. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  5269. )
  5270. return false;
  5271. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  5272. clif_msg(sd,ITEM_NOUSE_SITTING);
  5273. return false; // You cannot use this item while sitting.
  5274. }
  5275. if (sd->state.storage_flag && item->type != IT_CASH) {
  5276. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  5277. return false; // You cannot use this item while storage is open.
  5278. }
  5279. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  5280. return false;
  5281. switch( nameid ) {
  5282. case ITEMID_WING_OF_FLY:
  5283. case ITEMID_GIANT_FLY_WING:
  5284. case ITEMID_N_FLY_WING:
  5285. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  5286. clif_skill_teleportmessage(sd,0);
  5287. return false;
  5288. }
  5289. if (nameid == ITEMID_GIANT_FLY_WING) {
  5290. struct party_data *pd = party_search(sd->status.party_id);
  5291. if (pd) {
  5292. int i;
  5293. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  5294. if (i == MAX_PARTY) { // User is not party leader
  5295. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  5296. break;
  5297. }
  5298. 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));
  5299. if (i == MAX_PARTY) { // No party members found on same map
  5300. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  5301. break;
  5302. }
  5303. } else {
  5304. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  5305. break;
  5306. }
  5307. }
  5308. // Fall through
  5309. case ITEMID_WING_OF_BUTTERFLY:
  5310. case ITEMID_N_BUTTERFLY_WING:
  5311. case ITEMID_DUN_TELE_SCROLL1:
  5312. case ITEMID_DUN_TELE_SCROLL2:
  5313. case ITEMID_DUN_TELE_SCROLL3:
  5314. case ITEMID_WOB_RUNE:
  5315. case ITEMID_WOB_SCHWALTZ:
  5316. case ITEMID_WOB_RACHEL:
  5317. case ITEMID_WOB_LOCAL:
  5318. case ITEMID_SIEGE_TELEPORT_SCROLL:
  5319. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  5320. clif_displaymessage(sd->fd, msg_txt(sd,663));
  5321. return false;
  5322. }
  5323. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  5324. return false;
  5325. break;
  5326. case ITEMID_MERCENARY_RED_POTION:
  5327. case ITEMID_MERCENARY_BLUE_POTION:
  5328. case ITEMID_M_CENTER_POTION:
  5329. case ITEMID_M_AWAKENING_POTION:
  5330. case ITEMID_M_BERSERK_POTION:
  5331. if( sd->md == NULL || sd->md->db == NULL )
  5332. return false;
  5333. if( sd->md->sc.cant.consume )
  5334. return false;
  5335. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  5336. return false;
  5337. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  5338. return false;
  5339. break;
  5340. case ITEMID_NEURALIZER:
  5341. if( !mapdata->flag[MF_RESET] )
  5342. return false;
  5343. break;
  5344. }
  5345. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  5346. return false; // Mercenary Scrolls
  5347. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  5348. if( pc_is90overweight(sd) ) {
  5349. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  5350. return false;
  5351. }
  5352. if( !pc_inventoryblank(sd) ) {
  5353. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  5354. return false;
  5355. }
  5356. }
  5357. //Gender check
  5358. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  5359. return false;
  5360. //Required level check
  5361. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  5362. return false;
  5363. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  5364. return false;
  5365. //Not equipable by class. [Skotlex]
  5366. if (!pc_job_can_use_item(sd,item))
  5367. return false;
  5368. if (sd->sc.cant.consume)
  5369. return false;
  5370. if (!pc_isItemClass(sd,item))
  5371. return false;
  5372. //Dead Branch items
  5373. if( item->flag.dead_branch )
  5374. log_branch(sd);
  5375. return true;
  5376. }
  5377. /*==========================================
  5378. * Last checks to use an item.
  5379. * Return:
  5380. * 0 = fail
  5381. * 1 = success
  5382. *------------------------------------------*/
  5383. int pc_useitem(struct map_session_data *sd,int n)
  5384. {
  5385. t_tick tick = gettick();
  5386. int amount;
  5387. t_itemid nameid;
  5388. struct script_code *script;
  5389. struct item item;
  5390. struct item_data *id;
  5391. nullpo_ret(sd);
  5392. if (sd->state.mail_writing)
  5393. return 0;
  5394. if (sd->npc_id) {
  5395. if (sd->progressbar.npc_id) {
  5396. clif_progressbar_abort(sd);
  5397. return 0; // First item use attempt cancels the progress bar
  5398. }
  5399. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  5400. #ifdef RENEWAL
  5401. clif_msg( sd, WORK_IN_PROGRESS );
  5402. #endif
  5403. return 0;
  5404. }
  5405. }
  5406. item = sd->inventory.u.items_inventory[n];
  5407. id = sd->inventory_data[n];
  5408. if (item.nameid == 0 || item.amount <= 0)
  5409. return 0;
  5410. if( !pc_isUseitem(sd,n) )
  5411. return 0;
  5412. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  5413. nameid = id->nameid;
  5414. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  5415. return 0;
  5416. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  5417. if( id->flag.delay_consume > 0 ) {
  5418. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  5419. return 0;
  5420. else if( pc_issit(sd) )
  5421. return 0;
  5422. }
  5423. //Since most delay-consume items involve using a "skill-type" target cursor,
  5424. //perform a skill-use check before going through. [Skotlex]
  5425. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  5426. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  5427. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  5428. return 0;
  5429. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  5430. return 0;
  5431. /* on restricted maps the item is consumed but the effect is not used */
  5432. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  5433. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  5434. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  5435. clif_useitemack(sd,n,item.amount-1,true);
  5436. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  5437. }
  5438. return 0;/* regardless, effect is not run */
  5439. }
  5440. if (pet_db_search(id->nameid, PET_CATCH) != nullptr && map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  5441. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  5442. return 0;
  5443. }
  5444. sd->itemid = item.nameid;
  5445. sd->itemindex = n;
  5446. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  5447. sd->catch_target_class = PET_CATCH_FAIL;
  5448. amount = item.amount;
  5449. script = id->script;
  5450. //Check if the item is to be consumed immediately [Skotlex]
  5451. if (id->flag.delay_consume > 0)
  5452. clif_useitemack(sd, n, amount, true);
  5453. else
  5454. {
  5455. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  5456. {
  5457. clif_useitemack(sd, n, amount - 1, true);
  5458. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  5459. }
  5460. else
  5461. clif_useitemack(sd, n, 0, false);
  5462. }
  5463. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  5464. potion_flag = 2; // Famous player's potions have 50% more efficiency
  5465. //Update item use time.
  5466. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  5467. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  5468. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  5469. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  5470. potion_flag = 0;
  5471. return 1;
  5472. }
  5473. /**
  5474. * Add item on cart for given index.
  5475. * @param sd
  5476. * @param item
  5477. * @param amount
  5478. * @param log_type
  5479. * @return See pc.hpp::e_additem_result
  5480. */
  5481. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  5482. {
  5483. struct item_data *data;
  5484. int i,w;
  5485. nullpo_retr(ADDITEM_INVALID, sd);
  5486. nullpo_retr(ADDITEM_INVALID, item);
  5487. if(item->nameid == 0 || amount <= 0)
  5488. return ADDITEM_INVALID;
  5489. if (itemdb_ishatched_egg(item))
  5490. return ADDITEM_INVALID;
  5491. data = itemdb_search(item->nameid);
  5492. if( data->stack.cart && amount > data->stack.amount )
  5493. {// item stack limitation
  5494. return ADDITEM_STACKLIMIT;
  5495. }
  5496. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  5497. { // Check item trade restrictions [Skotlex]
  5498. clif_displaymessage (sd->fd, msg_txt(sd,264));
  5499. return ADDITEM_INVALID;
  5500. }
  5501. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  5502. return ADDITEM_OVERWEIGHT;
  5503. i = MAX_CART;
  5504. if( itemdb_isstackable2(data) && !item->expire_time )
  5505. {
  5506. for (i = 0; i < MAX_CART; i++) {
  5507. if (sd->cart.u.items_cart[i].nameid == item->nameid
  5508. && sd->cart.u.items_cart[i].bound == item->bound
  5509. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5510. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5511. )
  5512. break;
  5513. }
  5514. }
  5515. if( i < MAX_CART )
  5516. {// item already in cart, stack it
  5517. 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 ) )
  5518. return ADDITEM_OVERAMOUNT; // no slot
  5519. sd->cart.u.items_cart[i].amount += amount;
  5520. clif_cart_additem(sd,i,amount);
  5521. }
  5522. else
  5523. {// item not stackable or not present, add it
  5524. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5525. if( i == MAX_CART )
  5526. return ADDITEM_OVERAMOUNT; // no slot
  5527. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5528. sd->cart.u.items_cart[i].id = 0;
  5529. sd->cart.u.items_cart[i].amount = amount;
  5530. sd->cart_num++;
  5531. clif_cart_additem(sd,i,amount);
  5532. }
  5533. sd->cart.u.items_cart[i].favorite = 0; // clear
  5534. sd->cart.u.items_cart[i].equipSwitch = 0;
  5535. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5536. sd->cart_weight += w;
  5537. clif_updatestatus(sd,SP_CARTINFO);
  5538. return ADDITEM_SUCCESS;
  5539. }
  5540. /*==========================================
  5541. * Delete item on cart for given index.
  5542. *------------------------------------------*/
  5543. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  5544. {
  5545. nullpo_retv(sd);
  5546. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5547. sd->cart.u.items_cart[n].amount < amount)
  5548. return;
  5549. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5550. sd->cart.u.items_cart[n].amount -= amount;
  5551. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5552. if(sd->cart.u.items_cart[n].amount <= 0) {
  5553. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5554. sd->cart_num--;
  5555. }
  5556. if(!type) {
  5557. clif_cart_delitem(sd,n,amount);
  5558. clif_updatestatus(sd,SP_CARTINFO);
  5559. }
  5560. }
  5561. /*==========================================
  5562. * Transfer item from inventory to cart.
  5563. *------------------------------------------*/
  5564. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  5565. {
  5566. nullpo_retv(sd);
  5567. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5568. return;
  5569. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5570. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5571. return;
  5572. if( item_data->equipSwitch ){
  5573. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  5574. return;
  5575. }
  5576. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5577. if (flag == ADDITEM_SUCCESS)
  5578. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5579. else {
  5580. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  5581. clif_additem(sd, idx, amount, 0);
  5582. clif_delitem(sd, idx, amount, 0);
  5583. }
  5584. }
  5585. /*==========================================
  5586. * Get number of item in cart.
  5587. * Return:
  5588. -1 = itemid not found or no amount found
  5589. x = remaining itemid on cart after get
  5590. *------------------------------------------*/
  5591. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  5592. {
  5593. struct item* item_data;
  5594. nullpo_retr(-1, sd);
  5595. item_data = &sd->cart.u.items_cart[idx];
  5596. if( item_data->nameid == 0 || item_data->amount == 0 )
  5597. return -1;
  5598. return item_data->amount - amount;
  5599. }
  5600. /*==========================================
  5601. * Retrieve an item at index idx from cart.
  5602. *------------------------------------------*/
  5603. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  5604. {
  5605. nullpo_retv(sd);
  5606. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5607. return;
  5608. struct item *item_data=&sd->cart.u.items_cart[idx];
  5609. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5610. return;
  5611. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5612. if (flag == ADDITEM_SUCCESS)
  5613. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5614. else {
  5615. clif_cart_delitem(sd, idx, amount);
  5616. clif_additem(sd, idx, amount, flag);
  5617. clif_cart_additem(sd, idx, amount);
  5618. }
  5619. }
  5620. /*==========================================
  5621. * Bound Item Check
  5622. * Type:
  5623. * 1 Account Bound
  5624. * 2 Guild Bound
  5625. * 3 Party Bound
  5626. * 4 Character Bound
  5627. *------------------------------------------*/
  5628. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  5629. {
  5630. int i = 0, j = 0;
  5631. for(i = 0; i < MAX_INVENTORY; i++) {
  5632. 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) {
  5633. idxlist[j] = i;
  5634. j++;
  5635. }
  5636. }
  5637. return j;
  5638. }
  5639. /*==========================================
  5640. * Display item stolen msg to player sd
  5641. *------------------------------------------*/
  5642. int pc_show_steal(struct block_list *bl,va_list ap)
  5643. {
  5644. struct map_session_data *sd;
  5645. t_itemid itemid;
  5646. char output[100];
  5647. sd=va_arg(ap,struct map_session_data *);
  5648. itemid=va_arg(ap,int);
  5649. std::shared_ptr<item_data> id = item_db.find(itemid);
  5650. if(id == nullptr)
  5651. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5652. else
  5653. sprintf(output,"%s stole %s.",sd->status.name,id->ename.c_str());
  5654. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  5655. return 0;
  5656. }
  5657. /**
  5658. * Steal an item from bl (mob).
  5659. * @param sd: Player data
  5660. * @param bl: Object to steal from
  5661. * @param skill_lv: Level of skill used
  5662. * @return True on success or false otherwise
  5663. */
  5664. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5665. {
  5666. int i;
  5667. t_itemid itemid;
  5668. double rate;
  5669. unsigned char flag = 0;
  5670. struct status_data *sd_status, *md_status;
  5671. struct mob_data *md;
  5672. if(!sd || !bl || bl->type!=BL_MOB)
  5673. return false;
  5674. md = (TBL_MOB *)bl;
  5675. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5676. return false;
  5677. sd_status= status_get_status_data(&sd->bl);
  5678. md_status= status_get_status_data(bl);
  5679. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5680. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5681. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5682. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5683. ) { //Can't steal from
  5684. md->state.steal_flag = UCHAR_MAX;
  5685. return false;
  5686. }
  5687. // base skill success chance (percentual)
  5688. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5689. rate += sd->bonus.add_steal_rate;
  5690. if( rate < 1
  5691. #ifdef RENEWAL
  5692. || rnd()%100 >= rate
  5693. #endif
  5694. )
  5695. return false;
  5696. // Try dropping one item, in the order from first to last possible slot.
  5697. // Droprate is affected by the skill success rate.
  5698. for( i = 0; i < MAX_MOB_DROP; i++ )
  5699. if( item_db.exists(md->db->dropitem[i].nameid) && !md->db->dropitem[i].steal_protected && rnd() % 10000 < md->db->dropitem[i].rate
  5700. #ifndef RENEWAL
  5701. * rate/100.
  5702. #endif
  5703. )
  5704. break;
  5705. if( i == MAX_MOB_DROP )
  5706. return false;
  5707. itemid = md->db->dropitem[i].nameid;
  5708. struct item tmp_item = {};
  5709. tmp_item.nameid = itemid;
  5710. tmp_item.amount = 1;
  5711. tmp_item.identify = itemdb_isidentified(itemid);
  5712. if( battle_config.skill_steal_random_options ){
  5713. mob_setdropitem_option( &tmp_item, &md->db->dropitem[i] );
  5714. }
  5715. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5716. //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?
  5717. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5718. if(flag) { //Failed to steal due to overweight
  5719. clif_additem(sd,0,0,flag);
  5720. return false;
  5721. }
  5722. if(battle_config.show_steal_in_same_party)
  5723. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5724. //Logs items, Stolen from mobs [Lupus]
  5725. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5726. //A Rare Steal Global Announce by Lupus
  5727. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5728. struct item_data *i_data;
  5729. char message[128];
  5730. i_data = itemdb_search(itemid);
  5731. 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);
  5732. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5733. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5734. }
  5735. return true;
  5736. }
  5737. /*==========================================
  5738. * Stole zeny from bl (mob)
  5739. * return
  5740. * 0 = fail
  5741. * 1 = success
  5742. *------------------------------------------*/
  5743. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  5744. {
  5745. int rate, target_lv;
  5746. struct mob_data *md;
  5747. if(!sd || !target || target->type != BL_MOB)
  5748. return 0;
  5749. md = (TBL_MOB*)target;
  5750. target_lv = status_get_lv(target);
  5751. if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || md->sc.data[SC_HANDICAPSTATE_FROSTBITE] ||
  5752. md->sc.data[SC_HANDICAPSTATE_SWOONING] || md->sc.data[SC_HANDICAPSTATE_LIGHTNINGSTRIKE] || md->sc.data[SC_HANDICAPSTATE_CRYSTALLIZATION] ||
  5753. status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5754. return 0;
  5755. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5756. if(rnd()%1000 < rate)
  5757. {
  5758. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5759. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5760. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5761. md->state.steal_coin_flag = 1;
  5762. return 1;
  5763. }
  5764. return 0;
  5765. }
  5766. /*==========================================
  5767. * Set's a player position.
  5768. * @param sd
  5769. * @param mapindex
  5770. * @param x
  5771. * @param y
  5772. * @param clrtype
  5773. * @return SETPOS_OK Success
  5774. * SETPOS_MAPINDEX Invalid map index
  5775. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5776. * SETPOS_AUTOTRADE Player is in autotrade state
  5777. *------------------------------------------*/
  5778. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5779. {
  5780. nullpo_retr(SETPOS_OK,sd);
  5781. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5782. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5783. return SETPOS_MAPINDEX;
  5784. }
  5785. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5786. return SETPOS_AUTOTRADE;
  5787. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5788. pc_setstand(sd, true);
  5789. pc_setrestartvalue(sd,1);
  5790. }
  5791. int16 m = map_mapindex2mapid(mapindex);
  5792. struct map_data *mapdata = map_getmapdata(m);
  5793. status_change *sc = status_get_sc(&sd->bl);
  5794. sd->state.changemap = (sd->mapindex != mapindex);
  5795. sd->state.warping = 1;
  5796. sd->state.workinprogress = WIP_DISABLE_NONE;
  5797. sd->state.mail_writing = false;
  5798. sd->state.refineui_open = false;
  5799. if( sd->state.changemap ) { // Misc map-changing settings
  5800. int curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5801. if (curr_map_instance_id != new_map_instance_id) {
  5802. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5803. instance_delusers(curr_map_instance_id);
  5804. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5805. }
  5806. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5807. instance_addusers(new_map_instance_id);
  5808. }
  5809. if (sd->bg_id && mapdata && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5810. bg_team_leave(sd, false, true);
  5811. sd->state.pmap = sd->bl.m;
  5812. if (sc && sc->count) { // Cancel some map related stuff.
  5813. if (sc->cant.warp)
  5814. return SETPOS_MAPINDEX; // You may not get out!
  5815. for (const auto &it : status_db) {
  5816. if (sc->data[it.first]) {
  5817. if (it.second->flag[SCF_REMOVEONMAPWARP])
  5818. status_change_end(&sd->bl, static_cast<sc_type>(it.first));
  5819. if (it.second->flag[SCF_RESTARTONMAPWARP] && it.second->skill_id > 0) {
  5820. status_change_entry *sce = sd->sc.data[it.first];
  5821. if (sce->timer != INVALID_TIMER)
  5822. delete_timer(sce->timer, status_change_timer);
  5823. sce->timer = add_timer(gettick() + skill_get_time(it.second->skill_id, sce->val1), status_change_timer, sd->bl.id, it.first);
  5824. }
  5825. }
  5826. }
  5827. }
  5828. for(int i = 0; i < EQI_MAX; i++ ) {
  5829. if( sd->equip_index[i] >= 0 )
  5830. if( pc_isequip(sd,sd->equip_index[i]) )
  5831. pc_unequipitem(sd,sd->equip_index[i],2);
  5832. }
  5833. if (battle_config.clear_unit_onwarp&BL_PC)
  5834. skill_clear_unitgroup(&sd->bl);
  5835. if( battle_config.loose_ap_on_map && mapdata_flag_vs( mapdata ) ){
  5836. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  5837. }
  5838. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5839. guild_send_dot_remove(sd);
  5840. bg_send_dot_remove(sd);
  5841. if (sd->regen.state.gc)
  5842. sd->regen.state.gc = 0;
  5843. if (mapdata) {
  5844. // make sure vending is allowed here
  5845. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5846. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5847. vending_closevending(sd);
  5848. }
  5849. // make sure buyingstore is allowed here
  5850. if (sd->state.buyingstore && mapdata->flag[MF_NOBUYINGSTORE]) {
  5851. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5852. buyingstore_close(sd);
  5853. }
  5854. }
  5855. channel_pcquit(sd,4); //quit map chan
  5856. // Remove Cloaked NPC if changing to another map
  5857. for (auto it = sd->cloaked_npc.begin(); it != sd->cloaked_npc.end(); ++it) {
  5858. block_list *npc_bl = map_id2bl(*it);
  5859. if (npc_bl && npc_bl->m != m) {
  5860. sd->cloaked_npc.erase(it);
  5861. break;
  5862. }
  5863. }
  5864. }
  5865. if( m < 0 )
  5866. {
  5867. uint32 ip;
  5868. uint16 port;
  5869. struct script_state *st;
  5870. //if can't find any map-servers, just abort setting position.
  5871. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5872. return SETPOS_NO_MAPSERVER;
  5873. if (sd->npc_id){
  5874. npc_event_dequeue(sd,false);
  5875. st = sd->st;
  5876. }else{
  5877. st = nullptr;
  5878. }
  5879. if (sd->bg_id) // Switching map servers, remove from bg
  5880. bg_team_leave(sd, false, true);
  5881. if (sd->state.vending) // Stop vending
  5882. vending_closevending(sd);
  5883. if (sd->state.buyingstore) // Stop buyingstore
  5884. buyingstore_close(sd);
  5885. npc_script_event(sd, NPCE_LOGOUT);
  5886. //remove from map, THEN change x/y coordinates
  5887. unit_remove_map_pc(sd,clrtype);
  5888. sd->mapindex = mapindex;
  5889. sd->bl.x=x;
  5890. sd->bl.y=y;
  5891. pc_clean_skilltree(sd);
  5892. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5893. chrif_changemapserver(sd, ip, (short)port);
  5894. //Free session data from this map server [Kevin]
  5895. unit_free_pc(sd);
  5896. if( st ){
  5897. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5898. st->state = END;
  5899. }
  5900. return SETPOS_OK;
  5901. }
  5902. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5903. {
  5904. 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);
  5905. x = y = 0; // make it random
  5906. }
  5907. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5908. int c=0;
  5909. do {
  5910. x = rnd()%(mapdata->xs-2)+1;
  5911. y = rnd()%(mapdata->ys-2)+1;
  5912. c++;
  5913. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5914. 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));
  5915. return SETPOS_OK; //preventing warp
  5916. //break; //allow warp anyway
  5917. }
  5918. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5919. }
  5920. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5921. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5922. vending_closevending(sd);
  5923. }
  5924. if (sd->state.buyingstore && map_getcell(m, x, y, CELL_CHKNOBUYINGSTORE)) {
  5925. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  5926. buyingstore_close(sd);
  5927. }
  5928. if(sd->bl.prev != NULL){
  5929. unit_remove_map_pc(sd,clrtype);
  5930. clif_changemap(sd,m,x,y); // [MouseJstr]
  5931. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5932. sd->state.rewarp = 1;
  5933. sd->mapindex = mapindex;
  5934. sd->bl.m = m;
  5935. sd->bl.x = sd->ud.to_x = x;
  5936. sd->bl.y = sd->ud.to_y = y;
  5937. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5938. { // Increased guild castle regen [Valaris]
  5939. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  5940. if(gc && gc->guild_id == sd->status.guild_id)
  5941. sd->regen.state.gc = 1;
  5942. }
  5943. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5944. {
  5945. sd->pd->bl.m = m;
  5946. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5947. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5948. sd->pd->ud.dir = sd->ud.dir;
  5949. }
  5950. if( hom_is_active(sd->hd) )
  5951. {
  5952. sd->hd->bl.m = m;
  5953. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5954. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5955. sd->hd->ud.dir = sd->ud.dir;
  5956. }
  5957. if( sd->md )
  5958. {
  5959. sd->md->bl.m = m;
  5960. sd->md->bl.x = sd->md->ud.to_x = x;
  5961. sd->md->bl.y = sd->md->ud.to_y = y;
  5962. sd->md->ud.dir = sd->ud.dir;
  5963. }
  5964. if( sd->ed ) {
  5965. sd->ed->bl.m = m;
  5966. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5967. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5968. sd->ed->ud.dir = sd->ud.dir;
  5969. }
  5970. pc_cell_basilica(sd);
  5971. //check if we gonna be rewarped [lighta]
  5972. if(npc_check_areanpc(1,m,x,y,0)){
  5973. sd->count_rewarp++;
  5974. }
  5975. else
  5976. sd->count_rewarp = 0;
  5977. if (sd->state.vending)
  5978. vending_update(*sd);
  5979. if (sd->state.buyingstore)
  5980. buyingstore_update(*sd);
  5981. return SETPOS_OK;
  5982. }
  5983. /*==========================================
  5984. * Warp player sd to random location on current map.
  5985. * May fail if no walkable cell found (1000 attempts).
  5986. * Return:
  5987. * 0 = Success
  5988. * 1,2,3 = Fail
  5989. *------------------------------------------*/
  5990. char pc_randomwarp(struct map_session_data *sd, clr_type type, bool ignore_mapflag)
  5991. {
  5992. int x,y,i=0;
  5993. nullpo_ret(sd);
  5994. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5995. if (mapdata->flag[MF_NOTELEPORT] && !ignore_mapflag) //Teleport forbidden
  5996. return 3;
  5997. do {
  5998. x = rnd()%(mapdata->xs-2)+1;
  5999. y = rnd()%(mapdata->ys-2)+1;
  6000. } 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);
  6001. if (i < 1000)
  6002. return pc_setpos(sd,mapdata->index,x,y,type);
  6003. return 3;
  6004. }
  6005. /*==========================================
  6006. * Records a memo point at sd's current position
  6007. * pos - entry to replace, (-1: shift oldest entry out)
  6008. *------------------------------------------*/
  6009. bool pc_memo(struct map_session_data* sd, int pos)
  6010. {
  6011. int skill;
  6012. nullpo_ret(sd);
  6013. // check mapflags
  6014. 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) ) {
  6015. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  6016. return false;
  6017. }
  6018. // check inputs
  6019. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  6020. return false; // invalid input
  6021. // check required skill level
  6022. skill = pc_checkskill(sd, AL_WARP);
  6023. if( skill < 1 ) {
  6024. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  6025. return false;
  6026. }
  6027. if( skill < 2 || skill - 2 < pos ) {
  6028. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  6029. return false;
  6030. }
  6031. if( pos == -1 )
  6032. {
  6033. uint8 i;
  6034. // prevent memo-ing the same map multiple times
  6035. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  6036. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  6037. pos = 0;
  6038. }
  6039. if( map_getmapdata(sd->bl.m)->instance_id ) {
  6040. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  6041. return false;
  6042. }
  6043. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  6044. sd->status.memo_point[pos].x = sd->bl.x;
  6045. sd->status.memo_point[pos].y = sd->bl.y;
  6046. clif_skill_memomessage(sd, 0);
  6047. return true;
  6048. }
  6049. //
  6050. // Skills
  6051. //
  6052. /**
  6053. * Get the skill current cooldown for player.
  6054. * (get the db base cooldown for skill + player specific cooldown)
  6055. * @param sd : player pointer
  6056. * @param id : skill id
  6057. * @param lv : skill lv
  6058. * @return player skill cooldown
  6059. */
  6060. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  6061. if (skill_id == SJ_NOVAEXPLOSING) {
  6062. struct status_change *sc = status_get_sc(&sd->bl);
  6063. if (sc && sc->data[SC_DIMENSION])
  6064. return 0;
  6065. }
  6066. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  6067. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  6068. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  6069. for (auto &it : sd->skillcooldown) {
  6070. if (it.id == skill_id) {
  6071. cooldown += it.val;
  6072. break;
  6073. }
  6074. }
  6075. return max(0, cooldown);
  6076. }
  6077. /*==========================================
  6078. * Return player sd skill_lv learned for given skill
  6079. *------------------------------------------*/
  6080. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  6081. {
  6082. uint16 idx = 0;
  6083. if (sd == NULL)
  6084. return 0;
  6085. #ifdef RENEWAL
  6086. if ((idx = skill_get_index(skill_id)) == 0) {
  6087. #else
  6088. if( ( idx = skill_db.get_index( skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__ ) ) == 0 ){
  6089. if( skill_id >= RK_ENCHANTBLADE ){
  6090. // Silently fail for now -> future update planned
  6091. return 0;
  6092. }
  6093. #endif
  6094. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  6095. return 0;
  6096. }
  6097. if (SKILL_CHK_GUILD(skill_id) ) {
  6098. struct guild *g;
  6099. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  6100. return guild_checkskill(g,skill_id);
  6101. return 0;
  6102. }
  6103. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  6104. }
  6105. /**
  6106. * Returns the flag of the given skill (when learned).
  6107. * @param sd: Player data
  6108. * @param skill_id: Skill to lookup
  6109. * @return Skill flag type
  6110. */
  6111. e_skill_flag pc_checkskill_flag(map_session_data &sd, uint16 skill_id) {
  6112. uint16 idx;
  6113. #ifdef RENEWAL
  6114. if ((idx = skill_get_index(skill_id)) == 0) {
  6115. #else
  6116. if ((idx = skill_db.get_index(skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__)) == 0) {
  6117. if (skill_id >= RK_ENCHANTBLADE) {
  6118. // Silently fail for now -> future update planned
  6119. return SKILL_FLAG_NONE;
  6120. }
  6121. #endif
  6122. ShowError("pc_checkskill_flag: Invalid skill id %d (char_id=%d).\n", skill_id, sd.status.char_id);
  6123. return SKILL_FLAG_NONE;
  6124. }
  6125. 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;
  6126. }
  6127. /**
  6128. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  6129. * @param sd: Player data
  6130. * @param type: Summoner Power Type
  6131. * @return Skill points invested
  6132. */
  6133. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  6134. if (sd == nullptr)
  6135. return 0;
  6136. uint8 count = 0;
  6137. switch (type) {
  6138. case SUMMONER_POWER_SEA:
  6139. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  6140. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  6141. break;
  6142. case SUMMONER_POWER_LAND:
  6143. 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) +
  6144. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  6145. break;
  6146. case SUMMONER_POWER_LIFE:
  6147. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  6148. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  6149. break;
  6150. }
  6151. return count;
  6152. }
  6153. /**
  6154. * Checks for Imperial Guard's passive skills.
  6155. * @param sd: Player data
  6156. * @param flag:
  6157. * Flag&1 = IG_SHIELD_MASTERY
  6158. * Flag&2 = IG_SPEAR_SWORD_M
  6159. */
  6160. uint8 pc_checkskill_imperial_guard(struct map_session_data *sd, short flag)
  6161. {
  6162. nullpo_retr(0, sd);
  6163. uint8 count = 0;
  6164. if (flag&1 && sd->status.shield > 0)
  6165. count += pc_checkskill(sd, IG_SHIELD_MASTERY);
  6166. if (flag&2 && (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_1HSPEAR || sd->status.weapon == W_2HSPEAR))
  6167. count += pc_checkskill(sd, IG_SPEAR_SWORD_M);
  6168. return count;
  6169. }
  6170. /**
  6171. * Check if we still have the correct weapon to continue the skill (actually status)
  6172. * If not ending it
  6173. * @param sd
  6174. * @return 0:error, 1:check done
  6175. */
  6176. static void pc_checkallowskill(struct map_session_data *sd)
  6177. {
  6178. nullpo_retv(sd);
  6179. if(!sd->sc.count)
  6180. return;
  6181. for (const auto &it : status_db) {
  6182. sc_type status = it.second->type;
  6183. std::bitset<SCF_MAX> flag = it.second->flag;
  6184. if (flag[SCF_REQUIREWEAPON]) { // Skills requiring specific weapon types
  6185. if (status == SC_DANCING && !battle_config.dancing_weaponswitch_fix)
  6186. continue;
  6187. if (sd->sc.data[status] && !pc_check_weapontype(sd, skill_get_weapontype(it.second->skill_id)))
  6188. status_change_end(&sd->bl, status);
  6189. }
  6190. if (flag[SCF_REQUIRESHIELD]) { // Skills requiring a shield
  6191. if (sd->sc.data[status] && sd->status.shield <= 0)
  6192. status_change_end(&sd->bl, status);
  6193. }
  6194. }
  6195. }
  6196. /*==========================================
  6197. * Return equipped index of item on player sd at pos
  6198. * Return
  6199. * -1 : Nothing equipped
  6200. * idx : (this index could be used in inventory to found item_data)
  6201. *------------------------------------------*/
  6202. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  6203. {
  6204. uint8 i;
  6205. nullpo_retr(-1, sd);
  6206. for(i=0;i<EQI_MAX;i++){
  6207. if(pos & equip_bitmask[i]){
  6208. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  6209. // Check all if any match is found
  6210. continue;
  6211. }
  6212. return sd->equip_index[i];
  6213. }
  6214. }
  6215. return -1;
  6216. }
  6217. /*==========================================
  6218. * Check if sd has nameid equipped somewhere
  6219. * @sd : the player session
  6220. * @nameid : id of the item to check
  6221. * @min : : see pc.hpp enum equip_index from ? to @max
  6222. * @max : see pc.hpp enum equip_index for @min to ?
  6223. * -return true,false
  6224. *------------------------------------------*/
  6225. bool pc_checkequip2(struct map_session_data *sd, t_itemid nameid, int min, int max)
  6226. {
  6227. int i;
  6228. for(i = min; i < max; i++) {
  6229. if(equip_bitmask[i]) {
  6230. int idx = sd->equip_index[i];
  6231. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  6232. return true;
  6233. }
  6234. }
  6235. return false;
  6236. }
  6237. /*==========================================
  6238. * Convert's from the client's lame Job ID system
  6239. * to the map server's 'makes sense' system. [Skotlex]
  6240. *------------------------------------------*/
  6241. uint64 pc_jobid2mapid(unsigned short b_class)
  6242. {
  6243. switch(b_class)
  6244. {
  6245. //Novice And 1-1 Jobs
  6246. case JOB_NOVICE: return MAPID_NOVICE;
  6247. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  6248. case JOB_MAGE: return MAPID_MAGE;
  6249. case JOB_ARCHER: return MAPID_ARCHER;
  6250. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  6251. case JOB_MERCHANT: return MAPID_MERCHANT;
  6252. case JOB_THIEF: return MAPID_THIEF;
  6253. case JOB_TAEKWON: return MAPID_TAEKWON;
  6254. case JOB_WEDDING: return MAPID_WEDDING;
  6255. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  6256. case JOB_NINJA: return MAPID_NINJA;
  6257. case JOB_XMAS: return MAPID_XMAS;
  6258. case JOB_SUMMER: return MAPID_SUMMER;
  6259. case JOB_HANBOK: return MAPID_HANBOK;
  6260. case JOB_GANGSI: return MAPID_GANGSI;
  6261. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  6262. case JOB_SUMMER2: return MAPID_SUMMER2;
  6263. //2-1 Jobs
  6264. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  6265. case JOB_KNIGHT: return MAPID_KNIGHT;
  6266. case JOB_WIZARD: return MAPID_WIZARD;
  6267. case JOB_HUNTER: return MAPID_HUNTER;
  6268. case JOB_PRIEST: return MAPID_PRIEST;
  6269. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  6270. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  6271. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  6272. case JOB_KAGEROU:
  6273. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  6274. case JOB_REBELLION: return MAPID_REBELLION;
  6275. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  6276. //2-2 Jobs
  6277. case JOB_CRUSADER: return MAPID_CRUSADER;
  6278. case JOB_SAGE: return MAPID_SAGE;
  6279. case JOB_BARD:
  6280. case JOB_DANCER: return MAPID_BARDDANCER;
  6281. case JOB_MONK: return MAPID_MONK;
  6282. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  6283. case JOB_ROGUE: return MAPID_ROGUE;
  6284. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  6285. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  6286. //Trans Novice And Trans 1-1 Jobs
  6287. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  6288. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  6289. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  6290. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  6291. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  6292. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  6293. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  6294. //Trans 2-1 Jobs
  6295. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  6296. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  6297. case JOB_SNIPER: return MAPID_SNIPER;
  6298. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  6299. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  6300. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  6301. //Trans 2-2 Jobs
  6302. case JOB_PALADIN: return MAPID_PALADIN;
  6303. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  6304. case JOB_CLOWN:
  6305. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  6306. case JOB_CHAMPION: return MAPID_CHAMPION;
  6307. case JOB_CREATOR: return MAPID_CREATOR;
  6308. case JOB_STALKER: return MAPID_STALKER;
  6309. //Baby Novice And Baby 1-1 Jobs
  6310. case JOB_BABY: return MAPID_BABY;
  6311. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  6312. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  6313. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  6314. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  6315. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  6316. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  6317. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  6318. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  6319. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  6320. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  6321. //Baby 2-1 Jobs
  6322. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  6323. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  6324. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  6325. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  6326. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  6327. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  6328. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  6329. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  6330. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  6331. case JOB_BABY_KAGEROU:
  6332. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  6333. //Baby 2-2 Jobs
  6334. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  6335. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  6336. case JOB_BABY_BARD:
  6337. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  6338. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  6339. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  6340. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  6341. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  6342. //3-1 Jobs
  6343. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  6344. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  6345. case JOB_WARLOCK: return MAPID_WARLOCK;
  6346. case JOB_RANGER: return MAPID_RANGER;
  6347. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  6348. case JOB_MECHANIC: return MAPID_MECHANIC;
  6349. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  6350. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  6351. //3-2 Jobs
  6352. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  6353. case JOB_SORCERER: return MAPID_SORCERER;
  6354. case JOB_MINSTREL:
  6355. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  6356. case JOB_SURA: return MAPID_SURA;
  6357. case JOB_GENETIC: return MAPID_GENETIC;
  6358. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  6359. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  6360. //Trans 3-1 Jobs
  6361. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  6362. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  6363. case JOB_RANGER_T: return MAPID_RANGER_T;
  6364. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  6365. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  6366. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  6367. //Trans 3-2 Jobs
  6368. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  6369. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  6370. case JOB_MINSTREL_T:
  6371. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  6372. case JOB_SURA_T: return MAPID_SURA_T;
  6373. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  6374. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  6375. //Baby 3-1 Jobs
  6376. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  6377. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  6378. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  6379. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  6380. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  6381. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  6382. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  6383. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  6384. //Baby 3-2 Jobs
  6385. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  6386. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  6387. case JOB_BABY_MINSTREL:
  6388. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  6389. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  6390. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  6391. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  6392. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  6393. //Doram Jobs
  6394. case JOB_SUMMONER: return MAPID_SUMMONER;
  6395. case JOB_SPIRIT_HANDLER: return MAPID_SPIRIT_HANDLER;
  6396. //4-1 Jobs
  6397. case JOB_HYPER_NOVICE: return MAPID_HYPER_NOVICE;
  6398. case JOB_DRAGON_KNIGHT: return MAPID_DRAGON_KNIGHT;
  6399. case JOB_ARCH_MAGE: return MAPID_ARCH_MAGE;
  6400. case JOB_WINDHAWK: return MAPID_WINDHAWK;
  6401. case JOB_CARDINAL: return MAPID_CARDINAL;
  6402. case JOB_MEISTER: return MAPID_MEISTER;
  6403. case JOB_SHADOW_CROSS: return MAPID_SHADOW_CROSS;
  6404. case JOB_SKY_EMPEROR: return MAPID_SKY_EMPEROR;
  6405. case JOB_NIGHT_WATCH: return MAPID_NIGHT_WATCH;
  6406. case JOB_SHINKIRO:
  6407. case JOB_SHIRANUI: return MAPID_SHINKIRO_SHIRANUI;
  6408. //4-2 Jobs
  6409. case JOB_IMPERIAL_GUARD: return MAPID_IMPERIAL_GUARD;
  6410. case JOB_ELEMENTAL_MASTER: return MAPID_ELEMENTAL_MASTER;
  6411. case JOB_INQUISITOR: return MAPID_INQUISITOR;
  6412. case JOB_TROUBADOUR:
  6413. case JOB_TROUVERE: return MAPID_TROUBADOURTROUVERE;
  6414. case JOB_BIOLO: return MAPID_BIOLO;
  6415. case JOB_ABYSS_CHASER: return MAPID_ABYSS_CHASER;
  6416. case JOB_SOUL_ASCETIC: return MAPID_SOUL_ASCETIC;
  6417. //Unknown
  6418. default:
  6419. return -1;
  6420. }
  6421. }
  6422. //Reverts the map-style class id to the client-style one.
  6423. int pc_mapid2jobid(uint64 class_, int sex)
  6424. {
  6425. switch(class_) {
  6426. //Novice And 1-1 Jobs
  6427. case MAPID_NOVICE: return JOB_NOVICE;
  6428. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  6429. case MAPID_MAGE: return JOB_MAGE;
  6430. case MAPID_ARCHER: return JOB_ARCHER;
  6431. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  6432. case MAPID_MERCHANT: return JOB_MERCHANT;
  6433. case MAPID_THIEF: return JOB_THIEF;
  6434. case MAPID_TAEKWON: return JOB_TAEKWON;
  6435. case MAPID_WEDDING: return JOB_WEDDING;
  6436. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  6437. case MAPID_NINJA: return JOB_NINJA;
  6438. case MAPID_XMAS: return JOB_XMAS;
  6439. case MAPID_SUMMER: return JOB_SUMMER;
  6440. case MAPID_HANBOK: return JOB_HANBOK;
  6441. case MAPID_GANGSI: return JOB_GANGSI;
  6442. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  6443. case MAPID_SUMMER2: return JOB_SUMMER2;
  6444. //2-1 Jobs
  6445. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  6446. case MAPID_KNIGHT: return JOB_KNIGHT;
  6447. case MAPID_WIZARD: return JOB_WIZARD;
  6448. case MAPID_HUNTER: return JOB_HUNTER;
  6449. case MAPID_PRIEST: return JOB_PRIEST;
  6450. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  6451. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  6452. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  6453. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  6454. case MAPID_REBELLION: return JOB_REBELLION;
  6455. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  6456. //2-2 Jobs
  6457. case MAPID_CRUSADER: return JOB_CRUSADER;
  6458. case MAPID_SAGE: return JOB_SAGE;
  6459. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  6460. case MAPID_MONK: return JOB_MONK;
  6461. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  6462. case MAPID_ROGUE: return JOB_ROGUE;
  6463. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  6464. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  6465. //Trans Novice And Trans 2-1 Jobs
  6466. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  6467. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  6468. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  6469. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  6470. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  6471. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  6472. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  6473. //Trans 2-1 Jobs
  6474. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  6475. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  6476. case MAPID_SNIPER: return JOB_SNIPER;
  6477. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  6478. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  6479. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  6480. //Trans 2-2 Jobs
  6481. case MAPID_PALADIN: return JOB_PALADIN;
  6482. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  6483. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  6484. case MAPID_CHAMPION: return JOB_CHAMPION;
  6485. case MAPID_CREATOR: return JOB_CREATOR;
  6486. case MAPID_STALKER: return JOB_STALKER;
  6487. //Baby Novice And Baby 1-1 Jobs
  6488. case MAPID_BABY: return JOB_BABY;
  6489. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  6490. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  6491. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  6492. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  6493. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  6494. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  6495. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  6496. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  6497. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  6498. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  6499. //Baby 2-1 Jobs
  6500. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  6501. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  6502. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  6503. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  6504. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  6505. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  6506. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  6507. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  6508. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  6509. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  6510. //Baby 2-2 Jobs
  6511. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  6512. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  6513. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  6514. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  6515. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  6516. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  6517. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  6518. //3-1 Jobs
  6519. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  6520. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  6521. case MAPID_WARLOCK: return JOB_WARLOCK;
  6522. case MAPID_RANGER: return JOB_RANGER;
  6523. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  6524. case MAPID_MECHANIC: return JOB_MECHANIC;
  6525. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  6526. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  6527. //3-2 Jobs
  6528. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  6529. case MAPID_SORCERER: return JOB_SORCERER;
  6530. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  6531. case MAPID_SURA: return JOB_SURA;
  6532. case MAPID_GENETIC: return JOB_GENETIC;
  6533. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  6534. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  6535. //Trans 3-1 Jobs
  6536. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  6537. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  6538. case MAPID_RANGER_T: return JOB_RANGER_T;
  6539. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  6540. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  6541. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  6542. //Trans 3-2 Jobs
  6543. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  6544. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  6545. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  6546. case MAPID_SURA_T: return JOB_SURA_T;
  6547. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  6548. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  6549. //Baby 3-1 Jobs
  6550. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  6551. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  6552. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  6553. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  6554. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  6555. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  6556. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  6557. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  6558. //Baby 3-2 Jobs
  6559. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  6560. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  6561. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  6562. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  6563. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  6564. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  6565. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  6566. //Doram Jobs
  6567. case MAPID_SUMMONER: return JOB_SUMMONER;
  6568. case MAPID_SPIRIT_HANDLER: return JOB_SPIRIT_HANDLER;
  6569. //4-1 Jobs
  6570. case MAPID_HYPER_NOVICE: return JOB_HYPER_NOVICE;
  6571. case MAPID_DRAGON_KNIGHT: return JOB_DRAGON_KNIGHT;
  6572. case MAPID_ARCH_MAGE: return JOB_ARCH_MAGE;
  6573. case MAPID_WINDHAWK: return JOB_WINDHAWK;
  6574. case MAPID_CARDINAL: return JOB_CARDINAL;
  6575. case MAPID_MEISTER: return JOB_MEISTER;
  6576. case MAPID_SHADOW_CROSS: return JOB_SHADOW_CROSS;
  6577. case MAPID_SKY_EMPEROR: return JOB_SKY_EMPEROR;
  6578. case MAPID_NIGHT_WATCH: return JOB_NIGHT_WATCH;
  6579. case MAPID_SHINKIRO_SHIRANUI: return sex?JOB_SHINKIRO:JOB_SHIRANUI;
  6580. //4-2 Jobs
  6581. case MAPID_IMPERIAL_GUARD: return JOB_IMPERIAL_GUARD;
  6582. case MAPID_ELEMENTAL_MASTER: return JOB_ELEMENTAL_MASTER;
  6583. case MAPID_INQUISITOR: return JOB_INQUISITOR;
  6584. case MAPID_TROUBADOURTROUVERE: return sex?JOB_TROUBADOUR:JOB_TROUVERE;
  6585. case MAPID_BIOLO: return JOB_BIOLO;
  6586. case MAPID_ABYSS_CHASER: return JOB_ABYSS_CHASER;
  6587. case MAPID_SOUL_ASCETIC: return JOB_SOUL_ASCETIC;
  6588. //Unknown
  6589. default:
  6590. return -1;
  6591. }
  6592. }
  6593. /*====================================================
  6594. * This function return the name of the job (by [Yor])
  6595. *----------------------------------------------------*/
  6596. const char* job_name(int class_)
  6597. {
  6598. switch (class_) {
  6599. case JOB_NOVICE:
  6600. case JOB_SWORDMAN:
  6601. case JOB_MAGE:
  6602. case JOB_ARCHER:
  6603. case JOB_ACOLYTE:
  6604. case JOB_MERCHANT:
  6605. case JOB_THIEF:
  6606. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  6607. case JOB_KNIGHT:
  6608. case JOB_PRIEST:
  6609. case JOB_WIZARD:
  6610. case JOB_BLACKSMITH:
  6611. case JOB_HUNTER:
  6612. case JOB_ASSASSIN:
  6613. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  6614. case JOB_KNIGHT2:
  6615. return msg_txt(NULL,557);
  6616. case JOB_CRUSADER:
  6617. case JOB_MONK:
  6618. case JOB_SAGE:
  6619. case JOB_ROGUE:
  6620. case JOB_ALCHEMIST:
  6621. case JOB_BARD:
  6622. case JOB_DANCER:
  6623. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  6624. case JOB_CRUSADER2:
  6625. return msg_txt(NULL,563);
  6626. case JOB_WEDDING:
  6627. case JOB_SUPER_NOVICE:
  6628. case JOB_GUNSLINGER:
  6629. case JOB_NINJA:
  6630. case JOB_XMAS:
  6631. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  6632. case JOB_SUMMER:
  6633. case JOB_SUMMER2:
  6634. return msg_txt(NULL,621);
  6635. case JOB_HANBOK:
  6636. return msg_txt(NULL,694);
  6637. case JOB_OKTOBERFEST:
  6638. return msg_txt(NULL,696);
  6639. case JOB_NOVICE_HIGH:
  6640. case JOB_SWORDMAN_HIGH:
  6641. case JOB_MAGE_HIGH:
  6642. case JOB_ARCHER_HIGH:
  6643. case JOB_ACOLYTE_HIGH:
  6644. case JOB_MERCHANT_HIGH:
  6645. case JOB_THIEF_HIGH:
  6646. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  6647. case JOB_LORD_KNIGHT:
  6648. case JOB_HIGH_PRIEST:
  6649. case JOB_HIGH_WIZARD:
  6650. case JOB_WHITESMITH:
  6651. case JOB_SNIPER:
  6652. case JOB_ASSASSIN_CROSS:
  6653. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  6654. case JOB_LORD_KNIGHT2:
  6655. return msg_txt(NULL,582);
  6656. case JOB_PALADIN:
  6657. case JOB_CHAMPION:
  6658. case JOB_PROFESSOR:
  6659. case JOB_STALKER:
  6660. case JOB_CREATOR:
  6661. case JOB_CLOWN:
  6662. case JOB_GYPSY:
  6663. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  6664. case JOB_PALADIN2:
  6665. return msg_txt(NULL,588);
  6666. case JOB_BABY:
  6667. case JOB_BABY_SWORDMAN:
  6668. case JOB_BABY_MAGE:
  6669. case JOB_BABY_ARCHER:
  6670. case JOB_BABY_ACOLYTE:
  6671. case JOB_BABY_MERCHANT:
  6672. case JOB_BABY_THIEF:
  6673. return msg_txt(NULL,595 - JOB_BABY + class_);
  6674. case JOB_BABY_KNIGHT:
  6675. case JOB_BABY_PRIEST:
  6676. case JOB_BABY_WIZARD:
  6677. case JOB_BABY_BLACKSMITH:
  6678. case JOB_BABY_HUNTER:
  6679. case JOB_BABY_ASSASSIN:
  6680. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  6681. case JOB_BABY_KNIGHT2:
  6682. return msg_txt(NULL,602);
  6683. case JOB_BABY_CRUSADER:
  6684. case JOB_BABY_MONK:
  6685. case JOB_BABY_SAGE:
  6686. case JOB_BABY_ROGUE:
  6687. case JOB_BABY_ALCHEMIST:
  6688. case JOB_BABY_BARD:
  6689. case JOB_BABY_DANCER:
  6690. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  6691. case JOB_BABY_CRUSADER2:
  6692. return msg_txt(NULL,608);
  6693. case JOB_SUPER_BABY:
  6694. return msg_txt(NULL,615);
  6695. case JOB_TAEKWON:
  6696. return msg_txt(NULL,616);
  6697. case JOB_STAR_GLADIATOR:
  6698. case JOB_STAR_GLADIATOR2:
  6699. return msg_txt(NULL,617);
  6700. case JOB_SOUL_LINKER:
  6701. return msg_txt(NULL,618);
  6702. case JOB_GANGSI:
  6703. case JOB_DEATH_KNIGHT:
  6704. case JOB_DARK_COLLECTOR:
  6705. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  6706. case JOB_RUNE_KNIGHT:
  6707. case JOB_WARLOCK:
  6708. case JOB_RANGER:
  6709. case JOB_ARCH_BISHOP:
  6710. case JOB_MECHANIC:
  6711. case JOB_GUILLOTINE_CROSS:
  6712. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  6713. case JOB_RUNE_KNIGHT_T:
  6714. case JOB_WARLOCK_T:
  6715. case JOB_RANGER_T:
  6716. case JOB_ARCH_BISHOP_T:
  6717. case JOB_MECHANIC_T:
  6718. case JOB_GUILLOTINE_CROSS_T:
  6719. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  6720. case JOB_ROYAL_GUARD:
  6721. case JOB_SORCERER:
  6722. case JOB_MINSTREL:
  6723. case JOB_WANDERER:
  6724. case JOB_SURA:
  6725. case JOB_GENETIC:
  6726. case JOB_SHADOW_CHASER:
  6727. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  6728. case JOB_ROYAL_GUARD_T:
  6729. case JOB_SORCERER_T:
  6730. case JOB_MINSTREL_T:
  6731. case JOB_WANDERER_T:
  6732. case JOB_SURA_T:
  6733. case JOB_GENETIC_T:
  6734. case JOB_SHADOW_CHASER_T:
  6735. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  6736. case JOB_RUNE_KNIGHT2:
  6737. case JOB_RUNE_KNIGHT_T2:
  6738. return msg_txt(NULL,625);
  6739. case JOB_ROYAL_GUARD2:
  6740. case JOB_ROYAL_GUARD_T2:
  6741. return msg_txt(NULL,631);
  6742. case JOB_RANGER2:
  6743. case JOB_RANGER_T2:
  6744. return msg_txt(NULL,627);
  6745. case JOB_MECHANIC2:
  6746. case JOB_MECHANIC_T2:
  6747. return msg_txt(NULL,629);
  6748. case JOB_BABY_RUNE_KNIGHT:
  6749. case JOB_BABY_WARLOCK:
  6750. case JOB_BABY_RANGER:
  6751. case JOB_BABY_ARCH_BISHOP:
  6752. case JOB_BABY_MECHANIC:
  6753. case JOB_BABY_GUILLOTINE_CROSS:
  6754. case JOB_BABY_ROYAL_GUARD:
  6755. case JOB_BABY_SORCERER:
  6756. case JOB_BABY_MINSTREL:
  6757. case JOB_BABY_WANDERER:
  6758. case JOB_BABY_SURA:
  6759. case JOB_BABY_GENETIC:
  6760. case JOB_BABY_SHADOW_CHASER:
  6761. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6762. case JOB_BABY_RUNE_KNIGHT2:
  6763. return msg_txt(NULL,638);
  6764. case JOB_BABY_ROYAL_GUARD2:
  6765. return msg_txt(NULL,644);
  6766. case JOB_BABY_RANGER2:
  6767. return msg_txt(NULL,640);
  6768. case JOB_BABY_MECHANIC2:
  6769. return msg_txt(NULL,642);
  6770. case JOB_SUPER_NOVICE_E:
  6771. case JOB_SUPER_BABY_E:
  6772. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  6773. case JOB_KAGEROU:
  6774. case JOB_OBORO:
  6775. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  6776. case JOB_REBELLION:
  6777. return msg_txt(NULL,695);
  6778. case JOB_SUMMONER:
  6779. return msg_txt(NULL,697);
  6780. case JOB_BABY_SUMMONER:
  6781. return msg_txt(NULL,698);
  6782. case JOB_BABY_NINJA:
  6783. return msg_txt(NULL,699);
  6784. case JOB_BABY_KAGEROU:
  6785. case JOB_BABY_OBORO:
  6786. case JOB_BABY_TAEKWON:
  6787. case JOB_BABY_STAR_GLADIATOR:
  6788. case JOB_BABY_SOUL_LINKER:
  6789. case JOB_BABY_GUNSLINGER:
  6790. case JOB_BABY_REBELLION:
  6791. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  6792. case JOB_BABY_STAR_GLADIATOR2:
  6793. return msg_txt(NULL,756);
  6794. case JOB_STAR_EMPEROR:
  6795. case JOB_SOUL_REAPER:
  6796. case JOB_BABY_STAR_EMPEROR:
  6797. case JOB_BABY_SOUL_REAPER:
  6798. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  6799. case JOB_STAR_EMPEROR2:
  6800. return msg_txt(NULL,782);
  6801. case JOB_BABY_STAR_EMPEROR2:
  6802. return msg_txt(NULL,784);
  6803. case JOB_DRAGON_KNIGHT:
  6804. case JOB_MEISTER:
  6805. case JOB_SHADOW_CROSS:
  6806. case JOB_ARCH_MAGE:
  6807. case JOB_CARDINAL:
  6808. case JOB_WINDHAWK:
  6809. case JOB_IMPERIAL_GUARD:
  6810. case JOB_BIOLO:
  6811. case JOB_ABYSS_CHASER:
  6812. case JOB_ELEMENTAL_MASTER:
  6813. case JOB_INQUISITOR:
  6814. case JOB_TROUBADOUR:
  6815. case JOB_TROUVERE:
  6816. return msg_txt( nullptr, 800 - JOB_DRAGON_KNIGHT + class_ );
  6817. case JOB_WINDHAWK2:
  6818. return msg_txt( nullptr, 805);
  6819. case JOB_MEISTER2:
  6820. return msg_txt( nullptr, 801 );
  6821. case JOB_DRAGON_KNIGHT2:
  6822. return msg_txt( nullptr, 800 );
  6823. case JOB_IMPERIAL_GUARD2:
  6824. return msg_txt( nullptr, 806 );
  6825. case JOB_SKY_EMPEROR:
  6826. case JOB_SOUL_ASCETIC:
  6827. case JOB_SHINKIRO:
  6828. case JOB_SHIRANUI:
  6829. case JOB_NIGHT_WATCH:
  6830. case JOB_HYPER_NOVICE:
  6831. case JOB_SPIRIT_HANDLER:
  6832. return msg_txt( nullptr, 813 - JOB_SKY_EMPEROR + class_ );
  6833. case JOB_SKY_EMPEROR2:
  6834. return msg_txt( nullptr, 813 );
  6835. default:
  6836. return msg_txt(NULL,655);
  6837. }
  6838. }
  6839. /*====================================================
  6840. * Timered function to make id follow a target.
  6841. * @id = bl.id (player only atm)
  6842. * target is define in sd->followtarget (bl.id)
  6843. * used by pc_follow
  6844. *----------------------------------------------------*/
  6845. TIMER_FUNC(pc_follow_timer){
  6846. struct map_session_data *sd;
  6847. struct block_list *tbl;
  6848. sd = map_id2sd(id);
  6849. nullpo_ret(sd);
  6850. if (sd->followtimer != tid){
  6851. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6852. sd->followtimer = INVALID_TIMER;
  6853. return 0;
  6854. }
  6855. sd->followtimer = INVALID_TIMER;
  6856. tbl = map_id2bl(sd->followtarget);
  6857. if (tbl == NULL || pc_isdead(sd))
  6858. {
  6859. pc_stop_following(sd);
  6860. return 0;
  6861. }
  6862. // either player or target is currently detached from map blocks (could be teleporting),
  6863. // but still connected to this map, so we'll just increment the timer and check back later
  6864. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  6865. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6866. {
  6867. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  6868. if (!check_distance_bl(&sd->bl, tbl, 5))
  6869. unit_walktobl(&sd->bl, tbl, 5, 0);
  6870. } else
  6871. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  6872. }
  6873. sd->followtimer = add_timer(
  6874. tick + 1000, // increase time a bit to loosen up map's load
  6875. pc_follow_timer, sd->bl.id, 0);
  6876. return 0;
  6877. }
  6878. int pc_stop_following (struct map_session_data *sd)
  6879. {
  6880. nullpo_ret(sd);
  6881. if (sd->followtimer != INVALID_TIMER) {
  6882. delete_timer(sd->followtimer,pc_follow_timer);
  6883. sd->followtimer = INVALID_TIMER;
  6884. }
  6885. sd->followtarget = -1;
  6886. sd->ud.target_to = 0;
  6887. unit_stop_walking(&sd->bl, 1);
  6888. return 0;
  6889. }
  6890. int pc_follow(struct map_session_data *sd,int target_id)
  6891. {
  6892. struct block_list *bl = map_id2bl(target_id);
  6893. if (bl == NULL /*|| bl->type != BL_PC*/)
  6894. return 1;
  6895. if (sd->followtimer != INVALID_TIMER)
  6896. pc_stop_following(sd);
  6897. sd->followtarget = target_id;
  6898. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6899. return 0;
  6900. }
  6901. int pc_checkbaselevelup(struct map_session_data *sd) {
  6902. t_exp next = pc_nextbaseexp(sd);
  6903. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  6904. return 0;
  6905. uint32 base_level = sd->status.base_level;
  6906. do {
  6907. sd->status.base_exp -= next;
  6908. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6909. 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 )
  6910. sd->status.base_exp = next-1;
  6911. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level);
  6912. sd->status.trait_point += statpoint_db.pc_gets_trait_point(sd->status.base_level);
  6913. sd->status.base_level++;
  6914. if( pc_is_maxbaselv(sd) ){
  6915. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  6916. break;
  6917. }
  6918. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  6919. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  6920. status_calc_pet(sd->pd,SCO_NONE);
  6921. clif_updatestatus(sd,SP_STATUSPOINT);
  6922. clif_updatestatus(sd,SP_TRAITPOINT);
  6923. clif_updatestatus(sd,SP_BASELEVEL);
  6924. clif_updatestatus(sd,SP_BASEEXP);
  6925. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6926. status_calc_pc(sd,SCO_FORCE);
  6927. status_percent_heal(&sd->bl,100,100);
  6928. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6929. for (const auto &status : status_db) {
  6930. if (status.second->flag[SCF_SUPERNOVICEANGEL])
  6931. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 1, skill_get_time(status.second->skill_id, 1));
  6932. }
  6933. if (sd->state.snovice_dead_flag)
  6934. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6935. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6936. for (const auto &status : status_db) {
  6937. if (status.second->flag[SCF_TAEKWONANGEL])
  6938. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 10, 600000);
  6939. }
  6940. }
  6941. clif_misceffect(&sd->bl,0);
  6942. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6943. if(sd->status.party_id)
  6944. party_send_levelup(sd);
  6945. pc_baselevelchanged(sd);
  6946. for (; base_level <= sd->status.base_level; base_level++) {
  6947. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  6948. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  6949. }
  6950. return 1;
  6951. }
  6952. void pc_baselevelchanged(struct map_session_data *sd) {
  6953. uint8 i;
  6954. for( i = 0; i < EQI_MAX; i++ ) {
  6955. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  6956. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6957. pc_unequipitem(sd, sd->equip_index[i], 3);
  6958. }
  6959. }
  6960. pc_show_questinfo(sd);
  6961. }
  6962. int pc_checkjoblevelup(struct map_session_data *sd)
  6963. {
  6964. t_exp next = pc_nextjobexp(sd);
  6965. nullpo_ret(sd);
  6966. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6967. return 0;
  6968. uint32 job_level = sd->status.job_level;
  6969. do {
  6970. sd->status.job_exp -= next;
  6971. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6972. 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 )
  6973. sd->status.job_exp = next-1;
  6974. sd->status.job_level ++;
  6975. sd->status.skill_point ++;
  6976. if( pc_is_maxjoblv(sd) ){
  6977. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6978. break;
  6979. }
  6980. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6981. clif_updatestatus(sd,SP_JOBLEVEL);
  6982. clif_updatestatus(sd,SP_JOBEXP);
  6983. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6984. clif_updatestatus(sd,SP_SKILLPOINT);
  6985. status_calc_pc(sd,SCO_FORCE);
  6986. clif_misceffect(&sd->bl,1);
  6987. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6988. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6989. npc_script_event(sd, NPCE_JOBLVUP);
  6990. for (; job_level <= sd->status.job_level; job_level++)
  6991. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  6992. pc_show_questinfo(sd);
  6993. return 1;
  6994. }
  6995. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6996. * @param sd Player
  6997. * @param base_exp Base EXP before peronal bonuses
  6998. * @param job_exp Job EXP before peronal bonuses
  6999. * @param src Block list that affecting the exp calculation
  7000. */
  7001. static void pc_calcexp(struct map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  7002. {
  7003. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  7004. if (src) {
  7005. struct status_data *status = status_get_status_data(src);
  7006. if( sd->indexed_bonus.expaddrace[status->race] )
  7007. bonus += sd->indexed_bonus.expaddrace[status->race];
  7008. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  7009. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  7010. if( sd->indexed_bonus.expaddclass[status->class_] )
  7011. bonus += sd->indexed_bonus.expaddclass[status->class_];
  7012. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  7013. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  7014. if (battle_config.pk_mode &&
  7015. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  7016. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  7017. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  7018. vip_bonus_base = battle_config.vip_base_exp_increase;
  7019. vip_bonus_job = battle_config.vip_job_exp_increase;
  7020. }
  7021. }
  7022. // Give EXPBOOST for quests even if src is NULL.
  7023. if (sd->sc.data[SC_EXPBOOST]) {
  7024. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  7025. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  7026. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  7027. }
  7028. if (*base_exp) {
  7029. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  7030. *base_exp = cap_value(exp, 1, MAX_EXP);
  7031. }
  7032. // Give JEXPBOOST for quests even if src is NULL.
  7033. if (sd->sc.data[SC_JEXPBOOST])
  7034. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  7035. if (*job_exp) {
  7036. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  7037. *job_exp = cap_value(exp, 1, MAX_EXP);
  7038. }
  7039. return;
  7040. }
  7041. /**
  7042. * Show EXP gained by player in percentage by @showexp
  7043. * @param sd Player
  7044. * @param base_exp Base EXP gained/loss
  7045. * @param next_base_exp Base EXP needed for next base level
  7046. * @param job_exp Job EXP gained/loss
  7047. * @param next_job_exp Job EXP needed for next job level
  7048. * @param lost True:EXP penalty, lose EXP
  7049. **/
  7050. 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) {
  7051. char output[CHAT_SIZE_MAX];
  7052. nullpo_retv(sd);
  7053. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  7054. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  7055. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  7056. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  7057. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  7058. }
  7059. /**
  7060. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  7061. * @param sd Player
  7062. * @param src EXP source
  7063. * @param base_exp Base EXP gained
  7064. * @param base_exp Job EXP gained
  7065. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  7066. * @return
  7067. **/
  7068. void pc_gainexp(struct map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  7069. {
  7070. t_exp nextb = 0, nextj = 0;
  7071. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  7072. nullpo_retv(sd);
  7073. if(sd->bl.prev == NULL || pc_isdead(sd))
  7074. return;
  7075. if (!(exp_flag&2)) {
  7076. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  7077. return; // no exp on pvp maps
  7078. if (sd->status.guild_id>0)
  7079. base_exp -= guild_payexp(sd,base_exp);
  7080. }
  7081. flag = ((base_exp) ? 1 : 0) |
  7082. ((job_exp) ? 2 : 0) |
  7083. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  7084. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  7085. if (!(exp_flag&2))
  7086. pc_calcexp(sd, &base_exp, &job_exp, src);
  7087. nextb = pc_nextbaseexp(sd);
  7088. nextj = pc_nextjobexp(sd);
  7089. if (flag&4){
  7090. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  7091. base_exp = 0;
  7092. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  7093. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  7094. }
  7095. if (flag&8){
  7096. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  7097. job_exp = 0;
  7098. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  7099. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  7100. }
  7101. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  7102. //Note that this value should never be greater than the original
  7103. //therefore no overflow checks are needed. [Skotlex]
  7104. if (nextb > 0) {
  7105. float nextbp = (float) base_exp / (float) nextb;
  7106. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  7107. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  7108. }
  7109. if (nextj > 0) {
  7110. float nextjp = (float) job_exp / (float) nextj;
  7111. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  7112. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  7113. }
  7114. }
  7115. // Give EXP for Base Level
  7116. if (base_exp) {
  7117. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  7118. if (!pc_checkbaselevelup(sd))
  7119. clif_updatestatus(sd,SP_BASEEXP);
  7120. }
  7121. // Give EXP for Job Level
  7122. if (job_exp) {
  7123. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  7124. if (!pc_checkjoblevelup(sd))
  7125. clif_updatestatus(sd,SP_JOBEXP);
  7126. }
  7127. if (flag&1)
  7128. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  7129. if (flag&2)
  7130. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  7131. if (sd->state.showexp && (base_exp || job_exp))
  7132. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  7133. }
  7134. /**
  7135. * Lost Base/Job EXP from a player
  7136. * @param sd Player
  7137. * @param base_exp Base EXP lost
  7138. * @param job_exp Job EXP lost
  7139. **/
  7140. void pc_lostexp(struct map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  7141. nullpo_retv(sd);
  7142. if (base_exp) {
  7143. base_exp = u64min(sd->status.base_exp, base_exp);
  7144. sd->status.base_exp -= base_exp;
  7145. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  7146. clif_updatestatus(sd, SP_BASEEXP);
  7147. }
  7148. if (job_exp) {
  7149. job_exp = u64min(sd->status.job_exp, job_exp);
  7150. sd->status.job_exp -= job_exp;
  7151. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  7152. clif_updatestatus(sd, SP_JOBEXP);
  7153. }
  7154. if (sd->state.showexp && (base_exp || job_exp))
  7155. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  7156. }
  7157. /**
  7158. * Returns max base level for this character's class.
  7159. * @param job_id: Player's class
  7160. * @return Max Base Level
  7161. */
  7162. uint32 JobDatabase::get_maxBaseLv(uint16 job_id) {
  7163. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7164. return job ? job->max_base_level : 0;
  7165. }
  7166. /**
  7167. * Returns max base level for this character.
  7168. * @param sd Player
  7169. * @return Max Base Level
  7170. **/
  7171. unsigned int pc_maxbaselv(struct map_session_data *sd){
  7172. return job_db.get_maxBaseLv(sd->status.class_);
  7173. }
  7174. /**
  7175. * Returns max job level for this character's class.
  7176. * @param job_id: Player's class
  7177. * @return Max Job Level
  7178. */
  7179. uint32 JobDatabase::get_maxJobLv(uint16 job_id) {
  7180. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7181. return job ? job->max_job_level : 0;
  7182. }
  7183. /**
  7184. * Returns max job level for this character.
  7185. * @param sd Player
  7186. * @return Max Job Level
  7187. **/
  7188. unsigned int pc_maxjoblv(struct map_session_data *sd){
  7189. return job_db.get_maxJobLv(sd->status.class_);
  7190. }
  7191. /**
  7192. * Check if player is reached max base level
  7193. * @param sd
  7194. * @return True if reached max level
  7195. **/
  7196. bool pc_is_maxbaselv(struct map_session_data *sd) {
  7197. nullpo_retr(false, sd);
  7198. return (sd->status.base_level >= pc_maxbaselv(sd));
  7199. }
  7200. /**
  7201. * Check if player is reached max base level
  7202. * @param sd
  7203. * @return True if reached max level
  7204. **/
  7205. bool pc_is_maxjoblv(struct map_session_data *sd) {
  7206. nullpo_retr(false, sd);
  7207. return (sd->status.job_level >= pc_maxjoblv(sd));
  7208. }
  7209. /**
  7210. * Returns base experience for this character's class.
  7211. * @param job_id: Player's class
  7212. * @param level: Player's level
  7213. * @return Base EXP
  7214. */
  7215. t_exp JobDatabase::get_baseExp(uint16 job_id, uint32 level) {
  7216. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7217. return job ? job->base_exp[level - 1] : 0;
  7218. }
  7219. /**
  7220. * Base exp needed for player to level up.
  7221. * @param sd
  7222. * @return Base EXP needed for next base level
  7223. **/
  7224. t_exp pc_nextbaseexp(struct map_session_data *sd){
  7225. nullpo_ret(sd);
  7226. if (sd->status.base_level == 0) // Is this something that possible?
  7227. return 0;
  7228. if (pc_is_maxbaselv(sd))
  7229. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  7230. return static_cast<t_exp>(job_db.get_baseExp(sd->status.class_, sd->status.base_level));
  7231. }
  7232. /**
  7233. * Returns job experience for this character's class.
  7234. * @param job_id: Player's class
  7235. * @param level: Player's level
  7236. * @return Job EXP
  7237. */
  7238. t_exp JobDatabase::get_jobExp(uint16 job_id, uint32 level) {
  7239. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7240. return job ? job->job_exp[level - 1] : 0;
  7241. }
  7242. /**
  7243. * Job exp needed for player to level up.
  7244. * @param sd
  7245. * @return Job EXP needed for next job level
  7246. **/
  7247. t_exp pc_nextjobexp(struct map_session_data *sd){
  7248. nullpo_ret(sd);
  7249. if (sd->status.job_level == 0) // Is this something that possible?
  7250. return 0;
  7251. if (pc_is_maxjoblv(sd))
  7252. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  7253. return static_cast<t_exp>(job_db.get_jobExp(sd->status.class_, sd->status.job_level));
  7254. }
  7255. /**
  7256. * Returns max weight base for this character's class.
  7257. * @param job_id: Player's class
  7258. * @return Max weight base
  7259. */
  7260. int32 JobDatabase::get_maxWeight(uint16 job_id) {
  7261. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7262. return job ? job->max_weight_base : 0;
  7263. }
  7264. /// Returns the value of the specified stat.
  7265. int pc_getstat(map_session_data *sd, int type)
  7266. {
  7267. nullpo_retr(-1, sd);
  7268. switch( type ) {
  7269. case SP_STR: return sd->status.str;
  7270. case SP_AGI: return sd->status.agi;
  7271. case SP_VIT: return sd->status.vit;
  7272. case SP_INT: return sd->status.int_;
  7273. case SP_DEX: return sd->status.dex;
  7274. case SP_LUK: return sd->status.luk;
  7275. case SP_POW: return sd->status.pow;
  7276. case SP_STA: return sd->status.sta;
  7277. case SP_WIS: return sd->status.wis;
  7278. case SP_SPL: return sd->status.spl;
  7279. case SP_CON: return sd->status.con;
  7280. case SP_CRT: return sd->status.crt;
  7281. default:
  7282. return -1;
  7283. }
  7284. }
  7285. /// Sets the specified stat to the specified value.
  7286. /// Returns the new value.
  7287. int pc_setstat(struct map_session_data* sd, int type, int val)
  7288. {
  7289. nullpo_retr(-1, sd);
  7290. switch( type ) {
  7291. case SP_STR: sd->status.str = val; break;
  7292. case SP_AGI: sd->status.agi = val; break;
  7293. case SP_VIT: sd->status.vit = val; break;
  7294. case SP_INT: sd->status.int_ = val; break;
  7295. case SP_DEX: sd->status.dex = val; break;
  7296. case SP_LUK: sd->status.luk = val; break;
  7297. case SP_POW: sd->status.pow = val; break;
  7298. case SP_STA: sd->status.sta = val; break;
  7299. case SP_WIS: sd->status.wis = val; break;
  7300. case SP_SPL: sd->status.spl = val; break;
  7301. case SP_CON: sd->status.con = val; break;
  7302. case SP_CRT: sd->status.crt = val; break;
  7303. default:
  7304. return -1;
  7305. }
  7306. return val;
  7307. }
  7308. /**
  7309. * Gets the total number of status points at the provided level.
  7310. * @param level: Player base level.
  7311. * @return Total number of status points at specific base level.
  7312. */
  7313. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  7314. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7315. if( entry != nullptr ){
  7316. return entry->statpoints;
  7317. }else{
  7318. return 0;
  7319. }
  7320. }
  7321. /**
  7322. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  7323. * @param level: Player base level.
  7324. * @param table: Use table value or formula.
  7325. * @return Status points at specific base level.
  7326. */
  7327. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  7328. uint32 next_level = this->get_table_point( level + 1 );
  7329. uint32 current_level = this->get_table_point( level );
  7330. if( next_level > current_level ){
  7331. return next_level - current_level;
  7332. }else{
  7333. return 0;
  7334. }
  7335. }
  7336. /**
  7337. * Gets the total number of trait points at the provided level.
  7338. * @param level: Player base level.
  7339. * @return Total number of trait points at specific base level.
  7340. */
  7341. uint32 PlayerStatPointDatabase::get_trait_table_point(uint16 level) {
  7342. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7343. if( entry != nullptr ){
  7344. return entry->traitpoints;
  7345. }else{
  7346. return 0;
  7347. }
  7348. }
  7349. /**
  7350. * Calculates the number of trait points PC gets when leveling up (from level to level+1)
  7351. * @param level: Player base level.
  7352. * @param table: Use table value or formula.
  7353. * @return Trait points at specific base level.
  7354. */
  7355. uint32 PlayerStatPointDatabase::pc_gets_trait_point(uint16 level) {
  7356. uint32 next_level = this->get_trait_table_point( level + 1 );
  7357. uint32 current_level = this->get_trait_table_point( level );
  7358. if( next_level > current_level ){
  7359. return next_level - current_level;
  7360. }else{
  7361. return 0;
  7362. }
  7363. }
  7364. #ifdef RENEWAL_STAT
  7365. /// Renewal status point cost formula
  7366. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  7367. #else
  7368. /// Pre-Renewal status point cost formula
  7369. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  7370. #endif
  7371. /// Returns the number of stat points needed to change the specified stat by val.
  7372. /// If val is negative, returns the number of stat points that would be needed to
  7373. /// raise the specified stat from (current value - val) to current value.
  7374. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  7375. {
  7376. int low, high, sp = 0, max = 0;
  7377. if ( val == 0 )
  7378. return 0;
  7379. low = pc_getstat(sd,type);
  7380. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  7381. if ( low >= max && val > 0 )
  7382. return 0; // Official servers show '0' when max is reached
  7383. high = low + val;
  7384. if ( val < 0 )
  7385. SWAP(low, high);
  7386. for ( ; low < high; low++ )
  7387. sp += PC_STATUS_POINT_COST(low);
  7388. return sp;
  7389. }
  7390. /**
  7391. * Returns the value the specified stat can be increased by with the current
  7392. * amount of available status points for the current character's class.
  7393. *
  7394. * @param sd The target character.
  7395. * @param type Stat to verify.
  7396. * @return Maximum value the stat could grow by.
  7397. */
  7398. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  7399. {
  7400. int base, final_val, status_points, max_param;
  7401. nullpo_ret(sd);
  7402. base = final_val = pc_getstat(sd, type);
  7403. status_points = sd->status.status_point;
  7404. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  7405. while (final_val <= max_param && status_points >= 0) {
  7406. status_points -= PC_STATUS_POINT_COST(final_val);
  7407. final_val++;
  7408. }
  7409. final_val--;
  7410. return (final_val > base ? final_val-base : 0);
  7411. }
  7412. /**
  7413. * Raises a stat by the specified amount.
  7414. *
  7415. * Obeys max_parameter limits.
  7416. * Subtracts status points according to the cost of the increased stat points.
  7417. *
  7418. * @param sd The target character.
  7419. * @param type The stat to change (see enum _sp)
  7420. * @param increase The stat increase (strictly positive) amount.
  7421. * @retval true if the stat was increased by any amount.
  7422. * @retval false if there were no changes.
  7423. */
  7424. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  7425. {
  7426. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  7427. nullpo_ret(sd);
  7428. // check conditions
  7429. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  7430. clif_statusupack(sd, type, 0, 0);
  7431. return false;
  7432. }
  7433. // check limits
  7434. current = pc_getstat(sd, type);
  7435. max_increase = pc_maxparameterincrease(sd, type);
  7436. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7437. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  7438. clif_statusupack(sd, type, 0, 0);
  7439. return false;
  7440. }
  7441. // check status points
  7442. needed_points = pc_need_status_point(sd, type, increase);
  7443. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  7444. clif_statusupack(sd, type, 0, 0);
  7445. return false;
  7446. }
  7447. // set new values
  7448. final_value = pc_setstat(sd, type, current + increase);
  7449. sd->status.status_point -= needed_points;
  7450. status_calc_pc(sd,SCO_NONE);
  7451. // update increase cost indicator
  7452. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7453. // update statpoint count
  7454. clif_updatestatus(sd, SP_STATUSPOINT);
  7455. // update stat value
  7456. clif_statusupack(sd, type, 1, final_value); // required
  7457. if( final_value > 255 )
  7458. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7459. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7460. return true;
  7461. }
  7462. /**
  7463. * Raises a stat by the specified amount.
  7464. *
  7465. * Obeys max_parameter limits.
  7466. * Does not subtract status points for the cost of the modified stat points.
  7467. *
  7468. * @param sd The target character.
  7469. * @param type The stat to change (see enum _sp)
  7470. * @param val The stat increase (or decrease) amount.
  7471. * @return the stat increase amount.
  7472. * @retval 0 if no changes were made.
  7473. */
  7474. int pc_statusup2(struct map_session_data* sd, int type, int val)
  7475. {
  7476. int max, need;
  7477. nullpo_ret(sd);
  7478. if( type < SP_STR || type > SP_LUK )
  7479. {
  7480. clif_statusupack(sd,type,0,0);
  7481. return 0;
  7482. }
  7483. need = pc_need_status_point(sd,type,1);
  7484. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  7485. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  7486. status_calc_pc(sd,SCO_NONE);
  7487. // update increase cost indicator
  7488. if( need != pc_need_status_point(sd,type,1) )
  7489. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7490. // update stat value
  7491. clif_statusupack(sd,type,1,val); // required
  7492. if( val > 255 )
  7493. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  7494. return val;
  7495. }
  7496. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  7497. /// If val is negative, returns the number of trait stat points that would be needed to
  7498. /// raise the specified trait stat from (current value - val) to current value.
  7499. int pc_need_trait_point(struct map_session_data* sd, int type, int val)
  7500. {
  7501. nullpo_retr(0, sd);
  7502. if (val == 0 || type < SP_POW || type > SP_CRT)
  7503. return 0;
  7504. int low = pc_getstat(sd, type);
  7505. int max = pc_maxparameter(sd, (e_params)(PARAM_POW + type - SP_POW));
  7506. if (low >= max && val > 0)
  7507. return 0; // Official servers show '0' when max is reached
  7508. int high = low + val, sp = 0;
  7509. if (val < 0)
  7510. SWAP(low, high);
  7511. for (; low < high; low++)
  7512. sp += 1;
  7513. return sp;
  7514. }
  7515. /**
  7516. * Returns the value the specified trait stat can be increased by with the current
  7517. * amount of available trait status points for the current character's class.
  7518. *
  7519. * @param sd The target character.
  7520. * @param type Trait stat to verify.
  7521. * @return Maximum value the stat could grow by.
  7522. */
  7523. int pc_maxtraitparameterincrease(struct map_session_data* sd, int type)
  7524. {
  7525. nullpo_ret(sd);
  7526. if( type < SP_POW || type > SP_CRT ){
  7527. return 0;
  7528. }
  7529. int base, final_val = pc_getstat(sd, type);
  7530. int trait_points = sd->status.trait_point;
  7531. int max_param = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW));
  7532. base = final_val;
  7533. while (final_val <= max_param && trait_points >= 0) {
  7534. trait_points -= 1;
  7535. final_val++;
  7536. }
  7537. final_val--;
  7538. return (final_val > base ? final_val - base : 0);
  7539. }
  7540. /**
  7541. * Raises a trait stat by the specified amount.
  7542. *
  7543. * Obeys max_traitparameter limits.
  7544. * Subtracts trait status points according to the cost of the increased trait stat points.
  7545. *
  7546. * @param sd The target character.
  7547. * @param type The stat to change (see enum _sp)
  7548. * @param increase The stat increase (strictly positive) amount.
  7549. * @retval true if the trait stat was increased by any amount.
  7550. * @retval false if there were no changes.
  7551. */
  7552. bool pc_traitstatusup(struct map_session_data* sd, int type, int increase)
  7553. {
  7554. nullpo_ret(sd);
  7555. // check conditions
  7556. if (type < SP_POW || type > SP_CRT || increase <= 0) {
  7557. clif_statusupack(sd, type, 0, 0);
  7558. return false;
  7559. }
  7560. // check limits
  7561. int current = pc_getstat(sd, type);
  7562. int max_increase = pc_maxtraitparameterincrease(sd, type);
  7563. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7564. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW))) {
  7565. clif_statusupack(sd, type, 0, 0);
  7566. return false;
  7567. }
  7568. // check status points
  7569. int needed_points = pc_need_trait_point(sd, type, increase);
  7570. if (needed_points < 0 || needed_points > sd->status.trait_point) { // Sanity check
  7571. clif_statusupack(sd, type, 0, 0);
  7572. return false;
  7573. }
  7574. // set new values
  7575. int final_value = pc_setstat(sd, type, current + increase);
  7576. sd->status.trait_point -= needed_points;
  7577. status_calc_pc(sd, SCO_NONE);
  7578. // update increase cost indicator
  7579. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7580. // update statpoint count
  7581. clif_updatestatus(sd, SP_TRAITPOINT);
  7582. // update stat value
  7583. clif_statusupack(sd, type, 1, final_value); // required
  7584. if (final_value > 255)
  7585. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7586. //achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7587. return true;
  7588. }
  7589. /**
  7590. * Raises a trait stat by the specified amount.
  7591. *
  7592. * Obeys max_trait_parameter limits.
  7593. * Does not subtract status points for the cost of the modified stat points.
  7594. *
  7595. * @param sd The target character.
  7596. * @param type The stat to change (see enum _sp)
  7597. * @param val The stat increase (or decrease) amount.
  7598. * @return the stat increase amount.
  7599. * @retval 0 if no changes were made.
  7600. */
  7601. int pc_traitstatusup2(struct map_session_data* sd, int type, int val)
  7602. {
  7603. nullpo_ret(sd);
  7604. if (type < SP_POW || type > SP_CRT) {
  7605. clif_statusupack(sd, type, 0, 0);
  7606. return 0;
  7607. }
  7608. int need = pc_need_trait_point(sd, type, 1);
  7609. int max = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW)); // set new value
  7610. val = pc_setstat(sd, type, cap_value(pc_getstat(sd, type) + val, 0, max));
  7611. status_calc_pc(sd, SCO_NONE);
  7612. // update increase cost indicator
  7613. if (need != pc_need_trait_point(sd, type, 1))
  7614. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7615. // update stat value
  7616. clif_statusupack(sd, type, 1, val); // required
  7617. if (val > 255)
  7618. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7619. return val;
  7620. }
  7621. /*==========================================
  7622. * Update skill_lv for player sd
  7623. * Skill point allocation
  7624. *------------------------------------------*/
  7625. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  7626. {
  7627. uint16 idx = skill_get_index(skill_id);
  7628. nullpo_retv(sd);
  7629. if (!idx) {
  7630. if (skill_id)
  7631. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  7632. return;
  7633. }
  7634. // Level up guild skill
  7635. if (SKILL_CHK_GUILD(skill_id)) {
  7636. guild_skillup(sd, skill_id);
  7637. return;
  7638. }
  7639. // Level up homunculus skill
  7640. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  7641. hom_skillup(sd->hd, skill_id);
  7642. return;
  7643. }
  7644. else {
  7645. if( sd->status.skill_point > 0 &&
  7646. sd->status.skill[idx].id &&
  7647. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  7648. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  7649. {
  7650. int lv, range, upgradable;
  7651. sd->status.skill[idx].lv++;
  7652. sd->status.skill_point--;
  7653. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  7654. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  7655. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  7656. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  7657. else
  7658. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  7659. lv = sd->status.skill[idx].lv;
  7660. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  7661. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  7662. clif_skillup(sd,skill_id,lv,range,upgradable);
  7663. clif_updatestatus(sd,SP_SKILLPOINT);
  7664. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  7665. clif_updatestatus(sd,SP_CARTINFO);
  7666. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  7667. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7668. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  7669. clif_skillinfoblock(sd);
  7670. }
  7671. //else
  7672. // 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);
  7673. }
  7674. }
  7675. /*==========================================
  7676. * /allskill
  7677. *------------------------------------------*/
  7678. int pc_allskillup(struct map_session_data *sd)
  7679. {
  7680. int i;
  7681. nullpo_ret(sd);
  7682. for (i = 0; i < MAX_SKILL; i++) {
  7683. 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) {
  7684. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  7685. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  7686. if (sd->status.skill[i].lv == 0)
  7687. sd->status.skill[i].id = 0;
  7688. }
  7689. }
  7690. if (!pc_grant_allskills(sd, true)) {
  7691. uint16 sk_id;
  7692. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(sd->status.class_);
  7693. if (tree != nullptr && !tree->skills.empty()) {
  7694. for (const auto &skillsit : tree->skills) {
  7695. sk_id = skillsit.first;
  7696. uint16 sk_idx = skill_get_index(sk_id);
  7697. if (sk_idx == 0)
  7698. continue;
  7699. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  7700. if (
  7701. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  7702. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  7703. sk_id == SG_DEVIL
  7704. )
  7705. continue; //Cannot be learned normally.
  7706. sd->status.skill[sk_idx].id = sk_id;
  7707. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  7708. }
  7709. }
  7710. }
  7711. status_calc_pc(sd,SCO_NONE);
  7712. //Required because if you could level up all skills previously,
  7713. //the update will not be sent as only the lv variable changes.
  7714. clif_skillinfoblock(sd);
  7715. return 0;
  7716. }
  7717. /*==========================================
  7718. * /resetlvl
  7719. *------------------------------------------*/
  7720. int pc_resetlvl(struct map_session_data* sd,int type)
  7721. {
  7722. int i;
  7723. nullpo_ret(sd);
  7724. if (type != 3) //Also reset skills
  7725. pc_resetskill(sd, 0);
  7726. if(type == 1){
  7727. sd->status.skill_point=0;
  7728. sd->status.trait_point = 0;
  7729. sd->status.base_level=1;
  7730. sd->status.job_level=1;
  7731. sd->status.base_exp=0;
  7732. sd->status.job_exp=0;
  7733. if(sd->sc.option !=0)
  7734. sd->sc.option = 0;
  7735. sd->status.str=1;
  7736. sd->status.agi=1;
  7737. sd->status.vit=1;
  7738. sd->status.int_=1;
  7739. sd->status.dex=1;
  7740. sd->status.luk=1;
  7741. sd->status.pow=0;
  7742. sd->status.sta=0;
  7743. sd->status.wis=0;
  7744. sd->status.spl=0;
  7745. sd->status.con=0;
  7746. sd->status.crt=0;
  7747. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  7748. sd->status.status_point=100; // not 88 [celest]
  7749. // give platinum skills upon changing
  7750. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  7751. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  7752. }
  7753. }
  7754. if(type == 2){
  7755. sd->status.skill_point=0;
  7756. sd->status.base_level=1;
  7757. sd->status.job_level=1;
  7758. sd->status.base_exp=0;
  7759. sd->status.job_exp=0;
  7760. }
  7761. if(type == 3){
  7762. sd->status.base_level=1;
  7763. sd->status.base_exp=0;
  7764. }
  7765. if(type == 4){
  7766. sd->status.job_level=1;
  7767. sd->status.job_exp=0;
  7768. }
  7769. clif_updatestatus(sd,SP_STATUSPOINT);
  7770. clif_updatestatus(sd,SP_TRAITPOINT);
  7771. clif_updatestatus(sd,SP_STR);
  7772. clif_updatestatus(sd,SP_AGI);
  7773. clif_updatestatus(sd,SP_VIT);
  7774. clif_updatestatus(sd,SP_INT);
  7775. clif_updatestatus(sd,SP_DEX);
  7776. clif_updatestatus(sd,SP_LUK);
  7777. clif_updatestatus(sd,SP_POW);
  7778. clif_updatestatus(sd,SP_STA);
  7779. clif_updatestatus(sd,SP_WIS);
  7780. clif_updatestatus(sd,SP_SPL);
  7781. clif_updatestatus(sd,SP_CON);
  7782. clif_updatestatus(sd,SP_CRT);
  7783. clif_updatestatus(sd,SP_BASELEVEL);
  7784. clif_updatestatus(sd,SP_JOBLEVEL);
  7785. clif_updatestatus(sd,SP_STATUSPOINT);
  7786. clif_updatestatus(sd,SP_BASEEXP);
  7787. clif_updatestatus(sd,SP_JOBEXP);
  7788. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7789. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7790. clif_updatestatus(sd,SP_SKILLPOINT);
  7791. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7792. clif_updatestatus(sd,SP_UAGI);
  7793. clif_updatestatus(sd,SP_UVIT);
  7794. clif_updatestatus(sd,SP_UINT);
  7795. clif_updatestatus(sd,SP_UDEX);
  7796. clif_updatestatus(sd,SP_ULUK); // End Addition
  7797. clif_updatestatus(sd,SP_UPOW);
  7798. clif_updatestatus(sd,SP_USTA);
  7799. clif_updatestatus(sd,SP_UWIS);
  7800. clif_updatestatus(sd,SP_USPL);
  7801. clif_updatestatus(sd,SP_UCON);
  7802. clif_updatestatus(sd,SP_UCRT);
  7803. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  7804. if(sd->equip_index[i] >= 0)
  7805. if(pc_isequip(sd,sd->equip_index[i]))
  7806. pc_unequipitem(sd,sd->equip_index[i],2);
  7807. }
  7808. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  7809. party_send_levelup(sd);
  7810. status_calc_pc(sd, SCO_FORCE);
  7811. clif_skillinfoblock(sd);
  7812. return 0;
  7813. }
  7814. /*==========================================
  7815. * /resetstate
  7816. *------------------------------------------*/
  7817. int pc_resetstate(struct map_session_data* sd)
  7818. {
  7819. nullpo_ret(sd);
  7820. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  7821. 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",
  7822. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  7823. sd->status.base_level = pc_maxbaselv( sd );
  7824. clif_updatestatus( sd, SP_BASELEVEL );
  7825. }
  7826. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  7827. sd->status.trait_point = statpoint_db.get_trait_table_point(sd->status.base_level);
  7828. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  7829. sd->status.status_point += battle_config.transcendent_status_points;
  7830. }
  7831. if ((sd->class_&JOBL_FOURTH) != 0) {
  7832. sd->status.trait_point += battle_config.trait_points_job_change;
  7833. }
  7834. pc_setstat(sd, SP_STR, 1);
  7835. pc_setstat(sd, SP_AGI, 1);
  7836. pc_setstat(sd, SP_VIT, 1);
  7837. pc_setstat(sd, SP_INT, 1);
  7838. pc_setstat(sd, SP_DEX, 1);
  7839. pc_setstat(sd, SP_LUK, 1);
  7840. pc_setstat(sd, SP_POW, 0);
  7841. pc_setstat(sd, SP_STA, 0);
  7842. pc_setstat(sd, SP_WIS, 0);
  7843. pc_setstat(sd, SP_SPL, 0);
  7844. pc_setstat(sd, SP_CON, 0);
  7845. pc_setstat(sd, SP_CRT, 0);
  7846. clif_updatestatus(sd,SP_STR);
  7847. clif_updatestatus(sd,SP_AGI);
  7848. clif_updatestatus(sd,SP_VIT);
  7849. clif_updatestatus(sd,SP_INT);
  7850. clif_updatestatus(sd,SP_DEX);
  7851. clif_updatestatus(sd,SP_LUK);
  7852. clif_updatestatus(sd,SP_POW);
  7853. clif_updatestatus(sd,SP_STA);
  7854. clif_updatestatus(sd,SP_WIS);
  7855. clif_updatestatus(sd,SP_SPL);
  7856. clif_updatestatus(sd,SP_CON);
  7857. clif_updatestatus(sd,SP_CRT);
  7858. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7859. clif_updatestatus(sd,SP_UAGI);
  7860. clif_updatestatus(sd,SP_UVIT);
  7861. clif_updatestatus(sd,SP_UINT);
  7862. clif_updatestatus(sd,SP_UDEX);
  7863. clif_updatestatus(sd,SP_ULUK); // End Addition
  7864. clif_updatestatus(sd,SP_UPOW);
  7865. clif_updatestatus(sd,SP_USTA);
  7866. clif_updatestatus(sd,SP_UWIS);
  7867. clif_updatestatus(sd,SP_USPL);
  7868. clif_updatestatus(sd,SP_UCON);
  7869. clif_updatestatus(sd,SP_UCRT);
  7870. clif_updatestatus(sd,SP_STATUSPOINT);
  7871. clif_updatestatus(sd,SP_TRAITPOINT);
  7872. if( sd->mission_mobid ) { //bugreport:2200
  7873. sd->mission_mobid = 0;
  7874. sd->mission_count = 0;
  7875. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  7876. }
  7877. status_calc_pc(sd, SCO_NONE);
  7878. return 1;
  7879. }
  7880. /*==========================================
  7881. * /resetskill
  7882. * if flag&1, perform block resync and status_calc call.
  7883. * if flag&2, just count total amount of skill points used by player, do not really reset.
  7884. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  7885. *------------------------------------------*/
  7886. int pc_resetskill(struct map_session_data* sd, int flag)
  7887. {
  7888. int i, skill_point=0;
  7889. nullpo_ret(sd);
  7890. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  7891. return 0;
  7892. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  7893. /**
  7894. * 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)
  7895. **/
  7896. if( pc_is_taekwon_ranker(sd) )
  7897. return 0;
  7898. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7899. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  7900. i = sd->sc.option;
  7901. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  7902. i &= ~OPTION_RIDING;
  7903. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  7904. i &= ~OPTION_FALCON;
  7905. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  7906. i &= ~OPTION_DRAGON;
  7907. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  7908. i &= ~OPTION_WUG;
  7909. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  7910. i &= ~OPTION_WUGRIDER;
  7911. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  7912. i &= ~OPTION_MADOGEAR;
  7913. #ifndef NEW_CARTS
  7914. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  7915. i &= ~OPTION_CART;
  7916. #else
  7917. if( sd->sc.data[SC_PUSH_CART] )
  7918. pc_setcart(sd, 0);
  7919. #endif
  7920. if( i != sd->sc.option )
  7921. pc_setoption(sd, i);
  7922. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  7923. hom_vaporize(sd, HOM_ST_ACTIVE);
  7924. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  7925. status_change_end(&sd->bl, SC_SPRITEMABLE);
  7926. if (sd->sc.data[SC_SOULATTACK] && pc_checkskill(sd, SU_SOULATTACK))
  7927. status_change_end(&sd->bl, SC_SOULATTACK);
  7928. }
  7929. for (const auto &skill : skill_db) {
  7930. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  7931. uint8 lv = sd->status.skill[idx].lv;
  7932. if (lv == 0 || skill_id == 0)
  7933. continue;
  7934. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  7935. continue;
  7936. // Don't reset trick dead if not a novice/baby
  7937. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7938. {
  7939. sd->status.skill[idx].lv = 0;
  7940. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7941. continue;
  7942. }
  7943. // do not reset basic skill
  7944. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7945. continue;
  7946. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  7947. continue;
  7948. if( flag&4 && !skill_ischangesex(skill_id) )
  7949. continue;
  7950. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  7951. { //Only handle quest skills in a special way when you can't learn them manually
  7952. if( battle_config.quest_skill_reset && !(flag&2) )
  7953. { //Wipe them
  7954. sd->status.skill[idx].lv = 0;
  7955. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7956. }
  7957. continue;
  7958. }
  7959. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  7960. skill_point += lv;
  7961. else
  7962. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  7963. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  7964. if( !(flag&2) )
  7965. {// reset
  7966. sd->status.skill[idx].lv = 0;
  7967. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7968. }
  7969. }
  7970. if( flag&2 || !skill_point ) return skill_point;
  7971. sd->status.skill_point += skill_point;
  7972. if (flag&1) {
  7973. clif_updatestatus(sd,SP_SKILLPOINT);
  7974. clif_skillinfoblock(sd);
  7975. status_calc_pc(sd, SCO_FORCE);
  7976. }
  7977. return skill_point;
  7978. }
  7979. /*==========================================
  7980. * /resetfeel [Komurka]
  7981. *------------------------------------------*/
  7982. int pc_resetfeel(struct map_session_data* sd)
  7983. {
  7984. int i;
  7985. nullpo_ret(sd);
  7986. for (i=0; i<MAX_PC_FEELHATE; i++)
  7987. {
  7988. sd->feel_map[i].m = -1;
  7989. sd->feel_map[i].index = 0;
  7990. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  7991. }
  7992. return 0;
  7993. }
  7994. int pc_resethate(struct map_session_data* sd)
  7995. {
  7996. int i;
  7997. nullpo_ret(sd);
  7998. for (i=0; i<MAX_PC_FEELHATE; i++)
  7999. {
  8000. sd->hate_mob[i] = -1;
  8001. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  8002. }
  8003. return 0;
  8004. }
  8005. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  8006. {
  8007. int bonus = 0;
  8008. nullpo_ret(sd);
  8009. skill_id = skill_dummy2skill_id(skill_id);
  8010. for (auto &it : sd->skillatk) {
  8011. if (it.id == skill_id) {
  8012. bonus += it.val;
  8013. break;
  8014. }
  8015. }
  8016. return bonus;
  8017. }
  8018. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  8019. {
  8020. int bonus = 0;
  8021. nullpo_ret(sd);
  8022. skill_id = skill_dummy2skill_id(skill_id);
  8023. for (auto &it : sd->subskill) {
  8024. if (it.id == skill_id) {
  8025. bonus += it.val;
  8026. break;
  8027. }
  8028. }
  8029. return bonus;
  8030. }
  8031. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  8032. int bonus = sd->bonus.add_heal_rate;
  8033. nullpo_ret(sd);
  8034. skill_id = skill_dummy2skill_id(skill_id);
  8035. if( bonus ) {
  8036. switch( skill_id ) {
  8037. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  8038. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  8039. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  8040. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  8041. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  8042. case AB_CHEAL: if (!(battle_config.skill_add_heal_rate & 32)) bonus = 0; break;
  8043. case AB_HIGHNESSHEAL: if (!(battle_config.skill_add_heal_rate & 64)) bonus = 0; break;
  8044. case CD_MEDIALE_VOTUM: if (!(battle_config.skill_add_heal_rate & 128)) bonus = 0; break;
  8045. case CD_DILECTIO_HEAL: if (!(battle_config.skill_add_heal_rate & 256)) bonus = 0; break;
  8046. }
  8047. }
  8048. for (auto &it : sd->skillheal) {
  8049. if (it.id == skill_id) {
  8050. bonus += it.val;
  8051. break;
  8052. }
  8053. }
  8054. return bonus;
  8055. }
  8056. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  8057. int bonus = sd->bonus.add_heal2_rate;
  8058. skill_id = skill_dummy2skill_id(skill_id);
  8059. for (auto &it : sd->skillheal2) {
  8060. if (it.id == skill_id) {
  8061. bonus += it.val;
  8062. break;
  8063. }
  8064. }
  8065. return bonus;
  8066. }
  8067. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  8068. {
  8069. if( !pc_isdead(sd) )
  8070. return; // not applicable
  8071. if( sd->bg_id && bg_member_respawn(sd) )
  8072. return; // member revived by battleground
  8073. pc_setstand(sd, true);
  8074. pc_setrestartvalue(sd,3);
  8075. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  8076. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  8077. }
  8078. static TIMER_FUNC(pc_respawn_timer){
  8079. struct map_session_data *sd = map_id2sd(id);
  8080. if( sd != NULL )
  8081. {
  8082. sd->pvp_point=0;
  8083. sd->respawn_tid = INVALID_TIMER;
  8084. pc_respawn(sd,CLR_OUTSIGHT);
  8085. }
  8086. return 0;
  8087. }
  8088. /*==========================================
  8089. * Invoked when a player has received damage
  8090. *------------------------------------------*/
  8091. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp, unsigned int ap)
  8092. {
  8093. if (ap) clif_updatestatus(sd,SP_AP);
  8094. if (sp) clif_updatestatus(sd,SP_SP);
  8095. if (hp) clif_updatestatus(sd,SP_HP);
  8096. else return;
  8097. if (!src)
  8098. return;
  8099. if( pc_issit(sd) ) {
  8100. pc_setstand(sd, true);
  8101. skill_sit(sd,0);
  8102. }
  8103. if (sd->progressbar.npc_id)
  8104. clif_progressbar_abort(sd);
  8105. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  8106. pet_target_check(sd->pd,src,1);
  8107. if( sd->status.ele_id > 0 )
  8108. elemental_set_target(sd,src);
  8109. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  8110. sd->canlog_tick = gettick();
  8111. }
  8112. TIMER_FUNC(pc_close_npc_timer){
  8113. TBL_PC *sd = map_id2sd(id);
  8114. if(sd) pc_close_npc(sd,data);
  8115. return 0;
  8116. }
  8117. /**
  8118. * Method to properly close a NPC for player and clear anything related.
  8119. * @param sd: Player attached
  8120. * @param flag: Method of closure
  8121. * 1: Produce a close button and end the NPC
  8122. * 2: End the NPC (best for no dialog windows)
  8123. */
  8124. void pc_close_npc(struct map_session_data *sd,int flag)
  8125. {
  8126. nullpo_retv(sd);
  8127. if (sd->npc_id || sd->npc_shopid) {
  8128. if (sd->state.using_fake_npc) {
  8129. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  8130. sd->state.using_fake_npc = 0;
  8131. }
  8132. if (sd->st) {
  8133. if(sd->st->state == RUN){ //wait ending code execution
  8134. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  8135. return;
  8136. }
  8137. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  8138. sd->st->mes_active = 0;
  8139. }
  8140. sd->state.menu_or_input = 0;
  8141. sd->npc_menu = 0;
  8142. sd->npc_shopid = 0;
  8143. #ifdef SECURE_NPCTIMEOUT
  8144. if( sd->npc_idle_timer != INVALID_TIMER ){
  8145. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  8146. sd->npc_idle_timer = INVALID_TIMER;
  8147. }
  8148. #endif
  8149. if (sd->st) {
  8150. if (sd->st->state == CLOSE) {
  8151. clif_scriptclose(sd, sd->npc_id);
  8152. clif_scriptclear( *sd, sd->npc_id ); // [Ind/Hercules]
  8153. sd->st->state = END; // Force to end now
  8154. }
  8155. if (sd->st->state == END) { // free attached scripts that are waiting
  8156. script_free_state(sd->st);
  8157. sd->st = NULL;
  8158. sd->npc_id = 0;
  8159. }
  8160. }
  8161. }
  8162. }
  8163. /*==========================================
  8164. * Invoked when a player has negative current hp
  8165. *------------------------------------------*/
  8166. int pc_dead(struct map_session_data *sd,struct block_list *src)
  8167. {
  8168. int i=0,k=0;
  8169. t_tick tick = gettick();
  8170. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8171. // Activate Steel body if a super novice dies at 99+% exp [celest]
  8172. // Super Novices have no kill or die functions attached when saved by their angel
  8173. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  8174. t_exp exp = pc_nextbaseexp(sd);
  8175. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  8176. sd->state.snovice_dead_flag = 1;
  8177. pc_setrestartvalue(sd,1);
  8178. status_percent_heal(&sd->bl, 100, 100);
  8179. clif_resurrection(&sd->bl, 1);
  8180. if(battle_config.pc_invincible_time)
  8181. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8182. sc_start(&sd->bl,&sd->bl,SC_STEELBODY,100,5,skill_get_time(MO_STEELBODY,5));
  8183. if(mapdata_flag_gvg2(mapdata))
  8184. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  8185. return 0;
  8186. }
  8187. }
  8188. for(k = 0; k < MAX_DEVOTION; k++) {
  8189. if (sd->devotion[k]){
  8190. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  8191. if (devsd)
  8192. status_change_end(&devsd->bl, SC_DEVOTION);
  8193. sd->devotion[k] = 0;
  8194. }
  8195. }
  8196. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  8197. if (sd->stellar_mark[k]) {
  8198. struct map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  8199. if (smarksd)
  8200. status_change_end(&smarksd->bl, SC_FLASHKICK);
  8201. sd->stellar_mark[k] = 0;
  8202. }
  8203. }
  8204. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  8205. if (sd->united_soul[k]) {
  8206. struct map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  8207. if (usoulsd)
  8208. status_change_end(&usoulsd->bl, SC_SOULUNITY);
  8209. sd->united_soul[k] = 0;
  8210. }
  8211. }
  8212. for (k = 0; k < MAX_SERVANT_SIGN; k++) {
  8213. if (sd->servant_sign[k]) {
  8214. struct map_session_data *ssignsd = map_id2sd(sd->servant_sign[k]);
  8215. if (ssignsd)
  8216. status_change_end(&ssignsd->bl, SC_SERVANT_SIGN);
  8217. sd->servant_sign[k] = 0;
  8218. }
  8219. }
  8220. if(sd->shadowform_id) { //if we were target of shadowform
  8221. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM);
  8222. sd->shadowform_id = 0; //should be remove on status end anyway
  8223. }
  8224. if(sd->status.pet_id > 0 && sd->pd) {
  8225. struct pet_data *pd = sd->pd;
  8226. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  8227. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  8228. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  8229. pet_set_intimate(pd, PET_INTIMATE_NONE);
  8230. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  8231. }
  8232. if( sd->pd->target_id ) // Unlock all targets...
  8233. pet_unlocktarget(sd->pd);
  8234. }
  8235. 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)
  8236. hom_vaporize(sd, HOM_ST_ACTIVE);
  8237. if( sd->md )
  8238. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  8239. if( sd->ed )
  8240. elemental_delete(sd->ed);
  8241. // Leave duel if you die [LuzZza]
  8242. if(battle_config.duel_autoleave_when_die) {
  8243. if(sd->duel_group > 0)
  8244. duel_leave(sd->duel_group, sd);
  8245. if(sd->duel_invite > 0)
  8246. duel_reject(sd->duel_invite, sd);
  8247. }
  8248. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  8249. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  8250. sd->skill_keep_using.tid = INVALID_TIMER;
  8251. }
  8252. pc_close_npc(sd,2); //close npc if we were using one
  8253. /* e.g. not killed thru pc_damage */
  8254. if( pc_issit(sd) ) {
  8255. clif_status_load(&sd->bl,EFST_SIT,0);
  8256. }
  8257. pc_setdead(sd);
  8258. clif_party_dead( *sd );
  8259. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  8260. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  8261. if (battle_config.loose_ap_on_death == 1)
  8262. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  8263. //Reset menu skills/item skills
  8264. if ((sd->skillitem) != 0)
  8265. sd->skillitem = sd->skillitemlv = 0;
  8266. if ((sd->menuskill_id) != 0)
  8267. sd->menuskill_id = sd->menuskill_val = 0;
  8268. //Reset ticks.
  8269. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  8270. if ( sd->spiritball !=0 )
  8271. pc_delspiritball(sd,sd->spiritball,0);
  8272. if (sd->soulball != 0)
  8273. pc_delsoulball(sd, sd->soulball, false);
  8274. if (sd->servantball != 0)
  8275. pc_delservantball( *sd, sd->servantball );
  8276. if (sd->abyssball != 0)
  8277. pc_delabyssball( *sd, sd->abyssball );
  8278. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  8279. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  8280. if (src)
  8281. switch (src->type) {
  8282. case BL_MOB:
  8283. {
  8284. struct mob_data *md=(struct mob_data *)src;
  8285. if(md->target_id==sd->bl.id)
  8286. mob_unlocktarget(md,tick);
  8287. if(battle_config.mobs_level_up && md->status.hp &&
  8288. (unsigned int)md->level < pc_maxbaselv(sd) &&
  8289. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  8290. ) { // monster level up [Valaris]
  8291. clif_misceffect(&md->bl,0);
  8292. md->level++;
  8293. status_calc_mob(md, SCO_NONE);
  8294. status_percent_heal(src,10,0);
  8295. if( battle_config.show_mob_info&4 )
  8296. {// update name with new level
  8297. clif_name_area(&md->bl);
  8298. }
  8299. }
  8300. src = battle_get_master(src); // Maybe Player Summon
  8301. }
  8302. break;
  8303. case BL_PET: //Pass on to master...
  8304. case BL_HOM:
  8305. case BL_MER:
  8306. src = battle_get_master(src);
  8307. break;
  8308. }
  8309. if (src && src->type == BL_PC) {
  8310. struct map_session_data *ssd = (struct map_session_data *)src;
  8311. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  8312. npc_script_event(ssd, NPCE_KILLPC);
  8313. if (battle_config.pk_mode&2) {
  8314. ssd->status.manner -= 5;
  8315. if(ssd->status.manner < 0)
  8316. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  8317. #if 0
  8318. // PK/Karma system code (not enabled yet) [celest]
  8319. // originally from Kade Online, so i don't know if any of these is correct ^^;
  8320. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  8321. // karma going down = more 'good' / more honourable.
  8322. // The Karma System way...
  8323. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  8324. sd->status.karma--;
  8325. ssd->status.karma--;
  8326. }
  8327. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  8328. ssd->status.karma++;
  8329. // or the PK System way...
  8330. if (sd->status.karma > 0) // player killed is dishonourable?
  8331. ssd->status.karma--; // honour points earned
  8332. sd->status.karma++; // honour points lost
  8333. // To-do: Receive exp on certain occasions
  8334. #endif
  8335. }
  8336. }
  8337. if(battle_config.bone_drop==2
  8338. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  8339. {
  8340. struct item item_tmp;
  8341. memset(&item_tmp,0,sizeof(item_tmp));
  8342. item_tmp.nameid=ITEMID_SKULL_;
  8343. item_tmp.identify=1;
  8344. item_tmp.card[0]=CARD0_CREATE;
  8345. item_tmp.card[1]=0;
  8346. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  8347. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  8348. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  8349. }
  8350. //Remove bonus_script when dead
  8351. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  8352. // changed penalty options, added death by player if pk_mode [Valaris]
  8353. if(battle_config.death_penalty_type
  8354. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  8355. && !sd->sc.cant.deathpenalty
  8356. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  8357. {
  8358. t_exp base_penalty = 0;
  8359. t_exp job_penalty = 0;
  8360. uint32 zeny_penalty = 0;
  8361. if (pc_isvip(sd)) { // EXP penalty for VIP
  8362. base_penalty = battle_config.vip_exp_penalty_base;
  8363. job_penalty = battle_config.vip_exp_penalty_job;
  8364. zeny_penalty = battle_config.vip_zeny_penalty;
  8365. } else {
  8366. base_penalty = battle_config.death_penalty_base;
  8367. job_penalty = battle_config.death_penalty_job;
  8368. zeny_penalty = battle_config.zeny_penalty;
  8369. }
  8370. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  8371. switch (battle_config.death_penalty_type) {
  8372. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  8373. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  8374. }
  8375. if (base_penalty){ //recheck after altering to speedup
  8376. if (battle_config.pk_mode && src && src->type==BL_PC)
  8377. base_penalty *= 2;
  8378. base_penalty = u64min(sd->status.base_exp, base_penalty);
  8379. }
  8380. }
  8381. else
  8382. base_penalty = 0;
  8383. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  8384. switch (battle_config.death_penalty_type) {
  8385. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  8386. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  8387. }
  8388. if (job_penalty) {
  8389. if (battle_config.pk_mode && src && src->type==BL_PC)
  8390. job_penalty *= 2;
  8391. job_penalty = u64min(sd->status.job_exp, job_penalty);
  8392. }
  8393. }
  8394. else
  8395. job_penalty = 0;
  8396. if (base_penalty || job_penalty) {
  8397. short insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  8398. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  8399. pc_lostexp(sd, base_penalty, job_penalty);
  8400. }
  8401. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  8402. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  8403. if(zeny_penalty)
  8404. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  8405. }
  8406. }
  8407. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  8408. for (const auto &it : mapdata->drop_list) {
  8409. int id = it.drop_id, per = it.drop_per;
  8410. enum e_nightmare_drop_type type = it.drop_type;
  8411. if(id == 0)
  8412. continue;
  8413. if(id == -1){
  8414. int eq_num=0,eq_n[MAX_INVENTORY];
  8415. memset(eq_n,0,sizeof(eq_n));
  8416. for(i=0;i<MAX_INVENTORY;i++) {
  8417. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8418. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8419. || type&NMDT_ALL)
  8420. {
  8421. int l;
  8422. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  8423. if( l < MAX_INVENTORY )
  8424. eq_n[l] = i;
  8425. eq_num++;
  8426. }
  8427. }
  8428. if(eq_num > 0){
  8429. int n = eq_n[rnd()%eq_num];
  8430. if(rnd()%10000 < per) {
  8431. if(sd->inventory.u.items_inventory[n].equip)
  8432. pc_unequipitem(sd,n,3);
  8433. pc_dropitem(sd,n,1);
  8434. }
  8435. }
  8436. }
  8437. else if(id > 0) {
  8438. for(i=0;i<MAX_INVENTORY;i++){
  8439. if(sd->inventory.u.items_inventory[i].nameid == id
  8440. && rnd()%10000 < per
  8441. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8442. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8443. || type&NMDT_ALL) ){
  8444. if(sd->inventory.u.items_inventory[i].equip)
  8445. pc_unequipitem(sd,i,3);
  8446. pc_dropitem(sd,i,1);
  8447. break;
  8448. }
  8449. }
  8450. }
  8451. }
  8452. }
  8453. // pvp
  8454. // disable certain pvp functions on pk_mode [Valaris]
  8455. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  8456. sd->pvp_point -= 5;
  8457. sd->pvp_lost++;
  8458. if( src && src->type == BL_PC ) {
  8459. struct map_session_data *ssd = (struct map_session_data *)src;
  8460. ssd->pvp_point++;
  8461. ssd->pvp_won++;
  8462. }
  8463. if( sd->pvp_point < 0 ) {
  8464. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  8465. return 1|8;
  8466. }
  8467. }
  8468. //GvG
  8469. if( mapdata_flag_gvg2(mapdata) ) {
  8470. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  8471. return 1|8;
  8472. }
  8473. else if( sd->bg_id ) {
  8474. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  8475. if (bg) {
  8476. if (bg->cemetery.map > 0) { // Respawn by BG
  8477. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  8478. return 1|8;
  8479. }
  8480. }
  8481. }
  8482. //Reset "can log out" tick.
  8483. if( battle_config.prevent_logout )
  8484. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  8485. return 1;
  8486. }
  8487. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp, unsigned int ap) {
  8488. if(hp) clif_updatestatus(sd,SP_HP);
  8489. if(sp) clif_updatestatus(sd,SP_SP);
  8490. if(ap) clif_updatestatus(sd,SP_AP);
  8491. pc_setstand(sd, true);
  8492. if(battle_config.pc_invincible_time > 0)
  8493. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8494. if( sd->state.gmaster_flag ) {
  8495. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  8496. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  8497. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  8498. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  8499. }
  8500. }
  8501. bool pc_revive_item(struct map_session_data *sd) {
  8502. nullpo_retr(false, sd);
  8503. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  8504. return false;
  8505. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  8506. return false;
  8507. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  8508. uint8 hp = 100, sp = 100;
  8509. if (item_position < 0) {
  8510. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  8511. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  8512. sp = 0;
  8513. }
  8514. else
  8515. return false;
  8516. }
  8517. if (!status_revive(&sd->bl, hp, sp))
  8518. return false;
  8519. if (item_position < 0)
  8520. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE);
  8521. else
  8522. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  8523. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  8524. return true;
  8525. }
  8526. // script
  8527. //
  8528. /*==========================================
  8529. * script reading pc status registry
  8530. *------------------------------------------*/
  8531. int64 pc_readparam(struct map_session_data* sd,int64 type)
  8532. {
  8533. int64 val = 0;
  8534. nullpo_ret(sd);
  8535. switch(type) {
  8536. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  8537. case SP_STATUSPOINT: val = sd->status.status_point; break;
  8538. case SP_TRAITPOINT: val = sd->status.trait_point; break;
  8539. case SP_ZENY: val = sd->status.zeny; break;
  8540. case SP_BASELEVEL: val = sd->status.base_level; break;
  8541. case SP_JOBLEVEL: val = sd->status.job_level; break;
  8542. case SP_CLASS: val = sd->status.class_; break;
  8543. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  8544. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  8545. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  8546. case SP_SEX: val = sd->status.sex; break;
  8547. case SP_WEIGHT: val = sd->weight; break;
  8548. case SP_MAXWEIGHT: val = sd->max_weight; break;
  8549. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  8550. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  8551. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  8552. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  8553. case SP_HP: val = sd->battle_status.hp; break;
  8554. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  8555. case SP_SP: val = sd->battle_status.sp; break;
  8556. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  8557. case SP_AP: val = sd->battle_status.ap; break;
  8558. case SP_MAXAP: val = sd->battle_status.max_ap; break;
  8559. case SP_STR: val = sd->status.str; break;
  8560. case SP_AGI: val = sd->status.agi; break;
  8561. case SP_VIT: val = sd->status.vit; break;
  8562. case SP_INT: val = sd->status.int_; break;
  8563. case SP_DEX: val = sd->status.dex; break;
  8564. case SP_LUK: val = sd->status.luk; break;
  8565. case SP_POW: val = sd->status.pow; break;
  8566. case SP_STA: val = sd->status.sta; break;
  8567. case SP_WIS: val = sd->status.wis; break;
  8568. case SP_SPL: val = sd->status.spl; break;
  8569. case SP_CON: val = sd->status.con; break;
  8570. case SP_CRT: val = sd->status.crt; break;
  8571. case SP_KARMA: val = sd->status.karma; break;
  8572. case SP_MANNER: val = sd->status.manner; break;
  8573. case SP_FAME: val = sd->status.fame; break;
  8574. case SP_KILLERRID: val = sd->killerrid; break;
  8575. case SP_KILLEDRID: val = sd->killedrid; break;
  8576. case SP_KILLEDGID: val = sd->killedgid; break;
  8577. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  8578. case SP_CHARMOVE: val = sd->status.character_moves; break;
  8579. case SP_CHARRENAME: val = sd->status.rename; break;
  8580. case SP_CHARFONT: val = sd->status.font; break;
  8581. case SP_BANK_VAULT: val = sd->bank_vault; break;
  8582. case SP_CASHPOINTS: val = sd->cashPoints; break;
  8583. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  8584. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  8585. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  8586. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  8587. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  8588. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  8589. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  8590. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  8591. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  8592. case SP_BASE_ATK:
  8593. #ifdef RENEWAL
  8594. val = sd->bonus.eatk;
  8595. #else
  8596. val = sd->battle_status.batk;
  8597. #endif
  8598. break;
  8599. case SP_DEF1: val = sd->battle_status.def; break;
  8600. case SP_DEF2: val = sd->battle_status.def2; break;
  8601. case SP_MDEF1: val = sd->battle_status.mdef; break;
  8602. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  8603. case SP_HIT: val = sd->battle_status.hit; break;
  8604. case SP_FLEE1: val = sd->battle_status.flee; break;
  8605. case SP_FLEE2: val = sd->battle_status.flee2; break;
  8606. case SP_PATK: val = sd->battle_status.patk; break;
  8607. case SP_SMATK: val = sd->battle_status.smatk; break;
  8608. case SP_RES: val = sd->battle_status.res; break;
  8609. case SP_MRES: val = sd->battle_status.mres; break;
  8610. case SP_HPLUS: val = sd->battle_status.hplus; break;
  8611. case SP_CRATE: val = sd->battle_status.crate; break;
  8612. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  8613. case SP_MAXHPRATE: val = sd->hprate; break;
  8614. case SP_MAXSPRATE: val = sd->sprate; break;
  8615. case SP_MAXAPRATE: val = sd->aprate; break;
  8616. case SP_SPRATE: val = sd->dsprate; break;
  8617. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  8618. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  8619. case SP_ASPD_RATE:
  8620. #ifndef RENEWAL_ASPD
  8621. val = sd->battle_status.aspd_rate;
  8622. #else
  8623. val = sd->battle_status.aspd_rate2;
  8624. #endif
  8625. break;
  8626. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  8627. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  8628. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  8629. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  8630. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  8631. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  8632. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  8633. case SP_MATK_RATE: val = sd->matk_rate; break;
  8634. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  8635. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  8636. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  8637. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  8638. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  8639. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  8640. case SP_HIT_RATE: val = sd->hit_rate; break;
  8641. case SP_FLEE_RATE: val = sd->flee_rate; break;
  8642. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  8643. case SP_DEF_RATE: val = sd->def_rate; break;
  8644. case SP_DEF2_RATE: val = sd->def2_rate; break;
  8645. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  8646. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  8647. case SP_PATK_RATE: val = sd->patk_rate; break;
  8648. case SP_SMATK_RATE: val = sd->smatk_rate; break;
  8649. case SP_RES_RATE: val = sd->res_rate; break;
  8650. case SP_MRES_RATE: val = sd->mres_rate; break;
  8651. case SP_HPLUS_RATE: val = sd->hplus_rate; break;
  8652. case SP_CRATE_RATE: val = sd->crate_rate; break;
  8653. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  8654. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  8655. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  8656. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  8657. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  8658. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  8659. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  8660. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  8661. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  8662. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  8663. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  8664. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  8665. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  8666. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  8667. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  8668. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  8669. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  8670. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  8671. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  8672. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  8673. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  8674. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  8675. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  8676. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  8677. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  8678. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  8679. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  8680. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  8681. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  8682. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  8683. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  8684. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  8685. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  8686. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  8687. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  8688. case SP_UNSTRIPABLE:
  8689. case SP_UNSTRIPABLE_ARMOR:
  8690. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  8691. break;
  8692. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  8693. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  8694. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  8695. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  8696. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  8697. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  8698. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  8699. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  8700. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  8701. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  8702. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  8703. case SP_EMATK: val = sd->bonus.ematk; break;
  8704. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  8705. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  8706. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  8707. case SP_CASTRATE:
  8708. case SP_VARCASTRATE:
  8709. #ifdef RENEWAL_CAST
  8710. val = sd->bonus.varcastrate; break;
  8711. #else
  8712. val = sd->castrate; break;
  8713. #endif
  8714. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  8715. case SP_ADD_ITEM_SPHEAL_RATE: val = sd->bonus.itemsphealrate2; break;
  8716. default:
  8717. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  8718. return -1;
  8719. }
  8720. return val;
  8721. }
  8722. /*==========================================
  8723. * script set pc status registry
  8724. *------------------------------------------*/
  8725. bool pc_setparam(struct map_session_data *sd,int64 type,int64 val_tmp)
  8726. {
  8727. nullpo_retr(false,sd);
  8728. int val = static_cast<unsigned int>(val_tmp);
  8729. switch(type){
  8730. case SP_BASELEVEL:
  8731. if (val > pc_maxbaselv(sd)) //Capping to max
  8732. val = pc_maxbaselv(sd);
  8733. if (val > sd->status.base_level) {
  8734. for( int i = 0; i < (int)( val - sd->status.base_level ); i++ ){
  8735. sd->status.status_point += statpoint_db.pc_gets_status_point( sd->status.base_level + i );
  8736. sd->status.trait_point += statpoint_db.pc_gets_trait_point( sd->status.base_level + i );
  8737. }
  8738. }
  8739. sd->status.base_level = val;
  8740. sd->status.base_exp = 0;
  8741. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  8742. clif_updatestatus(sd, SP_NEXTBASEEXP);
  8743. clif_updatestatus(sd, SP_STATUSPOINT);
  8744. clif_updatestatus(sd, SP_TRAITPOINT);
  8745. clif_updatestatus(sd, SP_BASEEXP);
  8746. status_calc_pc(sd, SCO_FORCE);
  8747. if(sd->status.party_id)
  8748. party_send_levelup(sd);
  8749. break;
  8750. case SP_JOBLEVEL:
  8751. if (val >= sd->status.job_level) {
  8752. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  8753. sd->status.skill_point += val - sd->status.job_level;
  8754. clif_updatestatus(sd, SP_SKILLPOINT);
  8755. }
  8756. sd->status.job_level = val;
  8757. sd->status.job_exp = 0;
  8758. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  8759. clif_updatestatus(sd, SP_NEXTJOBEXP);
  8760. clif_updatestatus(sd, SP_JOBEXP);
  8761. status_calc_pc(sd, SCO_FORCE);
  8762. break;
  8763. case SP_SKILLPOINT:
  8764. sd->status.skill_point = val;
  8765. break;
  8766. case SP_STATUSPOINT:
  8767. sd->status.status_point = val;
  8768. break;
  8769. case SP_TRAITPOINT:
  8770. sd->status.trait_point = val;
  8771. break;
  8772. case SP_ZENY:
  8773. if( val < 0 )
  8774. return false;// can't set negative zeny
  8775. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  8776. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  8777. break;
  8778. case SP_BASEEXP:
  8779. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  8780. if (val_tmp < sd->status.base_exp) // Lost
  8781. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  8782. else // Gained
  8783. pc_gainexp(sd, NULL, val_tmp - sd->status.base_exp, 0, 2);
  8784. return true;
  8785. case SP_JOBEXP:
  8786. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  8787. if (val_tmp < sd->status.job_exp) // Lost
  8788. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  8789. else // Gained
  8790. pc_gainexp(sd, NULL, 0, val_tmp - sd->status.job_exp, 2);
  8791. return true;
  8792. case SP_SEX:
  8793. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  8794. break;
  8795. case SP_WEIGHT:
  8796. sd->weight = val;
  8797. break;
  8798. case SP_MAXWEIGHT:
  8799. sd->max_weight = val;
  8800. break;
  8801. case SP_HP:
  8802. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  8803. break;
  8804. case SP_MAXHP:
  8805. if (sd->status.base_level < 100)
  8806. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  8807. else if (sd->status.base_level < 151)
  8808. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  8809. else
  8810. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  8811. if( sd->battle_status.max_hp < sd->battle_status.hp )
  8812. {
  8813. sd->battle_status.hp = sd->battle_status.max_hp;
  8814. clif_updatestatus(sd, SP_HP);
  8815. }
  8816. break;
  8817. case SP_SP:
  8818. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  8819. break;
  8820. case SP_MAXSP:
  8821. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  8822. if( sd->battle_status.max_sp < sd->battle_status.sp )
  8823. {
  8824. sd->battle_status.sp = sd->battle_status.max_sp;
  8825. clif_updatestatus(sd, SP_SP);
  8826. }
  8827. break;
  8828. case SP_AP:
  8829. sd->battle_status.ap = cap_value(val, 0, (int)sd->battle_status.max_ap);
  8830. break;
  8831. case SP_MAXAP:
  8832. sd->battle_status.max_ap = cap_value(val, 1, battle_config.max_ap);
  8833. if (sd->battle_status.max_ap < sd->battle_status.ap) {
  8834. sd->battle_status.ap = sd->battle_status.max_ap;
  8835. clif_updatestatus(sd, SP_AP);
  8836. }
  8837. break;
  8838. case SP_STR:
  8839. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  8840. break;
  8841. case SP_AGI:
  8842. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  8843. break;
  8844. case SP_VIT:
  8845. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  8846. break;
  8847. case SP_INT:
  8848. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  8849. break;
  8850. case SP_DEX:
  8851. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  8852. break;
  8853. case SP_LUK:
  8854. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  8855. break;
  8856. case SP_POW:
  8857. sd->status.pow = cap_value(val, 0, pc_maxparameter(sd,PARAM_POW));
  8858. break;
  8859. case SP_STA:
  8860. sd->status.sta = cap_value(val, 0, pc_maxparameter(sd,PARAM_STA));
  8861. break;
  8862. case SP_WIS:
  8863. sd->status.wis = cap_value(val, 0, pc_maxparameter(sd,PARAM_WIS));
  8864. break;
  8865. case SP_SPL:
  8866. sd->status.spl = cap_value(val, 0, pc_maxparameter(sd,PARAM_SPL));
  8867. break;
  8868. case SP_CON:
  8869. sd->status.con = cap_value(val, 0, pc_maxparameter(sd,PARAM_CON));
  8870. break;
  8871. case SP_CRT:
  8872. sd->status.crt = cap_value(val, 0, pc_maxparameter(sd,PARAM_CRT));
  8873. break;
  8874. case SP_KARMA:
  8875. sd->status.karma = val;
  8876. break;
  8877. case SP_MANNER:
  8878. sd->status.manner = val;
  8879. if( val < 0 )
  8880. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  8881. else {
  8882. status_change_end(&sd->bl, SC_NOCHAT);
  8883. clif_manner_message(sd, 5);
  8884. }
  8885. return true; // status_change_start/status_change_end already sends packets warning the client
  8886. case SP_FAME:
  8887. sd->status.fame = val;
  8888. break;
  8889. case SP_KILLERRID:
  8890. sd->killerrid = val;
  8891. return true;
  8892. case SP_KILLEDRID:
  8893. sd->killedrid = val;
  8894. return true;
  8895. case SP_KILLEDGID:
  8896. sd->killedgid = val;
  8897. return true;
  8898. case SP_CHARMOVE:
  8899. sd->status.character_moves = val;
  8900. return true;
  8901. case SP_CHARRENAME:
  8902. sd->status.rename = val;
  8903. return true;
  8904. case SP_CHARFONT:
  8905. sd->status.font = val;
  8906. clif_font(sd);
  8907. return true;
  8908. case SP_BANK_VAULT:
  8909. if (val < 0)
  8910. return false;
  8911. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  8912. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  8913. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  8914. return true;
  8915. case SP_ROULETTE_BRONZE:
  8916. sd->roulette_point.bronze = val;
  8917. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  8918. return true;
  8919. case SP_ROULETTE_SILVER:
  8920. sd->roulette_point.silver = val;
  8921. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  8922. return true;
  8923. case SP_ROULETTE_GOLD:
  8924. sd->roulette_point.gold = val;
  8925. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  8926. return true;
  8927. case SP_CASHPOINTS:
  8928. if (val < 0)
  8929. return false;
  8930. if (!sd->state.connect_new)
  8931. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  8932. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  8933. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  8934. return true;
  8935. case SP_KAFRAPOINTS:
  8936. if (val < 0)
  8937. return false;
  8938. if (!sd->state.connect_new)
  8939. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  8940. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  8941. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  8942. return true;
  8943. case SP_PCDIECOUNTER:
  8944. if (val < 0)
  8945. return false;
  8946. if (sd->die_counter == val)
  8947. return true;
  8948. sd->die_counter = val;
  8949. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  8950. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  8951. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  8952. return true;
  8953. case SP_COOKMASTERY:
  8954. if (sd->cook_mastery == val)
  8955. return true;
  8956. val = cap_value(val, 0, 1999);
  8957. sd->cook_mastery = val;
  8958. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  8959. return true;
  8960. default:
  8961. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  8962. return false;
  8963. }
  8964. clif_updatestatus(sd,static_cast<int>(type));
  8965. return true;
  8966. }
  8967. /*==========================================
  8968. * HP/SP/AP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  8969. *------------------------------------------*/
  8970. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, unsigned int ap, int type)
  8971. {// Is there going to be a effect for gaining AP soon??? [Rytech]
  8972. nullpo_retv(sd);
  8973. if (type&2) {
  8974. if (hp || type&4) {
  8975. clif_heal(sd->fd,SP_HP,hp);
  8976. clif_update_hp(*sd);
  8977. }
  8978. if (sp)
  8979. clif_heal(sd->fd,SP_SP,sp);
  8980. if (ap)
  8981. clif_heal(sd->fd,SP_AP,ap);
  8982. } else {
  8983. if(hp)
  8984. clif_updatestatus(sd,SP_HP);
  8985. if(sp)
  8986. clif_updatestatus(sd,SP_SP);
  8987. if (ap)
  8988. clif_updatestatus(sd,SP_AP);
  8989. }
  8990. return;
  8991. }
  8992. /**
  8993. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  8994. * @param sd: Player data
  8995. * @param itemid: Item ID
  8996. * @param hp: HP to heal
  8997. * @param sp: SP to heal
  8998. * @return Amount healed to an object
  8999. */
  9000. int pc_itemheal(struct map_session_data *sd, t_itemid itemid, int hp, int sp)
  9001. {
  9002. int bonus, tmp, penalty = 0;
  9003. if (hp) {
  9004. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9005. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9006. if (potion_flag == 2) {
  9007. bonus += bonus * 50 / 100;
  9008. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  9009. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  9010. }
  9011. //All item bonuses.
  9012. bonus += sd->bonus.itemhealrate2;
  9013. //Item Group bonuses
  9014. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid, sd->itemgrouphealrate) / 100;
  9015. //Individual item bonuses.
  9016. for(const auto &it : sd->itemhealrate) {
  9017. if (it.id == itemid) {
  9018. bonus += bonus * it.val / 100;
  9019. break;
  9020. }
  9021. }
  9022. // Recovery Potion
  9023. if (sd->sc.data[SC_INCHEALRATE])
  9024. bonus += bonus * sd->sc.data[SC_INCHEALRATE]->val1 / 100;
  9025. // 2014 Halloween Event : Pumpkin Bonus
  9026. if (sd->sc.data[SC_MTF_PUMPKIN]) {
  9027. if (itemid == ITEMID_PUMPKIN)
  9028. bonus += bonus * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100;
  9029. else if (itemid == ITEMID_COOKIE_BAT)
  9030. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val2;
  9031. }
  9032. if (sd->sc.data[SC_VITALIZE_POTION])
  9033. bonus += bonus * 10 / 100;
  9034. tmp = hp * bonus / 100; // Overflow check
  9035. if (bonus != 100 && tmp > hp)
  9036. hp = tmp;
  9037. }
  9038. if (sp) {
  9039. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9040. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9041. if (potion_flag == 2)
  9042. bonus += bonus * 50 / 100;
  9043. // All item bonuses.
  9044. bonus += sd->bonus.itemsphealrate2;
  9045. // Item Group bonuses
  9046. bonus += bonus * pc_get_itemgroup_bonus( sd, itemid, sd->itemgroupsphealrate ) / 100;
  9047. // Individual item bonuses.
  9048. for( const auto &it : sd->itemsphealrate ){
  9049. if( it.id == itemid ){
  9050. bonus += bonus * it.val / 100;
  9051. break;
  9052. }
  9053. }
  9054. tmp = sp * bonus / 100; // Overflow check
  9055. if (bonus != 100 && tmp > sp)
  9056. sp = tmp;
  9057. }
  9058. if (sd->sc.count) {
  9059. // Critical Wound and Death Hurt stack
  9060. if (sd->sc.data[SC_CRITICALWOUND])
  9061. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  9062. if (sd->sc.data[SC_DEATHHURT] && sd->sc.data[SC_DEATHHURT]->val3 == 1)
  9063. penalty += 20;
  9064. if (sd->sc.data[SC_NORECOVER_STATE])
  9065. penalty = 100;
  9066. if (sd->sc.data[SC_VITALITYACTIVATION])
  9067. hp += hp / 2; // 1.5 times
  9068. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  9069. hp += hp / 10;
  9070. sp += sp / 10;
  9071. }
  9072. #ifdef RENEWAL
  9073. if (sd->sc.data[SC_APPLEIDUN])
  9074. hp += sd->sc.data[SC_APPLEIDUN]->val3 / 100;
  9075. #endif
  9076. if (penalty > 0) {
  9077. hp -= hp * penalty / 100;
  9078. sp -= sp * penalty / 100;
  9079. }
  9080. #ifdef RENEWAL
  9081. if (sd->sc.data[SC_EXTREMITYFIST2])
  9082. sp = 0;
  9083. #endif
  9084. if (sd->sc.data[SC_BITESCAR])
  9085. hp = 0;
  9086. }
  9087. return status_heal(&sd->bl, hp, sp, 1);
  9088. }
  9089. /*==========================================
  9090. * HP/SP Recovery
  9091. * Heal player hp nad/or sp by rate
  9092. *------------------------------------------*/
  9093. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  9094. {
  9095. nullpo_ret(sd);
  9096. if (hp > 100) hp = 100;
  9097. else if (hp <-100) hp = -100;
  9098. if (sp > 100) sp = 100;
  9099. else if (sp <-100) sp = -100;
  9100. if(hp >= 0 && sp >= 0) //Heal
  9101. return status_percent_heal(&sd->bl, hp, sp);
  9102. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  9103. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  9104. //Crossed signs
  9105. if(hp) {
  9106. if(hp > 0)
  9107. status_percent_heal(&sd->bl, hp, 0);
  9108. else
  9109. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  9110. }
  9111. if(sp) {
  9112. if(sp > 0)
  9113. status_percent_heal(&sd->bl, 0, sp);
  9114. else
  9115. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  9116. }
  9117. return 0;
  9118. }
  9119. static int jobchange_killclone(struct block_list *bl, va_list ap)
  9120. {
  9121. struct mob_data *md;
  9122. int flag;
  9123. md = (struct mob_data *)bl;
  9124. nullpo_ret(md);
  9125. flag = va_arg(ap, int);
  9126. if (md->master_id && md->special_state.clone && md->master_id == flag)
  9127. status_kill(&md->bl);
  9128. return 1;
  9129. }
  9130. /**
  9131. * Called when player changes job
  9132. * Rewrote to make it tidider [Celest]
  9133. * @param sd
  9134. * @param job JOB ID. See enum e_job
  9135. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  9136. * @return True if success, false if failed
  9137. **/
  9138. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  9139. {
  9140. int i, fame_flag = 0;
  9141. nullpo_retr(false,sd);
  9142. if (job < 0)
  9143. return false;
  9144. //Normalize job.
  9145. uint64 b_class = pc_jobid2mapid(job);
  9146. if (b_class == -1)
  9147. return false;
  9148. switch (upper) {
  9149. case 1:
  9150. b_class|= JOBL_UPPER;
  9151. break;
  9152. case 2:
  9153. b_class|= JOBL_BABY;
  9154. break;
  9155. }
  9156. //This will automatically adjust bard/dancer classes to the correct gender
  9157. //That is, if you try to jobchange into dancer, it will turn you to bard.
  9158. job = pc_mapid2jobid(b_class, sd->status.sex);
  9159. if (job == -1)
  9160. return false;
  9161. if ((unsigned short)b_class == sd->class_)
  9162. return false; //Nothing to change.
  9163. // If the job does not exist in the job db, dont allow changing to it
  9164. if( !job_db.exists( job ) ){
  9165. return false;
  9166. }
  9167. if( ( b_class&JOBL_FOURTH ) && !( sd->class_&JOBL_FOURTH ) ){
  9168. // Changing to 4th job
  9169. sd->change_level_4th = sd->status.job_level;
  9170. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ) , sd->change_level_4th );
  9171. }else if( ( b_class&JOBL_THIRD ) && !( sd->class_&JOBL_THIRD ) ){
  9172. // changing from 2nd to 3rd job
  9173. sd->change_level_3rd = sd->status.job_level;
  9174. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  9175. }else if( ( b_class&JOBL_2 ) && !( sd->class_&JOBL_2 ) ){
  9176. // changing from 1st to 2nd job
  9177. sd->change_level_2nd = sd->status.job_level;
  9178. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  9179. }
  9180. if(sd->cloneskill_idx > 0) {
  9181. pc_skill_plagiarism_reset(*sd, 1);
  9182. }
  9183. if(sd->reproduceskill_idx > 0) {
  9184. pc_skill_plagiarism_reset(*sd, 2);
  9185. }
  9186. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  9187. status_db.changeSkillTree(sd);
  9188. }
  9189. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  9190. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  9191. pc_resetfeel(sd);
  9192. pc_resethate(sd);
  9193. }
  9194. // Reset body style to 0 before changing job to avoid
  9195. // errors since not every job has a alternate outfit.
  9196. sd->status.body = 0;
  9197. clif_changelook(&sd->bl,LOOK_BODY2,0);
  9198. sd->status.class_ = job;
  9199. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  9200. uint64 previous_class = sd->class_;
  9201. sd->class_ = (unsigned short)b_class;
  9202. sd->status.job_level=1;
  9203. sd->status.job_exp=0;
  9204. if (sd->status.base_level > pc_maxbaselv(sd)) {
  9205. sd->status.base_level = pc_maxbaselv(sd);
  9206. sd->status.base_exp=0;
  9207. pc_resetstate(sd);
  9208. clif_updatestatus(sd,SP_STATUSPOINT);
  9209. clif_updatestatus(sd,SP_TRAITPOINT);
  9210. clif_updatestatus(sd,SP_BASELEVEL);
  9211. clif_updatestatus(sd,SP_BASEEXP);
  9212. clif_updatestatus(sd,SP_NEXTBASEEXP);
  9213. }
  9214. // Give or reduce transcendent status points
  9215. if( (b_class&JOBL_UPPER) && !(previous_class&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  9216. sd->status.status_point += battle_config.transcendent_status_points;
  9217. clif_updatestatus(sd,SP_STATUSPOINT);
  9218. }else if( !(b_class&JOBL_UPPER) && (previous_class&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  9219. if( sd->status.status_point < battle_config.transcendent_status_points ){
  9220. // The player already used his bonus points, so we have to reset his status points
  9221. pc_resetstate(sd);
  9222. }else{
  9223. sd->status.status_point -= battle_config.transcendent_status_points;
  9224. clif_updatestatus(sd,SP_STATUSPOINT);
  9225. }
  9226. }
  9227. // Give or reduce trait status points
  9228. if ((b_class & JOBL_FOURTH) && !(previous_class & JOBL_FOURTH)) {// Change to a 4th job.
  9229. sd->status.trait_point += battle_config.trait_points_job_change;
  9230. clif_updatestatus(sd, SP_TRAITPOINT);
  9231. clif_updatestatus(sd, SP_UPOW);
  9232. clif_updatestatus(sd, SP_USTA);
  9233. clif_updatestatus(sd, SP_UWIS);
  9234. clif_updatestatus(sd, SP_USPL);
  9235. clif_updatestatus(sd, SP_UCON);
  9236. clif_updatestatus(sd, SP_UCRT);
  9237. } else if (!(b_class & JOBL_FOURTH) && (previous_class & JOBL_FOURTH)) {// Change to a non 4th job.
  9238. if (sd->status.trait_point < battle_config.trait_points_job_change) {
  9239. // Player may have already used the trait status points. Force a reset.
  9240. pc_resetstate(sd);
  9241. } else {
  9242. sd->status.trait_point = 0;
  9243. clif_updatestatus(sd, SP_TRAITPOINT);
  9244. clif_updatestatus(sd, SP_UPOW);
  9245. clif_updatestatus(sd, SP_USTA);
  9246. clif_updatestatus(sd, SP_UWIS);
  9247. clif_updatestatus(sd, SP_USPL);
  9248. clif_updatestatus(sd, SP_UCON);
  9249. clif_updatestatus(sd, SP_UCRT);
  9250. }
  9251. }
  9252. clif_updatestatus(sd,SP_JOBLEVEL);
  9253. clif_updatestatus(sd,SP_JOBEXP);
  9254. clif_updatestatus(sd,SP_NEXTJOBEXP);
  9255. for(i=0;i<EQI_MAX;i++) {
  9256. if(sd->equip_index[i] >= 0)
  9257. if(pc_isequip(sd,sd->equip_index[i]))
  9258. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  9259. }
  9260. //Change look, if disguised, you need to undisguise
  9261. //to correctly calculate new job sprite without
  9262. if (sd->disguise)
  9263. pc_disguise(sd, 0);
  9264. status_set_viewdata(&sd->bl, job);
  9265. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  9266. #if PACKETVER >= 20151001
  9267. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  9268. #endif
  9269. if(sd->vd.cloth_color)
  9270. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9271. /*
  9272. if(sd->vd.body_style)
  9273. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9274. */
  9275. //Update skill tree.
  9276. pc_calc_skilltree(sd);
  9277. clif_skillinfoblock(sd);
  9278. if (sd->ed)
  9279. elemental_delete(sd->ed);
  9280. if (sd->state.vending)
  9281. vending_closevending(sd);
  9282. if (sd->state.buyingstore)
  9283. buyingstore_close(sd);
  9284. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  9285. //Remove peco/cart/falcon
  9286. i = sd->sc.option;
  9287. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  9288. i&=~OPTION_RIDING;
  9289. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  9290. i&=~OPTION_FALCON;
  9291. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  9292. i&=~OPTION_DRAGON;
  9293. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  9294. i&=~OPTION_WUGRIDER;
  9295. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  9296. i&=~OPTION_WUG;
  9297. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  9298. i&=~OPTION_MADOGEAR;
  9299. #ifndef NEW_CARTS
  9300. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  9301. i&=~OPTION_CART;
  9302. #else
  9303. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  9304. pc_setcart(sd, 0);
  9305. #endif
  9306. if(i != sd->sc.option)
  9307. pc_setoption(sd, i);
  9308. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  9309. hom_vaporize(sd, HOM_ST_ACTIVE);
  9310. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  9311. status_change_end(&sd->bl, SC_SPRITEMABLE);
  9312. if (sd->sc.data[SC_SOULATTACK] && !pc_checkskill(sd, SU_SOULATTACK))
  9313. status_change_end(&sd->bl, SC_SOULATTACK);
  9314. if( sd->sc.data[SC_SPIRIT] ){
  9315. status_change_end( &sd->bl, SC_SPIRIT );
  9316. }
  9317. if(sd->status.manner < 0)
  9318. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  9319. status_calc_pc(sd,SCO_FORCE);
  9320. pc_checkallowskill(sd);
  9321. pc_equiplookall(sd);
  9322. pc_show_questinfo(sd);
  9323. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  9324. if( sd->status.party_id ){
  9325. struct party_data* p;
  9326. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  9327. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  9328. if( i < MAX_PARTY ){
  9329. p->party.member[i].class_ = sd->status.class_;
  9330. clif_party_job_and_level( *sd );
  9331. }
  9332. }
  9333. }
  9334. chrif_save(sd, CSAVE_NORMAL);
  9335. //if you were previously famous, not anymore.
  9336. if (fame_flag)
  9337. chrif_buildfamelist();
  9338. else if (sd->status.fame > 0) {
  9339. //It may be that now they are famous?
  9340. switch (sd->class_&MAPID_UPPERMASK) {
  9341. case MAPID_BLACKSMITH:
  9342. case MAPID_ALCHEMIST:
  9343. case MAPID_TAEKWON:
  9344. chrif_buildfamelist();
  9345. break;
  9346. }
  9347. }
  9348. return true;
  9349. }
  9350. /*==========================================
  9351. * Tell client player sd has change equipement
  9352. *------------------------------------------*/
  9353. void pc_equiplookall(struct map_session_data *sd)
  9354. {
  9355. nullpo_retv(sd);
  9356. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  9357. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9358. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  9359. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  9360. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  9361. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  9362. }
  9363. /*==========================================
  9364. * Tell client player sd has change look (hair,equip...)
  9365. *------------------------------------------*/
  9366. void pc_changelook(struct map_session_data *sd,int type,int val) {
  9367. nullpo_retv(sd);
  9368. switch(type) {
  9369. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  9370. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  9371. if (sd->status.hair != val) {
  9372. sd->status.hair = val;
  9373. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9374. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9375. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  9376. }
  9377. break;
  9378. case LOOK_WEAPON:
  9379. sd->status.weapon = val;
  9380. break;
  9381. case LOOK_HEAD_BOTTOM:
  9382. sd->status.head_bottom = val;
  9383. sd->setlook_head_bottom = val;
  9384. break;
  9385. case LOOK_HEAD_TOP:
  9386. sd->status.head_top = val;
  9387. sd->setlook_head_top = val;
  9388. break;
  9389. case LOOK_HEAD_MID:
  9390. sd->status.head_mid = val;
  9391. sd->setlook_head_mid = val;
  9392. break;
  9393. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  9394. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  9395. if (sd->status.hair_color != val) {
  9396. sd->status.hair_color = val;
  9397. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9398. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9399. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  9400. }
  9401. break;
  9402. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  9403. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  9404. sd->status.clothes_color = val;
  9405. break;
  9406. case LOOK_SHIELD:
  9407. sd->status.shield = val;
  9408. break;
  9409. case LOOK_SHOES:
  9410. break;
  9411. case LOOK_ROBE:
  9412. sd->status.robe = val;
  9413. sd->setlook_robe = val;
  9414. break;
  9415. case LOOK_BODY2:
  9416. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  9417. sd->status.body = val;
  9418. break;
  9419. }
  9420. clif_changelook(&sd->bl, type, val);
  9421. }
  9422. /*==========================================
  9423. * Give an option (type) to player (sd) and display it to client
  9424. *------------------------------------------*/
  9425. void pc_setoption(struct map_session_data *sd,int type, int subtype)
  9426. {
  9427. int p_type, new_look=0;
  9428. nullpo_retv(sd);
  9429. p_type = sd->sc.option;
  9430. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  9431. sd->sc.option=type;
  9432. clif_changeoption(&sd->bl);
  9433. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9434. { // Mounting
  9435. clif_status_load(&sd->bl,EFST_RIDING,1);
  9436. status_calc_pc(sd,SCO_NONE);
  9437. }
  9438. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9439. { // Dismount
  9440. clif_status_load(&sd->bl,EFST_RIDING,0);
  9441. status_calc_pc(sd,SCO_NONE);
  9442. }
  9443. #ifndef NEW_CARTS
  9444. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  9445. clif_cartlist(sd);
  9446. clif_updatestatus(sd, SP_CARTINFO);
  9447. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9448. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  9449. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  9450. clif_clearcart(sd->fd);
  9451. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9452. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  9453. }
  9454. #endif
  9455. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  9456. clif_status_load(&sd->bl,EFST_FALCON,1);
  9457. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  9458. clif_status_load(&sd->bl,EFST_FALCON,0);
  9459. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  9460. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  9461. status_calc_pc(sd,SCO_NONE);
  9462. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  9463. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  9464. status_calc_pc(sd,SCO_NONE);
  9465. }
  9466. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  9467. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  9468. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  9469. status_change_end(&sd->bl, SC_MADOGEAR);
  9470. }
  9471. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  9472. new_look = JOB_STAR_GLADIATOR2;
  9473. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  9474. new_look = -1;
  9475. if (sd->disguise || !new_look)
  9476. return; //Disguises break sprite changes
  9477. if (new_look < 0) { //Restore normal look.
  9478. status_set_viewdata(&sd->bl, sd->status.class_);
  9479. new_look = sd->vd.class_;
  9480. }
  9481. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  9482. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  9483. if (sd->vd.cloth_color)
  9484. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9485. if( sd->vd.body_style )
  9486. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9487. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  9488. }
  9489. /**
  9490. * Give player a cart
  9491. * @param sd Player
  9492. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  9493. **/
  9494. bool pc_setcart(struct map_session_data *sd,int type) {
  9495. #ifndef NEW_CARTS
  9496. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  9497. int option;
  9498. #endif
  9499. nullpo_retr(false,sd);
  9500. if( type < 0 || type > MAX_CARTS )
  9501. return false;// Never trust the values sent by the client! [Skotlex]
  9502. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  9503. return false;// Push cart is required
  9504. #ifdef NEW_CARTS
  9505. switch( type ) {
  9506. case 0:
  9507. if( !sd->sc.data[SC_PUSH_CART] )
  9508. return 0;
  9509. status_change_end(&sd->bl,SC_PUSH_CART);
  9510. clif_clearcart(sd->fd);
  9511. break;
  9512. default:/* everything else is an allowed ID so we can move on */
  9513. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  9514. clif_cartlist(sd);
  9515. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  9516. }
  9517. clif_updatestatus(sd, SP_CARTINFO);
  9518. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  9519. break;
  9520. }
  9521. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9522. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  9523. #else
  9524. // Update option
  9525. option = sd->sc.option;
  9526. option &= ~OPTION_CART;// clear cart bits
  9527. option |= cart[type]; // set cart
  9528. pc_setoption(sd, option);
  9529. #endif
  9530. return true;
  9531. }
  9532. /*==========================================
  9533. * Give player a falcon
  9534. *------------------------------------------*/
  9535. void pc_setfalcon(struct map_session_data* sd, int flag)
  9536. {
  9537. if( flag ){
  9538. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  9539. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  9540. } else if( pc_isfalcon(sd) ){
  9541. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  9542. }
  9543. }
  9544. /*==========================================
  9545. * Set player riding
  9546. *------------------------------------------*/
  9547. void pc_setriding(struct map_session_data* sd, int flag)
  9548. {
  9549. if( sd->sc.data[SC_ALL_RIDING] )
  9550. return;
  9551. if( flag ){
  9552. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  9553. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  9554. } else if( pc_isriding(sd) ){
  9555. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  9556. }
  9557. }
  9558. /**
  9559. * Give player a mado
  9560. * @param sd: Player
  9561. * @param flag: Enable or disable mado
  9562. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  9563. */
  9564. void pc_setmadogear(struct map_session_data *sd, bool flag, e_mado_type type)
  9565. {
  9566. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  9567. return;
  9568. if (flag) {
  9569. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  9570. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  9571. } else if (pc_ismadogear(sd)) {
  9572. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  9573. }
  9574. }
  9575. /*==========================================
  9576. * Check if player can drop an item
  9577. *------------------------------------------*/
  9578. bool pc_candrop(struct map_session_data *sd, struct item *item)
  9579. {
  9580. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  9581. return false;
  9582. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  9583. return false;
  9584. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  9585. }
  9586. /*==========================================
  9587. * Read '@type' variables (temporary numeric char reg)
  9588. *------------------------------------------*/
  9589. int64 pc_readreg(struct map_session_data* sd, int64 reg)
  9590. {
  9591. return i64db_i64get(sd->regs.vars, reg);
  9592. }
  9593. /*==========================================
  9594. * Set '@type' variables (temporary numeric char reg)
  9595. *------------------------------------------*/
  9596. bool pc_setreg(struct map_session_data* sd, int64 reg, int64 val)
  9597. {
  9598. uint32 index = script_getvaridx(reg);
  9599. nullpo_retr(false, sd);
  9600. if( val ) {
  9601. i64db_i64put(sd->regs.vars, reg, val);
  9602. if( index )
  9603. script_array_update(&sd->regs, reg, false);
  9604. } else {
  9605. i64db_remove(sd->regs.vars, reg);
  9606. if( index )
  9607. script_array_update(&sd->regs, reg, true);
  9608. }
  9609. return true;
  9610. }
  9611. /*==========================================
  9612. * Read '@type$' variables (temporary string char reg)
  9613. *------------------------------------------*/
  9614. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  9615. {
  9616. struct script_reg_str *p = NULL;
  9617. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9618. return p ? p->value : NULL;
  9619. }
  9620. /*==========================================
  9621. * Set '@type$' variables (temporary string char reg)
  9622. *------------------------------------------*/
  9623. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  9624. {
  9625. struct script_reg_str *p = NULL;
  9626. unsigned int index = script_getvaridx(reg);
  9627. DBData prev;
  9628. nullpo_retr(false, sd);
  9629. if( str[0] ) {
  9630. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9631. p->value = aStrdup(str);
  9632. p->flag.type = 1;
  9633. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9634. p = (struct script_reg_str *)db_data2ptr(&prev);
  9635. if( p->value )
  9636. aFree(p->value);
  9637. ers_free(str_reg_ers, p);
  9638. } else {
  9639. if( index )
  9640. script_array_update(&sd->regs, reg, false);
  9641. }
  9642. } else {
  9643. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  9644. p = (struct script_reg_str *)db_data2ptr(&prev);
  9645. if( p->value )
  9646. aFree(p->value);
  9647. ers_free(str_reg_ers, p);
  9648. if( index )
  9649. script_array_update(&sd->regs, reg, true);
  9650. }
  9651. }
  9652. return true;
  9653. }
  9654. /**
  9655. * Serves the following variable types:
  9656. * - 'type' (permanent numeric char reg)
  9657. * - '#type' (permanent numeric account reg)
  9658. * - '##type' (permanent numeric account reg2)
  9659. **/
  9660. int64 pc_readregistry(struct map_session_data *sd, int64 reg)
  9661. {
  9662. struct script_reg_num *p = NULL;
  9663. if (!sd->vars_ok) {
  9664. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9665. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9666. //intif->request_registry(sd,type==3?4:type);
  9667. set_eof(sd->fd);
  9668. return 0;
  9669. }
  9670. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  9671. return p ? p->value : 0;
  9672. }
  9673. /**
  9674. * Serves the following variable types:
  9675. * - 'type$' (permanent str char reg)
  9676. * - '#type$' (permanent str account reg)
  9677. * - '##type$' (permanent str account reg2)
  9678. **/
  9679. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  9680. {
  9681. struct script_reg_str *p = NULL;
  9682. if (!sd->vars_ok) {
  9683. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9684. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9685. //intif->request_registry(sd,type==3?4:type);
  9686. set_eof(sd->fd);
  9687. return NULL;
  9688. }
  9689. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9690. return p ? p->value : NULL;
  9691. }
  9692. /**
  9693. * Serves the following variable types:
  9694. * - 'type' (permanent numeric char reg)
  9695. * - '#type' (permanent numeric account reg)
  9696. * - '##type' (permanent numeric account reg2)
  9697. **/
  9698. bool pc_setregistry(struct map_session_data *sd, int64 reg, int64 val)
  9699. {
  9700. struct script_reg_num *p = NULL;
  9701. const char *regname = get_str(script_getvarid(reg));
  9702. uint32 index = script_getvaridx(reg);
  9703. if ( !reg_load && !sd->vars_ok ) {
  9704. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  9705. return false;
  9706. }
  9707. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  9708. if( val ) {
  9709. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  9710. script_array_update(&sd->regs, reg, false);
  9711. p->value = val;
  9712. } else {
  9713. p->value = 0;
  9714. if( index )
  9715. script_array_update(&sd->regs, reg, true);
  9716. }
  9717. if (!reg_load)
  9718. p->flag.update = 1;/* either way, it will require either delete or replace */
  9719. } else if( val ) {
  9720. DBData prev;
  9721. if( index )
  9722. script_array_update(&sd->regs, reg, false);
  9723. p = ers_alloc(num_reg_ers, struct script_reg_num);
  9724. p->value = val;
  9725. if (!reg_load)
  9726. p->flag.update = 1;
  9727. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9728. p = (struct script_reg_num *)db_data2ptr(&prev);
  9729. ers_free(num_reg_ers, p);
  9730. }
  9731. }
  9732. if (!reg_load && p)
  9733. sd->vars_dirty = true;
  9734. return true;
  9735. }
  9736. /**
  9737. * Serves the following variable types:
  9738. * - 'type$' (permanent str char reg)
  9739. * - '#type$' (permanent str account reg)
  9740. * - '##type$' (permanent str account reg2)
  9741. **/
  9742. bool pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  9743. {
  9744. struct script_reg_str *p = NULL;
  9745. const char *regname = get_str(script_getvarid(reg));
  9746. unsigned int index = script_getvaridx(reg);
  9747. size_t vlen = 0;
  9748. if (!reg_load && !sd->vars_ok) {
  9749. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  9750. return false;
  9751. }
  9752. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  9753. {
  9754. 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);
  9755. return false;
  9756. }
  9757. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  9758. if( val[0] ) {
  9759. if( p->value )
  9760. aFree(p->value);
  9761. else if ( index ) // an entry that was deleted, so we reset
  9762. script_array_update(&sd->regs, reg, false);
  9763. p->value = aStrdup(val);
  9764. } else {
  9765. if (p->value)
  9766. aFree(p->value);
  9767. p->value = NULL;
  9768. if( index )
  9769. script_array_update(&sd->regs, reg, true);
  9770. }
  9771. if( !reg_load )
  9772. p->flag.update = 1; // either way, it will require either delete or replace
  9773. } else if( val[0] ) {
  9774. DBData prev;
  9775. if( index )
  9776. script_array_update(&sd->regs, reg, false);
  9777. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9778. p->value = aStrdup(val);
  9779. if( !reg_load )
  9780. p->flag.update = 1;
  9781. p->flag.type = 1;
  9782. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  9783. p = (struct script_reg_str *)db_data2ptr(&prev);
  9784. if( p->value )
  9785. aFree(p->value);
  9786. ers_free(str_reg_ers, p);
  9787. }
  9788. }
  9789. if( !reg_load && p )
  9790. sd->vars_dirty = true;
  9791. return true;
  9792. }
  9793. /**
  9794. * Set value of player variable
  9795. * @param sd Player
  9796. * @param reg Variable name
  9797. * @param value
  9798. * @return True if success, false if failed.
  9799. **/
  9800. bool pc_setreg2(struct map_session_data *sd, const char *reg, int64 val) {
  9801. char prefix = reg[0];
  9802. nullpo_retr(false, sd);
  9803. if (reg[strlen(reg)-1] == '$') {
  9804. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9805. return false;
  9806. }
  9807. val = cap_value(val, INT_MIN, INT_MAX);
  9808. switch (prefix) {
  9809. case '.':
  9810. case '\'':
  9811. case '$':
  9812. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9813. return false;
  9814. case '@':
  9815. return pc_setreg(sd, add_str(reg), val);
  9816. case '#':
  9817. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  9818. default:
  9819. return pc_setglobalreg(sd, add_str(reg), val);
  9820. }
  9821. return false;
  9822. }
  9823. /**
  9824. * Get value of player variable
  9825. * @param sd Player
  9826. * @param reg Variable name
  9827. * @return Variable value or 0 if failed.
  9828. **/
  9829. int64 pc_readreg2(struct map_session_data *sd, const char *reg) {
  9830. char prefix = reg[0];
  9831. nullpo_ret(sd);
  9832. if (reg[strlen(reg)-1] == '$') {
  9833. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9834. return 0;
  9835. }
  9836. switch (prefix) {
  9837. case '.':
  9838. case '\'':
  9839. case '$':
  9840. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9841. return 0;
  9842. case '@':
  9843. return pc_readreg(sd, add_str(reg));
  9844. case '#':
  9845. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  9846. default:
  9847. return pc_readglobalreg(sd, add_str(reg));
  9848. }
  9849. return 0;
  9850. }
  9851. /*==========================================
  9852. * Exec eventtimer for player sd (retrieved from map_session (id))
  9853. *------------------------------------------*/
  9854. static TIMER_FUNC(pc_eventtimer){
  9855. struct map_session_data *sd=map_id2sd(id);
  9856. char *p = (char *)data;
  9857. int i;
  9858. if(sd==NULL)
  9859. return 0;
  9860. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  9861. if( i < MAX_EVENTTIMER )
  9862. {
  9863. sd->eventtimer[i] = INVALID_TIMER;
  9864. sd->eventcount--;
  9865. npc_event(sd,p,0);
  9866. }
  9867. else
  9868. ShowError("pc_eventtimer: no such event timer\n");
  9869. if (p) aFree(p);
  9870. return 0;
  9871. }
  9872. /*==========================================
  9873. * Add eventtimer for player sd ?
  9874. *------------------------------------------*/
  9875. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  9876. {
  9877. int i;
  9878. nullpo_retr(false,sd);
  9879. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  9880. if( i == MAX_EVENTTIMER )
  9881. return false;
  9882. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  9883. sd->eventcount++;
  9884. return true;
  9885. }
  9886. /*==========================================
  9887. * Del eventtimer for player sd ?
  9888. *------------------------------------------*/
  9889. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  9890. {
  9891. char* p = NULL;
  9892. int i;
  9893. nullpo_retr(false,sd);
  9894. if (sd->eventcount == 0)
  9895. return false;
  9896. // find the named event timer
  9897. ARR_FIND( 0, MAX_EVENTTIMER, i,
  9898. sd->eventtimer[i] != INVALID_TIMER &&
  9899. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  9900. strcmp(p, name) == 0
  9901. );
  9902. if( i == MAX_EVENTTIMER )
  9903. return false; // not found
  9904. delete_timer(sd->eventtimer[i],pc_eventtimer);
  9905. sd->eventtimer[i] = INVALID_TIMER;
  9906. if(sd->eventcount > 0)
  9907. sd->eventcount--;
  9908. aFree(p);
  9909. return true;
  9910. }
  9911. /*==========================================
  9912. * Update eventtimer count for player sd
  9913. *------------------------------------------*/
  9914. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  9915. {
  9916. int i;
  9917. nullpo_retv(sd);
  9918. for(i=0;i<MAX_EVENTTIMER;i++)
  9919. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  9920. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  9921. addtick_timer(sd->eventtimer[i],tick);
  9922. break;
  9923. }
  9924. }
  9925. /*==========================================
  9926. * Remove all eventtimer for player sd
  9927. *------------------------------------------*/
  9928. void pc_cleareventtimer(struct map_session_data *sd)
  9929. {
  9930. int i;
  9931. nullpo_retv(sd);
  9932. if (sd->eventcount == 0)
  9933. return;
  9934. for(i=0;i<MAX_EVENTTIMER;i++){
  9935. if( sd->eventtimer[i] != INVALID_TIMER ){
  9936. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  9937. delete_timer(sd->eventtimer[i],pc_eventtimer);
  9938. sd->eventtimer[i] = INVALID_TIMER;
  9939. if(sd->eventcount > 0) //avoid looping to max val
  9940. sd->eventcount--;
  9941. if (p) aFree(p);
  9942. }
  9943. }
  9944. }
  9945. /**
  9946. * Called when an item with combo is worn
  9947. * @param sd: Player data
  9948. * @param data: Item data
  9949. * @return Number of succeeded combo(s)
  9950. */
  9951. static int pc_checkcombo(struct map_session_data *sd, item_data *data) {
  9952. int success = 0;
  9953. for (const auto &item_combo : data->combos) {
  9954. bool do_continue = false;
  9955. // Ensure this isn't a duplicate combo
  9956. for (const auto player_combo : sd->combos) {
  9957. if (player_combo->id == item_combo->id) {
  9958. do_continue = true;
  9959. break;
  9960. }
  9961. }
  9962. // Combo already equipped
  9963. if (do_continue)
  9964. continue;
  9965. size_t nb_itemCombo = item_combo->nameid.size();
  9966. if (nb_itemCombo < 2) // A combo with less then 2 item?
  9967. continue;
  9968. struct s_itemchk {
  9969. int idx;
  9970. t_itemid nameid, card[MAX_SLOTS];
  9971. s_itemchk() : idx(0), nameid(0), card() {};
  9972. };
  9973. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  9974. size_t j;
  9975. unsigned int pos = 0;
  9976. for (j = 0; j < nb_itemCombo; j++) {
  9977. t_itemid id = item_combo->nameid[j];
  9978. bool found = false;
  9979. for (int16 k = 0; k < EQI_MAX; k++) {
  9980. short index = sd->equip_index[k];
  9981. if (index < 0)
  9982. continue;
  9983. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  9984. continue;
  9985. if (!sd->inventory_data[index])
  9986. continue;
  9987. if (itemdb_type(id) != IT_CARD) {
  9988. if (sd->inventory_data[index]->nameid != id)
  9989. continue;
  9990. if (j > 0) { // Check if this item not already used
  9991. do_continue = false;
  9992. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  9993. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  9994. do_continue = true;
  9995. break;
  9996. }
  9997. }
  9998. if (do_continue)
  9999. continue;
  10000. }
  10001. combo_idx[j].nameid = id;
  10002. combo_idx[j].idx = index;
  10003. pos |= sd->inventory.u.items_inventory[index].equip;
  10004. found = true;
  10005. break;
  10006. } else { // Cards and enchants
  10007. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  10008. continue;
  10009. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  10010. do_continue = false;
  10011. if (sd->inventory.u.items_inventory[index].card[z] != id)
  10012. continue;
  10013. if (j > 0) {
  10014. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  10015. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  10016. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  10017. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  10018. do_continue = true;
  10019. break;
  10020. }
  10021. }
  10022. }
  10023. }
  10024. }
  10025. if (do_continue)
  10026. continue;
  10027. combo_idx[j].nameid = id;
  10028. combo_idx[j].idx = index;
  10029. combo_idx[j].card[z] = id;
  10030. pos |= sd->inventory.u.items_inventory[index].equip;
  10031. found = true;
  10032. break;
  10033. }
  10034. }
  10035. }
  10036. if (!found)
  10037. break; // Unable to found all the IDs for this combo, return
  10038. }
  10039. // Broke out of the count loop without finding all IDs, move to the next combo
  10040. if (j < nb_itemCombo)
  10041. continue;
  10042. // All items in the combo are matching
  10043. auto entry = std::make_shared<s_combos>();
  10044. entry->bonus = item_combo->script;
  10045. entry->id = item_combo->id;
  10046. entry->pos = pos;
  10047. sd->combos.push_back(entry);
  10048. combo_idx.clear();
  10049. success++;
  10050. }
  10051. return success;
  10052. }
  10053. /**
  10054. * Called when an item with combo is removed
  10055. * @param sd: Player data
  10056. * @param data: Item data
  10057. * @return Number of removed combo(s)
  10058. */
  10059. static int pc_removecombo(struct map_session_data *sd, item_data *data ) {
  10060. if (sd->combos.empty())
  10061. return 0; // Nothing to do here, player has no combos
  10062. int retval = 0;
  10063. for (const auto &item_combo : data->combos) {
  10064. std::shared_ptr<s_combos> del_combo = nullptr;
  10065. // Check if this combo exists on this player
  10066. for (const auto &player_combo : sd->combos) {
  10067. if (player_combo->id == item_combo->id) {
  10068. del_combo = player_combo;
  10069. break;
  10070. }
  10071. }
  10072. // No match, skip this combo
  10073. if (del_combo == nullptr)
  10074. continue;
  10075. util::vector_erase_if_exists(sd->combos, del_combo);
  10076. retval++;
  10077. // Check if combo requirements still fit
  10078. if (pc_checkcombo(sd, data))
  10079. continue;
  10080. // It's empty, clear all the memory
  10081. if (sd->combos.empty()) {
  10082. sd->combos.clear();
  10083. return retval; // Return at this point as there are no more combos to check
  10084. }
  10085. }
  10086. return retval;
  10087. }
  10088. /**
  10089. * Load combo data(s) of player
  10090. * @param sd: Player data
  10091. * @return ret numbers of succeed combo
  10092. */
  10093. int pc_load_combo(struct map_session_data *sd) {
  10094. int ret = 0;
  10095. for (int16 i = 0; i < EQI_MAX; i++) {
  10096. item_data *id;
  10097. short idx = sd->equip_index[i];
  10098. if (idx < 0 || !(id = sd->inventory_data[idx]))
  10099. continue;
  10100. if (!id->combos.empty())
  10101. ret += pc_checkcombo(sd, id);
  10102. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  10103. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  10104. if (!sd->inventory.u.items_inventory[idx].card[j])
  10105. continue;
  10106. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[idx].card[j]);
  10107. if (data != nullptr) {
  10108. if (!data->combos.empty())
  10109. ret += pc_checkcombo(sd, data.get());
  10110. }
  10111. }
  10112. }
  10113. }
  10114. return ret;
  10115. }
  10116. /*==========================================
  10117. * Equip item on player sd at req_pos from inventory index n
  10118. * return: false - fail; true - success
  10119. *------------------------------------------*/
  10120. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  10121. {
  10122. int i, pos, flag = 0, iflag;
  10123. struct item_data *id;
  10124. uint8 res = ITEM_EQUIP_ACK_OK;
  10125. short* equip_index;
  10126. nullpo_retr(false,sd);
  10127. if( n < 0 || n >= MAX_INVENTORY ) {
  10128. if( equipswitch ){
  10129. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10130. }else{
  10131. // Does not deserve an answer... [Lemongrass]
  10132. //clif_equipitemack( sd, ITEM_EQUIP_ACK_FAIL, n );
  10133. }
  10134. return false;
  10135. }
  10136. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  10137. if( equipswitch ){
  10138. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10139. }else{
  10140. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n );
  10141. }
  10142. return false;
  10143. }
  10144. if (!(id = sd->inventory_data[n]))
  10145. return false;
  10146. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  10147. if(battle_config.battle_log && !equipswitch)
  10148. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  10149. if((res = pc_isequip(sd,n))) {
  10150. if( equipswitch ){
  10151. clif_equipswitch_add( sd, n, req_pos, res );
  10152. }else{
  10153. clif_equipitemack( *sd, res, n ); // fail
  10154. }
  10155. return false;
  10156. }
  10157. if( equipswitch && id->type == IT_AMMO ){
  10158. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10159. return false;
  10160. }
  10161. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  10162. if( equipswitch ){
  10163. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10164. }else{
  10165. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); // fail
  10166. }
  10167. return false;
  10168. }
  10169. if( sd->sc.count && (sd->sc.cant.equip || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  10170. if( equipswitch ){
  10171. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10172. }else{
  10173. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); //Fail
  10174. }
  10175. return false;
  10176. }
  10177. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  10178. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  10179. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  10180. clif_notify_bindOnEquip( *sd, n );
  10181. }
  10182. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  10183. pos = req_pos&EQP_ACC;
  10184. if (pos == EQP_ACC) //User specified both slots.
  10185. pos = (equip_index[EQI_ACC_R] >= 0 && equip_index[EQI_ACC_L] < 0) ? EQP_ACC_L : EQP_ACC_R;
  10186. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  10187. if (!sd->inventory.u.items_inventory[n].card[i])
  10188. continue;
  10189. std::shared_ptr<item_data> card_data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10190. if (card_data) {
  10191. int card_pos = card_data->equip;
  10192. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  10193. pos = card_pos; // Use the card's equip position
  10194. break;
  10195. }
  10196. }
  10197. }
  10198. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  10199. pos = (req_pos&EQP_ARMS);
  10200. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  10201. #ifdef RENEWAL
  10202. pos = (equip_index[EQI_HAND_R] >= 0 && equip_index[EQI_HAND_L] < 0) ? EQP_HAND_L : EQP_HAND_R;
  10203. #else
  10204. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  10205. #endif
  10206. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  10207. pos = req_pos&EQP_SHADOW_ACC;
  10208. if (pos == EQP_SHADOW_ACC)
  10209. pos = (equip_index[EQI_SHADOW_ACC_L] >= 0 && equip_index[EQI_SHADOW_ACC_R] < 0) ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  10210. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  10211. pos = (req_pos&EQP_SHADOW_ARMS);
  10212. if( pos == EQP_SHADOW_ARMS )
  10213. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  10214. }
  10215. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  10216. //Update skill-block range database when weapon range changes. [Skotlex]
  10217. i = equip_index[EQI_HAND_R];
  10218. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  10219. flag = 1;
  10220. else
  10221. flag = id->range != sd->inventory_data[i]->range;
  10222. }
  10223. if( equipswitch ){
  10224. for( i = 0; i < EQI_MAX; i++ ){
  10225. if( pos&equip_bitmask[i] ){
  10226. // If there was already an item assigned to this slot
  10227. if( sd->equip_switch_index[i] >= 0 ){
  10228. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  10229. }
  10230. // Assign the new index to it
  10231. sd->equip_switch_index[i] = n;
  10232. }
  10233. }
  10234. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  10235. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  10236. return true;
  10237. }else{
  10238. for(i=0;i<EQI_MAX;i++) {
  10239. if(pos & equip_bitmask[i]) {
  10240. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  10241. pc_unequipitem(sd,sd->equip_index[i], 1 | 2 | 4);
  10242. sd->equip_index[i] = n;
  10243. }
  10244. }
  10245. pc_equipswitch_remove(sd, n);
  10246. if(pos==EQP_AMMO) {
  10247. clif_arrowequip(sd,n);
  10248. clif_arrow_fail(sd,3);
  10249. }
  10250. else
  10251. clif_equipitemack( *sd, ITEM_EQUIP_ACK_OK, n, pos );
  10252. sd->inventory.u.items_inventory[n].equip = pos;
  10253. }
  10254. if(pos & EQP_HAND_R) {
  10255. if(id)
  10256. sd->weapontype1 = id->subtype;
  10257. else
  10258. sd->weapontype1 = W_FIST;
  10259. pc_calcweapontype(sd);
  10260. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10261. }
  10262. if(pos & EQP_HAND_L) {
  10263. if(id) {
  10264. if(id->type == IT_WEAPON) {
  10265. sd->status.shield = W_FIST;
  10266. sd->weapontype2 = id->subtype;
  10267. }
  10268. else
  10269. if(id->type == IT_ARMOR) {
  10270. sd->status.shield = id->look;
  10271. sd->weapontype2 = W_FIST;
  10272. }
  10273. }
  10274. else
  10275. sd->status.shield = sd->weapontype2 = W_FIST;
  10276. pc_calcweapontype(sd);
  10277. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10278. }
  10279. if(pos & EQP_SHOES)
  10280. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10281. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  10282. short idx = sd->equip_index[EQI_AMMO];
  10283. if (idx >= 0) {
  10284. switch (sd->inventory_data[idx]->subtype) {
  10285. case AMMO_ARROW:
  10286. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  10287. pc_unequipitem(sd, idx, 2 | 4);
  10288. break;
  10289. case AMMO_BULLET:
  10290. case AMMO_SHELL:
  10291. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  10292. #ifdef RENEWAL
  10293. && id->subtype != W_GRENADE
  10294. #endif
  10295. )
  10296. pc_unequipitem(sd, idx, 2 | 4);
  10297. break;
  10298. #ifndef RENEWAL
  10299. case AMMO_GRENADE:
  10300. if (id->subtype != W_GRENADE)
  10301. pc_unequipitem(sd, idx, 2 | 4);
  10302. break;
  10303. #endif
  10304. }
  10305. }
  10306. }
  10307. pc_set_costume_view(sd);
  10308. pc_checkallowskill(sd); //Check if status changes should be halted.
  10309. iflag = sd->npc_item_flag;
  10310. // Check for combos (MUST be before status_calc_pc)
  10311. if (!id->combos.empty())
  10312. pc_checkcombo(sd, id);
  10313. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10314. ; // No cards
  10315. else {
  10316. for (i = 0; i < MAX_SLOTS; i++) {
  10317. if (!sd->inventory.u.items_inventory[n].card[i])
  10318. continue;
  10319. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10320. if (data != nullptr) {
  10321. if (!data->combos.empty())
  10322. pc_checkcombo(sd, data.get());
  10323. }
  10324. }
  10325. }
  10326. status_calc_pc(sd,SCO_NONE);
  10327. if (flag) //Update skill data
  10328. clif_skillinfoblock(sd);
  10329. //OnEquip script [Skotlex]
  10330. if (id) {
  10331. //only run the script if item isn't restricted
  10332. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  10333. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10334. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10335. ; //No cards
  10336. else {
  10337. for( i = 0; i < MAX_SLOTS; i++ ) {
  10338. if (!sd->inventory.u.items_inventory[n].card[i])
  10339. continue;
  10340. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10341. if ( data != nullptr ) {
  10342. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data.get(), sd->bl.m)))
  10343. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10344. }
  10345. }
  10346. }
  10347. }
  10348. sd->npc_item_flag = iflag;
  10349. return true;
  10350. }
  10351. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int position ){
  10352. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  10353. while( it != bonus.end() ){
  10354. std::shared_ptr<s_autobonus> b = *it;
  10355. if( ( b->pos & position ) != b->pos ){
  10356. it++;
  10357. continue;
  10358. }
  10359. it = bonus.erase( it );
  10360. }
  10361. }
  10362. /**
  10363. * Recalculate player status on unequip
  10364. * @param sd: Player data
  10365. * @param n: Item inventory index
  10366. * @param flag: Whether to recalculate a player's status or not
  10367. * @return True on success or false on failure
  10368. */
  10369. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  10370. int i, iflag;
  10371. bool status_calc = false;
  10372. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  10373. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  10374. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  10375. sd->inventory.u.items_inventory[n].equip = 0;
  10376. if (!(flag & 4))
  10377. pc_checkallowskill(sd);
  10378. iflag = sd->npc_item_flag;
  10379. // Check for combos (MUST be before status_calc_pc)
  10380. if (sd->inventory_data[n]) {
  10381. if (!sd->inventory_data[n]->combos.empty()) {
  10382. if (pc_removecombo(sd, sd->inventory_data[n]))
  10383. status_calc = true;
  10384. }
  10385. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10386. ; // No cards
  10387. else {
  10388. for (i = 0; i < MAX_SLOTS; i++) {
  10389. if (!sd->inventory.u.items_inventory[n].card[i])
  10390. continue;
  10391. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10392. if (data != nullptr) {
  10393. if (!data->combos.empty()) {
  10394. if (pc_removecombo(sd, data.get()))
  10395. status_calc = true;
  10396. }
  10397. }
  10398. }
  10399. }
  10400. }
  10401. if (flag & 1 || status_calc) {
  10402. pc_checkallowskill(sd);
  10403. status_calc_pc(sd, SCO_FORCE);
  10404. }
  10405. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  10406. status_change_end(&sd->bl, SC_SIGNUMCRUCIS);
  10407. //OnUnEquip script [Skotlex]
  10408. if (sd->inventory_data[n]) {
  10409. if (sd->inventory_data[n]->unequip_script)
  10410. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10411. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10412. ; //No cards
  10413. else {
  10414. for (i = 0; i < MAX_SLOTS; i++) {
  10415. if (!sd->inventory.u.items_inventory[n].card[i])
  10416. continue;
  10417. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10418. if (data != nullptr) {
  10419. if (data->unequip_script)
  10420. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10421. }
  10422. }
  10423. }
  10424. }
  10425. sd->npc_item_flag = iflag;
  10426. }
  10427. /**
  10428. * Called when attempting to unequip an item from a player
  10429. * @param sd: Player data
  10430. * @param n: Item inventory index
  10431. * @param flag: Type of unequip
  10432. * 0 - only unequip
  10433. * 1 - calculate status after unequipping
  10434. * 2 - force unequip
  10435. * 4 - unequip by switching equipment
  10436. * @return True on success or false on failure
  10437. */
  10438. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  10439. int i, pos;
  10440. nullpo_retr(false,sd);
  10441. if (n < 0 || n >= MAX_INVENTORY) {
  10442. clif_unequipitemack(sd,0,0,0);
  10443. return false;
  10444. }
  10445. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  10446. clif_unequipitemack(sd,n,0,0);
  10447. return false; //Nothing to unequip
  10448. }
  10449. // status change that makes player cannot unequip equipment
  10450. if (!(flag&2) && sd->sc.count &&( sd->sc.cant.unequip ||
  10451. (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  10452. {
  10453. clif_unequipitemack(sd,n,0,0);
  10454. return false;
  10455. }
  10456. if (battle_config.battle_log)
  10457. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  10458. for(i = 0; i < EQI_MAX; i++) {
  10459. if (pos & equip_bitmask[i])
  10460. sd->equip_index[i] = -1;
  10461. }
  10462. if(pos & EQP_HAND_R) {
  10463. sd->weapontype1 = W_FIST;
  10464. sd->status.weapon = sd->weapontype2;
  10465. pc_calcweapontype(sd);
  10466. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10467. if( !battle_config.dancing_weaponswitch_fix )
  10468. status_change_end(&sd->bl, SC_DANCING); // Unequipping => stop dancing.
  10469. #ifdef RENEWAL
  10470. if (battle_config.switch_remove_edp&2) {
  10471. #else
  10472. if (battle_config.switch_remove_edp&1) {
  10473. #endif
  10474. status_change_end(&sd->bl, SC_EDP);
  10475. }
  10476. }
  10477. if(pos & EQP_HAND_L) {
  10478. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  10479. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  10480. sd->status.shield = sd->weapontype2 = W_FIST;
  10481. pc_calcweapontype(sd);
  10482. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10483. }
  10484. if(pos & EQP_SHOES)
  10485. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10486. clif_unequipitemack(sd,n,pos,1);
  10487. pc_set_costume_view(sd);
  10488. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIP });
  10489. // On weapon change (right and left hand)
  10490. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  10491. if (battle_config.ammo_unequip && !(flag & 4)) {
  10492. switch (sd->inventory_data[n]->subtype) {
  10493. case W_BOW:
  10494. case W_MUSICAL:
  10495. case W_WHIP:
  10496. case W_REVOLVER:
  10497. case W_RIFLE:
  10498. case W_GATLING:
  10499. case W_SHOTGUN:
  10500. case W_GRENADE: {
  10501. short idx = sd->equip_index[EQI_AMMO];
  10502. if (idx >= 0) {
  10503. sd->equip_index[EQI_AMMO] = -1;
  10504. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  10505. pc_unequipitem_sub(sd, idx, 0);
  10506. }
  10507. }
  10508. break;
  10509. }
  10510. }
  10511. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPWEAPON });
  10512. }
  10513. // On armor change
  10514. if (pos & EQP_ARMOR) {
  10515. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPARMOR });
  10516. }
  10517. // On equipment change
  10518. #ifndef RENEWAL
  10519. if (!(flag&4))
  10520. status_change_end(&sd->bl, SC_CONCENTRATION);
  10521. #endif
  10522. // On ammo change
  10523. 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_))
  10524. status_change_end(&sd->bl, SC_P_ALTER);
  10525. pc_unequipitem_sub(sd, n, flag);
  10526. return true;
  10527. }
  10528. int pc_equipswitch( struct map_session_data* sd, int index ){
  10529. // Get the target equip mask
  10530. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  10531. // Get the currently equipped item
  10532. short equippedItem = pc_checkequip( sd, position, true );
  10533. // No item equipped at the target
  10534. if( equippedItem == -1 ){
  10535. // Remove it from the equip switch
  10536. pc_equipswitch_remove( sd, index );
  10537. pc_equipitem( sd, index, position );
  10538. return position;
  10539. }else{
  10540. std::map<int, int> unequipped;
  10541. int unequipped_position = 0;
  10542. // Unequip all items that interfere
  10543. for( int i = 0; i < EQI_MAX; i++ ){
  10544. int unequip_index = sd->equip_index[i];
  10545. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  10546. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  10547. // Store the unequipped index and position mask for later
  10548. unequipped[unequip_index] = unequip_item->equip;
  10549. // Keep the position for later
  10550. unequipped_position |= unequip_item->equip;
  10551. // Unequip the item
  10552. pc_unequipitem( sd, unequip_index, 0 );
  10553. }
  10554. }
  10555. int all_position = position | unequipped_position;
  10556. // Equip everything that is hit by the mask
  10557. for( int i = 0; i < EQI_MAX; i++ ){
  10558. int exchange_index = sd->equip_switch_index[i];
  10559. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  10560. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  10561. // Store the target position
  10562. int exchange_position = exchange_item->equipSwitch;
  10563. // Remove the item from equip switch
  10564. pc_equipswitch_remove( sd, exchange_index );
  10565. // Equip the item at the destinated position
  10566. pc_equipitem( sd, exchange_index, exchange_position );
  10567. }
  10568. }
  10569. // Place all unequipped items into the equip switch window
  10570. for( std::pair<int, int> pair : unequipped ){
  10571. int unequipped_index = pair.first;
  10572. int unequipped_position = pair.second;
  10573. // Rebuild the index cache
  10574. for( int i = 0; i < EQI_MAX; i++ ){
  10575. if( unequipped_position & equip_bitmask[i] ){
  10576. sd->equip_switch_index[i] = unequipped_index;
  10577. }
  10578. }
  10579. // Set the correct position mask
  10580. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  10581. // Notify the client
  10582. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  10583. }
  10584. return all_position;
  10585. }
  10586. }
  10587. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  10588. struct item* item = &sd->inventory.u.items_inventory[index];
  10589. if( !item->equipSwitch ){
  10590. return;
  10591. }
  10592. for( int i = 0; i < EQI_MAX; i++ ){
  10593. // If a match is found
  10594. if( sd->equip_switch_index[i] == index ){
  10595. // Remove it from the slot
  10596. sd->equip_switch_index[i] = -1;
  10597. }
  10598. }
  10599. // Send out one packet for all slots using the current item's mask
  10600. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  10601. item->equipSwitch = 0;
  10602. }
  10603. /*==========================================
  10604. * Checking if player (sd) has an invalid item
  10605. * and is unequiped on map load (item_noequip)
  10606. *------------------------------------------*/
  10607. void pc_checkitem(struct map_session_data *sd) {
  10608. int i, calc_flag = 0;
  10609. struct item* it;
  10610. nullpo_retv(sd);
  10611. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  10612. return;
  10613. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  10614. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10615. it = &sd->inventory.u.items_inventory[i];
  10616. if( it->nameid == 0 )
  10617. continue;
  10618. if( !it->equip )
  10619. continue;
  10620. if( it->equip&~pc_equippoint(sd,i) ) {
  10621. pc_unequipitem(sd, i, 2);
  10622. calc_flag = 1;
  10623. continue;
  10624. }
  10625. 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) ) {
  10626. pc_unequipitem(sd, i, 2);
  10627. calc_flag = 1;
  10628. continue;
  10629. }
  10630. }
  10631. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10632. it = &sd->inventory.u.items_inventory[i];
  10633. if( it->nameid == 0 )
  10634. continue;
  10635. if( !it->equipSwitch )
  10636. continue;
  10637. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  10638. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  10639. for( int j = 0; j < EQI_MAX; j++ ){
  10640. if( sd->equip_switch_index[j] == i ){
  10641. sd->equip_switch_index[j] = -1;
  10642. }
  10643. }
  10644. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  10645. continue;
  10646. }
  10647. }
  10648. if( calc_flag && sd->state.active ) {
  10649. pc_checkallowskill(sd);
  10650. status_calc_pc(sd,SCO_NONE);
  10651. }
  10652. }
  10653. /*==========================================
  10654. * Checks for unavailable items and removes them.
  10655. * @param sd: Player data
  10656. * @param type Forced check:
  10657. * 1 - Inventory
  10658. * 2 - Cart
  10659. * 4 - Storage
  10660. *------------------------------------------*/
  10661. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  10662. {
  10663. int i;
  10664. t_itemid nameid;
  10665. char output[256];
  10666. nullpo_retv(sd);
  10667. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  10668. for(i = 0; i < MAX_INVENTORY; i++) {
  10669. nameid = sd->inventory.u.items_inventory[i].nameid;
  10670. if (!nameid)
  10671. continue;
  10672. if (!itemdb_available(nameid)) {
  10673. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  10674. clif_displaymessage(sd->fd, output);
  10675. 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);
  10676. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  10677. continue;
  10678. }
  10679. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  10680. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  10681. }
  10682. }
  10683. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  10684. for(i = 0; i < MAX_CART; i++) {
  10685. nameid = sd->cart.u.items_cart[i].nameid;
  10686. if (!nameid)
  10687. continue;
  10688. if (!itemdb_available(nameid)) {
  10689. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  10690. clif_displaymessage(sd->fd, output);
  10691. 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);
  10692. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  10693. continue;
  10694. }
  10695. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  10696. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  10697. }
  10698. }
  10699. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  10700. for(i = 0; i < sd->storage.max_amount; i++) {
  10701. nameid = sd->storage.u.items_storage[i].nameid;
  10702. if (!nameid)
  10703. continue;
  10704. if (!itemdb_available(nameid)) {
  10705. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  10706. clif_displaymessage(sd->fd, output);
  10707. 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);
  10708. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  10709. continue;
  10710. }
  10711. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  10712. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  10713. }
  10714. }
  10715. }
  10716. /*==========================================
  10717. * Update PVP rank for sd1 in cmp to sd2
  10718. *------------------------------------------*/
  10719. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  10720. {
  10721. struct map_session_data *sd1,*sd2;
  10722. sd1=(struct map_session_data *)bl;
  10723. sd2=va_arg(ap,struct map_session_data *);
  10724. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  10725. {// cannot register pvp rank for hidden GMs
  10726. return 0;
  10727. }
  10728. if( sd1->pvp_point > sd2->pvp_point )
  10729. sd2->pvp_rank++;
  10730. return 0;
  10731. }
  10732. /*==========================================
  10733. * Calculate new rank beetween all present players (map_foreachinallarea)
  10734. * and display result
  10735. *------------------------------------------*/
  10736. int pc_calc_pvprank(struct map_session_data *sd)
  10737. {
  10738. int old = sd->pvp_rank;
  10739. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10740. sd->pvp_rank=1;
  10741. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  10742. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  10743. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  10744. return sd->pvp_rank;
  10745. }
  10746. /*==========================================
  10747. * Calculate next sd ranking calculation from config
  10748. *------------------------------------------*/
  10749. TIMER_FUNC(pc_calc_pvprank_timer){
  10750. struct map_session_data *sd;
  10751. sd=map_id2sd(id);
  10752. if(sd==NULL)
  10753. return 0;
  10754. sd->pvp_timer = INVALID_TIMER;
  10755. if( pc_isinvisible(sd) )
  10756. {// do not calculate the pvp rank for a hidden GM
  10757. return 0;
  10758. }
  10759. if( pc_calc_pvprank(sd) > 0 )
  10760. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  10761. return 0;
  10762. }
  10763. /*==========================================
  10764. * Checking if sd is married
  10765. * Return:
  10766. * partner_id = yes
  10767. * 0 = no
  10768. *------------------------------------------*/
  10769. int pc_ismarried(struct map_session_data *sd)
  10770. {
  10771. if(sd == NULL)
  10772. return -1;
  10773. if(sd->status.partner_id > 0)
  10774. return sd->status.partner_id;
  10775. else
  10776. return 0;
  10777. }
  10778. /*==========================================
  10779. * Marry player sd to player dstsd
  10780. * Return:
  10781. * false = fail
  10782. * true = success
  10783. *------------------------------------------*/
  10784. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  10785. {
  10786. if(sd == NULL || dstsd == NULL ||
  10787. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  10788. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  10789. return false;
  10790. sd->status.partner_id = dstsd->status.char_id;
  10791. dstsd->status.partner_id = sd->status.char_id;
  10792. achievement_update_objective(sd, AG_MARRY, 1, 1);
  10793. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  10794. return true;
  10795. }
  10796. /*==========================================
  10797. * Divorce sd from its partner
  10798. * Return:
  10799. * false = fail
  10800. * true = success
  10801. *------------------------------------------*/
  10802. bool pc_divorce(struct map_session_data *sd)
  10803. {
  10804. struct map_session_data *p_sd;
  10805. int i;
  10806. if( sd == NULL || !pc_ismarried(sd) )
  10807. return false;
  10808. if( !sd->status.partner_id )
  10809. return false; // Char is not married
  10810. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  10811. { // Lets char server do the divorce
  10812. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  10813. return false; // No char server connected
  10814. return true;
  10815. }
  10816. // Both players online, lets do the divorce manually
  10817. sd->status.partner_id = 0;
  10818. p_sd->status.partner_id = 0;
  10819. for( i = 0; i < MAX_INVENTORY; i++ )
  10820. {
  10821. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10822. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10823. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10824. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10825. }
  10826. clif_divorced(sd, p_sd->status.name);
  10827. clif_divorced(p_sd, sd->status.name);
  10828. return true;
  10829. }
  10830. /**
  10831. * Get the partner map_session_data of a player
  10832. * @param sd : the husband|wife session
  10833. * @return partner session or NULL
  10834. */
  10835. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  10836. if (!sd || !pc_ismarried(sd))
  10837. return NULL;
  10838. return map_charid2sd(sd->status.partner_id);
  10839. }
  10840. /**
  10841. * Get the father map_session_data of a player
  10842. * @param sd : the baby session
  10843. * @return father session or NULL
  10844. */
  10845. struct map_session_data *pc_get_father (struct map_session_data *sd){
  10846. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  10847. return NULL;
  10848. return map_charid2sd(sd->status.father);
  10849. }
  10850. /**
  10851. * Get the mother map_session_data of a player
  10852. * @param sd : the baby session
  10853. * @return mother session or NULL
  10854. */
  10855. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  10856. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  10857. return NULL;
  10858. return map_charid2sd(sd->status.mother);
  10859. }
  10860. /*==========================================
  10861. * Get sd children charid. (Need to be married)
  10862. *------------------------------------------*/
  10863. struct map_session_data *pc_get_child (struct map_session_data *sd)
  10864. {
  10865. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  10866. // charid2sd returns NULL if not found
  10867. return NULL;
  10868. return map_charid2sd(sd->status.child);
  10869. }
  10870. /*==========================================
  10871. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  10872. *------------------------------------------*/
  10873. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  10874. {
  10875. int hp = 0, sp = 0;
  10876. if( pc_isdead(sd) )
  10877. return;
  10878. if (sd->hp_loss.value) {
  10879. sd->hp_loss.tick += diff_tick;
  10880. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  10881. hp += sd->hp_loss.value;
  10882. sd->hp_loss.tick -= sd->hp_loss.rate;
  10883. }
  10884. if(hp >= sd->battle_status.hp)
  10885. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  10886. }
  10887. if (sd->sp_loss.value) {
  10888. sd->sp_loss.tick += diff_tick;
  10889. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  10890. sp += sd->sp_loss.value;
  10891. sd->sp_loss.tick -= sd->sp_loss.rate;
  10892. }
  10893. }
  10894. if (hp > 0 || sp > 0)
  10895. status_zap(&sd->bl, hp, sp);
  10896. }
  10897. //Character regen. Flag is used to know which types of regen can take place.
  10898. //&1: HP regen
  10899. //&2: SP regen
  10900. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  10901. {
  10902. int hp = 0, sp = 0;
  10903. if (sd->hp_regen.value) {
  10904. sd->hp_regen.tick += diff_tick;
  10905. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  10906. hp += sd->hp_regen.value;
  10907. sd->hp_regen.tick -= sd->hp_regen.rate;
  10908. }
  10909. }
  10910. if (sd->sp_regen.value) {
  10911. sd->sp_regen.tick += diff_tick;
  10912. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  10913. sp += sd->sp_regen.value;
  10914. sd->sp_regen.tick -= sd->sp_regen.rate;
  10915. }
  10916. }
  10917. if (sd->percent_hp_regen.value) {
  10918. sd->percent_hp_regen.tick += diff_tick;
  10919. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  10920. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  10921. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  10922. }
  10923. }
  10924. if (sd->percent_sp_regen.value) {
  10925. sd->percent_sp_regen.tick += diff_tick;
  10926. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  10927. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  10928. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  10929. }
  10930. }
  10931. if (hp > 0 || sp > 0)
  10932. status_heal(&sd->bl, hp, sp, 0);
  10933. }
  10934. /*==========================================
  10935. * Memo player sd savepoint. (map,x,y)
  10936. *------------------------------------------*/
  10937. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  10938. {
  10939. nullpo_retv(sd);
  10940. sd->status.save_point.map = mapindex;
  10941. sd->status.save_point.x = x;
  10942. sd->status.save_point.y = y;
  10943. }
  10944. /*==========================================
  10945. * Save 1 player data at autosave interval
  10946. *------------------------------------------*/
  10947. static TIMER_FUNC(pc_autosave){
  10948. int interval;
  10949. struct s_mapiterator* iter;
  10950. struct map_session_data* sd;
  10951. static int last_save_id = 0, save_flag = 0;
  10952. if(save_flag == 2) //Someone was saved on last call, normal cycle
  10953. save_flag = 0;
  10954. else
  10955. save_flag = 1; //Noone was saved, so save first found char.
  10956. iter = mapit_getallusers();
  10957. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10958. {
  10959. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  10960. continue;
  10961. if(sd->bl.id == last_save_id && save_flag != 1) {
  10962. save_flag = 1;
  10963. continue;
  10964. }
  10965. if(save_flag != 1) //Not our turn to save yet.
  10966. continue;
  10967. //Save char.
  10968. last_save_id = sd->bl.id;
  10969. save_flag = 2;
  10970. if (pc_isvip(sd)) // Check if we're still VIP
  10971. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  10972. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  10973. break;
  10974. }
  10975. mapit_free(iter);
  10976. interval = autosave_interval/(map_usercount()+1);
  10977. if(interval < minsave_interval)
  10978. interval = minsave_interval;
  10979. add_timer(gettick()+interval,pc_autosave,0,0);
  10980. return 0;
  10981. }
  10982. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  10983. {
  10984. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  10985. { //Night/day state does not match.
  10986. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  10987. sd->state.night = night_flag;
  10988. return 1;
  10989. }
  10990. return 0;
  10991. }
  10992. /*================================================
  10993. * timer to do the day [Yor]
  10994. * data: 0 = called by timer, 1 = gmcommand/script
  10995. *------------------------------------------------*/
  10996. TIMER_FUNC(map_day_timer){
  10997. char tmp_soutput[1024];
  10998. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  10999. return 0;
  11000. if (!night_flag)
  11001. return 0; //Already day.
  11002. night_flag = 0; // 0=day, 1=night [Yor]
  11003. map_foreachpc(pc_daynight_timer_sub);
  11004. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  11005. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11006. return 0;
  11007. }
  11008. /*================================================
  11009. * timer to do the night [Yor]
  11010. * data: 0 = called by timer, 1 = gmcommand/script
  11011. *------------------------------------------------*/
  11012. TIMER_FUNC(map_night_timer){
  11013. char tmp_soutput[1024];
  11014. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  11015. return 0;
  11016. if (night_flag)
  11017. return 0; //Already nigth.
  11018. night_flag = 1; // 0=day, 1=night [Yor]
  11019. map_foreachpc(pc_daynight_timer_sub);
  11020. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  11021. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11022. return 0;
  11023. }
  11024. /**
  11025. * Attempt to stand up a player
  11026. * @param sd
  11027. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  11028. * @return True if success, Fals if failed
  11029. */
  11030. bool pc_setstand(struct map_session_data *sd, bool force){
  11031. nullpo_ret(sd);
  11032. // Cannot stand yet
  11033. // TODO: Move to SCS_NOSTAND [Cydh]
  11034. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  11035. return false;
  11036. status_change_end(&sd->bl, SC_TENSIONRELAX);
  11037. clif_status_load(&sd->bl,EFST_SIT,0);
  11038. clif_standing(&sd->bl); //Inform area PC is standing
  11039. //Reset sitting tick.
  11040. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  11041. if( pc_isdead( sd ) ){
  11042. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11043. clif_party_dead( *sd );
  11044. }else{
  11045. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11046. }
  11047. return true;
  11048. }
  11049. /**
  11050. * Calculate Overheat value
  11051. * @param sd: Player data
  11052. * @param heat: Amount of Heat to adjust
  11053. **/
  11054. void pc_overheat(map_session_data &sd, int16 heat) {
  11055. status_change_entry *sce = sd.sc.data[SC_OVERHEAT_LIMITPOINT];
  11056. if (sce) {
  11057. sce->val1 += heat;
  11058. sce->val1 = cap_value(sce->val1, 0, 1000);
  11059. if (heat < 0 && sce->val1 == 0) { // Cooling device used.
  11060. status_change_end(&sd.bl, SC_OVERHEAT_LIMITPOINT);
  11061. status_change_end(&sd.bl, SC_OVERHEAT);
  11062. }
  11063. } else if (heat > 0)
  11064. sc_start(&sd.bl, &sd.bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  11065. }
  11066. /**
  11067. * Check if player is autolooting given itemID.
  11068. */
  11069. bool pc_isautolooting(struct map_session_data *sd, t_itemid nameid)
  11070. {
  11071. uint8 i = 0;
  11072. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  11073. return true;
  11074. if (!sd->state.autolooting)
  11075. return false;
  11076. if (sd->state.autolooting)
  11077. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  11078. return (i != AUTOLOOTITEM_SIZE);
  11079. }
  11080. /**
  11081. * Checks if player can use @/#command
  11082. * @param sd Player map session data
  11083. * @param command Command name without @/# and params
  11084. * @param type is it atcommand or charcommand
  11085. */
  11086. bool pc_can_use_command( struct map_session_data *sd, const char *command, AtCommandType type ){
  11087. return sd->group->can_use_command( command, type );
  11088. }
  11089. bool pc_has_permission( struct map_session_data* sd, e_pc_permission permission ){
  11090. return sd->permissions.test( permission );
  11091. }
  11092. /**
  11093. * Checks if commands used by a player should be logged
  11094. * according to their group setting.
  11095. * @param sd Player map session data
  11096. */
  11097. bool pc_should_log_commands( struct map_session_data *sd ){
  11098. return sd->group->log_commands;
  11099. }
  11100. /**
  11101. * Spirit Charm expiration timer.
  11102. * @see TimerFunc
  11103. */
  11104. static TIMER_FUNC(pc_spiritcharm_timer){
  11105. struct map_session_data *sd;
  11106. int i;
  11107. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  11108. return 1;
  11109. if (sd->spiritcharm <= 0) {
  11110. 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);
  11111. sd->spiritcharm = 0;
  11112. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11113. return 0;
  11114. }
  11115. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  11116. if (i == sd->spiritcharm) {
  11117. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  11118. return 0;
  11119. }
  11120. sd->spiritcharm--;
  11121. if (i != sd->spiritcharm)
  11122. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  11123. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11124. if (sd->spiritcharm <= 0)
  11125. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11126. clif_spiritcharm(sd);
  11127. return 0;
  11128. }
  11129. /**
  11130. * Adds a spirit charm.
  11131. * @param sd: Target character
  11132. * @param interval: Duration
  11133. * @param max: Maximum amount of charms to add
  11134. * @param type: Charm type (@see spirit_charm_types)
  11135. */
  11136. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  11137. {
  11138. int tid, i;
  11139. nullpo_retv(sd);
  11140. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  11141. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  11142. if (max > MAX_SPIRITCHARM)
  11143. max = MAX_SPIRITCHARM;
  11144. if (sd->spiritcharm < 0)
  11145. sd->spiritcharm = 0;
  11146. if (sd->spiritcharm && sd->spiritcharm >= max) {
  11147. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  11148. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  11149. sd->spiritcharm--;
  11150. if (sd->spiritcharm != 0)
  11151. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  11152. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11153. }
  11154. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  11155. 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);
  11156. if (i != sd->spiritcharm)
  11157. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  11158. sd->spiritcharm_timer[i] = tid;
  11159. sd->spiritcharm++;
  11160. sd->spiritcharm_type = type;
  11161. clif_spiritcharm(sd);
  11162. }
  11163. /**
  11164. * Removes one or more spirit charms.
  11165. * @param sd: The target character
  11166. * @param count: Amount of charms to remove
  11167. * @param type: Type of charm to remove
  11168. */
  11169. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  11170. {
  11171. int i;
  11172. nullpo_retv(sd);
  11173. if (sd->spiritcharm_type != type)
  11174. return;
  11175. if (sd->spiritcharm <= 0) {
  11176. sd->spiritcharm = 0;
  11177. return;
  11178. }
  11179. if (count <= 0)
  11180. return;
  11181. if (count > sd->spiritcharm)
  11182. count = sd->spiritcharm;
  11183. sd->spiritcharm -= count;
  11184. if (count > MAX_SPIRITCHARM)
  11185. count = MAX_SPIRITCHARM;
  11186. for (i = 0; i < count; i++) {
  11187. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  11188. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  11189. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11190. }
  11191. }
  11192. for (i = count; i < MAX_SPIRITCHARM; i++) {
  11193. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  11194. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11195. }
  11196. if (sd->spiritcharm <= 0)
  11197. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11198. clif_spiritcharm(sd);
  11199. }
  11200. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  11201. /**
  11202. * Renewal EXP/Item Drop rate modifier based on level penalty
  11203. * @param level_diff: Monster and Player level difference
  11204. * @param mob_class: Monster class
  11205. * @param mode: Monster mode
  11206. * @param type: 1 - EXP, 2 - Item Drop
  11207. * @return Penalty rate
  11208. */
  11209. uint16 pc_level_penalty_mod( struct map_session_data* sd, e_penalty_type type, std::shared_ptr<s_mob_db> mob, mob_data* md ){
  11210. // No player was attached, we don't use any modifier (100 = rates are not touched)
  11211. if( sd == nullptr ){
  11212. return 100;
  11213. }
  11214. if ((type == PENALTY_DROP && map_getmapflag(sd->bl.m, MF_NORENEWALDROPPENALTY)) || (type == PENALTY_EXP && map_getmapflag(sd->bl.m, MF_NORENEWALEXPPENALTY))) {
  11215. return 100;
  11216. }
  11217. int monster_level;
  11218. if( md != nullptr ){
  11219. monster_level = md->level;
  11220. mob = md->db;
  11221. }else if( mob != nullptr ){
  11222. monster_level = mob->lv;
  11223. }else{
  11224. return 100;
  11225. }
  11226. if( ( type == PENALTY_DROP || type == PENALTY_MVP_DROP ) && status_has_mode( &mob->status, MD_FIXEDITEMDROP ) ){
  11227. return 100;
  11228. }
  11229. int level_difference = monster_level - sd->status.base_level;
  11230. std::shared_ptr<s_penalty> penalty = penalty_db.find( type );
  11231. if( penalty != nullptr ){
  11232. return penalty->rate[ level_difference + MAX_LEVEL - 1 ];
  11233. }else{
  11234. return 100;
  11235. }
  11236. }
  11237. #endif
  11238. int pc_split_str(char *str,char **val,int num)
  11239. {
  11240. int i;
  11241. for (i=0; i<num && str; i++){
  11242. val[i] = str;
  11243. str = strchr(str,',');
  11244. if (str && i<num-1) //Do not remove a trailing comma.
  11245. *str++=0;
  11246. }
  11247. return i;
  11248. }
  11249. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  11250. {
  11251. static int warning=0;
  11252. int i,j;
  11253. for (i=0; i<max; i++) {
  11254. double f;
  11255. if (!str) break;
  11256. f = atof(str);
  11257. if (f < 0)
  11258. val[i] = 0;
  11259. else if (f > UINT_MAX) {
  11260. val[i] = UINT_MAX;
  11261. if (!warning) {
  11262. warning = 1;
  11263. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  11264. }
  11265. } else
  11266. val[i] = (unsigned int)f;
  11267. str = strchr(str,sep);
  11268. if (str)
  11269. *str++=0;
  11270. }
  11271. //Zero up the remaining.
  11272. for(j=i; j < max; j++)
  11273. val[j] = 0;
  11274. return i;
  11275. }
  11276. std::shared_ptr<s_skill_tree_entry> SkillTreeDatabase::get_skill_data(int class_, uint16 skill_id) {
  11277. std::shared_ptr<s_skill_tree> tree = this->find(class_);
  11278. if (tree != nullptr)
  11279. return util::umap_find(tree->skills, skill_id);
  11280. return nullptr;
  11281. }
  11282. const std::string SkillTreeDatabase::getDefaultLocation() {
  11283. return std::string(db_path) + "/skill_tree.yml";
  11284. }
  11285. /**
  11286. * Reads and parses an entry from the skill_tree.
  11287. * @param node: YAML node containing the entry.
  11288. * @return count of successfully parsed rows
  11289. */
  11290. uint64 SkillTreeDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11291. std::string job_name;
  11292. if (!this->asString(node, "Job", job_name))
  11293. return 0;
  11294. int64 constant;
  11295. std::string job_name_constant = "JOB_" + job_name;
  11296. if (!script_get_constant(job_name_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11297. this->invalidWarning(node["Job"], "Invalid job %s.\n", job_name.c_str());
  11298. return 0;
  11299. }
  11300. uint16 job_id = static_cast<uint16>(constant);
  11301. std::shared_ptr<s_skill_tree> tree = this->find(job_id);
  11302. bool exists = tree != nullptr;
  11303. if (!exists)
  11304. tree = std::make_shared<s_skill_tree>();
  11305. if (this->nodeExists(node, "Inherit")) {
  11306. const ryml::NodeRef& InheritNode = node["Inherit"];
  11307. for (const auto &Inheritit : InheritNode) {
  11308. std::string inheritname;
  11309. c4::from_chars(Inheritit.key(), &inheritname);
  11310. std::string inheritname_constant = "JOB_" + inheritname;
  11311. if (!script_get_constant(inheritname_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11312. this->invalidWarning(InheritNode[Inheritit.key()], "Invalid job %s.\n", inheritname.c_str());
  11313. return 0;
  11314. }
  11315. bool active;
  11316. if (!this->asBool(InheritNode, inheritname, active))
  11317. return 0;
  11318. uint16 inherit_job = static_cast<uint16>(constant);
  11319. if (!active) {
  11320. if (exists)
  11321. util::vector_erase_if_exists(tree->inherit_job, inherit_job);
  11322. }
  11323. else {
  11324. if (!util::vector_exists(tree->inherit_job, inherit_job))
  11325. tree->inherit_job.push_back(inherit_job);
  11326. }
  11327. }
  11328. }
  11329. if (this->nodeExists(node, "Tree")) {
  11330. for (const auto &it : node["Tree"]) {
  11331. std::string skill_name;
  11332. if (!this->asString(it, "Name", skill_name))
  11333. return 0;
  11334. uint16 skill_id = skill_name2id(skill_name.c_str());
  11335. if (skill_id == 0) {
  11336. this->invalidWarning(it["Name"], "Invalid skill name \"%s\".\n", skill_name.c_str());
  11337. return 0;
  11338. }
  11339. if (!skill_get_index(skill_id)) {
  11340. this->invalidWarning(it["Name"], "Unable to load skill %s into job %hu's tree.\n", skill_name.c_str(), job_id);
  11341. return 0;
  11342. }
  11343. std::shared_ptr<s_skill_tree_entry> entry;
  11344. bool skill_exists = tree->skills.count(skill_id) > 0;
  11345. if (skill_exists)
  11346. entry = tree->skills[skill_id];
  11347. else
  11348. entry = std::make_shared<s_skill_tree_entry>();
  11349. entry->skill_id = skill_id;
  11350. uint16 max_lv;
  11351. if (!this->asUInt16(it, "MaxLevel", max_lv))
  11352. return 0;
  11353. if (max_lv > MAX_SKILL_LEVEL) {
  11354. this->invalidWarning(it["MaxLevel"], "MaxLevel exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11355. return 0;
  11356. }
  11357. uint16 skill_lv_max = skill_get_max(skill_id);
  11358. if (max_lv > skill_lv_max) {
  11359. 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);
  11360. max_lv = skill_lv_max;
  11361. }
  11362. // if (max_lv == 0) { // skill lvl 0 removed on loadingFinished (because of inherit)
  11363. // if (!skill_exists || entry->skill_id.erase(skill_id) == 0)
  11364. // 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());
  11365. // continue;
  11366. // }
  11367. entry->max_lv = max_lv;
  11368. if (this->nodeExists(it, "BaseLevel")) {
  11369. uint32 baselv;
  11370. if (!this->asUInt32(it, "BaseLevel", baselv))
  11371. return 0;
  11372. uint32 baselv_max = job_db.get_maxBaseLv(job_id);
  11373. if (baselv > baselv_max) {
  11374. this->invalidWarning(it["BaseLevel"], "Skill %hu's base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11375. skill_id, baselv, job_name.c_str(), baselv_max);
  11376. baselv = baselv_max;
  11377. }
  11378. entry->baselv = baselv;
  11379. } else {
  11380. if (!skill_exists)
  11381. entry->baselv = 0;
  11382. }
  11383. if (this->nodeExists(it, "JobLevel")) {
  11384. uint32 joblv;
  11385. if (!this->asUInt32(it, "JobLevel", joblv))
  11386. return 0;
  11387. uint32 joblv_max = job_db.get_maxJobLv(job_id);
  11388. if (joblv > joblv_max) {
  11389. this->invalidWarning(it["JobLevel"], "Skill %hu's job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11390. skill_id, joblv, job_name.c_str(), joblv_max);
  11391. joblv = joblv_max;
  11392. }
  11393. entry->joblv = joblv;
  11394. } else {
  11395. if (!skill_exists)
  11396. entry->joblv = 0;
  11397. }
  11398. if (this->nodeExists(it, "Requires")) {
  11399. for (const auto &Requiresit : it["Requires"]) {
  11400. if (!this->nodesExist(Requiresit, { "Name" }))
  11401. return 0;
  11402. std::string skill_name_req;
  11403. if (!this->asString(Requiresit, "Name", skill_name_req))
  11404. return 0;
  11405. uint16 skill_id_req = skill_name2id(skill_name_req.c_str());
  11406. if (skill_id_req == 0) {
  11407. this->invalidWarning(Requiresit["Name"], "Invalid skill name \"%s\".\n", skill_name_req.c_str());
  11408. return 0;
  11409. }
  11410. uint16 lv_req;
  11411. if (!this->asUInt16(Requiresit, "Level", lv_req))
  11412. return 0;
  11413. if (lv_req > MAX_SKILL_LEVEL) {
  11414. this->invalidWarning(Requiresit["Level"], "Level exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11415. return 0;
  11416. }
  11417. uint16 lv_req_max = skill_get_max(skill_id_req);
  11418. if (lv_req > lv_req_max) {
  11419. 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);
  11420. lv_req = lv_req_max;
  11421. }
  11422. if (lv_req == 0) {
  11423. if (entry->need.erase(skill_id_req) == 0)
  11424. 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());
  11425. continue;
  11426. }
  11427. entry->need[skill_id_req] = lv_req;
  11428. }
  11429. }
  11430. if (this->nodeExists(it, "Exclude")) {
  11431. bool exclude;
  11432. if (!this->asBool(it, "Exclude", exclude))
  11433. return 0;
  11434. entry->exclude_inherit = exclude;
  11435. } else {
  11436. if (!skill_exists)
  11437. entry->exclude_inherit = false;
  11438. }
  11439. if (!skill_exists)
  11440. tree->skills.insert({ skill_id, entry });
  11441. }
  11442. }
  11443. if (!exists)
  11444. this->put(job_id, tree);
  11445. return true;
  11446. }
  11447. void SkillTreeDatabase::loadingFinished() {
  11448. std::unordered_map<uint16, std::shared_ptr<s_skill_tree>> job_tree; // get the data from skill_tree_db before populate it
  11449. for (auto &data : *this) {
  11450. if (data.second->inherit_job.empty())
  11451. continue;
  11452. std::shared_ptr<s_skill_tree> skill_tree = std::make_shared<s_skill_tree>();
  11453. uint32 baselv_max = job_db.get_maxBaseLv(data.first);
  11454. uint32 joblv_max = job_db.get_maxJobLv(data.first);
  11455. for (const auto &inherit_job : data.second->inherit_job) {
  11456. std::shared_ptr<s_skill_tree> tree = this->find(inherit_job);
  11457. if (tree == nullptr || tree->skills.empty())
  11458. continue;
  11459. for (const auto &it : tree->skills) {
  11460. if (it.second->exclude_inherit)
  11461. continue;
  11462. if (data.second->skills.count(it.first) > 0) // skill already in the skill tree
  11463. continue;
  11464. if (skill_tree->skills.count(it.first) > 0) // replaced by the last inheritance
  11465. skill_tree->skills[it.first] = it.second;
  11466. else
  11467. skill_tree->skills.insert({ it.first, it.second });
  11468. std::shared_ptr<s_skill_tree_entry> skill = skill_tree->skills[it.first];
  11469. if (skill->baselv > baselv_max) {
  11470. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11471. skill_get_name(skill->skill_id), skill->skill_id, skill->baselv, job_name(data.first), baselv_max);
  11472. skill->baselv = baselv_max;
  11473. }
  11474. if (skill->joblv > joblv_max) {
  11475. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11476. skill_get_name(skill->skill_id), skill->skill_id, skill->joblv, job_name(data.first), joblv_max);
  11477. skill->joblv = joblv_max;
  11478. }
  11479. }
  11480. }
  11481. if (skill_tree != nullptr && !skill_tree->skills.empty())
  11482. job_tree.insert({ data.first, skill_tree });
  11483. }
  11484. if (!job_tree.empty()) {
  11485. for (auto &data : *this) {
  11486. if (job_tree.count(data.first) == 0)
  11487. continue;
  11488. data.second->skills.insert(job_tree[data.first]->skills.begin(), job_tree[data.first]->skills.end());
  11489. }
  11490. }
  11491. // remove skills with max_lv = 0
  11492. for (const auto &job : *this) {
  11493. if (job.second->skills.empty())
  11494. continue;
  11495. auto it = job.second->skills.begin();
  11496. while( it != job.second->skills.end() ){
  11497. if( it->second->max_lv == 0 ){
  11498. it = job.second->skills.erase( it );
  11499. }else{
  11500. it++;
  11501. }
  11502. }
  11503. }
  11504. TypesafeYamlDatabase::loadingFinished();
  11505. }
  11506. /**
  11507. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  11508. * @param level: Base level of player
  11509. * @param job_id: Job ID @see enum e_job
  11510. * @return base_hp
  11511. * @author [Cydh]
  11512. */
  11513. static unsigned int pc_calc_basehp(uint16 level, uint16 job_id) {
  11514. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11515. double base_hp = 35 + level * (job->hp_increase / 100.);
  11516. #ifndef RENEWAL
  11517. if (level >= 10 && (job_id == JOB_NINJA || job_id == JOB_GUNSLINGER))
  11518. base_hp += 90;
  11519. #endif
  11520. for (uint16 i = 2; i <= level; i++)
  11521. base_hp += floor(((job->hp_factor / 100.) * i) + 0.5); //Don't have round()
  11522. if (job_id == JOB_SUMMONER || job_id == JOB_SPIRIT_HANDLER)
  11523. base_hp += floor((base_hp / 2) + 0.5);
  11524. return (unsigned int)base_hp;
  11525. }
  11526. /**
  11527. * Calculates base sp of player.
  11528. * @param level: Base level of player
  11529. * @param job_id: Job ID @see enum e_job
  11530. * @return base_sp
  11531. * @author [Playtester]
  11532. */
  11533. static unsigned int pc_calc_basesp(uint16 level, uint16 job_id) {
  11534. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11535. double base_sp = 10 + floor(level * (job->sp_increase / 100.));
  11536. switch (job_id) {
  11537. case JOB_NINJA:
  11538. if (level >= 10)
  11539. base_sp -= 22;
  11540. else
  11541. base_sp = 11 + 3*level;
  11542. break;
  11543. case JOB_GUNSLINGER:
  11544. if (level > 10)
  11545. base_sp -= 18;
  11546. else
  11547. base_sp = 9 + 3*level;
  11548. break;
  11549. case JOB_SUMMONER:
  11550. case JOB_SPIRIT_HANDLER:
  11551. base_sp -= floor(base_sp / 2);
  11552. break;
  11553. }
  11554. return (unsigned int)base_sp;
  11555. }
  11556. const std::string JobDatabase::getDefaultLocation() {
  11557. return std::string(db_path) + "/job_stats.yml";
  11558. }
  11559. /**
  11560. * Reads and parses an entry from the job_db.
  11561. * @param node: YAML node containing the entry.
  11562. * @return count of successfully parsed rows
  11563. */
  11564. uint64 JobDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11565. if (this->nodeExists(node, "Jobs")) {
  11566. const ryml::NodeRef& jobsNode = node["Jobs"];
  11567. for (const auto &jobit : jobsNode) {
  11568. std::string job_name;
  11569. c4::from_chars(jobit.key(), &job_name);
  11570. std::string job_name_constant = "JOB_" + job_name;
  11571. int64 job_id;
  11572. if (!script_get_constant(job_name_constant.c_str(), &job_id)) {
  11573. this->invalidWarning(node["Job"], "Job %s does not exist.\n", job_name.c_str());
  11574. return 0;
  11575. }
  11576. std::shared_ptr<s_job_info> job = job_db.find(static_cast<uint16>(job_id));
  11577. bool exists = job != nullptr;
  11578. if (!exists) {
  11579. job = std::make_shared<s_job_info>();
  11580. job->job_bonus.resize(MAX_LEVEL);
  11581. std::fill(job->job_bonus.begin(), job->job_bonus.end(), std::array<uint16, PARAM_MAX> { 0 });
  11582. job->base_hp.resize(MAX_LEVEL);
  11583. std::fill(job->base_hp.begin(), job->base_hp.end(), 0);
  11584. job->base_sp.resize(MAX_LEVEL);
  11585. std::fill(job->base_sp.begin(), job->base_sp.end(), 0);
  11586. job->base_ap.resize(MAX_LEVEL);
  11587. std::fill(job->base_ap.begin(), job->base_ap.end(), 0);
  11588. }
  11589. if (this->nodeExists(node, "MaxWeight")) {
  11590. uint32 weight;
  11591. if (!this->asUInt32(node, "MaxWeight", weight))
  11592. return 0;
  11593. job->max_weight_base = weight;
  11594. } else {
  11595. if (!exists)
  11596. job->max_weight_base = 20000;
  11597. }
  11598. if (this->nodeExists(node, "HpFactor")) {
  11599. uint32 hp;
  11600. if (!this->asUInt32(node, "HpFactor", hp))
  11601. return 0;
  11602. job->hp_factor = hp;
  11603. } else {
  11604. if (!exists)
  11605. job->hp_factor = 0;
  11606. }
  11607. if (this->nodeExists(node, "HpIncrease")) {
  11608. uint32 hp;
  11609. if (!this->asUInt32(node, "HpIncrease", hp))
  11610. return 0;
  11611. job->hp_increase = hp;
  11612. } else {
  11613. if (!exists)
  11614. job->hp_increase = 500;
  11615. }
  11616. if (this->nodeExists(node, "SpIncrease")) {
  11617. uint32 sp;
  11618. if (!this->asUInt32(node, "SpIncrease", sp))
  11619. return 0;
  11620. job->sp_increase = sp;
  11621. } else {
  11622. if (!exists)
  11623. job->sp_increase = 100;
  11624. }
  11625. if (this->nodeExists(node, "BaseASPD")) {
  11626. const ryml::NodeRef& aspdNode = node["BaseASPD"];
  11627. uint8 max = MAX_WEAPON_TYPE;
  11628. #ifdef RENEWAL // Renewal adds an extra column for shields
  11629. max += 1;
  11630. #endif
  11631. if (!exists) {
  11632. job->aspd_base.resize(max);
  11633. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11634. }
  11635. for (const auto &aspdit : aspdNode) {
  11636. std::string weapon;
  11637. c4::from_chars(aspdit.key(), &weapon);
  11638. std::string weapon_constant = "W_" + weapon;
  11639. int64 constant;
  11640. if (!script_get_constant(weapon_constant.c_str(), &constant)) {
  11641. this->invalidWarning(aspdNode["BaseASPD"], "Unknown weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11642. continue;
  11643. }
  11644. if (constant < W_FIST || constant > max) {
  11645. this->invalidWarning(aspdNode["BaseASPD"], "Invalid weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11646. continue;
  11647. }
  11648. int16 aspd;
  11649. if (!this->asInt16(aspdNode, weapon.c_str(), aspd))
  11650. return 0;
  11651. job->aspd_base[static_cast<int16>(constant)] = aspd;
  11652. }
  11653. } else {
  11654. if (!exists) {
  11655. uint8 max = MAX_WEAPON_TYPE;
  11656. #ifdef RENEWAL // Renewal adds an extra column for shields
  11657. max += 1;
  11658. #endif
  11659. job->aspd_base.resize(max);
  11660. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11661. }
  11662. }
  11663. if (this->nodeExists(node, "MaxStats")) {
  11664. const ryml::NodeRef& statNode = node["MaxStats"];
  11665. for (const auto &statit : statNode) {
  11666. std::string stat;
  11667. c4::from_chars(statit.key(), &stat);
  11668. std::string stat_constant = "PARAM_" + stat;
  11669. int64 constant;
  11670. if (!script_get_constant(stat_constant.c_str(), &constant)) {
  11671. this->invalidWarning(statNode["Bonus"], "Unknown max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11672. continue;
  11673. }
  11674. if (constant < PARAM_STR || constant >= PARAM_MAX) {
  11675. this->invalidWarning(statNode["Bonus"], "Invalid max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11676. continue;
  11677. }
  11678. uint16 max;
  11679. if (!this->asUInt16(statNode, stat.c_str(), max))
  11680. return 0;
  11681. job->max_param[constant] = max;
  11682. }
  11683. }
  11684. if (this->nodeExists(node, "MaxBaseLevel")) {
  11685. uint16 level;
  11686. if (!this->asUInt16(node, "MaxBaseLevel", level))
  11687. return 0;
  11688. if (level == 0 || level > MAX_LEVEL) {
  11689. this->invalidWarning(node["MaxBaseLevel"], "MaxBaseLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11690. level = MAX_LEVEL;
  11691. }
  11692. job->max_base_level = level;
  11693. } else {
  11694. if (!exists)
  11695. job->max_base_level = MAX_LEVEL;
  11696. }
  11697. if (this->nodeExists(node, "BaseExp")) {
  11698. for (const ryml::NodeRef& bexpNode : node["BaseExp"]) {
  11699. uint16 level;
  11700. if (!this->asUInt16(bexpNode, "Level", level))
  11701. return 0;
  11702. if (level > job->max_base_level)
  11703. continue;
  11704. if (level == 0 || level > MAX_LEVEL) {
  11705. this->invalidWarning(bexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11706. return 0;
  11707. }
  11708. if (this->nodeExists(bexpNode, "Exp")) {
  11709. t_exp exp;
  11710. if (!this->asUInt64(bexpNode, "Exp", exp))
  11711. return 0;
  11712. job->base_exp[level - 1] = exp;
  11713. }
  11714. }
  11715. }
  11716. if (this->nodeExists(node, "MaxJobLevel")) {
  11717. uint16 level;
  11718. if (!this->asUInt16(node, "MaxJobLevel", level))
  11719. return 0;
  11720. if (level == 0 || level > MAX_LEVEL) {
  11721. this->invalidWarning(node["MaxJobLevel"], "MaxJobLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11722. level = MAX_LEVEL;
  11723. }
  11724. job->max_job_level = level;
  11725. } else {
  11726. if (!exists)
  11727. job->max_job_level = MAX_LEVEL;
  11728. }
  11729. if (this->nodeExists(node, "JobExp")) {
  11730. for (const ryml::NodeRef& jexpNode : node["JobExp"]) {
  11731. uint16 level;
  11732. if (!this->asUInt16(jexpNode, "Level", level))
  11733. return 0;
  11734. if (level > job->max_job_level)
  11735. continue;
  11736. if (level == 0 || level > MAX_LEVEL) {
  11737. this->invalidWarning(jexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11738. return 0;
  11739. }
  11740. if (this->nodeExists(jexpNode, "Exp")) {
  11741. t_exp exp;
  11742. if (!this->asUInt64(jexpNode, "Exp", exp))
  11743. return 0;
  11744. job->job_exp[level - 1] = exp;
  11745. }
  11746. }
  11747. }
  11748. if (this->nodeExists(node, "BonusStats")) {
  11749. const ryml::NodeRef& bonusNode = node["BonusStats"];
  11750. for (const ryml::NodeRef& levelNode : bonusNode) {
  11751. uint16 level;
  11752. if (!this->asUInt16(levelNode, "Level", level))
  11753. return 0;
  11754. if (level == 0 || level > MAX_LEVEL) {
  11755. this->invalidWarning(levelNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11756. return 0;
  11757. }
  11758. for (uint8 idx = PARAM_STR; idx < PARAM_MAX; idx++) {
  11759. if (this->nodeExists(levelNode, parameter_names[idx])) {
  11760. int16 change;
  11761. if (!this->asInt16(levelNode, parameter_names[idx], change))
  11762. return 0;
  11763. job->job_bonus[level - 1][idx] = change;
  11764. }
  11765. }
  11766. }
  11767. }
  11768. #ifdef HP_SP_TABLES
  11769. if (this->nodeExists(node, "BaseHp")) {
  11770. for (const ryml::NodeRef& bhpNode : node["BaseHp"]) {
  11771. uint16 level;
  11772. if (!this->asUInt16(bhpNode, "Level", level))
  11773. return 0;
  11774. if (level > job->max_base_level)
  11775. continue;
  11776. if (level == 0 || level > MAX_LEVEL) {
  11777. this->invalidWarning(bhpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11778. return 0;
  11779. }
  11780. if (this->nodeExists(bhpNode, "Hp")) {
  11781. uint32 points;
  11782. if (!this->asUInt32(bhpNode, "Hp", points))
  11783. return 0;
  11784. job->base_hp[level - 1] = points;
  11785. }
  11786. }
  11787. }
  11788. if (this->nodeExists(node, "BaseSp")) {
  11789. for (const ryml::NodeRef& bspNode : node["BaseSp"]) {
  11790. uint16 level;
  11791. if (!this->asUInt16(bspNode, "Level", level))
  11792. return 0;
  11793. if (level > job->max_base_level)
  11794. continue;
  11795. if (level == 0 || level > MAX_LEVEL) {
  11796. this->invalidWarning(bspNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11797. return 0;
  11798. }
  11799. if (this->nodeExists(bspNode, "Sp")) {
  11800. uint32 points;
  11801. if (!this->asUInt32(bspNode, "Sp", points))
  11802. return 0;
  11803. job->base_sp[level - 1] = points;
  11804. }
  11805. }
  11806. }
  11807. if (this->nodeExists(node, "BaseAp")) {
  11808. for (const ryml::NodeRef& bapNode : node["BaseAp"]) {
  11809. uint16 level;
  11810. if (!this->asUInt16(bapNode, "Level", level))
  11811. return 0;
  11812. if (level > job->max_base_level)
  11813. continue;
  11814. if (level == 0 || level > MAX_LEVEL) {
  11815. this->invalidWarning(bapNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11816. return 0;
  11817. }
  11818. if (this->nodeExists(bapNode, "Ap")) {
  11819. uint32 points;
  11820. if (!this->asUInt32(bapNode, "Ap", points))
  11821. return 0;
  11822. job->base_ap[level - 1] = points;
  11823. }
  11824. }
  11825. }
  11826. #endif
  11827. if (!exists)
  11828. this->put(static_cast<uint16>(job_id), job);
  11829. }
  11830. }
  11831. return 1;
  11832. }
  11833. void JobDatabase::loadingFinished() {
  11834. // Checking if all class have their data
  11835. for (auto &jobIt : *this) {
  11836. uint16 job_id = jobIt.first;
  11837. if (!pcdb_checkid(job_id))
  11838. continue;
  11839. 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)
  11840. continue; // Classes that do not need exp tables.
  11841. std::shared_ptr<s_job_info> job = jobIt.second;
  11842. uint16 maxBaseLv = job->max_base_level, maxJobLv = job->max_job_level;
  11843. if (!maxBaseLv)
  11844. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(job_id), job_id);
  11845. if (!maxJobLv)
  11846. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(job_id), job_id);
  11847. // Init and checking the empty value of Base HP/SP [Cydh]
  11848. if (job->base_hp.empty())
  11849. job->base_hp.resize(maxBaseLv);
  11850. for (uint16 j = 0; j < maxBaseLv; j++) {
  11851. if (job->base_hp[j] == 0)
  11852. job->base_hp[j] = pc_calc_basehp(j + 1, job_id);
  11853. }
  11854. if (job->base_sp.empty())
  11855. job->base_sp.resize(maxBaseLv);
  11856. for (uint16 j = 0; j < maxBaseLv; j++) {
  11857. if (job->base_sp[j] == 0)
  11858. job->base_sp[j] = pc_calc_basesp(j + 1, job_id);
  11859. }
  11860. // Resize to the maximum base level
  11861. if (job->base_hp.capacity() > maxBaseLv)
  11862. job->base_hp.erase(job->base_hp.begin() + maxBaseLv, job->base_hp.end());
  11863. if (job->base_sp.capacity() > maxBaseLv)
  11864. job->base_sp.erase(job->base_sp.begin() + maxBaseLv, job->base_sp.end());
  11865. if (job->base_ap.capacity() > maxBaseLv)
  11866. job->base_ap.erase(job->base_ap.begin() + maxBaseLv, job->base_ap.end());
  11867. // Resize to the maximum job level
  11868. if (job->job_bonus.capacity() > maxJobLv)
  11869. job->job_bonus.erase(job->job_bonus.begin() + maxJobLv, job->job_bonus.end());
  11870. for (uint16 parameter = PARAM_STR; parameter < PARAM_MAX; parameter++) {
  11871. // Store total
  11872. int16 current = 0;
  11873. for (uint16 job_level = 0; job_level < maxJobLv; job_level++) {
  11874. // Add the bonus from this job level
  11875. current += job->job_bonus[job_level][parameter];
  11876. // Set the new total on this job level
  11877. job->job_bonus[job_level][parameter] = current;
  11878. }
  11879. }
  11880. uint64 class_ = pc_jobid2mapid( job_id );
  11881. // Set normal status limits
  11882. uint16 max = battle_config.max_parameter;
  11883. do{
  11884. // Always check babies first
  11885. if( class_ & JOBL_BABY ){
  11886. if( class_ & JOBL_THIRD ){
  11887. max = battle_config.max_baby_third_parameter;
  11888. break;
  11889. }else{
  11890. max = battle_config.max_baby_parameter;
  11891. break;
  11892. }
  11893. }
  11894. // Summoner
  11895. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11896. max = battle_config.max_summoner_parameter;
  11897. break;
  11898. }
  11899. // Extended classes
  11900. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11901. max = battle_config.max_extended_parameter;
  11902. break;
  11903. }
  11904. if( class_ & JOBL_FOURTH ){
  11905. max = battle_config.max_fourth_parameter;
  11906. break;
  11907. }
  11908. // 3rd class
  11909. if( class_ & JOBL_THIRD ){
  11910. // Transcendent
  11911. if( class_ & JOBL_UPPER ){
  11912. max = battle_config.max_third_trans_parameter;
  11913. break;
  11914. }else{
  11915. max = battle_config.max_third_parameter;
  11916. break;
  11917. }
  11918. }
  11919. // Transcendent
  11920. if( class_ & JOBL_UPPER ){
  11921. max = battle_config.max_trans_parameter;
  11922. break;
  11923. }
  11924. }while( false );
  11925. for( uint16 parameter = PARAM_STR; parameter < PARAM_POW; parameter++ ){
  11926. // If it is not explicitly set in the database file
  11927. if( job->max_param[parameter] == 0 ){
  11928. job->max_param[parameter] = max;
  11929. }
  11930. }
  11931. // Set trait status limit
  11932. if( class_ & JOBL_FOURTH ){
  11933. max = battle_config.max_trait_parameter;
  11934. }else{
  11935. max = 0;
  11936. }
  11937. for( uint16 parameter = PARAM_POW; parameter < PARAM_MAX; parameter++ ){
  11938. // If it is not explicitly set in the database file
  11939. if( job->max_param[parameter] == 0 ){
  11940. job->max_param[parameter] = max;
  11941. }
  11942. }
  11943. }
  11944. TypesafeCachedYamlDatabase::loadingFinished();
  11945. }
  11946. /**
  11947. * Read job_noenter_map.txt
  11948. **/
  11949. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  11950. int class_ = -1;
  11951. int64 class_tmp;
  11952. if (ISDIGIT(str[0][0])) {
  11953. class_ = atoi(str[0]);
  11954. } else {
  11955. if (!script_get_constant(str[0], &class_tmp)) {
  11956. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  11957. return false;
  11958. }
  11959. class_ = static_cast<int>(class_tmp);
  11960. }
  11961. if (!pcdb_checkid(class_)) {
  11962. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  11963. return false;
  11964. }
  11965. std::shared_ptr<s_job_info> job = job_db.find(class_);
  11966. if (job == nullptr) {
  11967. ShowError("pc_readdb_job_noenter_map: Job %d data not initialized.\n", class_);
  11968. return false;
  11969. }
  11970. job->noenter_map.zone = atoi(str[1]);
  11971. job->noenter_map.group_lv = atoi(str[2]);
  11972. return true;
  11973. }
  11974. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  11975. return std::string(db_path) + "/statpoint.yml";
  11976. }
  11977. uint64 PlayerStatPointDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11978. if (!this->nodesExist(node, { "Level", "Points" })) {
  11979. return 0;
  11980. }
  11981. uint16 level;
  11982. if (!this->asUInt16(node, "Level", level))
  11983. return 0;
  11984. uint32 point;
  11985. if (!this->asUInt32(node, "Points", point))
  11986. return 0;
  11987. if (level == 0) {
  11988. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  11989. return 0;
  11990. }
  11991. if (level > MAX_LEVEL) {
  11992. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  11993. return 0;
  11994. }
  11995. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  11996. bool exists = entry != nullptr;
  11997. if( !exists ){
  11998. entry = std::make_shared<s_statpoint_entry>();
  11999. entry->level = level;
  12000. entry->statpoints = point;
  12001. }
  12002. if( this->nodeExists( node, "TraitPoints" ) ){
  12003. uint32 traitpoints;
  12004. if( !this->asUInt32( node, "TraitPoints", traitpoints ) ){
  12005. return 0;
  12006. }
  12007. entry->traitpoints = traitpoints;
  12008. }else{
  12009. if( !exists ){
  12010. entry->traitpoints = 0;
  12011. }
  12012. }
  12013. if( !exists ){
  12014. this->put( level, entry );
  12015. }
  12016. return 1;
  12017. }
  12018. /**
  12019. * Generate the remaining parts of the db if necessary.
  12020. */
  12021. void PlayerStatPointDatabase::loadingFinished(){
  12022. const uint16 trait_start_level = 200;
  12023. std::shared_ptr<s_statpoint_entry> level_one = this->find( 1 );
  12024. if( level_one == nullptr ){
  12025. if( battle_config.use_statpoint_table ){
  12026. ShowError( "Missing status points for Level 1\n" );
  12027. }
  12028. level_one = std::make_shared<s_statpoint_entry>();
  12029. level_one->level = 1;
  12030. level_one->statpoints = inter_config.start_status_points;
  12031. level_one->traitpoints = 0;
  12032. this->put( 1, level_one );
  12033. }else if( battle_config.use_statpoint_table ){
  12034. if( level_one->statpoints != inter_config.start_status_points ){
  12035. ShowError( "Status points for Level 1 (=%u) do not match inter_athena.conf value (=%u).\n", level_one->statpoints, inter_config.start_status_points );
  12036. level_one->statpoints = inter_config.start_status_points;
  12037. }
  12038. }else{
  12039. level_one->statpoints = inter_config.start_status_points;
  12040. level_one->traitpoints = 0;
  12041. }
  12042. std::shared_ptr<s_statpoint_entry> last_level = level_one;
  12043. for( uint16 level = 2; level <= MAX_LEVEL; level++ ){
  12044. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12045. bool exists = entry != nullptr;
  12046. if( !exists ){
  12047. entry = std::make_shared<s_statpoint_entry>();
  12048. entry->level = level;
  12049. this->put( level, entry );
  12050. }
  12051. if( !battle_config.use_statpoint_table || !exists ){
  12052. if( battle_config.use_statpoint_table ){
  12053. ShowError("Missing status points for Level %hu\n", level);
  12054. }
  12055. if( level <= trait_start_level ){
  12056. entry->statpoints = last_level->statpoints + ( ( level - 1 + 15 ) / 5 );
  12057. }else{
  12058. entry->statpoints = last_level->statpoints;
  12059. }
  12060. }
  12061. if( !battle_config.use_traitpoint_table || !exists ){
  12062. if( battle_config.use_traitpoint_table && level > trait_start_level ){
  12063. ShowError( "Missing trait points for Level %hu\n", level );
  12064. }
  12065. if( level > trait_start_level ){
  12066. entry->traitpoints = ( level - trait_start_level ) * 3 + ( level - trait_start_level ) / 5 * 4;
  12067. }else{
  12068. entry->traitpoints = 0;
  12069. }
  12070. }
  12071. // Store it for next iteration
  12072. last_level = entry;
  12073. }
  12074. TypesafeCachedYamlDatabase::loadingFinished();
  12075. }
  12076. /*==========================================
  12077. * pc DB reading.
  12078. * job_stats.yml - Job values
  12079. * skill_tree.txt - skill tree for every class
  12080. * attr_fix.yml - elemental adjustment table
  12081. *------------------------------------------*/
  12082. void pc_readdb(void) {
  12083. int i, s = 1;
  12084. const char* dbsubpath[] = {
  12085. "",
  12086. "/" DBIMPORT,
  12087. //add other path here
  12088. };
  12089. //reset
  12090. job_db.clear(); // job_info table
  12091. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  12092. penalty_db.load();
  12093. #endif
  12094. statpoint_db.clear();
  12095. job_db.load();
  12096. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  12097. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  12098. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  12099. char* dbsubpath1 = (char*)aMalloc(n1+1);
  12100. char* dbsubpath2 = (char*)aMalloc(n2+1);
  12101. if(i==0) {
  12102. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12103. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  12104. }
  12105. else {
  12106. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12107. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  12108. }
  12109. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  12110. aFree(dbsubpath1);
  12111. aFree(dbsubpath2);
  12112. }
  12113. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  12114. skill_tree_db.reload();
  12115. statpoint_db.load();
  12116. }
  12117. // Read MOTD on startup. [Valaris]
  12118. int pc_read_motd(void)
  12119. {
  12120. FILE* fp;
  12121. // clear old MOTD
  12122. memset(motd_text, 0, sizeof(motd_text));
  12123. // read current MOTD
  12124. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  12125. {
  12126. unsigned int entries = 0;
  12127. char buf[CHAT_SIZE_MAX];
  12128. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  12129. {
  12130. unsigned int lines = 0;
  12131. size_t len;
  12132. lines++;
  12133. if( buf[0] == '/' && buf[1] == '/' )
  12134. continue;
  12135. len = strlen(buf);
  12136. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  12137. len--;
  12138. if( len ) {
  12139. char * ptr;
  12140. buf[len] = 0;
  12141. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  12142. 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);
  12143. }
  12144. else {// empty line
  12145. buf[0] = ' ';
  12146. buf[1] = 0;
  12147. }
  12148. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  12149. entries++;
  12150. }
  12151. fclose(fp);
  12152. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  12153. }
  12154. else
  12155. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  12156. return 0;
  12157. }
  12158. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  12159. int i,cursor = 0;
  12160. struct item_cd* cd = NULL;
  12161. if( load ) {
  12162. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  12163. // no item cooldown is associated with this character
  12164. return;
  12165. }
  12166. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12167. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  12168. sd->item_delay[cursor].tick = cd->tick[i];
  12169. sd->item_delay[cursor].nameid = cd->nameid[i];
  12170. cursor++;
  12171. }
  12172. }
  12173. idb_remove(itemcd_db,sd->status.char_id);
  12174. } else {
  12175. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  12176. // create a new skill cooldown object for map storage
  12177. CREATE( cd, struct item_cd, 1 );
  12178. idb_put( itemcd_db, sd->status.char_id, cd );
  12179. }
  12180. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12181. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  12182. cd->tick[cursor] = sd->item_delay[i].tick;
  12183. cd->nameid[cursor] = sd->item_delay[i].nameid;
  12184. cursor++;
  12185. }
  12186. }
  12187. }
  12188. return;
  12189. }
  12190. /**
  12191. * Add item delay to player's item delay data
  12192. * @param sd Player
  12193. * @param id Item data
  12194. * @param tick Current tick
  12195. * @param n Item index in inventory
  12196. * @return 0: No delay, can consume item.
  12197. * 1: Has delay, cancel consumption.
  12198. **/
  12199. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12200. int i;
  12201. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  12202. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  12203. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  12204. if( i < MAX_ITEMDELAYS ) {
  12205. if( sd->item_delay[i].nameid ) {// found
  12206. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  12207. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  12208. char e_msg[CHAT_SIZE_MAX];
  12209. if( e_tick > 99 )
  12210. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  12211. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  12212. else
  12213. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  12214. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  12215. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  12216. return 1; // Delay has not expired yet
  12217. }
  12218. } else {// not yet used item (all slots are initially empty)
  12219. sd->item_delay[i].nameid = id->nameid;
  12220. }
  12221. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  12222. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  12223. } else {// should not happen
  12224. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  12225. }
  12226. //clean up used delays so we can give room for more
  12227. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12228. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  12229. sd->item_delay[i].tick = 0;
  12230. sd->item_delay[i].nameid = 0;
  12231. }
  12232. }
  12233. return 0;
  12234. }
  12235. /**
  12236. * Check if player has delay to reuse item
  12237. * @param sd Player
  12238. * @param id Item data
  12239. * @param tick Current tick
  12240. * @param n Item index in inventory
  12241. * @return 0: No delay, can consume item.
  12242. * 1: Has delay, cancel consumption.
  12243. **/
  12244. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12245. struct status_change *sc = NULL;
  12246. nullpo_retr(0, sd);
  12247. nullpo_retr(0, id);
  12248. // Do normal delay assignment
  12249. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  12250. return pc_itemcd_add(sd, id, tick, n);
  12251. // Send reply of delay remains
  12252. if (sc->data[id->delay.sc]) {
  12253. const struct TimerData *timer = get_timer(sc->data[id->delay.sc]->timer);
  12254. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  12255. return 1;
  12256. }
  12257. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  12258. return 0;
  12259. }
  12260. /**
  12261. * Clear the dmglog data from player
  12262. * @param sd
  12263. * @param md
  12264. **/
  12265. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  12266. {
  12267. uint8 i;
  12268. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  12269. if (i < DAMAGELOG_SIZE) {
  12270. md->dmglog[i].id = 0;
  12271. md->dmglog[i].dmg = 0;
  12272. md->dmglog[i].flag = 0;
  12273. }
  12274. }
  12275. /**
  12276. * Add log to player's dmglog
  12277. * @param sd
  12278. * @param id Monster's GID
  12279. **/
  12280. void pc_damage_log_add(struct map_session_data *sd, int id)
  12281. {
  12282. uint8 i = 0;
  12283. if (!sd || !id)
  12284. return;
  12285. //Only store new data, don't need to renew the old one with same id
  12286. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  12287. if (i < DAMAGELOG_SIZE_PC)
  12288. return;
  12289. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12290. if (sd->dmglog[i] == 0) {
  12291. sd->dmglog[i] = id;
  12292. return;
  12293. }
  12294. }
  12295. }
  12296. /**
  12297. * Clear dmglog data from player
  12298. * @param sd
  12299. * @param id Monster's id
  12300. **/
  12301. void pc_damage_log_clear(struct map_session_data *sd, int id)
  12302. {
  12303. uint8 i;
  12304. struct mob_data *md = NULL;
  12305. if (!sd)
  12306. return;
  12307. if (!id) {
  12308. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12309. if( !sd->dmglog[i] ) //skip the empty value
  12310. continue;
  12311. if ((md = map_id2md(sd->dmglog[i])))
  12312. pc_clear_log_damage_sub(sd->status.char_id,md);
  12313. sd->dmglog[i] = 0;
  12314. }
  12315. }
  12316. else {
  12317. if ((md = map_id2md(id)))
  12318. pc_clear_log_damage_sub(sd->status.char_id,md);
  12319. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  12320. if (i < DAMAGELOG_SIZE_PC)
  12321. sd->dmglog[i] = 0;
  12322. }
  12323. }
  12324. /**
  12325. * Status change data arrived from char-server
  12326. * @param sd: Player data
  12327. */
  12328. void pc_scdata_received(struct map_session_data *sd) {
  12329. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  12330. clif_weight_limit( sd );
  12331. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) && pc_attendance_counter(sd) < 200 ){
  12332. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  12333. }
  12334. sd->state.pc_loaded = true;
  12335. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  12336. sd->state.connect_new = 1;
  12337. clif_parse_LoadEndAck(sd->fd, sd);
  12338. }
  12339. if (pc_iscarton(sd)) {
  12340. sd->cart_weight_max = 0; // Force a client refesh
  12341. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  12342. }
  12343. if (sd->sc.data[SC_SOULENERGY])
  12344. sd->soulball = sd->sc.data[SC_SOULENERGY]->val1;
  12345. }
  12346. /**
  12347. * Check player account expiration time and rental item expirations
  12348. * @param sd: Player data
  12349. */
  12350. void pc_check_expiration(struct map_session_data *sd) {
  12351. #ifndef ENABLE_SC_SAVING
  12352. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  12353. #endif
  12354. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  12355. time_t exp_time = sd->expiration_time;
  12356. char tmpstr[1024];
  12357. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  12358. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  12359. pc_expire_check(sd);
  12360. }
  12361. }
  12362. TIMER_FUNC(pc_expiration_timer){
  12363. struct map_session_data *sd = map_id2sd(id);
  12364. if( !sd ) return 0;
  12365. sd->expiration_tid = INVALID_TIMER;
  12366. if( sd->fd )
  12367. clif_authfail_fd(sd->fd,10);
  12368. map_quit(sd);
  12369. return 0;
  12370. }
  12371. TIMER_FUNC(pc_autotrade_timer){
  12372. struct map_session_data *sd = map_id2sd(id);
  12373. if (!sd)
  12374. return 0;
  12375. sd->autotrade_tid = INVALID_TIMER;
  12376. if (sd->state.autotrade&2)
  12377. vending_reopen(sd);
  12378. if (sd->state.autotrade&4)
  12379. buyingstore_reopen(sd);
  12380. if (!sd->vender_id && !sd->buyer_id) {
  12381. sd->state.autotrade = 0;
  12382. map_quit(sd);
  12383. }
  12384. return 0;
  12385. }
  12386. /* this timer exists only when a character with a expire timer > 24h is online */
  12387. /* it loops thru online players once an hour to check whether a new < 24h is available */
  12388. TIMER_FUNC(pc_global_expiration_timer){
  12389. struct s_mapiterator* iter;
  12390. struct map_session_data* sd;
  12391. iter = mapit_getallusers();
  12392. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  12393. if( sd->expiration_time )
  12394. pc_expire_check(sd);
  12395. mapit_free(iter);
  12396. return 0;
  12397. }
  12398. void pc_expire_check(struct map_session_data *sd) {
  12399. /* ongoing timer */
  12400. if( sd->expiration_tid != INVALID_TIMER )
  12401. return;
  12402. /* not within the next 24h, enable the global check */
  12403. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  12404. /* global check not running, enable */
  12405. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  12406. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  12407. return;
  12408. }
  12409. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  12410. }
  12411. /**
  12412. * Deposit some money to bank
  12413. * @param sd
  12414. * @param money Amount of money to deposit
  12415. **/
  12416. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  12417. unsigned int limit_check = money + sd->bank_vault;
  12418. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  12419. return BDA_OVERFLOW;
  12420. } else if ( money > sd->status.zeny ) {
  12421. return BDA_NO_MONEY;
  12422. }
  12423. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  12424. return BDA_NO_MONEY;
  12425. sd->bank_vault += money;
  12426. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12427. if( save_settings&CHARSAVE_BANK )
  12428. chrif_save(sd, CSAVE_NORMAL);
  12429. return BDA_SUCCESS;
  12430. }
  12431. /**
  12432. * Withdraw money from bank
  12433. * @param sd
  12434. * @param money Amount of money that will be withdrawn
  12435. **/
  12436. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  12437. unsigned int limit_check = money + sd->status.zeny;
  12438. if( money <= 0 ) {
  12439. return BWA_UNKNOWN_ERROR;
  12440. } else if ( money > sd->bank_vault ) {
  12441. return BWA_NO_MONEY;
  12442. } else if ( limit_check > MAX_ZENY ) {
  12443. /* no official response for this scenario exists. */
  12444. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  12445. return BWA_UNKNOWN_ERROR;
  12446. }
  12447. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  12448. return BWA_NO_MONEY;
  12449. sd->bank_vault -= money;
  12450. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12451. if( save_settings&CHARSAVE_BANK )
  12452. chrif_save(sd, CSAVE_NORMAL);
  12453. return BWA_SUCCESS;
  12454. }
  12455. /**
  12456. * Clear Crimson Marker data from caster
  12457. * @param sd: Player
  12458. **/
  12459. void pc_crimson_marker_clear(struct map_session_data *sd) {
  12460. uint8 i;
  12461. if (!sd)
  12462. return;
  12463. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  12464. struct block_list *bl = NULL;
  12465. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  12466. status_change_end(bl,SC_C_MARKER);
  12467. sd->c_marker[i] = 0;
  12468. }
  12469. }
  12470. /**
  12471. * Show version to player
  12472. * @param sd: Player
  12473. **/
  12474. void pc_show_version(struct map_session_data *sd) {
  12475. const char* svn = get_svn_revision();
  12476. char buf[CHAT_SIZE_MAX];
  12477. if( svn[0] != UNKNOWN_VERSION )
  12478. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  12479. else {
  12480. const char* git = get_git_hash();
  12481. if( git[0] != UNKNOWN_VERSION )
  12482. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  12483. else
  12484. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  12485. }
  12486. clif_displaymessage(sd->fd,buf);
  12487. }
  12488. /**
  12489. * Run bonus_script on player
  12490. * @param sd
  12491. * @author [Cydh]
  12492. **/
  12493. void pc_bonus_script(struct map_session_data *sd) {
  12494. t_tick now = gettick();
  12495. struct linkdb_node *node = NULL, *next = NULL;
  12496. if (!sd || !(node = sd->bonus_script.head))
  12497. return;
  12498. while (node) {
  12499. struct s_bonus_script_entry *entry = NULL;
  12500. next = node->next;
  12501. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  12502. // Only start timer for new bonus_script
  12503. if (entry->tid == INVALID_TIMER) {
  12504. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  12505. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  12506. entry->tick += now;
  12507. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  12508. }
  12509. if (entry->script)
  12510. run_script(entry->script, 0, sd->bl.id, 0);
  12511. else
  12512. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  12513. }
  12514. node = next;
  12515. }
  12516. }
  12517. /**
  12518. * Add bonus_script to player
  12519. * @param sd Player
  12520. * @param script_str Script string
  12521. * @param dur Duration in ms
  12522. * @param icon EFST
  12523. * @param flag Flags @see enum e_bonus_script_flags
  12524. * @param type 0 - None, 1 - Buff, 2 - Debuff
  12525. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  12526. * @author [Cydh]
  12527. **/
  12528. 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) {
  12529. struct script_code *script = NULL;
  12530. struct linkdb_node *node = NULL;
  12531. struct s_bonus_script_entry *entry = NULL;
  12532. if (!sd)
  12533. return NULL;
  12534. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  12535. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  12536. return NULL;
  12537. }
  12538. // Duplication checks
  12539. if ((node = sd->bonus_script.head)) {
  12540. while (node) {
  12541. entry = (struct s_bonus_script_entry *)node->data;
  12542. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  12543. t_tick newdur = gettick() + dur;
  12544. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  12545. settick_timer(entry->tid, newdur);
  12546. script_free_code(script);
  12547. return NULL;
  12548. }
  12549. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  12550. break;
  12551. else { // No duplicate bonus
  12552. script_free_code(script);
  12553. return NULL;
  12554. }
  12555. }
  12556. node = node->next;
  12557. }
  12558. }
  12559. CREATE(entry, struct s_bonus_script_entry, 1);
  12560. entry->script_buf = StringBuf_Malloc();
  12561. StringBuf_AppendStr(entry->script_buf, script_str);
  12562. entry->tid = INVALID_TIMER;
  12563. entry->flag = flag;
  12564. entry->icon = icon;
  12565. entry->tick = dur; // Use duration first, on run change to expire time
  12566. entry->type = type;
  12567. entry->script = script;
  12568. sd->bonus_script.count++;
  12569. return entry;
  12570. }
  12571. /**
  12572. * Remove bonus_script data from player
  12573. * @param sd: Target player
  12574. * @param list: Bonus script entry from player
  12575. * @author [Cydh]
  12576. **/
  12577. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  12578. if (entry->tid != INVALID_TIMER)
  12579. delete_timer(entry->tid, pc_bonus_script_timer);
  12580. if (entry->script)
  12581. script_free_code(entry->script);
  12582. if (entry->script_buf)
  12583. StringBuf_Free(entry->script_buf);
  12584. if (sd) {
  12585. if (entry->icon != EFST_BLANK)
  12586. clif_status_load(&sd->bl, entry->icon, 0);
  12587. if (sd->bonus_script.count > 0)
  12588. sd->bonus_script.count--;
  12589. }
  12590. aFree(entry);
  12591. }
  12592. /**
  12593. * Do final process if no entry left
  12594. * @param sd
  12595. **/
  12596. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  12597. if (sd->bonus_script.count == 0) {
  12598. if (sd->bonus_script.head && sd->bonus_script.head->data)
  12599. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  12600. linkdb_final(&sd->bonus_script.head);
  12601. }
  12602. }
  12603. /**
  12604. * Timer for bonus_script
  12605. * @param tid
  12606. * @param tick
  12607. * @param id
  12608. * @param data
  12609. * @author [Cydh]
  12610. **/
  12611. TIMER_FUNC(pc_bonus_script_timer){
  12612. struct map_session_data *sd;
  12613. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  12614. sd = map_id2sd(id);
  12615. if (!sd) {
  12616. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  12617. return 0;
  12618. }
  12619. if (tid == INVALID_TIMER)
  12620. return 0;
  12621. if (!sd->bonus_script.head || entry == NULL) {
  12622. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  12623. return 0;
  12624. }
  12625. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12626. pc_bonus_script_free_entry(sd, entry);
  12627. pc_bonus_script_check_final(sd);
  12628. status_calc_pc(sd,SCO_NONE);
  12629. return 0;
  12630. }
  12631. /**
  12632. * Check then clear all active timer(s) of bonus_script data from player based on reason
  12633. * @param sd: Target player
  12634. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  12635. * @author [Cydh]
  12636. **/
  12637. void pc_bonus_script_clear(struct map_session_data *sd, uint32 flag) {
  12638. struct linkdb_node *node = NULL;
  12639. uint16 count = 0;
  12640. if (!sd || !(node = sd->bonus_script.head))
  12641. return;
  12642. while (node) {
  12643. struct linkdb_node *next = node->next;
  12644. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  12645. if (entry && (
  12646. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  12647. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  12648. (flag&entry->flag) || // Matched flag
  12649. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  12650. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  12651. )
  12652. )
  12653. {
  12654. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12655. pc_bonus_script_free_entry(sd, entry);
  12656. count++;
  12657. }
  12658. node = next;
  12659. }
  12660. pc_bonus_script_check_final(sd);
  12661. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  12662. status_calc_pc(sd,SCO_NONE);
  12663. }
  12664. /** [Cydh]
  12665. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  12666. * @param sd: Target player
  12667. */
  12668. void pc_cell_basilica(struct map_session_data *sd) {
  12669. nullpo_retv(sd);
  12670. #ifdef RENEWAL
  12671. enum sc_type type = SC_BASILICA_CELL;
  12672. #else
  12673. enum sc_type type = SC_BASILICA;
  12674. #endif
  12675. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  12676. if (sd->sc.data[type])
  12677. status_change_end(&sd->bl, type);
  12678. }
  12679. else if (!sd->sc.data[type])
  12680. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  12681. }
  12682. /** [Cydh]
  12683. * Get maximum specified parameter for specified class
  12684. * @param sd: Player data
  12685. * @param param: Max parameter to check
  12686. * @return max_param
  12687. */
  12688. uint16 pc_maxparameter(struct map_session_data *sd, e_params param) {
  12689. nullpo_retr(0, sd);
  12690. std::shared_ptr<s_job_info> job = job_db.find(pc_mapid2jobid(sd->class_,sd->status.sex));
  12691. if( job == nullptr || param == PARAM_MAX ){
  12692. return 0;
  12693. }
  12694. return job->max_param[param];
  12695. }
  12696. /**
  12697. * Get max ASPD for player based on Class
  12698. * @param sd Player
  12699. * @return ASPD
  12700. */
  12701. short pc_maxaspd(struct map_session_data *sd) {
  12702. nullpo_ret(sd);
  12703. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  12704. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  12705. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  12706. battle_config.max_aspd ));
  12707. }
  12708. /**
  12709. * Calculates total item-group related bonuses for the given item
  12710. * @param sd Player
  12711. * @param nameid Item ID
  12712. * @return Heal rate
  12713. **/
  12714. short pc_get_itemgroup_bonus(struct map_session_data* sd, t_itemid nameid, std::vector<s_item_bonus>& bonuses) {
  12715. if (bonuses.empty())
  12716. return 0;
  12717. short bonus = 0;
  12718. for (const auto &it : bonuses) {
  12719. uint16 group_id = it.id;
  12720. if (group_id == 0)
  12721. continue;
  12722. if (itemdb_group.item_exists(group_id, nameid))
  12723. bonus += it.val;
  12724. }
  12725. return bonus;
  12726. }
  12727. /**
  12728. * Calculates total item-group related bonuses for the given item group
  12729. * @param sd Player
  12730. * @param group_id Item Group ID
  12731. * @return Heal rate
  12732. **/
  12733. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id, std::vector<s_item_bonus>& bonuses) {
  12734. if (bonuses.empty())
  12735. return 0;
  12736. for (const auto &it : bonuses) {
  12737. if (it.id == group_id)
  12738. return it.val;
  12739. }
  12740. return 0;
  12741. }
  12742. /**
  12743. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  12744. * @param eqi Item EQI of enum equip_index
  12745. * @param *equip_index Player's equip_index[]
  12746. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  12747. * @return True if item in same inventory index, False if doesn't
  12748. */
  12749. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  12750. if (index < 0 || index >= MAX_INVENTORY)
  12751. return true;
  12752. // Dual weapon checks
  12753. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  12754. return true;
  12755. // Headgear with Mid & Low location
  12756. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  12757. return true;
  12758. // Headgear with Top & Mid or Low location
  12759. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  12760. return true;
  12761. // Headgear with Mid & Low location
  12762. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  12763. return true;
  12764. // Headgear with Top & Mid or Low location
  12765. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  12766. return true;
  12767. return false;
  12768. }
  12769. /**
  12770. * Generate Unique item ID for player
  12771. * @param sd : Player
  12772. * @return A generated Unique item ID
  12773. */
  12774. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  12775. nullpo_ret(sd);
  12776. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  12777. }
  12778. /**
  12779. * Validating skill from player after logged on
  12780. * @param sd
  12781. **/
  12782. void pc_validate_skill(struct map_session_data *sd) {
  12783. if (sd) {
  12784. uint16 i = 0, count = 0;
  12785. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  12786. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  12787. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  12788. for (i = 0; i < MAX_SKILL; i++) {
  12789. uint16 idx = 0;
  12790. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  12791. continue;
  12792. if ((idx = skill_get_index(tmp_skills[i].id))) {
  12793. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  12794. count++;
  12795. }
  12796. else
  12797. 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);
  12798. }
  12799. }
  12800. }
  12801. /**
  12802. * Show available NPC Quest / Event Icon Check [Kisuka]
  12803. * @param sd Player
  12804. **/
  12805. void pc_show_questinfo(struct map_session_data *sd) {
  12806. #if PACKETVER >= 20090218
  12807. nullpo_retv(sd);
  12808. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12809. return;
  12810. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12811. nullpo_retv(mapdata);
  12812. if (mapdata->qi_npc.empty())
  12813. return;
  12814. if (mapdata->qi_npc.size() != sd->qi_display.size())
  12815. return; // init was not called yet
  12816. for (int i = 0; i < mapdata->qi_npc.size(); i++) {
  12817. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  12818. if (!nd || nd->qi_data.empty())
  12819. continue;
  12820. bool show = false;
  12821. for (auto &qi : nd->qi_data) {
  12822. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  12823. show = true;
  12824. // Check if need to be displayed
  12825. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  12826. sd->qi_display[i].is_active = true;
  12827. sd->qi_display[i].icon = qi->icon;
  12828. sd->qi_display[i].color = qi->color;
  12829. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  12830. }
  12831. break;
  12832. }
  12833. }
  12834. if (show == false) {
  12835. // Check if need to be hide
  12836. if (sd->qi_display[i].is_active) {
  12837. sd->qi_display[i].is_active = false;
  12838. sd->qi_display[i].icon = QTYPE_NONE;
  12839. sd->qi_display[i].color = QMARK_NONE;
  12840. #if PACKETVER >= 20120410
  12841. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  12842. #else
  12843. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  12844. #endif
  12845. }
  12846. }
  12847. }
  12848. #endif
  12849. }
  12850. /**
  12851. * Reinit the questinfo for player when changing map
  12852. * @param sd Player
  12853. **/
  12854. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  12855. #if PACKETVER >= 20090218
  12856. nullpo_retv(sd);
  12857. sd->qi_display.clear();
  12858. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12859. return;
  12860. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12861. nullpo_retv(mapdata);
  12862. if (mapdata->qi_npc.empty())
  12863. return;
  12864. sd->qi_display.reserve( mapdata->qi_npc.size() );
  12865. for( int i = 0; i < mapdata->qi_npc.size(); i++ ){
  12866. sd->qi_display.push_back( s_qi_display() );
  12867. }
  12868. #endif
  12869. }
  12870. /**
  12871. * Check if a job is allowed to enter the map
  12872. * @param jobid Job ID see enum e_job or sd->status.class_
  12873. * @param m ID -an index- for direct indexing map[] array
  12874. * @return 1 if job is allowed, 0 otherwise
  12875. **/
  12876. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  12877. // Map is other map server.
  12878. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  12879. if (m < 0)
  12880. return true;
  12881. struct map_data *mapdata = map_getmapdata(m);
  12882. if (!mapdata->cell)
  12883. return false;
  12884. if (!pcdb_checkid(jobid))
  12885. return false;
  12886. std::shared_ptr<s_job_info> job = job_db.find(jobid);
  12887. if (!job->noenter_map.zone || group_lv > job->noenter_map.group_lv)
  12888. return true;
  12889. if ((job->noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  12890. (job->noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  12891. (job->noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  12892. (job->noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  12893. (job->noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  12894. (job->noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  12895. )
  12896. return false;
  12897. return true;
  12898. }
  12899. /**
  12900. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  12901. * @param sd
  12902. **/
  12903. void pc_set_costume_view(struct map_session_data *sd) {
  12904. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  12905. struct item_data *id = NULL;
  12906. nullpo_retv(sd);
  12907. head_low = sd->status.head_bottom;
  12908. head_mid = sd->status.head_mid;
  12909. head_top = sd->status.head_top;
  12910. robe = sd->status.robe;
  12911. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  12912. //Added check to prevent sending the same look on multiple slots ->
  12913. //causes client to redraw item on top of itself. (suggested by Lupus)
  12914. // Normal headgear checks
  12915. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  12916. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  12917. sd->status.head_bottom = id->look;
  12918. else
  12919. sd->status.head_bottom = 0;
  12920. }
  12921. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  12922. if (!(id->equip&(EQP_HEAD_TOP)))
  12923. sd->status.head_mid = id->look;
  12924. else
  12925. sd->status.head_mid = 0;
  12926. }
  12927. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  12928. sd->status.head_top = id->look;
  12929. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  12930. sd->status.robe = id->look;
  12931. // Costumes check
  12932. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  12933. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  12934. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  12935. sd->status.head_bottom = id->look;
  12936. else
  12937. sd->status.head_bottom = 0;
  12938. }
  12939. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  12940. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  12941. sd->status.head_mid = id->look;
  12942. else
  12943. sd->status.head_mid = 0;
  12944. }
  12945. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  12946. sd->status.head_top = id->look;
  12947. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  12948. sd->status.robe = id->look;
  12949. }
  12950. if (sd->setlook_head_bottom)
  12951. sd->status.head_bottom = sd->setlook_head_bottom;
  12952. if (sd->setlook_head_mid)
  12953. sd->status.head_mid = sd->setlook_head_mid;
  12954. if (sd->setlook_head_top)
  12955. sd->status.head_top = sd->setlook_head_top;
  12956. if (sd->setlook_robe)
  12957. sd->status.robe = sd->setlook_robe;
  12958. if (head_low != sd->status.head_bottom)
  12959. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  12960. if (head_mid != sd->status.head_mid)
  12961. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  12962. if (head_top != sd->status.head_top)
  12963. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  12964. if (robe != sd->status.robe)
  12965. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  12966. }
  12967. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  12968. uint32 date = date_get(DT_YYYYMMDD);
  12969. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  12970. std::shared_ptr<s_attendance_period> period = pair.second;
  12971. if( period->start <= date && period->end >= date ){
  12972. return period;
  12973. }
  12974. }
  12975. return nullptr;
  12976. }
  12977. bool pc_attendance_enabled(){
  12978. // Check if the attendance feature is disabled
  12979. if( !battle_config.feature_attendance ){
  12980. return false;
  12981. }
  12982. // Check if there is a running attendance period
  12983. return pc_attendance_period() != nullptr;
  12984. }
  12985. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  12986. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  12987. }
  12988. int32 pc_attendance_counter( struct map_session_data* sd ){
  12989. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  12990. // No running attendance period
  12991. if( period == nullptr ){
  12992. return 0;
  12993. }
  12994. // Get the counter for the current period
  12995. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  12996. // Check if we have a remaining counter from a previous period
  12997. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  12998. // Reset the counter to zero
  12999. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  13000. return 0;
  13001. }
  13002. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  13003. }
  13004. void pc_attendance_claim_reward( struct map_session_data* sd ){
  13005. // If the user's group does not have the permission
  13006. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  13007. return;
  13008. }
  13009. // Check if the attendance feature is disabled
  13010. if( !pc_attendance_enabled() ){
  13011. return;
  13012. }
  13013. // Check if the user already got his reward today
  13014. if( pc_attendance_rewarded_today( sd ) ){
  13015. return;
  13016. }
  13017. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13018. attendance_counter += 1;
  13019. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13020. if( period == nullptr ){
  13021. return;
  13022. }
  13023. if( period->rewards.size() < attendance_counter ){
  13024. return;
  13025. }
  13026. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  13027. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  13028. if( save_settings&CHARSAVE_ATTENDANCE )
  13029. chrif_save(sd, CSAVE_NORMAL);
  13030. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  13031. struct mail_message msg;
  13032. memset( &msg, 0, sizeof( struct mail_message ) );
  13033. msg.dest_id = sd->status.char_id;
  13034. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  13035. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  13036. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  13037. msg.item[0].nameid = reward->item_id;
  13038. msg.item[0].amount = reward->amount;
  13039. msg.item[0].identify = 1;
  13040. msg.status = MAIL_NEW;
  13041. msg.type = MAIL_INBOX_NORMAL;
  13042. msg.timestamp = time(NULL);
  13043. intif_Mail_send(0, &msg);
  13044. clif_attendence_response( sd, attendance_counter );
  13045. }
  13046. /**
  13047. * Send a player to jail and determine the location to send in jail.
  13048. * @param sd: Player data
  13049. * @param duration: Duration in minutes (default INT_MAX = infinite)
  13050. */
  13051. void pc_jail(map_session_data &sd, int32 duration) {
  13052. uint16 m_index = mapindex_name2id(MAP_JAIL);
  13053. int16 x, y;
  13054. switch (rnd() % 2) { // Jail Locations
  13055. case 0: // Jail #1
  13056. x = 49;
  13057. y = 75;
  13058. break;
  13059. default: // Default Jail
  13060. x = 24;
  13061. y = 75;
  13062. break;
  13063. }
  13064. duration = i32max(0, duration); // Can't be less than 0 seconds.
  13065. // If duration > 0 then triggered via jailfor which checks every minute.
  13066. // If duration == INT_MAX then triggered via jail for infinite duration.
  13067. // If duration == 0 then triggered via unjail and end status.
  13068. if (duration > 0)
  13069. sc_start4(nullptr, &sd.bl, SC_JAILED, 100, duration, m_index, x, y, 60000);
  13070. else
  13071. status_change_end(&sd.bl, SC_JAILED);
  13072. }
  13073. /**
  13074. * Determine the punishment type when failing macro checks.
  13075. * @param sd: Player data
  13076. * @param stype: Macro detection status type (for banning)
  13077. */
  13078. static void pc_macro_punishment(map_session_data &sd, e_macro_detect_status stype) {
  13079. int32 duration = 0;
  13080. // Determine if there's a unique duration
  13081. if (battle_config.macro_detection_punishment_time > 0) {
  13082. char time[13];
  13083. safesnprintf(time, 13, "+%dnm", battle_config.macro_detection_punishment_time);
  13084. duration = static_cast<int32>(solve_time(time));
  13085. }
  13086. // Delete the timer
  13087. if (sd.macro_detect.timer != INVALID_TIMER)
  13088. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13089. // Clear the macro detect data
  13090. sd.macro_detect = {};
  13091. sd.macro_detect.timer = INVALID_TIMER;
  13092. // Unblock all actions for the player
  13093. sd.state.block_action &= ~PCBLOCK_ALL;
  13094. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13095. if (battle_config.macro_detection_punishment == 0) { // Ban
  13096. clif_macro_detector_status(sd, stype);
  13097. chrif_req_login_operation(sd.macro_detect.reporter_aid, sd.status.name, (duration == 0 ? CHRIF_OP_LOGIN_BLOCK : CHRIF_OP_LOGIN_BAN), duration, 0, 0);
  13098. } else { // Jail
  13099. // Send success to close the window without closing the client
  13100. clif_macro_detector_status(sd, MCD_GOOD);
  13101. pc_jail(sd, (duration == 0 ? INT_MAX : duration / 60));
  13102. }
  13103. }
  13104. /**
  13105. * Save a captcha image to memory via /macro_register.
  13106. * @param sd: Player data
  13107. * @param image_size: Captcha image size
  13108. * @param captcha_answer: Answer to captcha
  13109. */
  13110. void pc_macro_captcha_register(map_session_data &sd, uint16 image_size, char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13111. nullpo_retv(captcha_answer);
  13112. sd.captcha_upload.cd = nullptr;
  13113. sd.captcha_upload.upload_size = 0;
  13114. if (strlen(captcha_answer) < 4 || image_size == 0 || image_size > CAPTCHA_BMP_SIZE) {
  13115. clif_captcha_upload_request(sd); // Notify client of failure.
  13116. return;
  13117. }
  13118. std::shared_ptr<s_captcha_data> cd = std::make_shared<s_captcha_data>();
  13119. sd.captcha_upload.cd = cd;
  13120. cd->image_size = image_size;
  13121. safestrncpy(cd->captcha_answer, captcha_answer, sizeof(cd->captcha_answer));
  13122. memset(cd->image_data, 0, sizeof(cd->image_data));
  13123. // Request the image data from the client.
  13124. clif_captcha_upload_request(sd);
  13125. }
  13126. /**
  13127. * Save captcha image to server.
  13128. * @param sd: Player data
  13129. * @param captcha_key: Captcha ID
  13130. * @param upload_size: Captcha size
  13131. * @param upload_data: Image data
  13132. */
  13133. void pc_macro_captcha_register_upload(map_session_data &sd, uint16 upload_size, char *upload_data) {
  13134. nullpo_retv(upload_data);
  13135. memcpy(&sd.captcha_upload.cd->image_data[sd.captcha_upload.upload_size], upload_data, upload_size);
  13136. sd.captcha_upload.upload_size += upload_size;
  13137. // Notify that the image finished uploading.
  13138. if (sd.captcha_upload.upload_size == sd.captcha_upload.cd->image_size) {
  13139. // Tell the client that the upload was finished
  13140. clif_captcha_upload_end(sd);
  13141. // Look for a free key
  13142. uint16 index;
  13143. for (index = 0; index < UINT16_MAX; index++) {
  13144. if (!captcha_db.exists(index)) {
  13145. break;
  13146. }
  13147. }
  13148. if (index == UINT16_MAX) {
  13149. // no free key found...
  13150. sd.captcha_upload.cd = nullptr;
  13151. sd.captcha_upload.upload_size = 0;
  13152. return;
  13153. }
  13154. captcha_db.put(index, sd.captcha_upload.cd);
  13155. sd.captcha_upload.cd = nullptr;
  13156. sd.captcha_upload.upload_size = 0;
  13157. // TODO: write YAML and BMP file?
  13158. }
  13159. }
  13160. /**
  13161. * Timer attached to target player with attempts to confirm captcha.
  13162. */
  13163. TIMER_FUNC(pc_macro_detector_timeout) {
  13164. map_session_data *sd = map_id2sd(id);
  13165. nullpo_ret(sd);
  13166. // Remove the current timer
  13167. sd->macro_detect.timer = INVALID_TIMER;
  13168. // Deduct an answering attempt
  13169. sd->macro_detect.retry -= 1;
  13170. if (sd->macro_detect.retry == 0) {
  13171. // All attempts have been exhausted, punish the user
  13172. pc_macro_punishment(*sd, MCD_TIMEOUT);
  13173. } else {
  13174. // Update the client
  13175. clif_macro_detector_request_show(*sd);
  13176. // Start a new timer
  13177. sd->macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd->bl.id, 0);
  13178. }
  13179. return 0;
  13180. }
  13181. /**
  13182. * Check player's captcha answer.
  13183. * @param sd: Player data
  13184. * @param captcha_answer: Captcha answer entered by player
  13185. */
  13186. void pc_macro_detector_process_answer(map_session_data &sd, char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13187. nullpo_retv(captcha_answer);
  13188. const std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  13189. // Has no captcha request
  13190. if (cd == nullptr) {
  13191. return;
  13192. }
  13193. // Correct answer
  13194. if (strcmp(captcha_answer, cd->captcha_answer) == 0) {
  13195. // Delete the timer
  13196. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13197. // Clear the macro detect data
  13198. sd.macro_detect = {};
  13199. sd.macro_detect.timer = INVALID_TIMER;
  13200. // Unblock all actions for the player
  13201. sd.state.block_action &= ~PCBLOCK_ALL;
  13202. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13203. // Assign temporary macro variable to check failures
  13204. pc_setreg(&sd, add_str("@captcha_retries"), battle_config.macro_detection_retry - sd.macro_detect.retry);
  13205. // Grant bonuses via script
  13206. run_script(cd->bonus_script, 0, sd.bl.id, fake_nd->bl.id);
  13207. // Notify the client
  13208. clif_macro_detector_status(sd, MCD_GOOD);
  13209. } else {
  13210. // Deduct an answering attempt
  13211. sd.macro_detect.retry -= 1;
  13212. // All attempts have been exhausted, punish the user
  13213. if (sd.macro_detect.retry <= 0) {
  13214. pc_macro_punishment(sd, MCD_INCORRECT);
  13215. return;
  13216. }
  13217. // Incorrect response, update the client
  13218. clif_macro_detector_request_show(sd);
  13219. // Reset the timer
  13220. addtick_timer(sd.macro_detect.timer, gettick() + battle_config.macro_detection_timeout);
  13221. }
  13222. }
  13223. /**
  13224. * Determine if a player tries to log out during a captcha check.
  13225. * @param sd: Player data
  13226. */
  13227. void pc_macro_detector_disconnect(map_session_data &sd) {
  13228. // Delete the timeout timer
  13229. if (sd.macro_detect.timer != INVALID_TIMER) {
  13230. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13231. sd.macro_detect.timer = INVALID_TIMER;
  13232. }
  13233. // If the player disconnects before clearing the challenge the player is punished.
  13234. if (sd.macro_detect.retry != 0)
  13235. pc_macro_punishment(sd, MCD_TIMEOUT);
  13236. }
  13237. /**
  13238. * Save a list of players from an area select via /macro_detector.
  13239. */
  13240. int pc_macro_reporter_area_select_sub(block_list *bl, va_list ap) {
  13241. nullpo_retr(0, bl);
  13242. if (bl->type != BL_PC)
  13243. return 0;
  13244. std::vector<uint32> *aid_list = va_arg(ap, std::vector<uint32> *);
  13245. nullpo_ret(aid_list);
  13246. aid_list->push_back(bl->id);
  13247. return 0;
  13248. }
  13249. /**
  13250. * Area select via /macro_detector.
  13251. * @param sd: Player data
  13252. * @param x: X location
  13253. * @param y: Y location
  13254. * @param radius: Area
  13255. */
  13256. void pc_macro_reporter_area_select(map_session_data &sd, const int16 x, const int16 y, const int8 radius) {
  13257. std::vector<uint32> aid_list;
  13258. map_foreachinarea(pc_macro_reporter_area_select_sub, sd.bl.m, x - radius, y - radius, x + radius, y + radius, BL_PC, &aid_list);
  13259. clif_macro_reporter_select(sd, aid_list);
  13260. }
  13261. /**
  13262. * Send out captcha check to player.
  13263. * @param sd: Target player data
  13264. * @param reporter_account_id: Account ID of reporter
  13265. */
  13266. void pc_macro_reporter_process(map_session_data &sd, int32 reporter_account_id) {
  13267. if (captcha_db.empty())
  13268. return;
  13269. // Pick a random image from the database.
  13270. const std::shared_ptr<s_captcha_data> cd = captcha_db.random();
  13271. // Set macro detection data.
  13272. sd.macro_detect.cd = cd;
  13273. sd.macro_detect.reporter_aid = reporter_account_id;
  13274. sd.macro_detect.retry = battle_config.macro_detection_retry;
  13275. // Block all actions for the target player.
  13276. sd.state.block_action |= (PCBLOCK_ALL | PCBLOCK_IMMUNE);
  13277. // Open macro detect client side.
  13278. clif_macro_detector_request(sd);
  13279. // Start the timeout timer.
  13280. sd.macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd.bl.id, 0);
  13281. }
  13282. /**
  13283. * Parse a BMP image to memory.
  13284. * @param filepath: Image file location
  13285. * @param cd: Captcha data
  13286. */
  13287. bool pc_macro_read_captcha_db_loadbmp(const std::string &filepath, std::shared_ptr<s_captcha_data> cd) {
  13288. if (cd == nullptr)
  13289. return false;
  13290. FILE *fp = fopen(filepath.c_str(), "rb");
  13291. if (fp == nullptr) {
  13292. ShowError("%s: Failed to open file \"%s\"\n", __func__, filepath.c_str());
  13293. return false;
  13294. }
  13295. // Load the file data and verify magic
  13296. char bmp_data[CAPTCHA_BMP_SIZE];
  13297. if (fread(bmp_data, CAPTCHA_BMP_SIZE, 1, fp) != 1) {
  13298. ShowError("%s: Failed to read data from \"%s\"\n", __func__, filepath.c_str());
  13299. fclose(fp);
  13300. return false;
  13301. }
  13302. fclose(fp);
  13303. if (bmp_data[0] != 'B' || bmp_data[1] != 'M') {
  13304. ShowError("%s: Invalid BMP file header given at \"%s\"\n", __func__, filepath.c_str());
  13305. return false;
  13306. }
  13307. // Compress the data into the destination
  13308. unsigned long com_size = sizeof(cd->image_data);
  13309. encode_zip(cd->image_data, &com_size, bmp_data, CAPTCHA_BMP_SIZE);
  13310. cd->image_size = static_cast<int16>(com_size);
  13311. return true;
  13312. }
  13313. const std::string CaptchaDatabase::getDefaultLocation() {
  13314. return std::string(db_path) + "/captcha_db.yml";
  13315. }
  13316. /**
  13317. * Reads and parses an entry from the captcha_db.
  13318. * @param node: YAML node containing the entry.
  13319. * @return count of successfully parsed rows
  13320. */
  13321. uint64 CaptchaDatabase::parseBodyNode(const ryml::NodeRef &node) {
  13322. uint16 index;
  13323. if (!this->asUInt16(node, "Id", index))
  13324. return 0;
  13325. std::shared_ptr<s_captcha_data> cd = captcha_db.find(index);
  13326. bool exists = cd != nullptr;
  13327. if (!exists) {
  13328. if (!this->nodesExist(node, { "Filename", "Answer" }))
  13329. return 0;
  13330. cd = std::make_shared<s_captcha_data>();
  13331. cd->index = index;
  13332. }
  13333. if (this->nodeExists(node, "Filename")) {
  13334. std::string filename;
  13335. if (!this->asString(node, "Filename", filename))
  13336. return 0;
  13337. if (!pc_macro_read_captcha_db_loadbmp(filename, cd)) {
  13338. this->invalidWarning(node["Filename"], "Failed to parse BMP image, skipping...\n");
  13339. return 0;
  13340. }
  13341. }
  13342. if (this->nodeExists(node, "Answer")) {
  13343. std::string answer;
  13344. if (!this->asString(node, "Answer", answer))
  13345. return 0;
  13346. if (answer.length() < 4 || answer.length() > CAPTCHA_ANSWER_SIZE) {
  13347. this->invalidWarning(node["Answer"], "The captcha answer must be between 4~%d characters, skipping...", CAPTCHA_ANSWER_SIZE);
  13348. return 0;
  13349. }
  13350. safestrncpy(cd->captcha_answer, answer.c_str(), sizeof(cd->captcha_answer));
  13351. }
  13352. if (this->nodeExists(node, "Bonus")) {
  13353. std::string script;
  13354. if (!this->asString(node, "Bonus", script)) {
  13355. return 0;
  13356. }
  13357. if (cd->bonus_script) {
  13358. script_free_code(cd->bonus_script);
  13359. cd->bonus_script = nullptr;
  13360. }
  13361. cd->bonus_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Bonus"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  13362. } else {
  13363. if (!exists)
  13364. 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);
  13365. }
  13366. if (!exists)
  13367. captcha_db.put(index, cd);
  13368. return 1;
  13369. }
  13370. /*==========================================
  13371. * pc Init/Terminate
  13372. *------------------------------------------*/
  13373. void do_final_pc(void) {
  13374. db_destroy(itemcd_db);
  13375. do_final_pc_groups();
  13376. ers_destroy(pc_sc_display_ers);
  13377. ers_destroy(num_reg_ers);
  13378. ers_destroy(str_reg_ers);
  13379. attendance_db.clear();
  13380. reputation_db.clear();
  13381. #ifdef MAP_GENERATOR
  13382. reputationgroup_db.clear();
  13383. #endif
  13384. penalty_db.clear();
  13385. captcha_db.clear();
  13386. }
  13387. void do_init_pc(void) {
  13388. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  13389. pc_readdb();
  13390. pc_read_motd(); // Read MOTD [Valaris]
  13391. attendance_db.load();
  13392. reputation_db.load();
  13393. #ifdef MAP_GENERATOR
  13394. reputationgroup_db.load();
  13395. #endif
  13396. captcha_db.load();
  13397. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  13398. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  13399. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  13400. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  13401. add_timer_func_list(pc_autosave, "pc_autosave");
  13402. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  13403. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  13404. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  13405. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  13406. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  13407. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  13408. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  13409. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  13410. add_timer_func_list(pc_macro_detector_timeout, "pc_macro_detector_timeout");
  13411. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  13412. // 0=day, 1=night [Yor]
  13413. night_flag = battle_config.night_at_start ? 1 : 0;
  13414. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  13415. int day_duration = battle_config.day_duration;
  13416. int night_duration = battle_config.night_duration;
  13417. // add night/day timer [Yor]
  13418. add_timer_func_list(map_day_timer, "map_day_timer");
  13419. add_timer_func_list(map_night_timer, "map_night_timer");
  13420. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  13421. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  13422. }
  13423. do_init_pc_groups();
  13424. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  13425. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13426. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13427. ers_chunk_size(pc_sc_display_ers, 150);
  13428. ers_chunk_size(num_reg_ers, 300);
  13429. ers_chunk_size(str_reg_ers, 50);
  13430. }