pc.cpp 474 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pc.hpp"
  4. #include <map>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #ifdef MAP_GENERATOR
  8. #include <fstream>
  9. #include <iostream>
  10. #include <nlohmann/json.hpp>
  11. #endif
  12. #include <common/cbasetypes.hpp>
  13. #include <common/core.hpp> // get_svn_revision()
  14. #include <common/database.hpp>
  15. #include <common/ers.hpp> // ers_destroy
  16. #include <common/grfio.hpp>
  17. #include <common/malloc.hpp>
  18. #include <common/mmo.hpp> //NAME_LENGTH
  19. #include <common/nullpo.hpp>
  20. #include <common/random.hpp>
  21. #include <common/showmsg.hpp>
  22. #include <common/socket.hpp> // session[]
  23. #include <common/strlib.hpp> // safestrncpy()
  24. #include <common/timer.hpp>
  25. #include <common/utilities.hpp>
  26. #include <common/utils.hpp>
  27. #include "achievement.hpp"
  28. #include "atcommand.hpp" // get_atcommand_level()
  29. #include "battle.hpp" // battle_config
  30. #include "battleground.hpp"
  31. #include "buyingstore.hpp" // struct s_buyingstore
  32. #include "channel.hpp"
  33. #include "chat.hpp"
  34. #include "chrif.hpp"
  35. #include "clan.hpp"
  36. #include "clif.hpp"
  37. #include "date.hpp" // is_day_of_*()
  38. #include "duel.hpp"
  39. #include "elemental.hpp"
  40. #include "guild.hpp"
  41. #include "homunculus.hpp"
  42. #include "instance.hpp"
  43. #include "intif.hpp"
  44. #include "itemdb.hpp" // MAX_ITEMGROUP
  45. #include "log.hpp"
  46. #include "map.hpp"
  47. #include "mercenary.hpp"
  48. #include "mob.hpp"
  49. #include "npc.hpp"
  50. #include "party.hpp" // party_search()
  51. #include "pc_groups.hpp"
  52. #include "pet.hpp" // pet_unlocktarget()
  53. #include "quest.hpp"
  54. #include "skill.hpp" // skill_isCopyable()
  55. #include "script.hpp" // struct script_reg, struct script_regstr
  56. #include "searchstore.hpp" // struct s_search_store_info
  57. #include "status.hpp" // OPTION_*, struct weapon_atk
  58. #include "storage.hpp"
  59. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  60. #include "vending.hpp" // struct s_vending
  61. using namespace rathena;
  62. JobDatabase job_db;
  63. CaptchaDatabase captcha_db;
  64. const char *macro_allowed_answer_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  65. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  66. static inline bool pc_attendance_rewarded_today( map_session_data* sd );
  67. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  68. PlayerStatPointDatabase statpoint_db;
  69. SkillTreeDatabase skill_tree_db;
  70. // timer for night.day implementation
  71. int day_timer_tid = INVALID_TIMER;
  72. int night_timer_tid = INVALID_TIMER;
  73. struct eri *pc_sc_display_ers = NULL;
  74. struct eri *num_reg_ers;
  75. struct eri *str_reg_ers;
  76. int pc_expiration_tid = INVALID_TIMER;
  77. struct fame_list smith_fame_list[MAX_FAME_LIST];
  78. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  79. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  80. const std::string AttendanceDatabase::getDefaultLocation(){
  81. return std::string(db_path) + "/attendance.yml";
  82. }
  83. /**
  84. * Reads and parses an entry from the attendance_db.
  85. * @param node: YAML node containing the entry.
  86. * @return count of successfully parsed rows
  87. */
  88. uint64 AttendanceDatabase::parseBodyNode(const ryml::NodeRef& node){
  89. uint32 start;
  90. if( !this->asUInt32( node, "Start", start ) ){
  91. return 0;
  92. }
  93. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  94. bool exists = attendance_period != nullptr;
  95. if( !exists ){
  96. if( !this->nodeExists( node, "End" ) ){
  97. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  98. return 0;
  99. }
  100. if( !this->nodeExists( node, "Rewards" ) ){
  101. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  102. return 0;
  103. }
  104. attendance_period = std::make_shared<s_attendance_period>();
  105. attendance_period->start = start;
  106. }
  107. // If it does not exist yet, we need to check it for sure
  108. bool requiresCollisionDetection = !exists;
  109. if( this->nodeExists( node, "End" ) ){
  110. uint32 end;
  111. if( !this->asUInt32( node, "End", end ) ){
  112. return 0;
  113. }
  114. // If the period is outdated already, we do not even bother parsing
  115. if( end < date_get( DT_YYYYMMDD ) ){
  116. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  117. return 0;
  118. }
  119. if( !exists || attendance_period->end != end ){
  120. requiresCollisionDetection = true;
  121. attendance_period->end = end;
  122. }
  123. }
  124. // Collision detection
  125. if( requiresCollisionDetection ){
  126. bool collision = false;
  127. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  128. std::shared_ptr<s_attendance_period> period = pair.second;
  129. if( exists && period->start == attendance_period->start ){
  130. // Dont compare to yourself
  131. continue;
  132. }
  133. // Check if start is inside another period
  134. if( period->start <= attendance_period->start && start <= period->end ){
  135. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  136. collision = true;
  137. break;
  138. }
  139. // Check if end is inside another period
  140. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  141. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  142. collision = true;
  143. break;
  144. }
  145. }
  146. if( collision ){
  147. return 0;
  148. }
  149. }
  150. if( this->nodeExists( node, "Rewards" ) ){
  151. const auto& rewardsNode = node["Rewards"];
  152. for( const auto& rewardNode : rewardsNode ){
  153. uint32 day;
  154. if( !this->asUInt32( rewardNode, "Day", day ) ){
  155. continue;
  156. }
  157. day -= 1;
  158. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  159. bool reward_exists = reward != nullptr;
  160. if( !reward_exists ){
  161. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  162. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  163. return 0;
  164. }
  165. reward = std::make_shared<s_attendance_reward>();
  166. }
  167. if( this->nodeExists( rewardNode, "ItemId" ) ){
  168. t_itemid item_id;
  169. if( !this->asUInt32( rewardNode, "ItemId", item_id ) ){
  170. continue;
  171. }
  172. if( !item_db.exists( item_id ) ){
  173. ShowError( "pc_attendance_load: Unknown item ID %u for day %d.\n", item_id, day + 1 );
  174. continue;
  175. }
  176. reward->item_id = item_id;
  177. }
  178. if( this->nodeExists( rewardNode, "Amount" ) ){
  179. uint16 amount;
  180. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  181. continue;
  182. }
  183. if( amount == 0 ){
  184. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  185. amount = 1;
  186. }else if( amount > MAX_AMOUNT ){
  187. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  188. amount = MAX_AMOUNT;
  189. }
  190. reward->amount = amount;
  191. }else{
  192. if( !reward_exists ){
  193. reward->amount = 1;
  194. }
  195. }
  196. if( !reward_exists ){
  197. attendance_period->rewards[day] = reward;
  198. }
  199. }
  200. bool missing_day = false;
  201. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  202. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  203. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  204. missing_day = true;
  205. break;
  206. }
  207. }
  208. if( missing_day ){
  209. return 0;
  210. }
  211. }
  212. if( !exists ){
  213. this->put( start, attendance_period );
  214. }
  215. return 1;
  216. }
  217. AttendanceDatabase attendance_db;
  218. const std::string ReputationDatabase::getDefaultLocation(){
  219. return std::string( db_path ) + "/reputation.yml";
  220. }
  221. uint64 ReputationDatabase::parseBodyNode( const ryml::NodeRef& node ){
  222. int64 id;
  223. if( !this->asInt64( node, "Id", id ) ){
  224. return 0;
  225. }
  226. std::shared_ptr<s_reputation> reputation = this->find( id );
  227. bool exists = reputation != nullptr;
  228. if( !exists ){
  229. if( !this->nodesExist( node, { "Name", "Variable" } ) ){
  230. return 0;
  231. }
  232. reputation = std::make_shared<s_reputation>();
  233. reputation->id = id;
  234. }
  235. if( this->nodeExists( node, "Name" ) ){
  236. std::string name;
  237. if( !this->asString( node, "Name", name ) ){
  238. return 0;
  239. }
  240. reputation->name = name;
  241. }
  242. if( this->nodeExists( node, "Variable" ) ){
  243. std::string variable;
  244. if( !this->asString( node, "Variable", variable ) ){
  245. return 0;
  246. }
  247. if( variable.length() > 32 ){
  248. this->invalidWarning( node, "Variable name \"%s\" exceeds maximum length 32.\n", variable.c_str() );
  249. return 0;
  250. }
  251. reputation->variable = variable;
  252. }
  253. if( this->nodeExists( node, "Minimum" ) ){
  254. int64 minimum;
  255. if( !this->asInt64( node, "Minimum", minimum ) ){
  256. return 0;
  257. }
  258. reputation->minimum = minimum;
  259. }else{
  260. if( !exists ){
  261. reputation->minimum = INT64_MIN;
  262. }
  263. }
  264. if( this->nodeExists( node, "Maximum" ) ){
  265. int64 maximum;
  266. if( !this->asInt64( node, "Maximum", maximum ) ){
  267. return 0;
  268. }
  269. reputation->maximum = maximum;
  270. }else{
  271. if( !exists ){
  272. reputation->maximum = INT64_MIN;
  273. }
  274. }
  275. #ifdef MAP_GENERATOR
  276. if (this->nodeExists(node, "Visibility")) {
  277. std::string visibility;
  278. if (!this->asString(node, "Visibility", visibility)) {
  279. return 0;
  280. }
  281. if (visibility == "Always")
  282. reputation->visibility = s_reputation::e_visibility::ALWAYS;
  283. else if (visibility == "Never")
  284. reputation->visibility = s_reputation::e_visibility::NEVER;
  285. else if (visibility == "Exist")
  286. reputation->visibility = s_reputation::e_visibility::EXIST;
  287. else {
  288. this->invalidWarning(node, "Visibility \"%s\" unknown.\n", visibility.c_str());
  289. return 0;
  290. }
  291. } else {
  292. if (!exists) {
  293. reputation->visibility = s_reputation::e_visibility::ALWAYS;
  294. }
  295. }
  296. #endif
  297. if( !exists ){
  298. this->put( id, reputation );
  299. }
  300. return 1;
  301. }
  302. ReputationDatabase reputation_db;
  303. const std::string ReputationGroupDatabase::getDefaultLocation() {
  304. return std::string(db_path) + "/reputation_group.yml";
  305. }
  306. uint64 ReputationGroupDatabase::parseBodyNode(const ryml::NodeRef& node) {
  307. int64 id;
  308. if (!this->asInt64(node, "Id", id)) {
  309. return 0;
  310. }
  311. std::shared_ptr<s_reputationgroup> group = this->find(id);
  312. bool exists = group != nullptr;
  313. if (!exists) {
  314. if (!this->nodesExist(node, {"Name", "ReputeList"})) {
  315. return 0;
  316. }
  317. group = std::make_shared<s_reputationgroup>();
  318. group->id = id;
  319. }
  320. if (this->nodeExists(node, "Name")) {
  321. std::string name;
  322. if (!this->asString(node, "Name", name)) {
  323. return 0;
  324. }
  325. group->name = name;
  326. }
  327. if (this->nodeExists(node, "ScriptName")) {
  328. std::string script_name;
  329. if (!this->asString(node, "ScriptName", script_name)) {
  330. return 0;
  331. }
  332. group->script_name = script_name;
  333. }
  334. if (this->nodeExists(node, "ReputeList")) {
  335. const auto& reputelist = node[c4::to_csubstr("ReputeList")];
  336. for (const auto& repute : reputelist) {
  337. int64 repute_id;
  338. try {
  339. repute >> repute_id;
  340. } catch (std::runtime_error const&) {
  341. this->invalidWarning(node, "Value \"%s\" cannot be parsed as int64.\n", repute.val().str);
  342. continue;
  343. }
  344. if (!reputation_db.find(repute_id)) {
  345. this->invalidWarning(node, "Reputation id %lld does not exist in reputation_db!\n", repute_id);
  346. continue;
  347. }
  348. group->reputations.push_back(repute_id);
  349. }
  350. }
  351. if (!exists) {
  352. this->put(id, group);
  353. }
  354. return 1;
  355. }
  356. ReputationGroupDatabase reputationgroup_db;
  357. void pc_reputation_generate() {
  358. #ifdef MAP_GENERATOR
  359. const std::string filePrefix = "generated/clientside/data/contentdata/";
  360. auto reputeInfo = nlohmann::json::object();
  361. for (const auto& pair : reputation_db) {
  362. auto id = pair.first;
  363. auto rep = pair.second;
  364. nlohmann::json node;
  365. switch (rep->visibility) {
  366. case s_reputation::e_visibility::ALWAYS:
  367. node["Invisible"] = "VISIBLE_TRUE";
  368. break;
  369. case s_reputation::e_visibility::NEVER:
  370. node["Invisible"] = "VISIBLE_FALSE";
  371. break;
  372. case s_reputation::e_visibility::EXIST:
  373. node["Invisible"] = "VISIBLE_EXIST";
  374. break;
  375. }
  376. node["MaxPoint_Negative"] = std::abs(rep->minimum);
  377. node["MaxPoint_Positive"] = std::abs(rep->maximum);
  378. node["Name"] = rep->name;
  379. reputeInfo[std::to_string(id)] = node;
  380. }
  381. auto j = nlohmann::json::object();
  382. j["reputeInfo"] = reputeInfo;
  383. // std::cout << j.dump(2) << "\n";
  384. auto bson = nlohmann::json::to_bson(j);
  385. auto reputation_file = std::ofstream(filePrefix + "./reputeinfodata.bson", std::ios::binary);
  386. if (!reputation_file) {
  387. ShowError("Failed to create reputation file.\n");
  388. ShowError("Maybe the file directory \"%s\" does not exist?\n", filePrefix.c_str());
  389. ShowInfo("Create the directory and rerun map-server-generator\n");
  390. exit(1);
  391. }
  392. reputation_file.write((const char *)&bson[0], bson.size());
  393. auto reputeGroupInfo = nlohmann::json::object();
  394. for (const auto& pair : reputationgroup_db) {
  395. auto id = pair.first;
  396. auto group = pair.second;
  397. nlohmann::json node;
  398. node["ID"] = group->script_name;
  399. node["Name"] = group->name;
  400. node["ReputeList"] = group->reputations;
  401. reputeGroupInfo[std::to_string(id)] = node;
  402. }
  403. j = nlohmann::json::object();
  404. j["ReputeGroup"] = reputeGroupInfo;
  405. // std::cout << j.dump(2) << "\n";
  406. bson = nlohmann::json::to_bson(j);
  407. auto reputation_group_file = std::ofstream(filePrefix + "./reputegroupdata.bson", std::ios::binary);
  408. if (!reputation_group_file) {
  409. ShowError("Failed to create reputation group file.\n");
  410. ShowError("Maybe the file directory \"%s\" does not exist?\n", filePrefix.c_str());
  411. ShowInfo("Create the directory and rerun map-server-generator\n");
  412. exit(1);
  413. }
  414. reputation_group_file.write((const char *)&bson[0], bson.size());
  415. #endif
  416. }
  417. const std::string PenaltyDatabase::getDefaultLocation(){
  418. return std::string( db_path ) + "/level_penalty.yml";
  419. }
  420. uint64 PenaltyDatabase::parseBodyNode(const ryml::NodeRef& node){
  421. std::string type_constant;
  422. if( !this->asString( node, "Type", type_constant ) ){
  423. return 0;
  424. }
  425. int64 constant_value;
  426. if( !script_get_constant( ( "PENALTY_" + type_constant ).c_str(), &constant_value ) ){
  427. this->invalidWarning( node["Type"], "Unknown penalty type \"%s\".\n", type_constant.c_str() );
  428. return 0;
  429. }
  430. if( constant_value < PENALTY_NONE || constant_value > PENALTY_MAX ){
  431. this->invalidWarning( node["Type"], "Invalid penalty type \"%s\".\n", type_constant.c_str() );
  432. return 0;
  433. }
  434. e_penalty_type type = static_cast<e_penalty_type>( constant_value );
  435. std::shared_ptr<s_penalty> penalty = this->find( type );
  436. bool exists = penalty != nullptr;
  437. if( !exists ){
  438. penalty = std::make_shared<s_penalty>();
  439. penalty->type = type;
  440. for( int i = 0, max = ARRAYLENGTH( penalty->rate ); i < max; i++ ){
  441. penalty->rate[i] = UINT16_MAX;
  442. }
  443. }
  444. if( this->nodeExists( node, "LevelDifferences" ) ){
  445. for( const auto& levelNode : node["LevelDifferences"] ){
  446. if( !this->nodesExist( levelNode, { "Difference", "Rate" } ) ){
  447. return 0;
  448. }
  449. int32 difference;
  450. if( !this->asInt32( levelNode, "Difference", difference ) ){
  451. return 0;
  452. }
  453. if( std::abs( difference ) > MAX_LEVEL ){
  454. this->invalidWarning( levelNode["Difference"], "Level difference %d is bigger than maximum level %d.\n", difference, MAX_LEVEL );
  455. return 0;
  456. }
  457. uint16 rate;
  458. if( !this->asUInt16Rate( levelNode, "Rate", rate ) ){
  459. return 0;
  460. }
  461. penalty->rate[difference + MAX_LEVEL - 1] = rate;
  462. }
  463. }
  464. if( !exists ){
  465. this->put( type, penalty );
  466. }
  467. return 1;
  468. }
  469. void PenaltyDatabase::loadingFinished(){
  470. for( const auto& pair : *this ){
  471. for( int i = MAX_LEVEL - 1, max = ARRAYLENGTH( pair.second->rate ), last_rate = 100; i < max; i++ ){
  472. uint16 rate = pair.second->rate[i];
  473. // Check if it has been defined
  474. if( rate == UINT16_MAX ){
  475. pair.second->rate[i] = last_rate;
  476. }else{
  477. last_rate = rate;
  478. }
  479. }
  480. for( int i = MAX_LEVEL - 1, last_rate = 100; i >= 0; i-- ){
  481. uint16 rate = pair.second->rate[i];
  482. // Check if it has been defined
  483. if( rate == UINT16_MAX ){
  484. pair.second->rate[i] = last_rate;
  485. }else{
  486. last_rate = rate;
  487. }
  488. }
  489. }
  490. TypesafeYamlDatabase::loadingFinished();
  491. }
  492. PenaltyDatabase penalty_db;
  493. #define MOTD_LINE_SIZE 128
  494. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  495. bool reg_load;
  496. /**
  497. * Translation table from athena equip index to aegis bitmask
  498. */
  499. unsigned int equip_bitmask[EQI_MAX] = {
  500. EQP_ACC_L, // EQI_ACC_L
  501. EQP_ACC_R, // EQI_ACC_R
  502. EQP_SHOES, // EQI_SHOES
  503. EQP_GARMENT, // EQI_GARMENT
  504. EQP_HEAD_LOW, // EQI_HEAD_LOW
  505. EQP_HEAD_MID, // EQI_HEAD_MID
  506. EQP_HEAD_TOP, // EQI_HEAD_TOP
  507. EQP_ARMOR, // EQI_ARMOR
  508. EQP_HAND_L, // EQI_HAND_L
  509. EQP_HAND_R, // EQI_HAND_R
  510. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  511. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  512. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  513. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  514. EQP_AMMO, // EQI_AMMO
  515. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  516. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  517. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  518. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  519. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  520. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  521. };
  522. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  523. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  524. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  525. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  526. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  527. };
  528. void pc_set_reg_load( bool val ){
  529. reg_load = val;
  530. }
  531. /**
  532. * Item Cool Down Delay Saving
  533. * Struct item_cd is not a member of map_session_data
  534. * to keep cooldowns in memory between player log-ins.
  535. * All cooldowns are reset when server is restarted.
  536. **/
  537. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  538. struct item_cd {
  539. t_tick tick[MAX_ITEMDELAYS]; //tick
  540. t_itemid nameid[MAX_ITEMDELAYS]; //item id
  541. };
  542. /**
  543. * Converts a class to its array index for CLASS_COUNT defined arrays.
  544. * Note that it does not do a validity check for speed purposes, where parsing
  545. * player input make sure to use a pcdb_checkid first!
  546. * @param class_ Job ID see enum e_job
  547. * @return Class Index
  548. */
  549. int pc_class2idx(int class_) {
  550. if (class_ >= JOB_NOVICE_HIGH)
  551. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  552. return class_;
  553. }
  554. /**
  555. * Get player's group ID
  556. * @param sd
  557. * @return Group ID
  558. */
  559. int pc_get_group_id(map_session_data *sd) {
  560. return sd->group_id;
  561. }
  562. /** Get player's group Level
  563. * @param sd
  564. * @return Group Level
  565. */
  566. int pc_get_group_level(map_session_data *sd) {
  567. return sd->group->level;
  568. }
  569. /**
  570. * Remove a player from queue after timeout
  571. * @param tid: Timer ID
  572. * @param tick: Timer
  573. * @param id: ID
  574. * @return 0 on success or 1 otherwise
  575. */
  576. static TIMER_FUNC(pc_on_expire_active)
  577. {
  578. map_session_data *sd = (map_session_data *)data;
  579. nullpo_retr(1, sd);
  580. sd->tid_queue_active = INVALID_TIMER;
  581. bg_queue_leave(sd);
  582. clif_bg_queue_entry_init(sd);
  583. return 0;
  584. }
  585. /**
  586. * Function used to set timer externally
  587. * @param sd: Player data
  588. */
  589. void pc_set_bg_queue_timer(map_session_data *sd) {
  590. nullpo_retv(sd);
  591. if (sd->tid_queue_active != INVALID_TIMER) {
  592. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  593. sd->tid_queue_active = INVALID_TIMER;
  594. }
  595. sd->tid_queue_active = add_timer(gettick() + 20000, pc_on_expire_active, 0, (intptr_t)sd);
  596. }
  597. /**
  598. * Function used to delete timer externally
  599. * @param sd: Player data
  600. */
  601. void pc_delete_bg_queue_timer(map_session_data *sd) {
  602. nullpo_retv(sd);
  603. if (sd->tid_queue_active != INVALID_TIMER) {
  604. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  605. sd->tid_queue_active = INVALID_TIMER;
  606. }
  607. }
  608. static TIMER_FUNC(pc_invincible_timer){
  609. map_session_data *sd;
  610. if( (sd=(map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  611. return 1;
  612. if(sd->invincible_timer != tid){
  613. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  614. return 0;
  615. }
  616. sd->invincible_timer = INVALID_TIMER;
  617. skill_unit_move(&sd->bl,tick,1);
  618. return 0;
  619. }
  620. void pc_setinvincibletimer(map_session_data* sd, int val) {
  621. nullpo_retv(sd);
  622. if( sd->invincible_timer != INVALID_TIMER )
  623. delete_timer(sd->invincible_timer,pc_invincible_timer);
  624. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  625. }
  626. void pc_delinvincibletimer(map_session_data* sd)
  627. {
  628. nullpo_retv(sd);
  629. if( sd->invincible_timer != INVALID_TIMER )
  630. {
  631. delete_timer(sd->invincible_timer,pc_invincible_timer);
  632. sd->invincible_timer = INVALID_TIMER;
  633. skill_unit_move(&sd->bl,gettick(),1);
  634. }
  635. }
  636. static TIMER_FUNC(pc_spiritball_timer){
  637. map_session_data *sd;
  638. int i;
  639. if( (sd=(map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  640. return 1;
  641. if( sd->spiritball <= 0 )
  642. {
  643. ShowError("pc_spiritball_timer: %d spiritball's available. (aid=%d cid=%d tid=%d)\n", sd->spiritball, sd->status.account_id, sd->status.char_id, tid);
  644. sd->spiritball = 0;
  645. return 0;
  646. }
  647. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  648. if( i == sd->spiritball )
  649. {
  650. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  651. return 0;
  652. }
  653. sd->spiritball--;
  654. if( i != sd->spiritball )
  655. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  656. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  657. clif_spiritball(&sd->bl);
  658. return 0;
  659. }
  660. /**
  661. * Adds a spiritball to player for 'interval' ms
  662. * @param sd
  663. * @param interval
  664. * @param max
  665. */
  666. void pc_addspiritball(map_session_data *sd,int interval,int max)
  667. {
  668. int tid;
  669. uint8 i;
  670. nullpo_retv(sd);
  671. if(max > MAX_SPIRITBALL)
  672. max = MAX_SPIRITBALL;
  673. if(sd->spiritball < 0)
  674. sd->spiritball = 0;
  675. if( sd->spiritball && sd->spiritball >= max )
  676. {
  677. if(sd->spirit_timer[0] != INVALID_TIMER)
  678. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  679. sd->spiritball--;
  680. if( sd->spiritball != 0 )
  681. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  682. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  683. }
  684. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  685. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spirit_timer[i])->tick) < 0);
  686. if( i != sd->spiritball )
  687. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  688. sd->spirit_timer[i] = tid;
  689. sd->spiritball++;
  690. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  691. clif_millenniumshield(&sd->bl,sd->spiritball);
  692. else
  693. clif_spiritball(&sd->bl);
  694. }
  695. /**
  696. * Removes number of spiritball from player
  697. * @param sd
  698. * @param count
  699. * @param type 1 = doesn't give client effect
  700. */
  701. void pc_delspiritball(map_session_data *sd,int count,int type)
  702. {
  703. uint8 i;
  704. nullpo_retv(sd);
  705. if(sd->spiritball <= 0) {
  706. sd->spiritball = 0;
  707. return;
  708. }
  709. if(count == 0)
  710. return;
  711. if(count > sd->spiritball)
  712. count = sd->spiritball;
  713. sd->spiritball -= count;
  714. if(count > MAX_SPIRITBALL)
  715. count = MAX_SPIRITBALL;
  716. for(i=0;i<count;i++) {
  717. if(sd->spirit_timer[i] != INVALID_TIMER) {
  718. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  719. sd->spirit_timer[i] = INVALID_TIMER;
  720. }
  721. }
  722. for(i=count;i<MAX_SPIRITBALL;i++) {
  723. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  724. sd->spirit_timer[i] = INVALID_TIMER;
  725. }
  726. if(!type) {
  727. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  728. clif_millenniumshield(&sd->bl,sd->spiritball);
  729. else
  730. clif_spiritball(&sd->bl);
  731. }
  732. }
  733. /**
  734. * Adds a soulball to player
  735. * @param sd: Player data
  736. * @param max: Max amount of soulballs
  737. */
  738. int pc_addsoulball(map_session_data *sd, int max)
  739. {
  740. nullpo_ret(sd);
  741. status_change *sc = status_get_sc(&sd->bl);
  742. if (sc == nullptr || sc->getSCE(SC_SOULENERGY) == nullptr) {
  743. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, 0, skill_get_time2(SP_SOULCOLLECT, 1));
  744. sd->soulball = 0;
  745. }
  746. max = min(max, MAX_SOUL_BALL);
  747. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  748. if (sd->soulball && sd->soulball >= max)
  749. sd->soulball--;
  750. sd->soulball++;
  751. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, sd->soulball, skill_get_time2(SP_SOULCOLLECT, 1));
  752. clif_soulball(sd);
  753. return 0;
  754. }
  755. /**
  756. * Removes number of soulball from player
  757. * @param sd: Player data
  758. * @param count: Amount to remove
  759. * @param type: true = doesn't give client effect
  760. */
  761. int pc_delsoulball(map_session_data *sd, int count, bool type)
  762. {
  763. nullpo_ret(sd);
  764. if (count <= 0)
  765. return 0;
  766. status_change *sc = status_get_sc(&sd->bl);
  767. if (sd->soulball <= 0 || sc == nullptr || sc->getSCE(SC_SOULENERGY) == nullptr) {
  768. sd->soulball = 0;
  769. }else{
  770. sd->soulball -= cap_value(count, 0, sd->soulball);
  771. if (sd->soulball == 0)
  772. status_change_end(&sd->bl, SC_SOULENERGY);
  773. else
  774. sc->getSCE(SC_SOULENERGY)->val1 = sd->soulball;
  775. }
  776. if (!type)
  777. clif_soulball(sd);
  778. return 0;
  779. }
  780. /**
  781. * Adds servantballs to a player
  782. * @param sd: Player data
  783. * @param amount: Amount to add
  784. */
  785. void pc_addservantball( map_session_data& sd, int count ){
  786. sd.servantball = cap_value( sd.servantball + count, 0, MAX_SERVANTBALL );
  787. clif_servantball( sd );
  788. }
  789. /**
  790. * Removes number of servantballs from player
  791. * @param sd: Player data
  792. * @param count: Amount to remove
  793. */
  794. void pc_delservantball( map_session_data& sd, int count ){
  795. sd.servantball = cap_value( sd.servantball - count, 0, MAX_SERVANTBALL );
  796. clif_servantball( sd );
  797. }
  798. /**
  799. * Adds abyssballs to a player
  800. * @param sd: Player data
  801. * @param amount: Amount to add
  802. */
  803. void pc_addabyssball( map_session_data& sd, int count ){
  804. sd.abyssball = cap_value( sd.abyssball + count, 0, MAX_ABYSSBALL );
  805. clif_abyssball( sd );
  806. }
  807. /**
  808. * Removes number of abyssballs from player
  809. * @param sd: Player data
  810. * @param count: Amount to remove
  811. */
  812. void pc_delabyssball( map_session_data& sd, int count ){
  813. sd.abyssball = cap_value( sd.abyssball - count, 0, MAX_ABYSSBALL );
  814. clif_abyssball( sd );
  815. }
  816. /**
  817. * Increases a player's fame points and displays a notice to him
  818. * @param sd Player
  819. * @param count Fame point
  820. * @return true: on success, false: on error
  821. */
  822. bool pc_addfame(map_session_data &sd, int count)
  823. {
  824. enum e_rank ranktype;
  825. sd.status.fame += count;
  826. if (sd.status.fame > MAX_FAME)
  827. sd.status.fame = MAX_FAME;
  828. switch(sd.class_&MAPID_UPPERMASK){
  829. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  830. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  831. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  832. default:
  833. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd.status.class_), sd.status.class_ );
  834. return false;
  835. }
  836. clif_update_rankingpoint(sd, ranktype, count);
  837. chrif_updatefamelist(sd, ranktype);
  838. return true;
  839. }
  840. /**
  841. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  842. * @param sd
  843. * @param job Job use enum e_mapid
  844. * @return Rank
  845. */
  846. unsigned char pc_famerank(uint32 char_id, int job)
  847. {
  848. uint8 i;
  849. switch(job){
  850. case MAPID_BLACKSMITH: // Blacksmith
  851. for(i = 0; i < MAX_FAME_LIST; i++){
  852. if(smith_fame_list[i].id == char_id)
  853. return i + 1;
  854. }
  855. break;
  856. case MAPID_ALCHEMIST: // Alchemist
  857. for(i = 0; i < MAX_FAME_LIST; i++){
  858. if(chemist_fame_list[i].id == char_id)
  859. return i + 1;
  860. }
  861. break;
  862. case MAPID_TAEKWON: // Taekwon
  863. for(i = 0; i < MAX_FAME_LIST; i++){
  864. if(taekwon_fame_list[i].id == char_id)
  865. return i + 1;
  866. }
  867. break;
  868. }
  869. return 0;
  870. }
  871. /**
  872. * Restart player's HP & SP value
  873. * @param sd
  874. * @param type Restart type: 1 - Normal Resurection
  875. */
  876. void pc_setrestartvalue(map_session_data *sd, char type) {
  877. struct status_data *status, *b_status;
  878. nullpo_retv(sd);
  879. b_status = &sd->base_status;
  880. status = &sd->battle_status;
  881. if (type&1) { //Normal resurrection
  882. status->hp = 1; //Otherwise status_heal may fail if dead.
  883. status_heal(&sd->bl, b_status->hp, 0, 1);
  884. if( status->sp < b_status->sp )
  885. status_set_sp(&sd->bl, b_status->sp, 1);
  886. } else { //Just for saving on the char-server (with values as if respawned)
  887. sd->status.hp = b_status->hp;
  888. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  889. sd->status.ap = (status->ap < b_status->ap)?b_status->ap:status->ap;
  890. }
  891. }
  892. /*==========================================
  893. Rental System
  894. *------------------------------------------*/
  895. /**
  896. * Ends a rental and removes the item/effect
  897. * @param tid: Tick ID
  898. * @param tick: Timer
  899. * @param id: Timer ID
  900. * @param data: Data
  901. * @return false - failure, true - success
  902. */
  903. TIMER_FUNC(pc_inventory_rental_end){
  904. map_session_data *sd = map_id2sd(id);
  905. if( sd == NULL )
  906. return 0;
  907. if( tid != sd->rental_timer ) {
  908. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  909. return 0;
  910. }
  911. pc_inventory_rentals(sd);
  912. return 1;
  913. }
  914. /**
  915. * Removes the rental timer from the player
  916. * @param sd: Player data
  917. */
  918. void pc_inventory_rental_clear(map_session_data *sd)
  919. {
  920. if( sd->rental_timer != INVALID_TIMER ) {
  921. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  922. sd->rental_timer = INVALID_TIMER;
  923. }
  924. }
  925. /**
  926. * Check for items in the player's inventory that are rental type
  927. * @param sd: Player data
  928. */
  929. void pc_inventory_rentals(map_session_data *sd)
  930. {
  931. int i, c = 0;
  932. unsigned int next_tick = UINT_MAX;
  933. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  934. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  935. continue; // Nothing here
  936. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  937. continue;
  938. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  939. if (sd->inventory_data[i]->unequip_script)
  940. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  941. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  942. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  943. } else {
  944. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  945. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  946. next_tick = umin(expire_tick * 1000U, next_tick);
  947. c++;
  948. }
  949. }
  950. if( c > 0 ) // min(next_tick,3600000) 1 hour each timer to keep announcing to the owner, and to avoid a but with rental time > 15 days
  951. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  952. else
  953. sd->rental_timer = INVALID_TIMER;
  954. }
  955. /**
  956. * Add a rental item to the player and adjusts the rental timer appropriately
  957. * @param sd: Player data
  958. * @param seconds: Rental time
  959. */
  960. void pc_inventory_rental_add(map_session_data *sd, unsigned int seconds)
  961. {
  962. t_tick tick = seconds * 1000;
  963. if( sd == NULL )
  964. return;
  965. if( sd->rental_timer != INVALID_TIMER ) {
  966. const struct TimerData * td;
  967. td = get_timer(sd->rental_timer);
  968. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  969. pc_inventory_rental_clear(sd);
  970. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  971. }
  972. } else
  973. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  974. }
  975. /**
  976. * Check if the player can sell the current item
  977. * @param sd: map_session_data of the player
  978. * @param item: struct of the checking item
  979. * @param shoptype: NPC's sub type see enum npc_subtype
  980. * @return bool 'true' is sellable, 'false' otherwise
  981. */
  982. bool pc_can_sell_item(map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  983. if (sd == NULL || item == NULL)
  984. return false;
  985. if (!pc_can_give_items(sd))
  986. return false;
  987. if (item->equip > 0 || item->amount < 0)
  988. return false;
  989. if (battle_config.hide_fav_sell && item->favorite)
  990. return false; //Cannot sell favs (optional config)
  991. if (!battle_config.rental_transaction && item->expire_time)
  992. return false; // Cannot Sell Rental Items
  993. if( item->equipSwitch ){
  994. return false;
  995. }
  996. if (itemdb_ishatched_egg(item))
  997. return false;
  998. switch (shoptype) {
  999. case NPCTYPE_SHOP:
  1000. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  1001. item->bound != BOUND_GUILD ||
  1002. (sd->guild && sd->status.char_id == sd->guild->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->guild.member[0].char_id) ||
  1011. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  1012. ))
  1013. return true;
  1014. else if (!item->bound) {
  1015. struct item_data *itd = itemdb_search(item->nameid);
  1016. if (itd && itd->flag.trade_restriction.sell && battle_config.allow_bound_sell&ISR_SELLABLE)
  1017. return true;
  1018. }
  1019. break;
  1020. }
  1021. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  1022. return false;
  1023. if (item->bound && !pc_can_give_bounded_items(sd))
  1024. return false; // Don't allow sale of bound items
  1025. return true;
  1026. }
  1027. /**
  1028. * Determines if player can give / drop / trade / vend items
  1029. */
  1030. bool pc_can_give_items(map_session_data *sd)
  1031. {
  1032. return pc_has_permission(sd, PC_PERM_TRADE);
  1033. }
  1034. /**
  1035. * Determines if player can give / drop / trade / vend bounded items
  1036. */
  1037. bool pc_can_give_bounded_items(map_session_data *sd)
  1038. {
  1039. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  1040. }
  1041. /**
  1042. * Determine if an item in a player's inventory is tradeable based on several merits.
  1043. * Checks for item_trade, bound, and rental restrictions.
  1044. * @param sd: Player data
  1045. * @param index: Item inventory index
  1046. * @return True if the item can be traded or false otherwise
  1047. */
  1048. bool pc_can_trade_item(map_session_data *sd, int index) {
  1049. if (sd && index >= 0) {
  1050. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  1051. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  1052. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  1053. );
  1054. }
  1055. return false;
  1056. }
  1057. /*==========================================
  1058. * Prepares character for saving.
  1059. * @param sd
  1060. *------------------------------------------*/
  1061. void pc_makesavestatus(map_session_data *sd) {
  1062. nullpo_retv(sd);
  1063. if(!battle_config.save_clothcolor)
  1064. sd->status.clothes_color = 0;
  1065. if(!battle_config.save_body_style)
  1066. sd->status.body = 0;
  1067. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  1068. //status change load/saving. [Skotlex]
  1069. #ifdef NEW_CARTS
  1070. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1071. #else
  1072. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1073. #endif
  1074. // Mark last point as not instance related by default
  1075. sd->status.last_point_instanceid = 0;
  1076. if (sd->sc.getSCE(SC_JAILED)) { //When Jailed, do not move last point.
  1077. if(pc_isdead(sd)){
  1078. pc_setrestartvalue(sd, 0);
  1079. } else {
  1080. sd->status.hp = sd->battle_status.hp;
  1081. sd->status.sp = sd->battle_status.sp;
  1082. sd->status.ap = sd->battle_status.ap;
  1083. }
  1084. mapindex_getmapname( mapindex_id2name( sd->mapindex ), sd->status.last_point.map );
  1085. sd->status.last_point.x = sd->bl.x;
  1086. sd->status.last_point.y = sd->bl.y;
  1087. return;
  1088. }
  1089. if( pc_isdead( sd ) ){
  1090. pc_setrestartvalue( sd, 0 );
  1091. // Return to save point
  1092. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1093. sd->status.last_point.x = sd->status.save_point.x;
  1094. sd->status.last_point.y = sd->status.save_point.y;
  1095. }else{
  1096. sd->status.hp = sd->battle_status.hp;
  1097. sd->status.sp = sd->battle_status.sp;
  1098. sd->status.ap = sd->battle_status.ap;
  1099. struct map_data* mapdata = map_getmapdata( sd->bl.m );
  1100. // If saving is not allowed on the map, we return the player to the designated point
  1101. if( mapdata->getMapFlag(MF_NOSAVE) ){
  1102. // The map has a specific return point
  1103. if( mapdata->save.map ){
  1104. safestrncpy( sd->status.last_point.map, mapindex_id2name( mapdata->save.map ), sizeof( sd->status.last_point.map ) );
  1105. sd->status.last_point.x = mapdata->save.x;
  1106. sd->status.last_point.y = mapdata->save.y;
  1107. // Return the user to his save point
  1108. }else{
  1109. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1110. sd->status.last_point.x = sd->status.save_point.x;
  1111. sd->status.last_point.y = sd->status.save_point.y;
  1112. }
  1113. // If the user is on a instance map, special handling is needed
  1114. }else if( mapdata->instance_id ){
  1115. if( battle_config.instance_allow_reconnect ){
  1116. // Store the original mapname
  1117. struct map_data* mapdata_source = map_getmapdata( mapdata->instance_src_map );
  1118. mapindex_getmapname( mapindex_id2name( mapdata_source->index ), sd->status.last_point.map );
  1119. sd->status.last_point.x = sd->bl.x;
  1120. sd->status.last_point.y = sd->bl.y;
  1121. sd->status.last_point_instanceid = mapdata->instance_id;
  1122. }else{
  1123. // Return the user to his save point
  1124. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1125. sd->status.last_point.x = sd->status.save_point.x;
  1126. sd->status.last_point.y = sd->status.save_point.y;
  1127. }
  1128. }else{
  1129. // Save normally
  1130. mapindex_getmapname( mapindex_id2name( sd->mapindex ), sd->status.last_point.map );
  1131. sd->status.last_point.x = sd->bl.x;
  1132. sd->status.last_point.y = sd->bl.y;
  1133. }
  1134. }
  1135. }
  1136. /*==========================================
  1137. * Off init ? Connection?
  1138. *------------------------------------------*/
  1139. void pc_setnewpc(map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, t_tick client_tick, int sex, int fd) {
  1140. nullpo_retv(sd);
  1141. sd->bl.id = account_id;
  1142. sd->status.account_id = account_id;
  1143. sd->status.char_id = char_id;
  1144. sd->status.sex = sex;
  1145. sd->login_id1 = login_id1;
  1146. sd->login_id2 = 0; // at this point, we can not know the value :(
  1147. sd->client_tick = client_tick;
  1148. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  1149. sd->bl.type = BL_PC;
  1150. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  1151. sd->canlog_tick = gettick();
  1152. //Required to prevent homunculus copuing a base speed of 0.
  1153. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  1154. }
  1155. /**
  1156. * Get equip point for an equip
  1157. * @param sd
  1158. * @param id
  1159. */
  1160. int pc_equippoint_sub(map_session_data *sd,struct item_data* id){
  1161. int ep = 0;
  1162. nullpo_ret(sd);
  1163. nullpo_ret(id);
  1164. if (!itemdb_isequip2(id))
  1165. return 0; //Not equippable by players.
  1166. ep = id->equip;
  1167. if(id->subtype == W_DAGGER || id->subtype == W_1HSWORD || id->subtype == W_1HAXE) {
  1168. 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]
  1169. if (ep == EQP_WEAPON)
  1170. return EQP_ARMS;
  1171. if (ep == EQP_SHADOW_WEAPON)
  1172. return EQP_SHADOW_ARMS;
  1173. }
  1174. }
  1175. return ep;
  1176. }
  1177. /**
  1178. * Get equip point for an equip
  1179. * @param sd
  1180. * @param n Equip index in inventory
  1181. */
  1182. int pc_equippoint(map_session_data *sd,int n){
  1183. nullpo_ret(sd);
  1184. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  1185. }
  1186. /**
  1187. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  1188. * @param sd : player session
  1189. * @return 0 sucess, 1:invalid sd
  1190. */
  1191. void pc_setinventorydata(map_session_data *sd)
  1192. {
  1193. uint8 i;
  1194. nullpo_retv(sd);
  1195. for(i = 0; i < MAX_INVENTORY; i++) {
  1196. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  1197. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  1198. }
  1199. }
  1200. /**
  1201. * 'Calculates' weapon type
  1202. * @param sd : Player
  1203. */
  1204. void pc_calcweapontype(map_session_data *sd)
  1205. {
  1206. nullpo_retv(sd);
  1207. // single-hand
  1208. if(sd->weapontype2 == W_FIST) {
  1209. sd->status.weapon = sd->weapontype1;
  1210. return;
  1211. }
  1212. if(sd->weapontype1 == W_FIST) {
  1213. sd->status.weapon = sd->weapontype2;
  1214. return;
  1215. }
  1216. // dual-wield
  1217. sd->status.weapon = W_FIST;
  1218. switch (sd->weapontype1){
  1219. case W_DAGGER:
  1220. switch (sd->weapontype2) {
  1221. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  1222. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  1223. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  1224. }
  1225. break;
  1226. case W_1HSWORD:
  1227. switch (sd->weapontype2) {
  1228. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  1229. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  1230. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  1231. }
  1232. break;
  1233. case W_1HAXE:
  1234. switch (sd->weapontype2) {
  1235. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  1236. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  1237. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  1238. }
  1239. }
  1240. // unknown, default to right hand type
  1241. if (!sd->status.weapon)
  1242. sd->status.weapon = sd->weapontype1;
  1243. }
  1244. /**
  1245. * Set equip index
  1246. * @param sd : Player
  1247. */
  1248. void pc_setequipindex(map_session_data *sd)
  1249. {
  1250. uint16 i;
  1251. nullpo_retv(sd);
  1252. for (i = 0; i < EQI_MAX; i++){
  1253. sd->equip_index[i] = -1;
  1254. sd->equip_switch_index[i] = -1;
  1255. }
  1256. for (i = 0; i < MAX_INVENTORY; i++) {
  1257. if (sd->inventory.u.items_inventory[i].nameid == 0)
  1258. continue;
  1259. if (sd->inventory.u.items_inventory[i].equip) {
  1260. uint8 j;
  1261. for (j = 0; j < EQI_MAX; j++)
  1262. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  1263. sd->equip_index[j] = i;
  1264. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  1265. if (sd->inventory_data[i])
  1266. sd->weapontype1 = sd->inventory_data[i]->subtype;
  1267. else
  1268. sd->weapontype1 = W_FIST;
  1269. }
  1270. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  1271. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  1272. sd->weapontype2 = sd->inventory_data[i]->subtype;
  1273. else
  1274. sd->weapontype2 = W_FIST;
  1275. }
  1276. }
  1277. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  1278. for (uint8 j = 0; j < EQI_MAX; j++) {
  1279. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  1280. sd->equip_switch_index[j] = i;
  1281. }
  1282. }
  1283. }
  1284. }
  1285. pc_calcweapontype(sd);
  1286. }
  1287. //static int pc_isAllowedCardOn(map_session_data *sd,int s,int eqindex,int flag)
  1288. //{
  1289. // int i;
  1290. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  1291. // std::shared_ptr<item_data> data;
  1292. //
  1293. // //Crafted/made/hatched items.
  1294. // if (itemdb_isspecial(item->card[0]))
  1295. // return 1;
  1296. //
  1297. // /* scan for enchant armor gems */
  1298. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  1299. // s = MAX_SLOTS - 1;
  1300. //
  1301. // ARR_FIND( 0, s, i, item->card[i] && (data = item_db.find(item->card[i])) != NULL && data->flag.no_equip&flag );
  1302. // return( i < s ) ? 0 : 1;
  1303. //}
  1304. /**
  1305. * Check if an item is equiped by player
  1306. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  1307. * @param sd : player session
  1308. * @param nameid : itemid
  1309. * @return 1:yes, 0:no
  1310. */
  1311. bool pc_isequipped(map_session_data *sd, t_itemid nameid)
  1312. {
  1313. uint8 i;
  1314. for( i = 0; i < EQI_MAX; i++ )
  1315. {
  1316. short index = sd->equip_index[i], j;
  1317. if( index < 0 )
  1318. continue;
  1319. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  1320. continue;
  1321. if( !sd->inventory_data[index] )
  1322. continue;
  1323. if( sd->inventory_data[index]->nameid == nameid )
  1324. return true;
  1325. for( j = 0; j < MAX_SLOTS; j++ ){
  1326. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  1327. return true;
  1328. }
  1329. }
  1330. return false;
  1331. }
  1332. /**
  1333. * Check adoption rules
  1334. * @param p1_sd: Player 1
  1335. * @param p2_sd: Player 2
  1336. * @param b_sd: Player that will be adopted
  1337. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  1338. * ADOPT_ALREADY_ADOPTED - Already adopted
  1339. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  1340. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  1341. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  1342. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  1343. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  1344. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  1345. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  1346. */
  1347. enum adopt_responses pc_try_adopt(map_session_data *p1_sd, map_session_data *p2_sd, map_session_data *b_sd)
  1348. {
  1349. if( !p1_sd || !p2_sd || !b_sd )
  1350. return ADOPT_CHARACTER_NOT_FOUND;
  1351. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1352. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1353. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1354. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1355. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1356. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1357. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1358. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1359. // Parents need to have their ring equipped
  1360. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1361. return ADOPT_EQUIP_RINGS;
  1362. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1363. return ADOPT_EQUIP_RINGS;
  1364. // Already adopted a baby
  1365. if( p1_sd->status.child || p2_sd->status.child ) {
  1366. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1367. return ADOPT_MORE_CHILDREN;
  1368. }
  1369. // Parents need at least lvl 70 to adopt
  1370. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1371. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1372. return ADOPT_LEVEL_70;
  1373. }
  1374. if( b_sd->status.partner_id ) {
  1375. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1376. return ADOPT_MARRIED;
  1377. }
  1378. 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 ) )
  1379. return ADOPT_NOT_NOVICE;
  1380. return ADOPT_ALLOWED;
  1381. }
  1382. /*==========================================
  1383. * Adoption Process
  1384. *------------------------------------------*/
  1385. bool pc_adoption(map_session_data *p1_sd, map_session_data *p2_sd, map_session_data *b_sd)
  1386. {
  1387. int job, joblevel;
  1388. t_exp jobexp;
  1389. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1390. return false;
  1391. // Preserve current job levels and progress
  1392. joblevel = b_sd->status.job_level;
  1393. jobexp = b_sd->status.job_exp;
  1394. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1395. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1396. { // Success, proceed to configure parents and baby skills
  1397. p1_sd->status.child = b_sd->status.char_id;
  1398. p2_sd->status.child = b_sd->status.char_id;
  1399. b_sd->status.father = p1_sd->status.char_id;
  1400. b_sd->status.mother = p2_sd->status.char_id;
  1401. // Restore progress
  1402. b_sd->status.job_level = joblevel;
  1403. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1404. b_sd->status.job_exp = jobexp;
  1405. clif_updatestatus(b_sd, SP_JOBEXP);
  1406. // Baby Skills
  1407. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1408. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1409. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1410. // Parents Skills
  1411. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1412. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1413. chrif_save(p1_sd, CSAVE_NORMAL);
  1414. chrif_save(p2_sd, CSAVE_NORMAL);
  1415. chrif_save(b_sd, CSAVE_NORMAL);
  1416. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1417. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1418. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1419. return true;
  1420. }
  1421. return false; // Job Change Fail
  1422. }
  1423. static bool pc_job_can_use_item( map_session_data* sd, struct item_data* item ){
  1424. nullpo_retr( false, sd );
  1425. nullpo_retr( false, item );
  1426. // Calculate the required bit to check
  1427. uint64 job = 1ULL << ( sd->class_ & MAPID_BASEMASK );
  1428. size_t index;
  1429. // 2-1
  1430. if( ( sd->class_ & JOBL_2_1 ) != 0 ){
  1431. index = 1;
  1432. // 2-2
  1433. }else if( ( sd->class_ & JOBL_2_2 ) != 0 ){
  1434. index = 2;
  1435. // Basejob
  1436. }else{
  1437. index = 0;
  1438. }
  1439. return ( item->class_base[index] & job ) != 0;
  1440. }
  1441. /*==========================================
  1442. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1443. Returns:
  1444. false : Cannot use/equip
  1445. true : Can use/equip
  1446. * Credits:
  1447. [Inkfish] for first idea
  1448. [Haru] for third-classes extension
  1449. [Cydh] finishing :D
  1450. *------------------------------------------*/
  1451. static bool pc_isItemClass (map_session_data *sd, struct item_data* item) {
  1452. while (1) {
  1453. 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)
  1454. break;
  1455. #ifndef RENEWAL
  1456. //allow third classes to use trans. class items
  1457. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1458. break;
  1459. //third-baby classes can use same item too
  1460. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1461. break;
  1462. //don't need to decide specific rules for third-classes?
  1463. //items for third classes can be used for all third classes
  1464. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_UPPER|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1465. break;
  1466. #else
  1467. //trans. classes (exl. third-trans.)
  1468. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1469. break;
  1470. //baby classes (exl. third-baby)
  1471. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1472. break;
  1473. //third classes (exl. third-trans. and baby-third and fourth)
  1474. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)) && !(sd->class_&JOBL_FOURTH))
  1475. break;
  1476. //trans-third classes (exl. fourth)
  1477. if (item->class_upper&ITEMJ_THIRD_UPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_FOURTH))
  1478. break;
  1479. //third-baby classes (exl. fourth)
  1480. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY && !(sd->class_&JOBL_FOURTH))
  1481. break;
  1482. //fourth classes
  1483. if (item->class_upper&ITEMJ_FOURTH && sd->class_&JOBL_FOURTH)
  1484. break;
  1485. #endif
  1486. return false;
  1487. }
  1488. return true;
  1489. }
  1490. /*=================================================
  1491. * Checks if the player can equip the item at index n in inventory.
  1492. * @param sd
  1493. * @param n Item index in inventory
  1494. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1495. *------------------------------------------------*/
  1496. uint8 pc_isequip(map_session_data *sd,int n)
  1497. {
  1498. struct item_data *item;
  1499. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1500. item = sd->inventory_data[n];
  1501. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1502. return ITEM_EQUIP_ACK_OK;
  1503. if(item == NULL)
  1504. return ITEM_EQUIP_ACK_FAIL;
  1505. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1506. return ITEM_EQUIP_ACK_FAILLEVEL;
  1507. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1508. return ITEM_EQUIP_ACK_FAILLEVEL;
  1509. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1510. return ITEM_EQUIP_ACK_FAIL;
  1511. //fail to equip if item is restricted
  1512. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1513. return ITEM_EQUIP_ACK_FAIL;
  1514. if (item->equip&EQP_AMMO) {
  1515. switch (item->subtype) {
  1516. case AMMO_ARROW:
  1517. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1518. clif_msg(sd, ITEM_NEED_BOW);
  1519. return ITEM_EQUIP_ACK_FAIL;
  1520. }
  1521. break;
  1522. case AMMO_DAGGER:
  1523. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1524. return ITEM_EQUIP_ACK_FAIL;
  1525. break;
  1526. case AMMO_BULLET:
  1527. case AMMO_SHELL:
  1528. 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
  1529. #ifdef RENEWAL
  1530. && sd->status.weapon != W_GRENADE
  1531. #endif
  1532. ) {
  1533. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1534. return ITEM_EQUIP_ACK_FAIL;
  1535. }
  1536. break;
  1537. #ifndef RENEWAL
  1538. case AMMO_GRENADE:
  1539. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1540. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1541. return ITEM_EQUIP_ACK_FAIL;
  1542. }
  1543. break;
  1544. #endif
  1545. case AMMO_CANNONBALL:
  1546. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1547. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1548. return ITEM_EQUIP_ACK_FAIL;
  1549. }
  1550. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1551. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1552. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1553. return ITEM_EQUIP_ACK_FAIL;
  1554. }
  1555. break;
  1556. }
  1557. }
  1558. if (sd->sc.count) {
  1559. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.getSCE(SC_STRIPWEAPON)) // Also works with left-hand weapons [DracoRPG]
  1560. return ITEM_EQUIP_ACK_FAIL;
  1561. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.getSCE(SC_STRIPSHIELD))
  1562. return ITEM_EQUIP_ACK_FAIL;
  1563. if(item->equip & EQP_ARMOR && sd->sc.getSCE(SC_STRIPARMOR))
  1564. return ITEM_EQUIP_ACK_FAIL;
  1565. if(item->equip & EQP_HEAD_TOP && sd->sc.getSCE(SC_STRIPHELM))
  1566. return ITEM_EQUIP_ACK_FAIL;
  1567. if(item->equip & EQP_ACC && sd->sc.getSCE(SC__STRIPACCESSORY))
  1568. return ITEM_EQUIP_ACK_FAIL;
  1569. if (item->equip & EQP_ARMS && sd->sc.getSCE(SC__WEAKNESS))
  1570. return ITEM_EQUIP_ACK_FAIL;
  1571. if (item->equip & EQP_SHADOW_GEAR && sd->sc.getSCE(SC_SHADOW_STRIP))
  1572. return ITEM_EQUIP_ACK_FAIL;
  1573. if(item->equip && (sd->sc.getSCE(SC_KYOUGAKU) || sd->sc.getSCE(SC_SUHIDE)))
  1574. return ITEM_EQUIP_ACK_FAIL;
  1575. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_SUPERNOVICE) {
  1576. //Spirit of Super Novice equip bonuses. [Skotlex]
  1577. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1578. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1579. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->weapon_level == 4)
  1580. switch(item->subtype) { //In weapons, the look determines type of weapon.
  1581. case W_DAGGER: //All level 4 - Daggers
  1582. case W_1HSWORD: //All level 4 - 1H Swords
  1583. case W_1HAXE: //All level 4 - 1H Axes
  1584. case W_MACE: //All level 4 - 1H Maces
  1585. case W_STAFF: //All level 4 - 1H Staves
  1586. case W_2HSTAFF: //All level 4 - 2H Staves
  1587. return ITEM_EQUIP_ACK_OK;
  1588. }
  1589. }
  1590. }
  1591. //Not equipable by class. [Skotlex]
  1592. if (!pc_job_can_use_item(sd,item))
  1593. return ITEM_EQUIP_ACK_FAIL;
  1594. if (!pc_isItemClass(sd, item))
  1595. return ITEM_EQUIP_ACK_FAIL;
  1596. return ITEM_EQUIP_ACK_OK;
  1597. }
  1598. /**
  1599. * Performs some special modifications to a player's last point location,
  1600. * if the map has a nosave mapflag or if the map was an instance.
  1601. * @param sd: Player data
  1602. * @return True if the player should be returned to his savepoint or false if not
  1603. */
  1604. bool pc_lastpoint_special( map_session_data& sd ){
  1605. int16 mapid = map_mapname2mapid( sd.status.last_point.map );
  1606. // Should not happen because otherwise the char-server would have sent the player to another map-server
  1607. if( mapid < 0 ){
  1608. // Return the player to his savepoint
  1609. return true;
  1610. }
  1611. struct map_data* mapdata = map_getmapdata( mapid );
  1612. if( mapdata == nullptr ){
  1613. // Return the player to his savepoint
  1614. return true;
  1615. }
  1616. if (strcmpi(sd.status.last_point.map, MAP_JAIL) == 0) {
  1617. // Don't return jailed player to save point.
  1618. return false;
  1619. }
  1620. // Maybe since the player's logout the nosave mapflag was added to the map
  1621. if( mapdata->getMapFlag(MF_NOSAVE) ){
  1622. // The map has a specific return point
  1623. if( mapdata->save.map ){
  1624. safestrncpy( sd.status.last_point.map, mapindex_id2name( mapdata->save.map ), sizeof( sd.status.last_point.map ) );
  1625. sd.status.last_point.x = mapdata->save.x;
  1626. sd.status.last_point.y = mapdata->save.y;
  1627. return false;
  1628. }else{
  1629. // Return the user to his save point
  1630. return true;
  1631. }
  1632. }
  1633. // Check if the last point was an instance
  1634. if( sd.status.last_point_instanceid == 0 ){
  1635. // Nothing to do
  1636. return false;
  1637. }
  1638. // Check if returning to the instance is allowed in general
  1639. if( !battle_config.instance_allow_reconnect ){
  1640. // Return the player to his savepoint
  1641. return true;
  1642. }
  1643. std::shared_ptr<s_instance_data> instance_data = util::umap_find( instances, sd.status.last_point_instanceid );
  1644. if( instance_data == nullptr ){
  1645. // Instance does not exist anymore, return the player to his savepoint
  1646. return true;
  1647. }
  1648. switch( instance_data->mode ){
  1649. case IM_NONE:
  1650. // Cannot validate
  1651. break;
  1652. case IM_CHAR:
  1653. if( sd.status.char_id != instance_data->owner_id ){
  1654. // It is a character bound instance and the player is not the owner, return the player to his savepoint
  1655. return true;
  1656. }
  1657. break;
  1658. case IM_PARTY:
  1659. if( sd.status.party_id != instance_data->owner_id ){
  1660. // It is a party bound instance and the player is not in the party, return the player to his savepoint
  1661. return true;
  1662. }
  1663. break;
  1664. case IM_GUILD:
  1665. if( sd.status.guild_id != instance_data->owner_id ){
  1666. // It is a guild bound instance and the player is not in the guild, return the player to his savepoint
  1667. return true;
  1668. }
  1669. break;
  1670. case IM_CLAN:
  1671. if( sd.status.clan_id != instance_data->owner_id ){
  1672. // It is a clan bound instance and the player is not in the clan, return the player to his savepoint
  1673. return true;
  1674. }
  1675. break;
  1676. }
  1677. int16 imapid = instance_mapid( mapid, sd.status.last_point_instanceid );
  1678. if( imapid < 0 ){
  1679. // Instance does not contain this map anymore, return the player to his savepoint
  1680. return true;
  1681. }
  1682. // Overwrite the stored "source mapname" with the real mapname
  1683. instance_generate_mapname( mapid, sd.status.last_point_instanceid, sd.status.last_point.map );
  1684. // X/Y coordinates are already correct and do not need to be modified
  1685. // Dont warp the player back to his savepoint
  1686. return false;
  1687. }
  1688. /*==========================================
  1689. * No problem with the session id
  1690. * set the status that has been sent from char server
  1691. *------------------------------------------*/
  1692. bool pc_authok(map_session_data *sd, uint32 login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
  1693. {
  1694. t_tick tick = gettick();
  1695. uint32 ip = session[sd->fd]->client_addr;
  1696. sd->login_id2 = login_id2;
  1697. sd->group_id = group_id;
  1698. /* load user permissions */
  1699. pc_group_pc_load(sd);
  1700. memcpy(&sd->status, st, sizeof(*st));
  1701. if (st->sex != sd->status.sex) {
  1702. clif_authfail_fd(sd->fd, 0);
  1703. return false;
  1704. }
  1705. //Set the map-server used job id. [Skotlex]
  1706. uint64 class_ = pc_jobid2mapid(sd->status.class_);
  1707. if (class_ == -1 || !job_db.exists(sd->status.class_)) { //Invalid class?
  1708. 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);
  1709. sd->status.class_ = JOB_NOVICE;
  1710. sd->class_ = MAPID_NOVICE;
  1711. } else
  1712. sd->class_ = class_;
  1713. // Checks and fixes to character status data, that are required
  1714. // in case of configuration change or stuff, which cannot be
  1715. // checked on char-server.
  1716. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1717. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1718. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1719. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1720. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1721. sd->state.connect_new = 1;
  1722. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1723. sd->invincible_timer = INVALID_TIMER;
  1724. sd->npc_timer_id = INVALID_TIMER;
  1725. sd->pvp_timer = INVALID_TIMER;
  1726. sd->expiration_tid = INVALID_TIMER;
  1727. sd->autotrade_tid = INVALID_TIMER;
  1728. sd->respawn_tid = INVALID_TIMER;
  1729. sd->tid_queue_active = INVALID_TIMER;
  1730. sd->macro_detect.timer = INVALID_TIMER;
  1731. sd->skill_keep_using.tid = INVALID_TIMER;
  1732. sd->skill_keep_using.skill_id = 0;
  1733. sd->skill_keep_using.level = 0;
  1734. sd->skill_keep_using.target = 0;
  1735. #ifdef SECURE_NPCTIMEOUT
  1736. // Initialize to defaults/expected
  1737. sd->npc_idle_timer = INVALID_TIMER;
  1738. sd->npc_idle_tick = tick;
  1739. sd->npc_idle_type = NPCT_INPUT;
  1740. sd->state.ignoretimeout = false;
  1741. #endif
  1742. sd->canuseitem_tick = tick;
  1743. sd->canusecashfood_tick = tick;
  1744. sd->canequip_tick = tick;
  1745. sd->cantalk_tick = tick;
  1746. sd->canskill_tick = tick;
  1747. sd->cansendmail_tick = tick;
  1748. sd->idletime = last_tick;
  1749. sd->regen.tick.hp = tick;
  1750. sd->regen.tick.sp = tick;
  1751. for(int i = 0; i < MAX_SPIRITBALL; i++)
  1752. sd->spirit_timer[i] = INVALID_TIMER;
  1753. if (battle_config.item_auto_get)
  1754. sd->state.autoloot = 10000;
  1755. if (battle_config.disp_experience)
  1756. sd->state.showexp = 1;
  1757. if (battle_config.disp_zeny)
  1758. sd->state.showzeny = 1;
  1759. #ifdef VIP_ENABLE
  1760. if (!battle_config.vip_disp_rate)
  1761. sd->vip.disableshowrate = 1;
  1762. #endif
  1763. if (!(battle_config.display_skill_fail&2))
  1764. sd->state.showdelay = 1;
  1765. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1766. memset(&sd->cart, 0, sizeof(struct s_storage));
  1767. memset(&sd->storage, 0, sizeof(struct s_storage));
  1768. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1769. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1770. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1771. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1772. sd->status.option &= ~OPTION_INVISIBLE;
  1773. }
  1774. status_change_init(&sd->bl);
  1775. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1776. unit_dataset(&sd->bl);
  1777. sd->guild_x = -1;
  1778. sd->guild_y = -1;
  1779. sd->delayed_damage = 0;
  1780. // Event Timers
  1781. for( int i = 0; i < MAX_EVENTTIMER; i++ )
  1782. sd->eventtimer[i] = INVALID_TIMER;
  1783. // Rental Timer
  1784. sd->rental_timer = INVALID_TIMER;
  1785. for( int i = 0; i < 3; i++ )
  1786. sd->hate_mob[i] = -1;
  1787. sd->quest_log = NULL;
  1788. sd->num_quests = 0;
  1789. sd->avail_quests = 0;
  1790. sd->save_quest = false;
  1791. sd->count_rewarp = 0;
  1792. sd->mail.pending_weight = 0;
  1793. sd->mail.pending_zeny = 0;
  1794. sd->mail.pending_slots = 0;
  1795. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1796. sd->regs.arrays = NULL;
  1797. sd->vars_dirty = false;
  1798. sd->vars_ok = false;
  1799. sd->vars_received = 0x0;
  1800. // Check if the player's last point requires special handling and if conditions apply to return the player to his savepoint
  1801. if( pc_lastpoint_special( *sd ) ){
  1802. // The player should be warped back to his savepoint
  1803. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1804. sd->status.last_point.x = sd->status.save_point.x;
  1805. sd->status.last_point.y = sd->status.save_point.y;
  1806. }
  1807. //warp player
  1808. enum e_setpos setpos_result = pc_setpos( sd, mapindex_name2id( sd->status.last_point.map ), sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT );
  1809. if( setpos_result != SETPOS_OK ){
  1810. ShowError( "Last_point_map %s not found (error code %d)\n", sd->status.last_point.map, setpos_result );
  1811. // try warping to a default map instead (church graveyard)
  1812. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1813. // if we fail again
  1814. clif_authfail_fd(sd->fd, 0);
  1815. return false;
  1816. }
  1817. }
  1818. clif_inventory_expansion_info( sd );
  1819. clif_authok(sd);
  1820. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1821. sd->die_counter=-1;
  1822. //display login notice
  1823. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1824. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1825. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1826. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1827. sd->status.name, sd->status.account_id, sd->status.char_id,
  1828. CONVIP(ip), sd->group_id);
  1829. // Send friends list
  1830. clif_friendslist_send( *sd );
  1831. if( !changing_mapservers ) {
  1832. if (battle_config.display_version == 1)
  1833. pc_show_version(sd);
  1834. // Message of the Day [Valaris]
  1835. for(int i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1836. if (battle_config.motd_type)
  1837. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1838. else
  1839. clif_displaymessage(sd->fd, motd_text[i]);
  1840. }
  1841. if (expiration_time != 0)
  1842. sd->expiration_time = expiration_time;
  1843. /**
  1844. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1845. **/
  1846. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1847. }
  1848. pc_validate_skill(sd);
  1849. /* [Ind] */
  1850. sd->sc_display = NULL;
  1851. sd->sc_display_count = 0;
  1852. // Player has not yet received the CashShop list
  1853. sd->status.cashshop_sent = false;
  1854. sd->last_addeditem_index = -1;
  1855. sd->bonus_script.head = NULL;
  1856. sd->bonus_script.count = 0;
  1857. // Initialize BG queue
  1858. sd->bg_queue_id = 0;
  1859. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  1860. sd->hatEffects = {};
  1861. #endif
  1862. sd->catch_target_class = PET_CATCH_FAIL;
  1863. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1864. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1865. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1866. clif_updatestatus(sd, SP_BASEEXP);
  1867. }
  1868. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1869. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1870. clif_updatestatus(sd, SP_JOBEXP);
  1871. }
  1872. // Request all registries (auth is considered completed whence they arrive)
  1873. intif_request_registry(sd,7);
  1874. return true;
  1875. }
  1876. /*==========================================
  1877. * Closes a connection because it failed to be authenticated from the char server.
  1878. *------------------------------------------*/
  1879. void pc_authfail(map_session_data *sd)
  1880. {
  1881. clif_authfail_fd(sd->fd, 0);
  1882. return;
  1883. }
  1884. /**
  1885. * Player register a bl as hatred
  1886. * @param sd : player session
  1887. * @param pos : hate position [0;2]
  1888. * @param bl : target bl
  1889. * @return false:failed, true:success
  1890. */
  1891. bool pc_set_hate_mob(map_session_data *sd, int pos, struct block_list *bl)
  1892. {
  1893. int class_;
  1894. if (!sd || !bl || pos < 0 || pos > 2)
  1895. return false;
  1896. if (sd->hate_mob[pos] != -1)
  1897. { //Can't change hate targets.
  1898. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1899. return false;
  1900. }
  1901. class_ = status_get_class(bl);
  1902. if (!pcdb_checkid(class_)) {
  1903. unsigned int max_hp = status_get_max_hp(bl);
  1904. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1905. return false;
  1906. if (pos != status_get_size(bl))
  1907. return false; //Wrong size
  1908. }
  1909. sd->hate_mob[pos] = class_;
  1910. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1911. clif_hate_info(sd, pos, class_, 1);
  1912. return true;
  1913. }
  1914. /*==========================================
  1915. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1916. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1917. * See intif_parse_StorageReceived() for item operations [lighta]
  1918. *------------------------------------------*/
  1919. void pc_reg_received(map_session_data *sd)
  1920. {
  1921. uint8 i;
  1922. sd->vars_ok = true;
  1923. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1924. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1925. sd->change_level_4th = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE4TH_VAR)));
  1926. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1927. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1928. if (msg_checklangtype(sd->langtype,true) < 0)
  1929. sd->langtype = 0; //invalid langtype reset to default
  1930. // Cash shop
  1931. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1932. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1933. // Cooking Exp
  1934. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1935. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1936. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1937. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1938. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1939. }
  1940. if (battle_config.feature_banking)
  1941. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1942. if (battle_config.feature_roulette) {
  1943. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1944. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1945. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1946. }
  1947. sd->roulette.prizeIdx = -1;
  1948. //SG map and mob read [Komurka]
  1949. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1950. uint16 j;
  1951. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1952. sd->feel_map[i].index = j;
  1953. sd->feel_map[i].m = map_mapindex2mapid(j);
  1954. } else {
  1955. sd->feel_map[i].index = 0;
  1956. sd->feel_map[i].m = -1;
  1957. }
  1958. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1959. }
  1960. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1961. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1962. sd->cloneskill_idx = skill_get_index(skid);
  1963. if (sd->cloneskill_idx > 0) {
  1964. sd->status.skill[sd->cloneskill_idx].id = skid;
  1965. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1966. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1967. sd->status.skill[sd->cloneskill_idx].lv = i;
  1968. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1969. }
  1970. }
  1971. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1972. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1973. sd->reproduceskill_idx = skill_get_index(skid);
  1974. if (sd->reproduceskill_idx > 0) {
  1975. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1976. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1977. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1978. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1979. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1980. }
  1981. }
  1982. //Weird... maybe registries were reloaded?
  1983. if (sd->state.active)
  1984. return;
  1985. sd->state.active = 1;
  1986. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1987. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1988. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1989. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1990. // Restore IM_CHAR instance to the player
  1991. for (const auto &instance : instances) {
  1992. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1993. sd->instance_id = instance.first;
  1994. break;
  1995. }
  1996. }
  1997. if (sd->status.party_id > 0)
  1998. party_member_joined(sd);
  1999. if (sd->status.guild_id > 0)
  2000. guild_member_joined(sd);
  2001. if (sd->status.clan_id > 0)
  2002. clan_member_joined(sd);
  2003. #if !( PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410 )
  2004. // 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
  2005. clif_instance_info( *sd );
  2006. #endif
  2007. // pet
  2008. if (sd->status.pet_id > 0)
  2009. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  2010. // Homunculus [albator]
  2011. if( sd->status.hom_id > 0 )
  2012. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  2013. if( sd->status.mer_id > 0 )
  2014. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  2015. if( sd->status.ele_id > 0 )
  2016. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  2017. map_addiddb(&sd->bl);
  2018. map_delnickdb(sd->status.char_id, sd->status.name);
  2019. if (!chrif_auth_finished(sd))
  2020. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  2021. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  2022. chrif_bsdata_request(sd->status.char_id);
  2023. #ifdef VIP_ENABLE
  2024. sd->vip.time = 0;
  2025. sd->vip.enabled = 0;
  2026. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  2027. #endif
  2028. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  2029. intif_request_questlog(sd);
  2030. if (battle_config.feature_achievement) {
  2031. sd->achievement_data.total_score = 0;
  2032. sd->achievement_data.level = 0;
  2033. sd->achievement_data.save = false;
  2034. sd->achievement_data.count = 0;
  2035. sd->achievement_data.incompleteCount = 0;
  2036. sd->achievement_data.achievements = NULL;
  2037. intif_request_achievements(sd->status.char_id);
  2038. }
  2039. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  2040. sd->state.connect_new = 1;
  2041. clif_parse_LoadEndAck(sd->fd, sd);
  2042. if( pc_isinvisible( sd ) ){
  2043. sd->vd.class_ = JT_INVISIBLE;
  2044. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  2045. // decrement the number of pvp players on the map
  2046. map_getmapdata( sd->bl.m )->users_pvp--;
  2047. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  2048. // unregister the player for ranking
  2049. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  2050. sd->pvp_timer = INVALID_TIMER;
  2051. }
  2052. clif_changeoption( &sd->bl );
  2053. }
  2054. }
  2055. channel_autojoin(sd);
  2056. }
  2057. static int pc_calc_skillpoint(map_session_data* sd)
  2058. {
  2059. uint16 i, skill_point = 0;
  2060. nullpo_ret(sd);
  2061. for(i = 1; i < MAX_SKILL; i++) {
  2062. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  2063. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  2064. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  2065. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  2066. )
  2067. {
  2068. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  2069. skill_point += sd->status.skill[i].lv;
  2070. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  2071. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  2072. }
  2073. }
  2074. }
  2075. return skill_point;
  2076. }
  2077. static bool pc_grant_allskills(map_session_data *sd, bool addlv) {
  2078. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  2079. return false;
  2080. /**
  2081. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  2082. * skill tree and since they have no icons they'll give resource errors
  2083. * Get ALL skills except npc/guild ones. [Skotlex]
  2084. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  2085. **/
  2086. for (const auto &skill : skill_db) {
  2087. uint16 skill_id = skill.second->nameid;
  2088. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  2089. continue;
  2090. switch (skill_id) {
  2091. case SM_SELFPROVOKE:
  2092. case AB_DUPLELIGHT_MELEE:
  2093. case AB_DUPLELIGHT_MAGIC:
  2094. case WL_CHAINLIGHTNING_ATK:
  2095. case WL_TETRAVORTEX_FIRE:
  2096. case WL_TETRAVORTEX_WATER:
  2097. case WL_TETRAVORTEX_WIND:
  2098. case WL_TETRAVORTEX_GROUND:
  2099. case WL_SUMMON_ATK_FIRE:
  2100. case WL_SUMMON_ATK_WIND:
  2101. case WL_SUMMON_ATK_WATER:
  2102. case WL_SUMMON_ATK_GROUND:
  2103. case LG_OVERBRAND_BRANDISH:
  2104. case LG_OVERBRAND_PLUSATK:
  2105. case WM_SEVERE_RAINSTORM_MELEE:
  2106. case RL_R_TRIP_PLUSATK:
  2107. case SG_DEVIL:
  2108. case MO_TRIPLEATTACK:
  2109. case RG_SNATCHER:
  2110. continue;
  2111. default:
  2112. {
  2113. uint8 lv = (uint8)skill_get_max(skill_id);
  2114. if (lv > 0) {
  2115. uint16 idx = skill_get_index(skill_id);
  2116. sd->status.skill[idx].id = skill_id;
  2117. if (addlv)
  2118. sd->status.skill[idx].lv = lv;
  2119. }
  2120. }
  2121. break;
  2122. }
  2123. }
  2124. return true;
  2125. }
  2126. /*==========================================
  2127. * Calculation of skill level.
  2128. * @param sd
  2129. *------------------------------------------*/
  2130. void pc_calc_skilltree(map_session_data *sd)
  2131. {
  2132. nullpo_retv(sd);
  2133. uint64 job = pc_calc_skilltree_normalize_job(sd);
  2134. int class_ = pc_mapid2jobid(job, sd->status.sex);
  2135. if( class_ == -1 )
  2136. { //Unable to normalize job??
  2137. 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 );
  2138. return;
  2139. }
  2140. uint16 job_id = class_;
  2141. class_ = pc_class2idx(class_);
  2142. for (const auto &skill : skill_db) {
  2143. uint16 skill_id = skill.second->nameid;
  2144. uint16 idx = skill_get_index(skill_id);
  2145. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  2146. sd->status.skill[idx].id = 0; //First clear skills.
  2147. /* permanent skills that must be re-checked */
  2148. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  2149. if (skill_id == 0) {
  2150. sd->status.skill[idx].id = 0;
  2151. sd->status.skill[idx].lv = 0;
  2152. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2153. continue;
  2154. }
  2155. switch (skill_id) {
  2156. case NV_TRICKDEAD:
  2157. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  2158. sd->status.skill[idx].id = 0;
  2159. sd->status.skill[idx].lv = 0;
  2160. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2161. }
  2162. break;
  2163. }
  2164. }
  2165. }
  2166. for (const auto &skill : skill_db) {
  2167. uint16 idx = skill_get_index(skill.second->nameid);
  2168. // Restore original level of skills after deleting earned skills.
  2169. 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 ) {
  2170. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2171. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2172. }
  2173. }
  2174. // Removes Taekwon Ranker skill bonus
  2175. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  2176. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(JOB_TAEKWON);
  2177. if (tree != nullptr && !tree->skills.empty()) {
  2178. for (const auto &it : tree->skills) {
  2179. uint16 sk_idx = skill_get_index(it.first);
  2180. if (sk_idx == 0)
  2181. continue;
  2182. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  2183. if (it.first == NV_BASIC || it.first == NV_FIRSTAID || it.first == WE_CALLBABY)
  2184. continue;
  2185. sd->status.skill[sk_idx].id = 0;
  2186. }
  2187. }
  2188. }
  2189. }
  2190. // Grant all skills
  2191. pc_grant_allskills(sd, false);
  2192. int flag;
  2193. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2194. do {
  2195. flag = 0;
  2196. if (tree == nullptr || tree->skills.empty())
  2197. break;
  2198. for (const auto &skillsit : tree->skills) {
  2199. bool fail = false;
  2200. uint16 skid = skillsit.first;
  2201. uint16 sk_idx = skill_get_index(skid);
  2202. if (sd->status.skill[sk_idx].id)
  2203. continue; //Skill already known.
  2204. if (!battle_config.skillfree) {
  2205. // Checking required skills
  2206. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2207. if (entry != nullptr && !entry->need.empty()) {
  2208. for (const auto &it : entry->need) {
  2209. uint16 sk_need_id = it.first;
  2210. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2211. if (sk_need_idx > 0) {
  2212. uint16 sk_need = sk_need_id;
  2213. 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)
  2214. sk_need = 0; //Not learned.
  2215. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  2216. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2217. else
  2218. sk_need = pc_checkskill(sd,sk_need_id);
  2219. if (sk_need < it.second) {
  2220. fail = true;
  2221. break;
  2222. }
  2223. }
  2224. }
  2225. }
  2226. if (sd->status.base_level < entry->baselv) { //We need to get the actual class in this case
  2227. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2228. c_ = pc_class2idx(c_);
  2229. if (class_ == c_ || (class_ != c_ && sd->status.base_level < entry->baselv))
  2230. fail = true; // base level requirement wasn't satisfied
  2231. }
  2232. if (sd->status.job_level < entry->joblv) { //We need to get the actual class in this case
  2233. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2234. c_ = pc_class2idx(c_);
  2235. if (class_ == c_ || (class_ != c_ && sd->status.job_level < entry->joblv))
  2236. fail = true; // job level requirement wasn't satisfied
  2237. }
  2238. }
  2239. if (!fail) {
  2240. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2241. if (!sd->status.skill[sk_idx].lv && (
  2242. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2243. skill->inf2[INF2_ISWEDDING] ||
  2244. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2245. ))
  2246. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  2247. sd->status.skill[sk_idx].id = skid;
  2248. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  2249. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  2250. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  2251. }
  2252. flag = 1; // skill list has changed, perform another pass
  2253. }
  2254. }
  2255. } while(flag);
  2256. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  2257. uint16 skid = 0;
  2258. /* Taekwon Ranker Bonus Skill Tree
  2259. ============================================
  2260. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  2261. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  2262. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  2263. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2264. if (tree != nullptr && !tree->skills.empty()) {
  2265. for (const auto &it : tree->skills) {
  2266. skid = it.first;
  2267. uint16 sk_idx = skill_get_index(skid);
  2268. if (sk_idx == 0)
  2269. continue;
  2270. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  2271. continue; //Do not include Quest/Wedding skills.
  2272. if( sd->status.skill[sk_idx].id == 0 ) {
  2273. sd->status.skill[sk_idx].id = skid;
  2274. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  2275. } else if( skid != NV_BASIC )
  2276. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  2277. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  2278. }
  2279. }
  2280. }
  2281. // Enable Bard/Dancer spirit linked skills.
  2282. if (sd->sc.count && sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_BARDDANCER) {
  2283. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  2284. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  2285. { BA_POEMBRAGI, DC_FORTUNEKISS },
  2286. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  2287. for (const auto &skill : linked_skills) {
  2288. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  2289. continue;
  2290. // Tag it as a non-savable, non-uppable, bonus skill
  2291. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  2292. }
  2293. }
  2294. }
  2295. //Checks if you can learn a new skill after having leveled up a skill.
  2296. static void pc_check_skilltree(map_session_data *sd)
  2297. {
  2298. int flag = 0;
  2299. if (battle_config.skillfree)
  2300. return; //Function serves no purpose if this is set
  2301. int c = pc_mapid2jobid( pc_calc_skilltree_normalize_job( sd ), sd->status.sex );
  2302. if (c == -1) { //Unable to normalize job??
  2303. 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);
  2304. return;
  2305. }
  2306. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(c);
  2307. if (tree == nullptr || tree->skills.empty())
  2308. return;
  2309. do {
  2310. flag = 0;
  2311. for (const auto &skillsit : tree->skills) {
  2312. uint16 skid = skillsit.first;
  2313. uint16 sk_idx = skill_get_index(skid);
  2314. bool fail = false;
  2315. if (sd->status.skill[sk_idx].id) //Already learned
  2316. continue;
  2317. // Checking required skills
  2318. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2319. if (entry != nullptr && !entry->need.empty()) {
  2320. for (const auto &it : entry->need) {
  2321. uint16 sk_need_id = it.first;
  2322. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2323. if (sk_need_id > 0) {
  2324. short sk_need = sk_need_id;
  2325. 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)
  2326. sk_need = 0; //Not learned.
  2327. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  2328. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2329. else
  2330. sk_need = pc_checkskill(sd,sk_need_id);
  2331. if (sk_need < it.second) {
  2332. fail = true;
  2333. break;
  2334. }
  2335. }
  2336. }
  2337. }
  2338. if( fail )
  2339. continue;
  2340. if (sd->status.base_level < entry->baselv || sd->status.job_level < entry->joblv)
  2341. continue;
  2342. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2343. if( !sd->status.skill[sk_idx].lv && (
  2344. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2345. skill->inf2[INF2_ISWEDDING] ||
  2346. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2347. ) )
  2348. continue; //Cannot be learned via normal means.
  2349. sd->status.skill[sk_idx].id = skid;
  2350. flag = 1;
  2351. }
  2352. } while(flag);
  2353. }
  2354. // Make sure all the skills are in the correct condition
  2355. // before persisting to the backend.. [MouseJstr]
  2356. void pc_clean_skilltree(map_session_data *sd)
  2357. {
  2358. uint16 i;
  2359. for (i = 0; i < MAX_SKILL; i++){
  2360. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  2361. sd->status.skill[i].id = 0;
  2362. sd->status.skill[i].lv = 0;
  2363. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2364. }
  2365. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  2366. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  2367. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2368. }
  2369. }
  2370. }
  2371. uint64 pc_calc_skilltree_normalize_job_sub( map_session_data *sd ){
  2372. int skill_point = pc_calc_skillpoint( sd );
  2373. if( sd->class_ & MAPID_SUMMONER ){
  2374. // Novice's skill points for basic skill.
  2375. std::shared_ptr<s_job_info> summoner_job = job_db.find( JOB_SUMMONER );
  2376. int summoner_skills = summoner_job->max_job_level - 1;
  2377. if( skill_point < summoner_skills ){
  2378. return MAPID_SUMMONER;
  2379. }
  2380. skill_point -= summoner_skills;
  2381. }else{
  2382. // Novice's skill points for basic skill.
  2383. std::shared_ptr<s_job_info> novice_job = job_db.find( JOB_NOVICE );
  2384. int novice_skills = novice_job->max_job_level - 1;
  2385. if( skill_point < novice_skills ){
  2386. return MAPID_NOVICE;
  2387. }
  2388. skill_point -= novice_skills;
  2389. }
  2390. // 1st Class Job LV Check
  2391. if( sd->class_ & JOBL_2 && ( sd->class_ & MAPID_UPPERMASK ) != MAPID_SUPER_NOVICE ){
  2392. uint64 mapid_1st = sd->class_ & MAPID_BASEMASK;
  2393. int class_1st = pc_mapid2jobid( mapid_1st, sd->status.sex );
  2394. if( !sd->change_level_2nd ){
  2395. if( class_1st >= JOB_NOVICE ){
  2396. sd->change_level_2nd = job_db.find( class_1st )->max_job_level;
  2397. }else{
  2398. sd->change_level_2nd = 0;
  2399. }
  2400. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  2401. }
  2402. if( class_1st > 0 && skill_point < ( sd->change_level_2nd - 1 ) ){
  2403. return mapid_1st;
  2404. }
  2405. skill_point -= ( sd->change_level_2nd - 1 );
  2406. }
  2407. // 2nd Class Job LV Check
  2408. if( sd->class_ & JOBL_THIRD && (sd->class_ & MAPID_THIRDMASK) != MAPID_SUPER_NOVICE_E ){
  2409. uint64 mapid_2nd = sd->class_ & MAPID_UPPERMASK;
  2410. int class_2nd = pc_mapid2jobid( mapid_2nd, sd->status.sex );
  2411. if( !sd->change_level_3rd ){
  2412. if( class_2nd >= JOB_NOVICE ){
  2413. sd->change_level_3rd = job_db.find( class_2nd )->max_job_level;
  2414. }else{
  2415. sd->change_level_3rd = 0;
  2416. }
  2417. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  2418. }
  2419. if( class_2nd > 0 && skill_point < ( sd->change_level_3rd - 1 ) ){
  2420. return mapid_2nd;
  2421. }
  2422. skill_point -= ( sd->change_level_3rd - 1 );
  2423. }
  2424. // 3rd Class Job LV Check
  2425. if( sd->class_ & JOBL_FOURTH ){
  2426. uint64 mapid_3rd = sd->class_ & MAPID_THIRDMASK | JOBL_THIRD;
  2427. int class_3rd = pc_mapid2jobid( mapid_3rd, sd->status.sex );
  2428. if( !sd->change_level_4th ){
  2429. if( class_3rd >= JOB_NOVICE ){
  2430. sd->change_level_4th = job_db.find( class_3rd )->max_job_level;
  2431. }else{
  2432. sd->change_level_4th = 0;
  2433. }
  2434. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ), sd->change_level_4th );
  2435. }
  2436. if( class_3rd > 0 && skill_point < ( sd->change_level_4th - 1 ) ){
  2437. return mapid_3rd;
  2438. }
  2439. skill_point -= ( sd->change_level_4th - 1 );
  2440. }
  2441. return sd->class_;
  2442. }
  2443. uint64 pc_calc_skilltree_normalize_job( map_session_data *sd ){
  2444. if( !battle_config.skillup_limit || pc_has_permission( sd, PC_PERM_ALL_SKILL ) ){
  2445. return sd->class_;
  2446. }
  2447. uint64 c = pc_calc_skilltree_normalize_job_sub( sd );
  2448. // Special Masks
  2449. if( sd->class_&JOBL_UPPER ){
  2450. if( pc_mapid2jobid( c | JOBL_UPPER, sd->status.sex ) >= JOB_NOVICE ){
  2451. c |= JOBL_UPPER;// Rebirth Job
  2452. }
  2453. }
  2454. if( sd->class_&JOBL_BABY ){
  2455. if( pc_mapid2jobid( c | JOBL_BABY, sd->status.sex ) >= JOB_NOVICE ){
  2456. c |= JOBL_BABY;// Baby Job
  2457. }
  2458. }
  2459. return c;
  2460. }
  2461. /*==========================================
  2462. * Updates the weight status
  2463. *------------------------------------------
  2464. * 1: overweight 50% for pre-renewal and 70% for renewal
  2465. * 2: overweight 90%
  2466. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  2467. */
  2468. void pc_updateweightstatus(map_session_data *sd)
  2469. {
  2470. int old_overweight;
  2471. int new_overweight;
  2472. nullpo_retv(sd);
  2473. old_overweight = (sd->sc.getSCE(SC_WEIGHT90)) ? 2 : (sd->sc.getSCE(SC_WEIGHT50)) ? 1 : 0;
  2474. #ifdef RENEWAL
  2475. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  2476. #else
  2477. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  2478. #endif
  2479. if( old_overweight == new_overweight )
  2480. return; // no change
  2481. // stop old status change
  2482. if( old_overweight == 1 )
  2483. status_change_end(&sd->bl, SC_WEIGHT50);
  2484. else if( old_overweight == 2 )
  2485. status_change_end(&sd->bl, SC_WEIGHT90);
  2486. // start new status change
  2487. if( new_overweight == 1 )
  2488. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  2489. else if( new_overweight == 2 )
  2490. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  2491. // update overweight status
  2492. sd->regen.state.overweight = new_overweight;
  2493. }
  2494. int pc_disguise(map_session_data *sd, int class_)
  2495. {
  2496. if (!class_ && !sd->disguise)
  2497. return 0;
  2498. if (class_ && sd->disguise == class_)
  2499. return 0;
  2500. if(pc_isinvisible(sd))
  2501. { //Character is invisible. Stealth class-change. [Skotlex]
  2502. sd->disguise = class_; //viewdata is set on uncloaking.
  2503. return 2;
  2504. }
  2505. if (sd->bl.prev != NULL) {
  2506. pc_stop_walking(sd, 0);
  2507. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  2508. }
  2509. if (!class_) {
  2510. sd->disguise = 0;
  2511. class_ = sd->status.class_;
  2512. } else
  2513. sd->disguise=class_;
  2514. status_set_viewdata(&sd->bl, class_);
  2515. clif_changeoption(&sd->bl);
  2516. if (sd->bl.prev != NULL) {
  2517. clif_spawn(&sd->bl);
  2518. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2519. { //It seems the cart info is lost on undisguise.
  2520. clif_cartlist(sd);
  2521. clif_updatestatus(sd,SP_CARTINFO);
  2522. }
  2523. if (sd->chatID) {
  2524. struct chat_data* cd;
  2525. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  2526. clif_dispchat(cd,0);
  2527. }
  2528. }
  2529. return 1;
  2530. }
  2531. /// Show error message
  2532. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2533. /// Check for valid Element, break & show error message if invalid Element
  2534. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2535. /// Check for valid Race, break & show error message if invalid Race
  2536. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2537. /// Check for valid Race2, break & show error message if invalid Race2
  2538. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2539. /// Check for valid Class, break & show error message if invalid Class
  2540. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2541. /// Check for valid Size, break & show error message if invalid Size
  2542. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2543. /// Check for valid SC, break & show error message if invalid SC
  2544. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2545. /**
  2546. * Add auto spell bonus for player while attacking/attacked
  2547. * @param spell: Spell array
  2548. * @param id: Skill to cast
  2549. * @param lv: Skill level
  2550. * @param rate: Success chance
  2551. * @param battle_flag: Battle flag
  2552. * @param card_id: Used to prevent card stacking
  2553. * @param flag: Flags used for extra arguments
  2554. * &0: forces the skill to be casted on self, rather than on the target
  2555. * &1: forces the skill to be casted on target, rather than self
  2556. * &2: random skill level in [1..lv] is chosen
  2557. */
  2558. 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)
  2559. {
  2560. if (spell.size() == MAX_PC_BONUS) {
  2561. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2562. return;
  2563. }
  2564. if (!rate)
  2565. return;
  2566. if (!(battle_flag&BF_RANGEMASK))
  2567. battle_flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2568. if (!(battle_flag&BF_WEAPONMASK))
  2569. battle_flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2570. if (!(battle_flag&BF_SKILLMASK)) {
  2571. if (battle_flag&(BF_MAGIC | BF_MISC))
  2572. battle_flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2573. if (battle_flag&BF_WEAPON)
  2574. battle_flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2575. }
  2576. for (auto &it : spell) {
  2577. 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) {
  2578. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2579. return;
  2580. it.rate = util::safe_addition_cap(it.rate, rate, (short)1000);
  2581. return;
  2582. }
  2583. }
  2584. struct s_autospell entry = {};
  2585. entry.id = id;
  2586. entry.lv = lv;
  2587. entry.rate = cap_value(rate, -1000, 1000);
  2588. entry.battle_flag = battle_flag;
  2589. entry.card_id = card_id;
  2590. entry.flag = flag;
  2591. spell.push_back(entry);
  2592. }
  2593. /**
  2594. * Add auto spell bonus for player while using skills
  2595. * @param spell: Spell array
  2596. * @param src_skill: Trigger skill
  2597. * @param id: Support or target type
  2598. * @param lv: Skill level
  2599. * @param rate: Success chance
  2600. * @param card_id: Used to prevent card stacking
  2601. * @param flag: Flags used for extra arguments
  2602. * &1: forces the skill to be casted on self, rather than on the target of skill
  2603. * &2: random skill level in [1..lv] is chosen
  2604. */
  2605. 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)
  2606. {
  2607. if (spell.size() == MAX_PC_BONUS) {
  2608. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2609. return;
  2610. }
  2611. if (!rate)
  2612. return;
  2613. struct s_autospell entry = {};
  2614. entry.trigger_skill = src_skill;
  2615. entry.id = id;
  2616. entry.lv = lv;
  2617. entry.rate = cap_value(rate, -1000, 1000);
  2618. entry.card_id = card_id;
  2619. entry.flag = flag;
  2620. spell.push_back(entry);
  2621. }
  2622. /**
  2623. * Add inflict effect bonus for player while attacking/attacked
  2624. * @param effect: Effect array
  2625. * @param sc: SC/Effect type
  2626. * @param rate: Success chance
  2627. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2628. * @param flag: Target flag
  2629. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2630. */
  2631. 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)
  2632. {
  2633. if (effect.size() == MAX_PC_BONUS) {
  2634. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2635. return;
  2636. }
  2637. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2638. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2639. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2640. flag |= ATF_TARGET; //Default target: enemy.
  2641. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2642. flag |= ATF_WEAPON; //Default type: weapon.
  2643. if (!duration)
  2644. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2645. for (auto &it : effect) {
  2646. if (it.sc == sc && it.flag == flag) {
  2647. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2648. it.arrow_rate = util::safe_addition_cap(it.arrow_rate, arrow_rate, (short)SHRT_MAX);
  2649. it.duration = umax(it.duration, duration);
  2650. return;
  2651. }
  2652. }
  2653. struct s_addeffect entry = {};
  2654. entry.sc = sc;
  2655. entry.rate = rate;
  2656. entry.arrow_rate = arrow_rate;
  2657. entry.flag = flag;
  2658. entry.duration = duration;
  2659. effect.push_back(entry);
  2660. }
  2661. /**
  2662. * Add inflict effect bonus for player while attacking using skill
  2663. * @param effect: Effect array
  2664. * @param sc: SC/Effect type
  2665. * @param rate: Success chance
  2666. * @param skill_id: Skill to cast
  2667. * @param target: Target type
  2668. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2669. */
  2670. 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)
  2671. {
  2672. if (effect.size() == MAX_PC_BONUS) {
  2673. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2674. return;
  2675. }
  2676. if (!duration)
  2677. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2678. for (auto &it : effect) {
  2679. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2680. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2681. it.duration = umax(it.duration, duration);
  2682. return;
  2683. }
  2684. }
  2685. struct s_addeffectonskill entry = {};
  2686. entry.sc = sc;
  2687. entry.rate = rate;
  2688. entry.skill_id = skill_id;
  2689. entry.target = target;
  2690. entry.duration = duration;
  2691. effect.push_back(entry);
  2692. }
  2693. /**
  2694. * Adjust/add drop rate modifier for player
  2695. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2696. * @param nameid: item id that will be dropped
  2697. * @param group: group id
  2698. * @param class_: target class
  2699. * @param race: target race. if < 0, means monster_id
  2700. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2701. */
  2702. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int class_, short race, int rate)
  2703. {
  2704. if (!nameid && !group) {
  2705. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2706. return;
  2707. }
  2708. if (nameid && !item_db.exists(nameid)) {
  2709. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2710. return;
  2711. }
  2712. if (group && !itemdb_group.exists(group)) {
  2713. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2714. return;
  2715. }
  2716. if (drop.size() == MAX_PC_BONUS) {
  2717. 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);
  2718. return;
  2719. }
  2720. //Apply config rate adjustment settings.
  2721. if (rate >= 0) { //Absolute drop.
  2722. if (battle_config.item_rate_adddrop != 100)
  2723. rate = rate*battle_config.item_rate_adddrop/100;
  2724. if (rate < battle_config.item_drop_adddrop_min)
  2725. rate = battle_config.item_drop_adddrop_min;
  2726. else if (rate > battle_config.item_drop_adddrop_max)
  2727. rate = battle_config.item_drop_adddrop_max;
  2728. } else { //Relative drop, max/min limits are applied at drop time.
  2729. if (battle_config.item_rate_adddrop != 100)
  2730. rate = rate*battle_config.item_rate_adddrop/100;
  2731. if (rate > -1)
  2732. rate = -1;
  2733. }
  2734. for (auto &it : drop) {
  2735. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2736. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2737. it.rate = util::safe_addition_cap(it.rate, rate, 10000);
  2738. return;
  2739. }
  2740. }
  2741. struct s_add_drop entry = {};
  2742. entry.nameid = nameid;
  2743. entry.group = group;
  2744. entry.race = race;
  2745. entry.class_ = class_;
  2746. entry.rate = cap_value(rate, -10000, 10000);
  2747. drop.push_back(entry);
  2748. }
  2749. s_autobonus::~s_autobonus(){
  2750. if( this->active != INVALID_TIMER ){
  2751. delete_timer( this->active, pc_endautobonus );
  2752. this->active = INVALID_TIMER;
  2753. }
  2754. if( this->bonus_script != nullptr ){
  2755. aFree( this->bonus_script );
  2756. this->bonus_script = nullptr;
  2757. }
  2758. if( this->other_script != nullptr ){
  2759. aFree( this->other_script );
  2760. this->other_script = nullptr;
  2761. }
  2762. }
  2763. /**
  2764. * Add autobonus to player when attacking/attacked
  2765. * @param bonus: Bonus array
  2766. * @param script: Script to execute
  2767. * @param rate: Success chance
  2768. * @param dur: Duration
  2769. * @param flag: Battle flag/skill
  2770. * @param other_script: Secondary script to execute
  2771. * @param pos: Item equip position
  2772. * @param onskill: Skill used to trigger autobonus
  2773. * @return True on success or false otherwise
  2774. */
  2775. 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){
  2776. // Check if the same bonus already exists
  2777. for( std::shared_ptr<s_autobonus> autobonus : bonus ){
  2778. // Compare based on position and bonus script
  2779. if( autobonus->pos == pos && strcmp( script, autobonus->bonus_script ) == 0 ){
  2780. return false;
  2781. }
  2782. }
  2783. if (bonus.size() == MAX_PC_BONUS) {
  2784. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2785. return false;
  2786. }
  2787. if (!onskill) {
  2788. if (!(flag&BF_RANGEMASK))
  2789. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2790. if (!(flag&BF_WEAPONMASK))
  2791. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2792. if (!(flag&BF_SKILLMASK)) {
  2793. if (flag&(BF_MAGIC | BF_MISC))
  2794. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2795. if (flag&BF_WEAPON)
  2796. flag |= BF_NORMAL | BF_SKILL;
  2797. }
  2798. }
  2799. std::shared_ptr<s_autobonus> entry = std::make_shared<s_autobonus>();
  2800. if (rate < -10000 || rate > 10000)
  2801. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2802. entry->rate = cap_value(rate, -10000, 10000);
  2803. entry->duration = dur;
  2804. entry->active = INVALID_TIMER;
  2805. entry->atk_type = flag;
  2806. entry->pos = pos;
  2807. entry->bonus_script = aStrdup(script);
  2808. entry->other_script = (other_script ? aStrdup(other_script) : NULL);
  2809. bonus.push_back(entry);
  2810. return true;
  2811. }
  2812. /**
  2813. * Remove an autobonus from player
  2814. * @param sd: Player data
  2815. * @param bonus: Autobonus array
  2816. * @param restore: Run script on clearing or not
  2817. */
  2818. void pc_delautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> &bonus, bool restore){
  2819. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  2820. while( it != bonus.end() ){
  2821. std::shared_ptr<s_autobonus> b = *it;
  2822. if( b->active != INVALID_TIMER && restore && b->bonus_script != nullptr ){
  2823. unsigned int equip_pos_idx = 0;
  2824. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2825. for (uint8 j = 0; j < EQI_MAX; j++) {
  2826. if (sd.equip_index[j] >= 0)
  2827. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2828. }
  2829. if( ( equip_pos_idx&b->pos ) == b->pos ){
  2830. script_run_autobonus(b->bonus_script, &sd, b->pos);
  2831. }else{
  2832. // Not all required items equipped anymore
  2833. restore = false;
  2834. }
  2835. }
  2836. if( restore ){
  2837. it++;
  2838. continue;
  2839. }
  2840. it = bonus.erase(it);
  2841. }
  2842. }
  2843. /**
  2844. * Execute autobonus on player
  2845. * @param sd: Player data
  2846. * @param autobonus: Autobonus to run
  2847. */
  2848. void pc_exeautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> *bonus, std::shared_ptr<s_autobonus> autobonus)
  2849. {
  2850. if (autobonus->active != INVALID_TIMER)
  2851. delete_timer(autobonus->active, pc_endautobonus);
  2852. if( autobonus->other_script )
  2853. {
  2854. int j;
  2855. unsigned int equip_pos_idx = 0;
  2856. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2857. for(j = 0; j < EQI_MAX; j++) {
  2858. if(sd.equip_index[j] >= 0)
  2859. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2860. }
  2861. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2862. script_run_autobonus(autobonus->other_script,&sd,autobonus->pos);
  2863. }
  2864. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd.bl.id, (intptr_t)bonus);
  2865. status_calc_pc(&sd,SCO_FORCE);
  2866. }
  2867. /**
  2868. * Remove autobonus timer from player
  2869. */
  2870. TIMER_FUNC(pc_endautobonus){
  2871. map_session_data *sd = map_id2sd(id);
  2872. std::vector<std::shared_ptr<s_autobonus>> *bonus = (std::vector<std::shared_ptr<s_autobonus>> *)data;
  2873. nullpo_ret(sd);
  2874. nullpo_ret(bonus);
  2875. for( std::shared_ptr<s_autobonus> autobonus : *bonus ){
  2876. if( autobonus->active == tid ){
  2877. autobonus->active = INVALID_TIMER;
  2878. break;
  2879. }
  2880. }
  2881. status_calc_pc(sd,SCO_FORCE);
  2882. return 0;
  2883. }
  2884. /**
  2885. * Add element bonus to player when attacking
  2886. * @param sd: Player data
  2887. * @param ele: Element to adjust
  2888. * @param rate: Success chance
  2889. * @param flag: Battle flag
  2890. */
  2891. static void pc_bonus_addele(map_session_data* sd, unsigned char ele, short rate, short flag)
  2892. {
  2893. nullpo_retv(sd);
  2894. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2895. if (wd->addele2.size() == MAX_PC_BONUS) {
  2896. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2897. return;
  2898. }
  2899. if (!(flag&BF_RANGEMASK))
  2900. flag |= BF_SHORT | BF_LONG;
  2901. if (!(flag&BF_WEAPONMASK))
  2902. flag |= BF_WEAPON;
  2903. if (!(flag&BF_SKILLMASK)) {
  2904. if (flag&(BF_MAGIC | BF_MISC))
  2905. flag |= BF_SKILL;
  2906. if (flag&BF_WEAPON)
  2907. flag |= BF_NORMAL | BF_SKILL;
  2908. }
  2909. for (auto &it : wd->addele2) {
  2910. if (it.ele == ele && it.flag == flag) {
  2911. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2912. return;
  2913. }
  2914. }
  2915. struct s_addele2 entry = {};
  2916. entry.ele = ele;
  2917. entry.rate = cap_value(rate, -10000, 10000);
  2918. entry.flag = flag;
  2919. wd->addele2.push_back(entry);
  2920. }
  2921. /**
  2922. * Reduce element bonus to player when attacking
  2923. * @param sd: Player data
  2924. * @param ele: Element to adjust
  2925. * @param rate: Success chance
  2926. * @param flag: Battle flag
  2927. */
  2928. static void pc_bonus_subele(map_session_data* sd, unsigned char ele, short rate, short flag)
  2929. {
  2930. nullpo_retv(sd);
  2931. if (sd->subele2.size() == MAX_PC_BONUS) {
  2932. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2933. return;
  2934. }
  2935. if (!(flag&BF_RANGEMASK))
  2936. flag |= BF_SHORT | BF_LONG;
  2937. if (!(flag&BF_WEAPONMASK))
  2938. flag |= BF_WEAPON;
  2939. if (!(flag&BF_SKILLMASK)) {
  2940. if (flag&(BF_MAGIC | BF_MISC))
  2941. flag |= BF_SKILL;
  2942. if (flag&BF_WEAPON)
  2943. flag |= BF_NORMAL | BF_SKILL;
  2944. }
  2945. for (auto &it : sd->subele2) {
  2946. if (it.ele == ele && it.flag == flag) {
  2947. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2948. return;
  2949. }
  2950. }
  2951. struct s_addele2 entry = {};
  2952. entry.ele = ele;
  2953. entry.rate = cap_value(rate, -10000, 10000);
  2954. entry.flag = flag;
  2955. sd->subele2.push_back(entry);
  2956. }
  2957. /**
  2958. * Adjust race damage to target when attacking
  2959. * @param sd: Player data
  2960. * @param race: Race to adjust
  2961. * @param rate: Success chance
  2962. * @param flag: Battle flag
  2963. */
  2964. static void pc_bonus_subrace(map_session_data* sd, unsigned char race, short rate, short flag)
  2965. {
  2966. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2967. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2968. return;
  2969. }
  2970. if (!(flag&BF_RANGEMASK))
  2971. flag |= BF_SHORT | BF_LONG;
  2972. if (!(flag&BF_WEAPONMASK))
  2973. flag |= BF_WEAPON;
  2974. if (!(flag&BF_SKILLMASK)) {
  2975. if (flag&(BF_MAGIC | BF_MISC))
  2976. flag |= BF_SKILL;
  2977. if (flag&BF_WEAPON)
  2978. flag |= BF_NORMAL | BF_SKILL;
  2979. }
  2980. for (auto &it : sd->subrace3) {
  2981. if (it.race == race && it.flag == flag) {
  2982. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2983. return;
  2984. }
  2985. }
  2986. struct s_addrace2 entry = {};
  2987. entry.race = race;
  2988. entry.rate = cap_value(rate, -10000, 10000);
  2989. entry.flag = flag;
  2990. sd->subrace3.push_back(entry);
  2991. }
  2992. /**
  2993. * General item bonus for player
  2994. * @param bonus: Bonus array
  2995. * @param id: Key
  2996. * @param val: Value
  2997. * @param cap_rate: If Value is a rate value that needs to be capped
  2998. */
  2999. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  3000. {
  3001. for (auto &it : bonus) {
  3002. if (it.id == id) {
  3003. if (cap_rate)
  3004. it.val = util::safe_addition_cap(it.val, val, 10000);
  3005. else
  3006. it.val += val;
  3007. return;
  3008. }
  3009. }
  3010. struct s_item_bonus entry = {};
  3011. entry.id = id;
  3012. entry.val = cap_rate ? cap_value(val, -10000, 10000) : val;
  3013. bonus.push_back(entry);
  3014. }
  3015. /**
  3016. * Remove HP/SP to player when attacking
  3017. * @param bonus: Bonus array
  3018. * @param rate: Success chance
  3019. * @param per: Percentage of HP/SP to vanish
  3020. * @param flag: Battle flag
  3021. */
  3022. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  3023. if (bonus.size() == MAX_PC_BONUS) {
  3024. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  3025. return;
  3026. }
  3027. if (!(flag&BF_RANGEMASK))
  3028. flag |= BF_SHORT | BF_LONG;
  3029. if (!(flag&BF_WEAPONMASK))
  3030. flag |= BF_WEAPON;
  3031. if (!(flag&BF_SKILLMASK)) {
  3032. if (flag&(BF_MAGIC | BF_MISC))
  3033. flag |= BF_SKILL;
  3034. if (flag&BF_WEAPON)
  3035. flag |= BF_NORMAL | BF_SKILL;
  3036. }
  3037. for (auto &it : bonus) {
  3038. if (it.flag == flag) {
  3039. it.rate = util::safe_addition_cap(it.rate, rate, (int16)10000);
  3040. it.per += per;
  3041. return;
  3042. }
  3043. }
  3044. struct s_vanish_bonus entry = {};
  3045. entry.rate = cap_value(rate, -10000, 10000);
  3046. entry.per = per;
  3047. entry.flag = flag;
  3048. bonus.push_back(entry);
  3049. }
  3050. /*==========================================
  3051. * Add a bonus(type) to player sd
  3052. * format: bonus bBonusName,val;
  3053. * @param sd
  3054. * @param type Bonus type used by bBonusName
  3055. * @param val Value that usually for rate or fixed value
  3056. *------------------------------------------*/
  3057. void pc_bonus(map_session_data *sd,int type,int val)
  3058. {
  3059. struct status_data *status;
  3060. int bonus;
  3061. nullpo_retv(sd);
  3062. status = &sd->base_status;
  3063. switch(type){
  3064. case SP_STR:
  3065. case SP_AGI:
  3066. case SP_VIT:
  3067. case SP_INT:
  3068. case SP_DEX:
  3069. case SP_LUK:
  3070. if(sd->state.lr_flag != 2)
  3071. sd->indexed_bonus.param_bonus[type-SP_STR]+=val;
  3072. break;
  3073. case SP_POW:
  3074. case SP_STA:
  3075. case SP_WIS:
  3076. case SP_SPL:
  3077. case SP_CON:
  3078. case SP_CRT:
  3079. if (sd->state.lr_flag != 2)
  3080. sd->indexed_bonus.param_bonus[type - SP_POW + PARAM_POW] += val;
  3081. break;
  3082. case SP_ATK1:
  3083. if(!sd->state.lr_flag) {
  3084. bonus = status->rhw.atk + val;
  3085. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  3086. }
  3087. else if(sd->state.lr_flag == 1) {
  3088. bonus = status->lhw.atk + val;
  3089. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  3090. }
  3091. break;
  3092. case SP_ATK2:
  3093. if(!sd->state.lr_flag) {
  3094. bonus = status->rhw.atk2 + val;
  3095. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  3096. }
  3097. else if(sd->state.lr_flag == 1) {
  3098. bonus = status->lhw.atk2 + val;
  3099. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  3100. }
  3101. break;
  3102. case SP_BASE_ATK:
  3103. if(sd->state.lr_flag != 2) {
  3104. #ifdef RENEWAL
  3105. bonus = sd->bonus.eatk + val;
  3106. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3107. #else
  3108. bonus = status->batk + val;
  3109. status->batk = cap_value(bonus, 0, USHRT_MAX);
  3110. #endif
  3111. }
  3112. break;
  3113. case SP_DEF1:
  3114. if(sd->state.lr_flag != 2) {
  3115. bonus = status->def + val;
  3116. #ifdef RENEWAL
  3117. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3118. #else
  3119. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3120. #endif
  3121. }
  3122. break;
  3123. case SP_DEF2:
  3124. if(sd->state.lr_flag != 2) {
  3125. bonus = status->def2 + val;
  3126. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3127. }
  3128. break;
  3129. case SP_MDEF1:
  3130. if(sd->state.lr_flag != 2) {
  3131. bonus = status->mdef + val;
  3132. #ifdef RENEWAL
  3133. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3134. #else
  3135. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3136. #endif
  3137. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  3138. sd->bonus.shieldmdef += bonus;
  3139. }
  3140. }
  3141. break;
  3142. case SP_MDEF2:
  3143. if(sd->state.lr_flag != 2) {
  3144. bonus = status->mdef2 + val;
  3145. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3146. }
  3147. break;
  3148. case SP_HIT:
  3149. if(sd->state.lr_flag != 2) {
  3150. bonus = status->hit + val;
  3151. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3152. } else
  3153. sd->bonus.arrow_hit+=val;
  3154. break;
  3155. case SP_FLEE1:
  3156. if(sd->state.lr_flag != 2) {
  3157. bonus = status->flee + val;
  3158. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3159. }
  3160. break;
  3161. case SP_FLEE2:
  3162. if(sd->state.lr_flag != 2) {
  3163. bonus = status->flee2 + val*10;
  3164. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3165. }
  3166. break;
  3167. case SP_CRITICAL:
  3168. if(sd->state.lr_flag != 2) {
  3169. bonus = status->cri + val*10;
  3170. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3171. } else
  3172. sd->bonus.arrow_cri += val*10;
  3173. break;
  3174. case SP_PATK:
  3175. if (sd->state.lr_flag != 2) {
  3176. bonus = status->patk + val;
  3177. status->patk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3178. }
  3179. break;
  3180. case SP_SMATK:
  3181. if (sd->state.lr_flag != 2) {
  3182. bonus = status->smatk + val;
  3183. status->smatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3184. }
  3185. break;
  3186. case SP_RES:
  3187. if (sd->state.lr_flag != 2) {
  3188. bonus = status->res + val;
  3189. status->res = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3190. }
  3191. break;
  3192. case SP_MRES:
  3193. if (sd->state.lr_flag != 2) {
  3194. bonus = status->mres + val;
  3195. status->mres = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3196. }
  3197. break;
  3198. case SP_HPLUS:
  3199. if (sd->state.lr_flag != 2) {
  3200. bonus = status->hplus + val;
  3201. status->hplus = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3202. }
  3203. break;
  3204. case SP_CRATE:
  3205. if (sd->state.lr_flag != 2) {
  3206. bonus = status->crate + val;
  3207. status->crate = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3208. }
  3209. break;
  3210. case SP_ATKELE:
  3211. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  3212. switch (sd->state.lr_flag)
  3213. {
  3214. case 2:
  3215. switch (sd->status.weapon) {
  3216. case W_BOW:
  3217. case W_REVOLVER:
  3218. case W_RIFLE:
  3219. case W_GATLING:
  3220. case W_SHOTGUN:
  3221. case W_GRENADE:
  3222. //Become weapon element.
  3223. status->rhw.ele=val;
  3224. break;
  3225. default: //Become arrow element.
  3226. sd->bonus.arrow_ele=val;
  3227. break;
  3228. }
  3229. break;
  3230. case 1:
  3231. status->lhw.ele=val;
  3232. break;
  3233. default:
  3234. status->rhw.ele=val;
  3235. break;
  3236. }
  3237. break;
  3238. case SP_DEFELE:
  3239. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  3240. if(sd->state.lr_flag != 2)
  3241. status->def_ele=val;
  3242. break;
  3243. case SP_MAXHP:
  3244. if(sd->state.lr_flag == 2)
  3245. break;
  3246. sd->bonus.hp += val;
  3247. break;
  3248. case SP_MAXSP:
  3249. if(sd->state.lr_flag == 2)
  3250. break;
  3251. sd->bonus.sp += val;
  3252. break;
  3253. case SP_MAXAP:
  3254. if (sd->state.lr_flag == 2)
  3255. break;
  3256. sd->bonus.ap += val;
  3257. break;
  3258. case SP_MAXHPRATE:
  3259. if(sd->state.lr_flag != 2)
  3260. sd->hprate+=val;
  3261. break;
  3262. case SP_MAXSPRATE:
  3263. if(sd->state.lr_flag != 2)
  3264. sd->sprate+=val;
  3265. break;
  3266. case SP_MAXAPRATE:
  3267. if (sd->state.lr_flag != 2)
  3268. sd->aprate += val;
  3269. break;
  3270. case SP_SPRATE:
  3271. if(sd->state.lr_flag != 2)
  3272. sd->dsprate+=val;
  3273. break;
  3274. case SP_ATTACKRANGE:
  3275. switch (sd->state.lr_flag) {
  3276. case 2:
  3277. switch (sd->status.weapon) {
  3278. case W_BOW:
  3279. case W_REVOLVER:
  3280. case W_RIFLE:
  3281. case W_GATLING:
  3282. case W_SHOTGUN:
  3283. case W_GRENADE:
  3284. status->rhw.range += val;
  3285. }
  3286. break;
  3287. case 1:
  3288. status->lhw.range += val;
  3289. break;
  3290. default:
  3291. status->rhw.range += val;
  3292. break;
  3293. }
  3294. break;
  3295. case SP_SPEED_RATE: //Non stackable increase
  3296. if(sd->state.lr_flag != 2)
  3297. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  3298. break;
  3299. case SP_SPEED_ADDRATE: //Stackable increase
  3300. if(sd->state.lr_flag != 2)
  3301. sd->bonus.speed_add_rate -= val;
  3302. break;
  3303. case SP_ASPD: //Raw increase
  3304. if(sd->state.lr_flag != 2)
  3305. sd->bonus.aspd_add -= 10*val;
  3306. break;
  3307. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  3308. if(sd->state.lr_flag != 2)
  3309. #ifndef RENEWAL_ASPD
  3310. status->aspd_rate -= 10*val;
  3311. #else
  3312. status->aspd_rate2 += val;
  3313. #endif
  3314. break;
  3315. case SP_HP_RECOV_RATE:
  3316. if(sd->state.lr_flag != 2)
  3317. sd->hprecov_rate += val;
  3318. break;
  3319. case SP_SP_RECOV_RATE:
  3320. if(sd->state.lr_flag != 2)
  3321. sd->sprecov_rate += val;
  3322. break;
  3323. case SP_CRITICAL_DEF:
  3324. if(sd->state.lr_flag != 2)
  3325. sd->bonus.critical_def += val;
  3326. break;
  3327. case SP_NEAR_ATK_DEF:
  3328. if(sd->state.lr_flag != 2)
  3329. sd->bonus.near_attack_def_rate += val;
  3330. break;
  3331. case SP_LONG_ATK_DEF:
  3332. if(sd->state.lr_flag != 2)
  3333. sd->bonus.long_attack_def_rate += val;
  3334. break;
  3335. case SP_DOUBLE_RATE:
  3336. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  3337. sd->bonus.double_rate = val;
  3338. break;
  3339. case SP_DOUBLE_ADD_RATE:
  3340. if(sd->state.lr_flag == 0)
  3341. sd->bonus.double_add_rate += val;
  3342. break;
  3343. case SP_MATK_RATE:
  3344. if(sd->state.lr_flag != 2)
  3345. sd->matk_rate += val;
  3346. break;
  3347. case SP_IGNORE_DEF_ELE:
  3348. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  3349. if(!sd->state.lr_flag)
  3350. sd->right_weapon.ignore_def_ele |= 1<<val;
  3351. else if(sd->state.lr_flag == 1)
  3352. sd->left_weapon.ignore_def_ele |= 1<<val;
  3353. break;
  3354. case SP_IGNORE_DEF_RACE:
  3355. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  3356. if(!sd->state.lr_flag)
  3357. sd->right_weapon.ignore_def_race |= 1<<val;
  3358. else if(sd->state.lr_flag == 1)
  3359. sd->left_weapon.ignore_def_race |= 1<<val;
  3360. break;
  3361. case SP_IGNORE_DEF_CLASS:
  3362. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  3363. if(!sd->state.lr_flag)
  3364. sd->right_weapon.ignore_def_class |= 1<<val;
  3365. else if(sd->state.lr_flag == 1)
  3366. sd->left_weapon.ignore_def_class |= 1<<val;
  3367. break;
  3368. case SP_ATK_RATE:
  3369. if(sd->state.lr_flag != 2)
  3370. sd->bonus.atk_rate += val;
  3371. break;
  3372. case SP_MAGIC_ATK_DEF:
  3373. if(sd->state.lr_flag != 2)
  3374. sd->bonus.magic_def_rate += val;
  3375. break;
  3376. case SP_MISC_ATK_DEF:
  3377. if(sd->state.lr_flag != 2)
  3378. sd->bonus.misc_def_rate += val;
  3379. break;
  3380. case SP_IGNORE_MDEF_ELE:
  3381. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  3382. if(sd->state.lr_flag != 2)
  3383. sd->bonus.ignore_mdef_ele |= 1<<val;
  3384. break;
  3385. case SP_IGNORE_MDEF_RACE:
  3386. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  3387. if(sd->state.lr_flag != 2)
  3388. sd->bonus.ignore_mdef_race |= 1<<val;
  3389. break;
  3390. case SP_PERFECT_HIT_RATE:
  3391. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  3392. sd->bonus.perfect_hit = val;
  3393. break;
  3394. case SP_PERFECT_HIT_ADD_RATE:
  3395. if(sd->state.lr_flag != 2)
  3396. sd->bonus.perfect_hit_add += val;
  3397. break;
  3398. case SP_CRITICAL_RATE:
  3399. if(sd->state.lr_flag != 2)
  3400. sd->critical_rate+=val;
  3401. break;
  3402. case SP_DEF_RATIO_ATK_ELE:
  3403. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  3404. if(!sd->state.lr_flag)
  3405. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  3406. else if(sd->state.lr_flag == 1)
  3407. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  3408. break;
  3409. case SP_DEF_RATIO_ATK_RACE:
  3410. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  3411. if(!sd->state.lr_flag)
  3412. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  3413. else if(sd->state.lr_flag == 1)
  3414. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  3415. break;
  3416. case SP_DEF_RATIO_ATK_CLASS:
  3417. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  3418. if(!sd->state.lr_flag)
  3419. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  3420. else if(sd->state.lr_flag == 1)
  3421. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  3422. break;
  3423. case SP_HIT_RATE:
  3424. if(sd->state.lr_flag != 2)
  3425. sd->hit_rate += val;
  3426. break;
  3427. case SP_FLEE_RATE:
  3428. if(sd->state.lr_flag != 2)
  3429. sd->flee_rate += val;
  3430. break;
  3431. case SP_FLEE2_RATE:
  3432. if(sd->state.lr_flag != 2)
  3433. sd->flee2_rate += val;
  3434. break;
  3435. case SP_DEF_RATE:
  3436. if(sd->state.lr_flag != 2)
  3437. sd->def_rate += val;
  3438. break;
  3439. case SP_DEF2_RATE:
  3440. if(sd->state.lr_flag != 2)
  3441. sd->def2_rate += val;
  3442. break;
  3443. case SP_MDEF_RATE:
  3444. if(sd->state.lr_flag != 2)
  3445. sd->mdef_rate += val;
  3446. break;
  3447. case SP_MDEF2_RATE:
  3448. if(sd->state.lr_flag != 2)
  3449. sd->mdef2_rate += val;
  3450. break;
  3451. case SP_PATK_RATE:
  3452. if (sd->state.lr_flag != 2)
  3453. sd->patk_rate += val;
  3454. break;
  3455. case SP_SMATK_RATE:
  3456. if (sd->state.lr_flag != 2)
  3457. sd->smatk_rate += val;
  3458. break;
  3459. case SP_RES_RATE:
  3460. if (sd->state.lr_flag != 2)
  3461. sd->res_rate += val;
  3462. break;
  3463. case SP_MRES_RATE:
  3464. if (sd->state.lr_flag != 2)
  3465. sd->mres_rate += val;
  3466. break;
  3467. case SP_HPLUS_RATE:
  3468. if (sd->state.lr_flag != 2)
  3469. sd->hplus_rate += val;
  3470. break;
  3471. case SP_CRATE_RATE:
  3472. if (sd->state.lr_flag != 2)
  3473. sd->crate_rate += val;
  3474. break;
  3475. case SP_RESTART_FULL_RECOVER:
  3476. if(sd->state.lr_flag != 2)
  3477. sd->special_state.restart_full_recover = 1;
  3478. break;
  3479. case SP_NO_CASTCANCEL:
  3480. if(sd->state.lr_flag != 2)
  3481. sd->special_state.no_castcancel = 1;
  3482. break;
  3483. case SP_NO_CASTCANCEL2:
  3484. if(sd->state.lr_flag != 2)
  3485. sd->special_state.no_castcancel2 = 1;
  3486. break;
  3487. case SP_NO_SIZEFIX:
  3488. if(sd->state.lr_flag != 2)
  3489. sd->special_state.no_sizefix = 1;
  3490. break;
  3491. case SP_NO_MAGIC_DAMAGE:
  3492. if(sd->state.lr_flag == 2)
  3493. break;
  3494. val+= sd->special_state.no_magic_damage;
  3495. sd->special_state.no_magic_damage = cap_value(val,0,100);
  3496. break;
  3497. case SP_NO_WEAPON_DAMAGE:
  3498. if(sd->state.lr_flag == 2)
  3499. break;
  3500. val+= sd->special_state.no_weapon_damage;
  3501. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  3502. break;
  3503. case SP_NO_MISC_DAMAGE:
  3504. if(sd->state.lr_flag == 2)
  3505. break;
  3506. val+= sd->special_state.no_misc_damage;
  3507. sd->special_state.no_misc_damage = cap_value(val,0,100);
  3508. break;
  3509. case SP_NO_GEMSTONE:
  3510. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  3511. sd->special_state.no_gemstone = 1;
  3512. break;
  3513. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  3514. if(sd->state.lr_flag != 2) {
  3515. sd->special_state.intravision = 1;
  3516. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  3517. }
  3518. break;
  3519. case SP_NO_KNOCKBACK:
  3520. if(sd->state.lr_flag != 2)
  3521. sd->special_state.no_knockback = 1;
  3522. break;
  3523. case SP_SPLASH_RANGE:
  3524. if(sd->bonus.splash_range < val)
  3525. sd->bonus.splash_range = val;
  3526. break;
  3527. case SP_SPLASH_ADD_RANGE:
  3528. sd->bonus.splash_add_range += val;
  3529. break;
  3530. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  3531. if(sd->state.lr_flag != 2)
  3532. sd->bonus.short_weapon_damage_return += val;
  3533. break;
  3534. case SP_LONG_WEAPON_DAMAGE_RETURN:
  3535. if(sd->state.lr_flag != 2)
  3536. sd->bonus.long_weapon_damage_return += val;
  3537. break;
  3538. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  3539. if(sd->state.lr_flag != 2)
  3540. sd->bonus.magic_damage_return += val;
  3541. break;
  3542. case SP_REDUCE_DAMAGE_RETURN:
  3543. if (sd->state.lr_flag != 2)
  3544. sd->bonus.reduce_damage_return += val;
  3545. break;
  3546. case SP_ALL_STATS: // [Valaris]
  3547. if(sd->state.lr_flag!=2) {
  3548. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3549. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3550. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3551. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  3552. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3553. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  3554. }
  3555. break;
  3556. case SP_ALL_TRAIT_STATS:
  3557. if (sd->state.lr_flag != 2) {
  3558. sd->indexed_bonus.param_bonus[PARAM_POW] += val;
  3559. sd->indexed_bonus.param_bonus[PARAM_STA] += val;
  3560. sd->indexed_bonus.param_bonus[PARAM_WIS] += val;
  3561. sd->indexed_bonus.param_bonus[PARAM_SPL] += val;
  3562. sd->indexed_bonus.param_bonus[PARAM_CON] += val;
  3563. sd->indexed_bonus.param_bonus[PARAM_CRT] += val;
  3564. }
  3565. break;
  3566. case SP_AGI_VIT: // [Valaris]
  3567. if(sd->state.lr_flag!=2) {
  3568. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3569. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3570. }
  3571. break;
  3572. case SP_AGI_DEX_STR: // [Valaris]
  3573. if(sd->state.lr_flag!=2) {
  3574. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3575. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3576. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3577. }
  3578. break;
  3579. case SP_PERFECT_HIDE: // [Valaris]
  3580. if(sd->state.lr_flag!=2)
  3581. sd->special_state.perfect_hiding=1;
  3582. break;
  3583. case SP_UNBREAKABLE:
  3584. if(sd->state.lr_flag!=2)
  3585. sd->bonus.unbreakable += val;
  3586. break;
  3587. case SP_UNBREAKABLE_WEAPON:
  3588. if(sd->state.lr_flag != 2)
  3589. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3590. break;
  3591. case SP_UNBREAKABLE_ARMOR:
  3592. if(sd->state.lr_flag != 2)
  3593. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3594. break;
  3595. case SP_UNBREAKABLE_HELM:
  3596. if(sd->state.lr_flag != 2)
  3597. sd->bonus.unbreakable_equip |= EQP_HELM;
  3598. break;
  3599. case SP_UNBREAKABLE_SHIELD:
  3600. if(sd->state.lr_flag != 2)
  3601. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3602. break;
  3603. case SP_UNBREAKABLE_GARMENT:
  3604. if(sd->state.lr_flag != 2)
  3605. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3606. break;
  3607. case SP_UNBREAKABLE_SHOES:
  3608. if(sd->state.lr_flag != 2)
  3609. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3610. break;
  3611. case SP_CLASSCHANGE: // [Valaris]
  3612. if(sd->state.lr_flag !=2)
  3613. sd->bonus.classchange=val;
  3614. break;
  3615. case SP_SHORT_ATK_RATE:
  3616. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3617. sd->bonus.short_attack_atk_rate+=val;
  3618. break;
  3619. case SP_LONG_ATK_RATE:
  3620. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3621. sd->bonus.long_attack_atk_rate+=val;
  3622. break;
  3623. case SP_BREAK_WEAPON_RATE:
  3624. if(sd->state.lr_flag != 2)
  3625. sd->bonus.break_weapon_rate+=val;
  3626. break;
  3627. case SP_BREAK_ARMOR_RATE:
  3628. if(sd->state.lr_flag != 2)
  3629. sd->bonus.break_armor_rate+=val;
  3630. break;
  3631. case SP_ADD_STEAL_RATE:
  3632. if(sd->state.lr_flag != 2)
  3633. sd->bonus.add_steal_rate+=val;
  3634. break;
  3635. case SP_DELAYRATE:
  3636. if(sd->state.lr_flag != 2)
  3637. sd->bonus.delayrate -= val;
  3638. break;
  3639. case SP_CRIT_ATK_RATE:
  3640. if(sd->state.lr_flag != 2)
  3641. sd->bonus.crit_atk_rate += val;
  3642. break;
  3643. case SP_CRIT_DEF_RATE:
  3644. if(sd->state.lr_flag != 2)
  3645. sd->bonus.crit_def_rate += val;
  3646. break;
  3647. case SP_NO_REGEN:
  3648. if(sd->state.lr_flag != 2)
  3649. sd->regen.state.block|=val;
  3650. break;
  3651. case SP_UNSTRIPABLE_WEAPON:
  3652. if(sd->state.lr_flag != 2)
  3653. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3654. break;
  3655. case SP_UNSTRIPABLE:
  3656. case SP_UNSTRIPABLE_ARMOR:
  3657. if(sd->state.lr_flag != 2)
  3658. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3659. break;
  3660. case SP_UNSTRIPABLE_HELM:
  3661. if(sd->state.lr_flag != 2)
  3662. sd->bonus.unstripable_equip |= EQP_HELM;
  3663. break;
  3664. case SP_UNSTRIPABLE_SHIELD:
  3665. if(sd->state.lr_flag != 2)
  3666. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3667. break;
  3668. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3669. if(!sd->state.lr_flag) {
  3670. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3671. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3672. } else if(sd->state.lr_flag == 1) {
  3673. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3674. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3675. }
  3676. break;
  3677. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3678. if(!sd->state.lr_flag) {
  3679. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3680. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3681. } else if(sd->state.lr_flag == 1) {
  3682. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3683. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3684. }
  3685. break;
  3686. case SP_SP_GAIN_VALUE:
  3687. if(!sd->state.lr_flag)
  3688. sd->bonus.sp_gain_value += val;
  3689. break;
  3690. case SP_HP_GAIN_VALUE:
  3691. if(!sd->state.lr_flag)
  3692. sd->bonus.hp_gain_value += val;
  3693. break;
  3694. case SP_LONG_SP_GAIN_VALUE:
  3695. if(!sd->state.lr_flag)
  3696. sd->bonus.long_sp_gain_value += val;
  3697. case SP_LONG_HP_GAIN_VALUE:
  3698. if(!sd->state.lr_flag)
  3699. sd->bonus.long_hp_gain_value += val;
  3700. break;
  3701. case SP_MAGIC_SP_GAIN_VALUE:
  3702. if(!sd->state.lr_flag)
  3703. sd->bonus.magic_sp_gain_value += val;
  3704. break;
  3705. case SP_MAGIC_HP_GAIN_VALUE:
  3706. if(!sd->state.lr_flag)
  3707. sd->bonus.magic_hp_gain_value += val;
  3708. break;
  3709. case SP_ADD_HEAL_RATE:
  3710. if(sd->state.lr_flag != 2)
  3711. sd->bonus.add_heal_rate += val;
  3712. break;
  3713. case SP_ADD_HEAL2_RATE:
  3714. if(sd->state.lr_flag != 2)
  3715. sd->bonus.add_heal2_rate += val;
  3716. break;
  3717. case SP_ADD_ITEM_HEAL_RATE:
  3718. if(sd->state.lr_flag != 2)
  3719. sd->bonus.itemhealrate2 += val;
  3720. break;
  3721. case SP_EMATK:
  3722. if(sd->state.lr_flag != 2)
  3723. sd->bonus.ematk += val;
  3724. break;
  3725. case SP_ADD_VARIABLECAST:
  3726. if (sd->state.lr_flag != 2)
  3727. sd->bonus.add_varcast += val;
  3728. break;
  3729. #ifdef RENEWAL_CAST
  3730. case SP_FIXCASTRATE:
  3731. if(sd->state.lr_flag != 2)
  3732. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3733. break;
  3734. case SP_ADD_FIXEDCAST:
  3735. if(sd->state.lr_flag != 2)
  3736. sd->bonus.add_fixcast += val;
  3737. break;
  3738. case SP_CASTRATE:
  3739. case SP_VARCASTRATE:
  3740. if(sd->state.lr_flag != 2)
  3741. sd->bonus.varcastrate -= val;
  3742. break;
  3743. #else
  3744. case SP_ADD_FIXEDCAST:
  3745. case SP_FIXCASTRATE:
  3746. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3747. break;
  3748. case SP_VARCASTRATE:
  3749. case SP_CASTRATE:
  3750. if(sd->state.lr_flag != 2)
  3751. sd->castrate += val;
  3752. break;
  3753. #endif
  3754. case SP_ADDMAXWEIGHT:
  3755. if (sd->state.lr_flag != 2)
  3756. sd->add_max_weight += val;
  3757. break;
  3758. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3759. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3760. break;
  3761. case SP_ABSORB_DMG_MAXHP2:
  3762. sd->bonus.absorb_dmg_maxhp2 = max(sd->bonus.absorb_dmg_maxhp2, val);
  3763. break;
  3764. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3765. if (sd->state.lr_flag != 2)
  3766. sd->bonus.critical_rangeatk += val*10;
  3767. else
  3768. sd->bonus.arrow_cri += val*10;
  3769. break;
  3770. case SP_WEAPON_ATK_RATE:
  3771. if (sd->state.lr_flag != 2)
  3772. sd->bonus.weapon_atk_rate += val;
  3773. break;
  3774. case SP_WEAPON_MATK_RATE:
  3775. if (sd->state.lr_flag != 2)
  3776. sd->bonus.weapon_matk_rate += val;
  3777. break;
  3778. case SP_NO_MADO_FUEL:
  3779. if (sd->state.lr_flag != 2)
  3780. sd->special_state.no_mado_fuel = 1;
  3781. break;
  3782. case SP_NO_WALK_DELAY:
  3783. if (sd->state.lr_flag != 2)
  3784. sd->special_state.no_walk_delay = 1;
  3785. break;
  3786. case SP_ADD_ITEM_SPHEAL_RATE:
  3787. if(sd->state.lr_flag != 2)
  3788. sd->bonus.itemsphealrate2 += val;
  3789. break;
  3790. default:
  3791. if (current_equip_combo_pos > 0) {
  3792. 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);
  3793. }
  3794. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3795. 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);
  3796. }
  3797. else {
  3798. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3799. }
  3800. break;
  3801. }
  3802. }
  3803. /*==========================================
  3804. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3805. * format: bonus2 bBonusName,type2,val;
  3806. * @param sd
  3807. * @param type Bonus type used by bBonusName
  3808. * @param type2
  3809. * @param val Value that usually for rate or fixed value
  3810. *------------------------------------------*/
  3811. void pc_bonus2(map_session_data *sd,int type,int type2,int val)
  3812. {
  3813. nullpo_retv(sd);
  3814. switch(type){
  3815. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3816. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3817. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3818. sd->right_weapon.addele[type2]+=val;
  3819. else if(sd->state.lr_flag == 1)
  3820. sd->left_weapon.addele[type2]+=val;
  3821. else if(sd->state.lr_flag == 2)
  3822. sd->indexed_bonus.arrow_addele[type2]+=val;
  3823. break;
  3824. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3825. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3826. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3827. sd->right_weapon.addrace[type2]+=val;
  3828. else if(sd->state.lr_flag == 1)
  3829. sd->left_weapon.addrace[type2]+=val;
  3830. else if(sd->state.lr_flag == 2)
  3831. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3832. break;
  3833. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3834. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3835. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3836. sd->right_weapon.addclass[type2]+=val;
  3837. else if(sd->state.lr_flag == 1)
  3838. sd->left_weapon.addclass[type2]+=val;
  3839. else if(sd->state.lr_flag == 2)
  3840. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3841. break;
  3842. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3843. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3844. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3845. sd->right_weapon.addsize[type2]+=val;
  3846. else if(sd->state.lr_flag == 1)
  3847. sd->left_weapon.addsize[type2]+=val;
  3848. else if(sd->state.lr_flag == 2)
  3849. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3850. break;
  3851. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3852. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3853. if(sd->state.lr_flag != 2)
  3854. sd->indexed_bonus.subele_script[type2] += val;
  3855. break;
  3856. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3857. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3858. if(sd->state.lr_flag != 2)
  3859. sd->indexed_bonus.subrace[type2]+=val;
  3860. break;
  3861. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3862. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3863. if(sd->state.lr_flag != 2)
  3864. sd->indexed_bonus.subclass[type2]+=val;
  3865. break;
  3866. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3867. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3868. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3869. break;
  3870. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3871. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3872. 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);
  3873. break;
  3874. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3875. if(sd->state.lr_flag == 2)
  3876. break;
  3877. if (sd->reseff.size() == MAX_PC_BONUS) {
  3878. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3879. break;
  3880. }
  3881. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3882. break;
  3883. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3884. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3885. if(sd->state.lr_flag != 2)
  3886. sd->indexed_bonus.magic_addele_script[type2] += val;
  3887. break;
  3888. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3889. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3890. if(sd->state.lr_flag != 2)
  3891. sd->indexed_bonus.magic_addrace[type2]+=val;
  3892. break;
  3893. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3894. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3895. if(sd->state.lr_flag != 2)
  3896. sd->indexed_bonus.magic_addclass[type2]+=val;
  3897. break;
  3898. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3899. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3900. if(sd->state.lr_flag != 2)
  3901. sd->indexed_bonus.magic_addsize[type2]+=val;
  3902. break;
  3903. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3904. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3905. if(sd->state.lr_flag != 2)
  3906. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3907. break;
  3908. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3909. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3910. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3911. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3912. break;
  3913. }
  3914. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3915. }
  3916. break;
  3917. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3918. if(sd->state.lr_flag == 2)
  3919. break;
  3920. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3921. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3922. break;
  3923. }
  3924. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3925. break;
  3926. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3927. if(sd->state.lr_flag == 2)
  3928. break;
  3929. if (sd->add_def.size() == MAX_PC_BONUS) {
  3930. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3931. break;
  3932. }
  3933. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3934. break;
  3935. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3936. if(sd->state.lr_flag == 2)
  3937. break;
  3938. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3939. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3940. break;
  3941. }
  3942. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3943. break;
  3944. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3945. if(!sd->state.lr_flag) {
  3946. sd->right_weapon.hp_drain_rate.rate += type2;
  3947. sd->right_weapon.hp_drain_rate.per += val;
  3948. }
  3949. else if(sd->state.lr_flag == 1) {
  3950. sd->left_weapon.hp_drain_rate.rate += type2;
  3951. sd->left_weapon.hp_drain_rate.per += val;
  3952. }
  3953. break;
  3954. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3955. if(!sd->state.lr_flag) {
  3956. sd->right_weapon.sp_drain_rate.rate += type2;
  3957. sd->right_weapon.sp_drain_rate.per += val;
  3958. }
  3959. else if(sd->state.lr_flag == 1) {
  3960. sd->left_weapon.sp_drain_rate.rate += type2;
  3961. sd->left_weapon.sp_drain_rate.per += val;
  3962. }
  3963. break;
  3964. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3965. if(sd->state.lr_flag != 2) {
  3966. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3967. }
  3968. break;
  3969. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3970. if(sd->state.lr_flag != 2) {
  3971. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3972. }
  3973. break;
  3974. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3975. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3976. sd->bonus.get_zeny_rate = val;
  3977. sd->bonus.get_zeny_num = type2;
  3978. }
  3979. break;
  3980. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3981. if(sd->state.lr_flag != 2) {
  3982. sd->bonus.get_zeny_rate += val;
  3983. sd->bonus.get_zeny_num += type2;
  3984. }
  3985. break;
  3986. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3987. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3988. if(sd->state.lr_flag == 2)
  3989. break;
  3990. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3991. sd->special_state.bonus_coma = 1;
  3992. break;
  3993. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3994. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3995. if(sd->state.lr_flag == 2)
  3996. break;
  3997. sd->indexed_bonus.weapon_coma_race[type2] += val;
  3998. sd->special_state.bonus_coma = 1;
  3999. break;
  4000. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  4001. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  4002. if(sd->state.lr_flag == 2)
  4003. break;
  4004. sd->indexed_bonus.weapon_coma_class[type2] += val;
  4005. sd->special_state.bonus_coma = 1;
  4006. break;
  4007. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  4008. if(sd->state.lr_flag != 2)
  4009. sd->indexed_bonus.weapon_atk[type2]+=val;
  4010. break;
  4011. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  4012. if(sd->state.lr_flag != 2)
  4013. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  4014. break;
  4015. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  4016. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  4017. if(sd->state.lr_flag != 2)
  4018. sd->indexed_bonus.critaddrace[type2] += val*10;
  4019. break;
  4020. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  4021. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4022. if(sd->state.lr_flag != 2)
  4023. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  4024. break;
  4025. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  4026. if(sd->state.lr_flag == 2)
  4027. break;
  4028. if (sd->skillatk.size() == MAX_PC_BONUS) {
  4029. 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);
  4030. break;
  4031. }
  4032. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  4033. break;
  4034. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  4035. if(sd->state.lr_flag == 2)
  4036. break;
  4037. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  4038. 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);
  4039. break;
  4040. }
  4041. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  4042. break;
  4043. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  4044. if(sd->state.lr_flag == 2)
  4045. break;
  4046. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  4047. 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);
  4048. break;
  4049. }
  4050. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  4051. break;
  4052. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  4053. if(sd->state.lr_flag == 2)
  4054. break;
  4055. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4056. 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);
  4057. break;
  4058. }
  4059. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  4060. break;
  4061. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  4062. if(sd->state.lr_flag != 2) {
  4063. sd->hp_loss.value = type2;
  4064. sd->hp_loss.rate = val;
  4065. }
  4066. break;
  4067. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  4068. if(sd->state.lr_flag != 2) {
  4069. sd->hp_regen.value = type2;
  4070. sd->hp_regen.rate = val;
  4071. }
  4072. break;
  4073. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  4074. if (sd->state.lr_flag != 2) {
  4075. sd->percent_hp_regen.value = type2;
  4076. sd->percent_hp_regen.rate = val;
  4077. }
  4078. break;
  4079. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  4080. if (sd->state.lr_flag != 2) {
  4081. sd->percent_sp_regen.value = type2;
  4082. sd->percent_sp_regen.rate = val;
  4083. }
  4084. break;
  4085. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  4086. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  4087. if(sd->state.lr_flag != 2)
  4088. sd->right_weapon.addrace2[type2] += val;
  4089. else
  4090. sd->left_weapon.addrace2[type2] += val;
  4091. break;
  4092. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  4093. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  4094. if(sd->state.lr_flag != 2)
  4095. sd->indexed_bonus.subsize[type2]+=val;
  4096. break;
  4097. case SP_WEAPON_SUBSIZE: // bonus2 bWeaponSubSize,s,x;
  4098. PC_BONUS_CHK_SIZE(type2, SP_WEAPON_SUBSIZE);
  4099. if (sd->state.lr_flag != 2)
  4100. sd->indexed_bonus.weapon_subsize[type2] += val;
  4101. break;
  4102. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  4103. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  4104. if(sd->state.lr_flag != 2)
  4105. sd->indexed_bonus.magic_subsize[type2]+=val;
  4106. break;
  4107. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  4108. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  4109. if(sd->state.lr_flag != 2)
  4110. sd->indexed_bonus.subrace2[type2]+=val;
  4111. break;
  4112. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  4113. if(sd->state.lr_flag == 2)
  4114. break;
  4115. if( !item_db.exists( type2 ) ){
  4116. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  4117. break;
  4118. }
  4119. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  4120. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4121. break;
  4122. }
  4123. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  4124. break;
  4125. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  4126. if (sd->state.lr_flag == 2)
  4127. break;
  4128. if (!type2 || !itemdb_group.exists(type2)) {
  4129. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  4130. break;
  4131. }
  4132. if (sd->itemgrouphealrate.size() == MAX_PC_BONUS) {
  4133. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4134. break;
  4135. }
  4136. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  4137. break;
  4138. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  4139. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  4140. if(sd->state.lr_flag != 2)
  4141. sd->indexed_bonus.expaddrace[type2]+=val;
  4142. break;
  4143. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  4144. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  4145. if(sd->state.lr_flag != 2)
  4146. sd->indexed_bonus.expaddclass[type2]+=val;
  4147. break;
  4148. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  4149. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  4150. if(sd->state.lr_flag != 2)
  4151. sd->indexed_bonus.sp_gain_race[type2]+=val;
  4152. break;
  4153. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  4154. if (sd->state.lr_flag != 2)
  4155. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  4156. break;
  4157. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  4158. if (sd->state.lr_flag != 2)
  4159. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  4160. break;
  4161. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  4162. if(sd->state.lr_flag != 2) {
  4163. sd->sp_loss.value = type2;
  4164. sd->sp_loss.rate = val;
  4165. }
  4166. break;
  4167. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  4168. if(sd->state.lr_flag != 2) {
  4169. sd->sp_regen.value = type2;
  4170. sd->sp_regen.rate = val;
  4171. }
  4172. break;
  4173. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  4174. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  4175. if(!sd->state.lr_flag) {
  4176. sd->right_weapon.hp_drain_race[type2] += val;
  4177. }
  4178. else if(sd->state.lr_flag == 1) {
  4179. sd->left_weapon.hp_drain_race[type2] += val;
  4180. }
  4181. break;
  4182. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  4183. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  4184. if(!sd->state.lr_flag) {
  4185. sd->right_weapon.sp_drain_race[type2] += val;
  4186. }
  4187. else if(sd->state.lr_flag == 1) {
  4188. sd->left_weapon.sp_drain_race[type2] += val;
  4189. }
  4190. break;
  4191. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  4192. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  4193. if(!sd->state.lr_flag) {
  4194. sd->right_weapon.hp_drain_class[type2] += val;
  4195. }
  4196. else if(sd->state.lr_flag == 1) {
  4197. sd->left_weapon.hp_drain_class[type2] += val;
  4198. }
  4199. break;
  4200. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  4201. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  4202. if(!sd->state.lr_flag) {
  4203. sd->right_weapon.sp_drain_class[type2] += val;
  4204. }
  4205. else if(sd->state.lr_flag == 1) {
  4206. sd->left_weapon.sp_drain_class[type2] += val;
  4207. }
  4208. break;
  4209. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  4210. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  4211. if(sd->state.lr_flag != 2)
  4212. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  4213. break;
  4214. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  4215. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  4216. if(sd->state.lr_flag != 2)
  4217. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  4218. break;
  4219. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  4220. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  4221. if(sd->state.lr_flag != 2)
  4222. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  4223. break;
  4224. case SP_SP_IGNORE_RES_RACE_RATE: // bonus2 bIgnoreResRaceRate,r,n;
  4225. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_RES_RACE_RATE);
  4226. if(sd->state.lr_flag != 2)
  4227. sd->indexed_bonus.ignore_res_by_race[type2] += val;
  4228. break;
  4229. case SP_SP_IGNORE_MRES_RACE_RATE: // bonus2 bIgnoreMResRaceRate,r,n;
  4230. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_MRES_RACE_RATE);
  4231. if(sd->state.lr_flag != 2)
  4232. sd->indexed_bonus.ignore_mres_by_race[type2] += val;
  4233. break;
  4234. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  4235. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  4236. if (sd->state.lr_flag != 2)
  4237. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  4238. break;
  4239. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  4240. if(sd->state.lr_flag == 2)
  4241. break;
  4242. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  4243. 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);
  4244. break;
  4245. }
  4246. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  4247. break;
  4248. case SP_SKILL_DELAY:
  4249. if(sd->state.lr_flag == 2)
  4250. break;
  4251. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  4252. 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);
  4253. break;
  4254. }
  4255. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  4256. break;
  4257. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  4258. if(sd->state.lr_flag == 2)
  4259. break;
  4260. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  4261. 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);
  4262. break;
  4263. }
  4264. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  4265. break;
  4266. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  4267. if (sd->state.lr_flag == 2)
  4268. break;
  4269. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  4270. 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);
  4271. break;
  4272. }
  4273. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  4274. break;
  4275. #ifdef RENEWAL_CAST
  4276. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4277. if(sd->state.lr_flag == 2)
  4278. break;
  4279. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  4280. 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);
  4281. break;
  4282. }
  4283. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  4284. break;
  4285. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4286. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4287. if(sd->state.lr_flag == 2)
  4288. break;
  4289. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  4290. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4291. break;
  4292. }
  4293. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  4294. break;
  4295. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4296. if(sd->state.lr_flag == 2)
  4297. break;
  4298. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  4299. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4300. break;
  4301. }
  4302. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  4303. break;
  4304. #else
  4305. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4306. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4307. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  4308. break;
  4309. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4310. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4311. if(sd->state.lr_flag == 2)
  4312. break;
  4313. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4314. 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);
  4315. break;
  4316. }
  4317. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  4318. break;
  4319. #endif
  4320. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  4321. if(sd->state.lr_flag == 2)
  4322. break;
  4323. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  4324. 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);
  4325. break;
  4326. }
  4327. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  4328. break;
  4329. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  4330. if (sd->subskill.size() == MAX_PC_BONUS) {
  4331. 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);
  4332. break;
  4333. }
  4334. pc_bonus_itembonus(sd->subskill, type2, val, false);
  4335. break;
  4336. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  4337. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  4338. sd->indexed_bonus.subdefele[type2] += val;
  4339. break;
  4340. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  4341. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  4342. sd->indexed_bonus.coma_class[type2] += val;
  4343. sd->special_state.bonus_coma = 1;
  4344. break;
  4345. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  4346. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  4347. sd->indexed_bonus.coma_race[type2] += val;
  4348. sd->special_state.bonus_coma = 1;
  4349. break;
  4350. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  4351. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  4352. if(sd->state.lr_flag != 2)
  4353. sd->indexed_bonus.magic_addrace2[type2] += val;
  4354. break;
  4355. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  4356. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  4357. if (sd->state.lr_flag != 2)
  4358. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  4359. break;
  4360. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  4361. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  4362. if (sd->state.lr_flag != 2)
  4363. sd->indexed_bonus.dropaddrace[type2] += val;
  4364. break;
  4365. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  4366. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  4367. if (sd->state.lr_flag != 2)
  4368. sd->indexed_bonus.dropaddclass[type2] += val;
  4369. break;
  4370. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  4371. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  4372. sd->indexed_bonus.magic_subdefele[type2] += val;
  4373. break;
  4374. case SP_ADD_ITEM_SPHEAL_RATE: // bonus2 bAddItemSPHealRate,iid,n;
  4375. if( sd->state.lr_flag == 2 ){
  4376. break;
  4377. }
  4378. if( !item_db.exists( type2 ) ){
  4379. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE Invalid item with id %d\n", type2 );
  4380. break;
  4381. }
  4382. if( sd->itemsphealrate.size() == MAX_PC_BONUS ){
  4383. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4384. break;
  4385. }
  4386. pc_bonus_itembonus( sd->itemsphealrate, type2, val, false );
  4387. break;
  4388. case SP_ADD_ITEMGROUP_SPHEAL_RATE: // bonus2 bAddItemGroupSPHealRate,ig,n;
  4389. if( sd->state.lr_flag == 2 ){
  4390. break;
  4391. }
  4392. if( !type2 || !itemdb_group.exists( type2 ) ){
  4393. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Invalid item group with id %d\n", type2 );
  4394. break;
  4395. }
  4396. if( sd->itemgroupsphealrate.size() == MAX_PC_BONUS ){
  4397. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4398. break;
  4399. }
  4400. pc_bonus_itembonus( sd->itemgroupsphealrate, type2, val, false );
  4401. break;
  4402. default:
  4403. if (current_equip_combo_pos > 0) {
  4404. 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);
  4405. }
  4406. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4407. 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);
  4408. }
  4409. else {
  4410. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  4411. }
  4412. break;
  4413. }
  4414. }
  4415. /**
  4416. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  4417. * @param sd
  4418. * @param type Bonus type used by bBonusName
  4419. * @param type2
  4420. * @param type3
  4421. * @param val Value that usually for rate or fixed value
  4422. */
  4423. void pc_bonus3(map_session_data *sd,int type,int type2,int type3,int val)
  4424. {
  4425. nullpo_retv(sd);
  4426. switch(type){
  4427. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  4428. if(sd->state.lr_flag != 2)
  4429. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  4430. break;
  4431. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  4432. if(sd->state.lr_flag != 2)
  4433. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  4434. break;
  4435. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  4436. if(sd->state.lr_flag != 2)
  4437. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  4438. break;
  4439. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  4440. if(sd->state.lr_flag != 2)
  4441. {
  4442. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4443. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4444. pc_bonus_autospell(sd->autospell, type2, type3, val, 0, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4445. }
  4446. break;
  4447. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  4448. if(sd->state.lr_flag != 2)
  4449. {
  4450. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4451. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4452. pc_bonus_autospell(sd->autospell2, type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4453. }
  4454. break;
  4455. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  4456. if (sd->state.lr_flag != 2)
  4457. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  4458. break;
  4459. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  4460. if (sd->state.lr_flag != 2)
  4461. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  4462. break;
  4463. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  4464. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4465. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  4466. break;
  4467. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  4468. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4469. if(sd->state.lr_flag != 2)
  4470. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  4471. break;
  4472. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  4473. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4474. if( sd->state.lr_flag != 2 )
  4475. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  4476. break;
  4477. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  4478. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  4479. if (sd->state.lr_flag != 2)
  4480. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  4481. break;
  4482. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  4483. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  4484. if (sd->state.lr_flag != 2)
  4485. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  4486. break;
  4487. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  4488. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  4489. if (sd->state.lr_flag != 2)
  4490. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  4491. break;
  4492. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  4493. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  4494. if(sd->state.lr_flag != 2) {
  4495. sd->sp_vanish_race[type2].rate += type3;
  4496. sd->sp_vanish_race[type2].per += val;
  4497. }
  4498. break;
  4499. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  4500. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  4501. if(sd->state.lr_flag != 2) {
  4502. sd->hp_vanish_race[type2].rate += type3;
  4503. sd->hp_vanish_race[type2].per += val;
  4504. }
  4505. break;
  4506. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  4507. if(sd->state.lr_flag != 2) {
  4508. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  4509. }
  4510. break;
  4511. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  4512. if(sd->state.lr_flag != 2) {
  4513. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  4514. }
  4515. break;
  4516. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  4517. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  4518. if (sd->state.lr_flag == 2)
  4519. break;
  4520. //! CONFIRM: Is it not stackable? Does not check max or min value?
  4521. //if (type3 > sd->norecover_state_race[type2].rate) {
  4522. // sd->norecover_state_race[type2].rate = type3;
  4523. // sd->norecover_state_race[type2].tick = val;
  4524. // break;
  4525. //}
  4526. sd->norecover_state_race[type2].rate = type3;
  4527. sd->norecover_state_race[type2].tick = val;
  4528. break;
  4529. default:
  4530. if (current_equip_combo_pos > 0) {
  4531. 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);
  4532. }
  4533. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4534. 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);
  4535. }
  4536. else {
  4537. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  4538. }
  4539. break;
  4540. }
  4541. }
  4542. /**
  4543. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  4544. * @param sd
  4545. * @param type Bonus type used by bBonusName
  4546. * @param type2
  4547. * @param type3
  4548. * @param type4
  4549. * @param val Value that usually for rate or fixed value
  4550. */
  4551. void pc_bonus4(map_session_data *sd,int type,int type2,int type3,int type4,int val)
  4552. {
  4553. nullpo_retv(sd);
  4554. switch(type){
  4555. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4556. if(sd->state.lr_flag != 2)
  4557. pc_bonus_autospell(sd->autospell, type2, type3, type4, 0, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4558. break;
  4559. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4560. if(sd->state.lr_flag != 2)
  4561. pc_bonus_autospell(sd->autospell2, type2, type3, type4, BF_NORMAL|BF_SKILL, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4562. break;
  4563. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4564. if(sd->state.lr_flag != 2)
  4565. {
  4566. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4567. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4568. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, val, current_equip_card_id, target ? AUTOSPELL_FORCE_TARGET : AUTOSPELL_FORCE_SELF);
  4569. }
  4570. break;
  4571. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4572. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4573. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  4574. break;
  4575. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4576. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4577. if (sd->state.lr_flag != 2)
  4578. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4579. break;
  4580. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4581. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4582. if( sd->state.lr_flag != 2 )
  4583. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4584. break;
  4585. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4586. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4587. if(sd->state.lr_flag == 2)
  4588. break;
  4589. sd->def_set_race[type2].rate = type3;
  4590. sd->def_set_race[type2].tick = type4;
  4591. sd->def_set_race[type2].value = val;
  4592. break;
  4593. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4594. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4595. if(sd->state.lr_flag == 2)
  4596. break;
  4597. sd->mdef_set_race[type2].rate = type3;
  4598. sd->mdef_set_race[type2].tick = type4;
  4599. sd->mdef_set_race[type2].value = val;
  4600. break;
  4601. default:
  4602. if (current_equip_combo_pos > 0) {
  4603. 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);
  4604. }
  4605. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4606. 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);
  4607. }
  4608. else {
  4609. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4610. }
  4611. break;
  4612. }
  4613. }
  4614. /**
  4615. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4616. * @param sd
  4617. * @param type Bonus type used by bBonusName
  4618. * @param type2
  4619. * @param type3
  4620. * @param type4
  4621. * @param val Value that usually for rate or fixed value
  4622. */
  4623. void pc_bonus5(map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  4624. {
  4625. nullpo_retv(sd);
  4626. switch(type){
  4627. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4628. if(sd->state.lr_flag != 2)
  4629. pc_bonus_autospell(sd->autospell, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4630. break;
  4631. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4632. if(sd->state.lr_flag != 2)
  4633. pc_bonus_autospell(sd->autospell2, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4634. break;
  4635. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4636. if(sd->state.lr_flag != 2)
  4637. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4638. break;
  4639. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4640. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4641. if( sd->state.lr_flag != 2 )
  4642. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4643. break;
  4644. default:
  4645. if (current_equip_combo_pos > 0) {
  4646. 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);
  4647. }
  4648. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4649. 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);
  4650. }
  4651. else {
  4652. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4653. }
  4654. break;
  4655. }
  4656. }
  4657. /*==========================================
  4658. * Grants a player a given skill. Flag values are:
  4659. * 0 - Grant permanent skill to be bound to skill tree
  4660. * 1 - Grant an item skill (temporary)
  4661. * 2 - Like 1, except the level granted can stack with previously learned level.
  4662. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4663. *------------------------------------------*/
  4664. bool pc_skill(map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  4665. uint16 idx = 0;
  4666. nullpo_ret(sd);
  4667. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4668. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4669. return false;
  4670. }
  4671. if (level > MAX_SKILL_LEVEL) {
  4672. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4673. return false;
  4674. }
  4675. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4676. 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);
  4677. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4678. }
  4679. switch (type) {
  4680. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4681. sd->status.skill[idx].id = skill_id;
  4682. sd->status.skill[idx].lv = level;
  4683. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4684. if (level == 0) { //Remove skill.
  4685. sd->status.skill[idx].id = 0;
  4686. clif_deleteskill(sd,skill_id);
  4687. } else
  4688. clif_addskill(sd,skill_id);
  4689. 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.
  4690. status_calc_pc(sd, SCO_NONE);
  4691. break;
  4692. case ADDSKILL_TEMP: //Item bonus skill.
  4693. if (sd->status.skill[idx].id != 0) {
  4694. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4695. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4696. } else {
  4697. sd->status.skill[idx].id = skill_id;
  4698. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4699. }
  4700. sd->status.skill[idx].lv = max(sd->status.skill[idx].lv, level);
  4701. break;
  4702. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4703. if (sd->status.skill[idx].id != 0) {
  4704. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4705. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4706. } else {
  4707. sd->status.skill[idx].id = skill_id;
  4708. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4709. }
  4710. sd->status.skill[idx].lv += level;
  4711. break;
  4712. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4713. sd->status.skill[idx].id = skill_id;
  4714. sd->status.skill[idx].lv = level;
  4715. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4716. if (level == 0) { //Remove skill.
  4717. sd->status.skill[idx].id = 0;
  4718. clif_deleteskill(sd,skill_id);
  4719. } else
  4720. clif_addskill(sd,skill_id);
  4721. 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.
  4722. status_calc_pc(sd, SCO_NONE);
  4723. break;
  4724. default:
  4725. return false;
  4726. }
  4727. return true;
  4728. }
  4729. /**
  4730. * Set's a player's plagiarized skill.
  4731. * @param sd: Player
  4732. * @param skill_id: Skill to be plagiarized
  4733. * @param skill_lv: Skill level to be plagiarized
  4734. * @return True on success or false otherwise
  4735. */
  4736. bool pc_skill_plagiarism(map_session_data &sd, uint16 skill_id, uint16 skill_lv)
  4737. {
  4738. skill_id = skill_dummy2skill_id(skill_id);
  4739. uint16 idx = skill_get_index(skill_id);
  4740. // Use skill index, avoiding out-of-bound array [Cydh]
  4741. if (idx == 0) {
  4742. ShowWarning("pc_skill_plagiarism: invalid skill idx 0 for skill %d.\n", skill_id);
  4743. return false;
  4744. }
  4745. skill_lv = cap_value(skill_lv, 1, skill_get_max(skill_id));
  4746. int type = skill_isCopyable(&sd, skill_id);
  4747. if (type == 1) {
  4748. pc_skill_plagiarism_reset(sd, type);
  4749. sd.cloneskill_idx = idx;
  4750. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), skill_id);
  4751. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), skill_lv);
  4752. } else if (type == 2) {
  4753. pc_skill_plagiarism_reset(sd, type);
  4754. sd.reproduceskill_idx = idx;
  4755. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), skill_id);
  4756. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), skill_lv);
  4757. } else {
  4758. ShowWarning("pc_skill_plagiarism: skill %d is not copyable.\n", skill_id);
  4759. return false;
  4760. }
  4761. sd.status.skill[idx].id = skill_id;
  4762. sd.status.skill[idx].lv = static_cast<uint8>(skill_lv);
  4763. sd.status.skill[idx].flag = SKILL_FLAG_PLAGIARIZED;
  4764. clif_addskill(&sd, skill_id);
  4765. return true;
  4766. }
  4767. /**
  4768. * Clear plagiarized skills from a player.
  4769. * @param sd: Player
  4770. * @param type: 1 for Plagiarism or 2 for Reproduce
  4771. * @return True on success or false otherwise
  4772. */
  4773. bool pc_skill_plagiarism_reset(map_session_data &sd, uint8 type)
  4774. {
  4775. uint16 idx;
  4776. if (type == 1)
  4777. idx = sd.cloneskill_idx;
  4778. else if (type == 2)
  4779. idx = sd.reproduceskill_idx;
  4780. else {
  4781. ShowError("pc_skill_plagiarism_reset: Unknown type %d.\n", type);
  4782. return false;
  4783. }
  4784. if (sd.status.skill[idx].flag == SKILL_FLAG_PLAGIARIZED) {
  4785. uint16 skill_id = sd.status.skill[idx].id;
  4786. sd.status.skill[idx].id = 0;
  4787. sd.status.skill[idx].lv = 0;
  4788. sd.status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4789. clif_deleteskill(&sd, skill_id);
  4790. if (type == 1) {
  4791. sd.cloneskill_idx = 0;
  4792. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  4793. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  4794. }
  4795. else if (type == 2) {
  4796. sd.reproduceskill_idx = 0;
  4797. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), 0);
  4798. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  4799. }
  4800. }
  4801. return true;
  4802. }
  4803. /*==========================================
  4804. * Append a card to an item ?
  4805. *------------------------------------------*/
  4806. int pc_insert_card(map_session_data* sd, int idx_card, int idx_equip)
  4807. {
  4808. nullpo_ret(sd);
  4809. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4810. return 0;
  4811. }
  4812. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4813. return 0;
  4814. }
  4815. int i;
  4816. t_itemid nameid;
  4817. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4818. struct item_data* item_card = sd->inventory_data[idx_card];
  4819. if(item_eq == nullptr)
  4820. return 0; //Invalid item index.
  4821. if(item_card == nullptr)
  4822. return 0; //Invalid card index.
  4823. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4824. return 0; // target item missing
  4825. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4826. return 0; // target card missing
  4827. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4828. return 0; // only weapons and armor are allowed
  4829. if( item_card->type != IT_CARD )
  4830. return 0; // must be a card
  4831. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4832. return 0; // target must be identified
  4833. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4834. return 0; // card slots reserved for other purposes
  4835. if( (item_eq->equip & item_card->equip) == 0 )
  4836. return 0; // card cannot be compounded on this item type
  4837. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4838. return 0; // attempted to place shield card on left-hand weapon.
  4839. 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)) )
  4840. return 0; // specific accessory-card can only be inserted to specific accessory.
  4841. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4842. return 0; // item must be unequipped
  4843. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4844. if( i == item_eq->slots )
  4845. return 0; // no free slots
  4846. // remember the card id to insert
  4847. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4848. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4849. {// failed
  4850. clif_insert_card(sd,idx_equip,idx_card,1);
  4851. }
  4852. else
  4853. {// success
  4854. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4855. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4856. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4857. clif_insert_card(sd,idx_equip,idx_card,0);
  4858. }
  4859. return 0;
  4860. }
  4861. /**
  4862. * Returns the count of unidentified items with the option to identify too.
  4863. * @param sd: Player data
  4864. * @param identify_item: Whether or not to identify any unidentified items
  4865. * @return Unidentified items count
  4866. */
  4867. int pc_identifyall(map_session_data *sd, bool identify_item)
  4868. {
  4869. int unidentified_count = 0;
  4870. for (int i = 0; i < MAX_INVENTORY; i++) {
  4871. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4872. if (identify_item == true) {
  4873. sd->inventory.u.items_inventory[i].identify = 1;
  4874. clif_item_identified(sd,i,0);
  4875. }
  4876. unidentified_count++;
  4877. }
  4878. }
  4879. return unidentified_count;
  4880. }
  4881. //
  4882. // Items
  4883. //
  4884. /*==========================================
  4885. * Update buying value by skills
  4886. *------------------------------------------*/
  4887. int pc_modifybuyvalue(map_session_data *sd,int orig_value)
  4888. {
  4889. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4890. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4891. rate1 = 5+skill*2-((skill==10)? 1:0);
  4892. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4893. rate2 = 5+skill*4;
  4894. if(rate1 < rate2) rate1 = rate2;
  4895. if(rate1)
  4896. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4897. if(val < battle_config.min_shop_buy)
  4898. val = battle_config.min_shop_buy;
  4899. return val;
  4900. }
  4901. /*==========================================
  4902. * Update selling value by skills
  4903. *------------------------------------------*/
  4904. int pc_modifysellvalue(map_session_data *sd,int orig_value)
  4905. {
  4906. int skill,val = orig_value,rate = 0;
  4907. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4908. rate = 5+skill*2-((skill==10)? 1:0);
  4909. if(rate)
  4910. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4911. if (val < battle_config.min_shop_sell)
  4912. val = battle_config.min_shop_sell;
  4913. return val;
  4914. }
  4915. /*==========================================
  4916. * Checking if we have enough place on inventory for new item
  4917. * Make sure to take 30k as limit (for client I guess)
  4918. * @param sd
  4919. * @param nameid
  4920. * @param amount
  4921. * @return e_chkitem_result
  4922. *------------------------------------------*/
  4923. char pc_checkadditem(map_session_data *sd, t_itemid nameid, int amount)
  4924. {
  4925. int i;
  4926. struct item_data* data;
  4927. nullpo_ret(sd);
  4928. if(amount > MAX_AMOUNT)
  4929. return CHKADDITEM_OVERAMOUNT;
  4930. data = itemdb_search(nameid);
  4931. if(!itemdb_isstackable2(data))
  4932. return CHKADDITEM_NEW;
  4933. if( data->stack.inventory && amount > data->stack.amount )
  4934. return CHKADDITEM_OVERAMOUNT;
  4935. if (data->flag.guid)
  4936. return CHKADDITEM_NEW;
  4937. for(i=0;i<MAX_INVENTORY;i++){
  4938. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4939. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4940. 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 ) )
  4941. return CHKADDITEM_OVERAMOUNT;
  4942. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4943. if( i >= sd->status.inventory_slots ){
  4944. return CHKADDITEM_OVERAMOUNT;
  4945. }
  4946. return CHKADDITEM_EXIST;
  4947. }
  4948. }
  4949. return CHKADDITEM_NEW;
  4950. }
  4951. /*==========================================
  4952. * Return number of available place in inventory
  4953. * Each non stackable item will reduce place by 1
  4954. * @param sd
  4955. * @return Number of empty slots
  4956. *------------------------------------------*/
  4957. uint8 pc_inventoryblank(map_session_data *sd)
  4958. {
  4959. uint16 i;
  4960. uint8 b;
  4961. nullpo_ret(sd);
  4962. for(i = 0, b = 0; i < sd->status.inventory_slots; i++){
  4963. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4964. b++;
  4965. }
  4966. return b;
  4967. }
  4968. /**
  4969. * Attempts to remove zeny from player
  4970. * @param sd: Player
  4971. * @param zeny: Zeny removed
  4972. * @param type: Log type
  4973. * @param log_charid: (optional) From who to log (if not needed, use 0)
  4974. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4975. */
  4976. char pc_payzeny(map_session_data *sd, int zeny, enum e_log_pick_type type, uint32 log_charid)
  4977. {
  4978. nullpo_retr(2,sd);
  4979. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4980. if( zeny < 0 )
  4981. {
  4982. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4983. return 1;
  4984. }
  4985. if( sd->status.zeny < zeny )
  4986. return 1; //Not enough.
  4987. sd->status.zeny -= zeny;
  4988. clif_updatestatus(sd,SP_ZENY);
  4989. log_zeny(*sd, type, log_charid, -zeny);
  4990. if( zeny > 0 && sd->state.showzeny ) {
  4991. char output[255];
  4992. sprintf(output, "Removed %dz.", zeny);
  4993. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4994. }
  4995. return 0;
  4996. }
  4997. /**
  4998. * Attempts to give zeny to player
  4999. * @param sd: Player
  5000. * @param type: Log type
  5001. * @param log_charid: (optional) From who to log (if not needed, use 0)
  5002. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  5003. */
  5004. char pc_getzeny(map_session_data *sd, int zeny, enum e_log_pick_type type, uint32 log_charid)
  5005. {
  5006. nullpo_retr(-1,sd);
  5007. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  5008. if( zeny < 0 )
  5009. {
  5010. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  5011. return 1;
  5012. }
  5013. if( zeny > MAX_ZENY - sd->status.zeny )
  5014. zeny = MAX_ZENY - sd->status.zeny;
  5015. sd->status.zeny += zeny;
  5016. clif_updatestatus(sd,SP_ZENY);
  5017. log_zeny(*sd, type, log_charid, zeny);
  5018. if( zeny > 0 && sd->state.showzeny ) {
  5019. char output[255];
  5020. sprintf(output, "Gained %dz.", zeny);
  5021. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5022. }
  5023. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  5024. return 0;
  5025. }
  5026. /**
  5027. * Attempts to remove Cash Points from player
  5028. * @param sd: Player
  5029. * @param price: Total points (cash + kafra) the player has to pay
  5030. * @param points: Kafra points the player has to pay
  5031. * @param type: Log type
  5032. * @return -1: Not enough points, otherwise success (cash+points)
  5033. */
  5034. int pc_paycash(map_session_data *sd, int price, int points, e_log_pick_type type)
  5035. {
  5036. int cash;
  5037. nullpo_retr(-1,sd);
  5038. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  5039. cash = price-points;
  5040. if( sd->cashPoints < cash || sd->kafraPoints < points )
  5041. {
  5042. 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);
  5043. return -1;
  5044. }
  5045. if( cash ){
  5046. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  5047. sd->cashPoints -= cash;
  5048. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  5049. }
  5050. if( points ){
  5051. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  5052. sd->kafraPoints -= points;
  5053. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  5054. }
  5055. if( battle_config.cashshop_show_points )
  5056. {
  5057. char output[CHAT_SIZE_MAX];
  5058. 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.
  5059. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5060. }
  5061. return cash+points;
  5062. }
  5063. /**
  5064. * Attempts to give Cash Points to player
  5065. * @param sd: Player
  5066. * @param cash: Cash points the player gets
  5067. * @param points: Kafra points the player gets
  5068. * @param type: Log type
  5069. * @return -1: Error, otherwise success (cash or points)
  5070. */
  5071. int pc_getcash(map_session_data *sd, int cash, int points, e_log_pick_type type)
  5072. {
  5073. char output[CHAT_SIZE_MAX];
  5074. nullpo_retr(-1,sd);
  5075. cash = cap_value(cash, 0, MAX_CASHPOINT); //prevent command UB
  5076. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  5077. if( cash > 0 )
  5078. {
  5079. if( cash > MAX_CASHPOINT-sd->cashPoints )
  5080. {
  5081. 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);
  5082. cash = MAX_CASHPOINT-sd->cashPoints;
  5083. }
  5084. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  5085. sd->cashPoints += cash;
  5086. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  5087. if( battle_config.cashshop_show_points )
  5088. {
  5089. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  5090. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5091. }
  5092. return cash;
  5093. }
  5094. if( points > 0 )
  5095. {
  5096. if( points > MAX_KAFRAPOINT-sd->kafraPoints )
  5097. {
  5098. 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);
  5099. points = MAX_KAFRAPOINT-sd->kafraPoints;
  5100. }
  5101. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  5102. sd->kafraPoints += points;
  5103. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  5104. if( battle_config.cashshop_show_points )
  5105. {
  5106. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  5107. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5108. }
  5109. return points;
  5110. }
  5111. return -1; //shouldn't happen but just in case
  5112. }
  5113. /**
  5114. * Searching a specified itemid in inventory and return his stored index
  5115. * @param sd Player
  5116. * @param nameid Find this Item!
  5117. * @return Stored index in inventory, or -1 if not found.
  5118. **/
  5119. short pc_search_inventory(map_session_data *sd, t_itemid nameid) {
  5120. short i;
  5121. nullpo_retr(-1, sd);
  5122. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  5123. return ( i < MAX_INVENTORY ) ? i : -1;
  5124. }
  5125. /** Attempt to add a new item to player inventory
  5126. * @param sd
  5127. * @param item_data
  5128. * @param amount
  5129. * @param log_type
  5130. * @return
  5131. * 0 = success
  5132. * 1 = invalid itemid not found or negative amount
  5133. * 2 = overweight
  5134. * 3 = ?
  5135. * 4 = no free place found
  5136. * 5 = max amount reached
  5137. * 6 = ?
  5138. * 7 = stack limitation
  5139. */
  5140. enum e_additem_result pc_additem(map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  5141. struct item_data *id;
  5142. int16 i;
  5143. unsigned int w;
  5144. nullpo_retr(ADDITEM_INVALID, sd);
  5145. nullpo_retr(ADDITEM_INVALID, item);
  5146. if( item->nameid == 0 || amount <= 0 )
  5147. return ADDITEM_INVALID;
  5148. if( amount > MAX_AMOUNT )
  5149. return ADDITEM_OVERAMOUNT;
  5150. id = itemdb_search(item->nameid);
  5151. if( id->stack.inventory && amount > id->stack.amount )
  5152. {// item stack limitation
  5153. return ADDITEM_STACKLIMIT;
  5154. }
  5155. w = id->weight*amount;
  5156. if(sd->weight + w > sd->max_weight)
  5157. return ADDITEM_OVERWEIGHT;
  5158. if (id->flag.guid && !item->unique_id)
  5159. item->unique_id = pc_generate_unique_id(sd);
  5160. // Stackable | Non Rental
  5161. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  5162. for( i = 0; i < MAX_INVENTORY; i++ ) {
  5163. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  5164. sd->inventory.u.items_inventory[i].bound == item->bound &&
  5165. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  5166. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  5167. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  5168. 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 ) )
  5169. return ADDITEM_OVERAMOUNT;
  5170. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  5171. if( i >= sd->status.inventory_slots ){
  5172. return ADDITEM_OVERAMOUNT;
  5173. }
  5174. sd->inventory.u.items_inventory[i].amount += amount;
  5175. clif_additem(sd,i,amount,0);
  5176. break;
  5177. }
  5178. }
  5179. }else{
  5180. i = MAX_INVENTORY;
  5181. }
  5182. if (i >= MAX_INVENTORY) {
  5183. i = pc_search_inventory(sd,0);
  5184. if( i < 0 )
  5185. return ADDITEM_OVERITEM;
  5186. if( i >= sd->status.inventory_slots ){
  5187. return ADDITEM_OVERITEM;
  5188. }
  5189. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  5190. // clear equip and favorite fields first, just in case
  5191. if( item->equip )
  5192. sd->inventory.u.items_inventory[i].equip = 0;
  5193. if( item->favorite )
  5194. sd->inventory.u.items_inventory[i].favorite = 0;
  5195. if( item->equipSwitch )
  5196. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  5197. sd->inventory.u.items_inventory[i].amount = amount;
  5198. sd->inventory_data[i] = id;
  5199. sd->last_addeditem_index = i;
  5200. if (!itemdb_isstackable2(id) || id->flag.guid)
  5201. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  5202. clif_additem(sd,i,amount,0);
  5203. }
  5204. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  5205. sd->weight += w;
  5206. clif_updatestatus(sd,SP_WEIGHT);
  5207. //Auto-equip
  5208. if(id->flag.autoequip)
  5209. pc_equipitem(sd, i, id->equip);
  5210. /* rental item check */
  5211. if( item->expire_time ) {
  5212. if( time(NULL) > item->expire_time ) {
  5213. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  5214. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  5215. } else {
  5216. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  5217. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  5218. pc_inventory_rental_add(sd, seconds);
  5219. }
  5220. }
  5221. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  5222. pc_show_questinfo(sd);
  5223. return ADDITEM_SUCCESS;
  5224. }
  5225. /*==========================================
  5226. * Remove an item at index n from inventory by amount.
  5227. * @param sd
  5228. * @param n Item index in inventory
  5229. * @param amount
  5230. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  5231. * @param reason Delete reason
  5232. * @param log_type e_log_pick_type
  5233. * @return 1 - invalid itemid or negative amount; 0 - Success
  5234. *------------------------------------------*/
  5235. char pc_delitem(map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  5236. {
  5237. nullpo_retr(1, sd);
  5238. 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)
  5239. return 1;
  5240. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  5241. sd->inventory.u.items_inventory[n].amount -= amount;
  5242. sd->weight -= sd->inventory_data[n]->weight*amount ;
  5243. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  5244. if(sd->inventory.u.items_inventory[n].equip)
  5245. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  5246. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  5247. sd->inventory_data[n] = NULL;
  5248. }
  5249. if(!(type&1))
  5250. clif_delitem(sd,n,amount,reason);
  5251. if(!(type&2))
  5252. clif_updatestatus(sd,SP_WEIGHT);
  5253. pc_show_questinfo(sd);
  5254. return 0;
  5255. }
  5256. /*==========================================
  5257. * Attempt to drop an item.
  5258. * @param sd
  5259. * @param n Item index in inventory
  5260. * @param amount Amount of item
  5261. * @return False = fail; True = success
  5262. *------------------------------------------*/
  5263. bool pc_dropitem(map_session_data *sd,int n,int amount)
  5264. {
  5265. nullpo_retr(1, sd);
  5266. if(n < 0 || n >= MAX_INVENTORY)
  5267. return false;
  5268. if(amount <= 0)
  5269. return false;
  5270. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  5271. sd->inventory.u.items_inventory[n].amount <= 0 ||
  5272. sd->inventory.u.items_inventory[n].amount < amount ||
  5273. sd->state.trading || sd->state.vending ||
  5274. !sd->inventory_data[n] //pc_delitem would fail on this case.
  5275. )
  5276. return false;
  5277. if( sd->inventory.u.items_inventory[n].equipSwitch )
  5278. return false;
  5279. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  5280. {
  5281. clif_displaymessage (sd->fd, msg_txt(sd,271));
  5282. return false; //Can't drop items in nodrop mapflag maps.
  5283. }
  5284. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  5285. {
  5286. clif_displaymessage (sd->fd, msg_txt(sd,263));
  5287. return false;
  5288. }
  5289. // bypass drop restriction in map_addflooritem because we've already checked it above
  5290. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2|4, 0))
  5291. return false;
  5292. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  5293. clif_dropitem(sd, n, amount);
  5294. return true;
  5295. }
  5296. /*==========================================
  5297. * Attempt to pick up an item.
  5298. * @param sd
  5299. * @param fitem Item that will be picked
  5300. * @return False = fail; True = success
  5301. *------------------------------------------*/
  5302. bool pc_takeitem(map_session_data *sd,struct flooritem_data *fitem)
  5303. {
  5304. int flag = 0;
  5305. t_tick tick = gettick();
  5306. struct party_data *p = NULL;
  5307. nullpo_ret(sd);
  5308. nullpo_ret(fitem);
  5309. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  5310. return false; // Distance is too far
  5311. if (sd->sc.cant.pickup)
  5312. return false;
  5313. if (sd->status.party_id)
  5314. p = party_search(sd->status.party_id);
  5315. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  5316. map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  5317. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  5318. if (!(p && p->party.item&1 &&
  5319. first_sd && first_sd->status.party_id == sd->status.party_id
  5320. ))
  5321. return false;
  5322. }
  5323. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  5324. map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  5325. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  5326. if (!(p && p->party.item&1 &&
  5327. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5328. (second_sd && second_sd->status.party_id == sd->status.party_id))
  5329. ))
  5330. return false;
  5331. }
  5332. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  5333. map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  5334. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  5335. if(!(p && p->party.item&1 &&
  5336. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5337. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  5338. (third_sd && third_sd->status.party_id == sd->status.party_id))
  5339. ))
  5340. return false;
  5341. }
  5342. }
  5343. }
  5344. }
  5345. //This function takes care of giving the item to whoever should have it, considering party-share options.
  5346. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  5347. clif_additem(sd,0,0,flag);
  5348. return true;
  5349. }
  5350. //Display pickup animation.
  5351. pc_stop_attack(sd);
  5352. clif_takeitem(&sd->bl,&fitem->bl);
  5353. if (fitem->mob_id &&
  5354. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  5355. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  5356. )
  5357. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  5358. map_clearflooritem(&fitem->bl);
  5359. return true;
  5360. }
  5361. /*==========================================
  5362. * Check if item is usable.
  5363. * Return:
  5364. * false = no
  5365. * true = yes
  5366. *------------------------------------------*/
  5367. bool pc_isUseitem(map_session_data *sd,int n)
  5368. {
  5369. struct item_data *item;
  5370. t_itemid nameid;
  5371. nullpo_ret(sd);
  5372. item = sd->inventory_data[n];
  5373. nameid = sd->inventory.u.items_inventory[n].nameid;
  5374. if( item == NULL )
  5375. return false;
  5376. //Not consumable item
  5377. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  5378. return false;
  5379. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  5380. return true;
  5381. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5382. if(mapdata->getMapFlag(MF_NOITEMCONSUMPTION)) //consumable but mapflag prevent it
  5383. return false;
  5384. //Prevent mass item usage. [Skotlex]
  5385. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  5386. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  5387. )
  5388. return false;
  5389. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  5390. clif_msg(sd,ITEM_NOUSE_SITTING);
  5391. return false; // You cannot use this item while sitting.
  5392. }
  5393. if (sd->state.storage_flag && item->type != IT_CASH) {
  5394. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  5395. return false; // You cannot use this item while storage is open.
  5396. }
  5397. if (item->flag.dead_branch && (mapdata->getMapFlag(MF_NOBRANCH) || mapdata_flag_gvg2(mapdata)))
  5398. return false;
  5399. if( itemdb_group.item_exists( IG_MF_NOTELEPORT, nameid ) ){
  5400. if( ( mapdata->getMapFlag(MF_NOTELEPORT) || mapdata_flag_gvg2( mapdata ) ) ){
  5401. clif_skill_teleportmessage( sd, 0 );
  5402. return false;
  5403. }
  5404. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5405. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5406. return false;
  5407. }
  5408. }
  5409. if( itemdb_group.item_exists( IG_MF_NORETURN, nameid ) ){
  5410. if( mapdata->getMapFlag(MF_NORETURN) ){
  5411. return false;
  5412. }
  5413. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5414. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5415. return false;
  5416. }
  5417. }
  5418. if( itemdb_group.item_exists( IG_GIANT_FLY_WING, nameid ) ){
  5419. struct party_data *pd = party_search( sd->status.party_id );
  5420. if( pd ){
  5421. int i;
  5422. ARR_FIND( 0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader );
  5423. // User is not party leader
  5424. if( i == MAX_PARTY ){
  5425. clif_msg( sd, ITEM_PARTY_MEMBER_NOT_SUMMONED );
  5426. return false;
  5427. }
  5428. 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 ) );
  5429. // No party members found on same map
  5430. if( i == MAX_PARTY ){
  5431. clif_msg( sd, ITEM_PARTY_NO_MEMBER_IN_MAP );
  5432. return false;
  5433. }
  5434. }else{
  5435. clif_msg( sd, ITEM_PARTY_MEMBER_NOT_SUMMONED );
  5436. return false;
  5437. }
  5438. }
  5439. switch( nameid ) {
  5440. case ITEMID_MERCENARY_RED_POTION:
  5441. case ITEMID_MERCENARY_BLUE_POTION:
  5442. case ITEMID_M_CENTER_POTION:
  5443. case ITEMID_M_AWAKENING_POTION:
  5444. case ITEMID_M_BERSERK_POTION:
  5445. if( sd->md == NULL || sd->md->db == NULL )
  5446. return false;
  5447. if( sd->md->sc.cant.consume )
  5448. return false;
  5449. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  5450. return false;
  5451. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  5452. return false;
  5453. break;
  5454. case ITEMID_NEURALIZER:
  5455. if( !mapdata->getMapFlag(MF_RESET) )
  5456. return false;
  5457. break;
  5458. }
  5459. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  5460. return false; // Mercenary Scrolls
  5461. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  5462. if( pc_is90overweight(sd) ) {
  5463. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  5464. return false;
  5465. }
  5466. if( !pc_inventoryblank(sd) ) {
  5467. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  5468. return false;
  5469. }
  5470. }
  5471. //Gender check
  5472. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  5473. return false;
  5474. //Required level check
  5475. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  5476. return false;
  5477. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  5478. return false;
  5479. //Not equipable by class. [Skotlex]
  5480. if (!pc_job_can_use_item(sd,item))
  5481. return false;
  5482. if (sd->sc.cant.consume)
  5483. return false;
  5484. if (!pc_isItemClass(sd,item))
  5485. return false;
  5486. //Dead Branch items
  5487. if( item->flag.dead_branch )
  5488. log_branch(sd);
  5489. return true;
  5490. }
  5491. /*==========================================
  5492. * Last checks to use an item.
  5493. * Return:
  5494. * 0 = fail
  5495. * 1 = success
  5496. *------------------------------------------*/
  5497. int pc_useitem(map_session_data *sd,int n)
  5498. {
  5499. t_tick tick = gettick();
  5500. int amount;
  5501. t_itemid nameid;
  5502. struct script_code *script;
  5503. struct item item;
  5504. struct item_data *id;
  5505. nullpo_ret(sd);
  5506. if (sd->state.mail_writing)
  5507. return 0;
  5508. if (sd->npc_id) {
  5509. if (sd->progressbar.npc_id) {
  5510. clif_progressbar_abort(sd);
  5511. return 0; // First item use attempt cancels the progress bar
  5512. }
  5513. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  5514. #ifdef RENEWAL
  5515. clif_msg( sd, WORK_IN_PROGRESS );
  5516. #endif
  5517. return 0;
  5518. }
  5519. }
  5520. item = sd->inventory.u.items_inventory[n];
  5521. id = sd->inventory_data[n];
  5522. if (item.nameid == 0 || item.amount <= 0)
  5523. return 0;
  5524. if( !pc_isUseitem(sd,n) )
  5525. return 0;
  5526. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  5527. nameid = id->nameid;
  5528. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  5529. return 0;
  5530. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  5531. if( id->flag.delay_consume > 0 ) {
  5532. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.getSCE(SC_ALL_RIDING) )
  5533. return 0;
  5534. else if( pc_issit(sd) )
  5535. return 0;
  5536. }
  5537. //Since most delay-consume items involve using a "skill-type" target cursor,
  5538. //perform a skill-use check before going through. [Skotlex]
  5539. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  5540. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  5541. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  5542. return 0;
  5543. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  5544. return 0;
  5545. /* on restricted maps the item is consumed but the effect is not used */
  5546. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  5547. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  5548. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  5549. clif_useitemack(sd,n,item.amount-1,true);
  5550. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  5551. }
  5552. return 0;/* regardless, effect is not run */
  5553. }
  5554. if (pet_db_search(id->nameid, PET_CATCH) != nullptr && map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  5555. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  5556. return 0;
  5557. }
  5558. sd->itemid = item.nameid;
  5559. sd->itemindex = n;
  5560. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  5561. sd->catch_target_class = PET_CATCH_FAIL;
  5562. amount = item.amount;
  5563. script = id->script;
  5564. //Check if the item is to be consumed immediately [Skotlex]
  5565. if (id->flag.delay_consume > 0)
  5566. clif_useitemack(sd, n, amount, true);
  5567. else
  5568. {
  5569. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  5570. {
  5571. clif_useitemack(sd, n, amount - 1, true);
  5572. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  5573. }
  5574. else
  5575. clif_useitemack(sd, n, 0, false);
  5576. }
  5577. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  5578. potion_flag = 2; // Famous player's potions have 50% more efficiency
  5579. //Update item use time.
  5580. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  5581. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  5582. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  5583. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  5584. potion_flag = 0;
  5585. return 1;
  5586. }
  5587. /**
  5588. * Add item on cart for given index.
  5589. * @param sd
  5590. * @param item
  5591. * @param amount
  5592. * @param log_type
  5593. * @return See pc.hpp::e_additem_result
  5594. */
  5595. enum e_additem_result pc_cart_additem(map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  5596. {
  5597. struct item_data *data;
  5598. int i,w;
  5599. nullpo_retr(ADDITEM_INVALID, sd);
  5600. nullpo_retr(ADDITEM_INVALID, item);
  5601. if(item->nameid == 0 || amount <= 0)
  5602. return ADDITEM_INVALID;
  5603. if (itemdb_ishatched_egg(item))
  5604. return ADDITEM_INVALID;
  5605. data = itemdb_search(item->nameid);
  5606. if( data->stack.cart && amount > data->stack.amount )
  5607. {// item stack limitation
  5608. return ADDITEM_STACKLIMIT;
  5609. }
  5610. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  5611. { // Check item trade restrictions [Skotlex]
  5612. clif_displaymessage (sd->fd, msg_txt(sd,264));
  5613. return ADDITEM_INVALID;
  5614. }
  5615. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  5616. return ADDITEM_OVERWEIGHT;
  5617. i = MAX_CART;
  5618. if( itemdb_isstackable2(data) && !item->expire_time )
  5619. {
  5620. for (i = 0; i < MAX_CART; i++) {
  5621. if (sd->cart.u.items_cart[i].nameid == item->nameid
  5622. && sd->cart.u.items_cart[i].bound == item->bound
  5623. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5624. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5625. )
  5626. break;
  5627. }
  5628. }
  5629. if( i < MAX_CART )
  5630. {// item already in cart, stack it
  5631. 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 ) )
  5632. return ADDITEM_OVERAMOUNT; // no slot
  5633. sd->cart.u.items_cart[i].amount += amount;
  5634. clif_cart_additem(sd,i,amount);
  5635. }
  5636. else
  5637. {// item not stackable or not present, add it
  5638. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5639. if( i == MAX_CART )
  5640. return ADDITEM_OVERAMOUNT; // no slot
  5641. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5642. sd->cart.u.items_cart[i].id = 0;
  5643. sd->cart.u.items_cart[i].amount = amount;
  5644. sd->cart_num++;
  5645. clif_cart_additem(sd,i,amount);
  5646. }
  5647. sd->cart.u.items_cart[i].favorite = 0; // clear
  5648. sd->cart.u.items_cart[i].equipSwitch = 0;
  5649. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5650. sd->cart_weight += w;
  5651. clif_updatestatus(sd,SP_CARTINFO);
  5652. return ADDITEM_SUCCESS;
  5653. }
  5654. /*==========================================
  5655. * Delete item on cart for given index.
  5656. *------------------------------------------*/
  5657. void pc_cart_delitem(map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  5658. {
  5659. nullpo_retv(sd);
  5660. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5661. sd->cart.u.items_cart[n].amount < amount)
  5662. return;
  5663. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5664. sd->cart.u.items_cart[n].amount -= amount;
  5665. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5666. if(sd->cart.u.items_cart[n].amount <= 0) {
  5667. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5668. sd->cart_num--;
  5669. }
  5670. if(!type) {
  5671. clif_cart_delitem(sd,n,amount);
  5672. clif_updatestatus(sd,SP_CARTINFO);
  5673. }
  5674. }
  5675. /*==========================================
  5676. * Transfer item from inventory to cart.
  5677. *------------------------------------------*/
  5678. void pc_putitemtocart(map_session_data *sd,int idx,int amount)
  5679. {
  5680. nullpo_retv(sd);
  5681. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5682. return;
  5683. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5684. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5685. return;
  5686. if( item_data->equipSwitch ){
  5687. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  5688. return;
  5689. }
  5690. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5691. if (flag == ADDITEM_SUCCESS)
  5692. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5693. else {
  5694. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  5695. clif_additem(sd, idx, amount, 0);
  5696. clif_delitem(sd, idx, amount, 0);
  5697. }
  5698. }
  5699. /*==========================================
  5700. * Get number of item in cart.
  5701. * Return:
  5702. -1 = itemid not found or no amount found
  5703. x = remaining itemid on cart after get
  5704. *------------------------------------------*/
  5705. int pc_cartitem_amount(map_session_data* sd, int idx, int amount)
  5706. {
  5707. struct item* item_data;
  5708. nullpo_retr(-1, sd);
  5709. item_data = &sd->cart.u.items_cart[idx];
  5710. if( item_data->nameid == 0 || item_data->amount == 0 )
  5711. return -1;
  5712. return item_data->amount - amount;
  5713. }
  5714. /*==========================================
  5715. * Retrieve an item at index idx from cart.
  5716. *------------------------------------------*/
  5717. void pc_getitemfromcart(map_session_data *sd,int idx,int amount)
  5718. {
  5719. nullpo_retv(sd);
  5720. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5721. return;
  5722. struct item *item_data=&sd->cart.u.items_cart[idx];
  5723. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5724. return;
  5725. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5726. if (flag == ADDITEM_SUCCESS)
  5727. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5728. else {
  5729. clif_cart_delitem(sd, idx, amount);
  5730. clif_additem(sd, idx, amount, flag);
  5731. clif_cart_additem(sd, idx, amount);
  5732. }
  5733. }
  5734. /*==========================================
  5735. * Bound Item Check
  5736. * Type:
  5737. * 1 Account Bound
  5738. * 2 Guild Bound
  5739. * 3 Party Bound
  5740. * 4 Character Bound
  5741. *------------------------------------------*/
  5742. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  5743. {
  5744. int i = 0, j = 0;
  5745. for(i = 0; i < MAX_INVENTORY; i++) {
  5746. 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) {
  5747. idxlist[j] = i;
  5748. j++;
  5749. }
  5750. }
  5751. return j;
  5752. }
  5753. /*==========================================
  5754. * Display item stolen msg to player sd
  5755. *------------------------------------------*/
  5756. int pc_show_steal(struct block_list *bl,va_list ap)
  5757. {
  5758. map_session_data *sd;
  5759. t_itemid itemid;
  5760. char output[100];
  5761. sd=va_arg(ap,map_session_data *);
  5762. itemid=va_arg(ap,int);
  5763. std::shared_ptr<item_data> id = item_db.find(itemid);
  5764. if(id == nullptr)
  5765. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5766. else
  5767. sprintf(output,"%s stole %s.",sd->status.name,id->ename.c_str());
  5768. clif_displaymessage( ((map_session_data *)bl)->fd, output);
  5769. return 0;
  5770. }
  5771. /**
  5772. * Steal an item from bl (mob).
  5773. * @param sd: Player data
  5774. * @param bl: Object to steal from
  5775. * @param skill_lv: Level of skill used
  5776. * @return True on success or false otherwise
  5777. */
  5778. bool pc_steal_item(map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5779. {
  5780. int i;
  5781. t_itemid itemid;
  5782. double rate;
  5783. unsigned char flag = 0;
  5784. struct status_data *sd_status, *md_status;
  5785. struct mob_data *md;
  5786. if(!sd || !bl || bl->type!=BL_MOB)
  5787. return false;
  5788. md = (TBL_MOB *)bl;
  5789. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5790. return false;
  5791. sd_status= status_get_status_data(&sd->bl);
  5792. md_status= status_get_status_data(bl);
  5793. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5794. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5795. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5796. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5797. ) { //Can't steal from
  5798. md->state.steal_flag = UCHAR_MAX;
  5799. return false;
  5800. }
  5801. // base skill success chance (percentual)
  5802. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5803. rate += sd->bonus.add_steal_rate;
  5804. if( rate < 1
  5805. #ifdef RENEWAL
  5806. || rnd()%100 >= rate
  5807. #endif
  5808. )
  5809. return false;
  5810. // Try dropping one item, in the order from first to last possible slot.
  5811. // Droprate is affected by the skill success rate.
  5812. for( i = 0; i < MAX_MOB_DROP; i++ )
  5813. if( item_db.exists(md->db->dropitem[i].nameid) && !md->db->dropitem[i].steal_protected && rnd() % 10000 < md->db->dropitem[i].rate
  5814. #ifndef RENEWAL
  5815. * rate/100.
  5816. #endif
  5817. )
  5818. break;
  5819. if( i == MAX_MOB_DROP )
  5820. return false;
  5821. itemid = md->db->dropitem[i].nameid;
  5822. struct item tmp_item = {};
  5823. tmp_item.nameid = itemid;
  5824. tmp_item.amount = 1;
  5825. tmp_item.identify = itemdb_isidentified(itemid);
  5826. if( battle_config.skill_steal_random_options ){
  5827. mob_setdropitem_option( &tmp_item, &md->db->dropitem[i] );
  5828. }
  5829. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5830. //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?
  5831. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5832. if(flag) { //Failed to steal due to overweight
  5833. clif_additem(sd,0,0,flag);
  5834. return false;
  5835. }
  5836. if(battle_config.show_steal_in_same_party)
  5837. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5838. //Logs items, Stolen from mobs [Lupus]
  5839. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5840. //A Rare Steal Global Announce by Lupus
  5841. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5842. struct item_data *i_data;
  5843. char message[128];
  5844. i_data = itemdb_search(itemid);
  5845. 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);
  5846. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5847. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5848. }
  5849. return true;
  5850. }
  5851. /*==========================================
  5852. * Stole zeny from bl (mob)
  5853. * return
  5854. * 0 = fail
  5855. * 1 = success
  5856. *------------------------------------------*/
  5857. int pc_steal_coin(map_session_data *sd,struct block_list *target)
  5858. {
  5859. int rate, target_lv;
  5860. struct mob_data *md;
  5861. if(!sd || !target || target->type != BL_MOB)
  5862. return 0;
  5863. md = (TBL_MOB*)target;
  5864. target_lv = status_get_lv(target);
  5865. if (md->state.steal_coin_flag || md->sc.getSCE(SC_STONE) || md->sc.getSCE(SC_FREEZE) || md->sc.getSCE(SC_HANDICAPSTATE_FROSTBITE) ||
  5866. md->sc.getSCE(SC_HANDICAPSTATE_SWOONING) || md->sc.getSCE(SC_HANDICAPSTATE_LIGHTNINGSTRIKE) || md->sc.getSCE(SC_HANDICAPSTATE_CRYSTALLIZATION) ||
  5867. status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5868. return 0;
  5869. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5870. if(rnd()%1000 < rate)
  5871. {
  5872. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5873. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5874. pc_getzeny(sd, amount, LOG_TYPE_STEAL);
  5875. md->state.steal_coin_flag = 1;
  5876. return 1;
  5877. }
  5878. return 0;
  5879. }
  5880. /*==========================================
  5881. * Set's a player position.
  5882. * @param sd
  5883. * @param mapindex
  5884. * @param x
  5885. * @param y
  5886. * @param clrtype
  5887. * @return SETPOS_OK Success
  5888. * SETPOS_MAPINDEX Invalid map index
  5889. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5890. * SETPOS_AUTOTRADE Player is in autotrade state
  5891. *------------------------------------------*/
  5892. enum e_setpos pc_setpos(map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5893. {
  5894. nullpo_retr(SETPOS_OK,sd);
  5895. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5896. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5897. return SETPOS_MAPINDEX;
  5898. }
  5899. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5900. return SETPOS_AUTOTRADE;
  5901. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5902. pc_setstand(sd, true);
  5903. pc_setrestartvalue(sd,1);
  5904. }
  5905. int16 m = map_mapindex2mapid(mapindex);
  5906. struct map_data *mapdata = map_getmapdata(m);
  5907. status_change *sc = status_get_sc(&sd->bl);
  5908. sd->state.changemap = (sd->mapindex != mapindex);
  5909. sd->state.warping = 1;
  5910. sd->state.workinprogress = WIP_DISABLE_NONE;
  5911. sd->state.mail_writing = false;
  5912. sd->state.refineui_open = false;
  5913. if( sd->state.changemap ) { // Misc map-changing settings
  5914. int curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5915. if (curr_map_instance_id != new_map_instance_id) {
  5916. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5917. instance_delusers(curr_map_instance_id);
  5918. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5919. }
  5920. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5921. instance_addusers(new_map_instance_id);
  5922. }
  5923. if (sd->bg_id && mapdata && !mapdata->getMapFlag(MF_BATTLEGROUND)) // Moving to a map that isn't a Battlegrounds
  5924. bg_team_leave(sd, false, true);
  5925. sd->state.pmap = sd->bl.m;
  5926. if (sc && sc->count) { // Cancel some map related stuff.
  5927. if (sc->cant.warp)
  5928. return SETPOS_MAPINDEX; // You may not get out!
  5929. for (const auto &it : status_db) {
  5930. if (sc->getSCE(it.first)) {
  5931. if (it.second->flag[SCF_REMOVEONMAPWARP])
  5932. status_change_end(&sd->bl, static_cast<sc_type>(it.first));
  5933. if (it.second->flag[SCF_RESTARTONMAPWARP] && it.second->skill_id > 0) {
  5934. status_change_entry *sce = sd->sc.getSCE(it.first);
  5935. if (sce->timer != INVALID_TIMER)
  5936. delete_timer(sce->timer, status_change_timer);
  5937. sce->timer = add_timer(gettick() + skill_get_time(it.second->skill_id, sce->val1), status_change_timer, sd->bl.id, it.first);
  5938. }
  5939. }
  5940. }
  5941. }
  5942. for(int i = 0; i < EQI_MAX; i++ ) {
  5943. if( sd->equip_index[i] >= 0 )
  5944. if( pc_isequip(sd,sd->equip_index[i]) )
  5945. pc_unequipitem(sd,sd->equip_index[i],2);
  5946. }
  5947. if (battle_config.clear_unit_onwarp&BL_PC)
  5948. skill_clear_unitgroup(&sd->bl);
  5949. if( battle_config.loose_ap_on_map && mapdata_flag_vs( mapdata ) ){
  5950. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  5951. }
  5952. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5953. guild_send_dot_remove(sd);
  5954. bg_send_dot_remove(sd);
  5955. if (sd->regen.state.gc)
  5956. sd->regen.state.gc = 0;
  5957. if (mapdata) {
  5958. // make sure vending is allowed here
  5959. if (sd->state.vending && mapdata->getMapFlag(MF_NOVENDING)) {
  5960. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5961. vending_closevending(sd);
  5962. }
  5963. // make sure buyingstore is allowed here
  5964. if (sd->state.buyingstore && mapdata->getMapFlag(MF_NOBUYINGSTORE)) {
  5965. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5966. buyingstore_close(sd);
  5967. }
  5968. }
  5969. channel_pcquit(sd,4); //quit map chan
  5970. // Remove Cloaked NPCs on map change
  5971. sd->cloaked_npc.clear();
  5972. }
  5973. if( m < 0 )
  5974. {
  5975. uint32 ip;
  5976. uint16 port;
  5977. struct script_state *st;
  5978. //if can't find any map-servers, just abort setting position.
  5979. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5980. return SETPOS_NO_MAPSERVER;
  5981. if (sd->npc_id){
  5982. npc_event_dequeue(sd,false);
  5983. st = sd->st;
  5984. }else{
  5985. st = nullptr;
  5986. }
  5987. if (sd->bg_id) // Switching map servers, remove from bg
  5988. bg_team_leave(sd, false, true);
  5989. if (sd->state.vending) // Stop vending
  5990. vending_closevending(sd);
  5991. if (sd->state.buyingstore) // Stop buyingstore
  5992. buyingstore_close(sd);
  5993. npc_script_event(sd, NPCE_LOGOUT);
  5994. //remove from map, THEN change x/y coordinates
  5995. unit_remove_map_pc(sd,clrtype);
  5996. sd->mapindex = mapindex;
  5997. sd->bl.x=x;
  5998. sd->bl.y=y;
  5999. pc_clean_skilltree(sd);
  6000. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  6001. chrif_changemapserver(sd, ip, (short)port);
  6002. //Free session data from this map server [Kevin]
  6003. unit_free_pc(sd);
  6004. if( st ){
  6005. // Has to be done here, because otherwise unit_free_pc will free the stack already
  6006. st->state = END;
  6007. }
  6008. return SETPOS_OK;
  6009. }
  6010. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  6011. {
  6012. 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);
  6013. x = y = 0; // make it random
  6014. }
  6015. if( x == 0 && y == 0 ) { // pick a random walkable cell
  6016. int c=0;
  6017. do {
  6018. x = rnd()%(mapdata->xs-2)+1;
  6019. y = rnd()%(mapdata->ys-2)+1;
  6020. c++;
  6021. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  6022. 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));
  6023. return SETPOS_OK; //preventing warp
  6024. //break; //allow warp anyway
  6025. }
  6026. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  6027. }
  6028. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  6029. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  6030. vending_closevending(sd);
  6031. }
  6032. if (sd->state.buyingstore && map_getcell(m, x, y, CELL_CHKNOBUYINGSTORE)) {
  6033. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  6034. buyingstore_close(sd);
  6035. }
  6036. if(sd->bl.prev != NULL){
  6037. unit_remove_map_pc(sd,clrtype);
  6038. clif_changemap(sd,m,x,y); // [MouseJstr]
  6039. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  6040. sd->state.rewarp = 1;
  6041. sd->mapindex = mapindex;
  6042. sd->bl.m = m;
  6043. sd->bl.x = sd->ud.to_x = x;
  6044. sd->bl.y = sd->ud.to_y = y;
  6045. if( sd->status.guild_id > 0 && mapdata->getMapFlag(MF_GVG_CASTLE) )
  6046. { // Increased guild castle regen [Valaris]
  6047. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  6048. if(gc && gc->guild_id == sd->status.guild_id)
  6049. sd->regen.state.gc = 1;
  6050. }
  6051. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  6052. {
  6053. sd->pd->bl.m = m;
  6054. sd->pd->bl.x = sd->pd->ud.to_x = x;
  6055. sd->pd->bl.y = sd->pd->ud.to_y = y;
  6056. sd->pd->ud.dir = sd->ud.dir;
  6057. }
  6058. if( hom_is_active(sd->hd) )
  6059. {
  6060. sd->hd->bl.m = m;
  6061. sd->hd->bl.x = sd->hd->ud.to_x = x;
  6062. sd->hd->bl.y = sd->hd->ud.to_y = y;
  6063. sd->hd->ud.dir = sd->ud.dir;
  6064. }
  6065. if( sd->md )
  6066. {
  6067. sd->md->bl.m = m;
  6068. sd->md->bl.x = sd->md->ud.to_x = x;
  6069. sd->md->bl.y = sd->md->ud.to_y = y;
  6070. sd->md->ud.dir = sd->ud.dir;
  6071. }
  6072. if( sd->ed ) {
  6073. sd->ed->bl.m = m;
  6074. sd->ed->bl.x = sd->ed->ud.to_x = x;
  6075. sd->ed->bl.y = sd->ed->ud.to_y = y;
  6076. sd->ed->ud.dir = sd->ud.dir;
  6077. }
  6078. pc_cell_basilica(sd);
  6079. //check if we gonna be rewarped [lighta]
  6080. if(npc_check_areanpc(1,m,x,y,0)){
  6081. sd->count_rewarp++;
  6082. }
  6083. else
  6084. sd->count_rewarp = 0;
  6085. if (sd->state.vending)
  6086. vending_update(*sd);
  6087. if (sd->state.buyingstore)
  6088. buyingstore_update(*sd);
  6089. return SETPOS_OK;
  6090. }
  6091. enum e_setpos pc_setpos_savepoint( map_session_data& sd, clr_type clrtype ){
  6092. struct map_data *mapdata = map_getmapdata( sd.bl.m );
  6093. if( mapdata != nullptr && mapdata->getMapFlag(MF_NOSAVE) && mapdata->save.map ){
  6094. return pc_setpos( &sd, mapdata->save.map, mapdata->save.x, mapdata->save.y, clrtype );
  6095. }else{
  6096. return pc_setpos( &sd, mapindex_name2id( sd.status.save_point.map ), sd.status.save_point.x, sd.status.save_point.y, clrtype );
  6097. }
  6098. }
  6099. /*==========================================
  6100. * Warp player sd to random location on current map.
  6101. * May fail if no walkable cell found (1000 attempts).
  6102. * Return:
  6103. * 0 = Success
  6104. * 1,2,3 = Fail
  6105. *------------------------------------------*/
  6106. char pc_randomwarp(map_session_data *sd, clr_type type, bool ignore_mapflag)
  6107. {
  6108. int x,y,i=0;
  6109. nullpo_ret(sd);
  6110. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6111. if (mapdata->getMapFlag(MF_NOTELEPORT) && !ignore_mapflag) //Teleport forbidden
  6112. return 3;
  6113. do {
  6114. x = rnd()%(mapdata->xs-2)+1;
  6115. y = rnd()%(mapdata->ys-2)+1;
  6116. } 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);
  6117. if (i < 1000)
  6118. return pc_setpos(sd,mapdata->index,x,y,type);
  6119. return 3;
  6120. }
  6121. /*==========================================
  6122. * Records a memo point at sd's current position
  6123. * pos - entry to replace, (-1: shift oldest entry out)
  6124. *------------------------------------------*/
  6125. bool pc_memo(map_session_data* sd, int pos)
  6126. {
  6127. int skill;
  6128. nullpo_ret(sd);
  6129. // check mapflags
  6130. 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) ) {
  6131. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  6132. return false;
  6133. }
  6134. // check inputs
  6135. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  6136. return false; // invalid input
  6137. // check required skill level
  6138. skill = pc_checkskill(sd, AL_WARP);
  6139. if( skill < 1 ) {
  6140. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  6141. return false;
  6142. }
  6143. if( skill < 2 || skill - 2 < pos ) {
  6144. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  6145. return false;
  6146. }
  6147. if( pos == -1 )
  6148. {
  6149. uint8 i;
  6150. const char* mapname = map_mapid2mapname( sd->bl.m );
  6151. // prevent memo-ing the same map multiple times
  6152. ARR_FIND( 0, MAX_MEMOPOINTS, i, strncmp( sd->status.memo_point[i].map, mapname, sizeof( sd->status.memo_point[i].map ) ) == 0 );
  6153. memmove( &sd->status.memo_point[1], &sd->status.memo_point[0], ( u8min( i, MAX_MEMOPOINTS - 1 ) ) * sizeof( struct s_point_str ) );
  6154. pos = 0;
  6155. }
  6156. if( map_getmapdata(sd->bl.m)->instance_id ) {
  6157. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  6158. return false;
  6159. }
  6160. safestrncpy( sd->status.memo_point[pos].map, map_mapid2mapname( sd->bl.m ), sizeof( sd->status.memo_point[pos].map ) );
  6161. sd->status.memo_point[pos].x = sd->bl.x;
  6162. sd->status.memo_point[pos].y = sd->bl.y;
  6163. clif_skill_memomessage(sd, 0);
  6164. return true;
  6165. }
  6166. //
  6167. // Skills
  6168. //
  6169. /**
  6170. * Get the skill current cooldown for player.
  6171. * (get the db base cooldown for skill + player specific cooldown)
  6172. * @param sd : player pointer
  6173. * @param id : skill id
  6174. * @param lv : skill lv
  6175. * @return player skill cooldown
  6176. */
  6177. int pc_get_skillcooldown(map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  6178. if (skill_id == SJ_NOVAEXPLOSING) {
  6179. status_change *sc = status_get_sc(&sd->bl);
  6180. if (sc && sc->getSCE(SC_DIMENSION))
  6181. return 0;
  6182. }
  6183. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  6184. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  6185. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  6186. for (auto &it : sd->skillcooldown) {
  6187. if (it.id == skill_id) {
  6188. cooldown += it.val;
  6189. break;
  6190. }
  6191. }
  6192. return max(0, cooldown);
  6193. }
  6194. /*==========================================
  6195. * Return player sd skill_lv learned for given skill
  6196. *------------------------------------------*/
  6197. uint8 pc_checkskill(map_session_data *sd, uint16 skill_id)
  6198. {
  6199. uint16 idx = 0;
  6200. if (sd == NULL)
  6201. return 0;
  6202. #ifdef RENEWAL
  6203. if ((idx = skill_get_index(skill_id)) == 0) {
  6204. #else
  6205. if( ( idx = skill_db.get_index( skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__ ) ) == 0 ){
  6206. if( skill_id >= RK_ENCHANTBLADE ){
  6207. // Silently fail for now -> future update planned
  6208. return 0;
  6209. }
  6210. #endif
  6211. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  6212. return 0;
  6213. }
  6214. if (SKILL_CHK_GUILD(skill_id) ) {
  6215. if (sd->status.guild_id>0 && sd->guild)
  6216. return guild_checkskill(sd->guild->guild,skill_id);
  6217. return 0;
  6218. }
  6219. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  6220. }
  6221. /**
  6222. * Returns the flag of the given skill (when learned).
  6223. * @param sd: Player data
  6224. * @param skill_id: Skill to lookup
  6225. * @return Skill flag type
  6226. */
  6227. e_skill_flag pc_checkskill_flag(map_session_data &sd, uint16 skill_id) {
  6228. uint16 idx;
  6229. #ifdef RENEWAL
  6230. if ((idx = skill_get_index(skill_id)) == 0) {
  6231. #else
  6232. if ((idx = skill_db.get_index(skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__)) == 0) {
  6233. if (skill_id >= RK_ENCHANTBLADE) {
  6234. // Silently fail for now -> future update planned
  6235. return SKILL_FLAG_NONE;
  6236. }
  6237. #endif
  6238. ShowError("pc_checkskill_flag: Invalid skill id %d (char_id=%d).\n", skill_id, sd.status.char_id);
  6239. return SKILL_FLAG_NONE;
  6240. }
  6241. 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;
  6242. }
  6243. /**
  6244. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  6245. * @param sd: Player data
  6246. * @param type: Summoner Power Type
  6247. * @return Skill points invested
  6248. */
  6249. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  6250. if (sd == nullptr)
  6251. return 0;
  6252. uint8 count = 0;
  6253. switch (type) {
  6254. case SUMMONER_POWER_SEA:
  6255. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  6256. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  6257. break;
  6258. case SUMMONER_POWER_LAND:
  6259. 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) +
  6260. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  6261. break;
  6262. case SUMMONER_POWER_LIFE:
  6263. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  6264. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  6265. break;
  6266. }
  6267. return count;
  6268. }
  6269. /**
  6270. * Checks for Imperial Guard's passive skills.
  6271. * @param sd: Player data
  6272. * @param flag:
  6273. * Flag&1 = IG_SHIELD_MASTERY
  6274. * Flag&2 = IG_SPEAR_SWORD_M
  6275. */
  6276. uint8 pc_checkskill_imperial_guard(map_session_data *sd, short flag)
  6277. {
  6278. nullpo_retr(0, sd);
  6279. uint8 count = 0;
  6280. if (flag&1 && sd->status.shield > 0)
  6281. count += pc_checkskill(sd, IG_SHIELD_MASTERY);
  6282. if (flag&2 && (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_1HSPEAR || sd->status.weapon == W_2HSPEAR))
  6283. count += pc_checkskill(sd, IG_SPEAR_SWORD_M);
  6284. return count;
  6285. }
  6286. /**
  6287. * Check if we still have the correct weapon to continue the skill (actually status)
  6288. * If not ending it
  6289. * @param sd
  6290. * @return 0:error, 1:check done
  6291. */
  6292. static void pc_checkallowskill(map_session_data *sd)
  6293. {
  6294. nullpo_retv(sd);
  6295. if(!sd->sc.count)
  6296. return;
  6297. for (const auto &it : status_db) {
  6298. sc_type status = it.second->type;
  6299. std::bitset<SCF_MAX> flag = it.second->flag;
  6300. if (flag[SCF_REQUIREWEAPON]) { // Skills requiring specific weapon types
  6301. if (status == SC_DANCING && !battle_config.dancing_weaponswitch_fix)
  6302. continue;
  6303. if (sd->sc.getSCE(status) && !pc_check_weapontype(sd, skill_get_weapontype(it.second->skill_id)))
  6304. status_change_end(&sd->bl, status);
  6305. }
  6306. if (flag[SCF_REQUIRENOWEAPON]) {
  6307. if (sd->sc.getSCE(status) && sd->status.weapon)
  6308. status_change_end(&sd->bl, status, INVALID_TIMER);
  6309. }
  6310. if (flag[SCF_REQUIRESHIELD]) { // Skills requiring a shield
  6311. if (sd->sc.getSCE(status) && sd->status.shield <= 0)
  6312. status_change_end(&sd->bl, status);
  6313. }
  6314. }
  6315. }
  6316. /*==========================================
  6317. * Return equipped index of item on player sd at pos
  6318. * Return
  6319. * -1 : Nothing equipped
  6320. * idx : (this index could be used in inventory to found item_data)
  6321. *------------------------------------------*/
  6322. short pc_checkequip(map_session_data *sd,int pos, bool checkall)
  6323. {
  6324. uint8 i;
  6325. nullpo_retr(-1, sd);
  6326. for(i=0;i<EQI_MAX;i++){
  6327. if(pos & equip_bitmask[i]){
  6328. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  6329. // Check all if any match is found
  6330. continue;
  6331. }
  6332. return sd->equip_index[i];
  6333. }
  6334. }
  6335. return -1;
  6336. }
  6337. /*==========================================
  6338. * Check if sd has nameid equipped somewhere
  6339. * @sd : the player session
  6340. * @nameid : id of the item to check
  6341. * @min : : see pc.hpp enum equip_index from ? to @max
  6342. * @max : see pc.hpp enum equip_index for @min to ?
  6343. * -return true,false
  6344. *------------------------------------------*/
  6345. bool pc_checkequip2(map_session_data *sd, t_itemid nameid, int min, int max)
  6346. {
  6347. int i;
  6348. for(i = min; i < max; i++) {
  6349. if(equip_bitmask[i]) {
  6350. int idx = sd->equip_index[i];
  6351. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  6352. return true;
  6353. }
  6354. }
  6355. return false;
  6356. }
  6357. /*==========================================
  6358. * Convert's from the client's lame Job ID system
  6359. * to the map server's 'makes sense' system. [Skotlex]
  6360. *------------------------------------------*/
  6361. uint64 pc_jobid2mapid(unsigned short b_class)
  6362. {
  6363. switch(b_class)
  6364. {
  6365. //Novice And 1-1 Jobs
  6366. case JOB_NOVICE: return MAPID_NOVICE;
  6367. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  6368. case JOB_MAGE: return MAPID_MAGE;
  6369. case JOB_ARCHER: return MAPID_ARCHER;
  6370. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  6371. case JOB_MERCHANT: return MAPID_MERCHANT;
  6372. case JOB_THIEF: return MAPID_THIEF;
  6373. case JOB_TAEKWON: return MAPID_TAEKWON;
  6374. case JOB_WEDDING: return MAPID_WEDDING;
  6375. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  6376. case JOB_NINJA: return MAPID_NINJA;
  6377. case JOB_XMAS: return MAPID_XMAS;
  6378. case JOB_SUMMER: return MAPID_SUMMER;
  6379. case JOB_HANBOK: return MAPID_HANBOK;
  6380. case JOB_GANGSI: return MAPID_GANGSI;
  6381. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  6382. case JOB_SUMMER2: return MAPID_SUMMER2;
  6383. //2-1 Jobs
  6384. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  6385. case JOB_KNIGHT: return MAPID_KNIGHT;
  6386. case JOB_WIZARD: return MAPID_WIZARD;
  6387. case JOB_HUNTER: return MAPID_HUNTER;
  6388. case JOB_PRIEST: return MAPID_PRIEST;
  6389. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  6390. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  6391. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  6392. case JOB_KAGEROU:
  6393. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  6394. case JOB_REBELLION: return MAPID_REBELLION;
  6395. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  6396. //2-2 Jobs
  6397. case JOB_CRUSADER: return MAPID_CRUSADER;
  6398. case JOB_SAGE: return MAPID_SAGE;
  6399. case JOB_BARD:
  6400. case JOB_DANCER: return MAPID_BARDDANCER;
  6401. case JOB_MONK: return MAPID_MONK;
  6402. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  6403. case JOB_ROGUE: return MAPID_ROGUE;
  6404. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  6405. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  6406. //Trans Novice And Trans 1-1 Jobs
  6407. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  6408. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  6409. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  6410. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  6411. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  6412. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  6413. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  6414. //Trans 2-1 Jobs
  6415. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  6416. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  6417. case JOB_SNIPER: return MAPID_SNIPER;
  6418. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  6419. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  6420. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  6421. //Trans 2-2 Jobs
  6422. case JOB_PALADIN: return MAPID_PALADIN;
  6423. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  6424. case JOB_CLOWN:
  6425. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  6426. case JOB_CHAMPION: return MAPID_CHAMPION;
  6427. case JOB_CREATOR: return MAPID_CREATOR;
  6428. case JOB_STALKER: return MAPID_STALKER;
  6429. //Baby Novice And Baby 1-1 Jobs
  6430. case JOB_BABY: return MAPID_BABY;
  6431. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  6432. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  6433. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  6434. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  6435. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  6436. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  6437. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  6438. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  6439. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  6440. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  6441. //Baby 2-1 Jobs
  6442. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  6443. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  6444. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  6445. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  6446. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  6447. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  6448. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  6449. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  6450. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  6451. case JOB_BABY_KAGEROU:
  6452. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  6453. //Baby 2-2 Jobs
  6454. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  6455. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  6456. case JOB_BABY_BARD:
  6457. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  6458. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  6459. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  6460. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  6461. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  6462. //3-1 Jobs
  6463. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  6464. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  6465. case JOB_WARLOCK: return MAPID_WARLOCK;
  6466. case JOB_RANGER: return MAPID_RANGER;
  6467. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  6468. case JOB_MECHANIC: return MAPID_MECHANIC;
  6469. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  6470. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  6471. //3-2 Jobs
  6472. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  6473. case JOB_SORCERER: return MAPID_SORCERER;
  6474. case JOB_MINSTREL:
  6475. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  6476. case JOB_SURA: return MAPID_SURA;
  6477. case JOB_GENETIC: return MAPID_GENETIC;
  6478. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  6479. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  6480. //Trans 3-1 Jobs
  6481. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  6482. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  6483. case JOB_RANGER_T: return MAPID_RANGER_T;
  6484. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  6485. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  6486. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  6487. //Trans 3-2 Jobs
  6488. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  6489. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  6490. case JOB_MINSTREL_T:
  6491. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  6492. case JOB_SURA_T: return MAPID_SURA_T;
  6493. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  6494. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  6495. //Baby 3-1 Jobs
  6496. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  6497. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  6498. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  6499. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  6500. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  6501. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  6502. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  6503. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  6504. //Baby 3-2 Jobs
  6505. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  6506. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  6507. case JOB_BABY_MINSTREL:
  6508. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  6509. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  6510. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  6511. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  6512. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  6513. //Doram Jobs
  6514. case JOB_SUMMONER: return MAPID_SUMMONER;
  6515. case JOB_SPIRIT_HANDLER: return MAPID_SPIRIT_HANDLER;
  6516. //4-1 Jobs
  6517. case JOB_HYPER_NOVICE: return MAPID_HYPER_NOVICE;
  6518. case JOB_DRAGON_KNIGHT: return MAPID_DRAGON_KNIGHT;
  6519. case JOB_ARCH_MAGE: return MAPID_ARCH_MAGE;
  6520. case JOB_WINDHAWK: return MAPID_WINDHAWK;
  6521. case JOB_CARDINAL: return MAPID_CARDINAL;
  6522. case JOB_MEISTER: return MAPID_MEISTER;
  6523. case JOB_SHADOW_CROSS: return MAPID_SHADOW_CROSS;
  6524. case JOB_SKY_EMPEROR: return MAPID_SKY_EMPEROR;
  6525. case JOB_NIGHT_WATCH: return MAPID_NIGHT_WATCH;
  6526. case JOB_SHINKIRO:
  6527. case JOB_SHIRANUI: return MAPID_SHINKIRO_SHIRANUI;
  6528. //4-2 Jobs
  6529. case JOB_IMPERIAL_GUARD: return MAPID_IMPERIAL_GUARD;
  6530. case JOB_ELEMENTAL_MASTER: return MAPID_ELEMENTAL_MASTER;
  6531. case JOB_INQUISITOR: return MAPID_INQUISITOR;
  6532. case JOB_TROUBADOUR:
  6533. case JOB_TROUVERE: return MAPID_TROUBADOURTROUVERE;
  6534. case JOB_BIOLO: return MAPID_BIOLO;
  6535. case JOB_ABYSS_CHASER: return MAPID_ABYSS_CHASER;
  6536. case JOB_SOUL_ASCETIC: return MAPID_SOUL_ASCETIC;
  6537. //Unknown
  6538. default:
  6539. return -1;
  6540. }
  6541. }
  6542. //Reverts the map-style class id to the client-style one.
  6543. int pc_mapid2jobid(uint64 class_, int sex)
  6544. {
  6545. switch(class_) {
  6546. //Novice And 1-1 Jobs
  6547. case MAPID_NOVICE: return JOB_NOVICE;
  6548. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  6549. case MAPID_MAGE: return JOB_MAGE;
  6550. case MAPID_ARCHER: return JOB_ARCHER;
  6551. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  6552. case MAPID_MERCHANT: return JOB_MERCHANT;
  6553. case MAPID_THIEF: return JOB_THIEF;
  6554. case MAPID_TAEKWON: return JOB_TAEKWON;
  6555. case MAPID_WEDDING: return JOB_WEDDING;
  6556. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  6557. case MAPID_NINJA: return JOB_NINJA;
  6558. case MAPID_XMAS: return JOB_XMAS;
  6559. case MAPID_SUMMER: return JOB_SUMMER;
  6560. case MAPID_HANBOK: return JOB_HANBOK;
  6561. case MAPID_GANGSI: return JOB_GANGSI;
  6562. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  6563. case MAPID_SUMMER2: return JOB_SUMMER2;
  6564. //2-1 Jobs
  6565. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  6566. case MAPID_KNIGHT: return JOB_KNIGHT;
  6567. case MAPID_WIZARD: return JOB_WIZARD;
  6568. case MAPID_HUNTER: return JOB_HUNTER;
  6569. case MAPID_PRIEST: return JOB_PRIEST;
  6570. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  6571. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  6572. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  6573. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  6574. case MAPID_REBELLION: return JOB_REBELLION;
  6575. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  6576. //2-2 Jobs
  6577. case MAPID_CRUSADER: return JOB_CRUSADER;
  6578. case MAPID_SAGE: return JOB_SAGE;
  6579. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  6580. case MAPID_MONK: return JOB_MONK;
  6581. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  6582. case MAPID_ROGUE: return JOB_ROGUE;
  6583. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  6584. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  6585. //Trans Novice And Trans 2-1 Jobs
  6586. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  6587. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  6588. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  6589. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  6590. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  6591. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  6592. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  6593. //Trans 2-1 Jobs
  6594. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  6595. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  6596. case MAPID_SNIPER: return JOB_SNIPER;
  6597. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  6598. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  6599. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  6600. //Trans 2-2 Jobs
  6601. case MAPID_PALADIN: return JOB_PALADIN;
  6602. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  6603. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  6604. case MAPID_CHAMPION: return JOB_CHAMPION;
  6605. case MAPID_CREATOR: return JOB_CREATOR;
  6606. case MAPID_STALKER: return JOB_STALKER;
  6607. //Baby Novice And Baby 1-1 Jobs
  6608. case MAPID_BABY: return JOB_BABY;
  6609. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  6610. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  6611. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  6612. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  6613. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  6614. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  6615. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  6616. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  6617. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  6618. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  6619. //Baby 2-1 Jobs
  6620. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  6621. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  6622. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  6623. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  6624. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  6625. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  6626. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  6627. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  6628. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  6629. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  6630. //Baby 2-2 Jobs
  6631. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  6632. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  6633. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  6634. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  6635. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  6636. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  6637. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  6638. //3-1 Jobs
  6639. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  6640. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  6641. case MAPID_WARLOCK: return JOB_WARLOCK;
  6642. case MAPID_RANGER: return JOB_RANGER;
  6643. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  6644. case MAPID_MECHANIC: return JOB_MECHANIC;
  6645. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  6646. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  6647. //3-2 Jobs
  6648. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  6649. case MAPID_SORCERER: return JOB_SORCERER;
  6650. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  6651. case MAPID_SURA: return JOB_SURA;
  6652. case MAPID_GENETIC: return JOB_GENETIC;
  6653. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  6654. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  6655. //Trans 3-1 Jobs
  6656. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  6657. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  6658. case MAPID_RANGER_T: return JOB_RANGER_T;
  6659. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  6660. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  6661. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  6662. //Trans 3-2 Jobs
  6663. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  6664. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  6665. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  6666. case MAPID_SURA_T: return JOB_SURA_T;
  6667. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  6668. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  6669. //Baby 3-1 Jobs
  6670. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  6671. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  6672. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  6673. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  6674. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  6675. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  6676. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  6677. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  6678. //Baby 3-2 Jobs
  6679. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  6680. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  6681. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  6682. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  6683. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  6684. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  6685. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  6686. //Doram Jobs
  6687. case MAPID_SUMMONER: return JOB_SUMMONER;
  6688. case MAPID_SPIRIT_HANDLER: return JOB_SPIRIT_HANDLER;
  6689. //4-1 Jobs
  6690. case MAPID_HYPER_NOVICE: return JOB_HYPER_NOVICE;
  6691. case MAPID_DRAGON_KNIGHT: return JOB_DRAGON_KNIGHT;
  6692. case MAPID_ARCH_MAGE: return JOB_ARCH_MAGE;
  6693. case MAPID_WINDHAWK: return JOB_WINDHAWK;
  6694. case MAPID_CARDINAL: return JOB_CARDINAL;
  6695. case MAPID_MEISTER: return JOB_MEISTER;
  6696. case MAPID_SHADOW_CROSS: return JOB_SHADOW_CROSS;
  6697. case MAPID_SKY_EMPEROR: return JOB_SKY_EMPEROR;
  6698. case MAPID_NIGHT_WATCH: return JOB_NIGHT_WATCH;
  6699. case MAPID_SHINKIRO_SHIRANUI: return sex?JOB_SHINKIRO:JOB_SHIRANUI;
  6700. //4-2 Jobs
  6701. case MAPID_IMPERIAL_GUARD: return JOB_IMPERIAL_GUARD;
  6702. case MAPID_ELEMENTAL_MASTER: return JOB_ELEMENTAL_MASTER;
  6703. case MAPID_INQUISITOR: return JOB_INQUISITOR;
  6704. case MAPID_TROUBADOURTROUVERE: return sex?JOB_TROUBADOUR:JOB_TROUVERE;
  6705. case MAPID_BIOLO: return JOB_BIOLO;
  6706. case MAPID_ABYSS_CHASER: return JOB_ABYSS_CHASER;
  6707. case MAPID_SOUL_ASCETIC: return JOB_SOUL_ASCETIC;
  6708. //Unknown
  6709. default:
  6710. return -1;
  6711. }
  6712. }
  6713. /*====================================================
  6714. * This function return the name of the job (by [Yor])
  6715. *----------------------------------------------------*/
  6716. const char* job_name(int class_)
  6717. {
  6718. switch (class_) {
  6719. case JOB_NOVICE:
  6720. case JOB_SWORDMAN:
  6721. case JOB_MAGE:
  6722. case JOB_ARCHER:
  6723. case JOB_ACOLYTE:
  6724. case JOB_MERCHANT:
  6725. case JOB_THIEF:
  6726. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  6727. case JOB_KNIGHT:
  6728. case JOB_PRIEST:
  6729. case JOB_WIZARD:
  6730. case JOB_BLACKSMITH:
  6731. case JOB_HUNTER:
  6732. case JOB_ASSASSIN:
  6733. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  6734. case JOB_KNIGHT2:
  6735. return msg_txt(NULL,557);
  6736. case JOB_CRUSADER:
  6737. case JOB_MONK:
  6738. case JOB_SAGE:
  6739. case JOB_ROGUE:
  6740. case JOB_ALCHEMIST:
  6741. case JOB_BARD:
  6742. case JOB_DANCER:
  6743. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  6744. case JOB_CRUSADER2:
  6745. return msg_txt(NULL,563);
  6746. case JOB_WEDDING:
  6747. case JOB_SUPER_NOVICE:
  6748. case JOB_GUNSLINGER:
  6749. case JOB_NINJA:
  6750. case JOB_XMAS:
  6751. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  6752. case JOB_SUMMER:
  6753. case JOB_SUMMER2:
  6754. return msg_txt(NULL,621);
  6755. case JOB_HANBOK:
  6756. return msg_txt(NULL,694);
  6757. case JOB_OKTOBERFEST:
  6758. return msg_txt(NULL,696);
  6759. case JOB_NOVICE_HIGH:
  6760. case JOB_SWORDMAN_HIGH:
  6761. case JOB_MAGE_HIGH:
  6762. case JOB_ARCHER_HIGH:
  6763. case JOB_ACOLYTE_HIGH:
  6764. case JOB_MERCHANT_HIGH:
  6765. case JOB_THIEF_HIGH:
  6766. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  6767. case JOB_LORD_KNIGHT:
  6768. case JOB_HIGH_PRIEST:
  6769. case JOB_HIGH_WIZARD:
  6770. case JOB_WHITESMITH:
  6771. case JOB_SNIPER:
  6772. case JOB_ASSASSIN_CROSS:
  6773. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  6774. case JOB_LORD_KNIGHT2:
  6775. return msg_txt(NULL,582);
  6776. case JOB_PALADIN:
  6777. case JOB_CHAMPION:
  6778. case JOB_PROFESSOR:
  6779. case JOB_STALKER:
  6780. case JOB_CREATOR:
  6781. case JOB_CLOWN:
  6782. case JOB_GYPSY:
  6783. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  6784. case JOB_PALADIN2:
  6785. return msg_txt(NULL,588);
  6786. case JOB_BABY:
  6787. case JOB_BABY_SWORDMAN:
  6788. case JOB_BABY_MAGE:
  6789. case JOB_BABY_ARCHER:
  6790. case JOB_BABY_ACOLYTE:
  6791. case JOB_BABY_MERCHANT:
  6792. case JOB_BABY_THIEF:
  6793. return msg_txt(NULL,595 - JOB_BABY + class_);
  6794. case JOB_BABY_KNIGHT:
  6795. case JOB_BABY_PRIEST:
  6796. case JOB_BABY_WIZARD:
  6797. case JOB_BABY_BLACKSMITH:
  6798. case JOB_BABY_HUNTER:
  6799. case JOB_BABY_ASSASSIN:
  6800. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  6801. case JOB_BABY_KNIGHT2:
  6802. return msg_txt(NULL,602);
  6803. case JOB_BABY_CRUSADER:
  6804. case JOB_BABY_MONK:
  6805. case JOB_BABY_SAGE:
  6806. case JOB_BABY_ROGUE:
  6807. case JOB_BABY_ALCHEMIST:
  6808. case JOB_BABY_BARD:
  6809. case JOB_BABY_DANCER:
  6810. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  6811. case JOB_BABY_CRUSADER2:
  6812. return msg_txt(NULL,608);
  6813. case JOB_SUPER_BABY:
  6814. return msg_txt(NULL,615);
  6815. case JOB_TAEKWON:
  6816. return msg_txt(NULL,616);
  6817. case JOB_STAR_GLADIATOR:
  6818. case JOB_STAR_GLADIATOR2:
  6819. return msg_txt(NULL,617);
  6820. case JOB_SOUL_LINKER:
  6821. return msg_txt(NULL,618);
  6822. case JOB_GANGSI:
  6823. case JOB_DEATH_KNIGHT:
  6824. case JOB_DARK_COLLECTOR:
  6825. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  6826. case JOB_RUNE_KNIGHT:
  6827. case JOB_WARLOCK:
  6828. case JOB_RANGER:
  6829. case JOB_ARCH_BISHOP:
  6830. case JOB_MECHANIC:
  6831. case JOB_GUILLOTINE_CROSS:
  6832. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  6833. case JOB_RUNE_KNIGHT_T:
  6834. case JOB_WARLOCK_T:
  6835. case JOB_RANGER_T:
  6836. case JOB_ARCH_BISHOP_T:
  6837. case JOB_MECHANIC_T:
  6838. case JOB_GUILLOTINE_CROSS_T:
  6839. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  6840. case JOB_ROYAL_GUARD:
  6841. case JOB_SORCERER:
  6842. case JOB_MINSTREL:
  6843. case JOB_WANDERER:
  6844. case JOB_SURA:
  6845. case JOB_GENETIC:
  6846. case JOB_SHADOW_CHASER:
  6847. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  6848. case JOB_ROYAL_GUARD_T:
  6849. case JOB_SORCERER_T:
  6850. case JOB_MINSTREL_T:
  6851. case JOB_WANDERER_T:
  6852. case JOB_SURA_T:
  6853. case JOB_GENETIC_T:
  6854. case JOB_SHADOW_CHASER_T:
  6855. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  6856. case JOB_RUNE_KNIGHT2:
  6857. case JOB_RUNE_KNIGHT_T2:
  6858. return msg_txt(NULL,625);
  6859. case JOB_ROYAL_GUARD2:
  6860. case JOB_ROYAL_GUARD_T2:
  6861. return msg_txt(NULL,631);
  6862. case JOB_RANGER2:
  6863. case JOB_RANGER_T2:
  6864. return msg_txt(NULL,627);
  6865. case JOB_MECHANIC2:
  6866. case JOB_MECHANIC_T2:
  6867. return msg_txt(NULL,629);
  6868. case JOB_BABY_RUNE_KNIGHT:
  6869. case JOB_BABY_WARLOCK:
  6870. case JOB_BABY_RANGER:
  6871. case JOB_BABY_ARCH_BISHOP:
  6872. case JOB_BABY_MECHANIC:
  6873. case JOB_BABY_GUILLOTINE_CROSS:
  6874. case JOB_BABY_ROYAL_GUARD:
  6875. case JOB_BABY_SORCERER:
  6876. case JOB_BABY_MINSTREL:
  6877. case JOB_BABY_WANDERER:
  6878. case JOB_BABY_SURA:
  6879. case JOB_BABY_GENETIC:
  6880. case JOB_BABY_SHADOW_CHASER:
  6881. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6882. case JOB_BABY_RUNE_KNIGHT2:
  6883. return msg_txt(NULL,638);
  6884. case JOB_BABY_ROYAL_GUARD2:
  6885. return msg_txt(NULL,644);
  6886. case JOB_BABY_RANGER2:
  6887. return msg_txt(NULL,640);
  6888. case JOB_BABY_MECHANIC2:
  6889. return msg_txt(NULL,642);
  6890. case JOB_SUPER_NOVICE_E:
  6891. case JOB_SUPER_BABY_E:
  6892. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  6893. case JOB_KAGEROU:
  6894. case JOB_OBORO:
  6895. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  6896. case JOB_REBELLION:
  6897. return msg_txt(NULL,695);
  6898. case JOB_SUMMONER:
  6899. return msg_txt(NULL,697);
  6900. case JOB_BABY_SUMMONER:
  6901. return msg_txt(NULL,698);
  6902. case JOB_BABY_NINJA:
  6903. return msg_txt(NULL,699);
  6904. case JOB_BABY_KAGEROU:
  6905. case JOB_BABY_OBORO:
  6906. case JOB_BABY_TAEKWON:
  6907. case JOB_BABY_STAR_GLADIATOR:
  6908. case JOB_BABY_SOUL_LINKER:
  6909. case JOB_BABY_GUNSLINGER:
  6910. case JOB_BABY_REBELLION:
  6911. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  6912. case JOB_BABY_STAR_GLADIATOR2:
  6913. return msg_txt(NULL,756);
  6914. case JOB_STAR_EMPEROR:
  6915. case JOB_SOUL_REAPER:
  6916. case JOB_BABY_STAR_EMPEROR:
  6917. case JOB_BABY_SOUL_REAPER:
  6918. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  6919. case JOB_STAR_EMPEROR2:
  6920. return msg_txt(NULL,782);
  6921. case JOB_BABY_STAR_EMPEROR2:
  6922. return msg_txt(NULL,784);
  6923. case JOB_DRAGON_KNIGHT:
  6924. case JOB_MEISTER:
  6925. case JOB_SHADOW_CROSS:
  6926. case JOB_ARCH_MAGE:
  6927. case JOB_CARDINAL:
  6928. case JOB_WINDHAWK:
  6929. case JOB_IMPERIAL_GUARD:
  6930. case JOB_BIOLO:
  6931. case JOB_ABYSS_CHASER:
  6932. case JOB_ELEMENTAL_MASTER:
  6933. case JOB_INQUISITOR:
  6934. case JOB_TROUBADOUR:
  6935. case JOB_TROUVERE:
  6936. return msg_txt( nullptr, 800 - JOB_DRAGON_KNIGHT + class_ );
  6937. case JOB_WINDHAWK2:
  6938. return msg_txt( nullptr, 805);
  6939. case JOB_MEISTER2:
  6940. return msg_txt( nullptr, 801 );
  6941. case JOB_DRAGON_KNIGHT2:
  6942. return msg_txt( nullptr, 800 );
  6943. case JOB_IMPERIAL_GUARD2:
  6944. return msg_txt( nullptr, 806 );
  6945. case JOB_SKY_EMPEROR:
  6946. case JOB_SOUL_ASCETIC:
  6947. case JOB_SHINKIRO:
  6948. case JOB_SHIRANUI:
  6949. case JOB_NIGHT_WATCH:
  6950. case JOB_HYPER_NOVICE:
  6951. case JOB_SPIRIT_HANDLER:
  6952. return msg_txt( nullptr, 813 - JOB_SKY_EMPEROR + class_ );
  6953. case JOB_SKY_EMPEROR2:
  6954. return msg_txt( nullptr, 813 );
  6955. default:
  6956. return msg_txt(NULL,655);
  6957. }
  6958. }
  6959. /*====================================================
  6960. * Timered function to make id follow a target.
  6961. * @id = bl.id (player only atm)
  6962. * target is define in sd->followtarget (bl.id)
  6963. * used by pc_follow
  6964. *----------------------------------------------------*/
  6965. TIMER_FUNC(pc_follow_timer){
  6966. map_session_data *sd;
  6967. struct block_list *tbl;
  6968. sd = map_id2sd(id);
  6969. nullpo_ret(sd);
  6970. if (sd->followtimer != tid){
  6971. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6972. sd->followtimer = INVALID_TIMER;
  6973. return 0;
  6974. }
  6975. sd->followtimer = INVALID_TIMER;
  6976. tbl = map_id2bl(sd->followtarget);
  6977. if (tbl == NULL || pc_isdead(sd))
  6978. {
  6979. pc_stop_following(sd);
  6980. return 0;
  6981. }
  6982. // either player or target is currently detached from map blocks (could be teleporting),
  6983. // but still connected to this map, so we'll just increment the timer and check back later
  6984. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  6985. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6986. {
  6987. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  6988. if (!check_distance_bl(&sd->bl, tbl, 5))
  6989. unit_walktobl(&sd->bl, tbl, 5, 0);
  6990. } else
  6991. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  6992. }
  6993. sd->followtimer = add_timer(
  6994. tick + 1000, // increase time a bit to loosen up map's load
  6995. pc_follow_timer, sd->bl.id, 0);
  6996. return 0;
  6997. }
  6998. int pc_stop_following (map_session_data *sd)
  6999. {
  7000. nullpo_ret(sd);
  7001. if (sd->followtimer != INVALID_TIMER) {
  7002. delete_timer(sd->followtimer,pc_follow_timer);
  7003. sd->followtimer = INVALID_TIMER;
  7004. }
  7005. sd->followtarget = -1;
  7006. sd->ud.target_to = 0;
  7007. unit_stop_walking(&sd->bl, 1);
  7008. return 0;
  7009. }
  7010. int pc_follow(map_session_data *sd,int target_id)
  7011. {
  7012. struct block_list *bl = map_id2bl(target_id);
  7013. if (bl == NULL /*|| bl->type != BL_PC*/)
  7014. return 1;
  7015. if (sd->followtimer != INVALID_TIMER)
  7016. pc_stop_following(sd);
  7017. sd->followtarget = target_id;
  7018. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7019. return 0;
  7020. }
  7021. int pc_checkbaselevelup(map_session_data *sd) {
  7022. t_exp next = pc_nextbaseexp(sd);
  7023. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  7024. return 0;
  7025. uint32 base_level = sd->status.base_level;
  7026. do {
  7027. sd->status.base_exp -= next;
  7028. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7029. 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 )
  7030. sd->status.base_exp = next-1;
  7031. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level);
  7032. sd->status.trait_point += statpoint_db.pc_gets_trait_point(sd->status.base_level);
  7033. sd->status.base_level++;
  7034. if( pc_is_maxbaselv(sd) ){
  7035. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  7036. break;
  7037. }
  7038. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  7039. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  7040. status_calc_pet(sd->pd,SCO_NONE);
  7041. clif_updatestatus(sd,SP_STATUSPOINT);
  7042. clif_updatestatus(sd,SP_TRAITPOINT);
  7043. clif_updatestatus(sd,SP_BASELEVEL);
  7044. clif_updatestatus(sd,SP_BASEEXP);
  7045. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7046. status_calc_pc(sd,SCO_FORCE);
  7047. status_percent_heal(&sd->bl,100,100);
  7048. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7049. for (const auto &status : status_db) {
  7050. if (status.second->flag[SCF_SUPERNOVICEANGEL])
  7051. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 1, skill_get_time(status.second->skill_id, 1));
  7052. }
  7053. if (sd->state.snovice_dead_flag)
  7054. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  7055. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  7056. for (const auto &status : status_db) {
  7057. if (status.second->flag[SCF_TAEKWONANGEL])
  7058. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 10, 600000);
  7059. }
  7060. }
  7061. clif_misceffect(&sd->bl,0);
  7062. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  7063. if(sd->status.party_id)
  7064. party_send_levelup(sd);
  7065. pc_baselevelchanged(sd);
  7066. for (; base_level <= sd->status.base_level; base_level++) {
  7067. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  7068. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  7069. }
  7070. return 1;
  7071. }
  7072. void pc_baselevelchanged(map_session_data *sd) {
  7073. uint8 i;
  7074. for( i = 0; i < EQI_MAX; i++ ) {
  7075. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  7076. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  7077. pc_unequipitem(sd, sd->equip_index[i], 3);
  7078. }
  7079. }
  7080. pc_show_questinfo(sd);
  7081. }
  7082. int pc_checkjoblevelup(map_session_data *sd)
  7083. {
  7084. t_exp next = pc_nextjobexp(sd);
  7085. nullpo_ret(sd);
  7086. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  7087. return 0;
  7088. uint32 job_level = sd->status.job_level;
  7089. do {
  7090. sd->status.job_exp -= next;
  7091. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7092. 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 )
  7093. sd->status.job_exp = next-1;
  7094. sd->status.job_level ++;
  7095. sd->status.skill_point ++;
  7096. if( pc_is_maxjoblv(sd) ){
  7097. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  7098. break;
  7099. }
  7100. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  7101. clif_updatestatus(sd,SP_JOBLEVEL);
  7102. clif_updatestatus(sd,SP_JOBEXP);
  7103. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7104. clif_updatestatus(sd,SP_SKILLPOINT);
  7105. status_calc_pc(sd,SCO_FORCE);
  7106. clif_misceffect(&sd->bl,1);
  7107. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7108. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7109. npc_script_event(sd, NPCE_JOBLVUP);
  7110. for (; job_level <= sd->status.job_level; job_level++)
  7111. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  7112. pc_show_questinfo(sd);
  7113. return 1;
  7114. }
  7115. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  7116. * @param sd Player
  7117. * @param base_exp Base EXP before peronal bonuses
  7118. * @param job_exp Job EXP before peronal bonuses
  7119. * @param src Block list that affecting the exp calculation
  7120. */
  7121. static void pc_calcexp(map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  7122. {
  7123. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  7124. if (src) {
  7125. struct status_data *status = status_get_status_data(src);
  7126. if( sd->indexed_bonus.expaddrace[status->race] )
  7127. bonus += sd->indexed_bonus.expaddrace[status->race];
  7128. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  7129. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  7130. if( sd->indexed_bonus.expaddclass[status->class_] )
  7131. bonus += sd->indexed_bonus.expaddclass[status->class_];
  7132. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  7133. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  7134. if (battle_config.pk_mode &&
  7135. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  7136. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  7137. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  7138. vip_bonus_base = battle_config.vip_base_exp_increase;
  7139. vip_bonus_job = battle_config.vip_job_exp_increase;
  7140. }
  7141. }
  7142. // Give EXPBOOST for quests even if src is NULL.
  7143. if (sd->sc.getSCE(SC_EXPBOOST)) {
  7144. bonus += sd->sc.getSCE(SC_EXPBOOST)->val1;
  7145. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  7146. bonus += (sd->sc.getSCE(SC_EXPBOOST)->val1 / battle_config.vip_bm_increase);
  7147. }
  7148. if (*base_exp) {
  7149. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  7150. *base_exp = cap_value(exp, 1, MAX_EXP);
  7151. }
  7152. // Give JEXPBOOST for quests even if src is NULL.
  7153. if (sd->sc.getSCE(SC_JEXPBOOST))
  7154. bonus += sd->sc.getSCE(SC_JEXPBOOST)->val1;
  7155. if (*job_exp) {
  7156. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  7157. *job_exp = cap_value(exp, 1, MAX_EXP);
  7158. }
  7159. return;
  7160. }
  7161. /**
  7162. * Show EXP gained by player in percentage by @showexp
  7163. * @param sd Player
  7164. * @param base_exp Base EXP gained/loss
  7165. * @param next_base_exp Base EXP needed for next base level
  7166. * @param job_exp Job EXP gained/loss
  7167. * @param next_job_exp Job EXP needed for next job level
  7168. * @param lost True:EXP penalty, lose EXP
  7169. **/
  7170. void pc_gainexp_disp(map_session_data *sd, t_exp base_exp, t_exp next_base_exp, t_exp job_exp, t_exp next_job_exp, bool lost) {
  7171. char output[CHAT_SIZE_MAX];
  7172. nullpo_retv(sd);
  7173. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  7174. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  7175. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  7176. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  7177. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  7178. }
  7179. /**
  7180. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  7181. * @param sd Player
  7182. * @param src EXP source
  7183. * @param base_exp Base EXP gained
  7184. * @param base_exp Job EXP gained
  7185. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  7186. * @return
  7187. **/
  7188. void pc_gainexp(map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  7189. {
  7190. t_exp nextb = 0, nextj = 0;
  7191. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  7192. nullpo_retv(sd);
  7193. if(sd->bl.prev == NULL || pc_isdead(sd))
  7194. return;
  7195. if (!(exp_flag&2)) {
  7196. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  7197. return; // no exp on pvp maps
  7198. if (sd->status.guild_id>0)
  7199. base_exp -= guild_payexp(sd,base_exp);
  7200. }
  7201. flag = ((base_exp) ? 1 : 0) |
  7202. ((job_exp) ? 2 : 0) |
  7203. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  7204. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  7205. if (!(exp_flag&2))
  7206. pc_calcexp(sd, &base_exp, &job_exp, src);
  7207. nextb = pc_nextbaseexp(sd);
  7208. nextj = pc_nextjobexp(sd);
  7209. if (flag&4){
  7210. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  7211. base_exp = 0;
  7212. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  7213. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  7214. }
  7215. if (flag&8){
  7216. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  7217. job_exp = 0;
  7218. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  7219. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  7220. }
  7221. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  7222. //Note that this value should never be greater than the original
  7223. //therefore no overflow checks are needed. [Skotlex]
  7224. if (nextb > 0) {
  7225. float nextbp = (float) base_exp / (float) nextb;
  7226. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  7227. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  7228. }
  7229. if (nextj > 0) {
  7230. float nextjp = (float) job_exp / (float) nextj;
  7231. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  7232. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  7233. }
  7234. }
  7235. // Give EXP for Base Level
  7236. if (base_exp) {
  7237. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  7238. if (!pc_checkbaselevelup(sd))
  7239. clif_updatestatus(sd,SP_BASEEXP);
  7240. }
  7241. // Give EXP for Job Level
  7242. if (job_exp) {
  7243. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  7244. if (!pc_checkjoblevelup(sd))
  7245. clif_updatestatus(sd,SP_JOBEXP);
  7246. }
  7247. if (flag&1)
  7248. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  7249. if (flag&2)
  7250. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  7251. if (sd->state.showexp && (base_exp || job_exp))
  7252. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  7253. }
  7254. /**
  7255. * Lost Base/Job EXP from a player
  7256. * @param sd Player
  7257. * @param base_exp Base EXP lost
  7258. * @param job_exp Job EXP lost
  7259. **/
  7260. void pc_lostexp(map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  7261. nullpo_retv(sd);
  7262. if (base_exp) {
  7263. base_exp = u64min(sd->status.base_exp, base_exp);
  7264. sd->status.base_exp -= base_exp;
  7265. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  7266. clif_updatestatus(sd, SP_BASEEXP);
  7267. }
  7268. if (job_exp) {
  7269. job_exp = u64min(sd->status.job_exp, job_exp);
  7270. sd->status.job_exp -= job_exp;
  7271. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  7272. clif_updatestatus(sd, SP_JOBEXP);
  7273. }
  7274. if (sd->state.showexp && (base_exp || job_exp))
  7275. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  7276. }
  7277. /**
  7278. * Returns max base level for this character's class.
  7279. * @param job_id: Player's class
  7280. * @return Max Base Level
  7281. */
  7282. uint32 JobDatabase::get_maxBaseLv(uint16 job_id) {
  7283. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7284. return job ? job->max_base_level : 0;
  7285. }
  7286. /**
  7287. * Returns max base level for this character.
  7288. * @param sd Player
  7289. * @return Max Base Level
  7290. **/
  7291. unsigned int pc_maxbaselv(map_session_data *sd){
  7292. return job_db.get_maxBaseLv(sd->status.class_);
  7293. }
  7294. /**
  7295. * Returns max job level for this character's class.
  7296. * @param job_id: Player's class
  7297. * @return Max Job Level
  7298. */
  7299. uint32 JobDatabase::get_maxJobLv(uint16 job_id) {
  7300. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7301. return job ? job->max_job_level : 0;
  7302. }
  7303. /**
  7304. * Returns max job level for this character.
  7305. * @param sd Player
  7306. * @return Max Job Level
  7307. **/
  7308. unsigned int pc_maxjoblv(map_session_data *sd){
  7309. return job_db.get_maxJobLv(sd->status.class_);
  7310. }
  7311. /**
  7312. * Check if player is reached max base level
  7313. * @param sd
  7314. * @return True if reached max level
  7315. **/
  7316. bool pc_is_maxbaselv(map_session_data *sd) {
  7317. nullpo_retr(false, sd);
  7318. return (sd->status.base_level >= pc_maxbaselv(sd));
  7319. }
  7320. /**
  7321. * Check if player is reached max base level
  7322. * @param sd
  7323. * @return True if reached max level
  7324. **/
  7325. bool pc_is_maxjoblv(map_session_data *sd) {
  7326. nullpo_retr(false, sd);
  7327. return (sd->status.job_level >= pc_maxjoblv(sd));
  7328. }
  7329. /**
  7330. * Returns base experience for this character's class.
  7331. * @param job_id: Player's class
  7332. * @param level: Player's level
  7333. * @return Base EXP
  7334. */
  7335. t_exp JobDatabase::get_baseExp(uint16 job_id, uint32 level) {
  7336. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7337. return job ? job->base_exp[level - 1] : 0;
  7338. }
  7339. /**
  7340. * Base exp needed for player to level up.
  7341. * @param sd
  7342. * @return Base EXP needed for next base level
  7343. **/
  7344. t_exp pc_nextbaseexp(map_session_data *sd){
  7345. nullpo_ret(sd);
  7346. if (sd->status.base_level == 0) // Is this something that possible?
  7347. return 0;
  7348. if (pc_is_maxbaselv(sd))
  7349. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  7350. return static_cast<t_exp>(job_db.get_baseExp(sd->status.class_, sd->status.base_level));
  7351. }
  7352. /**
  7353. * Returns job experience for this character's class.
  7354. * @param job_id: Player's class
  7355. * @param level: Player's level
  7356. * @return Job EXP
  7357. */
  7358. t_exp JobDatabase::get_jobExp(uint16 job_id, uint32 level) {
  7359. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7360. return job ? job->job_exp[level - 1] : 0;
  7361. }
  7362. /**
  7363. * Job exp needed for player to level up.
  7364. * @param sd
  7365. * @return Job EXP needed for next job level
  7366. **/
  7367. t_exp pc_nextjobexp(map_session_data *sd){
  7368. nullpo_ret(sd);
  7369. if (sd->status.job_level == 0) // Is this something that possible?
  7370. return 0;
  7371. if (pc_is_maxjoblv(sd))
  7372. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  7373. return static_cast<t_exp>(job_db.get_jobExp(sd->status.class_, sd->status.job_level));
  7374. }
  7375. /**
  7376. * Returns max weight base for this character's class.
  7377. * @param job_id: Player's class
  7378. * @return Max weight base
  7379. */
  7380. int32 JobDatabase::get_maxWeight(uint16 job_id) {
  7381. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7382. return job ? job->max_weight_base : 0;
  7383. }
  7384. /// Returns the value of the specified stat.
  7385. int pc_getstat(map_session_data *sd, int type)
  7386. {
  7387. nullpo_retr(-1, sd);
  7388. switch( type ) {
  7389. case SP_STR: return sd->status.str;
  7390. case SP_AGI: return sd->status.agi;
  7391. case SP_VIT: return sd->status.vit;
  7392. case SP_INT: return sd->status.int_;
  7393. case SP_DEX: return sd->status.dex;
  7394. case SP_LUK: return sd->status.luk;
  7395. case SP_POW: return sd->status.pow;
  7396. case SP_STA: return sd->status.sta;
  7397. case SP_WIS: return sd->status.wis;
  7398. case SP_SPL: return sd->status.spl;
  7399. case SP_CON: return sd->status.con;
  7400. case SP_CRT: return sd->status.crt;
  7401. default:
  7402. return -1;
  7403. }
  7404. }
  7405. /// Sets the specified stat to the specified value.
  7406. /// Returns the new value.
  7407. int pc_setstat(map_session_data* sd, int type, int val)
  7408. {
  7409. nullpo_retr(-1, sd);
  7410. switch( type ) {
  7411. case SP_STR: sd->status.str = val; break;
  7412. case SP_AGI: sd->status.agi = val; break;
  7413. case SP_VIT: sd->status.vit = val; break;
  7414. case SP_INT: sd->status.int_ = val; break;
  7415. case SP_DEX: sd->status.dex = val; break;
  7416. case SP_LUK: sd->status.luk = val; break;
  7417. case SP_POW: sd->status.pow = val; break;
  7418. case SP_STA: sd->status.sta = val; break;
  7419. case SP_WIS: sd->status.wis = val; break;
  7420. case SP_SPL: sd->status.spl = val; break;
  7421. case SP_CON: sd->status.con = val; break;
  7422. case SP_CRT: sd->status.crt = val; break;
  7423. default:
  7424. return -1;
  7425. }
  7426. return val;
  7427. }
  7428. /**
  7429. * Gets the total number of status points at the provided level.
  7430. * @param level: Player base level.
  7431. * @return Total number of status points at specific base level.
  7432. */
  7433. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  7434. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7435. if( entry != nullptr ){
  7436. return entry->statpoints;
  7437. }else{
  7438. return 0;
  7439. }
  7440. }
  7441. /**
  7442. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  7443. * @param level: Player base level.
  7444. * @param table: Use table value or formula.
  7445. * @return Status points at specific base level.
  7446. */
  7447. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  7448. uint32 next_level = this->get_table_point( level + 1 );
  7449. uint32 current_level = this->get_table_point( level );
  7450. if( next_level > current_level ){
  7451. return next_level - current_level;
  7452. }else{
  7453. return 0;
  7454. }
  7455. }
  7456. /**
  7457. * Gets the total number of trait points at the provided level.
  7458. * @param level: Player base level.
  7459. * @return Total number of trait points at specific base level.
  7460. */
  7461. uint32 PlayerStatPointDatabase::get_trait_table_point(uint16 level) {
  7462. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7463. if( entry != nullptr ){
  7464. return entry->traitpoints;
  7465. }else{
  7466. return 0;
  7467. }
  7468. }
  7469. /**
  7470. * Calculates the number of trait points PC gets when leveling up (from level to level+1)
  7471. * @param level: Player base level.
  7472. * @param table: Use table value or formula.
  7473. * @return Trait points at specific base level.
  7474. */
  7475. uint32 PlayerStatPointDatabase::pc_gets_trait_point(uint16 level) {
  7476. uint32 next_level = this->get_trait_table_point( level + 1 );
  7477. uint32 current_level = this->get_trait_table_point( level );
  7478. if( next_level > current_level ){
  7479. return next_level - current_level;
  7480. }else{
  7481. return 0;
  7482. }
  7483. }
  7484. #ifdef RENEWAL_STAT
  7485. /// Renewal status point cost formula
  7486. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  7487. #else
  7488. /// Pre-Renewal status point cost formula
  7489. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  7490. #endif
  7491. /// Returns the number of stat points needed to change the specified stat by val.
  7492. /// If val is negative, returns the number of stat points that would be needed to
  7493. /// raise the specified stat from (current value - val) to current value.
  7494. int pc_need_status_point(map_session_data* sd, int type, int val)
  7495. {
  7496. int low, high, sp = 0, max = 0;
  7497. if ( val == 0 )
  7498. return 0;
  7499. low = pc_getstat(sd,type);
  7500. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  7501. if ( low >= max && val > 0 )
  7502. return 0; // Official servers show '0' when max is reached
  7503. high = low + val;
  7504. if ( val < 0 )
  7505. SWAP(low, high);
  7506. for ( ; low < high; low++ )
  7507. sp += PC_STATUS_POINT_COST(low);
  7508. return sp;
  7509. }
  7510. /**
  7511. * Returns the value the specified stat can be increased by with the current
  7512. * amount of available status points for the current character's class.
  7513. *
  7514. * @param sd The target character.
  7515. * @param type Stat to verify.
  7516. * @return Maximum value the stat could grow by.
  7517. */
  7518. int pc_maxparameterincrease(map_session_data* sd, int type)
  7519. {
  7520. int base, final_val, status_points, max_param;
  7521. nullpo_ret(sd);
  7522. base = final_val = pc_getstat(sd, type);
  7523. status_points = sd->status.status_point;
  7524. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  7525. while (final_val <= max_param && status_points >= 0) {
  7526. status_points -= PC_STATUS_POINT_COST(final_val);
  7527. final_val++;
  7528. }
  7529. final_val--;
  7530. return (final_val > base ? final_val-base : 0);
  7531. }
  7532. /**
  7533. * Raises a stat by the specified amount.
  7534. *
  7535. * Obeys max_parameter limits.
  7536. * Subtracts status points according to the cost of the increased stat points.
  7537. *
  7538. * @param sd The target character.
  7539. * @param type The stat to change (see enum _sp)
  7540. * @param increase The stat increase (strictly positive) amount.
  7541. * @retval true if the stat was increased by any amount.
  7542. * @retval false if there were no changes.
  7543. */
  7544. bool pc_statusup(map_session_data* sd, int type, int increase)
  7545. {
  7546. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  7547. nullpo_ret(sd);
  7548. // check conditions
  7549. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  7550. clif_statusupack(sd, type, 0, 0);
  7551. return false;
  7552. }
  7553. // check limits
  7554. current = pc_getstat(sd, type);
  7555. max_increase = pc_maxparameterincrease(sd, type);
  7556. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7557. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  7558. clif_statusupack(sd, type, 0, 0);
  7559. return false;
  7560. }
  7561. // check status points
  7562. needed_points = pc_need_status_point(sd, type, increase);
  7563. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  7564. clif_statusupack(sd, type, 0, 0);
  7565. return false;
  7566. }
  7567. // set new values
  7568. final_value = pc_setstat(sd, type, current + increase);
  7569. sd->status.status_point -= needed_points;
  7570. status_calc_pc(sd,SCO_NONE);
  7571. // update increase cost indicator
  7572. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7573. // update statpoint count
  7574. clif_updatestatus(sd, SP_STATUSPOINT);
  7575. // update stat value
  7576. clif_statusupack(sd, type, 1, final_value); // required
  7577. if( final_value > 255 )
  7578. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7579. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7580. return true;
  7581. }
  7582. /**
  7583. * Raises a stat by the specified amount.
  7584. *
  7585. * Obeys max_parameter limits.
  7586. * Does not subtract status points for the cost of the modified stat points.
  7587. *
  7588. * @param sd The target character.
  7589. * @param type The stat to change (see enum _sp)
  7590. * @param val The stat increase (or decrease) amount.
  7591. * @return the stat increase amount.
  7592. * @retval 0 if no changes were made.
  7593. */
  7594. int pc_statusup2(map_session_data* sd, int type, int val)
  7595. {
  7596. int max, need;
  7597. nullpo_ret(sd);
  7598. if( type < SP_STR || type > SP_LUK )
  7599. {
  7600. clif_statusupack(sd,type,0,0);
  7601. return 0;
  7602. }
  7603. need = pc_need_status_point(sd,type,1);
  7604. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  7605. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  7606. status_calc_pc(sd,SCO_NONE);
  7607. // update increase cost indicator
  7608. if( need != pc_need_status_point(sd,type,1) )
  7609. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7610. // update stat value
  7611. clif_statusupack(sd,type,1,val); // required
  7612. if( val > 255 )
  7613. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  7614. return val;
  7615. }
  7616. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  7617. /// If val is negative, returns the number of trait stat points that would be needed to
  7618. /// raise the specified trait stat from (current value - val) to current value.
  7619. int pc_need_trait_point(map_session_data* sd, int type, int val)
  7620. {
  7621. nullpo_retr(0, sd);
  7622. if (val == 0 || type < SP_POW || type > SP_CRT)
  7623. return 0;
  7624. int low = pc_getstat(sd, type);
  7625. int max = pc_maxparameter(sd, (e_params)(PARAM_POW + type - SP_POW));
  7626. if (low >= max && val > 0)
  7627. return 0; // Official servers show '0' when max is reached
  7628. int high = low + val, sp = 0;
  7629. if (val < 0)
  7630. SWAP(low, high);
  7631. for (; low < high; low++)
  7632. sp += 1;
  7633. return sp;
  7634. }
  7635. /**
  7636. * Returns the value the specified trait stat can be increased by with the current
  7637. * amount of available trait status points for the current character's class.
  7638. *
  7639. * @param sd The target character.
  7640. * @param type Trait stat to verify.
  7641. * @return Maximum value the stat could grow by.
  7642. */
  7643. int pc_maxtraitparameterincrease(map_session_data* sd, int type)
  7644. {
  7645. nullpo_ret(sd);
  7646. if( type < SP_POW || type > SP_CRT ){
  7647. return 0;
  7648. }
  7649. int base, final_val = pc_getstat(sd, type);
  7650. int trait_points = sd->status.trait_point;
  7651. int max_param = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW));
  7652. base = final_val;
  7653. while (final_val <= max_param && trait_points >= 0) {
  7654. trait_points -= 1;
  7655. final_val++;
  7656. }
  7657. final_val--;
  7658. return (final_val > base ? final_val - base : 0);
  7659. }
  7660. /**
  7661. * Raises a trait stat by the specified amount.
  7662. *
  7663. * Obeys max_traitparameter limits.
  7664. * Subtracts trait status points according to the cost of the increased trait stat points.
  7665. *
  7666. * @param sd The target character.
  7667. * @param type The stat to change (see enum _sp)
  7668. * @param increase The stat increase (strictly positive) amount.
  7669. * @retval true if the trait stat was increased by any amount.
  7670. * @retval false if there were no changes.
  7671. */
  7672. bool pc_traitstatusup(map_session_data* sd, int type, int increase)
  7673. {
  7674. nullpo_ret(sd);
  7675. // check conditions
  7676. if (type < SP_POW || type > SP_CRT || increase <= 0) {
  7677. clif_statusupack(sd, type, 0, 0);
  7678. return false;
  7679. }
  7680. // check limits
  7681. int current = pc_getstat(sd, type);
  7682. int max_increase = pc_maxtraitparameterincrease(sd, type);
  7683. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7684. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW))) {
  7685. clif_statusupack(sd, type, 0, 0);
  7686. return false;
  7687. }
  7688. // check status points
  7689. int needed_points = pc_need_trait_point(sd, type, increase);
  7690. if (needed_points < 0 || needed_points > sd->status.trait_point) { // Sanity check
  7691. clif_statusupack(sd, type, 0, 0);
  7692. return false;
  7693. }
  7694. // set new values
  7695. int final_value = pc_setstat(sd, type, current + increase);
  7696. sd->status.trait_point -= needed_points;
  7697. status_calc_pc(sd, SCO_NONE);
  7698. // update increase cost indicator
  7699. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7700. // update statpoint count
  7701. clif_updatestatus(sd, SP_TRAITPOINT);
  7702. // update stat value
  7703. clif_statusupack(sd, type, 1, final_value); // required
  7704. if (final_value > 255)
  7705. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7706. //achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7707. return true;
  7708. }
  7709. /**
  7710. * Raises a trait stat by the specified amount.
  7711. *
  7712. * Obeys max_trait_parameter limits.
  7713. * Does not subtract status points for the cost of the modified stat points.
  7714. *
  7715. * @param sd The target character.
  7716. * @param type The stat to change (see enum _sp)
  7717. * @param val The stat increase (or decrease) amount.
  7718. * @return the stat increase amount.
  7719. * @retval 0 if no changes were made.
  7720. */
  7721. int pc_traitstatusup2(map_session_data* sd, int type, int val)
  7722. {
  7723. nullpo_ret(sd);
  7724. if (type < SP_POW || type > SP_CRT) {
  7725. clif_statusupack(sd, type, 0, 0);
  7726. return 0;
  7727. }
  7728. int need = pc_need_trait_point(sd, type, 1);
  7729. int max = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW)); // set new value
  7730. val = pc_setstat(sd, type, cap_value(pc_getstat(sd, type) + val, 0, max));
  7731. status_calc_pc(sd, SCO_NONE);
  7732. // update increase cost indicator
  7733. if (need != pc_need_trait_point(sd, type, 1))
  7734. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7735. // update stat value
  7736. clif_statusupack(sd, type, 1, val); // required
  7737. if (val > 255)
  7738. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7739. return val;
  7740. }
  7741. /*==========================================
  7742. * Update skill_lv for player sd
  7743. * Skill point allocation
  7744. *------------------------------------------*/
  7745. void pc_skillup(map_session_data *sd,uint16 skill_id)
  7746. {
  7747. uint16 idx = skill_get_index(skill_id);
  7748. nullpo_retv(sd);
  7749. if (!idx) {
  7750. if (skill_id)
  7751. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  7752. return;
  7753. }
  7754. // Level up guild skill
  7755. if (SKILL_CHK_GUILD(skill_id)) {
  7756. guild_skillup(sd, skill_id);
  7757. return;
  7758. }
  7759. // Level up homunculus skill
  7760. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  7761. hom_skillup(sd->hd, skill_id);
  7762. return;
  7763. }
  7764. else {
  7765. if( sd->status.skill_point > 0 &&
  7766. sd->status.skill[idx].id &&
  7767. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  7768. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  7769. {
  7770. int lv, range, upgradable;
  7771. sd->status.skill[idx].lv++;
  7772. sd->status.skill_point--;
  7773. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  7774. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  7775. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  7776. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  7777. else
  7778. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  7779. lv = sd->status.skill[idx].lv;
  7780. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  7781. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  7782. clif_skillup(sd,skill_id,lv,range,upgradable);
  7783. clif_updatestatus(sd,SP_SKILLPOINT);
  7784. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  7785. clif_updatestatus(sd,SP_CARTINFO);
  7786. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  7787. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7788. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  7789. clif_skillinfoblock(sd);
  7790. }
  7791. //else
  7792. // 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);
  7793. }
  7794. }
  7795. /*==========================================
  7796. * /allskill
  7797. *------------------------------------------*/
  7798. int pc_allskillup(map_session_data *sd)
  7799. {
  7800. int i;
  7801. nullpo_ret(sd);
  7802. for (i = 0; i < MAX_SKILL; i++) {
  7803. 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) {
  7804. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  7805. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  7806. if (sd->status.skill[i].lv == 0)
  7807. sd->status.skill[i].id = 0;
  7808. }
  7809. }
  7810. if (!pc_grant_allskills(sd, true)) {
  7811. uint16 sk_id;
  7812. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(sd->status.class_);
  7813. if (tree != nullptr && !tree->skills.empty()) {
  7814. for (const auto &skillsit : tree->skills) {
  7815. sk_id = skillsit.first;
  7816. uint16 sk_idx = skill_get_index(sk_id);
  7817. if (sk_idx == 0)
  7818. continue;
  7819. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  7820. if (
  7821. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  7822. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  7823. sk_id == SG_DEVIL
  7824. )
  7825. continue; //Cannot be learned normally.
  7826. sd->status.skill[sk_idx].id = sk_id;
  7827. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  7828. }
  7829. }
  7830. }
  7831. status_calc_pc(sd,SCO_NONE);
  7832. //Required because if you could level up all skills previously,
  7833. //the update will not be sent as only the lv variable changes.
  7834. clif_skillinfoblock(sd);
  7835. return 0;
  7836. }
  7837. /*==========================================
  7838. * /resetlvl
  7839. *------------------------------------------*/
  7840. int pc_resetlvl(map_session_data* sd,int type)
  7841. {
  7842. int i;
  7843. nullpo_ret(sd);
  7844. if (type != 3) //Also reset skills
  7845. pc_resetskill(sd, 0);
  7846. if(type == 1){
  7847. sd->status.skill_point=0;
  7848. sd->status.trait_point = 0;
  7849. sd->status.base_level=1;
  7850. sd->status.job_level=1;
  7851. sd->status.base_exp=0;
  7852. sd->status.job_exp=0;
  7853. if(sd->sc.option !=0)
  7854. sd->sc.option = 0;
  7855. sd->status.str=1;
  7856. sd->status.agi=1;
  7857. sd->status.vit=1;
  7858. sd->status.int_=1;
  7859. sd->status.dex=1;
  7860. sd->status.luk=1;
  7861. sd->status.pow=0;
  7862. sd->status.sta=0;
  7863. sd->status.wis=0;
  7864. sd->status.spl=0;
  7865. sd->status.con=0;
  7866. sd->status.crt=0;
  7867. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  7868. sd->status.status_point=100; // not 88 [celest]
  7869. // give platinum skills upon changing
  7870. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  7871. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  7872. }
  7873. }
  7874. if(type == 2){
  7875. sd->status.skill_point=0;
  7876. sd->status.base_level=1;
  7877. sd->status.job_level=1;
  7878. sd->status.base_exp=0;
  7879. sd->status.job_exp=0;
  7880. }
  7881. if(type == 3){
  7882. sd->status.base_level=1;
  7883. sd->status.base_exp=0;
  7884. }
  7885. if(type == 4){
  7886. sd->status.job_level=1;
  7887. sd->status.job_exp=0;
  7888. }
  7889. clif_updatestatus(sd,SP_STATUSPOINT);
  7890. clif_updatestatus(sd,SP_TRAITPOINT);
  7891. clif_updatestatus(sd,SP_STR);
  7892. clif_updatestatus(sd,SP_AGI);
  7893. clif_updatestatus(sd,SP_VIT);
  7894. clif_updatestatus(sd,SP_INT);
  7895. clif_updatestatus(sd,SP_DEX);
  7896. clif_updatestatus(sd,SP_LUK);
  7897. clif_updatestatus(sd,SP_POW);
  7898. clif_updatestatus(sd,SP_STA);
  7899. clif_updatestatus(sd,SP_WIS);
  7900. clif_updatestatus(sd,SP_SPL);
  7901. clif_updatestatus(sd,SP_CON);
  7902. clif_updatestatus(sd,SP_CRT);
  7903. clif_updatestatus(sd,SP_BASELEVEL);
  7904. clif_updatestatus(sd,SP_JOBLEVEL);
  7905. clif_updatestatus(sd,SP_STATUSPOINT);
  7906. clif_updatestatus(sd,SP_BASEEXP);
  7907. clif_updatestatus(sd,SP_JOBEXP);
  7908. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7909. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7910. clif_updatestatus(sd,SP_SKILLPOINT);
  7911. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7912. clif_updatestatus(sd,SP_UAGI);
  7913. clif_updatestatus(sd,SP_UVIT);
  7914. clif_updatestatus(sd,SP_UINT);
  7915. clif_updatestatus(sd,SP_UDEX);
  7916. clif_updatestatus(sd,SP_ULUK); // End Addition
  7917. clif_updatestatus(sd,SP_UPOW);
  7918. clif_updatestatus(sd,SP_USTA);
  7919. clif_updatestatus(sd,SP_UWIS);
  7920. clif_updatestatus(sd,SP_USPL);
  7921. clif_updatestatus(sd,SP_UCON);
  7922. clif_updatestatus(sd,SP_UCRT);
  7923. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  7924. if(sd->equip_index[i] >= 0)
  7925. if(pc_isequip(sd,sd->equip_index[i]))
  7926. pc_unequipitem(sd,sd->equip_index[i],2);
  7927. }
  7928. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  7929. party_send_levelup(sd);
  7930. status_calc_pc(sd, SCO_FORCE);
  7931. clif_skillinfoblock(sd);
  7932. return 0;
  7933. }
  7934. /*==========================================
  7935. * /resetstate
  7936. *------------------------------------------*/
  7937. int pc_resetstate(map_session_data* sd)
  7938. {
  7939. nullpo_ret(sd);
  7940. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  7941. 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",
  7942. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  7943. sd->status.base_level = pc_maxbaselv( sd );
  7944. clif_updatestatus( sd, SP_BASELEVEL );
  7945. }
  7946. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  7947. sd->status.trait_point = statpoint_db.get_trait_table_point(sd->status.base_level);
  7948. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  7949. sd->status.status_point += battle_config.transcendent_status_points;
  7950. }
  7951. if ((sd->class_&JOBL_FOURTH) != 0) {
  7952. sd->status.trait_point += battle_config.trait_points_job_change;
  7953. }
  7954. pc_setstat(sd, SP_STR, 1);
  7955. pc_setstat(sd, SP_AGI, 1);
  7956. pc_setstat(sd, SP_VIT, 1);
  7957. pc_setstat(sd, SP_INT, 1);
  7958. pc_setstat(sd, SP_DEX, 1);
  7959. pc_setstat(sd, SP_LUK, 1);
  7960. pc_setstat(sd, SP_POW, 0);
  7961. pc_setstat(sd, SP_STA, 0);
  7962. pc_setstat(sd, SP_WIS, 0);
  7963. pc_setstat(sd, SP_SPL, 0);
  7964. pc_setstat(sd, SP_CON, 0);
  7965. pc_setstat(sd, SP_CRT, 0);
  7966. clif_updatestatus(sd,SP_STR);
  7967. clif_updatestatus(sd,SP_AGI);
  7968. clif_updatestatus(sd,SP_VIT);
  7969. clif_updatestatus(sd,SP_INT);
  7970. clif_updatestatus(sd,SP_DEX);
  7971. clif_updatestatus(sd,SP_LUK);
  7972. clif_updatestatus(sd,SP_POW);
  7973. clif_updatestatus(sd,SP_STA);
  7974. clif_updatestatus(sd,SP_WIS);
  7975. clif_updatestatus(sd,SP_SPL);
  7976. clif_updatestatus(sd,SP_CON);
  7977. clif_updatestatus(sd,SP_CRT);
  7978. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7979. clif_updatestatus(sd,SP_UAGI);
  7980. clif_updatestatus(sd,SP_UVIT);
  7981. clif_updatestatus(sd,SP_UINT);
  7982. clif_updatestatus(sd,SP_UDEX);
  7983. clif_updatestatus(sd,SP_ULUK); // End Addition
  7984. clif_updatestatus(sd,SP_UPOW);
  7985. clif_updatestatus(sd,SP_USTA);
  7986. clif_updatestatus(sd,SP_UWIS);
  7987. clif_updatestatus(sd,SP_USPL);
  7988. clif_updatestatus(sd,SP_UCON);
  7989. clif_updatestatus(sd,SP_UCRT);
  7990. clif_updatestatus(sd,SP_STATUSPOINT);
  7991. clif_updatestatus(sd,SP_TRAITPOINT);
  7992. if( sd->mission_mobid ) { //bugreport:2200
  7993. sd->mission_mobid = 0;
  7994. sd->mission_count = 0;
  7995. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  7996. }
  7997. status_calc_pc(sd, SCO_NONE);
  7998. return 1;
  7999. }
  8000. /*==========================================
  8001. * /resetskill
  8002. * if flag&1, perform block resync and status_calc call.
  8003. * if flag&2, just count total amount of skill points used by player, do not really reset.
  8004. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  8005. *------------------------------------------*/
  8006. int pc_resetskill(map_session_data* sd, int flag)
  8007. {
  8008. int i, skill_point=0;
  8009. nullpo_ret(sd);
  8010. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  8011. return 0;
  8012. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  8013. /**
  8014. * 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)
  8015. **/
  8016. if( pc_is_taekwon_ranker(sd) )
  8017. return 0;
  8018. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  8019. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  8020. i = sd->sc.option;
  8021. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  8022. i &= ~OPTION_RIDING;
  8023. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  8024. i &= ~OPTION_FALCON;
  8025. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  8026. i &= ~OPTION_DRAGON;
  8027. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  8028. i &= ~OPTION_WUG;
  8029. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  8030. i &= ~OPTION_WUGRIDER;
  8031. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  8032. i &= ~OPTION_MADOGEAR;
  8033. #ifndef NEW_CARTS
  8034. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  8035. i &= ~OPTION_CART;
  8036. #else
  8037. if( sd->sc.getSCE(SC_PUSH_CART) )
  8038. pc_setcart(sd, 0);
  8039. #endif
  8040. if( i != sd->sc.option )
  8041. pc_setoption(sd, i);
  8042. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  8043. hom_vaporize(sd, HOM_ST_ACTIVE);
  8044. if (sd->sc.getSCE(SC_SPRITEMABLE) && pc_checkskill(sd, SU_SPRITEMABLE))
  8045. status_change_end(&sd->bl, SC_SPRITEMABLE);
  8046. if (sd->sc.getSCE(SC_SOULATTACK) && pc_checkskill(sd, SU_SOULATTACK))
  8047. status_change_end(&sd->bl, SC_SOULATTACK);
  8048. }
  8049. for (const auto &skill : skill_db) {
  8050. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  8051. uint8 lv = sd->status.skill[idx].lv;
  8052. if (lv == 0 || skill_id == 0)
  8053. continue;
  8054. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  8055. continue;
  8056. // Don't reset trick dead if not a novice/baby
  8057. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  8058. {
  8059. sd->status.skill[idx].lv = 0;
  8060. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8061. continue;
  8062. }
  8063. // do not reset basic skill
  8064. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  8065. continue;
  8066. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  8067. continue;
  8068. if( flag&4 && !skill_ischangesex(skill_id) )
  8069. continue;
  8070. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  8071. { //Only handle quest skills in a special way when you can't learn them manually
  8072. if( battle_config.quest_skill_reset && !(flag&2) )
  8073. { //Wipe them
  8074. sd->status.skill[idx].lv = 0;
  8075. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8076. }
  8077. continue;
  8078. }
  8079. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  8080. skill_point += lv;
  8081. else
  8082. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  8083. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  8084. if( !(flag&2) )
  8085. {// reset
  8086. sd->status.skill[idx].lv = 0;
  8087. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8088. }
  8089. }
  8090. if( flag&2 || !skill_point ) return skill_point;
  8091. sd->status.skill_point += skill_point;
  8092. if (flag&1) {
  8093. clif_updatestatus(sd,SP_SKILLPOINT);
  8094. clif_skillinfoblock(sd);
  8095. status_calc_pc(sd, SCO_FORCE);
  8096. }
  8097. return skill_point;
  8098. }
  8099. /*==========================================
  8100. * /resetfeel [Komurka]
  8101. *------------------------------------------*/
  8102. int pc_resetfeel(map_session_data* sd)
  8103. {
  8104. int i;
  8105. nullpo_ret(sd);
  8106. for (i=0; i<MAX_PC_FEELHATE; i++)
  8107. {
  8108. sd->feel_map[i].m = -1;
  8109. sd->feel_map[i].index = 0;
  8110. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  8111. }
  8112. return 0;
  8113. }
  8114. int pc_resethate(map_session_data* sd)
  8115. {
  8116. int i;
  8117. nullpo_ret(sd);
  8118. for (i=0; i<MAX_PC_FEELHATE; i++)
  8119. {
  8120. sd->hate_mob[i] = -1;
  8121. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  8122. }
  8123. return 0;
  8124. }
  8125. int pc_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8126. {
  8127. int bonus = 0;
  8128. nullpo_ret(sd);
  8129. skill_id = skill_dummy2skill_id(skill_id);
  8130. for (auto &it : sd->skillatk) {
  8131. if (it.id == skill_id) {
  8132. bonus += it.val;
  8133. break;
  8134. }
  8135. }
  8136. return bonus;
  8137. }
  8138. int pc_sub_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8139. {
  8140. int bonus = 0;
  8141. nullpo_ret(sd);
  8142. skill_id = skill_dummy2skill_id(skill_id);
  8143. for (auto &it : sd->subskill) {
  8144. if (it.id == skill_id) {
  8145. bonus += it.val;
  8146. break;
  8147. }
  8148. }
  8149. return bonus;
  8150. }
  8151. int pc_skillheal_bonus(map_session_data *sd, uint16 skill_id) {
  8152. int bonus = sd->bonus.add_heal_rate;
  8153. nullpo_ret(sd);
  8154. skill_id = skill_dummy2skill_id(skill_id);
  8155. if( bonus ) {
  8156. switch( skill_id ) {
  8157. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  8158. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  8159. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  8160. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  8161. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  8162. case AB_CHEAL: if (!(battle_config.skill_add_heal_rate & 32)) bonus = 0; break;
  8163. case AB_HIGHNESSHEAL: if (!(battle_config.skill_add_heal_rate & 64)) bonus = 0; break;
  8164. case CD_MEDIALE_VOTUM: if (!(battle_config.skill_add_heal_rate & 128)) bonus = 0; break;
  8165. case CD_DILECTIO_HEAL: if (!(battle_config.skill_add_heal_rate & 256)) bonus = 0; break;
  8166. }
  8167. }
  8168. for (auto &it : sd->skillheal) {
  8169. if (it.id == skill_id) {
  8170. bonus += it.val;
  8171. break;
  8172. }
  8173. }
  8174. return bonus;
  8175. }
  8176. int pc_skillheal2_bonus(map_session_data *sd, uint16 skill_id) {
  8177. int bonus = sd->bonus.add_heal2_rate;
  8178. skill_id = skill_dummy2skill_id(skill_id);
  8179. for (auto &it : sd->skillheal2) {
  8180. if (it.id == skill_id) {
  8181. bonus += it.val;
  8182. break;
  8183. }
  8184. }
  8185. return bonus;
  8186. }
  8187. void pc_respawn(map_session_data* sd, clr_type clrtype)
  8188. {
  8189. if( !pc_isdead(sd) )
  8190. return; // not applicable
  8191. if( sd->bg_id && bg_member_respawn(sd) )
  8192. return; // member revived by battleground
  8193. pc_setstand(sd, true);
  8194. pc_setrestartvalue(sd,3);
  8195. if( pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, clrtype ) != SETPOS_OK ){
  8196. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  8197. }
  8198. }
  8199. static TIMER_FUNC(pc_respawn_timer){
  8200. map_session_data *sd = map_id2sd(id);
  8201. if( sd != NULL )
  8202. {
  8203. sd->pvp_point=0;
  8204. sd->respawn_tid = INVALID_TIMER;
  8205. pc_respawn(sd,CLR_OUTSIGHT);
  8206. }
  8207. return 0;
  8208. }
  8209. /*==========================================
  8210. * Invoked when a player has received damage
  8211. *------------------------------------------*/
  8212. void pc_damage(map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp, unsigned int ap)
  8213. {
  8214. if (ap) clif_updatestatus(sd,SP_AP);
  8215. if (sp) clif_updatestatus(sd,SP_SP);
  8216. if (hp) clif_updatestatus(sd,SP_HP);
  8217. else return;
  8218. if (!src)
  8219. return;
  8220. if( pc_issit(sd) ) {
  8221. pc_setstand(sd, true);
  8222. skill_sit(sd,0);
  8223. }
  8224. if (sd->progressbar.npc_id)
  8225. clif_progressbar_abort(sd);
  8226. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  8227. pet_target_check(sd->pd,src,1);
  8228. if( sd->status.ele_id > 0 )
  8229. elemental_set_target(sd,src);
  8230. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  8231. sd->canlog_tick = gettick();
  8232. }
  8233. TIMER_FUNC(pc_close_npc_timer){
  8234. TBL_PC *sd = map_id2sd(id);
  8235. if(sd) pc_close_npc(sd,data);
  8236. return 0;
  8237. }
  8238. /**
  8239. * Method to properly close a NPC for player and clear anything related.
  8240. * @param sd: Player attached
  8241. * @param flag: Method of closure
  8242. * 1: Produce a close button and end the NPC
  8243. * 2: End the NPC (best for no dialog windows)
  8244. */
  8245. void pc_close_npc(map_session_data *sd,int flag)
  8246. {
  8247. nullpo_retv(sd);
  8248. if (sd->npc_id || sd->npc_shopid) {
  8249. if (sd->state.using_fake_npc) {
  8250. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  8251. sd->state.using_fake_npc = 0;
  8252. }
  8253. if (sd->st) {
  8254. if(sd->st->state == RUN){ //wait ending code execution
  8255. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  8256. return;
  8257. }
  8258. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  8259. sd->st->mes_active = 0;
  8260. }
  8261. sd->state.menu_or_input = 0;
  8262. sd->npc_menu = 0;
  8263. sd->npc_shopid = 0;
  8264. #ifdef SECURE_NPCTIMEOUT
  8265. if( sd->npc_idle_timer != INVALID_TIMER ){
  8266. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  8267. sd->npc_idle_timer = INVALID_TIMER;
  8268. }
  8269. #endif
  8270. if (sd->st) {
  8271. if (sd->st->state == CLOSE) {
  8272. clif_scriptclose(sd, sd->npc_id);
  8273. clif_scriptclear( *sd, sd->npc_id ); // [Ind/Hercules]
  8274. sd->st->state = END; // Force to end now
  8275. }
  8276. if (sd->st->state == END) { // free attached scripts that are waiting
  8277. script_free_state(sd->st);
  8278. sd->st = NULL;
  8279. sd->npc_id = 0;
  8280. }
  8281. }
  8282. }
  8283. }
  8284. /*==========================================
  8285. * Invoked when a player has negative current hp
  8286. *------------------------------------------*/
  8287. int pc_dead(map_session_data *sd,struct block_list *src)
  8288. {
  8289. int i=0,k=0;
  8290. t_tick tick = gettick();
  8291. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8292. // Activate Steel body if a super novice dies at 99+% exp [celest]
  8293. // Super Novices have no kill or die functions attached when saved by their angel
  8294. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  8295. t_exp exp = pc_nextbaseexp(sd);
  8296. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  8297. sd->state.snovice_dead_flag = 1;
  8298. pc_setrestartvalue(sd,1);
  8299. status_percent_heal(&sd->bl, 100, 100);
  8300. clif_resurrection(&sd->bl, 1);
  8301. if(battle_config.pc_invincible_time)
  8302. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8303. sc_start(&sd->bl,&sd->bl,SC_STEELBODY,100,5,skill_get_time(MO_STEELBODY,5));
  8304. if(mapdata_flag_gvg2(mapdata))
  8305. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  8306. return 0;
  8307. }
  8308. }
  8309. for(k = 0; k < MAX_DEVOTION; k++) {
  8310. if (sd->devotion[k]){
  8311. map_session_data *devsd = map_id2sd(sd->devotion[k]);
  8312. if (devsd)
  8313. status_change_end(&devsd->bl, SC_DEVOTION);
  8314. sd->devotion[k] = 0;
  8315. }
  8316. }
  8317. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  8318. if (sd->stellar_mark[k]) {
  8319. map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  8320. if (smarksd)
  8321. status_change_end(&smarksd->bl, SC_FLASHKICK);
  8322. sd->stellar_mark[k] = 0;
  8323. }
  8324. }
  8325. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  8326. if (sd->united_soul[k]) {
  8327. map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  8328. if (usoulsd)
  8329. status_change_end(&usoulsd->bl, SC_SOULUNITY);
  8330. sd->united_soul[k] = 0;
  8331. }
  8332. }
  8333. for (k = 0; k < MAX_SERVANT_SIGN; k++) {
  8334. if (sd->servant_sign[k]) {
  8335. map_session_data *ssignsd = map_id2sd(sd->servant_sign[k]);
  8336. if (ssignsd)
  8337. status_change_end(&ssignsd->bl, SC_SERVANT_SIGN);
  8338. sd->servant_sign[k] = 0;
  8339. }
  8340. }
  8341. if(sd->shadowform_id) { //if we were target of shadowform
  8342. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM);
  8343. sd->shadowform_id = 0; //should be remove on status end anyway
  8344. }
  8345. if(sd->status.pet_id > 0 && sd->pd) {
  8346. struct pet_data *pd = sd->pd;
  8347. if( !mapdata->getMapFlag(MF_NOEXPPENALTY) ) {
  8348. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  8349. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  8350. }
  8351. if( sd->pd->target_id ) // Unlock all targets...
  8352. pet_unlocktarget(sd->pd);
  8353. }
  8354. 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)
  8355. hom_vaporize(sd, HOM_ST_ACTIVE);
  8356. if( sd->md )
  8357. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  8358. if( sd->ed )
  8359. elemental_delete(sd->ed);
  8360. // Leave duel if you die [LuzZza]
  8361. if(battle_config.duel_autoleave_when_die) {
  8362. if(sd->duel_group > 0)
  8363. duel_leave(sd->duel_group, sd);
  8364. if(sd->duel_invite > 0)
  8365. duel_reject(sd->duel_invite, sd);
  8366. }
  8367. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  8368. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  8369. sd->skill_keep_using.tid = INVALID_TIMER;
  8370. }
  8371. pc_close_npc(sd,2); //close npc if we were using one
  8372. /* e.g. not killed thru pc_damage */
  8373. if( pc_issit(sd) ) {
  8374. clif_status_load(&sd->bl,EFST_SIT,0);
  8375. }
  8376. pc_setdead(sd);
  8377. clif_party_dead( *sd );
  8378. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  8379. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  8380. if (battle_config.loose_ap_on_death == 1)
  8381. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  8382. //Reset menu skills/item skills
  8383. if ((sd->skillitem) != 0)
  8384. sd->skillitem = sd->skillitemlv = 0;
  8385. if ((sd->menuskill_id) != 0)
  8386. sd->menuskill_id = sd->menuskill_val = 0;
  8387. //Reset ticks.
  8388. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  8389. if ( sd->spiritball !=0 )
  8390. pc_delspiritball(sd,sd->spiritball,0);
  8391. if (sd->soulball != 0)
  8392. pc_delsoulball(sd, sd->soulball, false);
  8393. if (sd->servantball != 0)
  8394. pc_delservantball( *sd, sd->servantball );
  8395. if (sd->abyssball != 0)
  8396. pc_delabyssball( *sd, sd->abyssball );
  8397. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  8398. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  8399. if (src)
  8400. switch (src->type) {
  8401. case BL_MOB:
  8402. {
  8403. struct mob_data *md=(struct mob_data *)src;
  8404. if(md->target_id==sd->bl.id)
  8405. mob_unlocktarget(md,tick);
  8406. if(battle_config.mobs_level_up && md->status.hp &&
  8407. (unsigned int)md->level < pc_maxbaselv(sd) &&
  8408. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  8409. ) { // monster level up [Valaris]
  8410. clif_misceffect(&md->bl,0);
  8411. md->level++;
  8412. status_calc_mob(md, SCO_NONE);
  8413. status_percent_heal(src,10,0);
  8414. if( battle_config.show_mob_info&4 )
  8415. {// update name with new level
  8416. clif_name_area(&md->bl);
  8417. }
  8418. }
  8419. src = battle_get_master(src); // Maybe Player Summon
  8420. }
  8421. break;
  8422. case BL_PET: //Pass on to master...
  8423. case BL_HOM:
  8424. case BL_MER:
  8425. src = battle_get_master(src);
  8426. break;
  8427. }
  8428. if (src && src->type == BL_PC) {
  8429. map_session_data *ssd = (map_session_data *)src;
  8430. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  8431. npc_script_event(ssd, NPCE_KILLPC);
  8432. if (battle_config.pk_mode&2) {
  8433. ssd->status.manner -= 5;
  8434. if(ssd->status.manner < 0)
  8435. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  8436. #if 0
  8437. // PK/Karma system code (not enabled yet) [celest]
  8438. // originally from Kade Online, so i don't know if any of these is correct ^^;
  8439. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  8440. // karma going down = more 'good' / more honourable.
  8441. // The Karma System way...
  8442. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  8443. sd->status.karma--;
  8444. ssd->status.karma--;
  8445. }
  8446. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  8447. ssd->status.karma++;
  8448. // or the PK System way...
  8449. if (sd->status.karma > 0) // player killed is dishonourable?
  8450. ssd->status.karma--; // honour points earned
  8451. sd->status.karma++; // honour points lost
  8452. // To-do: Receive exp on certain occasions
  8453. #endif
  8454. }
  8455. }
  8456. if(battle_config.bone_drop==2
  8457. || (battle_config.bone_drop==1 && mapdata->getMapFlag(MF_PVP)))
  8458. {
  8459. struct item item_tmp;
  8460. memset(&item_tmp,0,sizeof(item_tmp));
  8461. item_tmp.nameid=ITEMID_SKULL_;
  8462. item_tmp.identify=1;
  8463. item_tmp.card[0]=CARD0_CREATE;
  8464. item_tmp.card[1]=0;
  8465. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  8466. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  8467. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  8468. }
  8469. //Remove bonus_script when dead
  8470. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  8471. // changed penalty options, added death by player if pk_mode [Valaris]
  8472. if(battle_config.death_penalty_type
  8473. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  8474. && !sd->sc.cant.deathpenalty
  8475. && !mapdata->getMapFlag(MF_NOEXPPENALTY) && !mapdata_flag_gvg2(mapdata))
  8476. {
  8477. t_exp base_penalty = 0;
  8478. t_exp job_penalty = 0;
  8479. uint32 zeny_penalty = 0;
  8480. if (pc_isvip(sd)) { // EXP penalty for VIP
  8481. base_penalty = battle_config.vip_exp_penalty_base;
  8482. job_penalty = battle_config.vip_exp_penalty_job;
  8483. zeny_penalty = battle_config.vip_zeny_penalty;
  8484. } else {
  8485. base_penalty = battle_config.death_penalty_base;
  8486. job_penalty = battle_config.death_penalty_job;
  8487. zeny_penalty = battle_config.zeny_penalty;
  8488. }
  8489. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  8490. switch (battle_config.death_penalty_type) {
  8491. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  8492. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  8493. }
  8494. if (base_penalty){ //recheck after altering to speedup
  8495. if (battle_config.pk_mode && src && src->type==BL_PC)
  8496. base_penalty *= 2;
  8497. base_penalty = u64min(sd->status.base_exp, base_penalty);
  8498. }
  8499. }
  8500. else
  8501. base_penalty = 0;
  8502. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  8503. switch (battle_config.death_penalty_type) {
  8504. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  8505. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  8506. }
  8507. if (job_penalty) {
  8508. if (battle_config.pk_mode && src && src->type==BL_PC)
  8509. job_penalty *= 2;
  8510. job_penalty = u64min(sd->status.job_exp, job_penalty);
  8511. }
  8512. }
  8513. else
  8514. job_penalty = 0;
  8515. if (base_penalty || job_penalty) {
  8516. short insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  8517. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  8518. pc_lostexp(sd, base_penalty, job_penalty);
  8519. }
  8520. if( zeny_penalty > 0 && !mapdata->getMapFlag(MF_NOZENYPENALTY)) {
  8521. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  8522. if(zeny_penalty)
  8523. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER);
  8524. }
  8525. }
  8526. if( mapdata->getMapFlag(MF_PVP_NIGHTMAREDROP) ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  8527. for (const auto &it : mapdata->drop_list) {
  8528. int id = it.drop_id, per = it.drop_per;
  8529. enum e_nightmare_drop_type type = it.drop_type;
  8530. if(id == 0)
  8531. continue;
  8532. if(id == -1){
  8533. int eq_num=0,eq_n[MAX_INVENTORY];
  8534. memset(eq_n,0,sizeof(eq_n));
  8535. for(i=0;i<MAX_INVENTORY;i++) {
  8536. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8537. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8538. || type&NMDT_ALL)
  8539. {
  8540. int l;
  8541. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  8542. if( l < MAX_INVENTORY )
  8543. eq_n[l] = i;
  8544. eq_num++;
  8545. }
  8546. }
  8547. if(eq_num > 0){
  8548. int n = eq_n[rnd()%eq_num];
  8549. if(rnd()%10000 < per) {
  8550. if(sd->inventory.u.items_inventory[n].equip)
  8551. pc_unequipitem(sd,n,3);
  8552. pc_dropitem(sd,n,1);
  8553. }
  8554. }
  8555. }
  8556. else if(id > 0) {
  8557. for(i=0;i<MAX_INVENTORY;i++){
  8558. if(sd->inventory.u.items_inventory[i].nameid == id
  8559. && rnd()%10000 < per
  8560. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8561. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8562. || type&NMDT_ALL) ){
  8563. if(sd->inventory.u.items_inventory[i].equip)
  8564. pc_unequipitem(sd,i,3);
  8565. pc_dropitem(sd,i,1);
  8566. break;
  8567. }
  8568. }
  8569. }
  8570. }
  8571. }
  8572. // pvp
  8573. // disable certain pvp functions on pk_mode [Valaris]
  8574. if( !battle_config.pk_mode && mapdata->getMapFlag(MF_PVP) && !mapdata->getMapFlag(MF_PVP_NOCALCRANK) ) {
  8575. sd->pvp_point -= 5;
  8576. sd->pvp_lost++;
  8577. if( src && src->type == BL_PC ) {
  8578. map_session_data *ssd = (map_session_data *)src;
  8579. ssd->pvp_point++;
  8580. ssd->pvp_won++;
  8581. }
  8582. if( sd->pvp_point < 0 ) {
  8583. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  8584. return 1|8;
  8585. }
  8586. }
  8587. //GvG
  8588. if( mapdata_flag_gvg2(mapdata) ) {
  8589. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  8590. return 1|8;
  8591. }
  8592. else if( sd->bg_id ) {
  8593. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  8594. if (bg) {
  8595. if (bg->cemetery.map > 0) { // Respawn by BG
  8596. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  8597. return 1|8;
  8598. }
  8599. }
  8600. }
  8601. //Reset "can log out" tick.
  8602. if( battle_config.prevent_logout )
  8603. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  8604. return 1;
  8605. }
  8606. void pc_revive(map_session_data *sd,unsigned int hp, unsigned int sp, unsigned int ap) {
  8607. if(hp) clif_updatestatus(sd,SP_HP);
  8608. if(sp) clif_updatestatus(sd,SP_SP);
  8609. if(ap) clif_updatestatus(sd,SP_AP);
  8610. pc_setstand(sd, true);
  8611. if(battle_config.pc_invincible_time > 0)
  8612. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8613. if (sd->state.gmaster_flag && sd->guild) {
  8614. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild->guild,GD_LEADERSHIP));
  8615. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild->guild,GD_GLORYWOUNDS));
  8616. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild->guild,GD_SOULCOLD));
  8617. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild->guild,GD_HAWKEYES));
  8618. }
  8619. }
  8620. bool pc_revive_item(map_session_data *sd) {
  8621. nullpo_retr(false, sd);
  8622. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  8623. return false;
  8624. if (sd->sc.getSCE(SC_HELLPOWER)) // Cannot resurrect while under the effect of SC_HELLPOWER.
  8625. return false;
  8626. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  8627. uint8 hp = 100, sp = 100;
  8628. if (item_position < 0) {
  8629. if (sd->sc.getSCE(SC_LIGHT_OF_REGENE)) {
  8630. hp = sd->sc.getSCE(SC_LIGHT_OF_REGENE)->val2;
  8631. sp = 0;
  8632. }
  8633. else
  8634. return false;
  8635. }
  8636. if (!status_revive(&sd->bl, hp, sp))
  8637. return false;
  8638. if (item_position < 0)
  8639. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE);
  8640. else
  8641. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  8642. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  8643. return true;
  8644. }
  8645. // script
  8646. //
  8647. /*==========================================
  8648. * script reading pc status registry
  8649. *------------------------------------------*/
  8650. int64 pc_readparam(map_session_data* sd,int64 type)
  8651. {
  8652. int64 val = 0;
  8653. nullpo_ret(sd);
  8654. switch(type) {
  8655. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  8656. case SP_STATUSPOINT: val = sd->status.status_point; break;
  8657. case SP_TRAITPOINT: val = sd->status.trait_point; break;
  8658. case SP_ZENY: val = sd->status.zeny; break;
  8659. case SP_BASELEVEL: val = sd->status.base_level; break;
  8660. case SP_JOBLEVEL: val = sd->status.job_level; break;
  8661. case SP_CLASS: val = sd->status.class_; break;
  8662. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  8663. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  8664. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  8665. case SP_SEX: val = sd->status.sex; break;
  8666. case SP_WEIGHT: val = sd->weight; break;
  8667. case SP_MAXWEIGHT: val = sd->max_weight; break;
  8668. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  8669. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  8670. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  8671. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  8672. case SP_HP: val = sd->battle_status.hp; break;
  8673. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  8674. case SP_SP: val = sd->battle_status.sp; break;
  8675. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  8676. case SP_AP: val = sd->battle_status.ap; break;
  8677. case SP_MAXAP: val = sd->battle_status.max_ap; break;
  8678. case SP_STR: val = sd->status.str; break;
  8679. case SP_AGI: val = sd->status.agi; break;
  8680. case SP_VIT: val = sd->status.vit; break;
  8681. case SP_INT: val = sd->status.int_; break;
  8682. case SP_DEX: val = sd->status.dex; break;
  8683. case SP_LUK: val = sd->status.luk; break;
  8684. case SP_POW: val = sd->status.pow; break;
  8685. case SP_STA: val = sd->status.sta; break;
  8686. case SP_WIS: val = sd->status.wis; break;
  8687. case SP_SPL: val = sd->status.spl; break;
  8688. case SP_CON: val = sd->status.con; break;
  8689. case SP_CRT: val = sd->status.crt; break;
  8690. case SP_KARMA: val = sd->status.karma; break;
  8691. case SP_MANNER: val = sd->status.manner; break;
  8692. case SP_FAME: val = sd->status.fame; break;
  8693. case SP_KILLERRID: val = sd->killerrid; break;
  8694. case SP_KILLEDRID: val = sd->killedrid; break;
  8695. case SP_KILLEDGID: val = sd->killedgid; break;
  8696. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  8697. case SP_CHARMOVE: val = sd->status.character_moves; break;
  8698. case SP_CHARRENAME: val = sd->status.rename; break;
  8699. case SP_CHARFONT: val = sd->status.font; break;
  8700. case SP_BANK_VAULT: val = sd->bank_vault; break;
  8701. case SP_CASHPOINTS: val = sd->cashPoints; break;
  8702. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  8703. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  8704. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  8705. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  8706. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  8707. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  8708. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  8709. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  8710. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  8711. case SP_BASE_ATK:
  8712. #ifdef RENEWAL
  8713. val = sd->bonus.eatk;
  8714. #else
  8715. val = sd->battle_status.batk;
  8716. #endif
  8717. break;
  8718. case SP_DEF1: val = sd->battle_status.def; break;
  8719. case SP_DEF2: val = sd->battle_status.def2; break;
  8720. case SP_MDEF1: val = sd->battle_status.mdef; break;
  8721. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  8722. case SP_HIT: val = sd->battle_status.hit; break;
  8723. case SP_FLEE1: val = sd->battle_status.flee; break;
  8724. case SP_FLEE2: val = sd->battle_status.flee2; break;
  8725. case SP_PATK: val = sd->battle_status.patk; break;
  8726. case SP_SMATK: val = sd->battle_status.smatk; break;
  8727. case SP_RES: val = sd->battle_status.res; break;
  8728. case SP_MRES: val = sd->battle_status.mres; break;
  8729. case SP_HPLUS: val = sd->battle_status.hplus; break;
  8730. case SP_CRATE: val = sd->battle_status.crate; break;
  8731. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  8732. case SP_MAXHPRATE: val = sd->hprate; break;
  8733. case SP_MAXSPRATE: val = sd->sprate; break;
  8734. case SP_MAXAPRATE: val = sd->aprate; break;
  8735. case SP_SPRATE: val = sd->dsprate; break;
  8736. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  8737. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  8738. case SP_ASPD_RATE:
  8739. #ifndef RENEWAL_ASPD
  8740. val = sd->battle_status.aspd_rate;
  8741. #else
  8742. val = sd->battle_status.aspd_rate2;
  8743. #endif
  8744. break;
  8745. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  8746. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  8747. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  8748. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  8749. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  8750. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  8751. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  8752. case SP_MATK_RATE: val = sd->matk_rate; break;
  8753. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  8754. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  8755. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  8756. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  8757. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  8758. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  8759. case SP_HIT_RATE: val = sd->hit_rate; break;
  8760. case SP_FLEE_RATE: val = sd->flee_rate; break;
  8761. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  8762. case SP_DEF_RATE: val = sd->def_rate; break;
  8763. case SP_DEF2_RATE: val = sd->def2_rate; break;
  8764. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  8765. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  8766. case SP_PATK_RATE: val = sd->patk_rate; break;
  8767. case SP_SMATK_RATE: val = sd->smatk_rate; break;
  8768. case SP_RES_RATE: val = sd->res_rate; break;
  8769. case SP_MRES_RATE: val = sd->mres_rate; break;
  8770. case SP_HPLUS_RATE: val = sd->hplus_rate; break;
  8771. case SP_CRATE_RATE: val = sd->crate_rate; break;
  8772. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  8773. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  8774. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  8775. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  8776. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  8777. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  8778. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  8779. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  8780. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  8781. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  8782. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  8783. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  8784. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  8785. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  8786. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  8787. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  8788. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  8789. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  8790. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  8791. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  8792. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  8793. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  8794. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  8795. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  8796. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  8797. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  8798. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  8799. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  8800. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  8801. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  8802. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  8803. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  8804. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  8805. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  8806. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  8807. case SP_UNSTRIPABLE:
  8808. case SP_UNSTRIPABLE_ARMOR:
  8809. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  8810. break;
  8811. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  8812. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  8813. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  8814. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  8815. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  8816. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  8817. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  8818. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  8819. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  8820. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  8821. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  8822. case SP_EMATK: val = sd->bonus.ematk; break;
  8823. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  8824. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  8825. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  8826. case SP_CASTRATE:
  8827. case SP_VARCASTRATE:
  8828. #ifdef RENEWAL_CAST
  8829. val = sd->bonus.varcastrate; break;
  8830. #else
  8831. val = sd->castrate; break;
  8832. #endif
  8833. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  8834. case SP_ADD_ITEM_SPHEAL_RATE: val = sd->bonus.itemsphealrate2; break;
  8835. default:
  8836. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  8837. return -1;
  8838. }
  8839. return val;
  8840. }
  8841. /*==========================================
  8842. * script set pc status registry
  8843. *------------------------------------------*/
  8844. bool pc_setparam(map_session_data *sd,int64 type,int64 val_tmp)
  8845. {
  8846. nullpo_retr(false,sd);
  8847. int val = static_cast<unsigned int>(val_tmp);
  8848. switch(type){
  8849. case SP_BASELEVEL:
  8850. if (val > pc_maxbaselv(sd)) //Capping to max
  8851. val = pc_maxbaselv(sd);
  8852. if (val > sd->status.base_level) {
  8853. for( int i = 0; i < (int)( val - sd->status.base_level ); i++ ){
  8854. sd->status.status_point += statpoint_db.pc_gets_status_point( sd->status.base_level + i );
  8855. sd->status.trait_point += statpoint_db.pc_gets_trait_point( sd->status.base_level + i );
  8856. }
  8857. }
  8858. sd->status.base_level = val;
  8859. sd->status.base_exp = 0;
  8860. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  8861. clif_updatestatus(sd, SP_NEXTBASEEXP);
  8862. clif_updatestatus(sd, SP_STATUSPOINT);
  8863. clif_updatestatus(sd, SP_TRAITPOINT);
  8864. clif_updatestatus(sd, SP_BASEEXP);
  8865. status_calc_pc(sd, SCO_FORCE);
  8866. if(sd->status.party_id)
  8867. party_send_levelup(sd);
  8868. break;
  8869. case SP_JOBLEVEL:
  8870. if (val >= sd->status.job_level) {
  8871. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  8872. sd->status.skill_point += val - sd->status.job_level;
  8873. clif_updatestatus(sd, SP_SKILLPOINT);
  8874. }
  8875. sd->status.job_level = val;
  8876. sd->status.job_exp = 0;
  8877. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  8878. clif_updatestatus(sd, SP_NEXTJOBEXP);
  8879. clif_updatestatus(sd, SP_JOBEXP);
  8880. status_calc_pc(sd, SCO_FORCE);
  8881. break;
  8882. case SP_SKILLPOINT:
  8883. sd->status.skill_point = val;
  8884. break;
  8885. case SP_STATUSPOINT:
  8886. sd->status.status_point = val;
  8887. break;
  8888. case SP_TRAITPOINT:
  8889. sd->status.trait_point = val;
  8890. break;
  8891. case SP_ZENY:
  8892. if( val < 0 )
  8893. return false;// can't set negative zeny
  8894. log_zeny(*sd, LOG_TYPE_SCRIPT, sd->status.char_id, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  8895. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  8896. break;
  8897. case SP_BASEEXP:
  8898. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  8899. if (val_tmp < sd->status.base_exp) // Lost
  8900. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  8901. else // Gained
  8902. pc_gainexp(sd, NULL, val_tmp - sd->status.base_exp, 0, 2);
  8903. return true;
  8904. case SP_JOBEXP:
  8905. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  8906. if (val_tmp < sd->status.job_exp) // Lost
  8907. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  8908. else // Gained
  8909. pc_gainexp(sd, NULL, 0, val_tmp - sd->status.job_exp, 2);
  8910. return true;
  8911. case SP_SEX:
  8912. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  8913. break;
  8914. case SP_WEIGHT:
  8915. sd->weight = val;
  8916. break;
  8917. case SP_MAXWEIGHT:
  8918. sd->max_weight = val;
  8919. break;
  8920. case SP_HP:
  8921. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  8922. break;
  8923. case SP_MAXHP:
  8924. if (sd->status.base_level < 100)
  8925. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  8926. else if (sd->status.base_level < 151)
  8927. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  8928. else
  8929. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  8930. if( sd->battle_status.max_hp < sd->battle_status.hp )
  8931. {
  8932. sd->battle_status.hp = sd->battle_status.max_hp;
  8933. clif_updatestatus(sd, SP_HP);
  8934. }
  8935. break;
  8936. case SP_SP:
  8937. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  8938. break;
  8939. case SP_MAXSP:
  8940. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  8941. if( sd->battle_status.max_sp < sd->battle_status.sp )
  8942. {
  8943. sd->battle_status.sp = sd->battle_status.max_sp;
  8944. clif_updatestatus(sd, SP_SP);
  8945. }
  8946. break;
  8947. case SP_AP:
  8948. sd->battle_status.ap = cap_value(val, 0, (int)sd->battle_status.max_ap);
  8949. break;
  8950. case SP_MAXAP:
  8951. sd->battle_status.max_ap = cap_value(val, 1, battle_config.max_ap);
  8952. if (sd->battle_status.max_ap < sd->battle_status.ap) {
  8953. sd->battle_status.ap = sd->battle_status.max_ap;
  8954. clif_updatestatus(sd, SP_AP);
  8955. }
  8956. break;
  8957. case SP_STR:
  8958. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  8959. break;
  8960. case SP_AGI:
  8961. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  8962. break;
  8963. case SP_VIT:
  8964. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  8965. break;
  8966. case SP_INT:
  8967. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  8968. break;
  8969. case SP_DEX:
  8970. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  8971. break;
  8972. case SP_LUK:
  8973. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  8974. break;
  8975. case SP_POW:
  8976. sd->status.pow = cap_value(val, 0, pc_maxparameter(sd,PARAM_POW));
  8977. break;
  8978. case SP_STA:
  8979. sd->status.sta = cap_value(val, 0, pc_maxparameter(sd,PARAM_STA));
  8980. break;
  8981. case SP_WIS:
  8982. sd->status.wis = cap_value(val, 0, pc_maxparameter(sd,PARAM_WIS));
  8983. break;
  8984. case SP_SPL:
  8985. sd->status.spl = cap_value(val, 0, pc_maxparameter(sd,PARAM_SPL));
  8986. break;
  8987. case SP_CON:
  8988. sd->status.con = cap_value(val, 0, pc_maxparameter(sd,PARAM_CON));
  8989. break;
  8990. case SP_CRT:
  8991. sd->status.crt = cap_value(val, 0, pc_maxparameter(sd,PARAM_CRT));
  8992. break;
  8993. case SP_KARMA:
  8994. sd->status.karma = val;
  8995. break;
  8996. case SP_MANNER:
  8997. sd->status.manner = val;
  8998. if( val < 0 )
  8999. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  9000. else {
  9001. status_change_end(&sd->bl, SC_NOCHAT);
  9002. clif_manner_message(sd, 5);
  9003. }
  9004. return true; // status_change_start/status_change_end already sends packets warning the client
  9005. case SP_FAME:
  9006. sd->status.fame = val;
  9007. break;
  9008. case SP_KILLERRID:
  9009. sd->killerrid = val;
  9010. return true;
  9011. case SP_KILLEDRID:
  9012. sd->killedrid = val;
  9013. return true;
  9014. case SP_KILLEDGID:
  9015. sd->killedgid = val;
  9016. return true;
  9017. case SP_CHARMOVE:
  9018. sd->status.character_moves = val;
  9019. return true;
  9020. case SP_CHARRENAME:
  9021. sd->status.rename = val;
  9022. return true;
  9023. case SP_CHARFONT:
  9024. sd->status.font = val;
  9025. clif_font(sd);
  9026. return true;
  9027. case SP_BANK_VAULT:
  9028. if (val < 0)
  9029. return false;
  9030. log_zeny(*sd, LOG_TYPE_BANK, sd->status.char_id, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  9031. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  9032. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  9033. return true;
  9034. case SP_ROULETTE_BRONZE:
  9035. sd->roulette_point.bronze = val;
  9036. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  9037. return true;
  9038. case SP_ROULETTE_SILVER:
  9039. sd->roulette_point.silver = val;
  9040. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  9041. return true;
  9042. case SP_ROULETTE_GOLD:
  9043. sd->roulette_point.gold = val;
  9044. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  9045. return true;
  9046. case SP_CASHPOINTS:
  9047. if (val < 0)
  9048. return false;
  9049. if (!sd->state.connect_new)
  9050. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_CASHPOINT)));
  9051. sd->cashPoints = cap_value(val, 0, MAX_CASHPOINT);
  9052. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  9053. return true;
  9054. case SP_KAFRAPOINTS:
  9055. if (val < 0)
  9056. return false;
  9057. if (!sd->state.connect_new)
  9058. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_KAFRAPOINT)));
  9059. sd->kafraPoints = cap_value(val, 0, MAX_KAFRAPOINT);
  9060. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  9061. return true;
  9062. case SP_PCDIECOUNTER:
  9063. if (val < 0)
  9064. return false;
  9065. if (sd->die_counter == val)
  9066. return true;
  9067. sd->die_counter = val;
  9068. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  9069. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  9070. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  9071. return true;
  9072. case SP_COOKMASTERY:
  9073. if (sd->cook_mastery == val)
  9074. return true;
  9075. val = cap_value(val, 0, 1999);
  9076. sd->cook_mastery = val;
  9077. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  9078. return true;
  9079. default:
  9080. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  9081. return false;
  9082. }
  9083. clif_updatestatus(sd,static_cast<int>(type));
  9084. return true;
  9085. }
  9086. /*==========================================
  9087. * HP/SP/AP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  9088. *------------------------------------------*/
  9089. void pc_heal(map_session_data *sd,unsigned int hp,unsigned int sp, unsigned int ap, int type)
  9090. {// Is there going to be a effect for gaining AP soon??? [Rytech]
  9091. nullpo_retv(sd);
  9092. if (type&2) {
  9093. if (hp || type&4) {
  9094. clif_heal(sd->fd,SP_HP,hp);
  9095. clif_update_hp(*sd);
  9096. }
  9097. if (sp)
  9098. clif_heal(sd->fd,SP_SP,sp);
  9099. if (ap)
  9100. clif_heal(sd->fd,SP_AP,ap);
  9101. } else {
  9102. if(hp)
  9103. clif_updatestatus(sd,SP_HP);
  9104. if(sp)
  9105. clif_updatestatus(sd,SP_SP);
  9106. if (ap)
  9107. clif_updatestatus(sd,SP_AP);
  9108. }
  9109. return;
  9110. }
  9111. /**
  9112. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  9113. * @param sd: Player data
  9114. * @param itemid: Item ID
  9115. * @param hp: HP to heal
  9116. * @param sp: SP to heal
  9117. * @return Amount healed to an object
  9118. */
  9119. int pc_itemheal(map_session_data *sd, t_itemid itemid, int hp, int sp)
  9120. {
  9121. int bonus, tmp, penalty = 0;
  9122. if (hp) {
  9123. bonus = 100 + (sd->battle_status.vit * 2) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9124. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9125. if (potion_flag == 2) {
  9126. bonus += bonus * 50 / 100;
  9127. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_ROGUE)
  9128. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  9129. }
  9130. //All item bonuses.
  9131. bonus += sd->bonus.itemhealrate2;
  9132. //Item Group bonuses
  9133. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid, sd->itemgrouphealrate) / 100;
  9134. //Individual item bonuses.
  9135. for(const auto &it : sd->itemhealrate) {
  9136. if (it.id == itemid) {
  9137. bonus += bonus * it.val / 100;
  9138. break;
  9139. }
  9140. }
  9141. // Recovery Potion
  9142. if (sd->sc.getSCE(SC_INCHEALRATE))
  9143. bonus += bonus * sd->sc.getSCE(SC_INCHEALRATE)->val1 / 100;
  9144. // 2014 Halloween Event : Pumpkin Bonus
  9145. if (sd->sc.getSCE(SC_MTF_PUMPKIN)) {
  9146. if (itemid == ITEMID_PUMPKIN)
  9147. bonus += bonus * sd->sc.getSCE(SC_MTF_PUMPKIN)->val1 / 100;
  9148. else if (itemid == ITEMID_COOKIE_BAT)
  9149. bonus += sd->sc.getSCE(SC_MTF_PUMPKIN)->val2;
  9150. }
  9151. if (sd->sc.getSCE(SC_VITALIZE_POTION))
  9152. bonus += bonus * 10 / 100;
  9153. tmp = hp * bonus / 100; // Overflow check
  9154. if (bonus != 100 && tmp > hp)
  9155. hp = tmp;
  9156. }
  9157. if (sp) {
  9158. bonus = 100 + (sd->battle_status.int_ * 2) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9159. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9160. if (potion_flag == 2)
  9161. bonus += bonus * 50 / 100;
  9162. // All item bonuses.
  9163. bonus += sd->bonus.itemsphealrate2;
  9164. // Item Group bonuses
  9165. bonus += bonus * pc_get_itemgroup_bonus( sd, itemid, sd->itemgroupsphealrate ) / 100;
  9166. // Individual item bonuses.
  9167. for( const auto &it : sd->itemsphealrate ){
  9168. if( it.id == itemid ){
  9169. bonus += bonus * it.val / 100;
  9170. break;
  9171. }
  9172. }
  9173. tmp = sp * bonus / 100; // Overflow check
  9174. if (bonus != 100 && tmp > sp)
  9175. sp = tmp;
  9176. }
  9177. if (sd->sc.count) {
  9178. // Critical Wound and Death Hurt stack
  9179. if (sd->sc.getSCE(SC_CRITICALWOUND))
  9180. penalty += sd->sc.getSCE(SC_CRITICALWOUND)->val2;
  9181. if (sd->sc.getSCE(SC_DEATHHURT) && sd->sc.getSCE(SC_DEATHHURT)->val3 == 1)
  9182. penalty += 20;
  9183. if (sd->sc.getSCE(SC_NORECOVER_STATE))
  9184. penalty = 100;
  9185. if (sd->sc.getSCE(SC_VITALITYACTIVATION))
  9186. hp += hp / 2; // 1.5 times
  9187. if (sd->sc.getSCE(SC_WATER_INSIGNIA) && sd->sc.getSCE(SC_WATER_INSIGNIA)->val1 == 2) {
  9188. hp += hp / 10;
  9189. sp += sp / 10;
  9190. }
  9191. #ifdef RENEWAL
  9192. if (sd->sc.getSCE(SC_APPLEIDUN))
  9193. hp += sd->sc.getSCE(SC_APPLEIDUN)->val3 / 100;
  9194. #endif
  9195. if (penalty > 0) {
  9196. hp -= hp * penalty / 100;
  9197. sp -= sp * penalty / 100;
  9198. }
  9199. #ifdef RENEWAL
  9200. if (sd->sc.getSCE(SC_EXTREMITYFIST2))
  9201. sp = 0;
  9202. #endif
  9203. if (sd->sc.getSCE(SC_BITESCAR))
  9204. hp = 0;
  9205. }
  9206. return status_heal(&sd->bl, hp, sp, 1);
  9207. }
  9208. /*==========================================
  9209. * HP/SP Recovery
  9210. * Heal player hp nad/or sp by rate
  9211. *------------------------------------------*/
  9212. int pc_percentheal(map_session_data *sd,int hp,int sp)
  9213. {
  9214. nullpo_ret(sd);
  9215. if (hp > 100) hp = 100;
  9216. else if (hp <-100) hp = -100;
  9217. if (sp > 100) sp = 100;
  9218. else if (sp <-100) sp = -100;
  9219. if(hp >= 0 && sp >= 0) //Heal
  9220. return status_percent_heal(&sd->bl, hp, sp);
  9221. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  9222. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  9223. //Crossed signs
  9224. if(hp) {
  9225. if(hp > 0)
  9226. status_percent_heal(&sd->bl, hp, 0);
  9227. else
  9228. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  9229. }
  9230. if(sp) {
  9231. if(sp > 0)
  9232. status_percent_heal(&sd->bl, 0, sp);
  9233. else
  9234. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  9235. }
  9236. return 0;
  9237. }
  9238. static int jobchange_killclone(struct block_list *bl, va_list ap)
  9239. {
  9240. struct mob_data *md;
  9241. int flag;
  9242. md = (struct mob_data *)bl;
  9243. nullpo_ret(md);
  9244. flag = va_arg(ap, int);
  9245. if (md->master_id && md->special_state.clone && md->master_id == flag)
  9246. status_kill(&md->bl);
  9247. return 1;
  9248. }
  9249. /**
  9250. * Called when player changes job
  9251. * Rewrote to make it tidider [Celest]
  9252. * @param sd
  9253. * @param job JOB ID. See enum e_job
  9254. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  9255. * @return True if success, false if failed
  9256. **/
  9257. bool pc_jobchange(map_session_data *sd,int job, char upper)
  9258. {
  9259. int i, fame_flag = 0;
  9260. nullpo_retr(false,sd);
  9261. if (job < 0)
  9262. return false;
  9263. //Normalize job.
  9264. uint64 b_class = pc_jobid2mapid(job);
  9265. if (b_class == -1)
  9266. return false;
  9267. switch (upper) {
  9268. case 1:
  9269. b_class|= JOBL_UPPER;
  9270. break;
  9271. case 2:
  9272. b_class|= JOBL_BABY;
  9273. break;
  9274. }
  9275. //This will automatically adjust bard/dancer classes to the correct gender
  9276. //That is, if you try to jobchange into dancer, it will turn you to bard.
  9277. job = pc_mapid2jobid(b_class, sd->status.sex);
  9278. if (job == -1)
  9279. return false;
  9280. if ((unsigned short)b_class == sd->class_)
  9281. return false; //Nothing to change.
  9282. // If the job does not exist in the job db, dont allow changing to it
  9283. if( !job_db.exists( job ) ){
  9284. return false;
  9285. }
  9286. if( ( b_class&JOBL_FOURTH ) && !( sd->class_&JOBL_FOURTH ) ){
  9287. // Changing to 4th job
  9288. sd->change_level_4th = sd->status.job_level;
  9289. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ) , sd->change_level_4th );
  9290. }else if( ( b_class&JOBL_THIRD ) && !( sd->class_&JOBL_THIRD ) ){
  9291. // changing from 2nd to 3rd job
  9292. sd->change_level_3rd = sd->status.job_level;
  9293. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  9294. }else if( ( b_class&JOBL_2 ) && !( sd->class_&JOBL_2 ) ){
  9295. // changing from 1st to 2nd job
  9296. sd->change_level_2nd = sd->status.job_level;
  9297. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  9298. }
  9299. if(sd->cloneskill_idx > 0) {
  9300. pc_skill_plagiarism_reset(*sd, 1);
  9301. }
  9302. if(sd->reproduceskill_idx > 0) {
  9303. pc_skill_plagiarism_reset(*sd, 2);
  9304. }
  9305. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  9306. status_db.changeSkillTree(sd);
  9307. }
  9308. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  9309. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  9310. pc_resetfeel(sd);
  9311. pc_resethate(sd);
  9312. }
  9313. // Reset body style to 0 before changing job to avoid
  9314. // errors since not every job has a alternate outfit.
  9315. sd->status.body = 0;
  9316. clif_changelook(&sd->bl,LOOK_BODY2,0);
  9317. sd->status.class_ = job;
  9318. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  9319. uint64 previous_class = sd->class_;
  9320. sd->class_ = (unsigned short)b_class;
  9321. sd->status.job_level=1;
  9322. sd->status.job_exp=0;
  9323. if (sd->status.base_level > pc_maxbaselv(sd)) {
  9324. sd->status.base_level = pc_maxbaselv(sd);
  9325. sd->status.base_exp=0;
  9326. pc_resetstate(sd);
  9327. clif_updatestatus(sd,SP_STATUSPOINT);
  9328. clif_updatestatus(sd,SP_TRAITPOINT);
  9329. clif_updatestatus(sd,SP_BASELEVEL);
  9330. clif_updatestatus(sd,SP_BASEEXP);
  9331. clif_updatestatus(sd,SP_NEXTBASEEXP);
  9332. }
  9333. // Give or reduce transcendent status points
  9334. if( (b_class&JOBL_UPPER) && !(previous_class&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  9335. sd->status.status_point += battle_config.transcendent_status_points;
  9336. clif_updatestatus(sd,SP_STATUSPOINT);
  9337. }else if( !(b_class&JOBL_UPPER) && (previous_class&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  9338. if( sd->status.status_point < battle_config.transcendent_status_points ){
  9339. // The player already used his bonus points, so we have to reset his status points
  9340. pc_resetstate(sd);
  9341. }else{
  9342. sd->status.status_point -= battle_config.transcendent_status_points;
  9343. clif_updatestatus(sd,SP_STATUSPOINT);
  9344. }
  9345. }
  9346. // Give or reduce trait status points
  9347. if ((b_class & JOBL_FOURTH) && !(previous_class & JOBL_FOURTH)) {// Change to a 4th job.
  9348. sd->status.trait_point += battle_config.trait_points_job_change;
  9349. clif_updatestatus(sd, SP_TRAITPOINT);
  9350. clif_updatestatus(sd, SP_UPOW);
  9351. clif_updatestatus(sd, SP_USTA);
  9352. clif_updatestatus(sd, SP_UWIS);
  9353. clif_updatestatus(sd, SP_USPL);
  9354. clif_updatestatus(sd, SP_UCON);
  9355. clif_updatestatus(sd, SP_UCRT);
  9356. } else if (!(b_class & JOBL_FOURTH) && (previous_class & JOBL_FOURTH)) {// Change to a non 4th job.
  9357. if (sd->status.trait_point < battle_config.trait_points_job_change) {
  9358. // Player may have already used the trait status points. Force a reset.
  9359. pc_resetstate(sd);
  9360. } else {
  9361. sd->status.trait_point = 0;
  9362. clif_updatestatus(sd, SP_TRAITPOINT);
  9363. clif_updatestatus(sd, SP_UPOW);
  9364. clif_updatestatus(sd, SP_USTA);
  9365. clif_updatestatus(sd, SP_UWIS);
  9366. clif_updatestatus(sd, SP_USPL);
  9367. clif_updatestatus(sd, SP_UCON);
  9368. clif_updatestatus(sd, SP_UCRT);
  9369. }
  9370. }
  9371. clif_updatestatus(sd,SP_JOBLEVEL);
  9372. clif_updatestatus(sd,SP_JOBEXP);
  9373. clif_updatestatus(sd,SP_NEXTJOBEXP);
  9374. for(i=0;i<EQI_MAX;i++) {
  9375. if(sd->equip_index[i] >= 0)
  9376. if(pc_isequip(sd,sd->equip_index[i]))
  9377. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  9378. }
  9379. //Change look, if disguised, you need to undisguise
  9380. //to correctly calculate new job sprite without
  9381. if (sd->disguise)
  9382. pc_disguise(sd, 0);
  9383. status_set_viewdata(&sd->bl, job);
  9384. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  9385. #if PACKETVER >= 20151001
  9386. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  9387. #endif
  9388. if(sd->vd.cloth_color)
  9389. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9390. /*
  9391. if(sd->vd.body_style)
  9392. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9393. */
  9394. //Update skill tree.
  9395. pc_calc_skilltree(sd);
  9396. clif_skillinfoblock(sd);
  9397. if (sd->ed)
  9398. elemental_delete(sd->ed);
  9399. if (sd->state.vending)
  9400. vending_closevending(sd);
  9401. if (sd->state.buyingstore)
  9402. buyingstore_close(sd);
  9403. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  9404. //Remove peco/cart/falcon
  9405. i = sd->sc.option;
  9406. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  9407. i&=~OPTION_RIDING;
  9408. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  9409. i&=~OPTION_FALCON;
  9410. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  9411. i&=~OPTION_DRAGON;
  9412. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  9413. i&=~OPTION_WUGRIDER;
  9414. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  9415. i&=~OPTION_WUG;
  9416. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  9417. i&=~OPTION_MADOGEAR;
  9418. #ifndef NEW_CARTS
  9419. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  9420. i&=~OPTION_CART;
  9421. #else
  9422. if( sd->sc.getSCE(SC_PUSH_CART) && !pc_checkskill(sd, MC_PUSHCART) )
  9423. pc_setcart(sd, 0);
  9424. #endif
  9425. if(i != sd->sc.option)
  9426. pc_setoption(sd, i);
  9427. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  9428. hom_vaporize(sd, HOM_ST_ACTIVE);
  9429. if (sd->sc.getSCE(SC_SPRITEMABLE) && !pc_checkskill(sd, SU_SPRITEMABLE))
  9430. status_change_end(&sd->bl, SC_SPRITEMABLE);
  9431. if (sd->sc.getSCE(SC_SOULATTACK) && !pc_checkskill(sd, SU_SOULATTACK))
  9432. status_change_end(&sd->bl, SC_SOULATTACK);
  9433. if( sd->sc.getSCE(SC_SPIRIT) ){
  9434. status_change_end( &sd->bl, SC_SPIRIT );
  9435. }
  9436. if(sd->status.manner < 0)
  9437. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  9438. status_calc_pc(sd,SCO_FORCE);
  9439. pc_checkallowskill(sd);
  9440. pc_equiplookall(sd);
  9441. pc_show_questinfo(sd);
  9442. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  9443. if( sd->status.party_id ){
  9444. struct party_data* p;
  9445. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  9446. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  9447. if( i < MAX_PARTY ){
  9448. p->party.member[i].class_ = sd->status.class_;
  9449. clif_party_job_and_level( *sd );
  9450. }
  9451. }
  9452. }
  9453. chrif_save(sd, CSAVE_NORMAL);
  9454. //if you were previously famous, not anymore.
  9455. if (fame_flag)
  9456. chrif_buildfamelist();
  9457. else if (sd->status.fame > 0) {
  9458. //It may be that now they are famous?
  9459. switch (sd->class_&MAPID_UPPERMASK) {
  9460. case MAPID_BLACKSMITH:
  9461. case MAPID_ALCHEMIST:
  9462. case MAPID_TAEKWON:
  9463. chrif_buildfamelist();
  9464. break;
  9465. }
  9466. }
  9467. return true;
  9468. }
  9469. /*==========================================
  9470. * Tell client player sd has change equipement
  9471. *------------------------------------------*/
  9472. void pc_equiplookall(map_session_data *sd)
  9473. {
  9474. nullpo_retv(sd);
  9475. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  9476. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9477. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  9478. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  9479. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  9480. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  9481. }
  9482. /*==========================================
  9483. * Tell client player sd has change look (hair,equip...)
  9484. *------------------------------------------*/
  9485. void pc_changelook(map_session_data *sd,int type,int val) {
  9486. nullpo_retv(sd);
  9487. switch(type) {
  9488. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  9489. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  9490. if (sd->status.hair != val) {
  9491. sd->status.hair = val;
  9492. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9493. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9494. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  9495. }
  9496. break;
  9497. case LOOK_WEAPON:
  9498. sd->status.weapon = val;
  9499. break;
  9500. case LOOK_HEAD_BOTTOM:
  9501. sd->status.head_bottom = val;
  9502. sd->setlook_head_bottom = val;
  9503. break;
  9504. case LOOK_HEAD_TOP:
  9505. sd->status.head_top = val;
  9506. sd->setlook_head_top = val;
  9507. break;
  9508. case LOOK_HEAD_MID:
  9509. sd->status.head_mid = val;
  9510. sd->setlook_head_mid = val;
  9511. break;
  9512. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  9513. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  9514. if (sd->status.hair_color != val) {
  9515. sd->status.hair_color = val;
  9516. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9517. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9518. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  9519. }
  9520. break;
  9521. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  9522. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  9523. sd->status.clothes_color = val;
  9524. break;
  9525. case LOOK_SHIELD:
  9526. sd->status.shield = val;
  9527. break;
  9528. case LOOK_SHOES:
  9529. break;
  9530. case LOOK_ROBE:
  9531. sd->status.robe = val;
  9532. sd->setlook_robe = val;
  9533. break;
  9534. case LOOK_BODY2:
  9535. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  9536. sd->status.body = val;
  9537. break;
  9538. }
  9539. clif_changelook(&sd->bl, type, val);
  9540. }
  9541. /*==========================================
  9542. * Give an option (type) to player (sd) and display it to client
  9543. *------------------------------------------*/
  9544. void pc_setoption(map_session_data *sd,int type, int subtype)
  9545. {
  9546. int p_type, new_look=0;
  9547. nullpo_retv(sd);
  9548. p_type = sd->sc.option;
  9549. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  9550. sd->sc.option=type;
  9551. clif_changeoption(&sd->bl);
  9552. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9553. { // Mounting
  9554. clif_status_load(&sd->bl,EFST_RIDING,1);
  9555. status_calc_pc(sd,SCO_NONE);
  9556. }
  9557. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9558. { // Dismount
  9559. clif_status_load(&sd->bl,EFST_RIDING,0);
  9560. status_calc_pc(sd,SCO_NONE);
  9561. }
  9562. #ifndef NEW_CARTS
  9563. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  9564. clif_cartlist(sd);
  9565. clif_updatestatus(sd, SP_CARTINFO);
  9566. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9567. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  9568. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  9569. clif_clearcart(sd->fd);
  9570. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9571. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  9572. }
  9573. #endif
  9574. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  9575. clif_status_load(&sd->bl,EFST_FALCON,1);
  9576. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  9577. clif_status_load(&sd->bl,EFST_FALCON,0);
  9578. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  9579. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  9580. status_calc_pc(sd,SCO_NONE);
  9581. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  9582. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  9583. status_calc_pc(sd,SCO_NONE);
  9584. }
  9585. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  9586. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  9587. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  9588. status_change_end(&sd->bl, SC_MADOGEAR);
  9589. }
  9590. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  9591. new_look = JOB_STAR_GLADIATOR2;
  9592. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  9593. new_look = -1;
  9594. if (sd->disguise || !new_look)
  9595. return; //Disguises break sprite changes
  9596. if (new_look < 0) { //Restore normal look.
  9597. status_set_viewdata(&sd->bl, sd->status.class_);
  9598. new_look = sd->vd.class_;
  9599. }
  9600. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  9601. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  9602. if (sd->vd.cloth_color)
  9603. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9604. if( sd->vd.body_style )
  9605. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9606. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  9607. }
  9608. /**
  9609. * Give player a cart
  9610. * @param sd Player
  9611. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  9612. **/
  9613. bool pc_setcart(map_session_data *sd,int type) {
  9614. #ifndef NEW_CARTS
  9615. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  9616. int option;
  9617. #endif
  9618. nullpo_retr(false,sd);
  9619. if( type < 0 || type > MAX_CARTS )
  9620. return false;// Never trust the values sent by the client! [Skotlex]
  9621. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  9622. return false;// Push cart is required
  9623. #ifdef NEW_CARTS
  9624. switch( type ) {
  9625. case 0:
  9626. if( !sd->sc.getSCE(SC_PUSH_CART) )
  9627. return 0;
  9628. status_change_end(&sd->bl,SC_PUSH_CART);
  9629. clif_clearcart(sd->fd);
  9630. break;
  9631. default:/* everything else is an allowed ID so we can move on */
  9632. if( !sd->sc.getSCE(SC_PUSH_CART) ) { /* first time, so fill cart data */
  9633. clif_cartlist(sd);
  9634. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  9635. }
  9636. clif_updatestatus(sd, SP_CARTINFO);
  9637. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  9638. break;
  9639. }
  9640. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9641. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  9642. #else
  9643. // Update option
  9644. option = sd->sc.option;
  9645. option &= ~OPTION_CART;// clear cart bits
  9646. option |= cart[type]; // set cart
  9647. pc_setoption(sd, option);
  9648. #endif
  9649. return true;
  9650. }
  9651. /*==========================================
  9652. * Give player a falcon
  9653. *------------------------------------------*/
  9654. void pc_setfalcon(map_session_data* sd, int flag)
  9655. {
  9656. if( flag ){
  9657. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  9658. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  9659. } else if( pc_isfalcon(sd) ){
  9660. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  9661. }
  9662. }
  9663. /*==========================================
  9664. * Set player riding
  9665. *------------------------------------------*/
  9666. void pc_setriding(map_session_data* sd, int flag)
  9667. {
  9668. if( sd->sc.getSCE(SC_ALL_RIDING) )
  9669. return;
  9670. if( flag ){
  9671. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  9672. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  9673. } else if( pc_isriding(sd) ){
  9674. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  9675. }
  9676. }
  9677. /**
  9678. * Give player a mado
  9679. * @param sd: Player
  9680. * @param flag: Enable or disable mado
  9681. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  9682. */
  9683. void pc_setmadogear(map_session_data *sd, bool flag, e_mado_type type)
  9684. {
  9685. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  9686. return;
  9687. if (flag) {
  9688. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  9689. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  9690. } else if (pc_ismadogear(sd)) {
  9691. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  9692. }
  9693. }
  9694. /*==========================================
  9695. * Check if player can drop an item
  9696. *------------------------------------------*/
  9697. bool pc_candrop(map_session_data *sd, struct item *item)
  9698. {
  9699. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  9700. return false;
  9701. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  9702. return false;
  9703. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  9704. }
  9705. /*==========================================
  9706. * Read '@type' variables (temporary numeric char reg)
  9707. *------------------------------------------*/
  9708. int64 pc_readreg(map_session_data* sd, int64 reg)
  9709. {
  9710. return i64db_i64get(sd->regs.vars, reg);
  9711. }
  9712. /*==========================================
  9713. * Set '@type' variables (temporary numeric char reg)
  9714. *------------------------------------------*/
  9715. bool pc_setreg(map_session_data* sd, int64 reg, int64 val)
  9716. {
  9717. uint32 index = script_getvaridx(reg);
  9718. nullpo_retr(false, sd);
  9719. if( val ) {
  9720. i64db_i64put(sd->regs.vars, reg, val);
  9721. if( index )
  9722. script_array_update(&sd->regs, reg, false);
  9723. } else {
  9724. i64db_remove(sd->regs.vars, reg);
  9725. if( index )
  9726. script_array_update(&sd->regs, reg, true);
  9727. }
  9728. return true;
  9729. }
  9730. /*==========================================
  9731. * Read '@type$' variables (temporary string char reg)
  9732. *------------------------------------------*/
  9733. char* pc_readregstr(map_session_data* sd, int64 reg)
  9734. {
  9735. struct script_reg_str *p = NULL;
  9736. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9737. return p ? p->value : NULL;
  9738. }
  9739. /*==========================================
  9740. * Set '@type$' variables (temporary string char reg)
  9741. *------------------------------------------*/
  9742. bool pc_setregstr(map_session_data* sd, int64 reg, const char* str)
  9743. {
  9744. struct script_reg_str *p = NULL;
  9745. unsigned int index = script_getvaridx(reg);
  9746. DBData prev;
  9747. nullpo_retr(false, sd);
  9748. if( str[0] ) {
  9749. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9750. p->value = aStrdup(str);
  9751. p->flag.type = 1;
  9752. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9753. p = (struct script_reg_str *)db_data2ptr(&prev);
  9754. if( p->value )
  9755. aFree(p->value);
  9756. ers_free(str_reg_ers, p);
  9757. } else {
  9758. if( index )
  9759. script_array_update(&sd->regs, reg, false);
  9760. }
  9761. } else {
  9762. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  9763. p = (struct script_reg_str *)db_data2ptr(&prev);
  9764. if( p->value )
  9765. aFree(p->value);
  9766. ers_free(str_reg_ers, p);
  9767. if( index )
  9768. script_array_update(&sd->regs, reg, true);
  9769. }
  9770. }
  9771. return true;
  9772. }
  9773. /**
  9774. * Serves the following variable types:
  9775. * - 'type' (permanent numeric char reg)
  9776. * - '#type' (permanent numeric account reg)
  9777. * - '##type' (permanent numeric account reg2)
  9778. **/
  9779. int64 pc_readregistry(map_session_data *sd, int64 reg)
  9780. {
  9781. struct script_reg_num *p = NULL;
  9782. if (!sd->vars_ok) {
  9783. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9784. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9785. //intif->request_registry(sd,type==3?4:type);
  9786. set_eof(sd->fd);
  9787. return 0;
  9788. }
  9789. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  9790. return p ? p->value : 0;
  9791. }
  9792. /**
  9793. * Serves the following variable types:
  9794. * - 'type$' (permanent str char reg)
  9795. * - '#type$' (permanent str account reg)
  9796. * - '##type$' (permanent str account reg2)
  9797. **/
  9798. char* pc_readregistry_str(map_session_data *sd, int64 reg)
  9799. {
  9800. struct script_reg_str *p = NULL;
  9801. if (!sd->vars_ok) {
  9802. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9803. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9804. //intif->request_registry(sd,type==3?4:type);
  9805. set_eof(sd->fd);
  9806. return NULL;
  9807. }
  9808. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9809. return p ? p->value : NULL;
  9810. }
  9811. /**
  9812. * Serves the following variable types:
  9813. * - 'type' (permanent numeric char reg)
  9814. * - '#type' (permanent numeric account reg)
  9815. * - '##type' (permanent numeric account reg2)
  9816. **/
  9817. bool pc_setregistry(map_session_data *sd, int64 reg, int64 val)
  9818. {
  9819. struct script_reg_num *p = NULL;
  9820. const char *regname = get_str(script_getvarid(reg));
  9821. uint32 index = script_getvaridx(reg);
  9822. if ( !reg_load && !sd->vars_ok ) {
  9823. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  9824. return false;
  9825. }
  9826. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  9827. if( val ) {
  9828. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  9829. script_array_update(&sd->regs, reg, false);
  9830. p->value = val;
  9831. } else {
  9832. p->value = 0;
  9833. if( index )
  9834. script_array_update(&sd->regs, reg, true);
  9835. }
  9836. if (!reg_load)
  9837. p->flag.update = 1;/* either way, it will require either delete or replace */
  9838. } else if( val ) {
  9839. DBData prev;
  9840. if( index )
  9841. script_array_update(&sd->regs, reg, false);
  9842. p = ers_alloc(num_reg_ers, struct script_reg_num);
  9843. p->value = val;
  9844. if (!reg_load)
  9845. p->flag.update = 1;
  9846. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9847. p = (struct script_reg_num *)db_data2ptr(&prev);
  9848. ers_free(num_reg_ers, p);
  9849. }
  9850. }
  9851. if (!reg_load && p)
  9852. sd->vars_dirty = true;
  9853. return true;
  9854. }
  9855. /**
  9856. * Serves the following variable types:
  9857. * - 'type$' (permanent str char reg)
  9858. * - '#type$' (permanent str account reg)
  9859. * - '##type$' (permanent str account reg2)
  9860. **/
  9861. bool pc_setregistry_str(map_session_data *sd, int64 reg, const char *val)
  9862. {
  9863. struct script_reg_str *p = NULL;
  9864. const char *regname = get_str(script_getvarid(reg));
  9865. unsigned int index = script_getvaridx(reg);
  9866. size_t vlen = 0;
  9867. if (!reg_load && !sd->vars_ok) {
  9868. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  9869. return false;
  9870. }
  9871. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  9872. {
  9873. 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);
  9874. return false;
  9875. }
  9876. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  9877. if( val[0] ) {
  9878. if( p->value )
  9879. aFree(p->value);
  9880. else if ( index ) // an entry that was deleted, so we reset
  9881. script_array_update(&sd->regs, reg, false);
  9882. p->value = aStrdup(val);
  9883. } else {
  9884. if (p->value)
  9885. aFree(p->value);
  9886. p->value = NULL;
  9887. if( index )
  9888. script_array_update(&sd->regs, reg, true);
  9889. }
  9890. if( !reg_load )
  9891. p->flag.update = 1; // either way, it will require either delete or replace
  9892. } else if( val[0] ) {
  9893. DBData prev;
  9894. if( index )
  9895. script_array_update(&sd->regs, reg, false);
  9896. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9897. p->value = aStrdup(val);
  9898. if( !reg_load )
  9899. p->flag.update = 1;
  9900. p->flag.type = 1;
  9901. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  9902. p = (struct script_reg_str *)db_data2ptr(&prev);
  9903. if( p->value )
  9904. aFree(p->value);
  9905. ers_free(str_reg_ers, p);
  9906. }
  9907. }
  9908. if( !reg_load && p )
  9909. sd->vars_dirty = true;
  9910. return true;
  9911. }
  9912. /**
  9913. * Set value of player variable
  9914. * @param sd Player
  9915. * @param reg Variable name
  9916. * @param value
  9917. * @return True if success, false if failed.
  9918. **/
  9919. bool pc_setreg2(map_session_data *sd, const char *reg, int64 val) {
  9920. char prefix = reg[0];
  9921. nullpo_retr(false, sd);
  9922. if (reg[strlen(reg)-1] == '$') {
  9923. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9924. return false;
  9925. }
  9926. val = cap_value(val, INT_MIN, INT_MAX);
  9927. switch (prefix) {
  9928. case '.':
  9929. case '\'':
  9930. case '$':
  9931. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9932. return false;
  9933. case '@':
  9934. return pc_setreg(sd, add_str(reg), val);
  9935. case '#':
  9936. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  9937. default:
  9938. return pc_setglobalreg(sd, add_str(reg), val);
  9939. }
  9940. return false;
  9941. }
  9942. /**
  9943. * Get value of player variable
  9944. * @param sd Player
  9945. * @param reg Variable name
  9946. * @return Variable value or 0 if failed.
  9947. **/
  9948. int64 pc_readreg2(map_session_data *sd, const char *reg) {
  9949. char prefix = reg[0];
  9950. nullpo_ret(sd);
  9951. if (reg[strlen(reg)-1] == '$') {
  9952. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9953. return 0;
  9954. }
  9955. switch (prefix) {
  9956. case '.':
  9957. case '\'':
  9958. case '$':
  9959. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9960. return 0;
  9961. case '@':
  9962. return pc_readreg(sd, add_str(reg));
  9963. case '#':
  9964. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  9965. default:
  9966. return pc_readglobalreg(sd, add_str(reg));
  9967. }
  9968. return 0;
  9969. }
  9970. /*==========================================
  9971. * Exec eventtimer for player sd (retrieved from map_session (id))
  9972. *------------------------------------------*/
  9973. static TIMER_FUNC(pc_eventtimer){
  9974. map_session_data *sd=map_id2sd(id);
  9975. char *p = (char *)data;
  9976. int i;
  9977. if(sd==NULL)
  9978. return 0;
  9979. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  9980. if( i < MAX_EVENTTIMER )
  9981. {
  9982. sd->eventtimer[i] = INVALID_TIMER;
  9983. sd->eventcount--;
  9984. npc_event(sd,p,0);
  9985. }
  9986. else
  9987. ShowError("pc_eventtimer: no such event timer\n");
  9988. if (p) aFree(p);
  9989. return 0;
  9990. }
  9991. /*==========================================
  9992. * Add eventtimer for player sd ?
  9993. *------------------------------------------*/
  9994. bool pc_addeventtimer(map_session_data *sd,int tick,const char *name)
  9995. {
  9996. int i;
  9997. nullpo_retr(false,sd);
  9998. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  9999. if( i == MAX_EVENTTIMER )
  10000. return false;
  10001. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  10002. sd->eventcount++;
  10003. return true;
  10004. }
  10005. /*==========================================
  10006. * Del eventtimer for player sd ?
  10007. *------------------------------------------*/
  10008. bool pc_deleventtimer(map_session_data *sd,const char *name)
  10009. {
  10010. char* p = NULL;
  10011. int i;
  10012. nullpo_retr(false,sd);
  10013. if (sd->eventcount == 0)
  10014. return false;
  10015. // find the named event timer
  10016. ARR_FIND( 0, MAX_EVENTTIMER, i,
  10017. sd->eventtimer[i] != INVALID_TIMER &&
  10018. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  10019. strcmp(p, name) == 0
  10020. );
  10021. if( i == MAX_EVENTTIMER )
  10022. return false; // not found
  10023. delete_timer(sd->eventtimer[i],pc_eventtimer);
  10024. sd->eventtimer[i] = INVALID_TIMER;
  10025. if(sd->eventcount > 0)
  10026. sd->eventcount--;
  10027. aFree(p);
  10028. return true;
  10029. }
  10030. /*==========================================
  10031. * Update eventtimer count for player sd
  10032. *------------------------------------------*/
  10033. void pc_addeventtimercount(map_session_data *sd,const char *name,int tick)
  10034. {
  10035. int i;
  10036. nullpo_retv(sd);
  10037. for(i=0;i<MAX_EVENTTIMER;i++)
  10038. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  10039. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  10040. addtick_timer(sd->eventtimer[i],tick);
  10041. break;
  10042. }
  10043. }
  10044. /*==========================================
  10045. * Remove all eventtimer for player sd
  10046. *------------------------------------------*/
  10047. void pc_cleareventtimer(map_session_data *sd)
  10048. {
  10049. int i;
  10050. nullpo_retv(sd);
  10051. if (sd->eventcount == 0)
  10052. return;
  10053. for(i=0;i<MAX_EVENTTIMER;i++){
  10054. if( sd->eventtimer[i] != INVALID_TIMER ){
  10055. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  10056. delete_timer(sd->eventtimer[i],pc_eventtimer);
  10057. sd->eventtimer[i] = INVALID_TIMER;
  10058. if(sd->eventcount > 0) //avoid looping to max val
  10059. sd->eventcount--;
  10060. if (p) aFree(p);
  10061. }
  10062. }
  10063. }
  10064. /**
  10065. * Called when an item with combo is worn
  10066. * @param sd: Player data
  10067. * @param data: Item data
  10068. * @return Number of succeeded combo(s)
  10069. */
  10070. static int pc_checkcombo(map_session_data *sd, item_data *data) {
  10071. int success = 0;
  10072. for (const auto &item_combo : data->combos) {
  10073. bool do_continue = false;
  10074. // Ensure this isn't a duplicate combo
  10075. for (const auto player_combo : sd->combos) {
  10076. if (player_combo->id == item_combo->id) {
  10077. do_continue = true;
  10078. break;
  10079. }
  10080. }
  10081. // Combo already equipped
  10082. if (do_continue)
  10083. continue;
  10084. size_t nb_itemCombo = item_combo->nameid.size();
  10085. if (nb_itemCombo < 2) // A combo with less then 2 item?
  10086. continue;
  10087. struct s_itemchk {
  10088. int idx;
  10089. t_itemid nameid, card[MAX_SLOTS];
  10090. s_itemchk() : idx(0), nameid(0), card() {};
  10091. };
  10092. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  10093. size_t j;
  10094. unsigned int pos = 0;
  10095. for (j = 0; j < nb_itemCombo; j++) {
  10096. t_itemid id = item_combo->nameid[j];
  10097. bool found = false;
  10098. for (int16 k = 0; k < EQI_MAX; k++) {
  10099. short index = sd->equip_index[k];
  10100. if (index < 0)
  10101. continue;
  10102. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  10103. continue;
  10104. if (!sd->inventory_data[index])
  10105. continue;
  10106. if (itemdb_type(id) != IT_CARD) {
  10107. if (sd->inventory_data[index]->nameid != id)
  10108. continue;
  10109. if (j > 0) { // Check if this item not already used
  10110. do_continue = false;
  10111. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  10112. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  10113. do_continue = true;
  10114. break;
  10115. }
  10116. }
  10117. if (do_continue)
  10118. continue;
  10119. }
  10120. combo_idx[j].nameid = id;
  10121. combo_idx[j].idx = index;
  10122. pos |= sd->inventory.u.items_inventory[index].equip;
  10123. found = true;
  10124. break;
  10125. } else { // Cards and enchants
  10126. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  10127. continue;
  10128. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  10129. do_continue = false;
  10130. if (sd->inventory.u.items_inventory[index].card[z] != id)
  10131. continue;
  10132. if (j > 0) {
  10133. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  10134. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  10135. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  10136. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  10137. do_continue = true;
  10138. break;
  10139. }
  10140. }
  10141. }
  10142. }
  10143. }
  10144. if (do_continue)
  10145. continue;
  10146. combo_idx[j].nameid = id;
  10147. combo_idx[j].idx = index;
  10148. combo_idx[j].card[z] = id;
  10149. pos |= sd->inventory.u.items_inventory[index].equip;
  10150. found = true;
  10151. break;
  10152. }
  10153. }
  10154. }
  10155. if (!found)
  10156. break; // Unable to found all the IDs for this combo, return
  10157. }
  10158. // Broke out of the count loop without finding all IDs, move to the next combo
  10159. if (j < nb_itemCombo)
  10160. continue;
  10161. // All items in the combo are matching
  10162. auto entry = std::make_shared<s_combos>();
  10163. entry->bonus = item_combo->script;
  10164. entry->id = item_combo->id;
  10165. entry->pos = pos;
  10166. sd->combos.push_back(entry);
  10167. combo_idx.clear();
  10168. success++;
  10169. }
  10170. return success;
  10171. }
  10172. /**
  10173. * Called when an item with combo is removed
  10174. * @param sd: Player data
  10175. * @param data: Item data
  10176. * @return Number of removed combo(s)
  10177. */
  10178. static int pc_removecombo(map_session_data *sd, item_data *data ) {
  10179. if (sd->combos.empty())
  10180. return 0; // Nothing to do here, player has no combos
  10181. int retval = 0;
  10182. for (const auto &item_combo : data->combos) {
  10183. std::shared_ptr<s_combos> del_combo = nullptr;
  10184. // Check if this combo exists on this player
  10185. for (const auto &player_combo : sd->combos) {
  10186. if (player_combo->id == item_combo->id) {
  10187. del_combo = player_combo;
  10188. break;
  10189. }
  10190. }
  10191. // No match, skip this combo
  10192. if (del_combo == nullptr)
  10193. continue;
  10194. util::vector_erase_if_exists(sd->combos, del_combo);
  10195. retval++;
  10196. // Check if combo requirements still fit
  10197. if (pc_checkcombo(sd, data))
  10198. continue;
  10199. // It's empty, clear all the memory
  10200. if (sd->combos.empty()) {
  10201. sd->combos.clear();
  10202. return retval; // Return at this point as there are no more combos to check
  10203. }
  10204. }
  10205. return retval;
  10206. }
  10207. /**
  10208. * Load combo data(s) of player
  10209. * @param sd: Player data
  10210. * @return ret numbers of succeed combo
  10211. */
  10212. int pc_load_combo(map_session_data *sd) {
  10213. int ret = 0;
  10214. for (int16 i = 0; i < EQI_MAX; i++) {
  10215. item_data *id;
  10216. short idx = sd->equip_index[i];
  10217. if (idx < 0 || !(id = sd->inventory_data[idx]))
  10218. continue;
  10219. if (!id->combos.empty())
  10220. ret += pc_checkcombo(sd, id);
  10221. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  10222. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  10223. if (!sd->inventory.u.items_inventory[idx].card[j])
  10224. continue;
  10225. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[idx].card[j]);
  10226. if (data != nullptr) {
  10227. if (!data->combos.empty())
  10228. ret += pc_checkcombo(sd, data.get());
  10229. }
  10230. }
  10231. }
  10232. }
  10233. return ret;
  10234. }
  10235. /*==========================================
  10236. * Equip item on player sd at req_pos from inventory index n
  10237. * return: false - fail; true - success
  10238. *------------------------------------------*/
  10239. bool pc_equipitem(map_session_data *sd,short n,int req_pos,bool equipswitch)
  10240. {
  10241. int i, pos, flag = 0, iflag;
  10242. struct item_data *id;
  10243. uint8 res = ITEM_EQUIP_ACK_OK;
  10244. short* equip_index;
  10245. nullpo_retr(false,sd);
  10246. if( n < 0 || n >= MAX_INVENTORY ) {
  10247. if( equipswitch ){
  10248. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10249. }else{
  10250. // Does not deserve an answer... [Lemongrass]
  10251. //clif_equipitemack( sd, ITEM_EQUIP_ACK_FAIL, n );
  10252. }
  10253. return false;
  10254. }
  10255. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  10256. if( equipswitch ){
  10257. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10258. }else{
  10259. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n );
  10260. }
  10261. return false;
  10262. }
  10263. if (!(id = sd->inventory_data[n]))
  10264. return false;
  10265. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  10266. if(battle_config.battle_log && !equipswitch)
  10267. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  10268. if((res = pc_isequip(sd,n))) {
  10269. if( equipswitch ){
  10270. clif_equipswitch_add( sd, n, req_pos, res );
  10271. }else{
  10272. clif_equipitemack( *sd, res, n ); // fail
  10273. }
  10274. return false;
  10275. }
  10276. if( equipswitch && id->type == IT_AMMO ){
  10277. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10278. return false;
  10279. }
  10280. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  10281. if( equipswitch ){
  10282. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10283. }else{
  10284. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); // fail
  10285. }
  10286. return false;
  10287. }
  10288. if( sd->sc.count && (sd->sc.cant.equip || (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  10289. if( equipswitch ){
  10290. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10291. }else{
  10292. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); //Fail
  10293. }
  10294. return false;
  10295. }
  10296. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  10297. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  10298. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  10299. clif_notify_bindOnEquip( *sd, n );
  10300. }
  10301. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  10302. pos = req_pos&EQP_ACC;
  10303. if (pos == EQP_ACC) //User specified both slots.
  10304. pos = (equip_index[EQI_ACC_R] >= 0 && equip_index[EQI_ACC_L] < 0) ? EQP_ACC_L : EQP_ACC_R;
  10305. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  10306. if (!sd->inventory.u.items_inventory[n].card[i])
  10307. continue;
  10308. std::shared_ptr<item_data> card_data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10309. if (card_data) {
  10310. int card_pos = card_data->equip;
  10311. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  10312. pos = card_pos; // Use the card's equip position
  10313. break;
  10314. }
  10315. }
  10316. }
  10317. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  10318. pos = (req_pos&EQP_ARMS);
  10319. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  10320. #ifdef RENEWAL
  10321. pos = (equip_index[EQI_HAND_R] >= 0 && equip_index[EQI_HAND_L] < 0) ? EQP_HAND_L : EQP_HAND_R;
  10322. #else
  10323. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  10324. #endif
  10325. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  10326. pos = req_pos&EQP_SHADOW_ACC;
  10327. if (pos == EQP_SHADOW_ACC)
  10328. pos = (equip_index[EQI_SHADOW_ACC_L] >= 0 && equip_index[EQI_SHADOW_ACC_R] < 0) ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  10329. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  10330. pos = (req_pos&EQP_SHADOW_ARMS);
  10331. if( pos == EQP_SHADOW_ARMS )
  10332. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  10333. }
  10334. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  10335. //Update skill-block range database when weapon range changes. [Skotlex]
  10336. i = equip_index[EQI_HAND_R];
  10337. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  10338. flag = 1;
  10339. else
  10340. flag = id->range != sd->inventory_data[i]->range;
  10341. }
  10342. if( equipswitch ){
  10343. for( i = 0; i < EQI_MAX; i++ ){
  10344. if( pos&equip_bitmask[i] ){
  10345. // If there was already an item assigned to this slot
  10346. if( sd->equip_switch_index[i] >= 0 ){
  10347. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  10348. }
  10349. // Assign the new index to it
  10350. sd->equip_switch_index[i] = n;
  10351. }
  10352. }
  10353. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  10354. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  10355. return true;
  10356. }else{
  10357. for(i=0;i<EQI_MAX;i++) {
  10358. if(pos & equip_bitmask[i]) {
  10359. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  10360. pc_unequipitem(sd,sd->equip_index[i], 1 | 2 | 4);
  10361. sd->equip_index[i] = n;
  10362. }
  10363. }
  10364. pc_equipswitch_remove(sd, n);
  10365. if(pos==EQP_AMMO) {
  10366. clif_arrowequip(sd,n);
  10367. clif_arrow_fail(sd,3);
  10368. }
  10369. else
  10370. clif_equipitemack( *sd, ITEM_EQUIP_ACK_OK, n, pos );
  10371. sd->inventory.u.items_inventory[n].equip = pos;
  10372. }
  10373. if(pos & EQP_HAND_R) {
  10374. if(id)
  10375. sd->weapontype1 = id->subtype;
  10376. else
  10377. sd->weapontype1 = W_FIST;
  10378. pc_calcweapontype(sd);
  10379. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10380. }
  10381. if(pos & EQP_HAND_L) {
  10382. if(id) {
  10383. if(id->type == IT_WEAPON) {
  10384. sd->status.shield = W_FIST;
  10385. sd->weapontype2 = id->subtype;
  10386. }
  10387. else
  10388. if(id->type == IT_ARMOR) {
  10389. sd->status.shield = id->look;
  10390. sd->weapontype2 = W_FIST;
  10391. }
  10392. }
  10393. else
  10394. sd->status.shield = sd->weapontype2 = W_FIST;
  10395. pc_calcweapontype(sd);
  10396. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10397. }
  10398. if(pos & EQP_SHOES)
  10399. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10400. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  10401. short idx = sd->equip_index[EQI_AMMO];
  10402. if (idx >= 0) {
  10403. switch (sd->inventory_data[idx]->subtype) {
  10404. case AMMO_ARROW:
  10405. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  10406. pc_unequipitem(sd, idx, 2 | 4);
  10407. break;
  10408. case AMMO_BULLET:
  10409. case AMMO_SHELL:
  10410. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  10411. #ifdef RENEWAL
  10412. && id->subtype != W_GRENADE
  10413. #endif
  10414. )
  10415. pc_unequipitem(sd, idx, 2 | 4);
  10416. break;
  10417. #ifndef RENEWAL
  10418. case AMMO_GRENADE:
  10419. if (id->subtype != W_GRENADE)
  10420. pc_unequipitem(sd, idx, 2 | 4);
  10421. break;
  10422. #endif
  10423. }
  10424. }
  10425. }
  10426. pc_set_costume_view(sd);
  10427. pc_checkallowskill(sd); //Check if status changes should be halted.
  10428. iflag = sd->npc_item_flag;
  10429. // Check for combos (MUST be before status_calc_pc)
  10430. if (!id->combos.empty())
  10431. pc_checkcombo(sd, id);
  10432. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10433. ; // No cards
  10434. else {
  10435. for (i = 0; i < MAX_SLOTS; i++) {
  10436. if (!sd->inventory.u.items_inventory[n].card[i])
  10437. continue;
  10438. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10439. if (data != nullptr) {
  10440. if (!data->combos.empty())
  10441. pc_checkcombo(sd, data.get());
  10442. }
  10443. }
  10444. }
  10445. status_calc_pc(sd,SCO_NONE);
  10446. if (flag) //Update skill data
  10447. clif_skillinfoblock(sd);
  10448. //OnEquip script [Skotlex]
  10449. if (id) {
  10450. current_equip_item_index = n;
  10451. current_equip_card_id = 0;
  10452. //only run the script if item isn't restricted
  10453. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  10454. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10455. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10456. ; //No cards
  10457. else {
  10458. for( i = 0; i < MAX_SLOTS; i++ ) {
  10459. if (!sd->inventory.u.items_inventory[n].card[i])
  10460. continue;
  10461. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10462. if ( data != nullptr ) {
  10463. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data.get(), sd->bl.m))) {
  10464. current_equip_card_id = sd->inventory.u.items_inventory[n].card[i];
  10465. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10466. }
  10467. }
  10468. current_equip_card_id = 0;
  10469. }
  10470. }
  10471. current_equip_item_index = -1;
  10472. }
  10473. sd->npc_item_flag = iflag;
  10474. return true;
  10475. }
  10476. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int position ){
  10477. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  10478. while( it != bonus.end() ){
  10479. std::shared_ptr<s_autobonus> b = *it;
  10480. if( ( b->pos & position ) != b->pos ){
  10481. it++;
  10482. continue;
  10483. }
  10484. it = bonus.erase( it );
  10485. }
  10486. }
  10487. /**
  10488. * Recalculate player status on unequip
  10489. * @param sd: Player data
  10490. * @param n: Item inventory index
  10491. * @param flag: Whether to recalculate a player's status or not
  10492. * @return True on success or false on failure
  10493. */
  10494. static void pc_unequipitem_sub(map_session_data *sd, int n, int flag) {
  10495. int i, iflag;
  10496. bool status_calc = false;
  10497. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  10498. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  10499. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  10500. sd->inventory.u.items_inventory[n].equip = 0;
  10501. if (!(flag & 4))
  10502. pc_checkallowskill(sd);
  10503. iflag = sd->npc_item_flag;
  10504. // Check for combos (MUST be before status_calc_pc)
  10505. if (sd->inventory_data[n]) {
  10506. if (!sd->inventory_data[n]->combos.empty()) {
  10507. if (pc_removecombo(sd, sd->inventory_data[n]))
  10508. status_calc = true;
  10509. }
  10510. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10511. ; // No cards
  10512. else {
  10513. for (i = 0; i < MAX_SLOTS; i++) {
  10514. if (!sd->inventory.u.items_inventory[n].card[i])
  10515. continue;
  10516. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10517. if (data != nullptr) {
  10518. if (!data->combos.empty()) {
  10519. if (pc_removecombo(sd, data.get()))
  10520. status_calc = true;
  10521. }
  10522. }
  10523. }
  10524. }
  10525. }
  10526. if (flag & 1 || status_calc) {
  10527. pc_checkallowskill(sd);
  10528. status_calc_pc(sd, SCO_FORCE);
  10529. }
  10530. if (sd->sc.getSCE(SC_SIGNUMCRUCIS) && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  10531. status_change_end(&sd->bl, SC_SIGNUMCRUCIS);
  10532. //OnUnEquip script [Skotlex]
  10533. if (sd->inventory_data[n]) {
  10534. current_equip_item_index = n;
  10535. current_equip_card_id = 0;
  10536. if (sd->inventory_data[n]->unequip_script)
  10537. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10538. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10539. ; //No cards
  10540. else {
  10541. for (i = 0; i < MAX_SLOTS; i++) {
  10542. if (!sd->inventory.u.items_inventory[n].card[i])
  10543. continue;
  10544. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10545. if (data != nullptr) {
  10546. if (data->unequip_script) {
  10547. current_equip_card_id = sd->inventory.u.items_inventory[n].card[i];
  10548. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10549. }
  10550. }
  10551. current_equip_card_id = 0;
  10552. }
  10553. }
  10554. current_equip_item_index = -1;
  10555. }
  10556. sd->npc_item_flag = iflag;
  10557. }
  10558. /**
  10559. * Called when attempting to unequip an item from a player
  10560. * @param sd: Player data
  10561. * @param n: Item inventory index
  10562. * @param flag: Type of unequip
  10563. * 0 - only unequip
  10564. * 1 - calculate status after unequipping
  10565. * 2 - force unequip
  10566. * 4 - unequip by switching equipment
  10567. * @return True on success or false on failure
  10568. */
  10569. bool pc_unequipitem(map_session_data *sd, int n, int flag) {
  10570. int i, pos;
  10571. nullpo_retr(false,sd);
  10572. if (n < 0 || n >= MAX_INVENTORY) {
  10573. clif_unequipitemack(sd,0,0,0);
  10574. return false;
  10575. }
  10576. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  10577. clif_unequipitemack(sd,n,0,0);
  10578. return false; //Nothing to unequip
  10579. }
  10580. // status change that makes player cannot unequip equipment
  10581. if (!(flag&2) && sd->sc.count &&( sd->sc.cant.unequip ||
  10582. (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  10583. {
  10584. clif_unequipitemack(sd,n,0,0);
  10585. return false;
  10586. }
  10587. if (battle_config.battle_log)
  10588. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  10589. for(i = 0; i < EQI_MAX; i++) {
  10590. if (pos & equip_bitmask[i])
  10591. sd->equip_index[i] = -1;
  10592. }
  10593. if(pos & EQP_HAND_R) {
  10594. sd->weapontype1 = W_FIST;
  10595. sd->status.weapon = sd->weapontype2;
  10596. pc_calcweapontype(sd);
  10597. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10598. if( !battle_config.dancing_weaponswitch_fix )
  10599. status_change_end(&sd->bl, SC_DANCING); // Unequipping => stop dancing.
  10600. #ifdef RENEWAL
  10601. if (battle_config.switch_remove_edp&2) {
  10602. #else
  10603. if (battle_config.switch_remove_edp&1) {
  10604. #endif
  10605. status_change_end(&sd->bl, SC_EDP);
  10606. }
  10607. }
  10608. if(pos & EQP_HAND_L) {
  10609. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  10610. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  10611. sd->status.shield = sd->weapontype2 = W_FIST;
  10612. pc_calcweapontype(sd);
  10613. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10614. }
  10615. if(pos & EQP_SHOES)
  10616. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10617. clif_unequipitemack(sd,n,pos,1);
  10618. pc_set_costume_view(sd);
  10619. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIP });
  10620. // On weapon change (right and left hand)
  10621. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  10622. if (battle_config.ammo_unequip && !(flag & 4)) {
  10623. switch (sd->inventory_data[n]->subtype) {
  10624. case W_BOW:
  10625. case W_MUSICAL:
  10626. case W_WHIP:
  10627. case W_REVOLVER:
  10628. case W_RIFLE:
  10629. case W_GATLING:
  10630. case W_SHOTGUN:
  10631. case W_GRENADE: {
  10632. short idx = sd->equip_index[EQI_AMMO];
  10633. if (idx >= 0) {
  10634. sd->equip_index[EQI_AMMO] = -1;
  10635. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  10636. pc_unequipitem_sub(sd, idx, 0);
  10637. }
  10638. }
  10639. break;
  10640. }
  10641. }
  10642. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPWEAPON });
  10643. }
  10644. // On armor change
  10645. if (pos & EQP_ARMOR) {
  10646. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPARMOR });
  10647. }
  10648. // On equipment change
  10649. #ifndef RENEWAL
  10650. if (!(flag&4))
  10651. status_change_end(&sd->bl, SC_CONCENTRATION);
  10652. #endif
  10653. // On ammo change
  10654. 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_))
  10655. status_change_end(&sd->bl, SC_P_ALTER);
  10656. pc_unequipitem_sub(sd, n, flag);
  10657. return true;
  10658. }
  10659. int pc_equipswitch( map_session_data* sd, int index ){
  10660. // Get the target equip mask
  10661. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  10662. // Get the currently equipped item
  10663. short equippedItem = pc_checkequip( sd, position, true );
  10664. // No item equipped at the target
  10665. if( equippedItem == -1 ){
  10666. // Remove it from the equip switch
  10667. pc_equipswitch_remove( sd, index );
  10668. pc_equipitem( sd, index, position );
  10669. return position;
  10670. }else{
  10671. std::map<int, int> unequipped;
  10672. int unequipped_position = 0;
  10673. // Unequip all items that interfere
  10674. for( int i = 0; i < EQI_MAX; i++ ){
  10675. int unequip_index = sd->equip_index[i];
  10676. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  10677. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  10678. // Store the unequipped index and position mask for later
  10679. unequipped[unequip_index] = unequip_item->equip;
  10680. // Keep the position for later
  10681. unequipped_position |= unequip_item->equip;
  10682. // Unequip the item
  10683. pc_unequipitem( sd, unequip_index, 0 );
  10684. }
  10685. }
  10686. int all_position = position | unequipped_position;
  10687. // Equip everything that is hit by the mask
  10688. for( int i = 0; i < EQI_MAX; i++ ){
  10689. int exchange_index = sd->equip_switch_index[i];
  10690. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  10691. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  10692. // Store the target position
  10693. int exchange_position = exchange_item->equipSwitch;
  10694. // Remove the item from equip switch
  10695. pc_equipswitch_remove( sd, exchange_index );
  10696. // Equip the item at the destinated position
  10697. pc_equipitem( sd, exchange_index, exchange_position );
  10698. }
  10699. }
  10700. // Place all unequipped items into the equip switch window
  10701. for( std::pair<int, int> pair : unequipped ){
  10702. int unequipped_index = pair.first;
  10703. int unequipped_position = pair.second;
  10704. // Rebuild the index cache
  10705. for( int i = 0; i < EQI_MAX; i++ ){
  10706. if( unequipped_position & equip_bitmask[i] ){
  10707. sd->equip_switch_index[i] = unequipped_index;
  10708. }
  10709. }
  10710. // Set the correct position mask
  10711. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  10712. // Notify the client
  10713. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  10714. }
  10715. return all_position;
  10716. }
  10717. }
  10718. void pc_equipswitch_remove( map_session_data* sd, int index ){
  10719. struct item* item = &sd->inventory.u.items_inventory[index];
  10720. if( !item->equipSwitch ){
  10721. return;
  10722. }
  10723. for( int i = 0; i < EQI_MAX; i++ ){
  10724. // If a match is found
  10725. if( sd->equip_switch_index[i] == index ){
  10726. // Remove it from the slot
  10727. sd->equip_switch_index[i] = -1;
  10728. }
  10729. }
  10730. // Send out one packet for all slots using the current item's mask
  10731. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  10732. item->equipSwitch = 0;
  10733. }
  10734. /*==========================================
  10735. * Checking if player (sd) has an invalid item
  10736. * and is unequiped on map load (item_noequip)
  10737. *------------------------------------------*/
  10738. void pc_checkitem(map_session_data *sd) {
  10739. int i, calc_flag = 0;
  10740. struct item* it;
  10741. nullpo_retv(sd);
  10742. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  10743. return;
  10744. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  10745. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10746. it = &sd->inventory.u.items_inventory[i];
  10747. if( it->nameid == 0 )
  10748. continue;
  10749. if( !it->equip )
  10750. continue;
  10751. if( it->equip&~pc_equippoint(sd,i) ) {
  10752. pc_unequipitem(sd, i, 2);
  10753. calc_flag = 1;
  10754. continue;
  10755. }
  10756. 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) ) {
  10757. pc_unequipitem(sd, i, 2);
  10758. calc_flag = 1;
  10759. continue;
  10760. }
  10761. }
  10762. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10763. it = &sd->inventory.u.items_inventory[i];
  10764. if( it->nameid == 0 )
  10765. continue;
  10766. if( !it->equipSwitch )
  10767. continue;
  10768. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  10769. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  10770. for( int j = 0; j < EQI_MAX; j++ ){
  10771. if( sd->equip_switch_index[j] == i ){
  10772. sd->equip_switch_index[j] = -1;
  10773. }
  10774. }
  10775. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  10776. continue;
  10777. }
  10778. }
  10779. if( calc_flag && sd->state.active ) {
  10780. pc_checkallowskill(sd);
  10781. status_calc_pc(sd,SCO_NONE);
  10782. }
  10783. }
  10784. /*==========================================
  10785. * Checks for unavailable items and removes them.
  10786. * @param sd: Player data
  10787. * @param type Forced check:
  10788. * 1 - Inventory
  10789. * 2 - Cart
  10790. * 4 - Storage
  10791. *------------------------------------------*/
  10792. void pc_check_available_item(map_session_data *sd, uint8 type)
  10793. {
  10794. int i;
  10795. t_itemid nameid;
  10796. char output[256];
  10797. nullpo_retv(sd);
  10798. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  10799. for(i = 0; i < MAX_INVENTORY; i++) {
  10800. nameid = sd->inventory.u.items_inventory[i].nameid;
  10801. if (!nameid)
  10802. continue;
  10803. if (!itemdb_available(nameid)) {
  10804. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  10805. clif_displaymessage(sd->fd, output);
  10806. 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);
  10807. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  10808. continue;
  10809. }
  10810. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  10811. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  10812. }
  10813. }
  10814. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  10815. for(i = 0; i < MAX_CART; i++) {
  10816. nameid = sd->cart.u.items_cart[i].nameid;
  10817. if (!nameid)
  10818. continue;
  10819. if (!itemdb_available(nameid)) {
  10820. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  10821. clif_displaymessage(sd->fd, output);
  10822. 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);
  10823. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  10824. continue;
  10825. }
  10826. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  10827. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  10828. }
  10829. }
  10830. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  10831. for(i = 0; i < sd->storage.max_amount; i++) {
  10832. nameid = sd->storage.u.items_storage[i].nameid;
  10833. if (!nameid)
  10834. continue;
  10835. if (!itemdb_available(nameid)) {
  10836. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  10837. clif_displaymessage(sd->fd, output);
  10838. 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);
  10839. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  10840. continue;
  10841. }
  10842. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  10843. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  10844. }
  10845. }
  10846. }
  10847. /*==========================================
  10848. * Update PVP rank for sd1 in cmp to sd2
  10849. *------------------------------------------*/
  10850. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  10851. {
  10852. map_session_data *sd1,*sd2;
  10853. sd1=(map_session_data *)bl;
  10854. sd2=va_arg(ap,map_session_data *);
  10855. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  10856. {// cannot register pvp rank for hidden GMs
  10857. return 0;
  10858. }
  10859. if( sd1->pvp_point > sd2->pvp_point )
  10860. sd2->pvp_rank++;
  10861. return 0;
  10862. }
  10863. /*==========================================
  10864. * Calculate new rank beetween all present players (map_foreachinallarea)
  10865. * and display result
  10866. *------------------------------------------*/
  10867. int pc_calc_pvprank(map_session_data *sd)
  10868. {
  10869. int old = sd->pvp_rank;
  10870. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10871. sd->pvp_rank=1;
  10872. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  10873. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  10874. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  10875. return sd->pvp_rank;
  10876. }
  10877. /*==========================================
  10878. * Calculate next sd ranking calculation from config
  10879. *------------------------------------------*/
  10880. TIMER_FUNC(pc_calc_pvprank_timer){
  10881. map_session_data *sd;
  10882. sd=map_id2sd(id);
  10883. if(sd==NULL)
  10884. return 0;
  10885. sd->pvp_timer = INVALID_TIMER;
  10886. if( pc_isinvisible(sd) )
  10887. {// do not calculate the pvp rank for a hidden GM
  10888. return 0;
  10889. }
  10890. if( pc_calc_pvprank(sd) > 0 )
  10891. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  10892. return 0;
  10893. }
  10894. /*==========================================
  10895. * Checking if sd is married
  10896. * Return:
  10897. * partner_id = yes
  10898. * 0 = no
  10899. *------------------------------------------*/
  10900. int pc_ismarried(map_session_data *sd)
  10901. {
  10902. if(sd == NULL)
  10903. return -1;
  10904. if(sd->status.partner_id > 0)
  10905. return sd->status.partner_id;
  10906. else
  10907. return 0;
  10908. }
  10909. /*==========================================
  10910. * Marry player sd to player dstsd
  10911. * Return:
  10912. * false = fail
  10913. * true = success
  10914. *------------------------------------------*/
  10915. bool pc_marriage(map_session_data *sd,map_session_data *dstsd)
  10916. {
  10917. if(sd == NULL || dstsd == NULL ||
  10918. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  10919. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  10920. return false;
  10921. sd->status.partner_id = dstsd->status.char_id;
  10922. dstsd->status.partner_id = sd->status.char_id;
  10923. achievement_update_objective(sd, AG_MARRY, 1, 1);
  10924. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  10925. return true;
  10926. }
  10927. /*==========================================
  10928. * Divorce sd from its partner
  10929. * Return:
  10930. * false = fail
  10931. * true = success
  10932. *------------------------------------------*/
  10933. bool pc_divorce(map_session_data *sd)
  10934. {
  10935. map_session_data *p_sd;
  10936. int i;
  10937. if( sd == NULL || !pc_ismarried(sd) )
  10938. return false;
  10939. if( !sd->status.partner_id )
  10940. return false; // Char is not married
  10941. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  10942. { // Lets char server do the divorce
  10943. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  10944. return false; // No char server connected
  10945. return true;
  10946. }
  10947. // Both players online, lets do the divorce manually
  10948. sd->status.partner_id = 0;
  10949. p_sd->status.partner_id = 0;
  10950. for( i = 0; i < MAX_INVENTORY; i++ )
  10951. {
  10952. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10953. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10954. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10955. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10956. }
  10957. clif_divorced(sd, p_sd->status.name);
  10958. clif_divorced(p_sd, sd->status.name);
  10959. return true;
  10960. }
  10961. /**
  10962. * Get the partner map_session_data of a player
  10963. * @param sd : the husband|wife session
  10964. * @return partner session or NULL
  10965. */
  10966. map_session_data *pc_get_partner(map_session_data *sd){
  10967. if (!sd || !pc_ismarried(sd))
  10968. return NULL;
  10969. return map_charid2sd(sd->status.partner_id);
  10970. }
  10971. /**
  10972. * Get the father map_session_data of a player
  10973. * @param sd : the baby session
  10974. * @return father session or NULL
  10975. */
  10976. map_session_data *pc_get_father (map_session_data *sd){
  10977. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  10978. return NULL;
  10979. return map_charid2sd(sd->status.father);
  10980. }
  10981. /**
  10982. * Get the mother map_session_data of a player
  10983. * @param sd : the baby session
  10984. * @return mother session or NULL
  10985. */
  10986. map_session_data *pc_get_mother (map_session_data *sd){
  10987. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  10988. return NULL;
  10989. return map_charid2sd(sd->status.mother);
  10990. }
  10991. /*==========================================
  10992. * Get sd children charid. (Need to be married)
  10993. *------------------------------------------*/
  10994. map_session_data *pc_get_child (map_session_data *sd)
  10995. {
  10996. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  10997. // charid2sd returns NULL if not found
  10998. return NULL;
  10999. return map_charid2sd(sd->status.child);
  11000. }
  11001. /*==========================================
  11002. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  11003. *------------------------------------------*/
  11004. void pc_bleeding (map_session_data *sd, t_tick diff_tick)
  11005. {
  11006. int hp = 0, sp = 0;
  11007. if( pc_isdead(sd) )
  11008. return;
  11009. if (sd->hp_loss.value) {
  11010. sd->hp_loss.tick += diff_tick;
  11011. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  11012. hp += sd->hp_loss.value;
  11013. sd->hp_loss.tick -= sd->hp_loss.rate;
  11014. }
  11015. if(hp >= sd->battle_status.hp)
  11016. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  11017. }
  11018. if (sd->sp_loss.value) {
  11019. sd->sp_loss.tick += diff_tick;
  11020. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  11021. sp += sd->sp_loss.value;
  11022. sd->sp_loss.tick -= sd->sp_loss.rate;
  11023. }
  11024. }
  11025. if (hp > 0 || sp > 0)
  11026. status_zap(&sd->bl, hp, sp);
  11027. }
  11028. //Character regen. Flag is used to know which types of regen can take place.
  11029. //&1: HP regen
  11030. //&2: SP regen
  11031. void pc_regen (map_session_data *sd, t_tick diff_tick)
  11032. {
  11033. int hp = 0, sp = 0;
  11034. if (sd->hp_regen.value) {
  11035. sd->hp_regen.tick += diff_tick;
  11036. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  11037. hp += sd->hp_regen.value;
  11038. sd->hp_regen.tick -= sd->hp_regen.rate;
  11039. }
  11040. }
  11041. if (sd->sp_regen.value) {
  11042. sd->sp_regen.tick += diff_tick;
  11043. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  11044. sp += sd->sp_regen.value;
  11045. sd->sp_regen.tick -= sd->sp_regen.rate;
  11046. }
  11047. }
  11048. if (sd->percent_hp_regen.value) {
  11049. sd->percent_hp_regen.tick += diff_tick;
  11050. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  11051. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  11052. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  11053. }
  11054. }
  11055. if (sd->percent_sp_regen.value) {
  11056. sd->percent_sp_regen.tick += diff_tick;
  11057. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  11058. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  11059. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  11060. }
  11061. }
  11062. if (hp > 0 || sp > 0)
  11063. status_heal(&sd->bl, hp, sp, 0);
  11064. }
  11065. /*==========================================
  11066. * Memo player sd savepoint. (map,x,y)
  11067. *------------------------------------------*/
  11068. void pc_setsavepoint(map_session_data *sd, short mapindex,int x,int y)
  11069. {
  11070. nullpo_retv(sd);
  11071. safestrncpy( sd->status.save_point.map, mapindex_id2name( mapindex ), sizeof( sd->status.save_point.map ) );
  11072. sd->status.save_point.x = x;
  11073. sd->status.save_point.y = y;
  11074. }
  11075. /*==========================================
  11076. * Save 1 player data at autosave interval
  11077. *------------------------------------------*/
  11078. static TIMER_FUNC(pc_autosave){
  11079. int interval;
  11080. struct s_mapiterator* iter;
  11081. map_session_data* sd;
  11082. static int last_save_id = 0, save_flag = 0;
  11083. if(save_flag == 2) //Someone was saved on last call, normal cycle
  11084. save_flag = 0;
  11085. else
  11086. save_flag = 1; //Noone was saved, so save first found char.
  11087. iter = mapit_getallusers();
  11088. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  11089. {
  11090. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  11091. continue;
  11092. if(sd->bl.id == last_save_id && save_flag != 1) {
  11093. save_flag = 1;
  11094. continue;
  11095. }
  11096. if(save_flag != 1) //Not our turn to save yet.
  11097. continue;
  11098. //Save char.
  11099. last_save_id = sd->bl.id;
  11100. save_flag = 2;
  11101. if (pc_isvip(sd)) // Check if we're still VIP
  11102. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  11103. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  11104. break;
  11105. }
  11106. mapit_free(iter);
  11107. interval = autosave_interval/(map_usercount()+1);
  11108. if(interval < minsave_interval)
  11109. interval = minsave_interval;
  11110. add_timer(gettick()+interval,pc_autosave,0,0);
  11111. return 0;
  11112. }
  11113. static int pc_daynight_timer_sub(map_session_data *sd,va_list ap)
  11114. {
  11115. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  11116. { //Night/day state does not match.
  11117. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  11118. sd->state.night = night_flag;
  11119. return 1;
  11120. }
  11121. return 0;
  11122. }
  11123. /*================================================
  11124. * timer to do the day [Yor]
  11125. * data: 0 = called by timer, 1 = gmcommand/script
  11126. *------------------------------------------------*/
  11127. TIMER_FUNC(map_day_timer){
  11128. char tmp_soutput[1024];
  11129. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  11130. return 0;
  11131. if (!night_flag)
  11132. return 0; //Already day.
  11133. night_flag = 0; // 0=day, 1=night [Yor]
  11134. map_foreachpc(pc_daynight_timer_sub);
  11135. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  11136. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11137. return 0;
  11138. }
  11139. /*================================================
  11140. * timer to do the night [Yor]
  11141. * data: 0 = called by timer, 1 = gmcommand/script
  11142. *------------------------------------------------*/
  11143. TIMER_FUNC(map_night_timer){
  11144. char tmp_soutput[1024];
  11145. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  11146. return 0;
  11147. if (night_flag)
  11148. return 0; //Already nigth.
  11149. night_flag = 1; // 0=day, 1=night [Yor]
  11150. map_foreachpc(pc_daynight_timer_sub);
  11151. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  11152. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11153. return 0;
  11154. }
  11155. /**
  11156. * Attempt to stand up a player
  11157. * @param sd
  11158. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  11159. * @return True if success, Fals if failed
  11160. */
  11161. bool pc_setstand(map_session_data *sd, bool force){
  11162. nullpo_ret(sd);
  11163. // Cannot stand yet
  11164. // TODO: Move to SCS_NOSTAND [Cydh]
  11165. if (!force && (sd->sc.getSCE(SC_SITDOWN_FORCE) || sd->sc.getSCE(SC_BANANA_BOMB_SITDOWN)))
  11166. return false;
  11167. status_change_end(&sd->bl, SC_TENSIONRELAX);
  11168. clif_status_load(&sd->bl,EFST_SIT,0);
  11169. clif_standing(&sd->bl); //Inform area PC is standing
  11170. //Reset sitting tick.
  11171. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  11172. if( pc_isdead( sd ) ){
  11173. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11174. clif_party_dead( *sd );
  11175. }else{
  11176. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11177. }
  11178. return true;
  11179. }
  11180. /**
  11181. * Calculate Overheat value
  11182. * @param sd: Player data
  11183. * @param heat: Amount of Heat to adjust
  11184. **/
  11185. void pc_overheat(map_session_data &sd, int16 heat) {
  11186. status_change_entry *sce = sd.sc.getSCE(SC_OVERHEAT_LIMITPOINT);
  11187. if (sce) {
  11188. sce->val1 += heat;
  11189. sce->val1 = cap_value(sce->val1, 0, 1000);
  11190. if (heat < 0 && sce->val1 == 0) { // Cooling device used.
  11191. status_change_end(&sd.bl, SC_OVERHEAT_LIMITPOINT);
  11192. status_change_end(&sd.bl, SC_OVERHEAT);
  11193. }
  11194. } else if (heat > 0)
  11195. sc_start(&sd.bl, &sd.bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  11196. }
  11197. /**
  11198. * Check if player is autolooting given itemID.
  11199. */
  11200. bool pc_isautolooting(map_session_data *sd, t_itemid nameid)
  11201. {
  11202. uint8 i = 0;
  11203. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  11204. return true;
  11205. if (!sd->state.autolooting)
  11206. return false;
  11207. if (sd->state.autolooting)
  11208. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  11209. return (i != AUTOLOOTITEM_SIZE);
  11210. }
  11211. /**
  11212. * Checks if player can use @/#command
  11213. * @param sd Player map session data
  11214. * @param command Command name without @/# and params
  11215. * @param type is it atcommand or charcommand
  11216. */
  11217. bool pc_can_use_command( map_session_data *sd, const char *command, AtCommandType type ){
  11218. return sd->group->can_use_command( command, type );
  11219. }
  11220. bool pc_has_permission( map_session_data* sd, e_pc_permission permission ){
  11221. return sd->permissions.test( permission );
  11222. }
  11223. /**
  11224. * Checks if commands used by a player should be logged
  11225. * according to their group setting.
  11226. * @param sd Player map session data
  11227. */
  11228. bool pc_should_log_commands( map_session_data *sd ){
  11229. return sd->group->log_commands;
  11230. }
  11231. /**
  11232. * Spirit Charm expiration timer.
  11233. * @see TimerFunc
  11234. */
  11235. static TIMER_FUNC(pc_spiritcharm_timer){
  11236. map_session_data *sd;
  11237. int i;
  11238. if ((sd = (map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  11239. return 1;
  11240. if (sd->spiritcharm <= 0) {
  11241. 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);
  11242. sd->spiritcharm = 0;
  11243. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11244. return 0;
  11245. }
  11246. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  11247. if (i == sd->spiritcharm) {
  11248. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  11249. return 0;
  11250. }
  11251. sd->spiritcharm--;
  11252. if (i != sd->spiritcharm)
  11253. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  11254. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11255. if (sd->spiritcharm <= 0)
  11256. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11257. clif_spiritcharm(sd);
  11258. return 0;
  11259. }
  11260. /**
  11261. * Adds a spirit charm.
  11262. * @param sd: Target character
  11263. * @param interval: Duration
  11264. * @param max: Maximum amount of charms to add
  11265. * @param type: Charm type (@see spirit_charm_types)
  11266. */
  11267. void pc_addspiritcharm(map_session_data *sd, int interval, int max, int type)
  11268. {
  11269. int tid, i;
  11270. nullpo_retv(sd);
  11271. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  11272. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  11273. if (max > MAX_SPIRITCHARM)
  11274. max = MAX_SPIRITCHARM;
  11275. if (sd->spiritcharm < 0)
  11276. sd->spiritcharm = 0;
  11277. if (sd->spiritcharm && sd->spiritcharm >= max) {
  11278. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  11279. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  11280. sd->spiritcharm--;
  11281. if (sd->spiritcharm != 0)
  11282. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  11283. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11284. }
  11285. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  11286. 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);
  11287. if (i != sd->spiritcharm)
  11288. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  11289. sd->spiritcharm_timer[i] = tid;
  11290. sd->spiritcharm++;
  11291. sd->spiritcharm_type = type;
  11292. clif_spiritcharm(sd);
  11293. }
  11294. /**
  11295. * Removes one or more spirit charms.
  11296. * @param sd: The target character
  11297. * @param count: Amount of charms to remove
  11298. * @param type: Type of charm to remove
  11299. */
  11300. void pc_delspiritcharm(map_session_data *sd, int count, int type)
  11301. {
  11302. int i;
  11303. nullpo_retv(sd);
  11304. if (sd->spiritcharm_type != type)
  11305. return;
  11306. if (sd->spiritcharm <= 0) {
  11307. sd->spiritcharm = 0;
  11308. return;
  11309. }
  11310. if (count <= 0)
  11311. return;
  11312. if (count > sd->spiritcharm)
  11313. count = sd->spiritcharm;
  11314. sd->spiritcharm -= count;
  11315. if (count > MAX_SPIRITCHARM)
  11316. count = MAX_SPIRITCHARM;
  11317. for (i = 0; i < count; i++) {
  11318. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  11319. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  11320. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11321. }
  11322. }
  11323. for (i = count; i < MAX_SPIRITCHARM; i++) {
  11324. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  11325. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11326. }
  11327. if (sd->spiritcharm <= 0)
  11328. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11329. clif_spiritcharm(sd);
  11330. }
  11331. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  11332. /**
  11333. * Renewal EXP/Item Drop rate modifier based on level penalty
  11334. * @param level_diff: Monster and Player level difference
  11335. * @param mob_class: Monster class
  11336. * @param mode: Monster mode
  11337. * @param type: 1 - EXP, 2 - Item Drop
  11338. * @return Penalty rate
  11339. */
  11340. uint16 pc_level_penalty_mod( map_session_data* sd, e_penalty_type type, std::shared_ptr<s_mob_db> mob, mob_data* md ){
  11341. // No player was attached, we don't use any modifier (100 = rates are not touched)
  11342. if( sd == nullptr ){
  11343. return 100;
  11344. }
  11345. if ((type == PENALTY_DROP && map_getmapflag(sd->bl.m, MF_NORENEWALDROPPENALTY)) || (type == PENALTY_EXP && map_getmapflag(sd->bl.m, MF_NORENEWALEXPPENALTY))) {
  11346. return 100;
  11347. }
  11348. int monster_level;
  11349. if( md != nullptr ){
  11350. monster_level = md->level;
  11351. mob = md->db;
  11352. }else if( mob != nullptr ){
  11353. monster_level = mob->lv;
  11354. }else{
  11355. return 100;
  11356. }
  11357. if( ( type == PENALTY_DROP || type == PENALTY_MVP_DROP ) && status_has_mode( &mob->status, MD_FIXEDITEMDROP ) ){
  11358. return 100;
  11359. }
  11360. int level_difference = monster_level - sd->status.base_level;
  11361. std::shared_ptr<s_penalty> penalty = penalty_db.find( type );
  11362. if( penalty != nullptr ){
  11363. return penalty->rate[ level_difference + MAX_LEVEL - 1 ];
  11364. }else{
  11365. return 100;
  11366. }
  11367. }
  11368. #endif
  11369. int pc_split_str(char *str,char **val,int num)
  11370. {
  11371. int i;
  11372. for (i=0; i<num && str; i++){
  11373. val[i] = str;
  11374. str = strchr(str,',');
  11375. if (str && i<num-1) //Do not remove a trailing comma.
  11376. *str++=0;
  11377. }
  11378. return i;
  11379. }
  11380. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  11381. {
  11382. static int warning=0;
  11383. int i,j;
  11384. for (i=0; i<max; i++) {
  11385. double f;
  11386. if (!str) break;
  11387. f = atof(str);
  11388. if (f < 0)
  11389. val[i] = 0;
  11390. else if (f > UINT_MAX) {
  11391. val[i] = UINT_MAX;
  11392. if (!warning) {
  11393. warning = 1;
  11394. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  11395. }
  11396. } else
  11397. val[i] = (unsigned int)f;
  11398. str = strchr(str,sep);
  11399. if (str)
  11400. *str++=0;
  11401. }
  11402. //Zero up the remaining.
  11403. for(j=i; j < max; j++)
  11404. val[j] = 0;
  11405. return i;
  11406. }
  11407. std::shared_ptr<s_skill_tree_entry> SkillTreeDatabase::get_skill_data(int class_, uint16 skill_id) {
  11408. std::shared_ptr<s_skill_tree> tree = this->find(class_);
  11409. if (tree != nullptr)
  11410. return util::umap_find(tree->skills, skill_id);
  11411. return nullptr;
  11412. }
  11413. const std::string SkillTreeDatabase::getDefaultLocation() {
  11414. return std::string(db_path) + "/skill_tree.yml";
  11415. }
  11416. /**
  11417. * Reads and parses an entry from the skill_tree.
  11418. * @param node: YAML node containing the entry.
  11419. * @return count of successfully parsed rows
  11420. */
  11421. uint64 SkillTreeDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11422. std::string job_name;
  11423. if (!this->asString(node, "Job", job_name))
  11424. return 0;
  11425. int64 constant;
  11426. std::string job_name_constant = "JOB_" + job_name;
  11427. if (!script_get_constant(job_name_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11428. this->invalidWarning(node["Job"], "Invalid job %s.\n", job_name.c_str());
  11429. return 0;
  11430. }
  11431. uint16 job_id = static_cast<uint16>(constant);
  11432. std::shared_ptr<s_skill_tree> tree = this->find(job_id);
  11433. bool exists = tree != nullptr;
  11434. if (!exists)
  11435. tree = std::make_shared<s_skill_tree>();
  11436. if (this->nodeExists(node, "Inherit")) {
  11437. const ryml::NodeRef& InheritNode = node["Inherit"];
  11438. for (const auto &Inheritit : InheritNode) {
  11439. std::string inheritname;
  11440. c4::from_chars(Inheritit.key(), &inheritname);
  11441. std::string inheritname_constant = "JOB_" + inheritname;
  11442. if (!script_get_constant(inheritname_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11443. this->invalidWarning(InheritNode[Inheritit.key()], "Invalid job %s.\n", inheritname.c_str());
  11444. return 0;
  11445. }
  11446. bool active;
  11447. if (!this->asBool(InheritNode, inheritname, active))
  11448. return 0;
  11449. uint16 inherit_job = static_cast<uint16>(constant);
  11450. if (!active) {
  11451. if (exists)
  11452. util::vector_erase_if_exists(tree->inherit_job, inherit_job);
  11453. }
  11454. else {
  11455. if (!util::vector_exists(tree->inherit_job, inherit_job))
  11456. tree->inherit_job.push_back(inherit_job);
  11457. }
  11458. }
  11459. }
  11460. if (this->nodeExists(node, "Tree")) {
  11461. for (const auto &it : node["Tree"]) {
  11462. std::string skill_name;
  11463. if (!this->asString(it, "Name", skill_name))
  11464. return 0;
  11465. uint16 skill_id = skill_name2id(skill_name.c_str());
  11466. if (skill_id == 0) {
  11467. this->invalidWarning(it["Name"], "Invalid skill name \"%s\".\n", skill_name.c_str());
  11468. return 0;
  11469. }
  11470. if (!skill_get_index(skill_id)) {
  11471. this->invalidWarning(it["Name"], "Unable to load skill %s into job %hu's tree.\n", skill_name.c_str(), job_id);
  11472. return 0;
  11473. }
  11474. std::shared_ptr<s_skill_tree_entry> entry;
  11475. bool skill_exists = tree->skills.count(skill_id) > 0;
  11476. if (skill_exists)
  11477. entry = tree->skills[skill_id];
  11478. else
  11479. entry = std::make_shared<s_skill_tree_entry>();
  11480. entry->skill_id = skill_id;
  11481. uint16 max_lv;
  11482. if (!this->asUInt16(it, "MaxLevel", max_lv))
  11483. return 0;
  11484. if (max_lv > MAX_SKILL_LEVEL) {
  11485. this->invalidWarning(it["MaxLevel"], "MaxLevel exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11486. return 0;
  11487. }
  11488. uint16 skill_lv_max = skill_get_max(skill_id);
  11489. if (max_lv > skill_lv_max) {
  11490. 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);
  11491. max_lv = skill_lv_max;
  11492. }
  11493. // if (max_lv == 0) { // skill lvl 0 removed on loadingFinished (because of inherit)
  11494. // if (!skill_exists || entry->skill_id.erase(skill_id) == 0)
  11495. // 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());
  11496. // continue;
  11497. // }
  11498. entry->max_lv = max_lv;
  11499. if (this->nodeExists(it, "BaseLevel")) {
  11500. uint32 baselv;
  11501. if (!this->asUInt32(it, "BaseLevel", baselv))
  11502. return 0;
  11503. uint32 baselv_max = job_db.get_maxBaseLv(job_id);
  11504. if (baselv > baselv_max) {
  11505. this->invalidWarning(it["BaseLevel"], "Skill %hu's base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11506. skill_id, baselv, job_name.c_str(), baselv_max);
  11507. baselv = baselv_max;
  11508. }
  11509. entry->baselv = baselv;
  11510. } else {
  11511. if (!skill_exists)
  11512. entry->baselv = 0;
  11513. }
  11514. if (this->nodeExists(it, "JobLevel")) {
  11515. uint32 joblv;
  11516. if (!this->asUInt32(it, "JobLevel", joblv))
  11517. return 0;
  11518. uint32 joblv_max = job_db.get_maxJobLv(job_id);
  11519. if (joblv > joblv_max) {
  11520. this->invalidWarning(it["JobLevel"], "Skill %hu's job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11521. skill_id, joblv, job_name.c_str(), joblv_max);
  11522. joblv = joblv_max;
  11523. }
  11524. entry->joblv = joblv;
  11525. } else {
  11526. if (!skill_exists)
  11527. entry->joblv = 0;
  11528. }
  11529. if (this->nodeExists(it, "Requires")) {
  11530. for (const auto &Requiresit : it["Requires"]) {
  11531. if (!this->nodesExist(Requiresit, { "Name" }))
  11532. return 0;
  11533. std::string skill_name_req;
  11534. if (!this->asString(Requiresit, "Name", skill_name_req))
  11535. return 0;
  11536. uint16 skill_id_req = skill_name2id(skill_name_req.c_str());
  11537. if (skill_id_req == 0) {
  11538. this->invalidWarning(Requiresit["Name"], "Invalid skill name \"%s\".\n", skill_name_req.c_str());
  11539. return 0;
  11540. }
  11541. uint16 lv_req;
  11542. if (!this->asUInt16(Requiresit, "Level", lv_req))
  11543. return 0;
  11544. if (lv_req > MAX_SKILL_LEVEL) {
  11545. this->invalidWarning(Requiresit["Level"], "Level exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11546. return 0;
  11547. }
  11548. uint16 lv_req_max = skill_get_max(skill_id_req);
  11549. if (lv_req > lv_req_max) {
  11550. 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);
  11551. lv_req = lv_req_max;
  11552. }
  11553. if (lv_req == 0) {
  11554. if (entry->need.erase(skill_id_req) == 0)
  11555. 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());
  11556. continue;
  11557. }
  11558. entry->need[skill_id_req] = lv_req;
  11559. }
  11560. }
  11561. if (this->nodeExists(it, "Exclude")) {
  11562. bool exclude;
  11563. if (!this->asBool(it, "Exclude", exclude))
  11564. return 0;
  11565. entry->exclude_inherit = exclude;
  11566. } else {
  11567. if (!skill_exists)
  11568. entry->exclude_inherit = false;
  11569. }
  11570. if (!skill_exists)
  11571. tree->skills.insert({ skill_id, entry });
  11572. }
  11573. }
  11574. if (!exists)
  11575. this->put(job_id, tree);
  11576. return true;
  11577. }
  11578. void SkillTreeDatabase::loadingFinished() {
  11579. std::unordered_map<uint16, std::shared_ptr<s_skill_tree>> job_tree; // get the data from skill_tree_db before populate it
  11580. for (auto &data : *this) {
  11581. if (data.second->inherit_job.empty())
  11582. continue;
  11583. std::shared_ptr<s_skill_tree> skill_tree = std::make_shared<s_skill_tree>();
  11584. uint32 baselv_max = job_db.get_maxBaseLv(data.first);
  11585. uint32 joblv_max = job_db.get_maxJobLv(data.first);
  11586. for (const auto &inherit_job : data.second->inherit_job) {
  11587. std::shared_ptr<s_skill_tree> tree = this->find(inherit_job);
  11588. if (tree == nullptr || tree->skills.empty())
  11589. continue;
  11590. for (const auto &it : tree->skills) {
  11591. if (it.second->exclude_inherit)
  11592. continue;
  11593. if (data.second->skills.count(it.first) > 0) // skill already in the skill tree
  11594. continue;
  11595. if (skill_tree->skills.count(it.first) > 0) // replaced by the last inheritance
  11596. skill_tree->skills[it.first] = it.second;
  11597. else
  11598. skill_tree->skills.insert({ it.first, it.second });
  11599. std::shared_ptr<s_skill_tree_entry> skill = skill_tree->skills[it.first];
  11600. if (skill->baselv > baselv_max) {
  11601. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11602. skill_get_name(skill->skill_id), skill->skill_id, skill->baselv, job_name(data.first), baselv_max);
  11603. skill->baselv = baselv_max;
  11604. }
  11605. if (skill->joblv > joblv_max) {
  11606. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11607. skill_get_name(skill->skill_id), skill->skill_id, skill->joblv, job_name(data.first), joblv_max);
  11608. skill->joblv = joblv_max;
  11609. }
  11610. }
  11611. }
  11612. if (skill_tree != nullptr && !skill_tree->skills.empty())
  11613. job_tree.insert({ data.first, skill_tree });
  11614. }
  11615. if (!job_tree.empty()) {
  11616. for (auto &data : *this) {
  11617. if (job_tree.count(data.first) == 0)
  11618. continue;
  11619. data.second->skills.insert(job_tree[data.first]->skills.begin(), job_tree[data.first]->skills.end());
  11620. }
  11621. }
  11622. // remove skills with max_lv = 0
  11623. for (const auto &job : *this) {
  11624. if (job.second->skills.empty())
  11625. continue;
  11626. auto it = job.second->skills.begin();
  11627. while( it != job.second->skills.end() ){
  11628. if( it->second->max_lv == 0 ){
  11629. it = job.second->skills.erase( it );
  11630. }else{
  11631. it++;
  11632. }
  11633. }
  11634. }
  11635. TypesafeYamlDatabase::loadingFinished();
  11636. }
  11637. /**
  11638. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  11639. * @param level: Base level of player
  11640. * @param job_id: Job ID @see enum e_job
  11641. * @return base_hp
  11642. * @author [Cydh]
  11643. */
  11644. static unsigned int pc_calc_basehp(uint16 level, uint16 job_id) {
  11645. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11646. double base_hp = 35 + level * (job->hp_increase / 100.);
  11647. #ifndef RENEWAL
  11648. if (level >= 10 && (job_id == JOB_NINJA || job_id == JOB_GUNSLINGER))
  11649. base_hp += 90;
  11650. #endif
  11651. for (uint16 i = 2; i <= level; i++)
  11652. base_hp += floor(((job->hp_factor / 100.) * i) + 0.5); //Don't have round()
  11653. if (job_id == JOB_SUMMONER || job_id == JOB_SPIRIT_HANDLER)
  11654. base_hp += floor((base_hp / 2) + 0.5);
  11655. return (unsigned int)base_hp;
  11656. }
  11657. /**
  11658. * Calculates base sp of player.
  11659. * @param level: Base level of player
  11660. * @param job_id: Job ID @see enum e_job
  11661. * @return base_sp
  11662. * @author [Playtester]
  11663. */
  11664. static unsigned int pc_calc_basesp(uint16 level, uint16 job_id) {
  11665. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11666. double base_sp = 10 + floor(level * (job->sp_increase / 100.));
  11667. switch (job_id) {
  11668. case JOB_NINJA:
  11669. if (level >= 10)
  11670. base_sp -= 22;
  11671. else
  11672. base_sp = 11 + 3*level;
  11673. break;
  11674. case JOB_GUNSLINGER:
  11675. if (level > 10)
  11676. base_sp -= 18;
  11677. else
  11678. base_sp = 9 + 3*level;
  11679. break;
  11680. case JOB_SUMMONER:
  11681. case JOB_SPIRIT_HANDLER:
  11682. base_sp -= floor(base_sp / 2);
  11683. break;
  11684. }
  11685. return (unsigned int)base_sp;
  11686. }
  11687. const std::string JobDatabase::getDefaultLocation() {
  11688. return std::string(db_path) + "/job_stats.yml";
  11689. }
  11690. /**
  11691. * Reads and parses an entry from the job_db.
  11692. * @param node: YAML node containing the entry.
  11693. * @return count of successfully parsed rows
  11694. */
  11695. uint64 JobDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11696. if (this->nodeExists(node, "Jobs")) {
  11697. const ryml::NodeRef& jobsNode = node["Jobs"];
  11698. for (const auto &jobit : jobsNode) {
  11699. std::string job_name;
  11700. c4::from_chars(jobit.key(), &job_name);
  11701. std::string job_name_constant = "JOB_" + job_name;
  11702. int64 job_id;
  11703. if (!script_get_constant(job_name_constant.c_str(), &job_id)) {
  11704. this->invalidWarning(node["Job"], "Job %s does not exist.\n", job_name.c_str());
  11705. return 0;
  11706. }
  11707. std::shared_ptr<s_job_info> job = job_db.find(static_cast<uint16>(job_id));
  11708. bool exists = job != nullptr;
  11709. if (!exists) {
  11710. job = std::make_shared<s_job_info>();
  11711. job->job_bonus.resize(MAX_LEVEL);
  11712. std::fill(job->job_bonus.begin(), job->job_bonus.end(), std::array<uint16, PARAM_MAX> { 0 });
  11713. job->base_hp.resize(MAX_LEVEL);
  11714. std::fill(job->base_hp.begin(), job->base_hp.end(), 0);
  11715. job->base_sp.resize(MAX_LEVEL);
  11716. std::fill(job->base_sp.begin(), job->base_sp.end(), 0);
  11717. job->base_ap.resize(MAX_LEVEL);
  11718. std::fill(job->base_ap.begin(), job->base_ap.end(), 0);
  11719. }
  11720. if (this->nodeExists(node, "MaxWeight")) {
  11721. uint32 weight;
  11722. if (!this->asUInt32(node, "MaxWeight", weight))
  11723. return 0;
  11724. job->max_weight_base = weight;
  11725. } else {
  11726. if (!exists)
  11727. job->max_weight_base = 20000;
  11728. }
  11729. if (this->nodeExists(node, "HpFactor")) {
  11730. uint32 hp;
  11731. if (!this->asUInt32(node, "HpFactor", hp))
  11732. return 0;
  11733. job->hp_factor = hp;
  11734. } else {
  11735. if (!exists)
  11736. job->hp_factor = 0;
  11737. }
  11738. if (this->nodeExists(node, "HpIncrease")) {
  11739. uint32 hp;
  11740. if (!this->asUInt32(node, "HpIncrease", hp))
  11741. return 0;
  11742. job->hp_increase = hp;
  11743. } else {
  11744. if (!exists)
  11745. job->hp_increase = 500;
  11746. }
  11747. if (this->nodeExists(node, "SpIncrease")) {
  11748. uint32 sp;
  11749. if (!this->asUInt32(node, "SpIncrease", sp))
  11750. return 0;
  11751. job->sp_increase = sp;
  11752. } else {
  11753. if (!exists)
  11754. job->sp_increase = 100;
  11755. }
  11756. if (this->nodeExists(node, "BaseASPD")) {
  11757. const ryml::NodeRef& aspdNode = node["BaseASPD"];
  11758. uint8 max = MAX_WEAPON_TYPE;
  11759. #ifdef RENEWAL // Renewal adds an extra column for shields
  11760. max += 1;
  11761. #endif
  11762. if (!exists) {
  11763. job->aspd_base.resize(max);
  11764. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11765. }
  11766. for (const auto &aspdit : aspdNode) {
  11767. std::string weapon;
  11768. c4::from_chars(aspdit.key(), &weapon);
  11769. std::string weapon_constant = "W_" + weapon;
  11770. int64 constant;
  11771. if (!script_get_constant(weapon_constant.c_str(), &constant)) {
  11772. this->invalidWarning(aspdNode["BaseASPD"], "Unknown weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11773. continue;
  11774. }
  11775. if (constant < W_FIST || constant >= max) {
  11776. this->invalidWarning(aspdNode["BaseASPD"], "Invalid weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11777. continue;
  11778. }
  11779. int16 aspd;
  11780. if (!this->asInt16(aspdNode, weapon.c_str(), aspd))
  11781. return 0;
  11782. job->aspd_base[static_cast<int16>(constant)] = aspd;
  11783. }
  11784. } else {
  11785. if (!exists) {
  11786. uint8 max = MAX_WEAPON_TYPE;
  11787. #ifdef RENEWAL // Renewal adds an extra column for shields
  11788. max += 1;
  11789. #endif
  11790. job->aspd_base.resize(max);
  11791. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11792. }
  11793. }
  11794. if (this->nodeExists(node, "MaxStats")) {
  11795. const ryml::NodeRef& statNode = node["MaxStats"];
  11796. for (const auto &statit : statNode) {
  11797. std::string stat;
  11798. c4::from_chars(statit.key(), &stat);
  11799. std::string stat_constant = "PARAM_" + stat;
  11800. int64 constant;
  11801. if (!script_get_constant(stat_constant.c_str(), &constant)) {
  11802. this->invalidWarning(statNode["Bonus"], "Unknown max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11803. continue;
  11804. }
  11805. if (constant < PARAM_STR || constant >= PARAM_MAX) {
  11806. this->invalidWarning(statNode["Bonus"], "Invalid max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11807. continue;
  11808. }
  11809. uint16 max;
  11810. if (!this->asUInt16(statNode, stat.c_str(), max))
  11811. return 0;
  11812. job->max_param[constant] = max;
  11813. }
  11814. }
  11815. if (this->nodeExists(node, "MaxBaseLevel")) {
  11816. uint16 level;
  11817. if (!this->asUInt16(node, "MaxBaseLevel", level))
  11818. return 0;
  11819. if (level == 0 || level > MAX_LEVEL) {
  11820. this->invalidWarning(node["MaxBaseLevel"], "MaxBaseLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11821. level = MAX_LEVEL;
  11822. }
  11823. job->max_base_level = level;
  11824. } else {
  11825. if (!exists)
  11826. job->max_base_level = MAX_LEVEL;
  11827. }
  11828. if (this->nodeExists(node, "BaseExp")) {
  11829. for (const ryml::NodeRef& bexpNode : node["BaseExp"]) {
  11830. uint16 level;
  11831. if (!this->asUInt16(bexpNode, "Level", level))
  11832. return 0;
  11833. if (level > job->max_base_level)
  11834. continue;
  11835. if (level == 0 || level > MAX_LEVEL) {
  11836. this->invalidWarning(bexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11837. return 0;
  11838. }
  11839. if (this->nodeExists(bexpNode, "Exp")) {
  11840. t_exp exp;
  11841. if (!this->asUInt64(bexpNode, "Exp", exp))
  11842. return 0;
  11843. job->base_exp[level - 1] = exp;
  11844. }
  11845. }
  11846. }
  11847. if (this->nodeExists(node, "MaxJobLevel")) {
  11848. uint16 level;
  11849. if (!this->asUInt16(node, "MaxJobLevel", level))
  11850. return 0;
  11851. if (level == 0 || level > MAX_LEVEL) {
  11852. this->invalidWarning(node["MaxJobLevel"], "MaxJobLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11853. level = MAX_LEVEL;
  11854. }
  11855. job->max_job_level = level;
  11856. } else {
  11857. if (!exists)
  11858. job->max_job_level = MAX_LEVEL;
  11859. }
  11860. if (this->nodeExists(node, "JobExp")) {
  11861. for (const ryml::NodeRef& jexpNode : node["JobExp"]) {
  11862. uint16 level;
  11863. if (!this->asUInt16(jexpNode, "Level", level))
  11864. return 0;
  11865. if (level > job->max_job_level)
  11866. continue;
  11867. if (level == 0 || level > MAX_LEVEL) {
  11868. this->invalidWarning(jexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11869. return 0;
  11870. }
  11871. if (this->nodeExists(jexpNode, "Exp")) {
  11872. t_exp exp;
  11873. if (!this->asUInt64(jexpNode, "Exp", exp))
  11874. return 0;
  11875. job->job_exp[level - 1] = exp;
  11876. }
  11877. }
  11878. }
  11879. if (this->nodeExists(node, "BonusStats")) {
  11880. const ryml::NodeRef& bonusNode = node["BonusStats"];
  11881. for (const ryml::NodeRef& levelNode : bonusNode) {
  11882. uint16 level;
  11883. if (!this->asUInt16(levelNode, "Level", level))
  11884. return 0;
  11885. if (level == 0 || level > MAX_LEVEL) {
  11886. this->invalidWarning(levelNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11887. return 0;
  11888. }
  11889. for (uint8 idx = PARAM_STR; idx < PARAM_MAX; idx++) {
  11890. if (this->nodeExists(levelNode, parameter_names[idx])) {
  11891. int16 change;
  11892. if (!this->asInt16(levelNode, parameter_names[idx], change))
  11893. return 0;
  11894. job->job_bonus[level - 1][idx] = change;
  11895. }
  11896. }
  11897. }
  11898. }
  11899. #ifdef HP_SP_TABLES
  11900. if (this->nodeExists(node, "BaseHp")) {
  11901. for (const ryml::NodeRef& bhpNode : node["BaseHp"]) {
  11902. uint16 level;
  11903. if (!this->asUInt16(bhpNode, "Level", level))
  11904. return 0;
  11905. if (level > job->max_base_level)
  11906. continue;
  11907. if (level == 0 || level > MAX_LEVEL) {
  11908. this->invalidWarning(bhpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11909. return 0;
  11910. }
  11911. if (this->nodeExists(bhpNode, "Hp")) {
  11912. uint32 points;
  11913. if (!this->asUInt32(bhpNode, "Hp", points))
  11914. return 0;
  11915. job->base_hp[level - 1] = points;
  11916. }
  11917. }
  11918. }
  11919. if (this->nodeExists(node, "BaseSp")) {
  11920. for (const ryml::NodeRef& bspNode : node["BaseSp"]) {
  11921. uint16 level;
  11922. if (!this->asUInt16(bspNode, "Level", level))
  11923. return 0;
  11924. if (level > job->max_base_level)
  11925. continue;
  11926. if (level == 0 || level > MAX_LEVEL) {
  11927. this->invalidWarning(bspNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11928. return 0;
  11929. }
  11930. if (this->nodeExists(bspNode, "Sp")) {
  11931. uint32 points;
  11932. if (!this->asUInt32(bspNode, "Sp", points))
  11933. return 0;
  11934. job->base_sp[level - 1] = points;
  11935. }
  11936. }
  11937. }
  11938. if (this->nodeExists(node, "BaseAp")) {
  11939. for (const ryml::NodeRef& bapNode : node["BaseAp"]) {
  11940. uint16 level;
  11941. if (!this->asUInt16(bapNode, "Level", level))
  11942. return 0;
  11943. if (level > job->max_base_level)
  11944. continue;
  11945. if (level == 0 || level > MAX_LEVEL) {
  11946. this->invalidWarning(bapNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11947. return 0;
  11948. }
  11949. if (this->nodeExists(bapNode, "Ap")) {
  11950. uint32 points;
  11951. if (!this->asUInt32(bapNode, "Ap", points))
  11952. return 0;
  11953. job->base_ap[level - 1] = points;
  11954. }
  11955. }
  11956. }
  11957. #endif
  11958. if (!exists)
  11959. this->put(static_cast<uint16>(job_id), job);
  11960. }
  11961. }
  11962. return 1;
  11963. }
  11964. void JobDatabase::loadingFinished() {
  11965. // Checking if all class have their data
  11966. for (auto &jobIt : *this) {
  11967. uint16 job_id = jobIt.first;
  11968. if (!pcdb_checkid(job_id))
  11969. continue;
  11970. 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)
  11971. continue; // Classes that do not need exp tables.
  11972. std::shared_ptr<s_job_info> job = jobIt.second;
  11973. uint16 maxBaseLv = job->max_base_level, maxJobLv = job->max_job_level;
  11974. if (!maxBaseLv)
  11975. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(job_id), job_id);
  11976. if (!maxJobLv)
  11977. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(job_id), job_id);
  11978. // Init and checking the empty value of Base HP/SP [Cydh]
  11979. if (job->base_hp.empty())
  11980. job->base_hp.resize(maxBaseLv);
  11981. for (uint16 j = 0; j < maxBaseLv; j++) {
  11982. if (job->base_hp[j] == 0)
  11983. job->base_hp[j] = pc_calc_basehp(j + 1, job_id);
  11984. }
  11985. if (job->base_sp.empty())
  11986. job->base_sp.resize(maxBaseLv);
  11987. for (uint16 j = 0; j < maxBaseLv; j++) {
  11988. if (job->base_sp[j] == 0)
  11989. job->base_sp[j] = pc_calc_basesp(j + 1, job_id);
  11990. }
  11991. // Resize to the maximum base level
  11992. if (job->base_hp.capacity() > maxBaseLv)
  11993. job->base_hp.erase(job->base_hp.begin() + maxBaseLv, job->base_hp.end());
  11994. if (job->base_sp.capacity() > maxBaseLv)
  11995. job->base_sp.erase(job->base_sp.begin() + maxBaseLv, job->base_sp.end());
  11996. if (job->base_ap.capacity() > maxBaseLv)
  11997. job->base_ap.erase(job->base_ap.begin() + maxBaseLv, job->base_ap.end());
  11998. // Resize to the maximum job level
  11999. if (job->job_bonus.capacity() > maxJobLv)
  12000. job->job_bonus.erase(job->job_bonus.begin() + maxJobLv, job->job_bonus.end());
  12001. for (uint16 parameter = PARAM_STR; parameter < PARAM_MAX; parameter++) {
  12002. // Store total
  12003. int16 current = 0;
  12004. for (uint16 job_level = 0; job_level < maxJobLv; job_level++) {
  12005. // Add the bonus from this job level
  12006. current += job->job_bonus[job_level][parameter];
  12007. // Set the new total on this job level
  12008. job->job_bonus[job_level][parameter] = current;
  12009. }
  12010. }
  12011. uint64 class_ = pc_jobid2mapid( job_id );
  12012. // Set normal status limits
  12013. uint16 max = battle_config.max_parameter;
  12014. do{
  12015. // Always check babies first
  12016. if( class_ & JOBL_BABY ){
  12017. if( class_ & JOBL_THIRD ){
  12018. max = battle_config.max_baby_third_parameter;
  12019. break;
  12020. }else{
  12021. max = battle_config.max_baby_parameter;
  12022. break;
  12023. }
  12024. }
  12025. // Summoner
  12026. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  12027. max = battle_config.max_summoner_parameter;
  12028. break;
  12029. }
  12030. // Extended classes
  12031. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  12032. max = battle_config.max_extended_parameter;
  12033. break;
  12034. }
  12035. if( class_ & JOBL_FOURTH ){
  12036. max = battle_config.max_fourth_parameter;
  12037. break;
  12038. }
  12039. // 3rd class
  12040. if( class_ & JOBL_THIRD ){
  12041. // Transcendent
  12042. if( class_ & JOBL_UPPER ){
  12043. max = battle_config.max_third_trans_parameter;
  12044. break;
  12045. }else{
  12046. max = battle_config.max_third_parameter;
  12047. break;
  12048. }
  12049. }
  12050. // Transcendent
  12051. if( class_ & JOBL_UPPER ){
  12052. max = battle_config.max_trans_parameter;
  12053. break;
  12054. }
  12055. }while( false );
  12056. for( uint16 parameter = PARAM_STR; parameter < PARAM_POW; parameter++ ){
  12057. // If it is not explicitly set in the database file
  12058. if( job->max_param[parameter] == 0 ){
  12059. job->max_param[parameter] = max;
  12060. }
  12061. }
  12062. // Set trait status limit
  12063. if( class_ & JOBL_FOURTH ){
  12064. max = battle_config.max_trait_parameter;
  12065. }else{
  12066. max = 0;
  12067. }
  12068. for( uint16 parameter = PARAM_POW; parameter < PARAM_MAX; parameter++ ){
  12069. // If it is not explicitly set in the database file
  12070. if( job->max_param[parameter] == 0 ){
  12071. job->max_param[parameter] = max;
  12072. }
  12073. }
  12074. }
  12075. TypesafeCachedYamlDatabase::loadingFinished();
  12076. }
  12077. /**
  12078. * Read job_noenter_map.txt
  12079. **/
  12080. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  12081. int class_ = -1;
  12082. int64 class_tmp;
  12083. if (ISDIGIT(str[0][0])) {
  12084. class_ = atoi(str[0]);
  12085. } else {
  12086. if (!script_get_constant(str[0], &class_tmp)) {
  12087. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  12088. return false;
  12089. }
  12090. class_ = static_cast<int>(class_tmp);
  12091. }
  12092. if (!pcdb_checkid(class_)) {
  12093. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  12094. return false;
  12095. }
  12096. std::shared_ptr<s_job_info> job = job_db.find(class_);
  12097. if (job == nullptr) {
  12098. ShowError("pc_readdb_job_noenter_map: Job %d data not initialized.\n", class_);
  12099. return false;
  12100. }
  12101. job->noenter_map.zone = atoi(str[1]);
  12102. job->noenter_map.group_lv = atoi(str[2]);
  12103. return true;
  12104. }
  12105. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  12106. return std::string(db_path) + "/statpoint.yml";
  12107. }
  12108. uint64 PlayerStatPointDatabase::parseBodyNode(const ryml::NodeRef& node) {
  12109. if (!this->nodesExist(node, { "Level", "Points" })) {
  12110. return 0;
  12111. }
  12112. uint16 level;
  12113. if (!this->asUInt16(node, "Level", level))
  12114. return 0;
  12115. uint32 point;
  12116. if (!this->asUInt32(node, "Points", point))
  12117. return 0;
  12118. if (level == 0) {
  12119. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  12120. return 0;
  12121. }
  12122. if (level > MAX_LEVEL) {
  12123. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  12124. return 0;
  12125. }
  12126. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12127. bool exists = entry != nullptr;
  12128. if( !exists ){
  12129. entry = std::make_shared<s_statpoint_entry>();
  12130. entry->level = level;
  12131. entry->statpoints = point;
  12132. }
  12133. if( this->nodeExists( node, "TraitPoints" ) ){
  12134. uint32 traitpoints;
  12135. if( !this->asUInt32( node, "TraitPoints", traitpoints ) ){
  12136. return 0;
  12137. }
  12138. entry->traitpoints = traitpoints;
  12139. }else{
  12140. if( !exists ){
  12141. entry->traitpoints = 0;
  12142. }
  12143. }
  12144. if( !exists ){
  12145. this->put( level, entry );
  12146. }
  12147. return 1;
  12148. }
  12149. /**
  12150. * Generate the remaining parts of the db if necessary.
  12151. */
  12152. void PlayerStatPointDatabase::loadingFinished(){
  12153. const uint16 trait_start_level = 200;
  12154. std::shared_ptr<s_statpoint_entry> level_one = this->find( 1 );
  12155. if( level_one == nullptr ){
  12156. if( battle_config.use_statpoint_table ){
  12157. ShowError( "Missing status points for Level 1\n" );
  12158. }
  12159. level_one = std::make_shared<s_statpoint_entry>();
  12160. level_one->level = 1;
  12161. level_one->statpoints = inter_config.start_status_points;
  12162. level_one->traitpoints = 0;
  12163. this->put( 1, level_one );
  12164. }else if( battle_config.use_statpoint_table ){
  12165. if( level_one->statpoints != inter_config.start_status_points ){
  12166. ShowError( "Status points for Level 1 (=%u) do not match inter_athena.conf value (=%u).\n", level_one->statpoints, inter_config.start_status_points );
  12167. level_one->statpoints = inter_config.start_status_points;
  12168. }
  12169. }else{
  12170. level_one->statpoints = inter_config.start_status_points;
  12171. level_one->traitpoints = 0;
  12172. }
  12173. std::shared_ptr<s_statpoint_entry> last_level = level_one;
  12174. for( uint16 level = 2; level <= MAX_LEVEL; level++ ){
  12175. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12176. bool exists = entry != nullptr;
  12177. if( !exists ){
  12178. entry = std::make_shared<s_statpoint_entry>();
  12179. entry->level = level;
  12180. this->put( level, entry );
  12181. }
  12182. if( !battle_config.use_statpoint_table || !exists ){
  12183. if( battle_config.use_statpoint_table ){
  12184. ShowError("Missing status points for Level %hu\n", level);
  12185. }
  12186. if( level <= trait_start_level ){
  12187. entry->statpoints = last_level->statpoints + ( ( level - 1 + 15 ) / 5 );
  12188. }else{
  12189. entry->statpoints = last_level->statpoints;
  12190. }
  12191. }
  12192. if( !battle_config.use_traitpoint_table || !exists ){
  12193. if( battle_config.use_traitpoint_table && level > trait_start_level ){
  12194. ShowError( "Missing trait points for Level %hu\n", level );
  12195. }
  12196. if( level > trait_start_level ){
  12197. entry->traitpoints = ( level - trait_start_level ) * 3 + ( level - trait_start_level ) / 5 * 4;
  12198. }else{
  12199. entry->traitpoints = 0;
  12200. }
  12201. }
  12202. // Store it for next iteration
  12203. last_level = entry;
  12204. }
  12205. TypesafeCachedYamlDatabase::loadingFinished();
  12206. }
  12207. /*==========================================
  12208. * pc DB reading.
  12209. * job_stats.yml - Job values
  12210. * skill_tree.txt - skill tree for every class
  12211. * attr_fix.yml - elemental adjustment table
  12212. *------------------------------------------*/
  12213. void pc_readdb(void) {
  12214. int i, s = 1;
  12215. const char* dbsubpath[] = {
  12216. "",
  12217. "/" DBIMPORT,
  12218. //add other path here
  12219. };
  12220. //reset
  12221. job_db.clear(); // job_info table
  12222. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  12223. penalty_db.load();
  12224. #endif
  12225. statpoint_db.clear();
  12226. job_db.load();
  12227. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  12228. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  12229. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  12230. char* dbsubpath1 = (char*)aMalloc(n1+1);
  12231. char* dbsubpath2 = (char*)aMalloc(n2+1);
  12232. if(i==0) {
  12233. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12234. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  12235. }
  12236. else {
  12237. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12238. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  12239. }
  12240. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  12241. aFree(dbsubpath1);
  12242. aFree(dbsubpath2);
  12243. }
  12244. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  12245. skill_tree_db.reload();
  12246. statpoint_db.load();
  12247. }
  12248. // Read MOTD on startup. [Valaris]
  12249. int pc_read_motd(void)
  12250. {
  12251. FILE* fp;
  12252. // clear old MOTD
  12253. memset(motd_text, 0, sizeof(motd_text));
  12254. // read current MOTD
  12255. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  12256. {
  12257. unsigned int entries = 0;
  12258. char buf[CHAT_SIZE_MAX];
  12259. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  12260. {
  12261. unsigned int lines = 0;
  12262. size_t len;
  12263. lines++;
  12264. if( buf[0] == '/' && buf[1] == '/' )
  12265. continue;
  12266. len = strlen(buf);
  12267. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  12268. len--;
  12269. if( len ) {
  12270. char * ptr;
  12271. buf[len] = 0;
  12272. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  12273. 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);
  12274. }
  12275. else {// empty line
  12276. buf[0] = ' ';
  12277. buf[1] = 0;
  12278. }
  12279. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  12280. entries++;
  12281. }
  12282. fclose(fp);
  12283. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  12284. }
  12285. else
  12286. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  12287. return 0;
  12288. }
  12289. void pc_itemcd_do(map_session_data *sd, bool load) {
  12290. int i,cursor = 0;
  12291. struct item_cd* cd = NULL;
  12292. if( load ) {
  12293. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  12294. // no item cooldown is associated with this character
  12295. return;
  12296. }
  12297. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12298. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  12299. sd->item_delay[cursor].tick = cd->tick[i];
  12300. sd->item_delay[cursor].nameid = cd->nameid[i];
  12301. cursor++;
  12302. }
  12303. }
  12304. idb_remove(itemcd_db,sd->status.char_id);
  12305. } else {
  12306. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  12307. // create a new skill cooldown object for map storage
  12308. CREATE( cd, struct item_cd, 1 );
  12309. idb_put( itemcd_db, sd->status.char_id, cd );
  12310. }
  12311. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12312. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  12313. cd->tick[cursor] = sd->item_delay[i].tick;
  12314. cd->nameid[cursor] = sd->item_delay[i].nameid;
  12315. cursor++;
  12316. }
  12317. }
  12318. }
  12319. return;
  12320. }
  12321. /**
  12322. * Add item delay to player's item delay data
  12323. * @param sd Player
  12324. * @param id Item data
  12325. * @param tick Current tick
  12326. * @param n Item index in inventory
  12327. * @return 0: No delay, can consume item.
  12328. * 1: Has delay, cancel consumption.
  12329. **/
  12330. uint8 pc_itemcd_add(map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12331. int i;
  12332. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  12333. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  12334. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  12335. if( i < MAX_ITEMDELAYS ) {
  12336. if( sd->item_delay[i].nameid ) {// found
  12337. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  12338. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  12339. char e_msg[CHAT_SIZE_MAX];
  12340. if( e_tick > 99 )
  12341. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  12342. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  12343. else
  12344. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  12345. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  12346. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  12347. return 1; // Delay has not expired yet
  12348. }
  12349. } else {// not yet used item (all slots are initially empty)
  12350. sd->item_delay[i].nameid = id->nameid;
  12351. }
  12352. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  12353. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  12354. } else {// should not happen
  12355. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  12356. }
  12357. //clean up used delays so we can give room for more
  12358. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12359. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  12360. sd->item_delay[i].tick = 0;
  12361. sd->item_delay[i].nameid = 0;
  12362. }
  12363. }
  12364. return 0;
  12365. }
  12366. /**
  12367. * Check if player has delay to reuse item
  12368. * @param sd Player
  12369. * @param id Item data
  12370. * @param tick Current tick
  12371. * @param n Item index in inventory
  12372. * @return 0: No delay, can consume item.
  12373. * 1: Has delay, cancel consumption.
  12374. **/
  12375. uint8 pc_itemcd_check(map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12376. status_change *sc = NULL;
  12377. nullpo_retr(0, sd);
  12378. nullpo_retr(0, id);
  12379. // Do normal delay assignment
  12380. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  12381. return pc_itemcd_add(sd, id, tick, n);
  12382. // Send reply of delay remains
  12383. if (sc->getSCE(id->delay.sc)) {
  12384. const struct TimerData *timer = get_timer(sc->getSCE(id->delay.sc)->timer);
  12385. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  12386. return 1;
  12387. }
  12388. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  12389. return 0;
  12390. }
  12391. /**
  12392. * Clear the dmglog data from player
  12393. * @param sd
  12394. * @param md
  12395. **/
  12396. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  12397. {
  12398. uint8 i;
  12399. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  12400. if (i < DAMAGELOG_SIZE) {
  12401. md->dmglog[i].id = 0;
  12402. md->dmglog[i].dmg = 0;
  12403. md->dmglog[i].flag = 0;
  12404. }
  12405. }
  12406. /**
  12407. * Add log to player's dmglog
  12408. * @param sd
  12409. * @param id Monster's GID
  12410. **/
  12411. void pc_damage_log_add(map_session_data *sd, int id)
  12412. {
  12413. uint8 i = 0;
  12414. if (!sd || !id)
  12415. return;
  12416. //Only store new data, don't need to renew the old one with same id
  12417. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  12418. if (i < DAMAGELOG_SIZE_PC)
  12419. return;
  12420. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12421. if (sd->dmglog[i] == 0) {
  12422. sd->dmglog[i] = id;
  12423. return;
  12424. }
  12425. }
  12426. }
  12427. /**
  12428. * Clear dmglog data from player
  12429. * @param sd
  12430. * @param id Monster's id
  12431. **/
  12432. void pc_damage_log_clear(map_session_data *sd, int id)
  12433. {
  12434. uint8 i;
  12435. struct mob_data *md = NULL;
  12436. if (!sd)
  12437. return;
  12438. if (!id) {
  12439. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12440. if( !sd->dmglog[i] ) //skip the empty value
  12441. continue;
  12442. if ((md = map_id2md(sd->dmglog[i])))
  12443. pc_clear_log_damage_sub(sd->status.char_id,md);
  12444. sd->dmglog[i] = 0;
  12445. }
  12446. }
  12447. else {
  12448. if ((md = map_id2md(id)))
  12449. pc_clear_log_damage_sub(sd->status.char_id,md);
  12450. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  12451. if (i < DAMAGELOG_SIZE_PC)
  12452. sd->dmglog[i] = 0;
  12453. }
  12454. }
  12455. /**
  12456. * Status change data arrived from char-server
  12457. * @param sd: Player data
  12458. */
  12459. void pc_scdata_received(map_session_data *sd) {
  12460. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  12461. clif_weight_limit( sd );
  12462. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) && pc_attendance_counter(sd) < 200 ){
  12463. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  12464. }
  12465. sd->state.pc_loaded = true;
  12466. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  12467. sd->state.connect_new = 1;
  12468. clif_parse_LoadEndAck(sd->fd, sd);
  12469. }
  12470. if (pc_iscarton(sd)) {
  12471. sd->cart_weight_max = 0; // Force a client refesh
  12472. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  12473. }
  12474. if (sd->sc.getSCE(SC_SOULENERGY))
  12475. sd->soulball = sd->sc.getSCE(SC_SOULENERGY)->val1;
  12476. }
  12477. /**
  12478. * Check player account expiration time and rental item expirations
  12479. * @param sd: Player data
  12480. */
  12481. void pc_check_expiration(map_session_data *sd) {
  12482. #ifndef ENABLE_SC_SAVING
  12483. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  12484. #endif
  12485. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  12486. time_t exp_time = sd->expiration_time;
  12487. char tmpstr[1024];
  12488. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  12489. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  12490. pc_expire_check(sd);
  12491. }
  12492. }
  12493. TIMER_FUNC(pc_expiration_timer){
  12494. map_session_data *sd = map_id2sd(id);
  12495. if( !sd ) return 0;
  12496. sd->expiration_tid = INVALID_TIMER;
  12497. if( sd->fd )
  12498. clif_authfail_fd(sd->fd,10);
  12499. map_quit(sd);
  12500. return 0;
  12501. }
  12502. TIMER_FUNC(pc_autotrade_timer){
  12503. map_session_data *sd = map_id2sd(id);
  12504. if (!sd)
  12505. return 0;
  12506. sd->autotrade_tid = INVALID_TIMER;
  12507. if (sd->state.autotrade&2)
  12508. vending_reopen(sd);
  12509. if (sd->state.autotrade&4)
  12510. buyingstore_reopen(sd);
  12511. if (!sd->vender_id && !sd->buyer_id) {
  12512. sd->state.autotrade = 0;
  12513. map_quit(sd);
  12514. }
  12515. return 0;
  12516. }
  12517. /* this timer exists only when a character with a expire timer > 24h is online */
  12518. /* it loops thru online players once an hour to check whether a new < 24h is available */
  12519. TIMER_FUNC(pc_global_expiration_timer){
  12520. struct s_mapiterator* iter;
  12521. map_session_data* sd;
  12522. iter = mapit_getallusers();
  12523. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  12524. if( sd->expiration_time )
  12525. pc_expire_check(sd);
  12526. mapit_free(iter);
  12527. return 0;
  12528. }
  12529. void pc_expire_check(map_session_data *sd) {
  12530. /* ongoing timer */
  12531. if( sd->expiration_tid != INVALID_TIMER )
  12532. return;
  12533. /* not within the next 24h, enable the global check */
  12534. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  12535. /* global check not running, enable */
  12536. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  12537. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  12538. return;
  12539. }
  12540. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  12541. }
  12542. /**
  12543. * Deposit some money to bank
  12544. * @param sd
  12545. * @param money Amount of money to deposit
  12546. **/
  12547. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(map_session_data *sd, int money) {
  12548. if (battle_config.feature_banking_state_enforce && !sd->state.banking) {
  12549. return BDA_ERROR;
  12550. }
  12551. unsigned int limit_check = money + sd->bank_vault;
  12552. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  12553. return BDA_OVERFLOW;
  12554. } else if ( money > sd->status.zeny ) {
  12555. return BDA_NO_MONEY;
  12556. }
  12557. if( pc_payzeny(sd, money, LOG_TYPE_BANK) )
  12558. return BDA_NO_MONEY;
  12559. sd->bank_vault += money;
  12560. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12561. if( save_settings&CHARSAVE_BANK )
  12562. chrif_save(sd, CSAVE_NORMAL);
  12563. return BDA_SUCCESS;
  12564. }
  12565. /**
  12566. * Withdraw money from bank
  12567. * @param sd
  12568. * @param money Amount of money that will be withdrawn
  12569. **/
  12570. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(map_session_data *sd, int money) {
  12571. if (battle_config.feature_banking_state_enforce && !sd->state.banking) {
  12572. return BWA_UNKNOWN_ERROR;
  12573. }
  12574. unsigned int limit_check = money + sd->status.zeny;
  12575. if( money <= 0 ) {
  12576. return BWA_UNKNOWN_ERROR;
  12577. } else if ( money > sd->bank_vault ) {
  12578. return BWA_NO_MONEY;
  12579. } else if ( limit_check > MAX_ZENY ) {
  12580. /* no official response for this scenario exists. */
  12581. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  12582. return BWA_UNKNOWN_ERROR;
  12583. }
  12584. if( pc_getzeny(sd,money, LOG_TYPE_BANK) )
  12585. return BWA_NO_MONEY;
  12586. sd->bank_vault -= money;
  12587. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12588. if( save_settings&CHARSAVE_BANK )
  12589. chrif_save(sd, CSAVE_NORMAL);
  12590. return BWA_SUCCESS;
  12591. }
  12592. /**
  12593. * Clear Crimson Marker data from caster
  12594. * @param sd: Player
  12595. **/
  12596. void pc_crimson_marker_clear(map_session_data *sd) {
  12597. uint8 i;
  12598. if (!sd)
  12599. return;
  12600. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  12601. struct block_list *bl = NULL;
  12602. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  12603. status_change_end(bl,SC_C_MARKER);
  12604. sd->c_marker[i] = 0;
  12605. }
  12606. }
  12607. /**
  12608. * Show version to player
  12609. * @param sd: Player
  12610. **/
  12611. void pc_show_version(map_session_data *sd) {
  12612. const char* svn = get_svn_revision();
  12613. char buf[CHAT_SIZE_MAX];
  12614. if( svn[0] != UNKNOWN_VERSION )
  12615. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  12616. else {
  12617. const char* git = get_git_hash();
  12618. if( git[0] != UNKNOWN_VERSION )
  12619. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  12620. else
  12621. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  12622. }
  12623. clif_displaymessage(sd->fd,buf);
  12624. }
  12625. /**
  12626. * Run bonus_script on player
  12627. * @param sd
  12628. * @author [Cydh]
  12629. **/
  12630. void pc_bonus_script(map_session_data *sd) {
  12631. t_tick now = gettick();
  12632. struct linkdb_node *node = NULL, *next = NULL;
  12633. if (!sd || !(node = sd->bonus_script.head))
  12634. return;
  12635. while (node) {
  12636. struct s_bonus_script_entry *entry = NULL;
  12637. next = node->next;
  12638. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  12639. // Only start timer for new bonus_script
  12640. if (entry->tid == INVALID_TIMER) {
  12641. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  12642. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  12643. entry->tick += now;
  12644. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  12645. }
  12646. if (entry->script)
  12647. run_script(entry->script, 0, sd->bl.id, 0);
  12648. else
  12649. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  12650. }
  12651. node = next;
  12652. }
  12653. }
  12654. /**
  12655. * Add bonus_script to player
  12656. * @param sd Player
  12657. * @param script_str Script string
  12658. * @param dur Duration in ms
  12659. * @param icon EFST
  12660. * @param flag Flags @see enum e_bonus_script_flags
  12661. * @param type 0 - None, 1 - Buff, 2 - Debuff
  12662. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  12663. * @author [Cydh]
  12664. **/
  12665. struct s_bonus_script_entry *pc_bonus_script_add(map_session_data *sd, const char *script_str, t_tick dur, enum efst_type icon, uint16 flag, uint8 type) {
  12666. struct script_code *script = NULL;
  12667. struct linkdb_node *node = NULL;
  12668. struct s_bonus_script_entry *entry = NULL;
  12669. if (!sd)
  12670. return NULL;
  12671. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  12672. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  12673. return NULL;
  12674. }
  12675. // Duplication checks
  12676. if ((node = sd->bonus_script.head)) {
  12677. while (node) {
  12678. entry = (struct s_bonus_script_entry *)node->data;
  12679. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  12680. t_tick newdur = gettick() + dur;
  12681. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  12682. settick_timer(entry->tid, newdur);
  12683. script_free_code(script);
  12684. return NULL;
  12685. }
  12686. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  12687. break;
  12688. else { // No duplicate bonus
  12689. script_free_code(script);
  12690. return NULL;
  12691. }
  12692. }
  12693. node = node->next;
  12694. }
  12695. }
  12696. CREATE(entry, struct s_bonus_script_entry, 1);
  12697. entry->script_buf = StringBuf_Malloc();
  12698. StringBuf_AppendStr(entry->script_buf, script_str);
  12699. entry->tid = INVALID_TIMER;
  12700. entry->flag = flag;
  12701. entry->icon = icon;
  12702. entry->tick = dur; // Use duration first, on run change to expire time
  12703. entry->type = type;
  12704. entry->script = script;
  12705. sd->bonus_script.count++;
  12706. return entry;
  12707. }
  12708. /**
  12709. * Remove bonus_script data from player
  12710. * @param sd: Target player
  12711. * @param list: Bonus script entry from player
  12712. * @author [Cydh]
  12713. **/
  12714. void pc_bonus_script_free_entry(map_session_data *sd, struct s_bonus_script_entry *entry) {
  12715. if (entry->tid != INVALID_TIMER)
  12716. delete_timer(entry->tid, pc_bonus_script_timer);
  12717. if (entry->script)
  12718. script_free_code(entry->script);
  12719. if (entry->script_buf)
  12720. StringBuf_Free(entry->script_buf);
  12721. if (sd) {
  12722. if (entry->icon != EFST_BLANK)
  12723. clif_status_load(&sd->bl, entry->icon, 0);
  12724. if (sd->bonus_script.count > 0)
  12725. sd->bonus_script.count--;
  12726. }
  12727. aFree(entry);
  12728. }
  12729. /**
  12730. * Do final process if no entry left
  12731. * @param sd
  12732. **/
  12733. static void inline pc_bonus_script_check_final(map_session_data *sd) {
  12734. if (sd->bonus_script.count == 0) {
  12735. if (sd->bonus_script.head && sd->bonus_script.head->data)
  12736. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  12737. linkdb_final(&sd->bonus_script.head);
  12738. }
  12739. }
  12740. /**
  12741. * Timer for bonus_script
  12742. * @param tid
  12743. * @param tick
  12744. * @param id
  12745. * @param data
  12746. * @author [Cydh]
  12747. **/
  12748. TIMER_FUNC(pc_bonus_script_timer){
  12749. map_session_data *sd;
  12750. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  12751. sd = map_id2sd(id);
  12752. if (!sd) {
  12753. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  12754. return 0;
  12755. }
  12756. if (tid == INVALID_TIMER)
  12757. return 0;
  12758. if (!sd->bonus_script.head || entry == NULL) {
  12759. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  12760. return 0;
  12761. }
  12762. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12763. pc_bonus_script_free_entry(sd, entry);
  12764. pc_bonus_script_check_final(sd);
  12765. status_calc_pc(sd,SCO_NONE);
  12766. return 0;
  12767. }
  12768. /**
  12769. * Check then clear all active timer(s) of bonus_script data from player based on reason
  12770. * @param sd: Target player
  12771. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  12772. * @author [Cydh]
  12773. **/
  12774. void pc_bonus_script_clear(map_session_data *sd, uint32 flag) {
  12775. struct linkdb_node *node = NULL;
  12776. uint16 count = 0;
  12777. if (!sd || !(node = sd->bonus_script.head))
  12778. return;
  12779. while (node) {
  12780. struct linkdb_node *next = node->next;
  12781. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  12782. if (entry && (
  12783. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  12784. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  12785. (flag&entry->flag) || // Matched flag
  12786. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  12787. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  12788. )
  12789. )
  12790. {
  12791. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12792. pc_bonus_script_free_entry(sd, entry);
  12793. count++;
  12794. }
  12795. node = next;
  12796. }
  12797. pc_bonus_script_check_final(sd);
  12798. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  12799. status_calc_pc(sd,SCO_NONE);
  12800. }
  12801. /** [Cydh]
  12802. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  12803. * @param sd: Target player
  12804. */
  12805. void pc_cell_basilica(map_session_data *sd) {
  12806. nullpo_retv(sd);
  12807. #ifdef RENEWAL
  12808. enum sc_type type = SC_BASILICA_CELL;
  12809. #else
  12810. enum sc_type type = SC_BASILICA;
  12811. #endif
  12812. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  12813. if (sd->sc.getSCE(type))
  12814. status_change_end(&sd->bl, type);
  12815. }
  12816. else if (!sd->sc.getSCE(type))
  12817. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  12818. }
  12819. /** [Cydh]
  12820. * Get maximum specified parameter for specified class
  12821. * @param sd: Player data
  12822. * @param param: Max parameter to check
  12823. * @return max_param
  12824. */
  12825. uint16 pc_maxparameter(map_session_data *sd, e_params param) {
  12826. nullpo_retr(0, sd);
  12827. std::shared_ptr<s_job_info> job = job_db.find(pc_mapid2jobid(sd->class_,sd->status.sex));
  12828. if( job == nullptr || param == PARAM_MAX ){
  12829. return 0;
  12830. }
  12831. return job->max_param[param];
  12832. }
  12833. /**
  12834. * Get max ASPD for player based on Class
  12835. * @param sd Player
  12836. * @return ASPD
  12837. */
  12838. short pc_maxaspd(map_session_data *sd) {
  12839. nullpo_ret(sd);
  12840. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  12841. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  12842. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  12843. battle_config.max_aspd ));
  12844. }
  12845. /**
  12846. * Calculates total item-group related bonuses for the given item
  12847. * @param sd Player
  12848. * @param nameid Item ID
  12849. * @return Heal rate
  12850. **/
  12851. short pc_get_itemgroup_bonus(map_session_data* sd, t_itemid nameid, std::vector<s_item_bonus>& bonuses) {
  12852. if (bonuses.empty())
  12853. return 0;
  12854. short bonus = 0;
  12855. for (const auto &it : bonuses) {
  12856. uint16 group_id = it.id;
  12857. if (group_id == 0)
  12858. continue;
  12859. if (itemdb_group.item_exists(group_id, nameid))
  12860. bonus += it.val;
  12861. }
  12862. return bonus;
  12863. }
  12864. /**
  12865. * Calculates total item-group related bonuses for the given item group
  12866. * @param sd Player
  12867. * @param group_id Item Group ID
  12868. * @return Heal rate
  12869. **/
  12870. short pc_get_itemgroup_bonus_group(map_session_data* sd, uint16 group_id, std::vector<s_item_bonus>& bonuses) {
  12871. if (bonuses.empty())
  12872. return 0;
  12873. for (const auto &it : bonuses) {
  12874. if (it.id == group_id)
  12875. return it.val;
  12876. }
  12877. return 0;
  12878. }
  12879. /**
  12880. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  12881. * @param eqi Item EQI of enum equip_index
  12882. * @param *equip_index Player's equip_index[]
  12883. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  12884. * @return True if item in same inventory index, False if doesn't
  12885. */
  12886. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  12887. if (index < 0 || index >= MAX_INVENTORY)
  12888. return true;
  12889. // Dual weapon checks
  12890. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  12891. return true;
  12892. // Headgear with Mid & Low location
  12893. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  12894. return true;
  12895. // Headgear with Top & Mid or Low location
  12896. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  12897. return true;
  12898. // Headgear with Mid & Low location
  12899. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  12900. return true;
  12901. // Headgear with Top & Mid or Low location
  12902. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  12903. return true;
  12904. return false;
  12905. }
  12906. /**
  12907. * Generate Unique item ID for player
  12908. * @param sd : Player
  12909. * @return A generated Unique item ID
  12910. */
  12911. uint64 pc_generate_unique_id(map_session_data *sd) {
  12912. nullpo_ret(sd);
  12913. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  12914. }
  12915. /**
  12916. * Validating skill from player after logged on
  12917. * @param sd
  12918. **/
  12919. void pc_validate_skill(map_session_data *sd) {
  12920. if (sd) {
  12921. uint16 i = 0, count = 0;
  12922. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  12923. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  12924. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  12925. for (i = 0; i < MAX_SKILL; i++) {
  12926. uint16 idx = 0;
  12927. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  12928. continue;
  12929. if ((idx = skill_get_index(tmp_skills[i].id))) {
  12930. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  12931. count++;
  12932. }
  12933. else
  12934. 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);
  12935. }
  12936. }
  12937. }
  12938. /**
  12939. * Show available NPC Quest / Event Icon Check [Kisuka]
  12940. * @param sd Player
  12941. **/
  12942. void pc_show_questinfo(map_session_data *sd) {
  12943. #if PACKETVER >= 20090218
  12944. nullpo_retv(sd);
  12945. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12946. return;
  12947. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12948. nullpo_retv(mapdata);
  12949. if (mapdata->qi_npc.empty())
  12950. return;
  12951. if (mapdata->qi_npc.size() != sd->qi_display.size())
  12952. return; // init was not called yet
  12953. for (int i = 0; i < mapdata->qi_npc.size(); i++) {
  12954. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  12955. if (!nd || nd->qi_data.empty())
  12956. continue;
  12957. bool show = false;
  12958. for (auto &qi : nd->qi_data) {
  12959. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  12960. show = true;
  12961. // Check if need to be displayed
  12962. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  12963. sd->qi_display[i].is_active = true;
  12964. sd->qi_display[i].icon = qi->icon;
  12965. sd->qi_display[i].color = qi->color;
  12966. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  12967. }
  12968. break;
  12969. }
  12970. }
  12971. if (show == false) {
  12972. // Check if need to be hide
  12973. if (sd->qi_display[i].is_active) {
  12974. sd->qi_display[i].is_active = false;
  12975. sd->qi_display[i].icon = QTYPE_NONE;
  12976. sd->qi_display[i].color = QMARK_NONE;
  12977. #if PACKETVER >= 20120410
  12978. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  12979. #else
  12980. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  12981. #endif
  12982. }
  12983. }
  12984. }
  12985. #endif
  12986. }
  12987. /**
  12988. * Reinit the questinfo for player when changing map
  12989. * @param sd Player
  12990. **/
  12991. void pc_show_questinfo_reinit(map_session_data *sd) {
  12992. #if PACKETVER >= 20090218
  12993. nullpo_retv(sd);
  12994. sd->qi_display.clear();
  12995. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12996. return;
  12997. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12998. nullpo_retv(mapdata);
  12999. if (mapdata->qi_npc.empty())
  13000. return;
  13001. sd->qi_display.reserve( mapdata->qi_npc.size() );
  13002. for( int i = 0; i < mapdata->qi_npc.size(); i++ ){
  13003. sd->qi_display.push_back( s_qi_display() );
  13004. }
  13005. #endif
  13006. }
  13007. /**
  13008. * Check if a job is allowed to enter the map
  13009. * @param jobid Job ID see enum e_job or sd->status.class_
  13010. * @param m ID -an index- for direct indexing map[] array
  13011. * @return 1 if job is allowed, 0 otherwise
  13012. **/
  13013. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  13014. // Map is other map server.
  13015. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  13016. if (m < 0)
  13017. return true;
  13018. struct map_data *mapdata = map_getmapdata(m);
  13019. if (!mapdata->cell)
  13020. return false;
  13021. if (!pcdb_checkid(jobid))
  13022. return false;
  13023. std::shared_ptr<s_job_info> job = job_db.find(jobid);
  13024. if (!job->noenter_map.zone || group_lv > job->noenter_map.group_lv)
  13025. return true;
  13026. if ((job->noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  13027. (job->noenter_map.zone&2 && mapdata->getMapFlag(MF_PVP)) || // PVP
  13028. (job->noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  13029. (job->noenter_map.zone&8 && mapdata->getMapFlag(MF_BATTLEGROUND)) || // Battleground
  13030. (job->noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  13031. (job->noenter_map.zone&(mapdata->zone) && mapdata->getMapFlag(MF_RESTRICTED)) // Zone restriction
  13032. )
  13033. return false;
  13034. return true;
  13035. }
  13036. /**
  13037. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  13038. * @param sd
  13039. **/
  13040. void pc_set_costume_view(map_session_data *sd) {
  13041. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  13042. struct item_data *id = NULL;
  13043. nullpo_retv(sd);
  13044. head_low = sd->status.head_bottom;
  13045. head_mid = sd->status.head_mid;
  13046. head_top = sd->status.head_top;
  13047. robe = sd->status.robe;
  13048. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  13049. //Added check to prevent sending the same look on multiple slots ->
  13050. //causes client to redraw item on top of itself. (suggested by Lupus)
  13051. // Normal headgear checks
  13052. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  13053. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  13054. sd->status.head_bottom = id->look;
  13055. else
  13056. sd->status.head_bottom = 0;
  13057. }
  13058. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  13059. if (!(id->equip&(EQP_HEAD_TOP)))
  13060. sd->status.head_mid = id->look;
  13061. else
  13062. sd->status.head_mid = 0;
  13063. }
  13064. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  13065. sd->status.head_top = id->look;
  13066. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  13067. sd->status.robe = id->look;
  13068. // Costumes check
  13069. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  13070. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  13071. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  13072. sd->status.head_bottom = id->look;
  13073. else
  13074. sd->status.head_bottom = 0;
  13075. }
  13076. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  13077. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  13078. sd->status.head_mid = id->look;
  13079. else
  13080. sd->status.head_mid = 0;
  13081. }
  13082. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  13083. sd->status.head_top = id->look;
  13084. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  13085. sd->status.robe = id->look;
  13086. }
  13087. if (sd->setlook_head_bottom)
  13088. sd->status.head_bottom = sd->setlook_head_bottom;
  13089. if (sd->setlook_head_mid)
  13090. sd->status.head_mid = sd->setlook_head_mid;
  13091. if (sd->setlook_head_top)
  13092. sd->status.head_top = sd->setlook_head_top;
  13093. if (sd->setlook_robe)
  13094. sd->status.robe = sd->setlook_robe;
  13095. if (head_low != sd->status.head_bottom)
  13096. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  13097. if (head_mid != sd->status.head_mid)
  13098. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  13099. if (head_top != sd->status.head_top)
  13100. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  13101. if (robe != sd->status.robe)
  13102. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  13103. }
  13104. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  13105. uint32 date = date_get(DT_YYYYMMDD);
  13106. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  13107. std::shared_ptr<s_attendance_period> period = pair.second;
  13108. if( period->start <= date && period->end >= date ){
  13109. return period;
  13110. }
  13111. }
  13112. return nullptr;
  13113. }
  13114. bool pc_attendance_enabled(){
  13115. // Check if the attendance feature is disabled
  13116. if( !battle_config.feature_attendance ){
  13117. return false;
  13118. }
  13119. // Check if there is a running attendance period
  13120. return pc_attendance_period() != nullptr;
  13121. }
  13122. static inline bool pc_attendance_rewarded_today( map_session_data* sd ){
  13123. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  13124. }
  13125. int32 pc_attendance_counter( map_session_data* sd ){
  13126. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13127. // No running attendance period
  13128. if( period == nullptr ){
  13129. return 0;
  13130. }
  13131. // Get the counter for the current period
  13132. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13133. // Check if we have a remaining counter from a previous period
  13134. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  13135. // Reset the counter to zero
  13136. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  13137. return 0;
  13138. }
  13139. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  13140. }
  13141. void pc_attendance_claim_reward( map_session_data* sd ){
  13142. // If the user's group does not have the permission
  13143. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  13144. return;
  13145. }
  13146. // Check if the attendance feature is disabled
  13147. if( !pc_attendance_enabled() ){
  13148. return;
  13149. }
  13150. // Check if the user already got his reward today
  13151. if( pc_attendance_rewarded_today( sd ) ){
  13152. return;
  13153. }
  13154. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13155. attendance_counter += 1;
  13156. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13157. if( period == nullptr ){
  13158. return;
  13159. }
  13160. if( period->rewards.size() < attendance_counter ){
  13161. return;
  13162. }
  13163. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  13164. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  13165. if( save_settings&CHARSAVE_ATTENDANCE )
  13166. chrif_save(sd, CSAVE_NORMAL);
  13167. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  13168. struct mail_message msg;
  13169. memset( &msg, 0, sizeof( struct mail_message ) );
  13170. msg.dest_id = sd->status.char_id;
  13171. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  13172. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  13173. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  13174. msg.item[0].nameid = reward->item_id;
  13175. msg.item[0].amount = reward->amount;
  13176. msg.item[0].identify = 1;
  13177. msg.status = MAIL_NEW;
  13178. msg.type = MAIL_INBOX_NORMAL;
  13179. msg.timestamp = time(NULL);
  13180. intif_Mail_send(0, &msg);
  13181. clif_attendence_response( sd, attendance_counter );
  13182. }
  13183. /**
  13184. * Send a player to jail and determine the location to send in jail.
  13185. * @param sd: Player data
  13186. * @param duration: Duration in minutes (default INT_MAX = infinite)
  13187. */
  13188. void pc_jail(map_session_data &sd, int32 duration) {
  13189. uint16 m_index = mapindex_name2id(MAP_JAIL);
  13190. int16 x, y;
  13191. switch (rnd() % 2) { // Jail Locations
  13192. case 0: // Jail #1
  13193. x = 49;
  13194. y = 75;
  13195. break;
  13196. default: // Default Jail
  13197. x = 24;
  13198. y = 75;
  13199. break;
  13200. }
  13201. duration = i32max(0, duration); // Can't be less than 0 seconds.
  13202. // If duration > 0 then triggered via jailfor which checks every minute.
  13203. // If duration == INT_MAX then triggered via jail for infinite duration.
  13204. // If duration == 0 then triggered via unjail and end status.
  13205. if (duration > 0)
  13206. sc_start4(nullptr, &sd.bl, SC_JAILED, 100, duration, m_index, x, y, 60000);
  13207. else
  13208. status_change_end(&sd.bl, SC_JAILED);
  13209. }
  13210. /**
  13211. * Determine the punishment type when failing macro checks.
  13212. * @param sd: Player data
  13213. * @param stype: Macro detection status type (for banning)
  13214. */
  13215. static void pc_macro_punishment(map_session_data &sd, e_macro_detect_status stype) {
  13216. int32 duration = 0;
  13217. // Determine if there's a unique duration
  13218. if (battle_config.macro_detection_punishment_time > 0) {
  13219. char time[13];
  13220. safesnprintf(time, 13, "+%dnm", battle_config.macro_detection_punishment_time);
  13221. duration = static_cast<int32>(solve_time(time));
  13222. }
  13223. // Delete the timer
  13224. if (sd.macro_detect.timer != INVALID_TIMER)
  13225. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13226. // Clear the macro detect data
  13227. sd.macro_detect = {};
  13228. sd.macro_detect.timer = INVALID_TIMER;
  13229. // Unblock all actions for the player
  13230. sd.state.block_action &= ~PCBLOCK_ALL;
  13231. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13232. if (battle_config.macro_detection_punishment == 0) { // Ban
  13233. clif_macro_detector_status(sd, stype);
  13234. 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);
  13235. } else { // Jail
  13236. // Send success to close the window without closing the client
  13237. clif_macro_detector_status(sd, MCD_GOOD);
  13238. pc_jail(sd, (duration == 0 ? INT_MAX : duration / 60));
  13239. }
  13240. }
  13241. /**
  13242. * Save a captcha image to memory via /macro_register.
  13243. * @param sd: Player data
  13244. * @param image_size: Captcha image size
  13245. * @param captcha_answer: Answer to captcha
  13246. */
  13247. void pc_macro_captcha_register(map_session_data &sd, uint16 image_size, char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13248. nullpo_retv(captcha_answer);
  13249. sd.captcha_upload.cd = nullptr;
  13250. sd.captcha_upload.upload_size = 0;
  13251. if (strlen(captcha_answer) < 4 || image_size == 0 || image_size > CAPTCHA_BMP_SIZE) {
  13252. clif_captcha_upload_request(sd); // Notify client of failure.
  13253. return;
  13254. }
  13255. std::shared_ptr<s_captcha_data> cd = std::make_shared<s_captcha_data>();
  13256. sd.captcha_upload.cd = cd;
  13257. cd->image_size = image_size;
  13258. safestrncpy(cd->captcha_answer, captcha_answer, sizeof(cd->captcha_answer));
  13259. memset(cd->image_data, 0, sizeof(cd->image_data));
  13260. // Request the image data from the client.
  13261. clif_captcha_upload_request(sd);
  13262. }
  13263. /**
  13264. * Save captcha image to server.
  13265. * @param sd: Player data
  13266. * @param captcha_key: Captcha ID
  13267. * @param upload_size: Captcha size
  13268. * @param upload_data: Image data
  13269. */
  13270. void pc_macro_captcha_register_upload(map_session_data &sd, uint16 upload_size, char *upload_data) {
  13271. nullpo_retv(upload_data);
  13272. memcpy(&sd.captcha_upload.cd->image_data[sd.captcha_upload.upload_size], upload_data, upload_size);
  13273. sd.captcha_upload.upload_size += upload_size;
  13274. // Notify that the image finished uploading.
  13275. if (sd.captcha_upload.upload_size == sd.captcha_upload.cd->image_size) {
  13276. // Tell the client that the upload was finished
  13277. clif_captcha_upload_end(sd);
  13278. // Look for a free key
  13279. uint16 index;
  13280. for (index = 0; index < UINT16_MAX; index++) {
  13281. if (!captcha_db.exists(index)) {
  13282. break;
  13283. }
  13284. }
  13285. if (index == UINT16_MAX) {
  13286. // no free key found...
  13287. sd.captcha_upload.cd = nullptr;
  13288. sd.captcha_upload.upload_size = 0;
  13289. return;
  13290. }
  13291. captcha_db.put(index, sd.captcha_upload.cd);
  13292. sd.captcha_upload.cd = nullptr;
  13293. sd.captcha_upload.upload_size = 0;
  13294. // TODO: write YAML and BMP file?
  13295. }
  13296. }
  13297. /**
  13298. * Timer attached to target player with attempts to confirm captcha.
  13299. */
  13300. TIMER_FUNC(pc_macro_detector_timeout) {
  13301. map_session_data *sd = map_id2sd(id);
  13302. nullpo_ret(sd);
  13303. // Remove the current timer
  13304. sd->macro_detect.timer = INVALID_TIMER;
  13305. // Deduct an answering attempt
  13306. sd->macro_detect.retry -= 1;
  13307. if (sd->macro_detect.retry == 0) {
  13308. // All attempts have been exhausted, punish the user
  13309. pc_macro_punishment(*sd, MCD_TIMEOUT);
  13310. } else {
  13311. // Update the client
  13312. clif_macro_detector_request_show(*sd);
  13313. // Start a new timer
  13314. sd->macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd->bl.id, 0);
  13315. }
  13316. return 0;
  13317. }
  13318. /**
  13319. * Check player's captcha answer.
  13320. * @param sd: Player data
  13321. * @param captcha_answer: Captcha answer entered by player
  13322. */
  13323. void pc_macro_detector_process_answer(map_session_data &sd, char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13324. nullpo_retv(captcha_answer);
  13325. const std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  13326. // Has no captcha request
  13327. if (cd == nullptr) {
  13328. return;
  13329. }
  13330. // Correct answer
  13331. if (strcmp(captcha_answer, cd->captcha_answer) == 0) {
  13332. // Delete the timer
  13333. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13334. // Clear the macro detect data
  13335. sd.macro_detect = {};
  13336. sd.macro_detect.timer = INVALID_TIMER;
  13337. // Unblock all actions for the player
  13338. sd.state.block_action &= ~PCBLOCK_ALL;
  13339. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13340. // Assign temporary macro variable to check failures
  13341. pc_setreg(&sd, add_str("@captcha_retries"), battle_config.macro_detection_retry - sd.macro_detect.retry);
  13342. // Grant bonuses via script
  13343. run_script(cd->bonus_script, 0, sd.bl.id, fake_nd->bl.id);
  13344. // Notify the client
  13345. clif_macro_detector_status(sd, MCD_GOOD);
  13346. } else {
  13347. // Deduct an answering attempt
  13348. sd.macro_detect.retry -= 1;
  13349. // All attempts have been exhausted, punish the user
  13350. if (sd.macro_detect.retry <= 0) {
  13351. pc_macro_punishment(sd, MCD_INCORRECT);
  13352. return;
  13353. }
  13354. // Incorrect response, update the client
  13355. clif_macro_detector_request_show(sd);
  13356. // Reset the timer
  13357. addtick_timer(sd.macro_detect.timer, gettick() + battle_config.macro_detection_timeout);
  13358. }
  13359. }
  13360. /**
  13361. * Determine if a player tries to log out during a captcha check.
  13362. * @param sd: Player data
  13363. */
  13364. void pc_macro_detector_disconnect(map_session_data &sd) {
  13365. // Delete the timeout timer
  13366. if (sd.macro_detect.timer != INVALID_TIMER) {
  13367. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13368. sd.macro_detect.timer = INVALID_TIMER;
  13369. }
  13370. // If the player disconnects before clearing the challenge the player is punished.
  13371. if (sd.macro_detect.retry != 0)
  13372. pc_macro_punishment(sd, MCD_TIMEOUT);
  13373. }
  13374. /**
  13375. * Save a list of players from an area select via /macro_detector.
  13376. */
  13377. int pc_macro_reporter_area_select_sub(block_list *bl, va_list ap) {
  13378. nullpo_retr(0, bl);
  13379. if (bl->type != BL_PC)
  13380. return 0;
  13381. std::vector<uint32> *aid_list = va_arg(ap, std::vector<uint32> *);
  13382. nullpo_ret(aid_list);
  13383. aid_list->push_back(bl->id);
  13384. return 0;
  13385. }
  13386. /**
  13387. * Area select via /macro_detector.
  13388. * @param sd: Player data
  13389. * @param x: X location
  13390. * @param y: Y location
  13391. * @param radius: Area
  13392. */
  13393. void pc_macro_reporter_area_select(map_session_data &sd, const int16 x, const int16 y, const int8 radius) {
  13394. std::vector<uint32> aid_list;
  13395. map_foreachinarea(pc_macro_reporter_area_select_sub, sd.bl.m, x - radius, y - radius, x + radius, y + radius, BL_PC, &aid_list);
  13396. clif_macro_reporter_select(sd, aid_list);
  13397. }
  13398. /**
  13399. * Send out captcha check to player.
  13400. * @param sd: Target player data
  13401. * @param reporter_account_id: Account ID of reporter
  13402. */
  13403. void pc_macro_reporter_process(map_session_data &sd, int32 reporter_account_id) {
  13404. if (captcha_db.empty())
  13405. return;
  13406. // Pick a random image from the database.
  13407. const std::shared_ptr<s_captcha_data> cd = captcha_db.random();
  13408. // Set macro detection data.
  13409. sd.macro_detect.cd = cd;
  13410. sd.macro_detect.reporter_aid = reporter_account_id;
  13411. sd.macro_detect.retry = battle_config.macro_detection_retry;
  13412. // Block all actions for the target player.
  13413. sd.state.block_action |= (PCBLOCK_ALL | PCBLOCK_IMMUNE);
  13414. // Open macro detect client side.
  13415. clif_macro_detector_request(sd);
  13416. // Start the timeout timer.
  13417. sd.macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd.bl.id, 0);
  13418. }
  13419. /**
  13420. * Parse a BMP image to memory.
  13421. * @param filepath: Image file location
  13422. * @param cd: Captcha data
  13423. */
  13424. bool pc_macro_read_captcha_db_loadbmp(const std::string &filepath, std::shared_ptr<s_captcha_data> cd) {
  13425. if (cd == nullptr)
  13426. return false;
  13427. FILE *fp = fopen(filepath.c_str(), "rb");
  13428. if (fp == nullptr) {
  13429. ShowError("%s: Failed to open file \"%s\"\n", __func__, filepath.c_str());
  13430. return false;
  13431. }
  13432. // Load the file data and verify magic
  13433. char bmp_data[CAPTCHA_BMP_SIZE];
  13434. if (fread(bmp_data, CAPTCHA_BMP_SIZE, 1, fp) != 1) {
  13435. ShowError("%s: Failed to read data from \"%s\"\n", __func__, filepath.c_str());
  13436. fclose(fp);
  13437. return false;
  13438. }
  13439. fclose(fp);
  13440. if (bmp_data[0] != 'B' || bmp_data[1] != 'M') {
  13441. ShowError("%s: Invalid BMP file header given at \"%s\"\n", __func__, filepath.c_str());
  13442. return false;
  13443. }
  13444. // Compress the data into the destination
  13445. unsigned long com_size = sizeof(cd->image_data);
  13446. encode_zip(cd->image_data, &com_size, bmp_data, CAPTCHA_BMP_SIZE);
  13447. cd->image_size = static_cast<int16>(com_size);
  13448. return true;
  13449. }
  13450. const std::string CaptchaDatabase::getDefaultLocation() {
  13451. return std::string(db_path) + "/captcha_db.yml";
  13452. }
  13453. /**
  13454. * Reads and parses an entry from the captcha_db.
  13455. * @param node: YAML node containing the entry.
  13456. * @return count of successfully parsed rows
  13457. */
  13458. uint64 CaptchaDatabase::parseBodyNode(const ryml::NodeRef &node) {
  13459. uint16 index;
  13460. if (!this->asUInt16(node, "Id", index))
  13461. return 0;
  13462. std::shared_ptr<s_captcha_data> cd = captcha_db.find(index);
  13463. bool exists = cd != nullptr;
  13464. if (!exists) {
  13465. if (!this->nodesExist(node, { "Filename", "Answer" }))
  13466. return 0;
  13467. cd = std::make_shared<s_captcha_data>();
  13468. cd->index = index;
  13469. }
  13470. if (this->nodeExists(node, "Filename")) {
  13471. std::string filename;
  13472. if (!this->asString(node, "Filename", filename))
  13473. return 0;
  13474. if (!pc_macro_read_captcha_db_loadbmp(filename, cd)) {
  13475. this->invalidWarning(node["Filename"], "Failed to parse BMP image, skipping...\n");
  13476. return 0;
  13477. }
  13478. }
  13479. if (this->nodeExists(node, "Answer")) {
  13480. std::string answer;
  13481. if (!this->asString(node, "Answer", answer))
  13482. return 0;
  13483. if (answer.length() < 4 || answer.length() > CAPTCHA_ANSWER_SIZE) {
  13484. this->invalidWarning(node["Answer"], "The captcha answer must be between 4~%d characters, skipping...", CAPTCHA_ANSWER_SIZE);
  13485. return 0;
  13486. }
  13487. safestrncpy(cd->captcha_answer, answer.c_str(), sizeof(cd->captcha_answer));
  13488. }
  13489. if (this->nodeExists(node, "Bonus")) {
  13490. std::string script;
  13491. if (!this->asString(node, "Bonus", script)) {
  13492. return 0;
  13493. }
  13494. if (cd->bonus_script) {
  13495. script_free_code(cd->bonus_script);
  13496. cd->bonus_script = nullptr;
  13497. }
  13498. cd->bonus_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Bonus"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  13499. } else {
  13500. if (!exists)
  13501. 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);
  13502. }
  13503. if (!exists)
  13504. captcha_db.put(index, cd);
  13505. return 1;
  13506. }
  13507. /*==========================================
  13508. * pc Init/Terminate
  13509. *------------------------------------------*/
  13510. void do_final_pc(void) {
  13511. db_destroy(itemcd_db);
  13512. do_final_pc_groups();
  13513. ers_destroy(pc_sc_display_ers);
  13514. ers_destroy(num_reg_ers);
  13515. ers_destroy(str_reg_ers);
  13516. attendance_db.clear();
  13517. reputation_db.clear();
  13518. #ifdef MAP_GENERATOR
  13519. reputationgroup_db.clear();
  13520. #endif
  13521. penalty_db.clear();
  13522. captcha_db.clear();
  13523. }
  13524. void do_init_pc(void) {
  13525. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  13526. pc_readdb();
  13527. pc_read_motd(); // Read MOTD [Valaris]
  13528. attendance_db.load();
  13529. reputation_db.load();
  13530. #ifdef MAP_GENERATOR
  13531. reputationgroup_db.load();
  13532. #endif
  13533. captcha_db.load();
  13534. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  13535. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  13536. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  13537. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  13538. add_timer_func_list(pc_autosave, "pc_autosave");
  13539. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  13540. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  13541. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  13542. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  13543. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  13544. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  13545. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  13546. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  13547. add_timer_func_list(pc_macro_detector_timeout, "pc_macro_detector_timeout");
  13548. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  13549. // 0=day, 1=night [Yor]
  13550. night_flag = battle_config.night_at_start ? 1 : 0;
  13551. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  13552. int day_duration = battle_config.day_duration;
  13553. int night_duration = battle_config.night_duration;
  13554. // add night/day timer [Yor]
  13555. add_timer_func_list(map_day_timer, "map_day_timer");
  13556. add_timer_func_list(map_night_timer, "map_night_timer");
  13557. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  13558. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  13559. }
  13560. do_init_pc_groups();
  13561. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  13562. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13563. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13564. ers_chunk_size(pc_sc_display_ers, 150);
  13565. ers_chunk_size(num_reg_ers, 300);
  13566. ers_chunk_size(str_reg_ers, 50);
  13567. }