pc.cpp 416 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pc.hpp"
  4. #include <map>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <yaml-cpp/yaml.h>
  8. #include "../common/cbasetypes.hpp"
  9. #include "../common/core.hpp" // get_svn_revision()
  10. #include "../common/database.hpp"
  11. #include "../common/ers.hpp" // ers_destroy
  12. #include "../common/malloc.hpp"
  13. #include "../common/mmo.hpp" //NAME_LENGTH
  14. #include "../common/nullpo.hpp"
  15. #include "../common/random.hpp"
  16. #include "../common/showmsg.hpp"
  17. #include "../common/socket.hpp" // session[]
  18. #include "../common/strlib.hpp" // safestrncpy()
  19. #include "../common/timer.hpp"
  20. #include "../common/utilities.hpp"
  21. #include "../common/utils.hpp"
  22. #include "achievement.hpp"
  23. #include "atcommand.hpp" // get_atcommand_level()
  24. #include "battle.hpp" // battle_config
  25. #include "battleground.hpp"
  26. #include "buyingstore.hpp" // struct s_buyingstore
  27. #include "channel.hpp"
  28. #include "chat.hpp"
  29. #include "chrif.hpp"
  30. #include "clan.hpp"
  31. #include "clif.hpp"
  32. #include "date.hpp" // is_day_of_*()
  33. #include "duel.hpp"
  34. #include "elemental.hpp"
  35. #include "guild.hpp"
  36. #include "homunculus.hpp"
  37. #include "instance.hpp"
  38. #include "intif.hpp"
  39. #include "itemdb.hpp" // MAX_ITEMGROUP
  40. #include "log.hpp"
  41. #include "map.hpp"
  42. #include "mercenary.hpp"
  43. #include "mob.hpp"
  44. #include "npc.hpp"
  45. #include "party.hpp" // party_search()
  46. #include "pc_groups.hpp"
  47. #include "pet.hpp" // pet_unlocktarget()
  48. #include "quest.hpp"
  49. #include "script.hpp" // struct script_reg, struct script_regstr
  50. #include "searchstore.hpp" // struct s_search_store_info
  51. #include "status.hpp" // OPTION_*, struct weapon_atk
  52. #include "storage.hpp"
  53. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  54. #include "vending.hpp" // struct s_vending
  55. using namespace rathena;
  56. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  57. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
  58. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  59. static unsigned int statp[MAX_LEVEL+1];
  60. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  61. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  62. #endif
  63. // h-files are for declarations, not for implementations... [Shinomori]
  64. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  65. // timer for night.day implementation
  66. int day_timer_tid = INVALID_TIMER;
  67. int night_timer_tid = INVALID_TIMER;
  68. struct eri *pc_sc_display_ers = NULL;
  69. struct eri *num_reg_ers;
  70. struct eri *str_reg_ers;
  71. int pc_expiration_tid = INVALID_TIMER;
  72. struct fame_list smith_fame_list[MAX_FAME_LIST];
  73. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  74. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  75. struct s_job_info job_info[CLASS_COUNT];
  76. const std::string AttendanceDatabase::getDefaultLocation(){
  77. return std::string(db_path) + "/attendance.yml";
  78. }
  79. /**
  80. * Reads and parses an entry from the attendance_db.
  81. * @param node: YAML node containing the entry.
  82. * @return count of successfully parsed rows
  83. */
  84. uint64 AttendanceDatabase::parseBodyNode(const YAML::Node &node){
  85. uint32 start;
  86. if( !this->asUInt32( node, "Start", start ) ){
  87. return 0;
  88. }
  89. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  90. bool exists = attendance_period != nullptr;
  91. if( !exists ){
  92. if( !this->nodeExists( node, "End" ) ){
  93. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  94. return 0;
  95. }
  96. if( !this->nodeExists( node, "Rewards" ) ){
  97. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  98. return 0;
  99. }
  100. attendance_period = std::make_shared<s_attendance_period>();
  101. attendance_period->start = start;
  102. }
  103. // If it does not exist yet, we need to check it for sure
  104. bool requiresCollisionDetection = !exists;
  105. if( this->nodeExists( node, "End" ) ){
  106. uint32 end;
  107. if( !this->asUInt32( node, "End", end ) ){
  108. return 0;
  109. }
  110. // If the period is outdated already, we do not even bother parsing
  111. if( end < date_get( DT_YYYYMMDD ) ){
  112. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  113. return 0;
  114. }
  115. if( !exists || attendance_period->end != end ){
  116. requiresCollisionDetection = true;
  117. attendance_period->end = end;
  118. }
  119. }
  120. // Collision detection
  121. if( requiresCollisionDetection ){
  122. bool collision = false;
  123. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  124. std::shared_ptr<s_attendance_period> period = pair.second;
  125. if( exists && period->start == attendance_period->start ){
  126. // Dont compare to yourself
  127. continue;
  128. }
  129. // Check if start is inside another period
  130. if( period->start <= attendance_period->start && start <= period->end ){
  131. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  132. collision = true;
  133. break;
  134. }
  135. // Check if end is inside another period
  136. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  137. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  138. collision = true;
  139. break;
  140. }
  141. }
  142. if( collision ){
  143. return 0;
  144. }
  145. }
  146. if( this->nodeExists( node, "Rewards" ) ){
  147. const YAML::Node& rewardsNode = node["Rewards"];
  148. for( const YAML::Node& rewardNode : rewardsNode ){
  149. uint32 day;
  150. if( !this->asUInt32( rewardNode, "Day", day ) ){
  151. continue;
  152. }
  153. day -= 1;
  154. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  155. bool reward_exists = reward != nullptr;
  156. if( !reward_exists ){
  157. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  158. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  159. return 0;
  160. }
  161. reward = std::make_shared<s_attendance_reward>();
  162. }
  163. if( this->nodeExists( rewardNode, "ItemId" ) ){
  164. t_itemid item_id;
  165. if( !this->asUInt32( rewardNode, "ItemId", item_id ) ){
  166. continue;
  167. }
  168. if( item_id == 0 || !itemdb_exists( item_id ) ){
  169. ShowError( "pc_attendance_load: Unknown item ID %u for day %d.\n", item_id, day + 1 );
  170. continue;
  171. }
  172. reward->item_id = item_id;
  173. }
  174. if( this->nodeExists( rewardNode, "Amount" ) ){
  175. uint16 amount;
  176. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  177. continue;
  178. }
  179. if( amount == 0 ){
  180. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  181. amount = 1;
  182. }else if( amount > MAX_AMOUNT ){
  183. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  184. amount = MAX_AMOUNT;
  185. }
  186. reward->amount = amount;
  187. }else{
  188. if( !reward_exists ){
  189. reward->amount = 1;
  190. }
  191. }
  192. if( !reward_exists ){
  193. attendance_period->rewards[day] = reward;
  194. }
  195. }
  196. bool missing_day = false;
  197. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  198. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  199. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  200. missing_day = true;
  201. break;
  202. }
  203. }
  204. if( missing_day ){
  205. return 0;
  206. }
  207. }
  208. if( !exists ){
  209. this->put( start, attendance_period );
  210. }
  211. return 1;
  212. }
  213. AttendanceDatabase attendance_db;
  214. #define MOTD_LINE_SIZE 128
  215. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  216. bool reg_load;
  217. /**
  218. * Translation table from athena equip index to aegis bitmask
  219. */
  220. unsigned int equip_bitmask[EQI_MAX] = {
  221. EQP_ACC_L, // EQI_ACC_L
  222. EQP_ACC_R, // EQI_ACC_R
  223. EQP_SHOES, // EQI_SHOES
  224. EQP_GARMENT, // EQI_GARMENT
  225. EQP_HEAD_LOW, // EQI_HEAD_LOW
  226. EQP_HEAD_MID, // EQI_HEAD_MID
  227. EQP_HEAD_TOP, // EQI_HEAD_TOP
  228. EQP_ARMOR, // EQI_ARMOR
  229. EQP_HAND_L, // EQI_HAND_L
  230. EQP_HAND_R, // EQI_HAND_R
  231. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  232. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  233. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  234. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  235. EQP_AMMO, // EQI_AMMO
  236. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  237. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  238. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  239. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  240. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  241. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  242. };
  243. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  244. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  245. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  246. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  247. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  248. };
  249. void pc_set_reg_load( bool val ){
  250. reg_load = val;
  251. }
  252. /**
  253. * Item Cool Down Delay Saving
  254. * Struct item_cd is not a member of struct map_session_data
  255. * to keep cooldowns in memory between player log-ins.
  256. * All cooldowns are reset when server is restarted.
  257. **/
  258. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  259. struct item_cd {
  260. t_tick tick[MAX_ITEMDELAYS]; //tick
  261. t_itemid nameid[MAX_ITEMDELAYS]; //item id
  262. };
  263. /**
  264. * Converts a class to its array index for CLASS_COUNT defined arrays.
  265. * Note that it does not do a validity check for speed purposes, where parsing
  266. * player input make sure to use a pcdb_checkid first!
  267. * @param class_ Job ID see enum e_job
  268. * @return Class Index
  269. */
  270. int pc_class2idx(int class_) {
  271. if (class_ >= JOB_NOVICE_HIGH)
  272. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  273. return class_;
  274. }
  275. /**
  276. * Get player's group ID
  277. * @param sd
  278. * @return Group ID
  279. */
  280. int pc_get_group_id(struct map_session_data *sd) {
  281. return sd->group_id;
  282. }
  283. /** Get player's group Level
  284. * @param sd
  285. * @return Group Level
  286. */
  287. int pc_get_group_level(struct map_session_data *sd) {
  288. return sd->group_level;
  289. }
  290. /**
  291. * Remove a player from queue after timeout
  292. * @param tid: Timer ID
  293. * @param tick: Timer
  294. * @param id: ID
  295. * @return 0 on success or 1 otherwise
  296. */
  297. static TIMER_FUNC(pc_on_expire_active)
  298. {
  299. map_session_data *sd = (map_session_data *)data;
  300. nullpo_retr(1, sd);
  301. sd->tid_queue_active = INVALID_TIMER;
  302. bg_queue_leave(sd);
  303. clif_bg_queue_entry_init(sd);
  304. return 0;
  305. }
  306. /**
  307. * Function used to set timer externally
  308. * @param sd: Player data
  309. */
  310. void pc_set_bg_queue_timer(map_session_data *sd) {
  311. nullpo_retv(sd);
  312. if (sd->tid_queue_active != INVALID_TIMER) {
  313. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  314. sd->tid_queue_active = INVALID_TIMER;
  315. }
  316. sd->tid_queue_active = add_timer(gettick() + 20000, pc_on_expire_active, 0, (intptr_t)sd);
  317. }
  318. /**
  319. * Function used to delete timer externally
  320. * @param sd: Player data
  321. */
  322. void pc_delete_bg_queue_timer(map_session_data *sd) {
  323. nullpo_retv(sd);
  324. if (sd->tid_queue_active != INVALID_TIMER) {
  325. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  326. sd->tid_queue_active = INVALID_TIMER;
  327. }
  328. }
  329. static TIMER_FUNC(pc_invincible_timer){
  330. struct map_session_data *sd;
  331. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  332. return 1;
  333. if(sd->invincible_timer != tid){
  334. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  335. return 0;
  336. }
  337. sd->invincible_timer = INVALID_TIMER;
  338. skill_unit_move(&sd->bl,tick,1);
  339. return 0;
  340. }
  341. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  342. nullpo_retv(sd);
  343. if( sd->invincible_timer != INVALID_TIMER )
  344. delete_timer(sd->invincible_timer,pc_invincible_timer);
  345. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  346. }
  347. void pc_delinvincibletimer(struct map_session_data* sd)
  348. {
  349. nullpo_retv(sd);
  350. if( sd->invincible_timer != INVALID_TIMER )
  351. {
  352. delete_timer(sd->invincible_timer,pc_invincible_timer);
  353. sd->invincible_timer = INVALID_TIMER;
  354. skill_unit_move(&sd->bl,gettick(),1);
  355. }
  356. }
  357. static TIMER_FUNC(pc_spiritball_timer){
  358. struct map_session_data *sd;
  359. int i;
  360. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  361. return 1;
  362. if( sd->spiritball <= 0 )
  363. {
  364. 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);
  365. sd->spiritball = 0;
  366. return 0;
  367. }
  368. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  369. if( i == sd->spiritball )
  370. {
  371. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  372. return 0;
  373. }
  374. sd->spiritball--;
  375. if( i != sd->spiritball )
  376. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  377. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  378. clif_spiritball(&sd->bl);
  379. return 0;
  380. }
  381. /**
  382. * Adds a spiritball to player for 'interval' ms
  383. * @param sd
  384. * @param interval
  385. * @param max
  386. */
  387. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  388. {
  389. int tid;
  390. uint8 i;
  391. nullpo_retv(sd);
  392. if(max > MAX_SPIRITBALL)
  393. max = MAX_SPIRITBALL;
  394. if(sd->spiritball < 0)
  395. sd->spiritball = 0;
  396. if( sd->spiritball && sd->spiritball >= max )
  397. {
  398. if(sd->spirit_timer[0] != INVALID_TIMER)
  399. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  400. sd->spiritball--;
  401. if( sd->spiritball != 0 )
  402. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  403. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  404. }
  405. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  406. 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);
  407. if( i != sd->spiritball )
  408. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  409. sd->spirit_timer[i] = tid;
  410. sd->spiritball++;
  411. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  412. clif_millenniumshield(&sd->bl,sd->spiritball);
  413. else
  414. clif_spiritball(&sd->bl);
  415. }
  416. /**
  417. * Removes number of spiritball from player
  418. * @param sd
  419. * @param count
  420. * @param type 1 = doesn't give client effect
  421. */
  422. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  423. {
  424. uint8 i;
  425. nullpo_retv(sd);
  426. if(sd->spiritball <= 0) {
  427. sd->spiritball = 0;
  428. return;
  429. }
  430. if(count == 0)
  431. return;
  432. if(count > sd->spiritball)
  433. count = sd->spiritball;
  434. sd->spiritball -= count;
  435. if(count > MAX_SPIRITBALL)
  436. count = MAX_SPIRITBALL;
  437. for(i=0;i<count;i++) {
  438. if(sd->spirit_timer[i] != INVALID_TIMER) {
  439. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  440. sd->spirit_timer[i] = INVALID_TIMER;
  441. }
  442. }
  443. for(i=count;i<MAX_SPIRITBALL;i++) {
  444. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  445. sd->spirit_timer[i] = INVALID_TIMER;
  446. }
  447. if(!type) {
  448. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  449. clif_millenniumshield(&sd->bl,sd->spiritball);
  450. else
  451. clif_spiritball(&sd->bl);
  452. }
  453. }
  454. /**
  455. * Adds a soulball to player
  456. * @param sd: Player data
  457. * @param max: Max amount of soulballs
  458. */
  459. int pc_addsoulball(map_session_data *sd, int max)
  460. {
  461. nullpo_ret(sd);
  462. status_change *sc = status_get_sc(&sd->bl);
  463. if (sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  464. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, 0, skill_get_time2(SP_SOULCOLLECT, 1));
  465. sd->soulball = 0;
  466. }
  467. max = min(max, MAX_SOUL_BALL);
  468. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  469. if (sd->soulball && sd->soulball >= max)
  470. sd->soulball--;
  471. sd->soulball++;
  472. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, sd->soulball, skill_get_time2(SP_SOULCOLLECT, 1));
  473. clif_soulball(sd);
  474. return 0;
  475. }
  476. /**
  477. * Removes number of soulball from player
  478. * @param sd: Player data
  479. * @param count: Amount to remove
  480. * @param type: true = doesn't give client effect
  481. */
  482. int pc_delsoulball(map_session_data *sd, int count, bool type)
  483. {
  484. nullpo_ret(sd);
  485. if (count <= 0)
  486. return 0;
  487. status_change *sc = status_get_sc(&sd->bl);
  488. if (sd->soulball <= 0 || sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  489. sd->soulball = 0;
  490. return 0;
  491. }
  492. sd->soulball -= cap_value(count, 0, sd->soulball);
  493. if (sd->soulball == 0)
  494. status_change_end(&sd->bl, SC_SOULENERGY, INVALID_TIMER);
  495. else
  496. sc->data[SC_SOULENERGY]->val1 = sd->soulball;
  497. if (!type)
  498. clif_soulball(sd);
  499. return 0;
  500. }
  501. /**
  502. * Increases a player's fame points and displays a notice to him
  503. * @param sd Player
  504. * @param count Fame point
  505. */
  506. void pc_addfame(struct map_session_data *sd,int count)
  507. {
  508. enum e_rank ranktype;
  509. nullpo_retv(sd);
  510. sd->status.fame += count;
  511. if(sd->status.fame > MAX_FAME)
  512. sd->status.fame = MAX_FAME;
  513. switch(sd->class_&MAPID_UPPERMASK){
  514. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  515. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  516. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  517. default:
  518. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  519. return;
  520. }
  521. clif_update_rankingpoint(sd,ranktype,count);
  522. chrif_updatefamelist(sd);
  523. }
  524. /**
  525. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  526. * @param sd
  527. * @param job Job use enum e_mapid
  528. * @return Rank
  529. */
  530. unsigned char pc_famerank(uint32 char_id, int job)
  531. {
  532. uint8 i;
  533. switch(job){
  534. case MAPID_BLACKSMITH: // Blacksmith
  535. for(i = 0; i < MAX_FAME_LIST; i++){
  536. if(smith_fame_list[i].id == char_id)
  537. return i + 1;
  538. }
  539. break;
  540. case MAPID_ALCHEMIST: // Alchemist
  541. for(i = 0; i < MAX_FAME_LIST; i++){
  542. if(chemist_fame_list[i].id == char_id)
  543. return i + 1;
  544. }
  545. break;
  546. case MAPID_TAEKWON: // Taekwon
  547. for(i = 0; i < MAX_FAME_LIST; i++){
  548. if(taekwon_fame_list[i].id == char_id)
  549. return i + 1;
  550. }
  551. break;
  552. }
  553. return 0;
  554. }
  555. /**
  556. * Restart player's HP & SP value
  557. * @param sd
  558. * @param type Restart type: 1 - Normal Resurection
  559. */
  560. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  561. struct status_data *status, *b_status;
  562. nullpo_retv(sd);
  563. b_status = &sd->base_status;
  564. status = &sd->battle_status;
  565. if (type&1) { //Normal resurrection
  566. status->hp = 1; //Otherwise status_heal may fail if dead.
  567. status_heal(&sd->bl, b_status->hp, 0, 1);
  568. if( status->sp < b_status->sp )
  569. status_set_sp(&sd->bl, b_status->sp, 1);
  570. } else { //Just for saving on the char-server (with values as if respawned)
  571. sd->status.hp = b_status->hp;
  572. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  573. }
  574. }
  575. /*==========================================
  576. Rental System
  577. *------------------------------------------*/
  578. /**
  579. * Ends a rental and removes the item/effect
  580. * @param tid: Tick ID
  581. * @param tick: Timer
  582. * @param id: Timer ID
  583. * @param data: Data
  584. * @return false - failure, true - success
  585. */
  586. TIMER_FUNC(pc_inventory_rental_end){
  587. struct map_session_data *sd = map_id2sd(id);
  588. if( sd == NULL )
  589. return 0;
  590. if( tid != sd->rental_timer ) {
  591. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  592. return 0;
  593. }
  594. pc_inventory_rentals(sd);
  595. return 1;
  596. }
  597. /**
  598. * Removes the rental timer from the player
  599. * @param sd: Player data
  600. */
  601. void pc_inventory_rental_clear(struct map_session_data *sd)
  602. {
  603. if( sd->rental_timer != INVALID_TIMER ) {
  604. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  605. sd->rental_timer = INVALID_TIMER;
  606. }
  607. }
  608. /**
  609. * Check for items in the player's inventory that are rental type
  610. * @param sd: Player data
  611. */
  612. void pc_inventory_rentals(struct map_session_data *sd)
  613. {
  614. int i, c = 0;
  615. unsigned int next_tick = UINT_MAX;
  616. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  617. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  618. continue; // Nothing here
  619. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  620. continue;
  621. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  622. if (sd->inventory_data[i]->unequip_script)
  623. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  624. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  625. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  626. } else {
  627. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  628. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  629. next_tick = umin(expire_tick * 1000U, next_tick);
  630. c++;
  631. }
  632. }
  633. 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
  634. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  635. else
  636. sd->rental_timer = INVALID_TIMER;
  637. }
  638. /**
  639. * Add a rental item to the player and adjusts the rental timer appropriately
  640. * @param sd: Player data
  641. * @param seconds: Rental time
  642. */
  643. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  644. {
  645. t_tick tick = seconds * 1000;
  646. if( sd == NULL )
  647. return;
  648. if( sd->rental_timer != INVALID_TIMER ) {
  649. const struct TimerData * td;
  650. td = get_timer(sd->rental_timer);
  651. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  652. pc_inventory_rental_clear(sd);
  653. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  654. }
  655. } else
  656. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  657. }
  658. /**
  659. * Check if the player can sell the current item
  660. * @param sd: map_session_data of the player
  661. * @param item: struct of the checking item
  662. * @param shoptype: NPC's sub type see enum npc_subtype
  663. * @return bool 'true' is sellable, 'false' otherwise
  664. */
  665. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  666. if (sd == NULL || item == NULL)
  667. return false;
  668. if (item->equip > 0 || item->amount < 0)
  669. return false;
  670. if (battle_config.hide_fav_sell && item->favorite)
  671. return false; //Cannot sell favs (optional config)
  672. if (!battle_config.rental_transaction && item->expire_time)
  673. return false; // Cannot Sell Rental Items
  674. if( item->equipSwitch ){
  675. return false;
  676. }
  677. if (itemdb_ishatched_egg(item))
  678. return false;
  679. switch (shoptype) {
  680. case NPCTYPE_SHOP:
  681. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  682. item->bound != BOUND_GUILD ||
  683. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  684. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  685. ))
  686. return true;
  687. break;
  688. case NPCTYPE_ITEMSHOP:
  689. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  690. item->bound != BOUND_GUILD ||
  691. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  692. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  693. ))
  694. return true;
  695. else if (!item->bound) {
  696. struct item_data *itd = itemdb_search(item->nameid);
  697. if (itd && itd->flag.trade_restriction.sell && battle_config.allow_bound_sell&ISR_SELLABLE)
  698. return true;
  699. }
  700. break;
  701. }
  702. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  703. return false;
  704. if (item->bound && !pc_can_give_bounded_items(sd))
  705. return false; // Don't allow sale of bound items
  706. return true;
  707. }
  708. /**
  709. * Determines if player can give / drop / trade / vend items
  710. */
  711. bool pc_can_give_items(struct map_session_data *sd)
  712. {
  713. return pc_has_permission(sd, PC_PERM_TRADE);
  714. }
  715. /**
  716. * Determines if player can give / drop / trade / vend bounded items
  717. */
  718. bool pc_can_give_bounded_items(struct map_session_data *sd)
  719. {
  720. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  721. }
  722. /**
  723. * Determine if an item in a player's inventory is tradeable based on several merits.
  724. * Checks for item_trade, bound, and rental restrictions.
  725. * @param sd: Player data
  726. * @param index: Item inventory index
  727. * @return True if the item can be traded or false otherwise
  728. */
  729. bool pc_can_trade_item(map_session_data *sd, int index) {
  730. if (sd && index >= 0) {
  731. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  732. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  733. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  734. );
  735. }
  736. return false;
  737. }
  738. /*==========================================
  739. * Prepares character for saving.
  740. * @param sd
  741. *------------------------------------------*/
  742. void pc_makesavestatus(struct map_session_data *sd) {
  743. nullpo_retv(sd);
  744. if(!battle_config.save_clothcolor)
  745. sd->status.clothes_color = 0;
  746. if(!battle_config.save_body_style)
  747. sd->status.body = 0;
  748. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  749. //status change load/saving. [Skotlex]
  750. #ifdef NEW_CARTS
  751. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  752. #else
  753. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  754. #endif
  755. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  756. if(pc_isdead(sd)){
  757. pc_setrestartvalue(sd, 0);
  758. } else {
  759. sd->status.hp = sd->battle_status.hp;
  760. sd->status.sp = sd->battle_status.sp;
  761. }
  762. sd->status.last_point.map = sd->mapindex;
  763. sd->status.last_point.x = sd->bl.x;
  764. sd->status.last_point.y = sd->bl.y;
  765. return;
  766. }
  767. if(pc_isdead(sd)) {
  768. pc_setrestartvalue(sd, 0);
  769. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  770. } else {
  771. sd->status.hp = sd->battle_status.hp;
  772. sd->status.sp = sd->battle_status.sp;
  773. sd->status.last_point.map = sd->mapindex;
  774. sd->status.last_point.x = sd->bl.x;
  775. sd->status.last_point.y = sd->bl.y;
  776. }
  777. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  778. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  779. if(mapdata->save.map)
  780. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  781. else
  782. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  783. }
  784. }
  785. /*==========================================
  786. * Off init ? Connection?
  787. *------------------------------------------*/
  788. void pc_setnewpc(struct map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, t_tick client_tick, int sex, int fd) {
  789. nullpo_retv(sd);
  790. sd->bl.id = account_id;
  791. sd->status.account_id = account_id;
  792. sd->status.char_id = char_id;
  793. sd->status.sex = sex;
  794. sd->login_id1 = login_id1;
  795. sd->login_id2 = 0; // at this point, we can not know the value :(
  796. sd->client_tick = client_tick;
  797. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  798. sd->bl.type = BL_PC;
  799. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  800. sd->canlog_tick = gettick();
  801. //Required to prevent homunculus copuing a base speed of 0.
  802. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  803. }
  804. /**
  805. * Get equip point for an equip
  806. * @param sd
  807. * @param id
  808. */
  809. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  810. int ep = 0;
  811. nullpo_ret(sd);
  812. nullpo_ret(id);
  813. if (!itemdb_isequip2(id))
  814. return 0; //Not equippable by players.
  815. ep = id->equip;
  816. if(id->subtype == W_DAGGER || id->subtype == W_1HSWORD || id->subtype == W_1HAXE) {
  817. 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]
  818. if (ep == EQP_WEAPON)
  819. return EQP_ARMS;
  820. if (ep == EQP_SHADOW_WEAPON)
  821. return EQP_SHADOW_ARMS;
  822. }
  823. }
  824. return ep;
  825. }
  826. /**
  827. * Get equip point for an equip
  828. * @param sd
  829. * @param n Equip index in inventory
  830. */
  831. int pc_equippoint(struct map_session_data *sd,int n){
  832. nullpo_ret(sd);
  833. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  834. }
  835. /**
  836. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  837. * @param sd : player session
  838. * @return 0 sucess, 1:invalid sd
  839. */
  840. void pc_setinventorydata(struct map_session_data *sd)
  841. {
  842. uint8 i;
  843. nullpo_retv(sd);
  844. for(i = 0; i < MAX_INVENTORY; i++) {
  845. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  846. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  847. }
  848. }
  849. /**
  850. * 'Calculates' weapon type
  851. * @param sd : Player
  852. */
  853. void pc_calcweapontype(struct map_session_data *sd)
  854. {
  855. nullpo_retv(sd);
  856. // single-hand
  857. if(sd->weapontype2 == W_FIST) {
  858. sd->status.weapon = sd->weapontype1;
  859. return;
  860. }
  861. if(sd->weapontype1 == W_FIST) {
  862. sd->status.weapon = sd->weapontype2;
  863. return;
  864. }
  865. // dual-wield
  866. sd->status.weapon = 0;
  867. switch (sd->weapontype1){
  868. case W_DAGGER:
  869. switch (sd->weapontype2) {
  870. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  871. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  872. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  873. }
  874. break;
  875. case W_1HSWORD:
  876. switch (sd->weapontype2) {
  877. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  878. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  879. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  880. }
  881. break;
  882. case W_1HAXE:
  883. switch (sd->weapontype2) {
  884. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  885. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  886. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  887. }
  888. }
  889. // unknown, default to right hand type
  890. if (!sd->status.weapon)
  891. sd->status.weapon = sd->weapontype1;
  892. }
  893. /**
  894. * Set equip index
  895. * @param sd : Player
  896. */
  897. void pc_setequipindex(struct map_session_data *sd)
  898. {
  899. uint16 i;
  900. nullpo_retv(sd);
  901. for (i = 0; i < EQI_MAX; i++){
  902. sd->equip_index[i] = -1;
  903. sd->equip_switch_index[i] = -1;
  904. }
  905. for (i = 0; i < MAX_INVENTORY; i++) {
  906. if (sd->inventory.u.items_inventory[i].nameid == 0)
  907. continue;
  908. if (sd->inventory.u.items_inventory[i].equip) {
  909. uint8 j;
  910. for (j = 0; j < EQI_MAX; j++)
  911. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  912. sd->equip_index[j] = i;
  913. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  914. if (sd->inventory_data[i])
  915. sd->weapontype1 = sd->inventory_data[i]->subtype;
  916. else
  917. sd->weapontype1 = 0;
  918. }
  919. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  920. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  921. sd->weapontype2 = sd->inventory_data[i]->subtype;
  922. else
  923. sd->weapontype2 = 0;
  924. }
  925. }
  926. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  927. for (uint8 j = 0; j < EQI_MAX; j++) {
  928. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  929. sd->equip_switch_index[j] = i;
  930. }
  931. }
  932. }
  933. }
  934. pc_calcweapontype(sd);
  935. }
  936. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  937. //{
  938. // int i;
  939. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  940. // struct item_data *data;
  941. //
  942. // //Crafted/made/hatched items.
  943. // if (itemdb_isspecial(item->card[0]))
  944. // return 1;
  945. //
  946. // /* scan for enchant armor gems */
  947. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  948. // s = MAX_SLOTS - 1;
  949. //
  950. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  951. // return( i < s ) ? 0 : 1;
  952. //}
  953. /**
  954. * Check if an item is equiped by player
  955. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  956. * @param sd : player session
  957. * @param nameid : itemid
  958. * @return 1:yes, 0:no
  959. */
  960. bool pc_isequipped(struct map_session_data *sd, t_itemid nameid)
  961. {
  962. uint8 i;
  963. for( i = 0; i < EQI_MAX; i++ )
  964. {
  965. short index = sd->equip_index[i], j;
  966. if( index < 0 )
  967. continue;
  968. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  969. continue;
  970. if( !sd->inventory_data[index] )
  971. continue;
  972. if( sd->inventory_data[index]->nameid == nameid )
  973. return true;
  974. for( j = 0; j < sd->inventory_data[index]->slots; j++ ){
  975. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  976. return true;
  977. }
  978. }
  979. return false;
  980. }
  981. /**
  982. * Check adoption rules
  983. * @param p1_sd: Player 1
  984. * @param p2_sd: Player 2
  985. * @param b_sd: Player that will be adopted
  986. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  987. * ADOPT_ALREADY_ADOPTED - Already adopted
  988. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  989. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  990. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  991. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  992. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  993. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  994. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  995. */
  996. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  997. {
  998. if( !p1_sd || !p2_sd || !b_sd )
  999. return ADOPT_CHARACTER_NOT_FOUND;
  1000. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1001. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1002. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1003. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1004. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1005. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1006. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1007. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1008. // Parents need to have their ring equipped
  1009. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1010. return ADOPT_EQUIP_RINGS;
  1011. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1012. return ADOPT_EQUIP_RINGS;
  1013. // Already adopted a baby
  1014. if( p1_sd->status.child || p2_sd->status.child ) {
  1015. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1016. return ADOPT_MORE_CHILDREN;
  1017. }
  1018. // Parents need at least lvl 70 to adopt
  1019. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1020. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1021. return ADOPT_LEVEL_70;
  1022. }
  1023. if( b_sd->status.partner_id ) {
  1024. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1025. return ADOPT_MARRIED;
  1026. }
  1027. 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 ) )
  1028. return ADOPT_NOT_NOVICE;
  1029. return ADOPT_ALLOWED;
  1030. }
  1031. /*==========================================
  1032. * Adoption Process
  1033. *------------------------------------------*/
  1034. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1035. {
  1036. int job, joblevel;
  1037. t_exp jobexp;
  1038. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1039. return false;
  1040. // Preserve current job levels and progress
  1041. joblevel = b_sd->status.job_level;
  1042. jobexp = b_sd->status.job_exp;
  1043. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1044. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1045. { // Success, proceed to configure parents and baby skills
  1046. p1_sd->status.child = b_sd->status.char_id;
  1047. p2_sd->status.child = b_sd->status.char_id;
  1048. b_sd->status.father = p1_sd->status.char_id;
  1049. b_sd->status.mother = p2_sd->status.char_id;
  1050. // Restore progress
  1051. b_sd->status.job_level = joblevel;
  1052. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1053. b_sd->status.job_exp = jobexp;
  1054. clif_updatestatus(b_sd, SP_JOBEXP);
  1055. // Baby Skills
  1056. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1057. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1058. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1059. // Parents Skills
  1060. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1061. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1062. chrif_save(p1_sd, CSAVE_NORMAL);
  1063. chrif_save(p2_sd, CSAVE_NORMAL);
  1064. chrif_save(b_sd, CSAVE_NORMAL);
  1065. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1066. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1067. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1068. return true;
  1069. }
  1070. return false; // Job Change Fail
  1071. }
  1072. static bool pc_job_can_use_item( struct map_session_data* sd, struct item_data* item ){
  1073. nullpo_retr( false, sd );
  1074. nullpo_retr( false, item );
  1075. // Calculate the required bit to check
  1076. uint64 job = 1ULL << ( sd->class_ & MAPID_BASEMASK );
  1077. size_t index;
  1078. // 2-1
  1079. if( ( sd->class_ & JOBL_2_1 ) != 0 ){
  1080. index = 1;
  1081. // 2-2
  1082. }else if( ( sd->class_ & JOBL_2_2 ) != 0 ){
  1083. index = 2;
  1084. // Basejob
  1085. }else{
  1086. index = 0;
  1087. }
  1088. return ( item->class_base[index] & job ) != 0;
  1089. }
  1090. /*==========================================
  1091. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1092. Returns:
  1093. false : Cannot use/equip
  1094. true : Can use/equip
  1095. * Credits:
  1096. [Inkfish] for first idea
  1097. [Haru] for third-classes extension
  1098. [Cydh] finishing :D
  1099. *------------------------------------------*/
  1100. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  1101. while (1) {
  1102. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  1103. break;
  1104. #ifndef RENEWAL
  1105. //allow third classes to use trans. class items
  1106. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1107. break;
  1108. //third-baby classes can use same item too
  1109. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1110. break;
  1111. //don't need to decide specific rules for third-classes?
  1112. //items for third classes can be used for all third classes
  1113. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_UPPER|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1114. break;
  1115. #else
  1116. //trans. classes (exl. third-trans.)
  1117. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1118. break;
  1119. //baby classes (exl. third-baby)
  1120. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1121. break;
  1122. //third classes (exl. third-trans. and baby-third)
  1123. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  1124. break;
  1125. //trans-third classes
  1126. if (item->class_upper&ITEMJ_THIRD_UPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  1127. break;
  1128. //third-baby classes
  1129. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  1130. break;
  1131. #endif
  1132. return false;
  1133. }
  1134. return true;
  1135. }
  1136. /*=================================================
  1137. * Checks if the player can equip the item at index n in inventory.
  1138. * @param sd
  1139. * @param n Item index in inventory
  1140. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1141. *------------------------------------------------*/
  1142. uint8 pc_isequip(struct map_session_data *sd,int n)
  1143. {
  1144. struct item_data *item;
  1145. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1146. item = sd->inventory_data[n];
  1147. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1148. return ITEM_EQUIP_ACK_OK;
  1149. if(item == NULL)
  1150. return ITEM_EQUIP_ACK_FAIL;
  1151. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1152. return ITEM_EQUIP_ACK_FAILLEVEL;
  1153. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1154. return ITEM_EQUIP_ACK_FAILLEVEL;
  1155. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1156. return ITEM_EQUIP_ACK_FAIL;
  1157. //fail to equip if item is restricted
  1158. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1159. return ITEM_EQUIP_ACK_FAIL;
  1160. if (item->equip&EQP_AMMO) {
  1161. switch (item->subtype) {
  1162. case AMMO_ARROW:
  1163. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1164. clif_msg(sd, ITEM_NEED_BOW);
  1165. return ITEM_EQUIP_ACK_FAIL;
  1166. }
  1167. break;
  1168. case AMMO_DAGGER:
  1169. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1170. return ITEM_EQUIP_ACK_FAIL;
  1171. break;
  1172. case AMMO_BULLET:
  1173. case AMMO_SHELL:
  1174. 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
  1175. #ifdef RENEWAL
  1176. && sd->status.weapon != W_GRENADE
  1177. #endif
  1178. ) {
  1179. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1180. return ITEM_EQUIP_ACK_FAIL;
  1181. }
  1182. break;
  1183. #ifndef RENEWAL
  1184. case AMMO_GRENADE:
  1185. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1186. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1187. return ITEM_EQUIP_ACK_FAIL;
  1188. }
  1189. break;
  1190. #endif
  1191. case AMMO_CANNONBALL:
  1192. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1193. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1194. return ITEM_EQUIP_ACK_FAIL;
  1195. }
  1196. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1197. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1198. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1199. return ITEM_EQUIP_ACK_FAIL;
  1200. }
  1201. break;
  1202. }
  1203. }
  1204. if (sd->sc.count) {
  1205. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1206. return ITEM_EQUIP_ACK_FAIL;
  1207. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1208. return ITEM_EQUIP_ACK_FAIL;
  1209. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1210. return ITEM_EQUIP_ACK_FAIL;
  1211. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1212. return ITEM_EQUIP_ACK_FAIL;
  1213. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1214. return ITEM_EQUIP_ACK_FAIL;
  1215. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1216. return ITEM_EQUIP_ACK_FAIL;
  1217. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1218. return ITEM_EQUIP_ACK_FAIL;
  1219. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1220. //Spirit of Super Novice equip bonuses. [Skotlex]
  1221. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1222. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1223. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  1224. switch(item->look) { //In weapons, the look determines type of weapon.
  1225. case W_DAGGER: //All level 4 - Daggers
  1226. case W_1HSWORD: //All level 4 - 1H Swords
  1227. case W_1HAXE: //All level 4 - 1H Axes
  1228. case W_MACE: //All level 4 - 1H Maces
  1229. case W_STAFF: //All level 4 - 1H Staves
  1230. case W_2HSTAFF: //All level 4 - 2H Staves
  1231. return ITEM_EQUIP_ACK_OK;
  1232. }
  1233. }
  1234. }
  1235. //Not equipable by class. [Skotlex]
  1236. if (!pc_job_can_use_item(sd,item))
  1237. return ITEM_EQUIP_ACK_FAIL;
  1238. if (!pc_isItemClass(sd, item))
  1239. return ITEM_EQUIP_ACK_FAIL;
  1240. return ITEM_EQUIP_ACK_OK;
  1241. }
  1242. /*==========================================
  1243. * No problem with the session id
  1244. * set the status that has been sent from char server
  1245. *------------------------------------------*/
  1246. bool pc_authok(struct map_session_data *sd, uint32 login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
  1247. {
  1248. int i;
  1249. t_tick tick = gettick();
  1250. uint32 ip = session[sd->fd]->client_addr;
  1251. sd->login_id2 = login_id2;
  1252. sd->group_id = group_id;
  1253. /* load user permissions */
  1254. pc_group_pc_load(sd);
  1255. memcpy(&sd->status, st, sizeof(*st));
  1256. if (st->sex != sd->status.sex) {
  1257. clif_authfail_fd(sd->fd, 0);
  1258. return false;
  1259. }
  1260. //Set the map-server used job id. [Skotlex]
  1261. i = pc_jobid2mapid(sd->status.class_);
  1262. if (i == -1) { //Invalid class?
  1263. 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);
  1264. sd->status.class_ = JOB_NOVICE;
  1265. sd->class_ = MAPID_NOVICE;
  1266. } else
  1267. sd->class_ = i;
  1268. // Checks and fixes to character status data, that are required
  1269. // in case of configuration change or stuff, which cannot be
  1270. // checked on char-server.
  1271. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1272. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1273. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1274. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1275. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1276. sd->state.connect_new = 1;
  1277. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1278. sd->invincible_timer = INVALID_TIMER;
  1279. sd->npc_timer_id = INVALID_TIMER;
  1280. sd->pvp_timer = INVALID_TIMER;
  1281. sd->expiration_tid = INVALID_TIMER;
  1282. sd->autotrade_tid = INVALID_TIMER;
  1283. sd->respawn_tid = INVALID_TIMER;
  1284. sd->tid_queue_active = INVALID_TIMER;
  1285. sd->skill_keep_using.tid = INVALID_TIMER;
  1286. sd->skill_keep_using.skill_id = 0;
  1287. sd->skill_keep_using.level = 0;
  1288. sd->skill_keep_using.target = 0;
  1289. #ifdef SECURE_NPCTIMEOUT
  1290. // Initialize to defaults/expected
  1291. sd->npc_idle_timer = INVALID_TIMER;
  1292. sd->npc_idle_tick = tick;
  1293. sd->npc_idle_type = NPCT_INPUT;
  1294. sd->state.ignoretimeout = false;
  1295. #endif
  1296. sd->canuseitem_tick = tick;
  1297. sd->canusecashfood_tick = tick;
  1298. sd->canequip_tick = tick;
  1299. sd->cantalk_tick = tick;
  1300. sd->canskill_tick = tick;
  1301. sd->cansendmail_tick = tick;
  1302. sd->idletime = last_tick;
  1303. for(i = 0; i < MAX_SPIRITBALL; i++)
  1304. sd->spirit_timer[i] = INVALID_TIMER;
  1305. if (battle_config.item_auto_get)
  1306. sd->state.autoloot = 10000;
  1307. if (battle_config.disp_experience)
  1308. sd->state.showexp = 1;
  1309. if (battle_config.disp_zeny)
  1310. sd->state.showzeny = 1;
  1311. #ifdef VIP_ENABLE
  1312. if (!battle_config.vip_disp_rate)
  1313. sd->vip.disableshowrate = 1;
  1314. #endif
  1315. if (!(battle_config.display_skill_fail&2))
  1316. sd->state.showdelay = 1;
  1317. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1318. memset(&sd->cart, 0, sizeof(struct s_storage));
  1319. memset(&sd->storage, 0, sizeof(struct s_storage));
  1320. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1321. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1322. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1323. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1324. sd->status.option &= ~OPTION_INVISIBLE;
  1325. }
  1326. status_change_init(&sd->bl);
  1327. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1328. unit_dataset(&sd->bl);
  1329. sd->guild_x = -1;
  1330. sd->guild_y = -1;
  1331. sd->delayed_damage = 0;
  1332. // Event Timers
  1333. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1334. sd->eventtimer[i] = INVALID_TIMER;
  1335. // Rental Timer
  1336. sd->rental_timer = INVALID_TIMER;
  1337. for( i = 0; i < 3; i++ )
  1338. sd->hate_mob[i] = -1;
  1339. sd->quest_log = NULL;
  1340. sd->num_quests = 0;
  1341. sd->avail_quests = 0;
  1342. sd->save_quest = false;
  1343. sd->count_rewarp = 0;
  1344. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1345. sd->regs.arrays = NULL;
  1346. sd->vars_dirty = false;
  1347. sd->vars_ok = false;
  1348. sd->vars_received = 0x0;
  1349. sd->qi_display = nullptr;
  1350. sd->qi_count = 0;
  1351. //warp player
  1352. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1353. ShowError ("Last_point_map %s - id %d not found (error code %d)\n", mapindex_id2name(sd->status.last_point.map), sd->status.last_point.map, i);
  1354. // try warping to a default map instead (church graveyard)
  1355. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1356. // if we fail again
  1357. clif_authfail_fd(sd->fd, 0);
  1358. return false;
  1359. }
  1360. }
  1361. clif_authok(sd);
  1362. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1363. sd->die_counter=-1;
  1364. //display login notice
  1365. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1366. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1367. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1368. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1369. sd->status.name, sd->status.account_id, sd->status.char_id,
  1370. CONVIP(ip), sd->group_id);
  1371. // Send friends list
  1372. clif_friendslist_send(sd);
  1373. if( !changing_mapservers ) {
  1374. if (battle_config.display_version == 1)
  1375. pc_show_version(sd);
  1376. // Message of the Day [Valaris]
  1377. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1378. if (battle_config.motd_type)
  1379. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1380. else
  1381. clif_displaymessage(sd->fd, motd_text[i]);
  1382. }
  1383. if (expiration_time != 0)
  1384. sd->expiration_time = expiration_time;
  1385. /**
  1386. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1387. **/
  1388. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1389. }
  1390. pc_validate_skill(sd);
  1391. /* [Ind] */
  1392. sd->sc_display = NULL;
  1393. sd->sc_display_count = 0;
  1394. // Player has not yet received the CashShop list
  1395. sd->status.cashshop_sent = false;
  1396. sd->last_addeditem_index = -1;
  1397. sd->bonus_script.head = NULL;
  1398. sd->bonus_script.count = 0;
  1399. // Initialize BG queue
  1400. sd->bg_queue_id = 0;
  1401. #if PACKETVER >= 20150513
  1402. sd->hatEffectIDs = NULL;
  1403. sd->hatEffectCount = 0;
  1404. #endif
  1405. sd->catch_target_class = PET_CATCH_FAIL;
  1406. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1407. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1408. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1409. clif_updatestatus(sd, SP_BASEEXP);
  1410. }
  1411. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1412. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1413. clif_updatestatus(sd, SP_JOBEXP);
  1414. }
  1415. // Request all registries (auth is considered completed whence they arrive)
  1416. intif_request_registry(sd,7);
  1417. return true;
  1418. }
  1419. /*==========================================
  1420. * Closes a connection because it failed to be authenticated from the char server.
  1421. *------------------------------------------*/
  1422. void pc_authfail(struct map_session_data *sd)
  1423. {
  1424. clif_authfail_fd(sd->fd, 0);
  1425. return;
  1426. }
  1427. /**
  1428. * Player register a bl as hatred
  1429. * @param sd : player session
  1430. * @param pos : hate position [0;2]
  1431. * @param bl : target bl
  1432. * @return false:failed, true:success
  1433. */
  1434. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1435. {
  1436. int class_;
  1437. if (!sd || !bl || pos < 0 || pos > 2)
  1438. return false;
  1439. if (sd->hate_mob[pos] != -1)
  1440. { //Can't change hate targets.
  1441. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1442. return false;
  1443. }
  1444. class_ = status_get_class(bl);
  1445. if (!pcdb_checkid(class_)) {
  1446. unsigned int max_hp = status_get_max_hp(bl);
  1447. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1448. return false;
  1449. if (pos != status_get_size(bl))
  1450. return false; //Wrong size
  1451. }
  1452. sd->hate_mob[pos] = class_;
  1453. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1454. clif_hate_info(sd, pos, class_, 1);
  1455. return true;
  1456. }
  1457. /*==========================================
  1458. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1459. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1460. * See intif_parse_StorageReceived() for item operations [lighta]
  1461. *------------------------------------------*/
  1462. void pc_reg_received(struct map_session_data *sd)
  1463. {
  1464. uint8 i;
  1465. sd->vars_ok = true;
  1466. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1467. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1468. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1469. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1470. if (msg_checklangtype(sd->langtype,true) < 0)
  1471. sd->langtype = 0; //invalid langtype reset to default
  1472. // Cash shop
  1473. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1474. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1475. // Cooking Exp
  1476. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1477. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1478. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1479. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1480. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1481. }
  1482. if (battle_config.feature_banking)
  1483. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1484. if (battle_config.feature_roulette) {
  1485. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1486. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1487. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1488. }
  1489. sd->roulette.prizeIdx = -1;
  1490. //SG map and mob read [Komurka]
  1491. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1492. uint16 j;
  1493. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1494. sd->feel_map[i].index = j;
  1495. sd->feel_map[i].m = map_mapindex2mapid(j);
  1496. } else {
  1497. sd->feel_map[i].index = 0;
  1498. sd->feel_map[i].m = -1;
  1499. }
  1500. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1501. }
  1502. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1503. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1504. sd->cloneskill_idx = skill_get_index(skid);
  1505. if (sd->cloneskill_idx > 0) {
  1506. sd->status.skill[sd->cloneskill_idx].id = skid;
  1507. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1508. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1509. sd->status.skill[sd->cloneskill_idx].lv = i;
  1510. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1511. }
  1512. }
  1513. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1514. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1515. sd->reproduceskill_idx = skill_get_index(skid);
  1516. if (sd->reproduceskill_idx > 0) {
  1517. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1518. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1519. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1520. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1521. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1522. }
  1523. }
  1524. //Weird... maybe registries were reloaded?
  1525. if (sd->state.active)
  1526. return;
  1527. sd->state.active = 1;
  1528. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1529. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1530. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1531. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1532. // Restore IM_CHAR instance to the player
  1533. for (const auto &instance : instances) {
  1534. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1535. sd->instance_id = instance.first;
  1536. break;
  1537. }
  1538. }
  1539. #if PACKETVER_MAIN_NUM < 20190403 || PACKETVER_RE_NUM < 20190320 || PACKETVER_ZERO_NUM < 20190410
  1540. if (sd->instance_id > 0)
  1541. instance_reqinfo(sd, sd->instance_id);
  1542. if (sd->status.party_id > 0)
  1543. party_member_joined(sd);
  1544. if (sd->status.guild_id > 0)
  1545. guild_member_joined(sd);
  1546. if (sd->status.clan_id > 0)
  1547. clan_member_joined(sd);
  1548. #endif
  1549. // pet
  1550. if (sd->status.pet_id > 0)
  1551. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1552. // Homunculus [albator]
  1553. if( sd->status.hom_id > 0 )
  1554. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1555. if( sd->status.mer_id > 0 )
  1556. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1557. if( sd->status.ele_id > 0 )
  1558. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1559. map_addiddb(&sd->bl);
  1560. map_delnickdb(sd->status.char_id, sd->status.name);
  1561. if (!chrif_auth_finished(sd))
  1562. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1563. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1564. chrif_bsdata_request(sd->status.char_id);
  1565. #ifdef VIP_ENABLE
  1566. sd->vip.time = 0;
  1567. sd->vip.enabled = 0;
  1568. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1569. #endif
  1570. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1571. intif_request_questlog(sd);
  1572. if (battle_config.feature_achievement) {
  1573. sd->achievement_data.total_score = 0;
  1574. sd->achievement_data.level = 0;
  1575. sd->achievement_data.save = false;
  1576. sd->achievement_data.count = 0;
  1577. sd->achievement_data.incompleteCount = 0;
  1578. sd->achievement_data.achievements = NULL;
  1579. intif_request_achievements(sd->status.char_id);
  1580. }
  1581. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1582. sd->state.connect_new = 1;
  1583. clif_parse_LoadEndAck(sd->fd, sd);
  1584. if( pc_isinvisible( sd ) ){
  1585. sd->vd.class_ = JT_INVISIBLE;
  1586. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1587. // decrement the number of pvp players on the map
  1588. map_getmapdata( sd->bl.m )->users_pvp--;
  1589. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1590. // unregister the player for ranking
  1591. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1592. sd->pvp_timer = INVALID_TIMER;
  1593. }
  1594. clif_changeoption( &sd->bl );
  1595. }
  1596. }
  1597. channel_autojoin(sd);
  1598. }
  1599. static int pc_calc_skillpoint(struct map_session_data* sd)
  1600. {
  1601. uint16 i, skill_point = 0;
  1602. nullpo_ret(sd);
  1603. for(i = 1; i < MAX_SKILL; i++) {
  1604. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1605. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1606. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1607. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1608. )
  1609. {
  1610. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1611. skill_point += sd->status.skill[i].lv;
  1612. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1613. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1614. }
  1615. }
  1616. }
  1617. return skill_point;
  1618. }
  1619. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1620. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1621. return false;
  1622. /**
  1623. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1624. * skill tree and since they have no icons they'll give resource errors
  1625. * Get ALL skills except npc/guild ones. [Skotlex]
  1626. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1627. **/
  1628. for (const auto &skill : skill_db) {
  1629. uint16 skill_id = skill.second->nameid;
  1630. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1631. continue;
  1632. switch (skill_id) {
  1633. case SM_SELFPROVOKE:
  1634. case AB_DUPLELIGHT_MELEE:
  1635. case AB_DUPLELIGHT_MAGIC:
  1636. case WL_CHAINLIGHTNING_ATK:
  1637. case WL_TETRAVORTEX_FIRE:
  1638. case WL_TETRAVORTEX_WATER:
  1639. case WL_TETRAVORTEX_WIND:
  1640. case WL_TETRAVORTEX_GROUND:
  1641. case WL_SUMMON_ATK_FIRE:
  1642. case WL_SUMMON_ATK_WIND:
  1643. case WL_SUMMON_ATK_WATER:
  1644. case WL_SUMMON_ATK_GROUND:
  1645. case LG_OVERBRAND_BRANDISH:
  1646. case LG_OVERBRAND_PLUSATK:
  1647. case WM_SEVERE_RAINSTORM_MELEE:
  1648. case RL_R_TRIP_PLUSATK:
  1649. case SG_DEVIL:
  1650. case MO_TRIPLEATTACK:
  1651. case RG_SNATCHER:
  1652. continue;
  1653. default:
  1654. {
  1655. uint8 lv = (uint8)skill_get_max(skill_id);
  1656. if (lv > 0) {
  1657. uint16 idx = skill_get_index(skill_id);
  1658. sd->status.skill[idx].id = skill_id;
  1659. if (addlv)
  1660. sd->status.skill[idx].lv = lv;
  1661. }
  1662. }
  1663. break;
  1664. }
  1665. }
  1666. return true;
  1667. }
  1668. /*==========================================
  1669. * Calculation of skill level.
  1670. * @param sd
  1671. *------------------------------------------*/
  1672. void pc_calc_skilltree(struct map_session_data *sd)
  1673. {
  1674. nullpo_retv(sd);
  1675. int job = pc_calc_skilltree_normalize_job(sd);
  1676. int class_ = pc_mapid2jobid(job, sd->status.sex);
  1677. if( class_ == -1 )
  1678. { //Unable to normalize job??
  1679. ShowError("pc_calc_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", job, sd->status.name, sd->status.account_id, sd->status.char_id);
  1680. return;
  1681. }
  1682. class_ = pc_class2idx(class_);
  1683. for (const auto &skill : skill_db) {
  1684. uint16 skill_id = skill.second->nameid;
  1685. uint16 idx = skill_get_index(skill_id);
  1686. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1687. sd->status.skill[idx].id = 0; //First clear skills.
  1688. /* permanent skills that must be re-checked */
  1689. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  1690. if (skill_id == 0) {
  1691. sd->status.skill[idx].id = 0;
  1692. sd->status.skill[idx].lv = 0;
  1693. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1694. continue;
  1695. }
  1696. switch (skill_id) {
  1697. case NV_TRICKDEAD:
  1698. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1699. sd->status.skill[idx].id = 0;
  1700. sd->status.skill[idx].lv = 0;
  1701. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1702. }
  1703. break;
  1704. }
  1705. }
  1706. }
  1707. for (const auto &skill : skill_db) {
  1708. uint16 idx = skill_get_index(skill.second->nameid);
  1709. // Restore original level of skills after deleting earned skills.
  1710. 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 ) {
  1711. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1712. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1713. }
  1714. }
  1715. // Removes Taekwon Ranker skill bonus
  1716. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1717. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1718. for (uint16 i = 0; i < MAX_SKILL_TREE; i++) {
  1719. uint16 sk_id = skill_tree[c_][i].skill_id;
  1720. uint16 sk_idx = 0;
  1721. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1722. continue;
  1723. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1724. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1725. continue;
  1726. sd->status.skill[sk_idx].id = 0;
  1727. }
  1728. }
  1729. }
  1730. // Grant all skills
  1731. pc_grant_allskills(sd, false);
  1732. int flag;
  1733. do {
  1734. uint16 skid = 0;
  1735. flag = 0;
  1736. for (uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++) {
  1737. bool fail = false;
  1738. uint16 sk_idx = skill_get_index(skid);
  1739. if (sd->status.skill[sk_idx].id)
  1740. continue; //Skill already known.
  1741. if (!battle_config.skillfree) {
  1742. // Checking required skills
  1743. for(uint8 j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1744. uint16 sk_need_id = skill_tree[class_][i].need[j].skill_id;
  1745. uint16 sk_need_idx = 0;
  1746. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1747. short sk_need = sk_need_id;
  1748. 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)
  1749. sk_need = 0; //Not learned.
  1750. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1751. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1752. else
  1753. sk_need = pc_checkskill(sd,sk_need_id);
  1754. if (sk_need < skill_tree[class_][i].need[j].skill_lv) {
  1755. fail = true;
  1756. break;
  1757. }
  1758. }
  1759. }
  1760. if (sd->status.base_level < skill_tree[class_][i].baselv) { //We need to get the actual class in this case
  1761. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1762. c_ = pc_class2idx(c_);
  1763. if (class_ == c_ || (class_ != c_ && sd->status.base_level < skill_tree[class_][i].baselv))
  1764. fail = true; // base level requirement wasn't satisfied
  1765. }
  1766. if (sd->status.job_level < skill_tree[class_][i].joblv) { //We need to get the actual class in this case
  1767. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1768. c_ = pc_class2idx(c_);
  1769. if (class_ == c_ || (class_ != c_ && sd->status.job_level < skill_tree[class_][i].joblv))
  1770. fail = true; // job level requirement wasn't satisfied
  1771. }
  1772. }
  1773. if (!fail) {
  1774. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1775. if (!sd->status.skill[sk_idx].lv && (
  1776. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1777. skill->inf2[INF2_ISWEDDING] ||
  1778. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1779. ))
  1780. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1781. sd->status.skill[sk_idx].id = skid;
  1782. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1783. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1784. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1785. }
  1786. flag = 1; // skill list has changed, perform another pass
  1787. }
  1788. }
  1789. } while(flag);
  1790. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1791. uint16 skid = 0;
  1792. /* Taekwon Ranker Bonus Skill Tree
  1793. ============================================
  1794. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1795. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1796. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1797. for( uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++ ) {
  1798. uint16 sk_idx = skill_get_index(skid);
  1799. if (sk_idx == 0)
  1800. continue;
  1801. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  1802. continue; //Do not include Quest/Wedding skills.
  1803. if( sd->status.skill[sk_idx].id == 0 ) {
  1804. sd->status.skill[sk_idx].id = skid;
  1805. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1806. } else if( skid != NV_BASIC )
  1807. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1808. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1809. }
  1810. }
  1811. // Enable Bard/Dancer spirit linked skills.
  1812. if (sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER) {
  1813. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  1814. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  1815. { BA_POEMBRAGI, DC_FORTUNEKISS },
  1816. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  1817. for (const auto &skill : linked_skills) {
  1818. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  1819. continue;
  1820. // Tag it as a non-savable, non-uppable, bonus skill
  1821. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  1822. }
  1823. }
  1824. }
  1825. //Checks if you can learn a new skill after having leveled up a skill.
  1826. static void pc_check_skilltree(struct map_session_data *sd)
  1827. {
  1828. int i, flag = 0;
  1829. int c = 0;
  1830. if (battle_config.skillfree)
  1831. return; //Function serves no purpose if this is set
  1832. i = pc_calc_skilltree_normalize_job(sd);
  1833. c = pc_mapid2jobid(i, sd->status.sex);
  1834. if (c == -1) { //Unable to normalize job??
  1835. ShowError("pc_check_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", i, sd->status.name, sd->status.account_id, sd->status.char_id);
  1836. return;
  1837. }
  1838. c = pc_class2idx(c);
  1839. do {
  1840. uint16 skid = 0;
  1841. flag = 0;
  1842. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1843. uint16 sk_idx = skill_get_index(skid);
  1844. bool fail = false;
  1845. uint8 j = 0;
  1846. if (sd->status.skill[sk_idx].id) //Already learned
  1847. continue;
  1848. // Checking required skills
  1849. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1850. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1851. uint16 sk_need_idx = 0;
  1852. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1853. short sk_need = sk_need_id;
  1854. 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)
  1855. sk_need = 0; //Not learned.
  1856. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1857. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1858. else
  1859. sk_need = pc_checkskill(sd,sk_need_id);
  1860. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1861. fail = true;
  1862. break;
  1863. }
  1864. }
  1865. }
  1866. if( fail )
  1867. continue;
  1868. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1869. continue;
  1870. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1871. if( !sd->status.skill[sk_idx].lv && (
  1872. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1873. skill->inf2[INF2_ISWEDDING] ||
  1874. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1875. ) )
  1876. continue; //Cannot be learned via normal means.
  1877. sd->status.skill[sk_idx].id = skid;
  1878. flag = 1;
  1879. }
  1880. } while(flag);
  1881. }
  1882. // Make sure all the skills are in the correct condition
  1883. // before persisting to the backend.. [MouseJstr]
  1884. void pc_clean_skilltree(struct map_session_data *sd)
  1885. {
  1886. uint16 i;
  1887. for (i = 0; i < MAX_SKILL; i++){
  1888. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1889. sd->status.skill[i].id = 0;
  1890. sd->status.skill[i].lv = 0;
  1891. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1892. }
  1893. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1894. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1895. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1896. }
  1897. }
  1898. }
  1899. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1900. {
  1901. int skill_point, novice_skills;
  1902. int c = sd->class_;
  1903. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1904. return c;
  1905. skill_point = pc_calc_skillpoint(sd);
  1906. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1907. // limit 1st class and above to novice job levels
  1908. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1909. {
  1910. c = MAPID_NOVICE;
  1911. }
  1912. // limit 2nd class and above to first class job levels (super novices are exempt)
  1913. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1914. {
  1915. // regenerate change_level_2nd
  1916. if (!sd->change_level_2nd)
  1917. {
  1918. if (sd->class_&JOBL_THIRD)
  1919. {
  1920. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1921. if (!sd->change_level_3rd)
  1922. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1923. else
  1924. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1925. - (sd->status.job_level - 1)
  1926. - (sd->change_level_3rd - 1)
  1927. - novice_skills;
  1928. }
  1929. else
  1930. {
  1931. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1932. - (sd->status.job_level - 1)
  1933. - novice_skills;
  1934. }
  1935. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1936. }
  1937. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1938. {
  1939. c &= MAPID_BASEMASK;
  1940. }
  1941. // limit 3rd class to 2nd class/trans job levels
  1942. else if(sd->class_&JOBL_THIRD)
  1943. {
  1944. // regenerate change_level_3rd
  1945. if (!sd->change_level_3rd)
  1946. {
  1947. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1948. - (sd->status.job_level - 1)
  1949. - (sd->change_level_2nd - 1)
  1950. - novice_skills;
  1951. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1952. }
  1953. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1954. c &= MAPID_UPPERMASK;
  1955. }
  1956. }
  1957. // restore non-limiting flags
  1958. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1959. return c;
  1960. }
  1961. /*==========================================
  1962. * Updates the weight status
  1963. *------------------------------------------
  1964. * 1: overweight 50% for pre-renewal and 70% for renewal
  1965. * 2: overweight 90%
  1966. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1967. */
  1968. void pc_updateweightstatus(struct map_session_data *sd)
  1969. {
  1970. int old_overweight;
  1971. int new_overweight;
  1972. nullpo_retv(sd);
  1973. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1974. #ifdef RENEWAL
  1975. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1976. #else
  1977. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1978. #endif
  1979. if( old_overweight == new_overweight )
  1980. return; // no change
  1981. // stop old status change
  1982. if( old_overweight == 1 )
  1983. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1984. else if( old_overweight == 2 )
  1985. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1986. // start new status change
  1987. if( new_overweight == 1 )
  1988. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1989. else if( new_overweight == 2 )
  1990. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1991. // update overweight status
  1992. sd->regen.state.overweight = new_overweight;
  1993. }
  1994. int pc_disguise(struct map_session_data *sd, int class_)
  1995. {
  1996. if (!class_ && !sd->disguise)
  1997. return 0;
  1998. if (class_ && sd->disguise == class_)
  1999. return 0;
  2000. if(pc_isinvisible(sd))
  2001. { //Character is invisible. Stealth class-change. [Skotlex]
  2002. sd->disguise = class_; //viewdata is set on uncloaking.
  2003. return 2;
  2004. }
  2005. if (sd->bl.prev != NULL) {
  2006. pc_stop_walking(sd, 0);
  2007. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  2008. }
  2009. if (!class_) {
  2010. sd->disguise = 0;
  2011. class_ = sd->status.class_;
  2012. } else
  2013. sd->disguise=class_;
  2014. status_set_viewdata(&sd->bl, class_);
  2015. clif_changeoption(&sd->bl);
  2016. if (sd->bl.prev != NULL) {
  2017. clif_spawn(&sd->bl);
  2018. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2019. { //It seems the cart info is lost on undisguise.
  2020. clif_cartlist(sd);
  2021. clif_updatestatus(sd,SP_CARTINFO);
  2022. }
  2023. if (sd->chatID) {
  2024. struct chat_data* cd;
  2025. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  2026. clif_dispchat(cd,0);
  2027. }
  2028. }
  2029. return 1;
  2030. }
  2031. /// Show error message
  2032. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2033. /// Check for valid Element, break & show error message if invalid Element
  2034. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2035. /// Check for valid Race, break & show error message if invalid Race
  2036. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2037. /// Check for valid Race2, break & show error message if invalid Race2
  2038. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2039. /// Check for valid Class, break & show error message if invalid Class
  2040. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2041. /// Check for valid Size, break & show error message if invalid Size
  2042. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2043. /// Check for valid SC, break & show error message if invalid SC
  2044. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2045. /**
  2046. * Add auto spell bonus for player while attacking/attacked
  2047. * @param spell: Spell array
  2048. * @param id: Skill to cast
  2049. * @param lv: Skill level
  2050. * @param rate: Success chance
  2051. * @param flag: Battle flag
  2052. * @param card_id: Used to prevent card stacking
  2053. */
  2054. static void pc_bonus_autospell(std::vector<s_autospell> &spell, short id, short lv, short rate, short flag, unsigned short card_id)
  2055. {
  2056. if (spell.size() == MAX_PC_BONUS) {
  2057. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2058. return;
  2059. }
  2060. if (!rate)
  2061. return;
  2062. if (!(flag&BF_RANGEMASK))
  2063. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2064. if (!(flag&BF_WEAPONMASK))
  2065. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2066. if (!(flag&BF_SKILLMASK)) {
  2067. if (flag&(BF_MAGIC | BF_MISC))
  2068. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2069. if (flag&BF_WEAPON)
  2070. flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2071. }
  2072. for (auto &it : spell) {
  2073. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.flag == flag) {
  2074. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2075. return;
  2076. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2077. return;
  2078. }
  2079. }
  2080. struct s_autospell entry = {};
  2081. if (rate < -10000 || rate > 10000)
  2082. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2083. entry.id = id;
  2084. entry.lv = lv;
  2085. entry.rate = cap_value(rate, -10000, 10000);
  2086. entry.flag = flag;
  2087. entry.card_id = card_id;
  2088. spell.push_back(entry);
  2089. }
  2090. /**
  2091. * Add auto spell bonus for player while using skills
  2092. * @param spell: Spell array
  2093. * @param src_skill: Trigger skill
  2094. * @param id: Support or target type
  2095. * @param lv: Skill level
  2096. * @param rate: Success chance
  2097. * @param card_id: Used to prevent card stacking
  2098. */
  2099. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, short src_skill, short id, short lv, short rate, unsigned short card_id)
  2100. {
  2101. if (spell.size() == MAX_PC_BONUS) {
  2102. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2103. return;
  2104. }
  2105. if (!rate)
  2106. return;
  2107. struct s_autospell entry = {};
  2108. if (rate < -10000 || rate > 10000)
  2109. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2110. entry.flag = src_skill;
  2111. entry.id = id;
  2112. entry.lv = lv;
  2113. entry.rate = cap_value(rate, -10000, 10000);
  2114. entry.card_id = card_id;
  2115. spell.push_back(entry);
  2116. }
  2117. /**
  2118. * Add inflict effect bonus for player while attacking/attacked
  2119. * @param effect: Effect array
  2120. * @param sc: SC/Effect type
  2121. * @param rate: Success chance
  2122. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2123. * @param flag: Target flag
  2124. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2125. */
  2126. static void pc_bonus_addeff(std::vector<s_addeffect> &effect, enum sc_type sc, short rate, short arrow_rate, unsigned char flag, unsigned int duration)
  2127. {
  2128. if (effect.size() == MAX_PC_BONUS) {
  2129. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2130. return;
  2131. }
  2132. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2133. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2134. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2135. flag |= ATF_TARGET; //Default target: enemy.
  2136. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2137. flag |= ATF_WEAPON; //Default type: weapon.
  2138. if (!duration)
  2139. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2140. for (auto &it : effect) {
  2141. if (it.sc == sc && it.flag == flag) {
  2142. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2143. it.arrow_rate += arrow_rate;
  2144. it.duration = umax(it.duration, duration);
  2145. return;
  2146. }
  2147. }
  2148. struct s_addeffect entry = {};
  2149. if (rate < -10000 || rate > 10000)
  2150. ShowWarning("pc_bonus_addeff: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2151. entry.sc = sc;
  2152. entry.rate = cap_value(rate, -10000, 10000);
  2153. entry.arrow_rate = arrow_rate;
  2154. entry.flag = flag;
  2155. entry.duration = duration;
  2156. effect.push_back(entry);
  2157. }
  2158. /**
  2159. * Add inflict effect bonus for player while attacking using skill
  2160. * @param effect: Effect array
  2161. * @param sc: SC/Effect type
  2162. * @param rate: Success chance
  2163. * @param skill_id: Skill to cast
  2164. * @param target: Target type
  2165. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2166. */
  2167. static void pc_bonus_addeff_onskill(std::vector<s_addeffectonskill> &effect, enum sc_type sc, short rate, short skill_id, unsigned char target, unsigned int duration)
  2168. {
  2169. if (effect.size() == MAX_PC_BONUS) {
  2170. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2171. return;
  2172. }
  2173. if (!duration)
  2174. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2175. for (auto &it : effect) {
  2176. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2177. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2178. it.duration = umax(it.duration, duration);
  2179. return;
  2180. }
  2181. }
  2182. struct s_addeffectonskill entry = {};
  2183. if (rate < -10000 || rate > 10000)
  2184. ShowWarning("pc_bonus_addeff_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2185. entry.sc = sc;
  2186. entry.rate = cap_value(rate, -10000, 10000);
  2187. entry.skill_id = skill_id;
  2188. entry.target = target;
  2189. entry.duration = duration;
  2190. effect.push_back(entry);
  2191. }
  2192. /**
  2193. * Adjust/add drop rate modifier for player
  2194. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2195. * @param nameid: item id that will be dropped
  2196. * @param group: group id
  2197. * @param class_: target class
  2198. * @param race: target race. if < 0, means monster_id
  2199. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2200. */
  2201. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int class_, short race, int rate)
  2202. {
  2203. if (!nameid && !group) {
  2204. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2205. return;
  2206. }
  2207. if (nameid && !itemdb_exists(nameid)) {
  2208. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2209. return;
  2210. }
  2211. if (group && !itemdb_group_exists(group)) {
  2212. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2213. return;
  2214. }
  2215. if (drop.size() == MAX_PC_BONUS) {
  2216. 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);
  2217. return;
  2218. }
  2219. //Apply config rate adjustment settings.
  2220. if (rate >= 0) { //Absolute drop.
  2221. if (battle_config.item_rate_adddrop != 100)
  2222. rate = rate*battle_config.item_rate_adddrop/100;
  2223. if (rate < battle_config.item_drop_adddrop_min)
  2224. rate = battle_config.item_drop_adddrop_min;
  2225. else if (rate > battle_config.item_drop_adddrop_max)
  2226. rate = battle_config.item_drop_adddrop_max;
  2227. } else { //Relative drop, max/min limits are applied at drop time.
  2228. if (battle_config.item_rate_adddrop != 100)
  2229. rate = rate*battle_config.item_rate_adddrop/100;
  2230. if (rate > -1)
  2231. rate = -1;
  2232. }
  2233. for (auto &it : drop) {
  2234. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2235. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2236. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2237. return;
  2238. }
  2239. }
  2240. struct s_add_drop entry = {};
  2241. if (rate < -10000 || rate > 10000)
  2242. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2243. entry.nameid = nameid;
  2244. entry.group = group;
  2245. entry.race = race;
  2246. entry.class_ = class_;
  2247. entry.rate = cap_value(rate, -10000, 10000);
  2248. drop.push_back(entry);
  2249. }
  2250. /**
  2251. * Add autobonus to player when attacking/attacked
  2252. * @param bonus: Bonus array
  2253. * @param script: Script to execute
  2254. * @param rate: Success chance
  2255. * @param dur: Duration
  2256. * @param flag: Battle flag/skill
  2257. * @param other_script: Secondary script to execute
  2258. * @param pos: Item equip position
  2259. * @param onskill: Skill used to trigger autobonus
  2260. * @return True on success or false otherwise
  2261. */
  2262. bool pc_addautobonus(std::vector<s_autobonus> &bonus, const char *script, short rate, unsigned int dur, uint16 flag, const char *other_script, unsigned int pos, bool onskill)
  2263. {
  2264. if (bonus.size() == MAX_PC_BONUS) {
  2265. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2266. return false;
  2267. }
  2268. if (!onskill) {
  2269. if (!(flag&BF_RANGEMASK))
  2270. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2271. if (!(flag&BF_WEAPONMASK))
  2272. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2273. if (!(flag&BF_SKILLMASK)) {
  2274. if (flag&(BF_MAGIC | BF_MISC))
  2275. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2276. if (flag&BF_WEAPON)
  2277. flag |= BF_NORMAL | BF_SKILL;
  2278. }
  2279. }
  2280. struct s_autobonus entry = {};
  2281. if (rate < -10000 || rate > 10000)
  2282. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2283. entry.rate = cap_value(rate, -10000, 10000);
  2284. entry.duration = dur;
  2285. entry.active = INVALID_TIMER;
  2286. entry.atk_type = flag;
  2287. entry.pos = pos;
  2288. entry.bonus_script = aStrdup(script);
  2289. entry.other_script = (other_script ? aStrdup(other_script) : NULL);
  2290. bonus.push_back(entry);
  2291. return true;
  2292. }
  2293. /**
  2294. * Remove an autobonus from player
  2295. * @param sd: Player data
  2296. * @param bonus: Autobonus array
  2297. * @param restore: Run script on clearing or not
  2298. */
  2299. void pc_delautobonus(struct map_session_data* sd, std::vector<s_autobonus> &bonus, bool restore)
  2300. {
  2301. nullpo_retv(sd);
  2302. std::vector<s_autobonus>::iterator it = bonus.begin();
  2303. while( it != bonus.end() ){
  2304. s_autobonus b = *it;
  2305. if (b.active != INVALID_TIMER) {
  2306. if (restore && (sd->state.autobonus&b.pos) == b.pos) {
  2307. if (b.bonus_script) {
  2308. unsigned int equip_pos_idx = 0;
  2309. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2310. for (uint8 j = 0; j < EQI_MAX; j++) {
  2311. if (sd->equip_index[j] >= 0)
  2312. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2313. }
  2314. if ((equip_pos_idx&b.pos) == b.pos)
  2315. script_run_autobonus(b.bonus_script, sd, b.pos);
  2316. }
  2317. it++;
  2318. continue;
  2319. } else { // Logout / Unequipped an item with an activated bonus
  2320. delete_timer(b.active, pc_endautobonus);
  2321. b.active = INVALID_TIMER;
  2322. }
  2323. }
  2324. if (b.bonus_script)
  2325. aFree(b.bonus_script);
  2326. if (b.other_script)
  2327. aFree(b.other_script);
  2328. it = bonus.erase(it);
  2329. }
  2330. }
  2331. /**
  2332. * Execute autobonus on player
  2333. * @param sd: Player data
  2334. * @param autobonus: Autobonus to run
  2335. */
  2336. void pc_exeautobonus(struct map_session_data *sd, std::vector<s_autobonus> *bonus, struct s_autobonus *autobonus)
  2337. {
  2338. nullpo_retv(sd);
  2339. nullpo_retv(autobonus);
  2340. if (autobonus->active != INVALID_TIMER)
  2341. delete_timer(autobonus->active, pc_endautobonus);
  2342. if( autobonus->other_script )
  2343. {
  2344. int j;
  2345. unsigned int equip_pos_idx = 0;
  2346. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2347. for(j = 0; j < EQI_MAX; j++) {
  2348. if(sd->equip_index[j] >= 0)
  2349. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2350. }
  2351. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2352. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2353. }
  2354. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)bonus);
  2355. sd->state.autobonus |= autobonus->pos;
  2356. status_calc_pc(sd,SCO_NONE);
  2357. }
  2358. /**
  2359. * Remove autobonus timer from player
  2360. */
  2361. TIMER_FUNC(pc_endautobonus){
  2362. struct map_session_data *sd = map_id2sd(id);
  2363. std::vector<s_autobonus> *bonus = (std::vector<s_autobonus> *)data;
  2364. nullpo_ret(sd);
  2365. nullpo_ret(bonus);
  2366. for( struct s_autobonus& autobonus : *bonus ){
  2367. if( autobonus.active == tid ){
  2368. autobonus.active = INVALID_TIMER;
  2369. sd->state.autobonus &= ~autobonus.pos;
  2370. break;
  2371. }
  2372. }
  2373. status_calc_pc(sd,SCO_NONE);
  2374. return 0;
  2375. }
  2376. /**
  2377. * Add element bonus to player when attacking
  2378. * @param sd: Player data
  2379. * @param ele: Element to adjust
  2380. * @param rate: Success chance
  2381. * @param flag: Battle flag
  2382. */
  2383. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2384. {
  2385. nullpo_retv(sd);
  2386. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2387. if (wd->addele2.size() == MAX_PC_BONUS) {
  2388. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2389. return;
  2390. }
  2391. if (!(flag&BF_RANGEMASK))
  2392. flag |= BF_SHORT | BF_LONG;
  2393. if (!(flag&BF_WEAPONMASK))
  2394. flag |= BF_WEAPON;
  2395. if (!(flag&BF_SKILLMASK)) {
  2396. if (flag&(BF_MAGIC | BF_MISC))
  2397. flag |= BF_SKILL;
  2398. if (flag&BF_WEAPON)
  2399. flag |= BF_NORMAL | BF_SKILL;
  2400. }
  2401. for (auto &it : wd->addele2) {
  2402. if (it.ele == ele && it.flag == flag) {
  2403. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2404. return;
  2405. }
  2406. }
  2407. struct s_addele2 entry = {};
  2408. if (rate < -10000 || rate > 10000)
  2409. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2410. entry.ele = ele;
  2411. entry.rate = cap_value(rate, -10000, 10000);
  2412. entry.flag = flag;
  2413. wd->addele2.push_back(entry);
  2414. }
  2415. /**
  2416. * Reduce element bonus to player when attacking
  2417. * @param sd: Player data
  2418. * @param ele: Element to adjust
  2419. * @param rate: Success chance
  2420. * @param flag: Battle flag
  2421. */
  2422. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2423. {
  2424. nullpo_retv(sd);
  2425. if (sd->subele2.size() == MAX_PC_BONUS) {
  2426. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2427. return;
  2428. }
  2429. if (!(flag&BF_RANGEMASK))
  2430. flag |= BF_SHORT | BF_LONG;
  2431. if (!(flag&BF_WEAPONMASK))
  2432. flag |= BF_WEAPON;
  2433. if (!(flag&BF_SKILLMASK)) {
  2434. if (flag&(BF_MAGIC | BF_MISC))
  2435. flag |= BF_SKILL;
  2436. if (flag&BF_WEAPON)
  2437. flag |= BF_NORMAL | BF_SKILL;
  2438. }
  2439. for (auto &it : sd->subele2) {
  2440. if (it.ele == ele && it.flag == flag) {
  2441. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2442. return;
  2443. }
  2444. }
  2445. struct s_addele2 entry = {};
  2446. if (rate < -10000 || rate > 10000)
  2447. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2448. entry.ele = ele;
  2449. entry.rate = cap_value(rate, -10000, 10000);
  2450. entry.flag = flag;
  2451. sd->subele2.push_back(entry);
  2452. }
  2453. /**
  2454. * Adjust race damage to target when attacking
  2455. * @param sd: Player data
  2456. * @param race: Race to adjust
  2457. * @param rate: Success chance
  2458. * @param flag: Battle flag
  2459. */
  2460. static void pc_bonus_subrace(struct map_session_data* sd, unsigned char race, short rate, short flag)
  2461. {
  2462. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2463. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2464. return;
  2465. }
  2466. if (!(flag&BF_RANGEMASK))
  2467. flag |= BF_SHORT | BF_LONG;
  2468. if (!(flag&BF_WEAPONMASK))
  2469. flag |= BF_WEAPON;
  2470. if (!(flag&BF_SKILLMASK)) {
  2471. if (flag&(BF_MAGIC | BF_MISC))
  2472. flag |= BF_SKILL;
  2473. if (flag&BF_WEAPON)
  2474. flag |= BF_NORMAL | BF_SKILL;
  2475. }
  2476. for (auto &it : sd->subrace3) {
  2477. if (it.race == race && it.flag == flag) {
  2478. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2479. return;
  2480. }
  2481. }
  2482. struct s_addrace2 entry = {};
  2483. if (rate < -10000 || rate > 10000)
  2484. ShowWarning("pc_bonus_subrace: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2485. entry.race = race;
  2486. entry.rate = cap_value(rate, -10000, 10000);
  2487. entry.flag = flag;
  2488. sd->subrace3.push_back(entry);
  2489. }
  2490. /**
  2491. * General item bonus for player
  2492. * @param bonus: Bonus array
  2493. * @param id: Key
  2494. * @param val: Value
  2495. * @param cap_rate: If Value is a rate value that needs to be capped
  2496. */
  2497. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2498. {
  2499. for (auto &it : bonus) {
  2500. if (it.id == id) {
  2501. if (cap_rate)
  2502. it.val = cap_value(it.val + val, -10000, 10000);
  2503. else
  2504. it.val += val;
  2505. return;
  2506. }
  2507. }
  2508. struct s_item_bonus entry = {};
  2509. if (cap_rate && (val < -10000 || val > 10000)) {
  2510. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2511. val = cap_value(val, -10000, 10000);
  2512. }
  2513. entry.id = id;
  2514. entry.val = val;
  2515. bonus.push_back(entry);
  2516. }
  2517. /**
  2518. * Remove HP/SP to player when attacking
  2519. * @param bonus: Bonus array
  2520. * @param rate: Success chance
  2521. * @param per: Percentage of HP/SP to vanish
  2522. * @param flag: Battle flag
  2523. */
  2524. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2525. if (bonus.size() == MAX_PC_BONUS) {
  2526. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2527. return;
  2528. }
  2529. if (!(flag&BF_RANGEMASK))
  2530. flag |= BF_SHORT | BF_LONG;
  2531. if (!(flag&BF_WEAPONMASK))
  2532. flag |= BF_WEAPON;
  2533. if (!(flag&BF_SKILLMASK)) {
  2534. if (flag&(BF_MAGIC | BF_MISC))
  2535. flag |= BF_SKILL;
  2536. if (flag&BF_WEAPON)
  2537. flag |= BF_NORMAL | BF_SKILL;
  2538. }
  2539. for (auto &it : bonus) {
  2540. if (it.flag == flag) {
  2541. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2542. it.per += per;
  2543. return;
  2544. }
  2545. }
  2546. struct s_vanish_bonus entry = {};
  2547. if (rate < -10000 || rate > 10000)
  2548. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2549. entry.rate = cap_value(rate, -10000, 10000);
  2550. entry.per = per;
  2551. entry.flag = flag;
  2552. bonus.push_back(entry);
  2553. }
  2554. /*==========================================
  2555. * Add a bonus(type) to player sd
  2556. * format: bonus bBonusName,val;
  2557. * @param sd
  2558. * @param type Bonus type used by bBonusName
  2559. * @param val Value that usually for rate or fixed value
  2560. *------------------------------------------*/
  2561. void pc_bonus(struct map_session_data *sd,int type,int val)
  2562. {
  2563. struct status_data *status;
  2564. int bonus;
  2565. nullpo_retv(sd);
  2566. status = &sd->base_status;
  2567. switch(type){
  2568. case SP_STR:
  2569. case SP_AGI:
  2570. case SP_VIT:
  2571. case SP_INT:
  2572. case SP_DEX:
  2573. case SP_LUK:
  2574. if(sd->state.lr_flag != 2)
  2575. sd->indexed_bonus.param_bonus[type-SP_STR]+=val;
  2576. break;
  2577. case SP_ATK1:
  2578. if(!sd->state.lr_flag) {
  2579. bonus = status->rhw.atk + val;
  2580. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2581. }
  2582. else if(sd->state.lr_flag == 1) {
  2583. bonus = status->lhw.atk + val;
  2584. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2585. }
  2586. break;
  2587. case SP_ATK2:
  2588. if(!sd->state.lr_flag) {
  2589. bonus = status->rhw.atk2 + val;
  2590. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2591. }
  2592. else if(sd->state.lr_flag == 1) {
  2593. bonus = status->lhw.atk2 + val;
  2594. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2595. }
  2596. break;
  2597. case SP_BASE_ATK:
  2598. if(sd->state.lr_flag != 2) {
  2599. #ifdef RENEWAL
  2600. bonus = sd->bonus.eatk + val;
  2601. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2602. #else
  2603. bonus = status->batk + val;
  2604. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2605. #endif
  2606. }
  2607. break;
  2608. case SP_DEF1:
  2609. if(sd->state.lr_flag != 2) {
  2610. bonus = status->def + val;
  2611. #ifdef RENEWAL
  2612. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2613. #else
  2614. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2615. #endif
  2616. }
  2617. break;
  2618. case SP_DEF2:
  2619. if(sd->state.lr_flag != 2) {
  2620. bonus = status->def2 + val;
  2621. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2622. }
  2623. break;
  2624. case SP_MDEF1:
  2625. if(sd->state.lr_flag != 2) {
  2626. bonus = status->mdef + val;
  2627. #ifdef RENEWAL
  2628. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2629. #else
  2630. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2631. #endif
  2632. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2633. sd->bonus.shieldmdef += bonus;
  2634. }
  2635. }
  2636. break;
  2637. case SP_MDEF2:
  2638. if(sd->state.lr_flag != 2) {
  2639. bonus = status->mdef2 + val;
  2640. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2641. }
  2642. break;
  2643. case SP_HIT:
  2644. if(sd->state.lr_flag != 2) {
  2645. bonus = status->hit + val;
  2646. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2647. } else
  2648. sd->bonus.arrow_hit+=val;
  2649. break;
  2650. case SP_FLEE1:
  2651. if(sd->state.lr_flag != 2) {
  2652. bonus = status->flee + val;
  2653. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2654. }
  2655. break;
  2656. case SP_FLEE2:
  2657. if(sd->state.lr_flag != 2) {
  2658. bonus = status->flee2 + val*10;
  2659. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2660. }
  2661. break;
  2662. case SP_CRITICAL:
  2663. if(sd->state.lr_flag != 2) {
  2664. bonus = status->cri + val*10;
  2665. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2666. } else
  2667. sd->bonus.arrow_cri += val*10;
  2668. break;
  2669. case SP_ATKELE:
  2670. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2671. switch (sd->state.lr_flag)
  2672. {
  2673. case 2:
  2674. switch (sd->status.weapon) {
  2675. case W_BOW:
  2676. case W_REVOLVER:
  2677. case W_RIFLE:
  2678. case W_GATLING:
  2679. case W_SHOTGUN:
  2680. case W_GRENADE:
  2681. //Become weapon element.
  2682. status->rhw.ele=val;
  2683. break;
  2684. default: //Become arrow element.
  2685. sd->bonus.arrow_ele=val;
  2686. break;
  2687. }
  2688. break;
  2689. case 1:
  2690. status->lhw.ele=val;
  2691. break;
  2692. default:
  2693. status->rhw.ele=val;
  2694. break;
  2695. }
  2696. break;
  2697. case SP_DEFELE:
  2698. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2699. if(sd->state.lr_flag != 2)
  2700. status->def_ele=val;
  2701. break;
  2702. case SP_MAXHP:
  2703. if(sd->state.lr_flag == 2)
  2704. break;
  2705. sd->bonus.hp += val;
  2706. break;
  2707. case SP_MAXSP:
  2708. if(sd->state.lr_flag == 2)
  2709. break;
  2710. sd->bonus.sp += val;
  2711. break;
  2712. case SP_MAXHPRATE:
  2713. if(sd->state.lr_flag != 2)
  2714. sd->hprate+=val;
  2715. break;
  2716. case SP_MAXSPRATE:
  2717. if(sd->state.lr_flag != 2)
  2718. sd->sprate+=val;
  2719. break;
  2720. case SP_SPRATE:
  2721. if(sd->state.lr_flag != 2)
  2722. sd->dsprate+=val;
  2723. break;
  2724. case SP_ATTACKRANGE:
  2725. switch (sd->state.lr_flag) {
  2726. case 2:
  2727. switch (sd->status.weapon) {
  2728. case W_BOW:
  2729. case W_REVOLVER:
  2730. case W_RIFLE:
  2731. case W_GATLING:
  2732. case W_SHOTGUN:
  2733. case W_GRENADE:
  2734. status->rhw.range += val;
  2735. }
  2736. break;
  2737. case 1:
  2738. status->lhw.range += val;
  2739. break;
  2740. default:
  2741. status->rhw.range += val;
  2742. break;
  2743. }
  2744. break;
  2745. case SP_SPEED_RATE: //Non stackable increase
  2746. if(sd->state.lr_flag != 2)
  2747. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2748. break;
  2749. case SP_SPEED_ADDRATE: //Stackable increase
  2750. if(sd->state.lr_flag != 2)
  2751. sd->bonus.speed_add_rate -= val;
  2752. break;
  2753. case SP_ASPD: //Raw increase
  2754. if(sd->state.lr_flag != 2)
  2755. sd->bonus.aspd_add -= 10*val;
  2756. break;
  2757. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2758. if(sd->state.lr_flag != 2)
  2759. #ifndef RENEWAL_ASPD
  2760. status->aspd_rate -= 10*val;
  2761. #else
  2762. status->aspd_rate2 += val;
  2763. #endif
  2764. break;
  2765. case SP_HP_RECOV_RATE:
  2766. if(sd->state.lr_flag != 2)
  2767. sd->hprecov_rate += val;
  2768. break;
  2769. case SP_SP_RECOV_RATE:
  2770. if(sd->state.lr_flag != 2)
  2771. sd->sprecov_rate += val;
  2772. break;
  2773. case SP_CRITICAL_DEF:
  2774. if(sd->state.lr_flag != 2)
  2775. sd->bonus.critical_def += val;
  2776. break;
  2777. case SP_NEAR_ATK_DEF:
  2778. if(sd->state.lr_flag != 2)
  2779. sd->bonus.near_attack_def_rate += val;
  2780. break;
  2781. case SP_LONG_ATK_DEF:
  2782. if(sd->state.lr_flag != 2)
  2783. sd->bonus.long_attack_def_rate += val;
  2784. break;
  2785. case SP_DOUBLE_RATE:
  2786. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2787. sd->bonus.double_rate = val;
  2788. break;
  2789. case SP_DOUBLE_ADD_RATE:
  2790. if(sd->state.lr_flag == 0)
  2791. sd->bonus.double_add_rate += val;
  2792. break;
  2793. case SP_MATK_RATE:
  2794. if(sd->state.lr_flag != 2)
  2795. sd->matk_rate += val;
  2796. break;
  2797. case SP_IGNORE_DEF_ELE:
  2798. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2799. if(!sd->state.lr_flag)
  2800. sd->right_weapon.ignore_def_ele |= 1<<val;
  2801. else if(sd->state.lr_flag == 1)
  2802. sd->left_weapon.ignore_def_ele |= 1<<val;
  2803. break;
  2804. case SP_IGNORE_DEF_RACE:
  2805. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2806. if(!sd->state.lr_flag)
  2807. sd->right_weapon.ignore_def_race |= 1<<val;
  2808. else if(sd->state.lr_flag == 1)
  2809. sd->left_weapon.ignore_def_race |= 1<<val;
  2810. break;
  2811. case SP_IGNORE_DEF_CLASS:
  2812. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2813. if(!sd->state.lr_flag)
  2814. sd->right_weapon.ignore_def_class |= 1<<val;
  2815. else if(sd->state.lr_flag == 1)
  2816. sd->left_weapon.ignore_def_class |= 1<<val;
  2817. break;
  2818. case SP_ATK_RATE:
  2819. if(sd->state.lr_flag != 2)
  2820. sd->bonus.atk_rate += val;
  2821. break;
  2822. case SP_MAGIC_ATK_DEF:
  2823. if(sd->state.lr_flag != 2)
  2824. sd->bonus.magic_def_rate += val;
  2825. break;
  2826. case SP_MISC_ATK_DEF:
  2827. if(sd->state.lr_flag != 2)
  2828. sd->bonus.misc_def_rate += val;
  2829. break;
  2830. case SP_IGNORE_MDEF_ELE:
  2831. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2832. if(sd->state.lr_flag != 2)
  2833. sd->bonus.ignore_mdef_ele |= 1<<val;
  2834. break;
  2835. case SP_IGNORE_MDEF_RACE:
  2836. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2837. if(sd->state.lr_flag != 2)
  2838. sd->bonus.ignore_mdef_race |= 1<<val;
  2839. break;
  2840. case SP_PERFECT_HIT_RATE:
  2841. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2842. sd->bonus.perfect_hit = val;
  2843. break;
  2844. case SP_PERFECT_HIT_ADD_RATE:
  2845. if(sd->state.lr_flag != 2)
  2846. sd->bonus.perfect_hit_add += val;
  2847. break;
  2848. case SP_CRITICAL_RATE:
  2849. if(sd->state.lr_flag != 2)
  2850. sd->critical_rate+=val;
  2851. break;
  2852. case SP_DEF_RATIO_ATK_ELE:
  2853. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2854. if(!sd->state.lr_flag)
  2855. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2856. else if(sd->state.lr_flag == 1)
  2857. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2858. break;
  2859. case SP_DEF_RATIO_ATK_RACE:
  2860. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2861. if(!sd->state.lr_flag)
  2862. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2863. else if(sd->state.lr_flag == 1)
  2864. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2865. break;
  2866. case SP_DEF_RATIO_ATK_CLASS:
  2867. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2868. if(!sd->state.lr_flag)
  2869. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2870. else if(sd->state.lr_flag == 1)
  2871. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2872. break;
  2873. case SP_HIT_RATE:
  2874. if(sd->state.lr_flag != 2)
  2875. sd->hit_rate += val;
  2876. break;
  2877. case SP_FLEE_RATE:
  2878. if(sd->state.lr_flag != 2)
  2879. sd->flee_rate += val;
  2880. break;
  2881. case SP_FLEE2_RATE:
  2882. if(sd->state.lr_flag != 2)
  2883. sd->flee2_rate += val;
  2884. break;
  2885. case SP_DEF_RATE:
  2886. if(sd->state.lr_flag != 2)
  2887. sd->def_rate += val;
  2888. break;
  2889. case SP_DEF2_RATE:
  2890. if(sd->state.lr_flag != 2)
  2891. sd->def2_rate += val;
  2892. break;
  2893. case SP_MDEF_RATE:
  2894. if(sd->state.lr_flag != 2)
  2895. sd->mdef_rate += val;
  2896. break;
  2897. case SP_MDEF2_RATE:
  2898. if(sd->state.lr_flag != 2)
  2899. sd->mdef2_rate += val;
  2900. break;
  2901. case SP_RESTART_FULL_RECOVER:
  2902. if(sd->state.lr_flag != 2)
  2903. sd->special_state.restart_full_recover = 1;
  2904. break;
  2905. case SP_NO_CASTCANCEL:
  2906. if(sd->state.lr_flag != 2)
  2907. sd->special_state.no_castcancel = 1;
  2908. break;
  2909. case SP_NO_CASTCANCEL2:
  2910. if(sd->state.lr_flag != 2)
  2911. sd->special_state.no_castcancel2 = 1;
  2912. break;
  2913. case SP_NO_SIZEFIX:
  2914. if(sd->state.lr_flag != 2)
  2915. sd->special_state.no_sizefix = 1;
  2916. break;
  2917. case SP_NO_MAGIC_DAMAGE:
  2918. if(sd->state.lr_flag == 2)
  2919. break;
  2920. val+= sd->special_state.no_magic_damage;
  2921. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2922. break;
  2923. case SP_NO_WEAPON_DAMAGE:
  2924. if(sd->state.lr_flag == 2)
  2925. break;
  2926. val+= sd->special_state.no_weapon_damage;
  2927. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2928. break;
  2929. case SP_NO_MISC_DAMAGE:
  2930. if(sd->state.lr_flag == 2)
  2931. break;
  2932. val+= sd->special_state.no_misc_damage;
  2933. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2934. break;
  2935. case SP_NO_GEMSTONE:
  2936. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2937. sd->special_state.no_gemstone = 1;
  2938. break;
  2939. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2940. if(sd->state.lr_flag != 2) {
  2941. sd->special_state.intravision = 1;
  2942. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2943. }
  2944. break;
  2945. case SP_NO_KNOCKBACK:
  2946. if(sd->state.lr_flag != 2)
  2947. sd->special_state.no_knockback = 1;
  2948. break;
  2949. case SP_SPLASH_RANGE:
  2950. if(sd->bonus.splash_range < val)
  2951. sd->bonus.splash_range = val;
  2952. break;
  2953. case SP_SPLASH_ADD_RANGE:
  2954. sd->bonus.splash_add_range += val;
  2955. break;
  2956. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2957. if(sd->state.lr_flag != 2)
  2958. sd->bonus.short_weapon_damage_return += val;
  2959. break;
  2960. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2961. if(sd->state.lr_flag != 2)
  2962. sd->bonus.long_weapon_damage_return += val;
  2963. break;
  2964. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2965. if(sd->state.lr_flag != 2)
  2966. sd->bonus.magic_damage_return += val;
  2967. break;
  2968. case SP_ALL_STATS: // [Valaris]
  2969. if(sd->state.lr_flag!=2) {
  2970. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  2971. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  2972. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  2973. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  2974. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  2975. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  2976. }
  2977. break;
  2978. case SP_AGI_VIT: // [Valaris]
  2979. if(sd->state.lr_flag!=2) {
  2980. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  2981. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  2982. }
  2983. break;
  2984. case SP_AGI_DEX_STR: // [Valaris]
  2985. if(sd->state.lr_flag!=2) {
  2986. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  2987. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  2988. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  2989. }
  2990. break;
  2991. case SP_PERFECT_HIDE: // [Valaris]
  2992. if(sd->state.lr_flag!=2)
  2993. sd->special_state.perfect_hiding=1;
  2994. break;
  2995. case SP_UNBREAKABLE:
  2996. if(sd->state.lr_flag!=2)
  2997. sd->bonus.unbreakable += val;
  2998. break;
  2999. case SP_UNBREAKABLE_WEAPON:
  3000. if(sd->state.lr_flag != 2)
  3001. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3002. break;
  3003. case SP_UNBREAKABLE_ARMOR:
  3004. if(sd->state.lr_flag != 2)
  3005. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3006. break;
  3007. case SP_UNBREAKABLE_HELM:
  3008. if(sd->state.lr_flag != 2)
  3009. sd->bonus.unbreakable_equip |= EQP_HELM;
  3010. break;
  3011. case SP_UNBREAKABLE_SHIELD:
  3012. if(sd->state.lr_flag != 2)
  3013. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3014. break;
  3015. case SP_UNBREAKABLE_GARMENT:
  3016. if(sd->state.lr_flag != 2)
  3017. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3018. break;
  3019. case SP_UNBREAKABLE_SHOES:
  3020. if(sd->state.lr_flag != 2)
  3021. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3022. break;
  3023. case SP_CLASSCHANGE: // [Valaris]
  3024. if(sd->state.lr_flag !=2)
  3025. sd->bonus.classchange=val;
  3026. break;
  3027. case SP_SHORT_ATK_RATE:
  3028. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3029. sd->bonus.short_attack_atk_rate+=val;
  3030. break;
  3031. case SP_LONG_ATK_RATE:
  3032. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3033. sd->bonus.long_attack_atk_rate+=val;
  3034. break;
  3035. case SP_BREAK_WEAPON_RATE:
  3036. if(sd->state.lr_flag != 2)
  3037. sd->bonus.break_weapon_rate+=val;
  3038. break;
  3039. case SP_BREAK_ARMOR_RATE:
  3040. if(sd->state.lr_flag != 2)
  3041. sd->bonus.break_armor_rate+=val;
  3042. break;
  3043. case SP_ADD_STEAL_RATE:
  3044. if(sd->state.lr_flag != 2)
  3045. sd->bonus.add_steal_rate+=val;
  3046. break;
  3047. case SP_DELAYRATE:
  3048. if(sd->state.lr_flag != 2)
  3049. sd->delayrate+=val;
  3050. break;
  3051. case SP_CRIT_ATK_RATE:
  3052. if(sd->state.lr_flag != 2)
  3053. sd->bonus.crit_atk_rate += val;
  3054. break;
  3055. case SP_CRIT_DEF_RATE:
  3056. if(sd->state.lr_flag != 2)
  3057. sd->bonus.crit_def_rate += val;
  3058. break;
  3059. case SP_NO_REGEN:
  3060. if(sd->state.lr_flag != 2)
  3061. sd->regen.state.block|=val;
  3062. break;
  3063. case SP_UNSTRIPABLE_WEAPON:
  3064. if(sd->state.lr_flag != 2)
  3065. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3066. break;
  3067. case SP_UNSTRIPABLE:
  3068. case SP_UNSTRIPABLE_ARMOR:
  3069. if(sd->state.lr_flag != 2)
  3070. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3071. break;
  3072. case SP_UNSTRIPABLE_HELM:
  3073. if(sd->state.lr_flag != 2)
  3074. sd->bonus.unstripable_equip |= EQP_HELM;
  3075. break;
  3076. case SP_UNSTRIPABLE_SHIELD:
  3077. if(sd->state.lr_flag != 2)
  3078. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3079. break;
  3080. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3081. if(!sd->state.lr_flag) {
  3082. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3083. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3084. } else if(sd->state.lr_flag == 1) {
  3085. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3086. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3087. }
  3088. break;
  3089. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3090. if(!sd->state.lr_flag) {
  3091. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3092. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3093. } else if(sd->state.lr_flag == 1) {
  3094. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3095. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3096. }
  3097. break;
  3098. case SP_SP_GAIN_VALUE:
  3099. if(!sd->state.lr_flag)
  3100. sd->bonus.sp_gain_value += val;
  3101. break;
  3102. case SP_HP_GAIN_VALUE:
  3103. if(!sd->state.lr_flag)
  3104. sd->bonus.hp_gain_value += val;
  3105. break;
  3106. case SP_LONG_SP_GAIN_VALUE:
  3107. if(!sd->state.lr_flag)
  3108. sd->bonus.long_sp_gain_value += val;
  3109. case SP_LONG_HP_GAIN_VALUE:
  3110. if(!sd->state.lr_flag)
  3111. sd->bonus.long_hp_gain_value += val;
  3112. break;
  3113. case SP_MAGIC_SP_GAIN_VALUE:
  3114. if(!sd->state.lr_flag)
  3115. sd->bonus.magic_sp_gain_value += val;
  3116. break;
  3117. case SP_MAGIC_HP_GAIN_VALUE:
  3118. if(!sd->state.lr_flag)
  3119. sd->bonus.magic_hp_gain_value += val;
  3120. break;
  3121. case SP_ADD_HEAL_RATE:
  3122. if(sd->state.lr_flag != 2)
  3123. sd->bonus.add_heal_rate += val;
  3124. break;
  3125. case SP_ADD_HEAL2_RATE:
  3126. if(sd->state.lr_flag != 2)
  3127. sd->bonus.add_heal2_rate += val;
  3128. break;
  3129. case SP_ADD_ITEM_HEAL_RATE:
  3130. if(sd->state.lr_flag != 2)
  3131. sd->bonus.itemhealrate2 += val;
  3132. break;
  3133. case SP_EMATK:
  3134. if(sd->state.lr_flag != 2)
  3135. sd->bonus.ematk += val;
  3136. break;
  3137. case SP_ADD_VARIABLECAST:
  3138. if (sd->state.lr_flag != 2)
  3139. sd->bonus.add_varcast += val;
  3140. break;
  3141. #ifdef RENEWAL_CAST
  3142. case SP_FIXCASTRATE:
  3143. if(sd->state.lr_flag != 2)
  3144. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3145. break;
  3146. case SP_ADD_FIXEDCAST:
  3147. if(sd->state.lr_flag != 2)
  3148. sd->bonus.add_fixcast += val;
  3149. break;
  3150. case SP_CASTRATE:
  3151. case SP_VARCASTRATE:
  3152. if(sd->state.lr_flag != 2)
  3153. sd->bonus.varcastrate -= val;
  3154. break;
  3155. #else
  3156. case SP_ADD_FIXEDCAST:
  3157. case SP_FIXCASTRATE:
  3158. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3159. break;
  3160. case SP_VARCASTRATE:
  3161. case SP_CASTRATE:
  3162. if(sd->state.lr_flag != 2)
  3163. sd->castrate += val;
  3164. break;
  3165. #endif
  3166. case SP_ADDMAXWEIGHT:
  3167. if (sd->state.lr_flag != 2)
  3168. sd->add_max_weight += val;
  3169. break;
  3170. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3171. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3172. break;
  3173. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3174. if (sd->state.lr_flag != 2)
  3175. sd->bonus.critical_rangeatk += val*10;
  3176. else
  3177. sd->bonus.arrow_cri += val*10;
  3178. break;
  3179. case SP_WEAPON_ATK_RATE:
  3180. if (sd->state.lr_flag != 2)
  3181. sd->bonus.weapon_atk_rate += val;
  3182. break;
  3183. case SP_WEAPON_MATK_RATE:
  3184. if (sd->state.lr_flag != 2)
  3185. sd->bonus.weapon_matk_rate += val;
  3186. break;
  3187. case SP_NO_MADO_FUEL:
  3188. if (sd->state.lr_flag != 2)
  3189. sd->special_state.no_mado_fuel = 1;
  3190. break;
  3191. case SP_NO_WALK_DELAY:
  3192. if (sd->state.lr_flag != 2)
  3193. sd->special_state.no_walk_delay = 1;
  3194. break;
  3195. default:
  3196. if (current_equip_combo_pos > 0) {
  3197. 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);
  3198. }
  3199. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3200. 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);
  3201. }
  3202. else {
  3203. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3204. }
  3205. break;
  3206. }
  3207. }
  3208. /*==========================================
  3209. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3210. * format: bonus2 bBonusName,type2,val;
  3211. * @param sd
  3212. * @param type Bonus type used by bBonusName
  3213. * @param type2
  3214. * @param val Value that usually for rate or fixed value
  3215. *------------------------------------------*/
  3216. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3217. {
  3218. nullpo_retv(sd);
  3219. switch(type){
  3220. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3221. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3222. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3223. sd->right_weapon.addele[type2]+=val;
  3224. else if(sd->state.lr_flag == 1)
  3225. sd->left_weapon.addele[type2]+=val;
  3226. else if(sd->state.lr_flag == 2)
  3227. sd->indexed_bonus.arrow_addele[type2]+=val;
  3228. break;
  3229. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3230. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3231. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3232. sd->right_weapon.addrace[type2]+=val;
  3233. else if(sd->state.lr_flag == 1)
  3234. sd->left_weapon.addrace[type2]+=val;
  3235. else if(sd->state.lr_flag == 2)
  3236. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3237. break;
  3238. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3239. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3240. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3241. sd->right_weapon.addclass[type2]+=val;
  3242. else if(sd->state.lr_flag == 1)
  3243. sd->left_weapon.addclass[type2]+=val;
  3244. else if(sd->state.lr_flag == 2)
  3245. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3246. break;
  3247. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3248. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3249. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3250. sd->right_weapon.addsize[type2]+=val;
  3251. else if(sd->state.lr_flag == 1)
  3252. sd->left_weapon.addsize[type2]+=val;
  3253. else if(sd->state.lr_flag == 2)
  3254. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3255. break;
  3256. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3257. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3258. if(sd->state.lr_flag != 2)
  3259. sd->indexed_bonus.subele_script[type2] += val;
  3260. break;
  3261. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3262. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3263. if(sd->state.lr_flag != 2)
  3264. sd->indexed_bonus.subrace[type2]+=val;
  3265. break;
  3266. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3267. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3268. if(sd->state.lr_flag != 2)
  3269. sd->indexed_bonus.subclass[type2]+=val;
  3270. break;
  3271. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3272. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3273. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3274. break;
  3275. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3276. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3277. 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);
  3278. break;
  3279. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3280. if(sd->state.lr_flag == 2)
  3281. break;
  3282. if (sd->reseff.size() == MAX_PC_BONUS) {
  3283. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3284. break;
  3285. }
  3286. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3287. break;
  3288. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3289. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3290. if(sd->state.lr_flag != 2)
  3291. sd->indexed_bonus.magic_addele_script[type2] += val;
  3292. break;
  3293. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3294. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3295. if(sd->state.lr_flag != 2)
  3296. sd->indexed_bonus.magic_addrace[type2]+=val;
  3297. break;
  3298. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3299. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3300. if(sd->state.lr_flag != 2)
  3301. sd->indexed_bonus.magic_addclass[type2]+=val;
  3302. break;
  3303. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3304. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3305. if(sd->state.lr_flag != 2)
  3306. sd->indexed_bonus.magic_addsize[type2]+=val;
  3307. break;
  3308. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3309. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3310. if(sd->state.lr_flag != 2)
  3311. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3312. break;
  3313. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3314. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3315. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3316. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3317. break;
  3318. }
  3319. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3320. }
  3321. break;
  3322. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3323. if(sd->state.lr_flag == 2)
  3324. break;
  3325. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3326. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3327. break;
  3328. }
  3329. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3330. break;
  3331. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3332. if(sd->state.lr_flag == 2)
  3333. break;
  3334. if (sd->add_def.size() == MAX_PC_BONUS) {
  3335. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3336. break;
  3337. }
  3338. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3339. break;
  3340. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3341. if(sd->state.lr_flag == 2)
  3342. break;
  3343. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3344. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3345. break;
  3346. }
  3347. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3348. break;
  3349. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3350. if(!sd->state.lr_flag) {
  3351. sd->right_weapon.hp_drain_rate.rate += type2;
  3352. sd->right_weapon.hp_drain_rate.per += val;
  3353. }
  3354. else if(sd->state.lr_flag == 1) {
  3355. sd->left_weapon.hp_drain_rate.rate += type2;
  3356. sd->left_weapon.hp_drain_rate.per += val;
  3357. }
  3358. break;
  3359. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3360. if(!sd->state.lr_flag) {
  3361. sd->right_weapon.sp_drain_rate.rate += type2;
  3362. sd->right_weapon.sp_drain_rate.per += val;
  3363. }
  3364. else if(sd->state.lr_flag == 1) {
  3365. sd->left_weapon.sp_drain_rate.rate += type2;
  3366. sd->left_weapon.sp_drain_rate.per += val;
  3367. }
  3368. break;
  3369. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3370. if(sd->state.lr_flag != 2) {
  3371. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3372. }
  3373. break;
  3374. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3375. if(sd->state.lr_flag != 2) {
  3376. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3377. }
  3378. break;
  3379. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3380. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3381. sd->bonus.get_zeny_rate = val;
  3382. sd->bonus.get_zeny_num = type2;
  3383. }
  3384. break;
  3385. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3386. if(sd->state.lr_flag != 2) {
  3387. sd->bonus.get_zeny_rate += val;
  3388. sd->bonus.get_zeny_num += type2;
  3389. }
  3390. break;
  3391. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3392. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3393. if(sd->state.lr_flag == 2)
  3394. break;
  3395. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3396. sd->special_state.bonus_coma = 1;
  3397. break;
  3398. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3399. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3400. if(sd->state.lr_flag == 2)
  3401. break;
  3402. sd->indexed_bonus.weapon_coma_race[type2] += val;
  3403. sd->special_state.bonus_coma = 1;
  3404. break;
  3405. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3406. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3407. if(sd->state.lr_flag == 2)
  3408. break;
  3409. sd->indexed_bonus.weapon_coma_class[type2] += val;
  3410. sd->special_state.bonus_coma = 1;
  3411. break;
  3412. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3413. if(sd->state.lr_flag != 2)
  3414. sd->indexed_bonus.weapon_atk[type2]+=val;
  3415. break;
  3416. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3417. if(sd->state.lr_flag != 2)
  3418. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  3419. break;
  3420. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3421. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3422. if(sd->state.lr_flag != 2)
  3423. sd->indexed_bonus.critaddrace[type2] += val*10;
  3424. break;
  3425. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3426. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3427. if(sd->state.lr_flag != 2)
  3428. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3429. break;
  3430. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3431. if(sd->state.lr_flag == 2)
  3432. break;
  3433. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3434. 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);
  3435. break;
  3436. }
  3437. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3438. break;
  3439. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3440. if(sd->state.lr_flag == 2)
  3441. break;
  3442. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3443. 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);
  3444. break;
  3445. }
  3446. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3447. break;
  3448. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3449. if(sd->state.lr_flag == 2)
  3450. break;
  3451. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3452. 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);
  3453. break;
  3454. }
  3455. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3456. break;
  3457. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3458. if(sd->state.lr_flag == 2)
  3459. break;
  3460. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3461. 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);
  3462. break;
  3463. }
  3464. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3465. break;
  3466. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3467. if(sd->state.lr_flag != 2) {
  3468. sd->hp_loss.value = type2;
  3469. sd->hp_loss.rate = val;
  3470. }
  3471. break;
  3472. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3473. if(sd->state.lr_flag != 2) {
  3474. sd->hp_regen.value = type2;
  3475. sd->hp_regen.rate = val;
  3476. }
  3477. break;
  3478. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3479. if (sd->state.lr_flag != 2) {
  3480. sd->percent_hp_regen.value = type2;
  3481. sd->percent_hp_regen.rate = val;
  3482. }
  3483. break;
  3484. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3485. if (sd->state.lr_flag != 2) {
  3486. sd->percent_sp_regen.value = type2;
  3487. sd->percent_sp_regen.rate = val;
  3488. }
  3489. break;
  3490. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3491. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3492. if(sd->state.lr_flag != 2)
  3493. sd->right_weapon.addrace2[type2] += val;
  3494. else
  3495. sd->left_weapon.addrace2[type2] += val;
  3496. break;
  3497. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3498. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3499. if(sd->state.lr_flag != 2)
  3500. sd->indexed_bonus.subsize[type2]+=val;
  3501. break;
  3502. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  3503. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  3504. if(sd->state.lr_flag != 2)
  3505. sd->indexed_bonus.magic_subsize[type2]+=val;
  3506. break;
  3507. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3508. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3509. if(sd->state.lr_flag != 2)
  3510. sd->indexed_bonus.subrace2[type2]+=val;
  3511. break;
  3512. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3513. if(sd->state.lr_flag == 2)
  3514. break;
  3515. if (!itemdb_exists(type2)) {
  3516. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3517. break;
  3518. }
  3519. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3520. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3521. break;
  3522. }
  3523. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3524. break;
  3525. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3526. if (sd->state.lr_flag == 2)
  3527. break;
  3528. if (!type2 || !itemdb_group_exists(type2)) {
  3529. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3530. break;
  3531. }
  3532. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3533. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3534. break;
  3535. }
  3536. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3537. break;
  3538. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3539. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3540. if(sd->state.lr_flag != 2)
  3541. sd->indexed_bonus.expaddrace[type2]+=val;
  3542. break;
  3543. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3544. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3545. if(sd->state.lr_flag != 2)
  3546. sd->indexed_bonus.expaddclass[type2]+=val;
  3547. break;
  3548. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3549. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3550. if(sd->state.lr_flag != 2)
  3551. sd->indexed_bonus.sp_gain_race[type2]+=val;
  3552. break;
  3553. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3554. if (sd->state.lr_flag != 2)
  3555. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3556. break;
  3557. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3558. if (sd->state.lr_flag != 2)
  3559. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3560. break;
  3561. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3562. if(sd->state.lr_flag != 2) {
  3563. sd->sp_loss.value = type2;
  3564. sd->sp_loss.rate = val;
  3565. }
  3566. break;
  3567. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3568. if(sd->state.lr_flag != 2) {
  3569. sd->sp_regen.value = type2;
  3570. sd->sp_regen.rate = val;
  3571. }
  3572. break;
  3573. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3574. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3575. if(!sd->state.lr_flag) {
  3576. sd->right_weapon.hp_drain_race[type2] += val;
  3577. }
  3578. else if(sd->state.lr_flag == 1) {
  3579. sd->left_weapon.hp_drain_race[type2] += val;
  3580. }
  3581. break;
  3582. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3583. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3584. if(!sd->state.lr_flag) {
  3585. sd->right_weapon.sp_drain_race[type2] += val;
  3586. }
  3587. else if(sd->state.lr_flag == 1) {
  3588. sd->left_weapon.sp_drain_race[type2] += val;
  3589. }
  3590. break;
  3591. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3592. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3593. if(!sd->state.lr_flag) {
  3594. sd->right_weapon.hp_drain_class[type2] += val;
  3595. }
  3596. else if(sd->state.lr_flag == 1) {
  3597. sd->left_weapon.hp_drain_class[type2] += val;
  3598. }
  3599. break;
  3600. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3601. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3602. if(!sd->state.lr_flag) {
  3603. sd->right_weapon.sp_drain_class[type2] += val;
  3604. }
  3605. else if(sd->state.lr_flag == 1) {
  3606. sd->left_weapon.sp_drain_class[type2] += val;
  3607. }
  3608. break;
  3609. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3610. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3611. if(sd->state.lr_flag != 2)
  3612. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  3613. break;
  3614. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3615. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3616. if(sd->state.lr_flag != 2)
  3617. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  3618. break;
  3619. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3620. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3621. if(sd->state.lr_flag != 2)
  3622. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  3623. break;
  3624. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3625. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3626. if (sd->state.lr_flag != 2)
  3627. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  3628. break;
  3629. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3630. if(sd->state.lr_flag == 2)
  3631. break;
  3632. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3633. 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);
  3634. break;
  3635. }
  3636. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3637. break;
  3638. case SP_SKILL_DELAY:
  3639. if(sd->state.lr_flag == 2)
  3640. break;
  3641. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3642. 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);
  3643. break;
  3644. }
  3645. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3646. break;
  3647. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3648. if(sd->state.lr_flag == 2)
  3649. break;
  3650. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3651. 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);
  3652. break;
  3653. }
  3654. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  3655. break;
  3656. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3657. if (sd->state.lr_flag == 2)
  3658. break;
  3659. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3660. 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);
  3661. break;
  3662. }
  3663. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  3664. break;
  3665. #ifdef RENEWAL_CAST
  3666. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3667. if(sd->state.lr_flag == 2)
  3668. break;
  3669. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3670. 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);
  3671. break;
  3672. }
  3673. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  3674. break;
  3675. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3676. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3677. if(sd->state.lr_flag == 2)
  3678. break;
  3679. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3680. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3681. break;
  3682. }
  3683. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  3684. break;
  3685. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3686. if(sd->state.lr_flag == 2)
  3687. break;
  3688. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3689. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3690. break;
  3691. }
  3692. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  3693. break;
  3694. #else
  3695. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3696. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3697. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3698. break;
  3699. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3700. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3701. if(sd->state.lr_flag == 2)
  3702. break;
  3703. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3704. 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);
  3705. break;
  3706. }
  3707. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  3708. break;
  3709. #endif
  3710. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3711. if(sd->state.lr_flag == 2)
  3712. break;
  3713. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3714. 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);
  3715. break;
  3716. }
  3717. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  3718. break;
  3719. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3720. if (sd->subskill.size() == MAX_PC_BONUS) {
  3721. 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);
  3722. break;
  3723. }
  3724. pc_bonus_itembonus(sd->subskill, type2, val, false);
  3725. break;
  3726. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3727. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3728. sd->indexed_bonus.subdefele[type2] += val;
  3729. break;
  3730. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3731. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3732. sd->indexed_bonus.coma_class[type2] += val;
  3733. sd->special_state.bonus_coma = 1;
  3734. break;
  3735. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3736. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3737. sd->indexed_bonus.coma_race[type2] += val;
  3738. sd->special_state.bonus_coma = 1;
  3739. break;
  3740. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3741. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3742. if(sd->state.lr_flag != 2)
  3743. sd->indexed_bonus.magic_addrace2[type2] += val;
  3744. break;
  3745. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3746. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3747. if (sd->state.lr_flag != 2)
  3748. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  3749. break;
  3750. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3751. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3752. if (sd->state.lr_flag != 2)
  3753. sd->indexed_bonus.dropaddrace[type2] += val;
  3754. break;
  3755. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3756. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3757. if (sd->state.lr_flag != 2)
  3758. sd->indexed_bonus.dropaddclass[type2] += val;
  3759. break;
  3760. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  3761. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  3762. sd->indexed_bonus.magic_subdefele[type2] += val;
  3763. break;
  3764. default:
  3765. if (current_equip_combo_pos > 0) {
  3766. 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);
  3767. }
  3768. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3769. 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);
  3770. }
  3771. else {
  3772. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3773. }
  3774. break;
  3775. }
  3776. }
  3777. /**
  3778. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  3779. * @param sd
  3780. * @param type Bonus type used by bBonusName
  3781. * @param type2
  3782. * @param type3
  3783. * @param val Value that usually for rate or fixed value
  3784. */
  3785. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3786. {
  3787. nullpo_retv(sd);
  3788. switch(type){
  3789. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3790. if(sd->state.lr_flag != 2)
  3791. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  3792. break;
  3793. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3794. if(sd->state.lr_flag != 2)
  3795. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  3796. break;
  3797. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3798. if(sd->state.lr_flag != 2)
  3799. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  3800. break;
  3801. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3802. if(sd->state.lr_flag != 2)
  3803. {
  3804. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3805. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3806. pc_bonus_autospell(sd->autospell, target?-type2:type2, type3, val, 0, current_equip_card_id);
  3807. }
  3808. break;
  3809. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3810. if(sd->state.lr_flag != 2)
  3811. {
  3812. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3813. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3814. pc_bonus_autospell(sd->autospell2, target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3815. }
  3816. break;
  3817. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3818. if (sd->state.lr_flag != 2)
  3819. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  3820. break;
  3821. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3822. if (sd->state.lr_flag != 2)
  3823. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  3824. break;
  3825. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3826. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3827. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3828. break;
  3829. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3830. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3831. if(sd->state.lr_flag != 2)
  3832. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  3833. break;
  3834. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3835. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3836. if( sd->state.lr_flag != 2 )
  3837. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  3838. break;
  3839. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3840. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3841. if (sd->state.lr_flag != 2)
  3842. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3843. break;
  3844. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3845. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3846. if (sd->state.lr_flag != 2)
  3847. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3848. break;
  3849. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  3850. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  3851. if (sd->state.lr_flag != 2)
  3852. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  3853. break;
  3854. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3855. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3856. if(sd->state.lr_flag != 2) {
  3857. sd->sp_vanish_race[type2].rate += type3;
  3858. sd->sp_vanish_race[type2].per += val;
  3859. }
  3860. break;
  3861. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3862. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3863. if(sd->state.lr_flag != 2) {
  3864. sd->hp_vanish_race[type2].rate += type3;
  3865. sd->hp_vanish_race[type2].per += val;
  3866. }
  3867. break;
  3868. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  3869. if(sd->state.lr_flag != 2) {
  3870. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  3871. }
  3872. break;
  3873. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  3874. if(sd->state.lr_flag != 2) {
  3875. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  3876. }
  3877. break;
  3878. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3879. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3880. if (sd->state.lr_flag == 2)
  3881. break;
  3882. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3883. //if (type3 > sd->norecover_state_race[type2].rate) {
  3884. // sd->norecover_state_race[type2].rate = type3;
  3885. // sd->norecover_state_race[type2].tick = val;
  3886. // break;
  3887. //}
  3888. sd->norecover_state_race[type2].rate = type3;
  3889. sd->norecover_state_race[type2].tick = val;
  3890. break;
  3891. default:
  3892. if (current_equip_combo_pos > 0) {
  3893. 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);
  3894. }
  3895. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3896. 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);
  3897. }
  3898. else {
  3899. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3900. }
  3901. break;
  3902. }
  3903. }
  3904. /**
  3905. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  3906. * @param sd
  3907. * @param type Bonus type used by bBonusName
  3908. * @param type2
  3909. * @param type3
  3910. * @param type4
  3911. * @param val Value that usually for rate or fixed value
  3912. */
  3913. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3914. {
  3915. nullpo_retv(sd);
  3916. switch(type){
  3917. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3918. if(sd->state.lr_flag != 2)
  3919. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3920. break;
  3921. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3922. if(sd->state.lr_flag != 2)
  3923. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3924. break;
  3925. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3926. if(sd->state.lr_flag != 2)
  3927. {
  3928. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3929. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  3930. pc_bonus_autospell_onskill(sd->autospell3, type2, target?-type3:type3, type4, val, current_equip_card_id);
  3931. }
  3932. break;
  3933. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3934. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3935. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3936. break;
  3937. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3938. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3939. if (sd->state.lr_flag != 2)
  3940. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  3941. break;
  3942. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3943. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3944. if( sd->state.lr_flag != 2 )
  3945. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  3946. break;
  3947. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3948. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3949. if(sd->state.lr_flag == 2)
  3950. break;
  3951. sd->def_set_race[type2].rate = type3;
  3952. sd->def_set_race[type2].tick = type4;
  3953. sd->def_set_race[type2].value = val;
  3954. break;
  3955. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3956. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3957. if(sd->state.lr_flag == 2)
  3958. break;
  3959. sd->mdef_set_race[type2].rate = type3;
  3960. sd->mdef_set_race[type2].tick = type4;
  3961. sd->mdef_set_race[type2].value = val;
  3962. break;
  3963. default:
  3964. if (current_equip_combo_pos > 0) {
  3965. 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);
  3966. }
  3967. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3968. 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);
  3969. }
  3970. else {
  3971. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3972. }
  3973. break;
  3974. }
  3975. }
  3976. /**
  3977. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3978. * @param sd
  3979. * @param type Bonus type used by bBonusName
  3980. * @param type2
  3981. * @param type3
  3982. * @param type4
  3983. * @param val Value that usually for rate or fixed value
  3984. */
  3985. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3986. {
  3987. nullpo_retv(sd);
  3988. switch(type){
  3989. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3990. if(sd->state.lr_flag != 2)
  3991. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3992. break;
  3993. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3994. if(sd->state.lr_flag != 2)
  3995. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3996. break;
  3997. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3998. if(sd->state.lr_flag != 2)
  3999. pc_bonus_autospell_onskill(sd->autospell3, type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  4000. break;
  4001. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4002. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4003. if( sd->state.lr_flag != 2 )
  4004. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4005. break;
  4006. default:
  4007. if (current_equip_combo_pos > 0) {
  4008. 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);
  4009. }
  4010. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4011. 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);
  4012. }
  4013. else {
  4014. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4015. }
  4016. break;
  4017. }
  4018. }
  4019. /*==========================================
  4020. * Grants a player a given skill. Flag values are:
  4021. * 0 - Grant permanent skill to be bound to skill tree
  4022. * 1 - Grant an item skill (temporary)
  4023. * 2 - Like 1, except the level granted can stack with previously learned level.
  4024. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4025. *------------------------------------------*/
  4026. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  4027. uint16 idx = 0;
  4028. nullpo_ret(sd);
  4029. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4030. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4031. return false;
  4032. }
  4033. if (level > MAX_SKILL_LEVEL) {
  4034. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4035. return false;
  4036. }
  4037. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4038. 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);
  4039. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4040. }
  4041. switch (type) {
  4042. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4043. sd->status.skill[idx].id = skill_id;
  4044. sd->status.skill[idx].lv = level;
  4045. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4046. if (level == 0) { //Remove skill.
  4047. sd->status.skill[idx].id = 0;
  4048. clif_deleteskill(sd,skill_id);
  4049. } else
  4050. clif_addskill(sd,skill_id);
  4051. 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.
  4052. status_calc_pc(sd, SCO_NONE);
  4053. break;
  4054. case ADDSKILL_TEMP: //Item bonus skill.
  4055. if (sd->status.skill[idx].id != 0) {
  4056. if (sd->status.skill[idx].lv >= level)
  4057. return true;
  4058. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4059. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4060. } else {
  4061. sd->status.skill[idx].id = skill_id;
  4062. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4063. }
  4064. sd->status.skill[idx].lv = level;
  4065. break;
  4066. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4067. if (sd->status.skill[idx].id != 0) {
  4068. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4069. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4070. } else {
  4071. sd->status.skill[idx].id = skill_id;
  4072. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4073. }
  4074. sd->status.skill[idx].lv += level;
  4075. break;
  4076. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4077. sd->status.skill[idx].id = skill_id;
  4078. sd->status.skill[idx].lv = level;
  4079. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4080. if (level == 0) { //Remove skill.
  4081. sd->status.skill[idx].id = 0;
  4082. clif_deleteskill(sd,skill_id);
  4083. } else
  4084. clif_addskill(sd,skill_id);
  4085. 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.
  4086. status_calc_pc(sd, SCO_NONE);
  4087. break;
  4088. default:
  4089. return false;
  4090. }
  4091. return true;
  4092. }
  4093. /*==========================================
  4094. * Append a card to an item ?
  4095. *------------------------------------------*/
  4096. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  4097. {
  4098. nullpo_ret(sd);
  4099. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4100. return 0;
  4101. }
  4102. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4103. return 0;
  4104. }
  4105. int i;
  4106. t_itemid nameid;
  4107. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4108. struct item_data* item_card = sd->inventory_data[idx_card];
  4109. if(item_eq == nullptr)
  4110. return 0; //Invalid item index.
  4111. if(item_card == nullptr)
  4112. return 0; //Invalid card index.
  4113. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4114. return 0; // target item missing
  4115. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4116. return 0; // target card missing
  4117. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4118. return 0; // only weapons and armor are allowed
  4119. if( item_card->type != IT_CARD )
  4120. return 0; // must be a card
  4121. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4122. return 0; // target must be identified
  4123. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4124. return 0; // card slots reserved for other purposes
  4125. if( (item_eq->equip & item_card->equip) == 0 )
  4126. return 0; // card cannot be compounded on this item type
  4127. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4128. return 0; // attempted to place shield card on left-hand weapon.
  4129. 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)) )
  4130. return 0; // specific accessory-card can only be inserted to specific accessory.
  4131. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4132. return 0; // item must be unequipped
  4133. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4134. if( i == item_eq->slots )
  4135. return 0; // no free slots
  4136. // remember the card id to insert
  4137. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4138. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4139. {// failed
  4140. clif_insert_card(sd,idx_equip,idx_card,1);
  4141. }
  4142. else
  4143. {// success
  4144. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4145. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4146. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4147. clif_insert_card(sd,idx_equip,idx_card,0);
  4148. }
  4149. return 0;
  4150. }
  4151. /**
  4152. * Returns the count of unidentified items with the option to identify too.
  4153. * @param sd: Player data
  4154. * @param identify_item: Whether or not to identify any unidentified items
  4155. * @return Unidentified items count
  4156. */
  4157. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  4158. {
  4159. int unidentified_count = 0;
  4160. for (int i = 0; i < MAX_INVENTORY; i++) {
  4161. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4162. if (identify_item == true) {
  4163. sd->inventory.u.items_inventory[i].identify = 1;
  4164. clif_item_identified(sd,i,0);
  4165. }
  4166. unidentified_count++;
  4167. }
  4168. }
  4169. return unidentified_count;
  4170. }
  4171. //
  4172. // Items
  4173. //
  4174. /*==========================================
  4175. * Update buying value by skills
  4176. *------------------------------------------*/
  4177. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4178. {
  4179. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4180. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4181. rate1 = 5+skill*2-((skill==10)? 1:0);
  4182. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4183. rate2 = 5+skill*4;
  4184. if(rate1 < rate2) rate1 = rate2;
  4185. if(rate1)
  4186. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4187. if(val < battle_config.min_shop_buy)
  4188. val = battle_config.min_shop_buy;
  4189. return val;
  4190. }
  4191. /*==========================================
  4192. * Update selling value by skills
  4193. *------------------------------------------*/
  4194. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4195. {
  4196. int skill,val = orig_value,rate = 0;
  4197. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4198. rate = 5+skill*2-((skill==10)? 1:0);
  4199. if(rate)
  4200. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4201. if (val < battle_config.min_shop_sell)
  4202. val = battle_config.min_shop_sell;
  4203. return val;
  4204. }
  4205. /*==========================================
  4206. * Checking if we have enough place on inventory for new item
  4207. * Make sure to take 30k as limit (for client I guess)
  4208. * @param sd
  4209. * @param nameid
  4210. * @param amount
  4211. * @return e_chkitem_result
  4212. *------------------------------------------*/
  4213. char pc_checkadditem(struct map_session_data *sd, t_itemid nameid, int amount)
  4214. {
  4215. int i;
  4216. struct item_data* data;
  4217. nullpo_ret(sd);
  4218. if(amount > MAX_AMOUNT)
  4219. return CHKADDITEM_OVERAMOUNT;
  4220. data = itemdb_search(nameid);
  4221. if(!itemdb_isstackable2(data))
  4222. return CHKADDITEM_NEW;
  4223. if( data->stack.inventory && amount > data->stack.amount )
  4224. return CHKADDITEM_OVERAMOUNT;
  4225. if (data->flag.guid)
  4226. return CHKADDITEM_NEW;
  4227. for(i=0;i<MAX_INVENTORY;i++){
  4228. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4229. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4230. 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 ) )
  4231. return CHKADDITEM_OVERAMOUNT;
  4232. return CHKADDITEM_EXIST;
  4233. }
  4234. }
  4235. return CHKADDITEM_NEW;
  4236. }
  4237. /*==========================================
  4238. * Return number of available place in inventory
  4239. * Each non stackable item will reduce place by 1
  4240. * @param sd
  4241. * @return Number of empty slots
  4242. *------------------------------------------*/
  4243. uint8 pc_inventoryblank(struct map_session_data *sd)
  4244. {
  4245. uint8 i, b;
  4246. nullpo_ret(sd);
  4247. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  4248. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4249. b++;
  4250. }
  4251. return b;
  4252. }
  4253. /**
  4254. * Attempts to remove zeny from player
  4255. * @param sd: Player
  4256. * @param zeny: Zeny removed
  4257. * @param type: Log type
  4258. * @param tsd: (optional) From who to log (if null take sd)
  4259. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4260. */
  4261. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4262. {
  4263. nullpo_retr(2,sd);
  4264. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4265. if( zeny < 0 )
  4266. {
  4267. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4268. return 1;
  4269. }
  4270. if( sd->status.zeny < zeny )
  4271. return 1; //Not enough.
  4272. sd->status.zeny -= zeny;
  4273. clif_updatestatus(sd,SP_ZENY);
  4274. if(!tsd) tsd = sd;
  4275. log_zeny(sd, type, tsd, -zeny);
  4276. if( zeny > 0 && sd->state.showzeny ) {
  4277. char output[255];
  4278. sprintf(output, "Removed %dz.", zeny);
  4279. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4280. }
  4281. return 0;
  4282. }
  4283. /**
  4284. * Attempts to give zeny to player
  4285. * @param sd: Player
  4286. * @param type: Log type
  4287. * @param tsd: (optional) From who to log (if null take sd)
  4288. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4289. */
  4290. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4291. {
  4292. nullpo_retr(-1,sd);
  4293. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4294. if( zeny < 0 )
  4295. {
  4296. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4297. return 1;
  4298. }
  4299. if( zeny > MAX_ZENY - sd->status.zeny )
  4300. zeny = MAX_ZENY - sd->status.zeny;
  4301. sd->status.zeny += zeny;
  4302. clif_updatestatus(sd,SP_ZENY);
  4303. if(!tsd) tsd = sd;
  4304. log_zeny(sd, type, tsd, zeny);
  4305. if( zeny > 0 && sd->state.showzeny ) {
  4306. char output[255];
  4307. sprintf(output, "Gained %dz.", zeny);
  4308. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4309. }
  4310. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4311. return 0;
  4312. }
  4313. /**
  4314. * Attempts to remove Cash Points from player
  4315. * @param sd: Player
  4316. * @param price: Total points (cash + kafra) the player has to pay
  4317. * @param points: Kafra points the player has to pay
  4318. * @param type: Log type
  4319. * @return -1: Not enough points, otherwise success (cash+points)
  4320. */
  4321. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4322. {
  4323. int cash;
  4324. nullpo_retr(-1,sd);
  4325. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4326. cash = price-points;
  4327. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4328. {
  4329. 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);
  4330. return -1;
  4331. }
  4332. if( cash ){
  4333. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4334. sd->cashPoints -= cash;
  4335. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4336. }
  4337. if( points ){
  4338. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4339. sd->kafraPoints -= points;
  4340. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4341. }
  4342. if( battle_config.cashshop_show_points )
  4343. {
  4344. char output[CHAT_SIZE_MAX];
  4345. 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.
  4346. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4347. }
  4348. return cash+points;
  4349. }
  4350. /**
  4351. * Attempts to give Cash Points to player
  4352. * @param sd: Player
  4353. * @param cash: Cash points the player gets
  4354. * @param points: Kafra points the player gets
  4355. * @param type: Log type
  4356. * @return -1: Error, otherwise success (cash or points)
  4357. */
  4358. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4359. {
  4360. char output[CHAT_SIZE_MAX];
  4361. nullpo_retr(-1,sd);
  4362. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4363. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4364. if( cash > 0 )
  4365. {
  4366. if( cash > MAX_ZENY-sd->cashPoints )
  4367. {
  4368. 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);
  4369. cash = MAX_ZENY-sd->cashPoints;
  4370. }
  4371. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4372. sd->cashPoints += cash;
  4373. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4374. if( battle_config.cashshop_show_points )
  4375. {
  4376. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4377. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4378. }
  4379. return cash;
  4380. }
  4381. if( points > 0 )
  4382. {
  4383. if( points > MAX_ZENY-sd->kafraPoints )
  4384. {
  4385. 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);
  4386. points = MAX_ZENY-sd->kafraPoints;
  4387. }
  4388. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4389. sd->kafraPoints += points;
  4390. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4391. if( battle_config.cashshop_show_points )
  4392. {
  4393. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4394. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4395. }
  4396. return points;
  4397. }
  4398. return -1; //shouldn't happen but just in case
  4399. }
  4400. /**
  4401. * Searching a specified itemid in inventory and return his stored index
  4402. * @param sd Player
  4403. * @param nameid Find this Item!
  4404. * @return Stored index in inventory, or -1 if not found.
  4405. **/
  4406. short pc_search_inventory(struct map_session_data *sd, t_itemid nameid) {
  4407. short i;
  4408. nullpo_retr(-1, sd);
  4409. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4410. return ( i < MAX_INVENTORY ) ? i : -1;
  4411. }
  4412. /** Attempt to add a new item to player inventory
  4413. * @param sd
  4414. * @param item_data
  4415. * @param amount
  4416. * @param log_type
  4417. * @return
  4418. * 0 = success
  4419. * 1 = invalid itemid not found or negative amount
  4420. * 2 = overweight
  4421. * 3 = ?
  4422. * 4 = no free place found
  4423. * 5 = max amount reached
  4424. * 6 = ?
  4425. * 7 = stack limitation
  4426. */
  4427. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4428. struct item_data *id;
  4429. int16 i;
  4430. unsigned int w;
  4431. nullpo_retr(ADDITEM_INVALID, sd);
  4432. nullpo_retr(ADDITEM_INVALID, item);
  4433. if( item->nameid == 0 || amount <= 0 )
  4434. return ADDITEM_INVALID;
  4435. if( amount > MAX_AMOUNT )
  4436. return ADDITEM_OVERAMOUNT;
  4437. id = itemdb_search(item->nameid);
  4438. if( id->stack.inventory && amount > id->stack.amount )
  4439. {// item stack limitation
  4440. return ADDITEM_STACKLIMIT;
  4441. }
  4442. w = id->weight*amount;
  4443. if(sd->weight + w > sd->max_weight)
  4444. return ADDITEM_OVERWEIGHT;
  4445. i = MAX_INVENTORY;
  4446. if (id->flag.guid && !item->unique_id)
  4447. item->unique_id = pc_generate_unique_id(sd);
  4448. // Stackable | Non Rental
  4449. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4450. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4451. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4452. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4453. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4454. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4455. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4456. 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 ) )
  4457. return ADDITEM_OVERAMOUNT;
  4458. sd->inventory.u.items_inventory[i].amount += amount;
  4459. clif_additem(sd,i,amount,0);
  4460. break;
  4461. }
  4462. }
  4463. }
  4464. if (i >= MAX_INVENTORY) {
  4465. i = pc_search_inventory(sd,0);
  4466. if( i < 0 )
  4467. return ADDITEM_OVERITEM;
  4468. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4469. // clear equip and favorite fields first, just in case
  4470. if( item->equip )
  4471. sd->inventory.u.items_inventory[i].equip = 0;
  4472. if( item->favorite )
  4473. sd->inventory.u.items_inventory[i].favorite = 0;
  4474. if( item->equipSwitch )
  4475. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4476. sd->inventory.u.items_inventory[i].amount = amount;
  4477. sd->inventory_data[i] = id;
  4478. sd->last_addeditem_index = i;
  4479. if (!itemdb_isstackable2(id) || id->flag.guid)
  4480. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4481. clif_additem(sd,i,amount,0);
  4482. }
  4483. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4484. sd->weight += w;
  4485. clif_updatestatus(sd,SP_WEIGHT);
  4486. //Auto-equip
  4487. if(id->flag.autoequip)
  4488. pc_equipitem(sd, i, id->equip);
  4489. /* rental item check */
  4490. if( item->expire_time ) {
  4491. if( time(NULL) > item->expire_time ) {
  4492. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  4493. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4494. } else {
  4495. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4496. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4497. pc_inventory_rental_add(sd, seconds);
  4498. }
  4499. }
  4500. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4501. pc_show_questinfo(sd);
  4502. return ADDITEM_SUCCESS;
  4503. }
  4504. /*==========================================
  4505. * Remove an item at index n from inventory by amount.
  4506. * @param sd
  4507. * @param n Item index in inventory
  4508. * @param amount
  4509. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4510. * @param reason Delete reason
  4511. * @param log_type e_log_pick_type
  4512. * @return 1 - invalid itemid or negative amount; 0 - Success
  4513. *------------------------------------------*/
  4514. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4515. {
  4516. nullpo_retr(1, sd);
  4517. 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)
  4518. return 1;
  4519. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4520. sd->inventory.u.items_inventory[n].amount -= amount;
  4521. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4522. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4523. if(sd->inventory.u.items_inventory[n].equip)
  4524. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4525. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4526. sd->inventory_data[n] = NULL;
  4527. }
  4528. if(!(type&1))
  4529. clif_delitem(sd,n,amount,reason);
  4530. if(!(type&2))
  4531. clif_updatestatus(sd,SP_WEIGHT);
  4532. pc_show_questinfo(sd);
  4533. return 0;
  4534. }
  4535. /*==========================================
  4536. * Attempt to drop an item.
  4537. * @param sd
  4538. * @param n Item index in inventory
  4539. * @param amount Amount of item
  4540. * @return False = fail; True = success
  4541. *------------------------------------------*/
  4542. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4543. {
  4544. nullpo_retr(1, sd);
  4545. if(n < 0 || n >= MAX_INVENTORY)
  4546. return false;
  4547. if(amount <= 0)
  4548. return false;
  4549. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  4550. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4551. sd->inventory.u.items_inventory[n].amount < amount ||
  4552. sd->state.trading || sd->state.vending ||
  4553. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4554. )
  4555. return false;
  4556. if( sd->inventory.u.items_inventory[n].equipSwitch )
  4557. return false;
  4558. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4559. {
  4560. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4561. return false; //Can't drop items in nodrop mapflag maps.
  4562. }
  4563. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4564. {
  4565. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4566. return false;
  4567. }
  4568. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4569. return false;
  4570. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4571. clif_dropitem(sd, n, amount);
  4572. return true;
  4573. }
  4574. /*==========================================
  4575. * Attempt to pick up an item.
  4576. * @param sd
  4577. * @param fitem Item that will be picked
  4578. * @return False = fail; True = success
  4579. *------------------------------------------*/
  4580. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4581. {
  4582. int flag = 0;
  4583. t_tick tick = gettick();
  4584. struct party_data *p = NULL;
  4585. nullpo_ret(sd);
  4586. nullpo_ret(fitem);
  4587. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4588. return false; // Distance is too far
  4589. if (sd->sc.cant.pickup)
  4590. return false;
  4591. if (sd->status.party_id)
  4592. p = party_search(sd->status.party_id);
  4593. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4594. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4595. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4596. if (!(p && p->party.item&1 &&
  4597. first_sd && first_sd->status.party_id == sd->status.party_id
  4598. ))
  4599. return false;
  4600. }
  4601. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4602. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4603. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4604. if (!(p && p->party.item&1 &&
  4605. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4606. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4607. ))
  4608. return false;
  4609. }
  4610. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4611. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4612. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4613. if(!(p && p->party.item&1 &&
  4614. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4615. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4616. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4617. ))
  4618. return false;
  4619. }
  4620. }
  4621. }
  4622. }
  4623. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4624. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4625. clif_additem(sd,0,0,flag);
  4626. return true;
  4627. }
  4628. //Display pickup animation.
  4629. pc_stop_attack(sd);
  4630. clif_takeitem(&sd->bl,&fitem->bl);
  4631. if (fitem->mob_id &&
  4632. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4633. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4634. )
  4635. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4636. map_clearflooritem(&fitem->bl);
  4637. return true;
  4638. }
  4639. /*==========================================
  4640. * Check if item is usable.
  4641. * Return:
  4642. * false = no
  4643. * true = yes
  4644. *------------------------------------------*/
  4645. bool pc_isUseitem(struct map_session_data *sd,int n)
  4646. {
  4647. struct item_data *item;
  4648. t_itemid nameid;
  4649. nullpo_ret(sd);
  4650. item = sd->inventory_data[n];
  4651. nameid = sd->inventory.u.items_inventory[n].nameid;
  4652. if( item == NULL )
  4653. return false;
  4654. //Not consumable item
  4655. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4656. return false;
  4657. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4658. return true;
  4659. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4660. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4661. return false;
  4662. //Prevent mass item usage. [Skotlex]
  4663. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4664. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4665. )
  4666. return false;
  4667. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4668. clif_msg(sd,ITEM_NOUSE_SITTING);
  4669. return false; // You cannot use this item while sitting.
  4670. }
  4671. if (sd->state.storage_flag && item->type != IT_CASH) {
  4672. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4673. return false; // You cannot use this item while storage is open.
  4674. }
  4675. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4676. return false;
  4677. switch( nameid ) {
  4678. case ITEMID_WING_OF_FLY:
  4679. case ITEMID_GIANT_FLY_WING:
  4680. case ITEMID_N_FLY_WING:
  4681. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4682. clif_skill_teleportmessage(sd,0);
  4683. return false;
  4684. }
  4685. if (nameid == ITEMID_GIANT_FLY_WING) {
  4686. struct party_data *pd = party_search(sd->status.party_id);
  4687. if (pd) {
  4688. int i;
  4689. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4690. if (i == MAX_PARTY) { // User is not party leader
  4691. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4692. break;
  4693. }
  4694. 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));
  4695. if (i == MAX_PARTY) { // No party members found on same map
  4696. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4697. break;
  4698. }
  4699. } else {
  4700. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4701. break;
  4702. }
  4703. }
  4704. // Fall through
  4705. case ITEMID_WING_OF_BUTTERFLY:
  4706. case ITEMID_N_BUTTERFLY_WING:
  4707. case ITEMID_DUN_TELE_SCROLL1:
  4708. case ITEMID_DUN_TELE_SCROLL2:
  4709. case ITEMID_DUN_TELE_SCROLL3:
  4710. case ITEMID_WOB_RUNE:
  4711. case ITEMID_WOB_SCHWALTZ:
  4712. case ITEMID_WOB_RACHEL:
  4713. case ITEMID_WOB_LOCAL:
  4714. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4715. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4716. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4717. return false;
  4718. }
  4719. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4720. return false;
  4721. break;
  4722. case ITEMID_MERCENARY_RED_POTION:
  4723. case ITEMID_MERCENARY_BLUE_POTION:
  4724. case ITEMID_M_CENTER_POTION:
  4725. case ITEMID_M_AWAKENING_POTION:
  4726. case ITEMID_M_BERSERK_POTION:
  4727. if( sd->md == NULL || sd->md->db == NULL )
  4728. return false;
  4729. if( sd->md->sc.data[SC_BERSERK] )
  4730. return false;
  4731. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4732. return false;
  4733. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4734. return false;
  4735. break;
  4736. case ITEMID_NEURALIZER:
  4737. if( !mapdata->flag[MF_RESET] )
  4738. return false;
  4739. break;
  4740. }
  4741. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4742. return false; // Mercenary Scrolls
  4743. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4744. if( pc_is90overweight(sd) ) {
  4745. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4746. return false;
  4747. }
  4748. if( !pc_inventoryblank(sd) ) {
  4749. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4750. return false;
  4751. }
  4752. }
  4753. //Gender check
  4754. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  4755. return false;
  4756. //Required level check
  4757. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4758. return false;
  4759. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4760. return false;
  4761. //Not equipable by class. [Skotlex]
  4762. if (!pc_job_can_use_item(sd,item))
  4763. return false;
  4764. if (sd->sc.count && (
  4765. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4766. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4767. sd->sc.data[SC_TRICKDEAD] ||
  4768. sd->sc.data[SC_HIDING] ||
  4769. sd->sc.data[SC__SHADOWFORM] ||
  4770. sd->sc.data[SC__INVISIBILITY] ||
  4771. sd->sc.data[SC__MANHOLE] ||
  4772. sd->sc.data[SC_DEEPSLEEP] ||
  4773. sd->sc.data[SC_CRYSTALIZE] ||
  4774. sd->sc.data[SC_KAGEHUMI] ||
  4775. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4776. sd->sc.data[SC_KINGS_GRACE] ||
  4777. sd->sc.data[SC_SUHIDE]))
  4778. return false;
  4779. if (!pc_isItemClass(sd,item))
  4780. return false;
  4781. //Dead Branch items
  4782. if( item->flag.dead_branch )
  4783. log_branch(sd);
  4784. return true;
  4785. }
  4786. /*==========================================
  4787. * Last checks to use an item.
  4788. * Return:
  4789. * 0 = fail
  4790. * 1 = success
  4791. *------------------------------------------*/
  4792. int pc_useitem(struct map_session_data *sd,int n)
  4793. {
  4794. t_tick tick = gettick();
  4795. int amount;
  4796. t_itemid nameid;
  4797. struct script_code *script;
  4798. struct item item;
  4799. struct item_data *id;
  4800. nullpo_ret(sd);
  4801. if (sd->state.mail_writing)
  4802. return 0;
  4803. if (sd->npc_id) {
  4804. if (sd->progressbar.npc_id) {
  4805. clif_progressbar_abort(sd);
  4806. return 0; // First item use attempt cancels the progress bar
  4807. }
  4808. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4809. #ifdef RENEWAL
  4810. clif_msg( sd, WORK_IN_PROGRESS );
  4811. #endif
  4812. return 0;
  4813. }
  4814. }
  4815. item = sd->inventory.u.items_inventory[n];
  4816. id = sd->inventory_data[n];
  4817. if (item.nameid == 0 || item.amount <= 0)
  4818. return 0;
  4819. if( !pc_isUseitem(sd,n) )
  4820. return 0;
  4821. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4822. nameid = id->nameid;
  4823. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4824. return 0;
  4825. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4826. if( id->flag.delay_consume > 0 ) {
  4827. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4828. return 0;
  4829. else if( pc_issit(sd) )
  4830. return 0;
  4831. }
  4832. //Since most delay-consume items involve using a "skill-type" target cursor,
  4833. //perform a skill-use check before going through. [Skotlex]
  4834. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4835. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  4836. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4837. return 0;
  4838. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4839. return 0;
  4840. /* on restricted maps the item is consumed but the effect is not used */
  4841. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4842. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4843. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  4844. clif_useitemack(sd,n,item.amount-1,true);
  4845. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4846. }
  4847. return 0;/* regardless, effect is not run */
  4848. }
  4849. sd->itemid = item.nameid;
  4850. sd->itemindex = n;
  4851. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4852. sd->catch_target_class = PET_CATCH_FAIL;
  4853. amount = item.amount;
  4854. script = id->script;
  4855. //Check if the item is to be consumed immediately [Skotlex]
  4856. if (id->flag.delay_consume > 0)
  4857. clif_useitemack(sd, n, amount, true);
  4858. else
  4859. {
  4860. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4861. {
  4862. clif_useitemack(sd, n, amount - 1, true);
  4863. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4864. }
  4865. else
  4866. clif_useitemack(sd, n, 0, false);
  4867. }
  4868. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4869. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4870. //Update item use time.
  4871. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4872. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4873. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4874. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4875. potion_flag = 0;
  4876. return 1;
  4877. }
  4878. /**
  4879. * Add item on cart for given index.
  4880. * @param sd
  4881. * @param item
  4882. * @param amount
  4883. * @param log_type
  4884. * @return See pc.hpp::e_additem_result
  4885. */
  4886. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4887. {
  4888. struct item_data *data;
  4889. int i,w;
  4890. nullpo_retr(ADDITEM_INVALID, sd);
  4891. nullpo_retr(ADDITEM_INVALID, item);
  4892. if(item->nameid == 0 || amount <= 0)
  4893. return ADDITEM_INVALID;
  4894. if (itemdb_ishatched_egg(item))
  4895. return ADDITEM_INVALID;
  4896. data = itemdb_search(item->nameid);
  4897. if( data->stack.cart && amount > data->stack.amount )
  4898. {// item stack limitation
  4899. return ADDITEM_STACKLIMIT;
  4900. }
  4901. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4902. { // Check item trade restrictions [Skotlex]
  4903. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4904. return ADDITEM_INVALID;
  4905. }
  4906. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4907. return ADDITEM_OVERWEIGHT;
  4908. i = MAX_CART;
  4909. if( itemdb_isstackable2(data) && !item->expire_time )
  4910. {
  4911. for (i = 0; i < MAX_CART; i++) {
  4912. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4913. && sd->cart.u.items_cart[i].bound == item->bound
  4914. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4915. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4916. )
  4917. break;
  4918. }
  4919. }
  4920. if( i < MAX_CART )
  4921. {// item already in cart, stack it
  4922. 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 ) )
  4923. return ADDITEM_OVERAMOUNT; // no slot
  4924. sd->cart.u.items_cart[i].amount += amount;
  4925. clif_cart_additem(sd,i,amount);
  4926. }
  4927. else
  4928. {// item not stackable or not present, add it
  4929. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4930. if( i == MAX_CART )
  4931. return ADDITEM_OVERAMOUNT; // no slot
  4932. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4933. sd->cart.u.items_cart[i].id = 0;
  4934. sd->cart.u.items_cart[i].amount = amount;
  4935. sd->cart_num++;
  4936. clif_cart_additem(sd,i,amount);
  4937. }
  4938. sd->cart.u.items_cart[i].favorite = 0; // clear
  4939. sd->cart.u.items_cart[i].equipSwitch = 0;
  4940. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4941. sd->cart_weight += w;
  4942. clif_updatestatus(sd,SP_CARTINFO);
  4943. return ADDITEM_SUCCESS;
  4944. }
  4945. /*==========================================
  4946. * Delete item on cart for given index.
  4947. *------------------------------------------*/
  4948. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4949. {
  4950. nullpo_retv(sd);
  4951. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4952. sd->cart.u.items_cart[n].amount < amount)
  4953. return;
  4954. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4955. sd->cart.u.items_cart[n].amount -= amount;
  4956. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4957. if(sd->cart.u.items_cart[n].amount <= 0) {
  4958. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4959. sd->cart_num--;
  4960. }
  4961. if(!type) {
  4962. clif_cart_delitem(sd,n,amount);
  4963. clif_updatestatus(sd,SP_CARTINFO);
  4964. }
  4965. }
  4966. /*==========================================
  4967. * Transfer item from inventory to cart.
  4968. *------------------------------------------*/
  4969. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4970. {
  4971. nullpo_retv(sd);
  4972. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4973. return;
  4974. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  4975. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  4976. return;
  4977. if( item_data->equipSwitch ){
  4978. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  4979. return;
  4980. }
  4981. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  4982. if (flag == ADDITEM_SUCCESS)
  4983. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4984. else {
  4985. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  4986. clif_additem(sd, idx, amount, 0);
  4987. clif_delitem(sd, idx, amount, 0);
  4988. }
  4989. }
  4990. /*==========================================
  4991. * Get number of item in cart.
  4992. * Return:
  4993. -1 = itemid not found or no amount found
  4994. x = remaining itemid on cart after get
  4995. *------------------------------------------*/
  4996. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4997. {
  4998. struct item* item_data;
  4999. nullpo_retr(-1, sd);
  5000. item_data = &sd->cart.u.items_cart[idx];
  5001. if( item_data->nameid == 0 || item_data->amount == 0 )
  5002. return -1;
  5003. return item_data->amount - amount;
  5004. }
  5005. /*==========================================
  5006. * Retrieve an item at index idx from cart.
  5007. *------------------------------------------*/
  5008. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  5009. {
  5010. nullpo_retv(sd);
  5011. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5012. return;
  5013. struct item *item_data=&sd->cart.u.items_cart[idx];
  5014. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5015. return;
  5016. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5017. if (flag == ADDITEM_SUCCESS)
  5018. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5019. else {
  5020. clif_cart_delitem(sd, idx, amount);
  5021. clif_additem(sd, idx, amount, flag);
  5022. clif_cart_additem(sd, idx, amount);
  5023. }
  5024. }
  5025. /*==========================================
  5026. * Bound Item Check
  5027. * Type:
  5028. * 1 Account Bound
  5029. * 2 Guild Bound
  5030. * 3 Party Bound
  5031. * 4 Character Bound
  5032. *------------------------------------------*/
  5033. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  5034. {
  5035. int i = 0, j = 0;
  5036. for(i = 0; i < MAX_INVENTORY; i++) {
  5037. 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) {
  5038. idxlist[j] = i;
  5039. j++;
  5040. }
  5041. }
  5042. return j;
  5043. }
  5044. /*==========================================
  5045. * Display item stolen msg to player sd
  5046. *------------------------------------------*/
  5047. int pc_show_steal(struct block_list *bl,va_list ap)
  5048. {
  5049. struct map_session_data *sd;
  5050. t_itemid itemid;
  5051. struct item_data *item=NULL;
  5052. char output[100];
  5053. sd=va_arg(ap,struct map_session_data *);
  5054. itemid=va_arg(ap,int);
  5055. if((item=itemdb_exists(itemid))==NULL)
  5056. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5057. else
  5058. sprintf(output,"%s stole %s.",sd->status.name,item->ename.c_str());
  5059. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  5060. return 0;
  5061. }
  5062. /**
  5063. * Steal an item from bl (mob).
  5064. * @param sd: Player data
  5065. * @param bl: Object to steal from
  5066. * @param skill_lv: Level of skill used
  5067. * @return True on success or false otherwise
  5068. */
  5069. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5070. {
  5071. int i;
  5072. t_itemid itemid;
  5073. double rate;
  5074. unsigned char flag = 0;
  5075. struct status_data *sd_status, *md_status;
  5076. struct mob_data *md;
  5077. struct item tmp_item;
  5078. if(!sd || !bl || bl->type!=BL_MOB)
  5079. return false;
  5080. md = (TBL_MOB *)bl;
  5081. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5082. return false;
  5083. sd_status= status_get_status_data(&sd->bl);
  5084. md_status= status_get_status_data(bl);
  5085. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  5086. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5087. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5088. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5089. ) { //Can't steal from
  5090. md->state.steal_flag = UCHAR_MAX;
  5091. return false;
  5092. }
  5093. // base skill success chance (percentual)
  5094. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5095. rate += sd->bonus.add_steal_rate;
  5096. if( rate < 1
  5097. #ifdef RENEWAL
  5098. || rnd()%100 >= rate
  5099. #endif
  5100. )
  5101. return false;
  5102. // Try dropping one item, in the order from first to last possible slot.
  5103. // Droprate is affected by the skill success rate.
  5104. for( i = 0; i < MAX_STEAL_DROP; i++ )
  5105. if( md->db->dropitem[i].nameid > 0 && !md->db->dropitem[i].steal_protected && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p
  5106. #ifndef RENEWAL
  5107. * rate/100.
  5108. #endif
  5109. )
  5110. break;
  5111. if( i == MAX_STEAL_DROP )
  5112. return false;
  5113. itemid = md->db->dropitem[i].nameid;
  5114. memset(&tmp_item,0,sizeof(tmp_item));
  5115. tmp_item.nameid = itemid;
  5116. tmp_item.amount = 1;
  5117. tmp_item.identify = itemdb_isidentified(itemid);
  5118. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  5119. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5120. //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?
  5121. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5122. if(flag) { //Failed to steal due to overweight
  5123. clif_additem(sd,0,0,flag);
  5124. return false;
  5125. }
  5126. if(battle_config.show_steal_in_same_party)
  5127. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5128. //Logs items, Stolen from mobs [Lupus]
  5129. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5130. //A Rare Steal Global Announce by Lupus
  5131. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  5132. struct item_data *i_data;
  5133. char message[128];
  5134. i_data = itemdb_search(itemid);
  5135. sprintf (message, msg_txt(sd,542), (sd->status.name[0])?sd->status.name :"GM", md->db->jname, i_data->ename.c_str(), (float)md->db->dropitem[i].p/100);
  5136. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5137. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5138. }
  5139. return true;
  5140. }
  5141. /*==========================================
  5142. * Stole zeny from bl (mob)
  5143. * return
  5144. * 0 = fail
  5145. * 1 = success
  5146. *------------------------------------------*/
  5147. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  5148. {
  5149. int rate, target_lv;
  5150. struct mob_data *md;
  5151. if(!sd || !target || target->type != BL_MOB)
  5152. return 0;
  5153. md = (TBL_MOB*)target;
  5154. target_lv = status_get_lv(target);
  5155. if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || status_bl_has_mode(target,MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE)
  5156. return 0;
  5157. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5158. if(rnd()%1000 < rate)
  5159. {
  5160. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5161. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5162. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5163. md->state.steal_coin_flag = 1;
  5164. return 1;
  5165. }
  5166. return 0;
  5167. }
  5168. /*==========================================
  5169. * Set's a player position.
  5170. * @param sd
  5171. * @param mapindex
  5172. * @param x
  5173. * @param y
  5174. * @param clrtype
  5175. * @return SETPOS_OK Success
  5176. * SETPOS_MAPINDEX Invalid map index
  5177. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5178. * SETPOS_AUTOTRADE Player is in autotrade state
  5179. *------------------------------------------*/
  5180. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5181. {
  5182. nullpo_retr(SETPOS_OK,sd);
  5183. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5184. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5185. return SETPOS_MAPINDEX;
  5186. }
  5187. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5188. return SETPOS_AUTOTRADE;
  5189. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5190. pc_setstand(sd, true);
  5191. pc_setrestartvalue(sd,1);
  5192. }
  5193. int16 m = map_mapindex2mapid(mapindex);
  5194. struct map_data *mapdata = map_getmapdata(m);
  5195. status_change *sc = status_get_sc(&sd->bl);
  5196. sd->state.changemap = (sd->mapindex != mapindex);
  5197. sd->state.warping = 1;
  5198. sd->state.workinprogress = WIP_DISABLE_NONE;
  5199. sd->state.mail_writing = false;
  5200. if( sd->state.changemap ) { // Misc map-changing settings
  5201. int curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5202. if (curr_map_instance_id != new_map_instance_id) {
  5203. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5204. instance_delusers(curr_map_instance_id);
  5205. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5206. }
  5207. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5208. instance_addusers(new_map_instance_id);
  5209. }
  5210. if (sd->bg_id && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5211. bg_team_leave(sd, false, true);
  5212. sd->state.pmap = sd->bl.m;
  5213. if (sc && sc->count) { // Cancel some map related stuff.
  5214. if (sc->data[SC_JAILED])
  5215. return SETPOS_MAPINDEX; //You may not get out!
  5216. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  5217. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  5218. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  5219. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  5220. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  5221. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  5222. if (sc->data[SC_KNOWLEDGE]) {
  5223. struct status_change_entry *sce = sc->data[SC_KNOWLEDGE];
  5224. if (sce->timer != INVALID_TIMER)
  5225. delete_timer(sce->timer, status_change_timer);
  5226. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  5227. }
  5228. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  5229. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  5230. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  5231. status_change_end(&sd->bl, SC_SUHIDE, INVALID_TIMER);
  5232. }
  5233. for(int i = 0; i < EQI_MAX; i++ ) {
  5234. if( sd->equip_index[i] >= 0 )
  5235. if( pc_isequip(sd,sd->equip_index[i]) )
  5236. pc_unequipitem(sd,sd->equip_index[i],2);
  5237. }
  5238. if (battle_config.clear_unit_onwarp&BL_PC)
  5239. skill_clear_unitgroup(&sd->bl);
  5240. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5241. guild_send_dot_remove(sd);
  5242. bg_send_dot_remove(sd);
  5243. if (sd->regen.state.gc)
  5244. sd->regen.state.gc = 0;
  5245. // make sure vending is allowed here
  5246. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5247. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  5248. vending_closevending(sd);
  5249. }
  5250. channel_pcquit(sd,4); //quit map chan
  5251. // Remove Cloaked NPC if changing to another map
  5252. for (auto it = sd->cloaked_npc.begin(); it != sd->cloaked_npc.end(); ++it) {
  5253. block_list *npc_bl = map_id2bl(*it);
  5254. if (npc_bl && npc_bl->m != m) {
  5255. sd->cloaked_npc.erase(it);
  5256. break;
  5257. }
  5258. }
  5259. }
  5260. if( m < 0 )
  5261. {
  5262. uint32 ip;
  5263. uint16 port;
  5264. struct script_state *st;
  5265. //if can't find any map-servers, just abort setting position.
  5266. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5267. return SETPOS_NO_MAPSERVER;
  5268. if (sd->npc_id){
  5269. npc_event_dequeue(sd,false);
  5270. st = sd->st;
  5271. }else{
  5272. st = nullptr;
  5273. }
  5274. npc_script_event(sd, NPCE_LOGOUT);
  5275. //remove from map, THEN change x/y coordinates
  5276. unit_remove_map_pc(sd,clrtype);
  5277. sd->mapindex = mapindex;
  5278. sd->bl.x=x;
  5279. sd->bl.y=y;
  5280. pc_clean_skilltree(sd);
  5281. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5282. chrif_changemapserver(sd, ip, (short)port);
  5283. //Free session data from this map server [Kevin]
  5284. unit_free_pc(sd);
  5285. if( st ){
  5286. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5287. st->state = END;
  5288. }
  5289. return SETPOS_OK;
  5290. }
  5291. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5292. {
  5293. 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);
  5294. x = y = 0; // make it random
  5295. }
  5296. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5297. int c=0;
  5298. do {
  5299. x = rnd()%(mapdata->xs-2)+1;
  5300. y = rnd()%(mapdata->ys-2)+1;
  5301. c++;
  5302. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5303. 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));
  5304. return SETPOS_OK; //preventing warp
  5305. //break; //allow warp anyway
  5306. }
  5307. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5308. }
  5309. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5310. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5311. vending_closevending(sd);
  5312. }
  5313. if(sd->bl.prev != NULL){
  5314. unit_remove_map_pc(sd,clrtype);
  5315. clif_changemap(sd,m,x,y); // [MouseJstr]
  5316. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5317. sd->state.rewarp = 1;
  5318. sd->mapindex = mapindex;
  5319. sd->bl.m = m;
  5320. sd->bl.x = sd->ud.to_x = x;
  5321. sd->bl.y = sd->ud.to_y = y;
  5322. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5323. { // Increased guild castle regen [Valaris]
  5324. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5325. if(gc && gc->guild_id == sd->status.guild_id)
  5326. sd->regen.state.gc = 1;
  5327. }
  5328. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5329. {
  5330. sd->pd->bl.m = m;
  5331. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5332. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5333. sd->pd->ud.dir = sd->ud.dir;
  5334. }
  5335. if( hom_is_active(sd->hd) )
  5336. {
  5337. sd->hd->bl.m = m;
  5338. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5339. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5340. sd->hd->ud.dir = sd->ud.dir;
  5341. }
  5342. if( sd->md )
  5343. {
  5344. sd->md->bl.m = m;
  5345. sd->md->bl.x = sd->md->ud.to_x = x;
  5346. sd->md->bl.y = sd->md->ud.to_y = y;
  5347. sd->md->ud.dir = sd->ud.dir;
  5348. }
  5349. if( sd->ed ) {
  5350. sd->ed->bl.m = m;
  5351. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5352. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5353. sd->ed->ud.dir = sd->ud.dir;
  5354. }
  5355. pc_cell_basilica(sd);
  5356. //check if we gonna be rewarped [lighta]
  5357. if(npc_check_areanpc(1,m,x,y,0)){
  5358. sd->count_rewarp++;
  5359. }
  5360. else
  5361. sd->count_rewarp = 0;
  5362. return SETPOS_OK;
  5363. }
  5364. /*==========================================
  5365. * Warp player sd to random location on current map.
  5366. * May fail if no walkable cell found (1000 attempts).
  5367. * Return:
  5368. * 0 = Success
  5369. * 1,2,3 = Fail
  5370. *------------------------------------------*/
  5371. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5372. {
  5373. int x,y,i=0;
  5374. nullpo_ret(sd);
  5375. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5376. if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
  5377. return 3;
  5378. do {
  5379. x = rnd()%(mapdata->xs-2)+1;
  5380. y = rnd()%(mapdata->ys-2)+1;
  5381. } 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);
  5382. if (i < 1000)
  5383. return pc_setpos(sd,mapdata->index,x,y,type);
  5384. return 3;
  5385. }
  5386. /*==========================================
  5387. * Records a memo point at sd's current position
  5388. * pos - entry to replace, (-1: shift oldest entry out)
  5389. *------------------------------------------*/
  5390. bool pc_memo(struct map_session_data* sd, int pos)
  5391. {
  5392. int skill;
  5393. nullpo_ret(sd);
  5394. // check mapflags
  5395. 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) ) {
  5396. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5397. return false;
  5398. }
  5399. // check inputs
  5400. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5401. return false; // invalid input
  5402. // check required skill level
  5403. skill = pc_checkskill(sd, AL_WARP);
  5404. if( skill < 1 ) {
  5405. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5406. return false;
  5407. }
  5408. if( skill < 2 || skill - 2 < pos ) {
  5409. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5410. return false;
  5411. }
  5412. if( pos == -1 )
  5413. {
  5414. uint8 i;
  5415. // prevent memo-ing the same map multiple times
  5416. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5417. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5418. pos = 0;
  5419. }
  5420. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5421. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5422. return false;
  5423. }
  5424. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5425. sd->status.memo_point[pos].x = sd->bl.x;
  5426. sd->status.memo_point[pos].y = sd->bl.y;
  5427. clif_skill_memomessage(sd, 0);
  5428. return true;
  5429. }
  5430. //
  5431. // Skills
  5432. //
  5433. /**
  5434. * Get the skill current cooldown for player.
  5435. * (get the db base cooldown for skill + player specific cooldown)
  5436. * @param sd : player pointer
  5437. * @param id : skill id
  5438. * @param lv : skill lv
  5439. * @return player skill cooldown
  5440. */
  5441. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5442. if (skill_id == SJ_NOVAEXPLOSING) {
  5443. struct status_change *sc = status_get_sc(&sd->bl);
  5444. if (sc && sc->data[SC_DIMENSION])
  5445. return 0;
  5446. }
  5447. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  5448. if (cooldown == 0)
  5449. return 0;
  5450. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5451. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5452. for (auto &it : sd->skillcooldown) {
  5453. if (it.id == skill_id) {
  5454. cooldown += it.val;
  5455. cooldown = max(0, cooldown);
  5456. break;
  5457. }
  5458. }
  5459. return cooldown;
  5460. }
  5461. /*==========================================
  5462. * Return player sd skill_lv learned for given skill
  5463. *------------------------------------------*/
  5464. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5465. {
  5466. uint16 idx = 0;
  5467. if (sd == NULL)
  5468. return 0;
  5469. if ((idx = skill_get_index(skill_id)) == 0) {
  5470. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5471. return 0;
  5472. }
  5473. if (SKILL_CHK_GUILD(skill_id) ) {
  5474. struct guild *g;
  5475. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5476. return guild_checkskill(g,skill_id);
  5477. return 0;
  5478. }
  5479. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5480. }
  5481. /**
  5482. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  5483. * @param sd: Player data
  5484. * @param type: Summoner Power Type
  5485. * @return Skill points invested
  5486. */
  5487. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  5488. if (sd == nullptr)
  5489. return 0;
  5490. uint8 count = 0;
  5491. switch (type) {
  5492. case SUMMONER_POWER_SEA:
  5493. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  5494. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  5495. break;
  5496. case SUMMONER_POWER_LAND:
  5497. 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) +
  5498. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  5499. break;
  5500. case SUMMONER_POWER_LIFE:
  5501. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  5502. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  5503. break;
  5504. }
  5505. return count;
  5506. }
  5507. /**
  5508. * Check if we still have the correct weapon to continue the skill (actually status)
  5509. * If not ending it
  5510. * @param sd
  5511. * @return 0:error, 1:check done
  5512. */
  5513. static void pc_checkallowskill(struct map_session_data *sd)
  5514. {
  5515. const enum sc_type scw_list[] = {
  5516. SC_TWOHANDQUICKEN,
  5517. SC_ONEHAND,
  5518. SC_AURABLADE,
  5519. SC_PARRYING,
  5520. SC_SPEARQUICKEN,
  5521. SC_ADRENALINE,
  5522. SC_ADRENALINE2,
  5523. SC_DANCING,
  5524. SC_GATLINGFEVER,
  5525. };
  5526. uint8 i;
  5527. nullpo_retv(sd);
  5528. if(!sd->sc.count)
  5529. return;
  5530. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5531. { // Skills requiring specific weapon types
  5532. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5533. continue;
  5534. if(sd->sc.data[scw_list[i]] &&
  5535. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5536. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5537. }
  5538. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5539. // Spurt requires bare hands (feet, in fact xD)
  5540. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5541. if(sd->status.shield <= 0) { // Skills requiring a shield
  5542. const enum sc_type scs_list[] = {
  5543. SC_AUTOGUARD,
  5544. SC_DEFENDER,
  5545. SC_REFLECTSHIELD,
  5546. SC_REFLECTDAMAGE
  5547. };
  5548. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5549. if(sd->sc.data[scs_list[i]])
  5550. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5551. }
  5552. }
  5553. /*==========================================
  5554. * Return equipped index of item on player sd at pos
  5555. * Return
  5556. * -1 : Nothing equipped
  5557. * idx : (this index could be used in inventory to found item_data)
  5558. *------------------------------------------*/
  5559. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  5560. {
  5561. uint8 i;
  5562. nullpo_retr(-1, sd);
  5563. for(i=0;i<EQI_MAX;i++){
  5564. if(pos & equip_bitmask[i]){
  5565. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  5566. // Check all if any match is found
  5567. continue;
  5568. }
  5569. return sd->equip_index[i];
  5570. }
  5571. }
  5572. return -1;
  5573. }
  5574. /*==========================================
  5575. * Check if sd has nameid equipped somewhere
  5576. * @sd : the player session
  5577. * @nameid : id of the item to check
  5578. * @min : : see pc.hpp enum equip_index from ? to @max
  5579. * @max : see pc.hpp enum equip_index for @min to ?
  5580. * -return true,false
  5581. *------------------------------------------*/
  5582. bool pc_checkequip2(struct map_session_data *sd, t_itemid nameid, int min, int max)
  5583. {
  5584. int i;
  5585. for(i = min; i < max; i++) {
  5586. if(equip_bitmask[i]) {
  5587. int idx = sd->equip_index[i];
  5588. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5589. return true;
  5590. }
  5591. }
  5592. return false;
  5593. }
  5594. /*==========================================
  5595. * Convert's from the client's lame Job ID system
  5596. * to the map server's 'makes sense' system. [Skotlex]
  5597. *------------------------------------------*/
  5598. int pc_jobid2mapid(unsigned short b_class)
  5599. {
  5600. switch(b_class)
  5601. {
  5602. //Novice And 1-1 Jobs
  5603. case JOB_NOVICE: return MAPID_NOVICE;
  5604. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5605. case JOB_MAGE: return MAPID_MAGE;
  5606. case JOB_ARCHER: return MAPID_ARCHER;
  5607. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5608. case JOB_MERCHANT: return MAPID_MERCHANT;
  5609. case JOB_THIEF: return MAPID_THIEF;
  5610. case JOB_TAEKWON: return MAPID_TAEKWON;
  5611. case JOB_WEDDING: return MAPID_WEDDING;
  5612. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5613. case JOB_NINJA: return MAPID_NINJA;
  5614. case JOB_XMAS: return MAPID_XMAS;
  5615. case JOB_SUMMER: return MAPID_SUMMER;
  5616. case JOB_HANBOK: return MAPID_HANBOK;
  5617. case JOB_GANGSI: return MAPID_GANGSI;
  5618. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5619. case JOB_SUMMER2: return MAPID_SUMMER2;
  5620. //2-1 Jobs
  5621. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5622. case JOB_KNIGHT: return MAPID_KNIGHT;
  5623. case JOB_WIZARD: return MAPID_WIZARD;
  5624. case JOB_HUNTER: return MAPID_HUNTER;
  5625. case JOB_PRIEST: return MAPID_PRIEST;
  5626. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5627. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5628. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5629. case JOB_KAGEROU:
  5630. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5631. case JOB_REBELLION: return MAPID_REBELLION;
  5632. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5633. //2-2 Jobs
  5634. case JOB_CRUSADER: return MAPID_CRUSADER;
  5635. case JOB_SAGE: return MAPID_SAGE;
  5636. case JOB_BARD:
  5637. case JOB_DANCER: return MAPID_BARDDANCER;
  5638. case JOB_MONK: return MAPID_MONK;
  5639. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5640. case JOB_ROGUE: return MAPID_ROGUE;
  5641. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5642. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5643. //Trans Novice And Trans 1-1 Jobs
  5644. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5645. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5646. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5647. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5648. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5649. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5650. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5651. //Trans 2-1 Jobs
  5652. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5653. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5654. case JOB_SNIPER: return MAPID_SNIPER;
  5655. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5656. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5657. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5658. //Trans 2-2 Jobs
  5659. case JOB_PALADIN: return MAPID_PALADIN;
  5660. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5661. case JOB_CLOWN:
  5662. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5663. case JOB_CHAMPION: return MAPID_CHAMPION;
  5664. case JOB_CREATOR: return MAPID_CREATOR;
  5665. case JOB_STALKER: return MAPID_STALKER;
  5666. //Baby Novice And Baby 1-1 Jobs
  5667. case JOB_BABY: return MAPID_BABY;
  5668. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5669. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5670. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5671. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5672. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5673. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5674. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5675. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5676. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5677. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5678. //Baby 2-1 Jobs
  5679. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5680. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5681. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5682. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5683. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5684. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5685. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5686. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5687. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5688. case JOB_BABY_KAGEROU:
  5689. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5690. //Baby 2-2 Jobs
  5691. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5692. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5693. case JOB_BABY_BARD:
  5694. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5695. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5696. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5697. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5698. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5699. //3-1 Jobs
  5700. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5701. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5702. case JOB_WARLOCK: return MAPID_WARLOCK;
  5703. case JOB_RANGER: return MAPID_RANGER;
  5704. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5705. case JOB_MECHANIC: return MAPID_MECHANIC;
  5706. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5707. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5708. //3-2 Jobs
  5709. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5710. case JOB_SORCERER: return MAPID_SORCERER;
  5711. case JOB_MINSTREL:
  5712. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5713. case JOB_SURA: return MAPID_SURA;
  5714. case JOB_GENETIC: return MAPID_GENETIC;
  5715. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5716. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5717. //Trans 3-1 Jobs
  5718. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5719. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5720. case JOB_RANGER_T: return MAPID_RANGER_T;
  5721. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5722. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5723. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5724. //Trans 3-2 Jobs
  5725. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5726. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5727. case JOB_MINSTREL_T:
  5728. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5729. case JOB_SURA_T: return MAPID_SURA_T;
  5730. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5731. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5732. //Baby 3-1 Jobs
  5733. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5734. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  5735. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5736. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5737. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  5738. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5739. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  5740. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5741. //Baby 3-2 Jobs
  5742. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  5743. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5744. case JOB_BABY_MINSTREL:
  5745. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5746. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5747. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5748. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  5749. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5750. //Doram Jobs
  5751. case JOB_SUMMONER: return MAPID_SUMMONER;
  5752. default:
  5753. return -1;
  5754. }
  5755. }
  5756. //Reverts the map-style class id to the client-style one.
  5757. int pc_mapid2jobid(unsigned short class_, int sex)
  5758. {
  5759. switch(class_) {
  5760. //Novice And 1-1 Jobs
  5761. case MAPID_NOVICE: return JOB_NOVICE;
  5762. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5763. case MAPID_MAGE: return JOB_MAGE;
  5764. case MAPID_ARCHER: return JOB_ARCHER;
  5765. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5766. case MAPID_MERCHANT: return JOB_MERCHANT;
  5767. case MAPID_THIEF: return JOB_THIEF;
  5768. case MAPID_TAEKWON: return JOB_TAEKWON;
  5769. case MAPID_WEDDING: return JOB_WEDDING;
  5770. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5771. case MAPID_NINJA: return JOB_NINJA;
  5772. case MAPID_XMAS: return JOB_XMAS;
  5773. case MAPID_SUMMER: return JOB_SUMMER;
  5774. case MAPID_HANBOK: return JOB_HANBOK;
  5775. case MAPID_GANGSI: return JOB_GANGSI;
  5776. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5777. case MAPID_SUMMER2: return JOB_SUMMER2;
  5778. //2-1 Jobs
  5779. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5780. case MAPID_KNIGHT: return JOB_KNIGHT;
  5781. case MAPID_WIZARD: return JOB_WIZARD;
  5782. case MAPID_HUNTER: return JOB_HUNTER;
  5783. case MAPID_PRIEST: return JOB_PRIEST;
  5784. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5785. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5786. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5787. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5788. case MAPID_REBELLION: return JOB_REBELLION;
  5789. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5790. //2-2 Jobs
  5791. case MAPID_CRUSADER: return JOB_CRUSADER;
  5792. case MAPID_SAGE: return JOB_SAGE;
  5793. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5794. case MAPID_MONK: return JOB_MONK;
  5795. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5796. case MAPID_ROGUE: return JOB_ROGUE;
  5797. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5798. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5799. //Trans Novice And Trans 2-1 Jobs
  5800. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5801. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5802. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5803. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5804. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5805. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5806. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5807. //Trans 2-1 Jobs
  5808. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5809. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5810. case MAPID_SNIPER: return JOB_SNIPER;
  5811. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5812. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5813. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5814. //Trans 2-2 Jobs
  5815. case MAPID_PALADIN: return JOB_PALADIN;
  5816. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5817. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5818. case MAPID_CHAMPION: return JOB_CHAMPION;
  5819. case MAPID_CREATOR: return JOB_CREATOR;
  5820. case MAPID_STALKER: return JOB_STALKER;
  5821. //Baby Novice And Baby 1-1 Jobs
  5822. case MAPID_BABY: return JOB_BABY;
  5823. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5824. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5825. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5826. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5827. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5828. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5829. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5830. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5831. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5832. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5833. //Baby 2-1 Jobs
  5834. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5835. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5836. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5837. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5838. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5839. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5840. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5841. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5842. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5843. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5844. //Baby 2-2 Jobs
  5845. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5846. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5847. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5848. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5849. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5850. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5851. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5852. //3-1 Jobs
  5853. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5854. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5855. case MAPID_WARLOCK: return JOB_WARLOCK;
  5856. case MAPID_RANGER: return JOB_RANGER;
  5857. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5858. case MAPID_MECHANIC: return JOB_MECHANIC;
  5859. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5860. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5861. //3-2 Jobs
  5862. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5863. case MAPID_SORCERER: return JOB_SORCERER;
  5864. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5865. case MAPID_SURA: return JOB_SURA;
  5866. case MAPID_GENETIC: return JOB_GENETIC;
  5867. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5868. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5869. //Trans 3-1 Jobs
  5870. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5871. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5872. case MAPID_RANGER_T: return JOB_RANGER_T;
  5873. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5874. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5875. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5876. //Trans 3-2 Jobs
  5877. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5878. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5879. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5880. case MAPID_SURA_T: return JOB_SURA_T;
  5881. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5882. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5883. //Baby 3-1 Jobs
  5884. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5885. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  5886. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5887. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5888. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  5889. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5890. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  5891. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5892. //Baby 3-2 Jobs
  5893. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  5894. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5895. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5896. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5897. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5898. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  5899. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5900. //Doram Jobs
  5901. case MAPID_SUMMONER: return JOB_SUMMONER;
  5902. default:
  5903. return -1;
  5904. }
  5905. }
  5906. /*====================================================
  5907. * This function return the name of the job (by [Yor])
  5908. *----------------------------------------------------*/
  5909. const char* job_name(int class_)
  5910. {
  5911. switch (class_) {
  5912. case JOB_NOVICE:
  5913. case JOB_SWORDMAN:
  5914. case JOB_MAGE:
  5915. case JOB_ARCHER:
  5916. case JOB_ACOLYTE:
  5917. case JOB_MERCHANT:
  5918. case JOB_THIEF:
  5919. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5920. case JOB_KNIGHT:
  5921. case JOB_PRIEST:
  5922. case JOB_WIZARD:
  5923. case JOB_BLACKSMITH:
  5924. case JOB_HUNTER:
  5925. case JOB_ASSASSIN:
  5926. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5927. case JOB_KNIGHT2:
  5928. return msg_txt(NULL,557);
  5929. case JOB_CRUSADER:
  5930. case JOB_MONK:
  5931. case JOB_SAGE:
  5932. case JOB_ROGUE:
  5933. case JOB_ALCHEMIST:
  5934. case JOB_BARD:
  5935. case JOB_DANCER:
  5936. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5937. case JOB_CRUSADER2:
  5938. return msg_txt(NULL,563);
  5939. case JOB_WEDDING:
  5940. case JOB_SUPER_NOVICE:
  5941. case JOB_GUNSLINGER:
  5942. case JOB_NINJA:
  5943. case JOB_XMAS:
  5944. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5945. case JOB_SUMMER:
  5946. case JOB_SUMMER2:
  5947. return msg_txt(NULL,621);
  5948. case JOB_HANBOK:
  5949. return msg_txt(NULL,694);
  5950. case JOB_OKTOBERFEST:
  5951. return msg_txt(NULL,696);
  5952. case JOB_NOVICE_HIGH:
  5953. case JOB_SWORDMAN_HIGH:
  5954. case JOB_MAGE_HIGH:
  5955. case JOB_ARCHER_HIGH:
  5956. case JOB_ACOLYTE_HIGH:
  5957. case JOB_MERCHANT_HIGH:
  5958. case JOB_THIEF_HIGH:
  5959. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5960. case JOB_LORD_KNIGHT:
  5961. case JOB_HIGH_PRIEST:
  5962. case JOB_HIGH_WIZARD:
  5963. case JOB_WHITESMITH:
  5964. case JOB_SNIPER:
  5965. case JOB_ASSASSIN_CROSS:
  5966. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5967. case JOB_LORD_KNIGHT2:
  5968. return msg_txt(NULL,582);
  5969. case JOB_PALADIN:
  5970. case JOB_CHAMPION:
  5971. case JOB_PROFESSOR:
  5972. case JOB_STALKER:
  5973. case JOB_CREATOR:
  5974. case JOB_CLOWN:
  5975. case JOB_GYPSY:
  5976. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5977. case JOB_PALADIN2:
  5978. return msg_txt(NULL,588);
  5979. case JOB_BABY:
  5980. case JOB_BABY_SWORDMAN:
  5981. case JOB_BABY_MAGE:
  5982. case JOB_BABY_ARCHER:
  5983. case JOB_BABY_ACOLYTE:
  5984. case JOB_BABY_MERCHANT:
  5985. case JOB_BABY_THIEF:
  5986. return msg_txt(NULL,595 - JOB_BABY + class_);
  5987. case JOB_BABY_KNIGHT:
  5988. case JOB_BABY_PRIEST:
  5989. case JOB_BABY_WIZARD:
  5990. case JOB_BABY_BLACKSMITH:
  5991. case JOB_BABY_HUNTER:
  5992. case JOB_BABY_ASSASSIN:
  5993. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5994. case JOB_BABY_KNIGHT2:
  5995. return msg_txt(NULL,602);
  5996. case JOB_BABY_CRUSADER:
  5997. case JOB_BABY_MONK:
  5998. case JOB_BABY_SAGE:
  5999. case JOB_BABY_ROGUE:
  6000. case JOB_BABY_ALCHEMIST:
  6001. case JOB_BABY_BARD:
  6002. case JOB_BABY_DANCER:
  6003. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  6004. case JOB_BABY_CRUSADER2:
  6005. return msg_txt(NULL,608);
  6006. case JOB_SUPER_BABY:
  6007. return msg_txt(NULL,615);
  6008. case JOB_TAEKWON:
  6009. return msg_txt(NULL,616);
  6010. case JOB_STAR_GLADIATOR:
  6011. case JOB_STAR_GLADIATOR2:
  6012. return msg_txt(NULL,617);
  6013. case JOB_SOUL_LINKER:
  6014. return msg_txt(NULL,618);
  6015. case JOB_GANGSI:
  6016. case JOB_DEATH_KNIGHT:
  6017. case JOB_DARK_COLLECTOR:
  6018. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  6019. case JOB_RUNE_KNIGHT:
  6020. case JOB_WARLOCK:
  6021. case JOB_RANGER:
  6022. case JOB_ARCH_BISHOP:
  6023. case JOB_MECHANIC:
  6024. case JOB_GUILLOTINE_CROSS:
  6025. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  6026. case JOB_RUNE_KNIGHT_T:
  6027. case JOB_WARLOCK_T:
  6028. case JOB_RANGER_T:
  6029. case JOB_ARCH_BISHOP_T:
  6030. case JOB_MECHANIC_T:
  6031. case JOB_GUILLOTINE_CROSS_T:
  6032. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  6033. case JOB_ROYAL_GUARD:
  6034. case JOB_SORCERER:
  6035. case JOB_MINSTREL:
  6036. case JOB_WANDERER:
  6037. case JOB_SURA:
  6038. case JOB_GENETIC:
  6039. case JOB_SHADOW_CHASER:
  6040. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  6041. case JOB_ROYAL_GUARD_T:
  6042. case JOB_SORCERER_T:
  6043. case JOB_MINSTREL_T:
  6044. case JOB_WANDERER_T:
  6045. case JOB_SURA_T:
  6046. case JOB_GENETIC_T:
  6047. case JOB_SHADOW_CHASER_T:
  6048. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  6049. case JOB_RUNE_KNIGHT2:
  6050. case JOB_RUNE_KNIGHT_T2:
  6051. return msg_txt(NULL,625);
  6052. case JOB_ROYAL_GUARD2:
  6053. case JOB_ROYAL_GUARD_T2:
  6054. return msg_txt(NULL,631);
  6055. case JOB_RANGER2:
  6056. case JOB_RANGER_T2:
  6057. return msg_txt(NULL,627);
  6058. case JOB_MECHANIC2:
  6059. case JOB_MECHANIC_T2:
  6060. return msg_txt(NULL,629);
  6061. case JOB_BABY_RUNE_KNIGHT:
  6062. case JOB_BABY_WARLOCK:
  6063. case JOB_BABY_RANGER:
  6064. case JOB_BABY_ARCH_BISHOP:
  6065. case JOB_BABY_MECHANIC:
  6066. case JOB_BABY_GUILLOTINE_CROSS:
  6067. case JOB_BABY_ROYAL_GUARD:
  6068. case JOB_BABY_SORCERER:
  6069. case JOB_BABY_MINSTREL:
  6070. case JOB_BABY_WANDERER:
  6071. case JOB_BABY_SURA:
  6072. case JOB_BABY_GENETIC:
  6073. case JOB_BABY_SHADOW_CHASER:
  6074. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6075. case JOB_BABY_RUNE_KNIGHT2:
  6076. return msg_txt(NULL,638);
  6077. case JOB_BABY_ROYAL_GUARD2:
  6078. return msg_txt(NULL,644);
  6079. case JOB_BABY_RANGER2:
  6080. return msg_txt(NULL,640);
  6081. case JOB_BABY_MECHANIC2:
  6082. return msg_txt(NULL,642);
  6083. case JOB_SUPER_NOVICE_E:
  6084. case JOB_SUPER_BABY_E:
  6085. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  6086. case JOB_KAGEROU:
  6087. case JOB_OBORO:
  6088. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  6089. case JOB_REBELLION:
  6090. return msg_txt(NULL,695);
  6091. case JOB_SUMMONER:
  6092. return msg_txt(NULL,697);
  6093. case JOB_BABY_SUMMONER:
  6094. return msg_txt(NULL,698);
  6095. case JOB_BABY_NINJA:
  6096. return msg_txt(NULL,699);
  6097. case JOB_BABY_KAGEROU:
  6098. case JOB_BABY_OBORO:
  6099. case JOB_BABY_TAEKWON:
  6100. case JOB_BABY_STAR_GLADIATOR:
  6101. case JOB_BABY_SOUL_LINKER:
  6102. case JOB_BABY_GUNSLINGER:
  6103. case JOB_BABY_REBELLION:
  6104. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  6105. case JOB_BABY_STAR_GLADIATOR2:
  6106. return msg_txt(NULL,756);
  6107. case JOB_STAR_EMPEROR:
  6108. case JOB_SOUL_REAPER:
  6109. case JOB_BABY_STAR_EMPEROR:
  6110. case JOB_BABY_SOUL_REAPER:
  6111. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  6112. case JOB_STAR_EMPEROR2:
  6113. return msg_txt(NULL,782);
  6114. case JOB_BABY_STAR_EMPEROR2:
  6115. return msg_txt(NULL,784);
  6116. default:
  6117. return msg_txt(NULL,655);
  6118. }
  6119. }
  6120. /*====================================================
  6121. * Timered function to make id follow a target.
  6122. * @id = bl.id (player only atm)
  6123. * target is define in sd->followtarget (bl.id)
  6124. * used by pc_follow
  6125. *----------------------------------------------------*/
  6126. TIMER_FUNC(pc_follow_timer){
  6127. struct map_session_data *sd;
  6128. struct block_list *tbl;
  6129. sd = map_id2sd(id);
  6130. nullpo_ret(sd);
  6131. if (sd->followtimer != tid){
  6132. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6133. sd->followtimer = INVALID_TIMER;
  6134. return 0;
  6135. }
  6136. sd->followtimer = INVALID_TIMER;
  6137. tbl = map_id2bl(sd->followtarget);
  6138. if (tbl == NULL || pc_isdead(sd))
  6139. {
  6140. pc_stop_following(sd);
  6141. return 0;
  6142. }
  6143. // either player or target is currently detached from map blocks (could be teleporting),
  6144. // but still connected to this map, so we'll just increment the timer and check back later
  6145. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  6146. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6147. {
  6148. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  6149. if (!check_distance_bl(&sd->bl, tbl, 5))
  6150. unit_walktobl(&sd->bl, tbl, 5, 0);
  6151. } else
  6152. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  6153. }
  6154. sd->followtimer = add_timer(
  6155. tick + 1000, // increase time a bit to loosen up map's load
  6156. pc_follow_timer, sd->bl.id, 0);
  6157. return 0;
  6158. }
  6159. int pc_stop_following (struct map_session_data *sd)
  6160. {
  6161. nullpo_ret(sd);
  6162. if (sd->followtimer != INVALID_TIMER) {
  6163. delete_timer(sd->followtimer,pc_follow_timer);
  6164. sd->followtimer = INVALID_TIMER;
  6165. }
  6166. sd->followtarget = -1;
  6167. sd->ud.target_to = 0;
  6168. unit_stop_walking(&sd->bl, 1);
  6169. return 0;
  6170. }
  6171. int pc_follow(struct map_session_data *sd,int target_id)
  6172. {
  6173. struct block_list *bl = map_id2bl(target_id);
  6174. if (bl == NULL /*|| bl->type != BL_PC*/)
  6175. return 1;
  6176. if (sd->followtimer != INVALID_TIMER)
  6177. pc_stop_following(sd);
  6178. sd->followtarget = target_id;
  6179. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6180. return 0;
  6181. }
  6182. int pc_checkbaselevelup(struct map_session_data *sd) {
  6183. t_exp next = pc_nextbaseexp(sd);
  6184. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  6185. return 0;
  6186. uint32 base_level = sd->status.base_level;
  6187. do {
  6188. sd->status.base_exp -= next;
  6189. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6190. 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 )
  6191. sd->status.base_exp = next-1;
  6192. sd->status.status_point += pc_gets_status_point(sd->status.base_level++);
  6193. if( pc_is_maxbaselv(sd) ){
  6194. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  6195. break;
  6196. }
  6197. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  6198. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  6199. status_calc_pet(sd->pd,SCO_NONE);
  6200. clif_updatestatus(sd,SP_STATUSPOINT);
  6201. clif_updatestatus(sd,SP_BASELEVEL);
  6202. clif_updatestatus(sd,SP_BASEEXP);
  6203. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6204. status_calc_pc(sd,SCO_FORCE);
  6205. status_percent_heal(&sd->bl,100,100);
  6206. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6207. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  6208. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  6209. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  6210. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  6211. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  6212. if (sd->state.snovice_dead_flag)
  6213. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6214. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6215. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  6216. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  6217. }
  6218. clif_misceffect(&sd->bl,0);
  6219. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6220. if(sd->status.party_id)
  6221. party_send_levelup(sd);
  6222. pc_baselevelchanged(sd);
  6223. for (; base_level <= sd->status.base_level; base_level++) {
  6224. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  6225. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  6226. }
  6227. return 1;
  6228. }
  6229. void pc_baselevelchanged(struct map_session_data *sd) {
  6230. uint8 i;
  6231. for( i = 0; i < EQI_MAX; i++ ) {
  6232. if( sd->equip_index[i] >= 0 ) {
  6233. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6234. pc_unequipitem(sd, sd->equip_index[i], 3);
  6235. }
  6236. }
  6237. pc_show_questinfo(sd);
  6238. }
  6239. int pc_checkjoblevelup(struct map_session_data *sd)
  6240. {
  6241. t_exp next = pc_nextjobexp(sd);
  6242. nullpo_ret(sd);
  6243. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6244. return 0;
  6245. uint32 job_level = sd->status.job_level;
  6246. do {
  6247. sd->status.job_exp -= next;
  6248. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6249. 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 )
  6250. sd->status.job_exp = next-1;
  6251. sd->status.job_level ++;
  6252. sd->status.skill_point ++;
  6253. if( pc_is_maxjoblv(sd) ){
  6254. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6255. break;
  6256. }
  6257. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6258. clif_updatestatus(sd,SP_JOBLEVEL);
  6259. clif_updatestatus(sd,SP_JOBEXP);
  6260. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6261. clif_updatestatus(sd,SP_SKILLPOINT);
  6262. status_calc_pc(sd,SCO_FORCE);
  6263. clif_misceffect(&sd->bl,1);
  6264. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6265. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6266. npc_script_event(sd, NPCE_JOBLVUP);
  6267. for (; job_level <= sd->status.job_level; job_level++)
  6268. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  6269. pc_show_questinfo(sd);
  6270. return 1;
  6271. }
  6272. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6273. * @param sd Player
  6274. * @param base_exp Base EXP before peronal bonuses
  6275. * @param job_exp Job EXP before peronal bonuses
  6276. * @param src Block list that affecting the exp calculation
  6277. */
  6278. static void pc_calcexp(struct map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  6279. {
  6280. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6281. if (src) {
  6282. struct status_data *status = status_get_status_data(src);
  6283. if( sd->indexed_bonus.expaddrace[status->race] )
  6284. bonus += sd->indexed_bonus.expaddrace[status->race];
  6285. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  6286. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  6287. if( sd->indexed_bonus.expaddclass[status->class_] )
  6288. bonus += sd->indexed_bonus.expaddclass[status->class_];
  6289. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  6290. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  6291. if (battle_config.pk_mode &&
  6292. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6293. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6294. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6295. vip_bonus_base = battle_config.vip_base_exp_increase;
  6296. vip_bonus_job = battle_config.vip_job_exp_increase;
  6297. }
  6298. }
  6299. // Give EXPBOOST for quests even if src is NULL.
  6300. if (sd->sc.data[SC_EXPBOOST]) {
  6301. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6302. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6303. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6304. }
  6305. if (*base_exp) {
  6306. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  6307. *base_exp = cap_value(exp, 1, MAX_EXP);
  6308. }
  6309. // Give JEXPBOOST for quests even if src is NULL.
  6310. if (sd->sc.data[SC_JEXPBOOST])
  6311. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6312. if (*job_exp) {
  6313. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  6314. *job_exp = cap_value(exp, 1, MAX_EXP);
  6315. }
  6316. return;
  6317. }
  6318. /**
  6319. * Show EXP gained by player in percentage by @showexp
  6320. * @param sd Player
  6321. * @param base_exp Base EXP gained/loss
  6322. * @param next_base_exp Base EXP needed for next base level
  6323. * @param job_exp Job EXP gained/loss
  6324. * @param next_job_exp Job EXP needed for next job level
  6325. * @param lost True:EXP penalty, lose EXP
  6326. **/
  6327. void pc_gainexp_disp(struct map_session_data *sd, t_exp base_exp, t_exp next_base_exp, t_exp job_exp, t_exp next_job_exp, bool lost) {
  6328. char output[CHAT_SIZE_MAX];
  6329. nullpo_retv(sd);
  6330. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6331. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6332. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6333. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6334. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6335. }
  6336. /**
  6337. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6338. * @param sd Player
  6339. * @param src EXP source
  6340. * @param base_exp Base EXP gained
  6341. * @param base_exp Job EXP gained
  6342. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6343. * @return
  6344. **/
  6345. void pc_gainexp(struct map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  6346. {
  6347. t_exp nextb = 0, nextj = 0;
  6348. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6349. nullpo_retv(sd);
  6350. if(sd->bl.prev == NULL || pc_isdead(sd))
  6351. return;
  6352. if (!(exp_flag&2)) {
  6353. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6354. return; // no exp on pvp maps
  6355. if (sd->status.guild_id>0)
  6356. base_exp -= guild_payexp(sd,base_exp);
  6357. }
  6358. flag = ((base_exp) ? 1 : 0) |
  6359. ((job_exp) ? 2 : 0) |
  6360. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6361. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6362. if (!(exp_flag&2))
  6363. pc_calcexp(sd, &base_exp, &job_exp, src);
  6364. nextb = pc_nextbaseexp(sd);
  6365. nextj = pc_nextjobexp(sd);
  6366. if (flag&4){
  6367. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6368. base_exp = 0;
  6369. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6370. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6371. }
  6372. if (flag&8){
  6373. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6374. job_exp = 0;
  6375. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6376. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6377. }
  6378. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6379. //Note that this value should never be greater than the original
  6380. //therefore no overflow checks are needed. [Skotlex]
  6381. if (nextb > 0) {
  6382. float nextbp = (float) base_exp / (float) nextb;
  6383. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6384. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6385. }
  6386. if (nextj > 0) {
  6387. float nextjp = (float) job_exp / (float) nextj;
  6388. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6389. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6390. }
  6391. }
  6392. // Give EXP for Base Level
  6393. if (base_exp) {
  6394. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  6395. if (!pc_checkbaselevelup(sd))
  6396. clif_updatestatus(sd,SP_BASEEXP);
  6397. }
  6398. // Give EXP for Job Level
  6399. if (job_exp) {
  6400. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  6401. if (!pc_checkjoblevelup(sd))
  6402. clif_updatestatus(sd,SP_JOBEXP);
  6403. }
  6404. if (flag&1)
  6405. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6406. if (flag&2)
  6407. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6408. if (sd->state.showexp && (base_exp || job_exp))
  6409. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6410. }
  6411. /**
  6412. * Lost Base/Job EXP from a player
  6413. * @param sd Player
  6414. * @param base_exp Base EXP lost
  6415. * @param job_exp Job EXP lost
  6416. **/
  6417. void pc_lostexp(struct map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  6418. nullpo_retv(sd);
  6419. if (base_exp) {
  6420. base_exp = u64min(sd->status.base_exp, base_exp);
  6421. sd->status.base_exp -= base_exp;
  6422. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6423. clif_updatestatus(sd, SP_BASEEXP);
  6424. }
  6425. if (job_exp) {
  6426. job_exp = u64min(sd->status.job_exp, job_exp);
  6427. sd->status.job_exp -= job_exp;
  6428. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6429. clif_updatestatus(sd, SP_JOBEXP);
  6430. }
  6431. if (sd->state.showexp && (base_exp || job_exp))
  6432. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6433. }
  6434. /**
  6435. * Returns max base level for this character's class.
  6436. * @param class_: Player's class
  6437. * @return Max Base Level
  6438. */
  6439. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6440. return job_info[pc_class2idx(class_)].max_level[0];
  6441. }
  6442. /**
  6443. * Returns max base level for this character.
  6444. * @param sd Player
  6445. * @return Max Base Level
  6446. **/
  6447. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6448. return pc_class_maxbaselv(sd->status.class_);
  6449. }
  6450. /**
  6451. * Returns max job level for this character's class.
  6452. * @param class_: Player's class
  6453. * @return Max Job Level
  6454. */
  6455. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6456. return job_info[pc_class2idx(class_)].max_level[1];
  6457. }
  6458. /**
  6459. * Returns max job level for this character.
  6460. * @param sd Player
  6461. * @return Max Job Level
  6462. **/
  6463. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6464. return pc_class_maxjoblv(sd->status.class_);
  6465. }
  6466. /**
  6467. * Check if player is reached max base level
  6468. * @param sd
  6469. * @return True if reached max level
  6470. **/
  6471. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6472. nullpo_retr(false, sd);
  6473. return (sd->status.base_level >= pc_maxbaselv(sd));
  6474. }
  6475. /**
  6476. * Check if player is reached max base level
  6477. * @param sd
  6478. * @return True if reached max level
  6479. **/
  6480. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6481. nullpo_retr(false, sd);
  6482. return (sd->status.job_level >= pc_maxjoblv(sd));
  6483. }
  6484. /**
  6485. * Base exp needed for player to level up.
  6486. * @param sd
  6487. * @return Base EXP needed for next base level
  6488. **/
  6489. t_exp pc_nextbaseexp(struct map_session_data *sd){
  6490. nullpo_ret(sd);
  6491. if (sd->status.base_level == 0) // Is this something that possible?
  6492. return 0;
  6493. if (pc_is_maxbaselv(sd))
  6494. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6495. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6496. }
  6497. /**
  6498. * Job exp needed for player to level up.
  6499. * @param sd
  6500. * @return Job EXP needed for next job level
  6501. **/
  6502. t_exp pc_nextjobexp(struct map_session_data *sd){
  6503. nullpo_ret(sd);
  6504. if (sd->status.job_level == 0) // Is this something that possible?
  6505. return 0;
  6506. if (pc_is_maxjoblv(sd))
  6507. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6508. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6509. }
  6510. /// Returns the value of the specified stat.
  6511. static int pc_getstat(struct map_session_data* sd, int type)
  6512. {
  6513. nullpo_retr(-1, sd);
  6514. switch( type ) {
  6515. case SP_STR: return sd->status.str;
  6516. case SP_AGI: return sd->status.agi;
  6517. case SP_VIT: return sd->status.vit;
  6518. case SP_INT: return sd->status.int_;
  6519. case SP_DEX: return sd->status.dex;
  6520. case SP_LUK: return sd->status.luk;
  6521. default:
  6522. return -1;
  6523. }
  6524. }
  6525. /// Sets the specified stat to the specified value.
  6526. /// Returns the new value.
  6527. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6528. {
  6529. nullpo_retr(-1, sd);
  6530. switch( type ) {
  6531. case SP_STR: sd->status.str = val; break;
  6532. case SP_AGI: sd->status.agi = val; break;
  6533. case SP_VIT: sd->status.vit = val; break;
  6534. case SP_INT: sd->status.int_ = val; break;
  6535. case SP_DEX: sd->status.dex = val; break;
  6536. case SP_LUK: sd->status.luk = val; break;
  6537. default:
  6538. return -1;
  6539. }
  6540. return val;
  6541. }
  6542. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6543. int pc_gets_status_point(int level)
  6544. {
  6545. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6546. return (statp[level+1] - statp[level]);
  6547. else //Default increase
  6548. return ((level+15) / 5);
  6549. }
  6550. #ifdef RENEWAL_STAT
  6551. /// Renewal status point cost formula
  6552. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6553. #else
  6554. /// Pre-Renewal status point cost formula
  6555. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6556. #endif
  6557. /// Returns the number of stat points needed to change the specified stat by val.
  6558. /// If val is negative, returns the number of stat points that would be needed to
  6559. /// raise the specified stat from (current value - val) to current value.
  6560. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6561. {
  6562. int low, high, sp = 0, max = 0;
  6563. if ( val == 0 )
  6564. return 0;
  6565. low = pc_getstat(sd,type);
  6566. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6567. if ( low >= max && val > 0 )
  6568. return 0; // Official servers show '0' when max is reached
  6569. high = low + val;
  6570. if ( val < 0 )
  6571. SWAP(low, high);
  6572. for ( ; low < high; low++ )
  6573. sp += PC_STATUS_POINT_COST(low);
  6574. return sp;
  6575. }
  6576. /**
  6577. * Returns the value the specified stat can be increased by with the current
  6578. * amount of available status points for the current character's class.
  6579. *
  6580. * @param sd The target character.
  6581. * @param type Stat to verify.
  6582. * @return Maximum value the stat could grow by.
  6583. */
  6584. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6585. {
  6586. int base, final_val, status_points, max_param;
  6587. nullpo_ret(sd);
  6588. base = final_val = pc_getstat(sd, type);
  6589. status_points = sd->status.status_point;
  6590. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6591. while (final_val <= max_param && status_points >= 0) {
  6592. status_points -= PC_STATUS_POINT_COST(final_val);
  6593. final_val++;
  6594. }
  6595. final_val--;
  6596. return (final_val > base ? final_val-base : 0);
  6597. }
  6598. /**
  6599. * Raises a stat by the specified amount.
  6600. *
  6601. * Obeys max_parameter limits.
  6602. * Subtracts status points according to the cost of the increased stat points.
  6603. *
  6604. * @param sd The target character.
  6605. * @param type The stat to change (see enum _sp)
  6606. * @param increase The stat increase (strictly positive) amount.
  6607. * @retval true if the stat was increased by any amount.
  6608. * @retval false if there were no changes.
  6609. */
  6610. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6611. {
  6612. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6613. nullpo_ret(sd);
  6614. // check conditions
  6615. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6616. clif_statusupack(sd, type, 0, 0);
  6617. return false;
  6618. }
  6619. // check limits
  6620. current = pc_getstat(sd, type);
  6621. max_increase = pc_maxparameterincrease(sd, type);
  6622. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6623. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6624. clif_statusupack(sd, type, 0, 0);
  6625. return false;
  6626. }
  6627. // check status points
  6628. needed_points = pc_need_status_point(sd, type, increase);
  6629. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6630. clif_statusupack(sd, type, 0, 0);
  6631. return false;
  6632. }
  6633. // set new values
  6634. final_value = pc_setstat(sd, type, current + increase);
  6635. sd->status.status_point -= needed_points;
  6636. status_calc_pc(sd,SCO_NONE);
  6637. // update increase cost indicator
  6638. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6639. // update statpoint count
  6640. clif_updatestatus(sd, SP_STATUSPOINT);
  6641. // update stat value
  6642. clif_statusupack(sd, type, 1, final_value); // required
  6643. if( final_value > 255 )
  6644. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6645. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6646. return true;
  6647. }
  6648. /**
  6649. * Raises a stat by the specified amount.
  6650. *
  6651. * Obeys max_parameter limits.
  6652. * Does not subtract status points for the cost of the modified stat points.
  6653. *
  6654. * @param sd The target character.
  6655. * @param type The stat to change (see enum _sp)
  6656. * @param val The stat increase (or decrease) amount.
  6657. * @return the stat increase amount.
  6658. * @retval 0 if no changes were made.
  6659. */
  6660. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6661. {
  6662. int max, need;
  6663. nullpo_ret(sd);
  6664. if( type < SP_STR || type > SP_LUK )
  6665. {
  6666. clif_statusupack(sd,type,0,0);
  6667. return 0;
  6668. }
  6669. need = pc_need_status_point(sd,type,1);
  6670. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6671. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6672. status_calc_pc(sd,SCO_NONE);
  6673. // update increase cost indicator
  6674. if( need != pc_need_status_point(sd,type,1) )
  6675. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6676. // update stat value
  6677. clif_statusupack(sd,type,1,val); // required
  6678. if( val > 255 )
  6679. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6680. return val;
  6681. }
  6682. /*==========================================
  6683. * Update skill_lv for player sd
  6684. * Skill point allocation
  6685. *------------------------------------------*/
  6686. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6687. {
  6688. uint16 idx = skill_get_index(skill_id);
  6689. nullpo_retv(sd);
  6690. if (!idx) {
  6691. if (skill_id)
  6692. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6693. return;
  6694. }
  6695. // Level up guild skill
  6696. if (SKILL_CHK_GUILD(skill_id)) {
  6697. guild_skillup(sd, skill_id);
  6698. return;
  6699. }
  6700. // Level up homunculus skill
  6701. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6702. hom_skillup(sd->hd, skill_id);
  6703. return;
  6704. }
  6705. else {
  6706. if( sd->status.skill_point > 0 &&
  6707. sd->status.skill[idx].id &&
  6708. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6709. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6710. {
  6711. int lv, range, upgradable;
  6712. sd->status.skill[idx].lv++;
  6713. sd->status.skill_point--;
  6714. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  6715. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6716. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6717. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6718. else
  6719. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6720. lv = sd->status.skill[idx].lv;
  6721. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6722. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6723. clif_skillup(sd,skill_id,lv,range,upgradable);
  6724. clif_updatestatus(sd,SP_SKILLPOINT);
  6725. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6726. clif_updatestatus(sd,SP_CARTINFO);
  6727. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  6728. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6729. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6730. clif_skillinfoblock(sd);
  6731. }
  6732. //else
  6733. // 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);
  6734. }
  6735. }
  6736. /*==========================================
  6737. * /allskill
  6738. *------------------------------------------*/
  6739. int pc_allskillup(struct map_session_data *sd)
  6740. {
  6741. int i;
  6742. nullpo_ret(sd);
  6743. for (i = 0; i < MAX_SKILL; i++) {
  6744. 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) {
  6745. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6746. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6747. if (sd->status.skill[i].lv == 0)
  6748. sd->status.skill[i].id = 0;
  6749. }
  6750. }
  6751. if (!pc_grant_allskills(sd, true)) {
  6752. uint16 sk_id;
  6753. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6754. uint16 sk_idx = skill_get_index(sk_id);
  6755. if (sk_id == 0 || sk_idx == 0)
  6756. continue;
  6757. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  6758. if (
  6759. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  6760. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  6761. sk_id == SG_DEVIL
  6762. )
  6763. continue; //Cannot be learned normally.
  6764. sd->status.skill[sk_idx].id = sk_id;
  6765. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6766. }
  6767. }
  6768. status_calc_pc(sd,SCO_NONE);
  6769. //Required because if you could level up all skills previously,
  6770. //the update will not be sent as only the lv variable changes.
  6771. clif_skillinfoblock(sd);
  6772. return 0;
  6773. }
  6774. /*==========================================
  6775. * /resetlvl
  6776. *------------------------------------------*/
  6777. int pc_resetlvl(struct map_session_data* sd,int type)
  6778. {
  6779. int i;
  6780. nullpo_ret(sd);
  6781. if (type != 3) //Also reset skills
  6782. pc_resetskill(sd, 0);
  6783. if(type == 1){
  6784. sd->status.skill_point=0;
  6785. sd->status.base_level=1;
  6786. sd->status.job_level=1;
  6787. sd->status.base_exp=0;
  6788. sd->status.job_exp=0;
  6789. if(sd->sc.option !=0)
  6790. sd->sc.option = 0;
  6791. sd->status.str=1;
  6792. sd->status.agi=1;
  6793. sd->status.vit=1;
  6794. sd->status.int_=1;
  6795. sd->status.dex=1;
  6796. sd->status.luk=1;
  6797. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6798. sd->status.status_point=100; // not 88 [celest]
  6799. // give platinum skills upon changing
  6800. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6801. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6802. }
  6803. }
  6804. if(type == 2){
  6805. sd->status.skill_point=0;
  6806. sd->status.base_level=1;
  6807. sd->status.job_level=1;
  6808. sd->status.base_exp=0;
  6809. sd->status.job_exp=0;
  6810. }
  6811. if(type == 3){
  6812. sd->status.base_level=1;
  6813. sd->status.base_exp=0;
  6814. }
  6815. if(type == 4){
  6816. sd->status.job_level=1;
  6817. sd->status.job_exp=0;
  6818. }
  6819. clif_updatestatus(sd,SP_STATUSPOINT);
  6820. clif_updatestatus(sd,SP_STR);
  6821. clif_updatestatus(sd,SP_AGI);
  6822. clif_updatestatus(sd,SP_VIT);
  6823. clif_updatestatus(sd,SP_INT);
  6824. clif_updatestatus(sd,SP_DEX);
  6825. clif_updatestatus(sd,SP_LUK);
  6826. clif_updatestatus(sd,SP_BASELEVEL);
  6827. clif_updatestatus(sd,SP_JOBLEVEL);
  6828. clif_updatestatus(sd,SP_STATUSPOINT);
  6829. clif_updatestatus(sd,SP_BASEEXP);
  6830. clif_updatestatus(sd,SP_JOBEXP);
  6831. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6832. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6833. clif_updatestatus(sd,SP_SKILLPOINT);
  6834. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6835. clif_updatestatus(sd,SP_UAGI);
  6836. clif_updatestatus(sd,SP_UVIT);
  6837. clif_updatestatus(sd,SP_UINT);
  6838. clif_updatestatus(sd,SP_UDEX);
  6839. clif_updatestatus(sd,SP_ULUK); // End Addition
  6840. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6841. if(sd->equip_index[i] >= 0)
  6842. if(pc_isequip(sd,sd->equip_index[i]))
  6843. pc_unequipitem(sd,sd->equip_index[i],2);
  6844. }
  6845. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6846. party_send_levelup(sd);
  6847. status_calc_pc(sd, SCO_FORCE);
  6848. clif_skillinfoblock(sd);
  6849. return 0;
  6850. }
  6851. /*==========================================
  6852. * /resetstate
  6853. *------------------------------------------*/
  6854. int pc_resetstate(struct map_session_data* sd)
  6855. {
  6856. nullpo_ret(sd);
  6857. if (battle_config.use_statpoint_table)
  6858. { // New statpoint table used here - Dexity
  6859. if (sd->status.base_level > MAX_LEVEL)
  6860. { //statp[] goes out of bounds, can't reset!
  6861. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6862. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6863. return 0;
  6864. }
  6865. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6866. }
  6867. else
  6868. {
  6869. int add=0;
  6870. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6871. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6872. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6873. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6874. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6875. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6876. sd->status.status_point+=add;
  6877. }
  6878. pc_setstat(sd, SP_STR, 1);
  6879. pc_setstat(sd, SP_AGI, 1);
  6880. pc_setstat(sd, SP_VIT, 1);
  6881. pc_setstat(sd, SP_INT, 1);
  6882. pc_setstat(sd, SP_DEX, 1);
  6883. pc_setstat(sd, SP_LUK, 1);
  6884. clif_updatestatus(sd,SP_STR);
  6885. clif_updatestatus(sd,SP_AGI);
  6886. clif_updatestatus(sd,SP_VIT);
  6887. clif_updatestatus(sd,SP_INT);
  6888. clif_updatestatus(sd,SP_DEX);
  6889. clif_updatestatus(sd,SP_LUK);
  6890. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6891. clif_updatestatus(sd,SP_UAGI);
  6892. clif_updatestatus(sd,SP_UVIT);
  6893. clif_updatestatus(sd,SP_UINT);
  6894. clif_updatestatus(sd,SP_UDEX);
  6895. clif_updatestatus(sd,SP_ULUK); // End Addition
  6896. clif_updatestatus(sd,SP_STATUSPOINT);
  6897. if( sd->mission_mobid ) { //bugreport:2200
  6898. sd->mission_mobid = 0;
  6899. sd->mission_count = 0;
  6900. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6901. }
  6902. status_calc_pc(sd, SCO_NONE);
  6903. return 1;
  6904. }
  6905. /*==========================================
  6906. * /resetskill
  6907. * if flag&1, perform block resync and status_calc call.
  6908. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6909. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6910. *------------------------------------------*/
  6911. int pc_resetskill(struct map_session_data* sd, int flag)
  6912. {
  6913. int i, skill_point=0;
  6914. nullpo_ret(sd);
  6915. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6916. return 0;
  6917. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6918. /**
  6919. * 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)
  6920. **/
  6921. if( pc_is_taekwon_ranker(sd) )
  6922. return 0;
  6923. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6924. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6925. i = sd->sc.option;
  6926. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6927. i &= ~OPTION_RIDING;
  6928. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6929. i &= ~OPTION_FALCON;
  6930. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6931. i &= ~OPTION_DRAGON;
  6932. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6933. i &= ~OPTION_WUG;
  6934. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6935. i &= ~OPTION_WUGRIDER;
  6936. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6937. i &= ~OPTION_MADOGEAR;
  6938. #ifndef NEW_CARTS
  6939. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6940. i &= ~OPTION_CART;
  6941. #else
  6942. if( sd->sc.data[SC_PUSH_CART] )
  6943. pc_setcart(sd, 0);
  6944. #endif
  6945. if( i != sd->sc.option )
  6946. pc_setoption(sd, i);
  6947. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6948. hom_vaporize(sd, HOM_ST_ACTIVE);
  6949. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6950. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6951. }
  6952. for (const auto &skill : skill_db) {
  6953. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  6954. uint8 lv = sd->status.skill[idx].lv;
  6955. if (lv == 0 || skill_id == 0)
  6956. continue;
  6957. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  6958. continue;
  6959. // Don't reset trick dead if not a novice/baby
  6960. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6961. {
  6962. sd->status.skill[idx].lv = 0;
  6963. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6964. continue;
  6965. }
  6966. // do not reset basic skill
  6967. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6968. continue;
  6969. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  6970. continue;
  6971. if( flag&4 && !skill_ischangesex(skill_id) )
  6972. continue;
  6973. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  6974. { //Only handle quest skills in a special way when you can't learn them manually
  6975. if( battle_config.quest_skill_reset && !(flag&2) )
  6976. { //Wipe them
  6977. sd->status.skill[idx].lv = 0;
  6978. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6979. }
  6980. continue;
  6981. }
  6982. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  6983. skill_point += lv;
  6984. else
  6985. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6986. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  6987. if( !(flag&2) )
  6988. {// reset
  6989. sd->status.skill[idx].lv = 0;
  6990. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6991. }
  6992. }
  6993. if( flag&2 || !skill_point ) return skill_point;
  6994. sd->status.skill_point += skill_point;
  6995. if (flag&1) {
  6996. clif_updatestatus(sd,SP_SKILLPOINT);
  6997. clif_skillinfoblock(sd);
  6998. status_calc_pc(sd, SCO_FORCE);
  6999. }
  7000. return skill_point;
  7001. }
  7002. /*==========================================
  7003. * /resetfeel [Komurka]
  7004. *------------------------------------------*/
  7005. int pc_resetfeel(struct map_session_data* sd)
  7006. {
  7007. int i;
  7008. nullpo_ret(sd);
  7009. for (i=0; i<MAX_PC_FEELHATE; i++)
  7010. {
  7011. sd->feel_map[i].m = -1;
  7012. sd->feel_map[i].index = 0;
  7013. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  7014. }
  7015. return 0;
  7016. }
  7017. int pc_resethate(struct map_session_data* sd)
  7018. {
  7019. int i;
  7020. nullpo_ret(sd);
  7021. for (i=0; i<MAX_PC_FEELHATE; i++)
  7022. {
  7023. sd->hate_mob[i] = -1;
  7024. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  7025. }
  7026. return 0;
  7027. }
  7028. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  7029. {
  7030. int bonus = 0;
  7031. nullpo_ret(sd);
  7032. skill_id = skill_dummy2skill_id(skill_id);
  7033. for (auto &it : sd->skillatk) {
  7034. if (it.id == skill_id) {
  7035. bonus += it.val;
  7036. break;
  7037. }
  7038. }
  7039. return bonus;
  7040. }
  7041. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  7042. {
  7043. int bonus = 0;
  7044. nullpo_ret(sd);
  7045. skill_id = skill_dummy2skill_id(skill_id);
  7046. for (auto &it : sd->subskill) {
  7047. if (it.id == skill_id) {
  7048. bonus += it.val;
  7049. break;
  7050. }
  7051. }
  7052. return bonus;
  7053. }
  7054. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  7055. int bonus = sd->bonus.add_heal_rate;
  7056. nullpo_ret(sd);
  7057. skill_id = skill_dummy2skill_id(skill_id);
  7058. if( bonus ) {
  7059. switch( skill_id ) {
  7060. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  7061. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  7062. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  7063. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  7064. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  7065. }
  7066. }
  7067. for (auto &it : sd->skillheal) {
  7068. if (it.id == skill_id) {
  7069. bonus += it.val;
  7070. break;
  7071. }
  7072. }
  7073. return bonus;
  7074. }
  7075. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  7076. int bonus = sd->bonus.add_heal2_rate;
  7077. skill_id = skill_dummy2skill_id(skill_id);
  7078. for (auto &it : sd->skillheal2) {
  7079. if (it.id == skill_id) {
  7080. bonus += it.val;
  7081. break;
  7082. }
  7083. }
  7084. return bonus;
  7085. }
  7086. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  7087. {
  7088. if( !pc_isdead(sd) )
  7089. return; // not applicable
  7090. if( sd->bg_id && bg_member_respawn(sd) )
  7091. return; // member revived by battleground
  7092. pc_setstand(sd, true);
  7093. pc_setrestartvalue(sd,3);
  7094. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  7095. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  7096. }
  7097. static TIMER_FUNC(pc_respawn_timer){
  7098. struct map_session_data *sd = map_id2sd(id);
  7099. if( sd != NULL )
  7100. {
  7101. sd->pvp_point=0;
  7102. sd->respawn_tid = INVALID_TIMER;
  7103. pc_respawn(sd,CLR_OUTSIGHT);
  7104. }
  7105. return 0;
  7106. }
  7107. /*==========================================
  7108. * Invoked when a player has received damage
  7109. *------------------------------------------*/
  7110. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  7111. {
  7112. if (sp) clif_updatestatus(sd,SP_SP);
  7113. if (hp) clif_updatestatus(sd,SP_HP);
  7114. else return;
  7115. if (!src)
  7116. return;
  7117. if( pc_issit(sd) ) {
  7118. pc_setstand(sd, true);
  7119. skill_sit(sd,0);
  7120. }
  7121. if (sd->progressbar.npc_id)
  7122. clif_progressbar_abort(sd);
  7123. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  7124. pet_target_check(sd->pd,src,1);
  7125. if( sd->status.ele_id > 0 )
  7126. elemental_set_target(sd,src);
  7127. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  7128. sd->canlog_tick = gettick();
  7129. }
  7130. TIMER_FUNC(pc_close_npc_timer){
  7131. TBL_PC *sd = map_id2sd(id);
  7132. if(sd) pc_close_npc(sd,data);
  7133. return 0;
  7134. }
  7135. /**
  7136. * Method to properly close a NPC for player and clear anything related.
  7137. * @param sd: Player attached
  7138. * @param flag: Method of closure
  7139. * 1: Produce a close button and end the NPC
  7140. * 2: End the NPC (best for no dialog windows)
  7141. */
  7142. void pc_close_npc(struct map_session_data *sd,int flag)
  7143. {
  7144. nullpo_retv(sd);
  7145. if (sd->npc_id || sd->npc_shopid) {
  7146. if (sd->state.using_fake_npc) {
  7147. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  7148. sd->state.using_fake_npc = 0;
  7149. }
  7150. if (sd->st) {
  7151. if(sd->st->state == RUN){ //wait ending code execution
  7152. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  7153. return;
  7154. }
  7155. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  7156. sd->st->mes_active = 0;
  7157. }
  7158. sd->state.menu_or_input = 0;
  7159. sd->npc_menu = 0;
  7160. sd->npc_shopid = 0;
  7161. #ifdef SECURE_NPCTIMEOUT
  7162. if( sd->npc_idle_timer != INVALID_TIMER ){
  7163. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  7164. sd->npc_idle_timer = INVALID_TIMER;
  7165. }
  7166. #endif
  7167. if (sd->st) {
  7168. if (sd->st->state == CLOSE) {
  7169. clif_scriptclose(sd, sd->npc_id);
  7170. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  7171. sd->st->state = END; // Force to end now
  7172. }
  7173. if (sd->st->state == END) { // free attached scripts that are waiting
  7174. script_free_state(sd->st);
  7175. sd->st = NULL;
  7176. sd->npc_id = 0;
  7177. }
  7178. }
  7179. }
  7180. }
  7181. /*==========================================
  7182. * Invoked when a player has negative current hp
  7183. *------------------------------------------*/
  7184. int pc_dead(struct map_session_data *sd,struct block_list *src)
  7185. {
  7186. int i=0,k=0;
  7187. t_tick tick = gettick();
  7188. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  7189. // Activate Steel body if a super novice dies at 99+% exp [celest]
  7190. // Super Novices have no kill or die functions attached when saved by their angel
  7191. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7192. t_exp exp = pc_nextbaseexp(sd);
  7193. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  7194. sd->state.snovice_dead_flag = 1;
  7195. pc_setrestartvalue(sd,1);
  7196. status_percent_heal(&sd->bl, 100, 100);
  7197. clif_resurrection(&sd->bl, 1);
  7198. if(battle_config.pc_invincible_time)
  7199. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7200. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  7201. if(mapdata_flag_gvg2(mapdata))
  7202. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7203. return 0;
  7204. }
  7205. }
  7206. for(k = 0; k < MAX_DEVOTION; k++) {
  7207. if (sd->devotion[k]){
  7208. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  7209. if (devsd)
  7210. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  7211. sd->devotion[k] = 0;
  7212. }
  7213. }
  7214. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  7215. if (sd->stellar_mark[k]) {
  7216. struct map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  7217. if (smarksd)
  7218. status_change_end(&smarksd->bl, SC_FLASHKICK, INVALID_TIMER);
  7219. sd->stellar_mark[k] = 0;
  7220. }
  7221. }
  7222. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  7223. if (sd->united_soul[k]) {
  7224. struct map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  7225. if (usoulsd)
  7226. status_change_end(&usoulsd->bl, SC_SOULUNITY, INVALID_TIMER);
  7227. sd->united_soul[k] = 0;
  7228. }
  7229. }
  7230. if(sd->shadowform_id) { //if we were target of shadowform
  7231. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  7232. sd->shadowform_id = 0; //should be remove on status end anyway
  7233. }
  7234. if(sd->status.pet_id > 0 && sd->pd) {
  7235. struct pet_data *pd = sd->pd;
  7236. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  7237. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  7238. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  7239. pet_set_intimate(pd, PET_INTIMATE_NONE);
  7240. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  7241. }
  7242. if( sd->pd->target_id ) // Unlock all targets...
  7243. pet_unlocktarget(sd->pd);
  7244. }
  7245. 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)
  7246. hom_vaporize(sd, HOM_ST_ACTIVE);
  7247. if( sd->md )
  7248. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  7249. if( sd->ed )
  7250. elemental_delete(sd->ed);
  7251. // Leave duel if you die [LuzZza]
  7252. if(battle_config.duel_autoleave_when_die) {
  7253. if(sd->duel_group > 0)
  7254. duel_leave(sd->duel_group, sd);
  7255. if(sd->duel_invite > 0)
  7256. duel_reject(sd->duel_invite, sd);
  7257. }
  7258. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  7259. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  7260. sd->skill_keep_using.tid = INVALID_TIMER;
  7261. }
  7262. pc_close_npc(sd,2); //close npc if we were using one
  7263. /* e.g. not killed thru pc_damage */
  7264. if( pc_issit(sd) ) {
  7265. clif_status_load(&sd->bl,EFST_SIT,0);
  7266. }
  7267. pc_setdead(sd);
  7268. clif_party_dead( sd );
  7269. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  7270. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  7271. //Reset menu skills/item skills
  7272. if ((sd->skillitem) != 0)
  7273. sd->skillitem = sd->skillitemlv = 0;
  7274. if ((sd->menuskill_id) != 0)
  7275. sd->menuskill_id = sd->menuskill_val = 0;
  7276. //Reset ticks.
  7277. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  7278. if ( sd->spiritball !=0 )
  7279. pc_delspiritball(sd,sd->spiritball,0);
  7280. if (sd->soulball != 0)
  7281. pc_delsoulball(sd, sd->soulball, false);
  7282. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  7283. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  7284. if (src)
  7285. switch (src->type) {
  7286. case BL_MOB:
  7287. {
  7288. struct mob_data *md=(struct mob_data *)src;
  7289. if(md->target_id==sd->bl.id)
  7290. mob_unlocktarget(md,tick);
  7291. if(battle_config.mobs_level_up && md->status.hp &&
  7292. (unsigned int)md->level < pc_maxbaselv(sd) &&
  7293. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  7294. ) { // monster level up [Valaris]
  7295. clif_misceffect(&md->bl,0);
  7296. md->level++;
  7297. status_calc_mob(md, SCO_NONE);
  7298. status_percent_heal(src,10,0);
  7299. if( battle_config.show_mob_info&4 )
  7300. {// update name with new level
  7301. clif_name_area(&md->bl);
  7302. }
  7303. }
  7304. src = battle_get_master(src); // Maybe Player Summon
  7305. }
  7306. break;
  7307. case BL_PET: //Pass on to master...
  7308. case BL_HOM:
  7309. case BL_MER:
  7310. src = battle_get_master(src);
  7311. break;
  7312. }
  7313. if (src && src->type == BL_PC) {
  7314. struct map_session_data *ssd = (struct map_session_data *)src;
  7315. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  7316. npc_script_event(ssd, NPCE_KILLPC);
  7317. if (battle_config.pk_mode&2) {
  7318. ssd->status.manner -= 5;
  7319. if(ssd->status.manner < 0)
  7320. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  7321. #if 0
  7322. // PK/Karma system code (not enabled yet) [celest]
  7323. // originally from Kade Online, so i don't know if any of these is correct ^^;
  7324. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  7325. // karma going down = more 'good' / more honourable.
  7326. // The Karma System way...
  7327. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  7328. sd->status.karma--;
  7329. ssd->status.karma--;
  7330. }
  7331. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  7332. ssd->status.karma++;
  7333. // or the PK System way...
  7334. if (sd->status.karma > 0) // player killed is dishonourable?
  7335. ssd->status.karma--; // honour points earned
  7336. sd->status.karma++; // honour points lost
  7337. // To-do: Receive exp on certain occasions
  7338. #endif
  7339. }
  7340. }
  7341. if(battle_config.bone_drop==2
  7342. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  7343. {
  7344. struct item item_tmp;
  7345. memset(&item_tmp,0,sizeof(item_tmp));
  7346. item_tmp.nameid=ITEMID_SKULL_;
  7347. item_tmp.identify=1;
  7348. item_tmp.card[0]=CARD0_CREATE;
  7349. item_tmp.card[1]=0;
  7350. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  7351. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  7352. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  7353. }
  7354. //Remove bonus_script when dead
  7355. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  7356. // changed penalty options, added death by player if pk_mode [Valaris]
  7357. if(battle_config.death_penalty_type
  7358. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  7359. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  7360. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  7361. {
  7362. t_exp base_penalty = 0;
  7363. t_exp job_penalty = 0;
  7364. uint32 zeny_penalty = 0;
  7365. if (pc_isvip(sd)) { // EXP penalty for VIP
  7366. base_penalty = battle_config.vip_exp_penalty_base;
  7367. job_penalty = battle_config.vip_exp_penalty_job;
  7368. zeny_penalty = battle_config.vip_zeny_penalty;
  7369. } else {
  7370. base_penalty = battle_config.death_penalty_base;
  7371. job_penalty = battle_config.death_penalty_job;
  7372. zeny_penalty = battle_config.zeny_penalty;
  7373. }
  7374. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7375. switch (battle_config.death_penalty_type) {
  7376. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7377. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  7378. }
  7379. if (base_penalty){ //recheck after altering to speedup
  7380. if (battle_config.pk_mode && src && src->type==BL_PC)
  7381. base_penalty *= 2;
  7382. base_penalty = u64min(sd->status.base_exp, base_penalty);
  7383. }
  7384. }
  7385. else
  7386. base_penalty = 0;
  7387. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7388. switch (battle_config.death_penalty_type) {
  7389. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7390. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7391. }
  7392. if (job_penalty) {
  7393. if (battle_config.pk_mode && src && src->type==BL_PC)
  7394. job_penalty *= 2;
  7395. job_penalty = u64min(sd->status.job_exp, job_penalty);
  7396. }
  7397. }
  7398. else
  7399. job_penalty = 0;
  7400. if (base_penalty || job_penalty)
  7401. pc_lostexp(sd, base_penalty, job_penalty);
  7402. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  7403. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7404. if(zeny_penalty)
  7405. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7406. }
  7407. }
  7408. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7409. for (const auto &it : mapdata->drop_list) {
  7410. int id = it.drop_id, per = it.drop_per;
  7411. enum e_nightmare_drop_type type = it.drop_type;
  7412. if(id == 0)
  7413. continue;
  7414. if(id == -1){
  7415. int eq_num=0,eq_n[MAX_INVENTORY];
  7416. memset(eq_n,0,sizeof(eq_n));
  7417. for(i=0;i<MAX_INVENTORY;i++) {
  7418. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7419. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7420. || type&NMDT_ALL)
  7421. {
  7422. int l;
  7423. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7424. if( l < MAX_INVENTORY )
  7425. eq_n[l] = i;
  7426. eq_num++;
  7427. }
  7428. }
  7429. if(eq_num > 0){
  7430. int n = eq_n[rnd()%eq_num];
  7431. if(rnd()%10000 < per) {
  7432. if(sd->inventory.u.items_inventory[n].equip)
  7433. pc_unequipitem(sd,n,3);
  7434. pc_dropitem(sd,n,1);
  7435. }
  7436. }
  7437. }
  7438. else if(id > 0) {
  7439. for(i=0;i<MAX_INVENTORY;i++){
  7440. if(sd->inventory.u.items_inventory[i].nameid == id
  7441. && rnd()%10000 < per
  7442. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7443. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7444. || type&NMDT_ALL) ){
  7445. if(sd->inventory.u.items_inventory[i].equip)
  7446. pc_unequipitem(sd,i,3);
  7447. pc_dropitem(sd,i,1);
  7448. break;
  7449. }
  7450. }
  7451. }
  7452. }
  7453. }
  7454. // pvp
  7455. // disable certain pvp functions on pk_mode [Valaris]
  7456. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  7457. sd->pvp_point -= 5;
  7458. sd->pvp_lost++;
  7459. if( src && src->type == BL_PC ) {
  7460. struct map_session_data *ssd = (struct map_session_data *)src;
  7461. ssd->pvp_point++;
  7462. ssd->pvp_won++;
  7463. }
  7464. if( sd->pvp_point < 0 ) {
  7465. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7466. return 1|8;
  7467. }
  7468. }
  7469. //GvG
  7470. if( mapdata_flag_gvg2(mapdata) ) {
  7471. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7472. return 1|8;
  7473. }
  7474. else if( sd->bg_id ) {
  7475. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  7476. if (bg) {
  7477. if (bg->cemetery.map > 0) { // Respawn by BG
  7478. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  7479. return 1|8;
  7480. }
  7481. }
  7482. }
  7483. //Reset "can log out" tick.
  7484. if( battle_config.prevent_logout )
  7485. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7486. return 1;
  7487. }
  7488. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7489. if(hp) clif_updatestatus(sd,SP_HP);
  7490. if(sp) clif_updatestatus(sd,SP_SP);
  7491. pc_setstand(sd, true);
  7492. if(battle_config.pc_invincible_time > 0)
  7493. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7494. if( sd->state.gmaster_flag ) {
  7495. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7496. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7497. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7498. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7499. }
  7500. }
  7501. bool pc_revive_item(struct map_session_data *sd) {
  7502. nullpo_retr(false, sd);
  7503. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  7504. return false;
  7505. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  7506. return false;
  7507. int16 item_position = itemdb_group_item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  7508. uint8 hp = 100, sp = 100;
  7509. if (item_position < 0) {
  7510. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  7511. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  7512. sp = 0;
  7513. }
  7514. else
  7515. return false;
  7516. }
  7517. if (!status_revive(&sd->bl, hp, sp))
  7518. return false;
  7519. if (item_position < 0)
  7520. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE, INVALID_TIMER);
  7521. else
  7522. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  7523. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  7524. return true;
  7525. }
  7526. // script
  7527. //
  7528. /*==========================================
  7529. * script reading pc status registry
  7530. *------------------------------------------*/
  7531. int64 pc_readparam(struct map_session_data* sd,int64 type)
  7532. {
  7533. int64 val = 0;
  7534. nullpo_ret(sd);
  7535. switch(type) {
  7536. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7537. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7538. case SP_ZENY: val = sd->status.zeny; break;
  7539. case SP_BASELEVEL: val = sd->status.base_level; break;
  7540. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7541. case SP_CLASS: val = sd->status.class_; break;
  7542. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7543. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7544. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7545. case SP_SEX: val = sd->status.sex; break;
  7546. case SP_WEIGHT: val = sd->weight; break;
  7547. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7548. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  7549. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  7550. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  7551. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  7552. case SP_HP: val = sd->battle_status.hp; break;
  7553. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7554. case SP_SP: val = sd->battle_status.sp; break;
  7555. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7556. case SP_STR: val = sd->status.str; break;
  7557. case SP_AGI: val = sd->status.agi; break;
  7558. case SP_VIT: val = sd->status.vit; break;
  7559. case SP_INT: val = sd->status.int_; break;
  7560. case SP_DEX: val = sd->status.dex; break;
  7561. case SP_LUK: val = sd->status.luk; break;
  7562. case SP_KARMA: val = sd->status.karma; break;
  7563. case SP_MANNER: val = sd->status.manner; break;
  7564. case SP_FAME: val = sd->status.fame; break;
  7565. case SP_KILLERRID: val = sd->killerrid; break;
  7566. case SP_KILLEDRID: val = sd->killedrid; break;
  7567. case SP_KILLEDGID: val = sd->killedgid; break;
  7568. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7569. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7570. case SP_CHARRENAME: val = sd->status.rename; break;
  7571. case SP_CHARFONT: val = sd->status.font; break;
  7572. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7573. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7574. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7575. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7576. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7577. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7578. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7579. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7580. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  7581. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7582. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7583. case SP_BASE_ATK:
  7584. #ifdef RENEWAL
  7585. val = sd->bonus.eatk;
  7586. #else
  7587. val = sd->battle_status.batk;
  7588. #endif
  7589. break;
  7590. case SP_DEF1: val = sd->battle_status.def; break;
  7591. case SP_DEF2: val = sd->battle_status.def2; break;
  7592. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7593. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7594. case SP_HIT: val = sd->battle_status.hit; break;
  7595. case SP_FLEE1: val = sd->battle_status.flee; break;
  7596. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7597. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7598. case SP_MAXHPRATE: val = sd->hprate; break;
  7599. case SP_MAXSPRATE: val = sd->sprate; break;
  7600. case SP_SPRATE: val = sd->dsprate; break;
  7601. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7602. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7603. case SP_ASPD_RATE:
  7604. #ifndef RENEWAL_ASPD
  7605. val = sd->battle_status.aspd_rate;
  7606. #else
  7607. val = sd->battle_status.aspd_rate2;
  7608. #endif
  7609. break;
  7610. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7611. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7612. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7613. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7614. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7615. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7616. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7617. case SP_MATK_RATE: val = sd->matk_rate; break;
  7618. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7619. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7620. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7621. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7622. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7623. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7624. case SP_HIT_RATE: val = sd->hit_rate; break;
  7625. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7626. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7627. case SP_DEF_RATE: val = sd->def_rate; break;
  7628. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7629. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7630. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7631. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7632. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7633. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7634. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7635. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7636. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7637. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7638. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7639. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7640. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7641. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  7642. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  7643. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7644. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7645. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7646. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7647. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7648. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7649. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7650. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7651. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7652. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7653. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7654. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7655. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7656. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7657. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  7658. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7659. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7660. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7661. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7662. case SP_DELAYRATE: val = sd->delayrate; break;
  7663. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7664. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7665. case SP_UNSTRIPABLE:
  7666. case SP_UNSTRIPABLE_ARMOR:
  7667. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7668. break;
  7669. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7670. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7671. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7672. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7673. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  7674. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  7675. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7676. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7677. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7678. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7679. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7680. case SP_EMATK: val = sd->bonus.ematk; break;
  7681. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7682. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7683. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7684. case SP_CASTRATE:
  7685. case SP_VARCASTRATE:
  7686. #ifdef RENEWAL_CAST
  7687. val = sd->bonus.varcastrate; break;
  7688. #else
  7689. val = sd->castrate; break;
  7690. #endif
  7691. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  7692. default:
  7693. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  7694. return -1;
  7695. }
  7696. return val;
  7697. }
  7698. /*==========================================
  7699. * script set pc status registry
  7700. *------------------------------------------*/
  7701. bool pc_setparam(struct map_session_data *sd,int64 type,int64 val_tmp)
  7702. {
  7703. nullpo_retr(false,sd);
  7704. int val = static_cast<unsigned int>(val_tmp);
  7705. switch(type){
  7706. case SP_BASELEVEL:
  7707. if (val > pc_maxbaselv(sd)) //Capping to max
  7708. val = pc_maxbaselv(sd);
  7709. if (val > sd->status.base_level) {
  7710. int i = 0;
  7711. int stat=0;
  7712. for (i = 0; i < (int)(val - sd->status.base_level); i++)
  7713. stat += pc_gets_status_point(sd->status.base_level + i);
  7714. sd->status.status_point += stat;
  7715. }
  7716. sd->status.base_level = val;
  7717. sd->status.base_exp = 0;
  7718. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7719. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7720. clif_updatestatus(sd, SP_STATUSPOINT);
  7721. clif_updatestatus(sd, SP_BASEEXP);
  7722. status_calc_pc(sd, SCO_FORCE);
  7723. if(sd->status.party_id)
  7724. party_send_levelup(sd);
  7725. break;
  7726. case SP_JOBLEVEL:
  7727. if (val >= sd->status.job_level) {
  7728. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7729. sd->status.skill_point += val - sd->status.job_level;
  7730. clif_updatestatus(sd, SP_SKILLPOINT);
  7731. }
  7732. sd->status.job_level = val;
  7733. sd->status.job_exp = 0;
  7734. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7735. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7736. clif_updatestatus(sd, SP_JOBEXP);
  7737. status_calc_pc(sd, SCO_FORCE);
  7738. break;
  7739. case SP_SKILLPOINT:
  7740. sd->status.skill_point = val;
  7741. break;
  7742. case SP_STATUSPOINT:
  7743. sd->status.status_point = val;
  7744. break;
  7745. case SP_ZENY:
  7746. if( val < 0 )
  7747. return false;// can't set negative zeny
  7748. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7749. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7750. break;
  7751. case SP_BASEEXP:
  7752. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  7753. if (val_tmp < sd->status.base_exp) // Lost
  7754. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  7755. else // Gained
  7756. pc_gainexp(sd, NULL, val_tmp - sd->status.base_exp, 0, 2);
  7757. return true;
  7758. case SP_JOBEXP:
  7759. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  7760. if (val_tmp < sd->status.job_exp) // Lost
  7761. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  7762. else // Gained
  7763. pc_gainexp(sd, NULL, 0, val_tmp - sd->status.job_exp, 2);
  7764. return true;
  7765. case SP_SEX:
  7766. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7767. break;
  7768. case SP_WEIGHT:
  7769. sd->weight = val;
  7770. break;
  7771. case SP_MAXWEIGHT:
  7772. sd->max_weight = val;
  7773. break;
  7774. case SP_HP:
  7775. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7776. break;
  7777. case SP_MAXHP:
  7778. if (sd->status.base_level < 100)
  7779. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7780. else if (sd->status.base_level < 151)
  7781. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7782. else
  7783. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7784. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7785. {
  7786. sd->battle_status.hp = sd->battle_status.max_hp;
  7787. clif_updatestatus(sd, SP_HP);
  7788. }
  7789. break;
  7790. case SP_SP:
  7791. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7792. break;
  7793. case SP_MAXSP:
  7794. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7795. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7796. {
  7797. sd->battle_status.sp = sd->battle_status.max_sp;
  7798. clif_updatestatus(sd, SP_SP);
  7799. }
  7800. break;
  7801. case SP_STR:
  7802. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7803. break;
  7804. case SP_AGI:
  7805. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7806. break;
  7807. case SP_VIT:
  7808. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7809. break;
  7810. case SP_INT:
  7811. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7812. break;
  7813. case SP_DEX:
  7814. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7815. break;
  7816. case SP_LUK:
  7817. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7818. break;
  7819. case SP_KARMA:
  7820. sd->status.karma = val;
  7821. break;
  7822. case SP_MANNER:
  7823. sd->status.manner = val;
  7824. if( val < 0 )
  7825. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7826. else {
  7827. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7828. clif_manner_message(sd, 5);
  7829. }
  7830. return true; // status_change_start/status_change_end already sends packets warning the client
  7831. case SP_FAME:
  7832. sd->status.fame = val;
  7833. break;
  7834. case SP_KILLERRID:
  7835. sd->killerrid = val;
  7836. return true;
  7837. case SP_KILLEDRID:
  7838. sd->killedrid = val;
  7839. return true;
  7840. case SP_KILLEDGID:
  7841. sd->killedgid = val;
  7842. return true;
  7843. case SP_CHARMOVE:
  7844. sd->status.character_moves = val;
  7845. return true;
  7846. case SP_CHARRENAME:
  7847. sd->status.rename = val;
  7848. return true;
  7849. case SP_CHARFONT:
  7850. sd->status.font = val;
  7851. clif_font(sd);
  7852. return true;
  7853. case SP_BANK_VAULT:
  7854. if (val < 0)
  7855. return false;
  7856. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7857. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7858. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7859. return true;
  7860. case SP_ROULETTE_BRONZE:
  7861. sd->roulette_point.bronze = val;
  7862. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7863. return true;
  7864. case SP_ROULETTE_SILVER:
  7865. sd->roulette_point.silver = val;
  7866. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7867. return true;
  7868. case SP_ROULETTE_GOLD:
  7869. sd->roulette_point.gold = val;
  7870. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7871. return true;
  7872. case SP_CASHPOINTS:
  7873. if (val < 0)
  7874. return false;
  7875. if (!sd->state.connect_new)
  7876. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7877. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7878. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7879. return true;
  7880. case SP_KAFRAPOINTS:
  7881. if (val < 0)
  7882. return false;
  7883. if (!sd->state.connect_new)
  7884. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7885. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7886. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7887. return true;
  7888. case SP_PCDIECOUNTER:
  7889. if (val < 0)
  7890. return false;
  7891. if (sd->die_counter == val)
  7892. return true;
  7893. sd->die_counter = val;
  7894. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7895. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7896. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7897. return true;
  7898. case SP_COOKMASTERY:
  7899. if (val < 0)
  7900. return false;
  7901. if (sd->cook_mastery == val)
  7902. return true;
  7903. val = cap_value(val, 0, 1999);
  7904. sd->cook_mastery = val;
  7905. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7906. return true;
  7907. default:
  7908. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  7909. return false;
  7910. }
  7911. clif_updatestatus(sd,static_cast<int>(type));
  7912. return true;
  7913. }
  7914. /*==========================================
  7915. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7916. *------------------------------------------*/
  7917. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7918. {
  7919. if (type&2) {
  7920. if (hp || type&4)
  7921. clif_heal(sd->fd,SP_HP,hp);
  7922. if (sp)
  7923. clif_heal(sd->fd,SP_SP,sp);
  7924. } else {
  7925. if(hp)
  7926. clif_updatestatus(sd,SP_HP);
  7927. if(sp)
  7928. clif_updatestatus(sd,SP_SP);
  7929. }
  7930. return;
  7931. }
  7932. /**
  7933. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7934. * @param sd: Player data
  7935. * @param itemid: Item ID
  7936. * @param hp: HP to heal
  7937. * @param sp: SP to heal
  7938. * @return Amount healed to an object
  7939. */
  7940. int pc_itemheal(struct map_session_data *sd, t_itemid itemid, int hp, int sp)
  7941. {
  7942. int bonus, tmp, penalty = 0;
  7943. if (hp) {
  7944. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7945. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7946. if (potion_flag == 2) {
  7947. bonus += bonus * 50 / 100;
  7948. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7949. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  7950. }
  7951. //All item bonuses.
  7952. bonus += sd->bonus.itemhealrate2;
  7953. //Item Group bonuses
  7954. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid) / 100;
  7955. //Individual item bonuses.
  7956. for(const auto &it : sd->itemhealrate) {
  7957. if (it.id == itemid) {
  7958. bonus += bonus * it.val / 100;
  7959. break;
  7960. }
  7961. }
  7962. // Recovery Potion
  7963. if (sd->sc.data[SC_INCHEALRATE])
  7964. bonus += bonus * sd->sc.data[SC_INCHEALRATE]->val1 / 100;
  7965. // 2014 Halloween Event : Pumpkin Bonus
  7966. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7967. bonus += bonus * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100;
  7968. tmp = hp * bonus / 100; // Overflow check
  7969. if (bonus != 100 && tmp > hp)
  7970. hp = tmp;
  7971. }
  7972. if (sp) {
  7973. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7974. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7975. if (potion_flag == 2)
  7976. bonus += bonus * 50 / 100;
  7977. tmp = sp * bonus / 100; // Overflow check
  7978. if (bonus != 100 && tmp > sp)
  7979. sp = tmp;
  7980. }
  7981. if (sd->sc.count) {
  7982. // Critical Wound and Death Hurt stack
  7983. if (sd->sc.data[SC_CRITICALWOUND])
  7984. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7985. if (sd->sc.data[SC_DEATHHURT] && sd->sc.data[SC_DEATHHURT]->val3 == 1)
  7986. penalty += 20;
  7987. if (sd->sc.data[SC_NORECOVER_STATE])
  7988. penalty = 100;
  7989. if (sd->sc.data[SC_VITALITYACTIVATION])
  7990. hp += hp / 2; // 1.5 times
  7991. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7992. hp += hp / 10;
  7993. sp += sp / 10;
  7994. }
  7995. #ifdef RENEWAL
  7996. if (sd->sc.data[SC_APPLEIDUN])
  7997. hp += sd->sc.data[SC_APPLEIDUN]->val3 / 100;
  7998. #endif
  7999. if (penalty > 0) {
  8000. hp -= hp * penalty / 100;
  8001. sp -= sp * penalty / 100;
  8002. }
  8003. #ifdef RENEWAL
  8004. if (sd->sc.data[SC_EXTREMITYFIST2])
  8005. sp = 0;
  8006. #endif
  8007. if (sd->sc.data[SC_BITESCAR])
  8008. hp = 0;
  8009. }
  8010. return status_heal(&sd->bl, hp, sp, 1);
  8011. }
  8012. /*==========================================
  8013. * HP/SP Recovery
  8014. * Heal player hp nad/or sp by rate
  8015. *------------------------------------------*/
  8016. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  8017. {
  8018. nullpo_ret(sd);
  8019. if (hp > 100) hp = 100;
  8020. else if (hp <-100) hp = -100;
  8021. if (sp > 100) sp = 100;
  8022. else if (sp <-100) sp = -100;
  8023. if(hp >= 0 && sp >= 0) //Heal
  8024. return status_percent_heal(&sd->bl, hp, sp);
  8025. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  8026. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  8027. //Crossed signs
  8028. if(hp) {
  8029. if(hp > 0)
  8030. status_percent_heal(&sd->bl, hp, 0);
  8031. else
  8032. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  8033. }
  8034. if(sp) {
  8035. if(sp > 0)
  8036. status_percent_heal(&sd->bl, 0, sp);
  8037. else
  8038. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  8039. }
  8040. return 0;
  8041. }
  8042. static int jobchange_killclone(struct block_list *bl, va_list ap)
  8043. {
  8044. struct mob_data *md;
  8045. int flag;
  8046. md = (struct mob_data *)bl;
  8047. nullpo_ret(md);
  8048. flag = va_arg(ap, int);
  8049. if (md->master_id && md->special_state.clone && md->master_id == flag)
  8050. status_kill(&md->bl);
  8051. return 1;
  8052. }
  8053. /**
  8054. * Called when player changes job
  8055. * Rewrote to make it tidider [Celest]
  8056. * @param sd
  8057. * @param job JOB ID. See enum e_job
  8058. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  8059. * @return True if success, false if failed
  8060. **/
  8061. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  8062. {
  8063. int i, fame_flag = 0;
  8064. int b_class;
  8065. nullpo_retr(false,sd);
  8066. if (job < 0)
  8067. return false;
  8068. //Normalize job.
  8069. b_class = pc_jobid2mapid(job);
  8070. if (b_class == -1)
  8071. return false;
  8072. switch (upper) {
  8073. case 1:
  8074. b_class|= JOBL_UPPER;
  8075. break;
  8076. case 2:
  8077. b_class|= JOBL_BABY;
  8078. break;
  8079. }
  8080. //This will automatically adjust bard/dancer classes to the correct gender
  8081. //That is, if you try to jobchange into dancer, it will turn you to bard.
  8082. job = pc_mapid2jobid(b_class, sd->status.sex);
  8083. if (job == -1)
  8084. return false;
  8085. if ((unsigned short)b_class == sd->class_)
  8086. return false; //Nothing to change.
  8087. // changing from 1st to 2nd job
  8088. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  8089. sd->change_level_2nd = sd->status.job_level;
  8090. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  8091. }
  8092. // changing from 2nd to 3rd job
  8093. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  8094. sd->change_level_3rd = sd->status.job_level;
  8095. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  8096. }
  8097. if(sd->cloneskill_idx > 0) {
  8098. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  8099. sd->status.skill[sd->cloneskill_idx].id = 0;
  8100. sd->status.skill[sd->cloneskill_idx].lv = 0;
  8101. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  8102. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM))));
  8103. }
  8104. sd->cloneskill_idx = 0;
  8105. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  8106. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  8107. }
  8108. if(sd->reproduceskill_idx > 0) {
  8109. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  8110. sd->status.skill[sd->reproduceskill_idx].id = 0;
  8111. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  8112. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  8113. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE))));
  8114. }
  8115. sd->reproduceskill_idx = 0;
  8116. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  8117. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  8118. }
  8119. // Give or reduce transcendent status points
  8120. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  8121. sd->status.status_point += battle_config.transcendent_status_points;
  8122. clif_updatestatus(sd,SP_STATUSPOINT);
  8123. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  8124. if( sd->status.status_point < battle_config.transcendent_status_points ){
  8125. // The player already used his bonus points, so we have to reset his status points
  8126. pc_resetstate(sd);
  8127. }
  8128. sd->status.status_point -= battle_config.transcendent_status_points;
  8129. clif_updatestatus(sd,SP_STATUSPOINT);
  8130. }
  8131. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  8132. const int class_ = pc_class2idx(sd->status.class_);
  8133. uint16 skill_id;
  8134. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  8135. //Remove status specific to your current tree skills.
  8136. enum sc_type sc = status_skill2sc(skill_id);
  8137. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  8138. status_change_end(&sd->bl, sc, INVALID_TIMER);
  8139. }
  8140. }
  8141. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  8142. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  8143. pc_resetfeel(sd);
  8144. pc_resethate(sd);
  8145. }
  8146. // Reset body style to 0 before changing job to avoid
  8147. // errors since not every job has a alternate outfit.
  8148. sd->status.body = 0;
  8149. clif_changelook(&sd->bl,LOOK_BODY2,0);
  8150. sd->status.class_ = job;
  8151. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  8152. sd->class_ = (unsigned short)b_class;
  8153. sd->status.job_level=1;
  8154. sd->status.job_exp=0;
  8155. if (sd->status.base_level > pc_maxbaselv(sd)) {
  8156. sd->status.base_level = pc_maxbaselv(sd);
  8157. sd->status.base_exp=0;
  8158. pc_resetstate(sd);
  8159. clif_updatestatus(sd,SP_STATUSPOINT);
  8160. clif_updatestatus(sd,SP_BASELEVEL);
  8161. clif_updatestatus(sd,SP_BASEEXP);
  8162. clif_updatestatus(sd,SP_NEXTBASEEXP);
  8163. }
  8164. clif_updatestatus(sd,SP_JOBLEVEL);
  8165. clif_updatestatus(sd,SP_JOBEXP);
  8166. clif_updatestatus(sd,SP_NEXTJOBEXP);
  8167. for(i=0;i<EQI_MAX;i++) {
  8168. if(sd->equip_index[i] >= 0)
  8169. if(pc_isequip(sd,sd->equip_index[i]))
  8170. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  8171. }
  8172. //Change look, if disguised, you need to undisguise
  8173. //to correctly calculate new job sprite without
  8174. if (sd->disguise)
  8175. pc_disguise(sd, 0);
  8176. status_set_viewdata(&sd->bl, job);
  8177. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  8178. #if PACKETVER >= 20151001
  8179. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  8180. #endif
  8181. if(sd->vd.cloth_color)
  8182. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8183. /*
  8184. if(sd->vd.body_style)
  8185. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8186. */
  8187. //Update skill tree.
  8188. pc_calc_skilltree(sd);
  8189. clif_skillinfoblock(sd);
  8190. if (sd->ed)
  8191. elemental_delete(sd->ed);
  8192. if (sd->state.vending)
  8193. vending_closevending(sd);
  8194. if (sd->state.buyingstore)
  8195. buyingstore_close(sd);
  8196. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  8197. //Remove peco/cart/falcon
  8198. i = sd->sc.option;
  8199. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  8200. i&=~OPTION_RIDING;
  8201. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  8202. i&=~OPTION_FALCON;
  8203. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  8204. i&=~OPTION_DRAGON;
  8205. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  8206. i&=~OPTION_WUGRIDER;
  8207. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  8208. i&=~OPTION_WUG;
  8209. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  8210. i&=~OPTION_MADOGEAR;
  8211. #ifndef NEW_CARTS
  8212. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  8213. i&=~OPTION_CART;
  8214. #else
  8215. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  8216. pc_setcart(sd, 0);
  8217. #endif
  8218. if(i != sd->sc.option)
  8219. pc_setoption(sd, i);
  8220. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  8221. hom_vaporize(sd, HOM_ST_ACTIVE);
  8222. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  8223. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  8224. if(sd->status.manner < 0)
  8225. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  8226. status_calc_pc(sd,SCO_FORCE);
  8227. pc_checkallowskill(sd);
  8228. pc_equiplookall(sd);
  8229. pc_show_questinfo(sd);
  8230. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  8231. if( sd->status.party_id ){
  8232. struct party_data* p;
  8233. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  8234. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  8235. if( i < MAX_PARTY ){
  8236. p->party.member[i].class_ = sd->status.class_;
  8237. clif_party_job_and_level(sd);
  8238. }
  8239. }
  8240. }
  8241. chrif_save(sd, CSAVE_NORMAL);
  8242. //if you were previously famous, not anymore.
  8243. if (fame_flag)
  8244. chrif_buildfamelist();
  8245. else if (sd->status.fame > 0) {
  8246. //It may be that now they are famous?
  8247. switch (sd->class_&MAPID_UPPERMASK) {
  8248. case MAPID_BLACKSMITH:
  8249. case MAPID_ALCHEMIST:
  8250. case MAPID_TAEKWON:
  8251. chrif_buildfamelist();
  8252. break;
  8253. }
  8254. }
  8255. return true;
  8256. }
  8257. /*==========================================
  8258. * Tell client player sd has change equipement
  8259. *------------------------------------------*/
  8260. void pc_equiplookall(struct map_session_data *sd)
  8261. {
  8262. nullpo_retv(sd);
  8263. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8264. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8265. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8266. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8267. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8268. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  8269. }
  8270. /*==========================================
  8271. * Tell client player sd has change look (hair,equip...)
  8272. *------------------------------------------*/
  8273. void pc_changelook(struct map_session_data *sd,int type,int val) {
  8274. nullpo_retv(sd);
  8275. switch(type) {
  8276. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  8277. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  8278. if (sd->status.hair != val) {
  8279. sd->status.hair = val;
  8280. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8281. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8282. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  8283. }
  8284. break;
  8285. case LOOK_WEAPON:
  8286. sd->status.weapon = val;
  8287. break;
  8288. case LOOK_HEAD_BOTTOM:
  8289. sd->status.head_bottom = val;
  8290. sd->setlook_head_bottom = val;
  8291. break;
  8292. case LOOK_HEAD_TOP:
  8293. sd->status.head_top = val;
  8294. sd->setlook_head_top = val;
  8295. break;
  8296. case LOOK_HEAD_MID:
  8297. sd->status.head_mid = val;
  8298. sd->setlook_head_mid = val;
  8299. break;
  8300. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  8301. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  8302. if (sd->status.hair_color != val) {
  8303. sd->status.hair_color = val;
  8304. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8305. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8306. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  8307. }
  8308. break;
  8309. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  8310. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  8311. sd->status.clothes_color = val;
  8312. break;
  8313. case LOOK_SHIELD:
  8314. sd->status.shield = val;
  8315. break;
  8316. case LOOK_SHOES:
  8317. break;
  8318. case LOOK_ROBE:
  8319. sd->status.robe = val;
  8320. sd->setlook_robe = val;
  8321. break;
  8322. case LOOK_BODY2:
  8323. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  8324. sd->status.body = val;
  8325. break;
  8326. }
  8327. clif_changelook(&sd->bl, type, val);
  8328. }
  8329. /*==========================================
  8330. * Give an option (type) to player (sd) and display it to client
  8331. *------------------------------------------*/
  8332. void pc_setoption(struct map_session_data *sd,int type)
  8333. {
  8334. int p_type, new_look=0;
  8335. nullpo_retv(sd);
  8336. p_type = sd->sc.option;
  8337. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  8338. sd->sc.option=type;
  8339. clif_changeoption(&sd->bl);
  8340. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8341. { // Mounting
  8342. clif_status_load(&sd->bl,EFST_RIDING,1);
  8343. status_calc_pc(sd,SCO_NONE);
  8344. }
  8345. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8346. { // Dismount
  8347. clif_status_load(&sd->bl,EFST_RIDING,0);
  8348. status_calc_pc(sd,SCO_NONE);
  8349. }
  8350. #ifndef NEW_CARTS
  8351. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  8352. clif_cartlist(sd);
  8353. clif_updatestatus(sd, SP_CARTINFO);
  8354. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8355. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  8356. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  8357. clif_clearcart(sd->fd);
  8358. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8359. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  8360. }
  8361. #endif
  8362. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  8363. clif_status_load(&sd->bl,EFST_FALCON,1);
  8364. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  8365. clif_status_load(&sd->bl,EFST_FALCON,0);
  8366. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  8367. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  8368. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  8369. status_calc_pc(sd,SCO_NONE);
  8370. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  8371. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  8372. status_calc_pc(sd,SCO_NONE);
  8373. }
  8374. }
  8375. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  8376. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  8377. status_calc_pc(sd,SCO_NONE);
  8378. for (const auto &sc : mado_statuses) {
  8379. uint16 skill_id = status_sc2skill(sc);
  8380. if (skill_id > 0 && !skill_get_inf2(skill_id, INF2_ALLOWONMADO))
  8381. status_change_end(&sd->bl,sc,INVALID_TIMER);
  8382. }
  8383. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8384. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106
  8385. clif_status_load( &sd->bl, EFST_MADOGEAR, 1 );
  8386. #endif
  8387. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  8388. status_calc_pc(sd,SCO_NONE);
  8389. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  8390. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  8391. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  8392. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  8393. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  8394. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  8395. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  8396. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  8397. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8398. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106
  8399. clif_status_load( &sd->bl, EFST_MADOGEAR, 0 );
  8400. #endif
  8401. }
  8402. }
  8403. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  8404. new_look = JOB_STAR_GLADIATOR2;
  8405. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  8406. new_look = -1;
  8407. if (sd->disguise || !new_look)
  8408. return; //Disguises break sprite changes
  8409. if (new_look < 0) { //Restore normal look.
  8410. status_set_viewdata(&sd->bl, sd->status.class_);
  8411. new_look = sd->vd.class_;
  8412. }
  8413. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  8414. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  8415. if (sd->vd.cloth_color)
  8416. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8417. if( sd->vd.body_style )
  8418. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8419. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  8420. }
  8421. /**
  8422. * Give player a cart
  8423. * @param sd Player
  8424. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8425. **/
  8426. bool pc_setcart(struct map_session_data *sd,int type) {
  8427. #ifndef NEW_CARTS
  8428. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8429. int option;
  8430. #endif
  8431. nullpo_retr(false,sd);
  8432. if( type < 0 || type > MAX_CARTS )
  8433. return false;// Never trust the values sent by the client! [Skotlex]
  8434. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8435. return false;// Push cart is required
  8436. #ifdef NEW_CARTS
  8437. switch( type ) {
  8438. case 0:
  8439. if( !sd->sc.data[SC_PUSH_CART] )
  8440. return 0;
  8441. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8442. clif_clearcart(sd->fd);
  8443. break;
  8444. default:/* everything else is an allowed ID so we can move on */
  8445. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8446. clif_cartlist(sd);
  8447. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8448. }
  8449. clif_updatestatus(sd, SP_CARTINFO);
  8450. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8451. break;
  8452. }
  8453. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8454. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8455. #else
  8456. // Update option
  8457. option = sd->sc.option;
  8458. option &= ~OPTION_CART;// clear cart bits
  8459. option |= cart[type]; // set cart
  8460. pc_setoption(sd, option);
  8461. #endif
  8462. return true;
  8463. }
  8464. /*==========================================
  8465. * Give player a falcon
  8466. *------------------------------------------*/
  8467. void pc_setfalcon(struct map_session_data* sd, int flag)
  8468. {
  8469. if( flag ){
  8470. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8471. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8472. } else if( pc_isfalcon(sd) ){
  8473. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8474. }
  8475. }
  8476. /*==========================================
  8477. * Set player riding
  8478. *------------------------------------------*/
  8479. void pc_setriding(struct map_session_data* sd, int flag)
  8480. {
  8481. if( sd->sc.data[SC_ALL_RIDING] )
  8482. return;
  8483. if( flag ){
  8484. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8485. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8486. } else if( pc_isriding(sd) ){
  8487. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8488. }
  8489. }
  8490. /*==========================================
  8491. * Give player a mado
  8492. *------------------------------------------*/
  8493. void pc_setmadogear(struct map_session_data* sd, int flag)
  8494. {
  8495. if( flag ){
  8496. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8497. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8498. } else if( pc_ismadogear(sd) ){
  8499. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8500. }
  8501. }
  8502. /*==========================================
  8503. * Check if player can drop an item
  8504. *------------------------------------------*/
  8505. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8506. {
  8507. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  8508. return false;
  8509. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8510. return false;
  8511. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8512. }
  8513. /**
  8514. * Determines whether a player can attack based on status changes
  8515. * Why not use status_check_skilluse?
  8516. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8517. * Even ground-based attacks should be blocked by these statuses
  8518. * Called from unit_attack and unit_attack_timer_sub
  8519. * @retval true Can attack
  8520. **/
  8521. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8522. nullpo_retr(false, sd);
  8523. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8524. return false;
  8525. if (sd->state.block_action & PCBLOCK_ATTACK)
  8526. return false;
  8527. if(
  8528. #ifdef RENEWAL
  8529. sd->sc.data[SC_BASILICA_CELL] ||
  8530. #else
  8531. sd->sc.data[SC_BASILICA] ||
  8532. #endif
  8533. sd->sc.data[SC__SHADOWFORM] ||
  8534. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8535. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8536. sd->sc.data[SC_CRYSTALIZE] ||
  8537. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8538. sd->sc.data[SC_TRICKDEAD] ||
  8539. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8540. sd->sc.data[SC_BLADESTOP] ||
  8541. sd->sc.data[SC_DEEPSLEEP] ||
  8542. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8543. sd->sc.data[SC_KINGS_GRACE] )
  8544. return false;
  8545. return true;
  8546. }
  8547. /*==========================================
  8548. * Read '@type' variables (temporary numeric char reg)
  8549. *------------------------------------------*/
  8550. int64 pc_readreg(struct map_session_data* sd, int64 reg)
  8551. {
  8552. return i64db_i64get(sd->regs.vars, reg);
  8553. }
  8554. /*==========================================
  8555. * Set '@type' variables (temporary numeric char reg)
  8556. *------------------------------------------*/
  8557. bool pc_setreg(struct map_session_data* sd, int64 reg, int64 val)
  8558. {
  8559. uint32 index = script_getvaridx(reg);
  8560. nullpo_retr(false, sd);
  8561. if( val ) {
  8562. i64db_i64put(sd->regs.vars, reg, val);
  8563. if( index )
  8564. script_array_update(&sd->regs, reg, false);
  8565. } else {
  8566. i64db_remove(sd->regs.vars, reg);
  8567. if( index )
  8568. script_array_update(&sd->regs, reg, true);
  8569. }
  8570. return true;
  8571. }
  8572. /*==========================================
  8573. * Read '@type$' variables (temporary string char reg)
  8574. *------------------------------------------*/
  8575. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8576. {
  8577. struct script_reg_str *p = NULL;
  8578. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8579. return p ? p->value : NULL;
  8580. }
  8581. /*==========================================
  8582. * Set '@type$' variables (temporary string char reg)
  8583. *------------------------------------------*/
  8584. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8585. {
  8586. struct script_reg_str *p = NULL;
  8587. unsigned int index = script_getvaridx(reg);
  8588. DBData prev;
  8589. nullpo_retr(false, sd);
  8590. if( str[0] ) {
  8591. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8592. p->value = aStrdup(str);
  8593. p->flag.type = 1;
  8594. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8595. p = (struct script_reg_str *)db_data2ptr(&prev);
  8596. if( p->value )
  8597. aFree(p->value);
  8598. ers_free(str_reg_ers, p);
  8599. } else {
  8600. if( index )
  8601. script_array_update(&sd->regs, reg, false);
  8602. }
  8603. } else {
  8604. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8605. p = (struct script_reg_str *)db_data2ptr(&prev);
  8606. if( p->value )
  8607. aFree(p->value);
  8608. ers_free(str_reg_ers, p);
  8609. if( index )
  8610. script_array_update(&sd->regs, reg, true);
  8611. }
  8612. }
  8613. return true;
  8614. }
  8615. /**
  8616. * Serves the following variable types:
  8617. * - 'type' (permanent numeric char reg)
  8618. * - '#type' (permanent numeric account reg)
  8619. * - '##type' (permanent numeric account reg2)
  8620. **/
  8621. int64 pc_readregistry(struct map_session_data *sd, int64 reg)
  8622. {
  8623. struct script_reg_num *p = NULL;
  8624. if (!sd->vars_ok) {
  8625. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8626. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8627. //intif->request_registry(sd,type==3?4:type);
  8628. set_eof(sd->fd);
  8629. return 0;
  8630. }
  8631. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8632. return p ? p->value : 0;
  8633. }
  8634. /**
  8635. * Serves the following variable types:
  8636. * - 'type$' (permanent str char reg)
  8637. * - '#type$' (permanent str account reg)
  8638. * - '##type$' (permanent str account reg2)
  8639. **/
  8640. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8641. {
  8642. struct script_reg_str *p = NULL;
  8643. if (!sd->vars_ok) {
  8644. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8645. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8646. //intif->request_registry(sd,type==3?4:type);
  8647. set_eof(sd->fd);
  8648. return NULL;
  8649. }
  8650. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8651. return p ? p->value : NULL;
  8652. }
  8653. /**
  8654. * Serves the following variable types:
  8655. * - 'type' (permanent numeric char reg)
  8656. * - '#type' (permanent numeric account reg)
  8657. * - '##type' (permanent numeric account reg2)
  8658. **/
  8659. bool pc_setregistry(struct map_session_data *sd, int64 reg, int64 val)
  8660. {
  8661. struct script_reg_num *p = NULL;
  8662. const char *regname = get_str(script_getvarid(reg));
  8663. uint32 index = script_getvaridx(reg);
  8664. if ( !reg_load && !sd->vars_ok ) {
  8665. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8666. return false;
  8667. }
  8668. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8669. if( val ) {
  8670. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8671. script_array_update(&sd->regs, reg, false);
  8672. p->value = val;
  8673. } else {
  8674. p->value = 0;
  8675. if( index )
  8676. script_array_update(&sd->regs, reg, true);
  8677. }
  8678. if (!reg_load)
  8679. p->flag.update = 1;/* either way, it will require either delete or replace */
  8680. } else if( val ) {
  8681. DBData prev;
  8682. if( index )
  8683. script_array_update(&sd->regs, reg, false);
  8684. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8685. p->value = val;
  8686. if (!reg_load)
  8687. p->flag.update = 1;
  8688. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8689. p = (struct script_reg_num *)db_data2ptr(&prev);
  8690. ers_free(num_reg_ers, p);
  8691. }
  8692. }
  8693. if (!reg_load && p)
  8694. sd->vars_dirty = true;
  8695. return true;
  8696. }
  8697. /**
  8698. * Serves the following variable types:
  8699. * - 'type$' (permanent str char reg)
  8700. * - '#type$' (permanent str account reg)
  8701. * - '##type$' (permanent str account reg2)
  8702. **/
  8703. bool pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8704. {
  8705. struct script_reg_str *p = NULL;
  8706. const char *regname = get_str(script_getvarid(reg));
  8707. unsigned int index = script_getvaridx(reg);
  8708. size_t vlen = 0;
  8709. if (!reg_load && !sd->vars_ok) {
  8710. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8711. return false;
  8712. }
  8713. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8714. {
  8715. 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);
  8716. return false;
  8717. }
  8718. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8719. if( val[0] ) {
  8720. if( p->value )
  8721. aFree(p->value);
  8722. else if ( index ) // an entry that was deleted, so we reset
  8723. script_array_update(&sd->regs, reg, false);
  8724. p->value = aStrdup(val);
  8725. } else {
  8726. if (p->value)
  8727. aFree(p->value);
  8728. p->value = NULL;
  8729. if( index )
  8730. script_array_update(&sd->regs, reg, true);
  8731. }
  8732. if( !reg_load )
  8733. p->flag.update = 1; // either way, it will require either delete or replace
  8734. } else if( val[0] ) {
  8735. DBData prev;
  8736. if( index )
  8737. script_array_update(&sd->regs, reg, false);
  8738. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8739. p->value = aStrdup(val);
  8740. if( !reg_load )
  8741. p->flag.update = 1;
  8742. p->flag.type = 1;
  8743. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8744. p = (struct script_reg_str *)db_data2ptr(&prev);
  8745. if( p->value )
  8746. aFree(p->value);
  8747. ers_free(str_reg_ers, p);
  8748. }
  8749. }
  8750. if( !reg_load && p )
  8751. sd->vars_dirty = true;
  8752. return true;
  8753. }
  8754. /**
  8755. * Set value of player variable
  8756. * @param sd Player
  8757. * @param reg Variable name
  8758. * @param value
  8759. * @return True if success, false if failed.
  8760. **/
  8761. bool pc_setreg2(struct map_session_data *sd, const char *reg, int64 val) {
  8762. char prefix = reg[0];
  8763. nullpo_retr(false, sd);
  8764. if (reg[strlen(reg)-1] == '$') {
  8765. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8766. return false;
  8767. }
  8768. val = cap_value(val, INT_MIN, INT_MAX);
  8769. switch (prefix) {
  8770. case '.':
  8771. case '\'':
  8772. case '$':
  8773. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8774. return false;
  8775. case '@':
  8776. return pc_setreg(sd, add_str(reg), val);
  8777. case '#':
  8778. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  8779. default:
  8780. return pc_setglobalreg(sd, add_str(reg), val);
  8781. }
  8782. return false;
  8783. }
  8784. /**
  8785. * Get value of player variable
  8786. * @param sd Player
  8787. * @param reg Variable name
  8788. * @return Variable value or 0 if failed.
  8789. **/
  8790. int64 pc_readreg2(struct map_session_data *sd, const char *reg) {
  8791. char prefix = reg[0];
  8792. nullpo_ret(sd);
  8793. if (reg[strlen(reg)-1] == '$') {
  8794. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8795. return 0;
  8796. }
  8797. switch (prefix) {
  8798. case '.':
  8799. case '\'':
  8800. case '$':
  8801. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8802. return 0;
  8803. case '@':
  8804. return pc_readreg(sd, add_str(reg));
  8805. case '#':
  8806. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8807. default:
  8808. return pc_readglobalreg(sd, add_str(reg));
  8809. }
  8810. return 0;
  8811. }
  8812. /*==========================================
  8813. * Exec eventtimer for player sd (retrieved from map_session (id))
  8814. *------------------------------------------*/
  8815. static TIMER_FUNC(pc_eventtimer){
  8816. struct map_session_data *sd=map_id2sd(id);
  8817. char *p = (char *)data;
  8818. int i;
  8819. if(sd==NULL)
  8820. return 0;
  8821. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8822. if( i < MAX_EVENTTIMER )
  8823. {
  8824. sd->eventtimer[i] = INVALID_TIMER;
  8825. sd->eventcount--;
  8826. npc_event(sd,p,0);
  8827. }
  8828. else
  8829. ShowError("pc_eventtimer: no such event timer\n");
  8830. if (p) aFree(p);
  8831. return 0;
  8832. }
  8833. /*==========================================
  8834. * Add eventtimer for player sd ?
  8835. *------------------------------------------*/
  8836. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8837. {
  8838. int i;
  8839. nullpo_retr(false,sd);
  8840. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8841. if( i == MAX_EVENTTIMER )
  8842. return false;
  8843. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8844. sd->eventcount++;
  8845. return true;
  8846. }
  8847. /*==========================================
  8848. * Del eventtimer for player sd ?
  8849. *------------------------------------------*/
  8850. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8851. {
  8852. char* p = NULL;
  8853. int i;
  8854. nullpo_retr(false,sd);
  8855. if (sd->eventcount == 0)
  8856. return false;
  8857. // find the named event timer
  8858. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8859. sd->eventtimer[i] != INVALID_TIMER &&
  8860. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8861. strcmp(p, name) == 0
  8862. );
  8863. if( i == MAX_EVENTTIMER )
  8864. return false; // not found
  8865. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8866. sd->eventtimer[i] = INVALID_TIMER;
  8867. if(sd->eventcount > 0)
  8868. sd->eventcount--;
  8869. aFree(p);
  8870. return true;
  8871. }
  8872. /*==========================================
  8873. * Update eventtimer count for player sd
  8874. *------------------------------------------*/
  8875. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8876. {
  8877. int i;
  8878. nullpo_retv(sd);
  8879. for(i=0;i<MAX_EVENTTIMER;i++)
  8880. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8881. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8882. addt_tickimer(sd->eventtimer[i],tick);
  8883. break;
  8884. }
  8885. }
  8886. /*==========================================
  8887. * Remove all eventtimer for player sd
  8888. *------------------------------------------*/
  8889. void pc_cleareventtimer(struct map_session_data *sd)
  8890. {
  8891. int i;
  8892. nullpo_retv(sd);
  8893. if (sd->eventcount == 0)
  8894. return;
  8895. for(i=0;i<MAX_EVENTTIMER;i++){
  8896. if( sd->eventtimer[i] != INVALID_TIMER ){
  8897. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8898. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8899. sd->eventtimer[i] = INVALID_TIMER;
  8900. if(sd->eventcount > 0) //avoid looping to max val
  8901. sd->eventcount--;
  8902. if (p) aFree(p);
  8903. }
  8904. }
  8905. }
  8906. /**
  8907. * Called when an item with combo is worn
  8908. * @param sd: Player data
  8909. * @param data: Item data
  8910. * @return Number of succeeded combo(s)
  8911. */
  8912. static int pc_checkcombo(struct map_session_data *sd, item_data *data) {
  8913. int success = 0;
  8914. for (const auto &item_combo : data->combos) {
  8915. bool do_continue = false;
  8916. // Ensure this isn't a duplicate combo
  8917. for (const auto player_combo : sd->combos) {
  8918. if (player_combo->id == item_combo->id) {
  8919. do_continue = true;
  8920. break;
  8921. }
  8922. }
  8923. // Combo already equipped
  8924. if (do_continue)
  8925. continue;
  8926. size_t nb_itemCombo = item_combo->nameid.size();
  8927. if (nb_itemCombo < 2) // A combo with less then 2 item?
  8928. continue;
  8929. struct s_itemchk {
  8930. int idx;
  8931. t_itemid nameid, card[MAX_SLOTS];
  8932. };
  8933. std::vector<s_itemchk> combo_idx;
  8934. size_t j;
  8935. unsigned int pos = 0;
  8936. combo_idx.reserve(nb_itemCombo);
  8937. // Zero out temporary combo array
  8938. for (auto &tmp_combo : combo_idx) {
  8939. tmp_combo = {};
  8940. }
  8941. for (j = 0; j < nb_itemCombo; j++) {
  8942. t_itemid id = item_combo->nameid[j];
  8943. bool found = false;
  8944. for (int16 k = 0; k < EQI_MAX; k++) {
  8945. short index = sd->equip_index[k];
  8946. if (index < 0)
  8947. continue;
  8948. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  8949. continue;
  8950. if (!sd->inventory_data[index])
  8951. continue;
  8952. if (itemdb_type(id) != IT_CARD) {
  8953. if (sd->inventory_data[index]->nameid != id)
  8954. continue;
  8955. if (j > 0) { // Check if this item not already used
  8956. do_continue = false;
  8957. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  8958. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  8959. do_continue = true;
  8960. break;
  8961. }
  8962. }
  8963. if (do_continue)
  8964. continue;
  8965. }
  8966. combo_idx[j].nameid = id;
  8967. combo_idx[j].idx = index;
  8968. pos |= sd->inventory.u.items_inventory[index].equip;
  8969. found = true;
  8970. break;
  8971. } else { // Cards and enchants
  8972. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  8973. continue;
  8974. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  8975. do_continue = false;
  8976. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8977. continue;
  8978. if (j > 0) {
  8979. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  8980. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  8981. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  8982. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  8983. do_continue = true;
  8984. break;
  8985. }
  8986. }
  8987. }
  8988. }
  8989. }
  8990. if (do_continue)
  8991. continue;
  8992. combo_idx[j].nameid = id;
  8993. combo_idx[j].idx = index;
  8994. combo_idx[j].card[z] = id;
  8995. pos |= sd->inventory.u.items_inventory[index].equip;
  8996. found = true;
  8997. break;
  8998. }
  8999. }
  9000. }
  9001. if (!found)
  9002. break; // Unable to found all the IDs for this combo, return
  9003. }
  9004. // Broke out of the count loop without finding all IDs, move to the next combo
  9005. if (j < nb_itemCombo)
  9006. continue;
  9007. // All items in the combo are matching
  9008. auto entry = std::make_shared<s_combos>();
  9009. entry->bonus = item_combo->script;
  9010. entry->id = item_combo->id;
  9011. entry->pos = pos;
  9012. sd->combos.push_back(entry);
  9013. combo_idx.clear();
  9014. success++;
  9015. }
  9016. return success;
  9017. }
  9018. /**
  9019. * Called when an item with combo is removed
  9020. * @param sd: Player data
  9021. * @param data: Item data
  9022. * @return Number of removed combo(s)
  9023. */
  9024. static int pc_removecombo(struct map_session_data *sd, item_data *data ) {
  9025. if (sd->combos.empty())
  9026. return 0; // Nothing to do here, player has no combos
  9027. int retval = 0;
  9028. for (const auto &item_combo : data->combos) {
  9029. std::shared_ptr<s_combos> del_combo = nullptr;
  9030. // Check if this combo exists on this player
  9031. for (const auto &player_combo : sd->combos) {
  9032. if (player_combo->id == item_combo->id) {
  9033. del_combo = player_combo;
  9034. break;
  9035. }
  9036. }
  9037. // No match, skip this combo
  9038. if (del_combo == nullptr)
  9039. continue;
  9040. util::vector_erase_if_exists(sd->combos, del_combo);
  9041. retval++;
  9042. // Check if combo requirements still fit
  9043. if (pc_checkcombo(sd, data))
  9044. continue;
  9045. // It's empty, clear all the memory
  9046. if (sd->combos.empty()) {
  9047. sd->combos.clear();
  9048. return retval; // Return at this point as there are no more combos to check
  9049. }
  9050. }
  9051. return retval;
  9052. }
  9053. /**
  9054. * Load combo data(s) of player
  9055. * @param sd: Player data
  9056. * @return ret numbers of succeed combo
  9057. */
  9058. int pc_load_combo(struct map_session_data *sd) {
  9059. int ret = 0;
  9060. for (int16 i = 0; i < EQI_MAX; i++) {
  9061. item_data *id;
  9062. short idx = sd->equip_index[i];
  9063. if (idx < 0 || !(id = sd->inventory_data[idx]))
  9064. continue;
  9065. if (!id->combos.empty())
  9066. ret += pc_checkcombo(sd, id);
  9067. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  9068. item_data *data;
  9069. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  9070. if (!sd->inventory.u.items_inventory[idx].card[j])
  9071. continue;
  9072. if ((data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j])) != nullptr) {
  9073. if (!data->combos.empty())
  9074. ret += pc_checkcombo(sd, data);
  9075. }
  9076. }
  9077. }
  9078. }
  9079. return ret;
  9080. }
  9081. /*==========================================
  9082. * Equip item on player sd at req_pos from inventory index n
  9083. * return: false - fail; true - success
  9084. *------------------------------------------*/
  9085. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  9086. {
  9087. int i, pos, flag = 0, iflag;
  9088. struct item_data *id;
  9089. uint8 res = ITEM_EQUIP_ACK_OK;
  9090. short* equip_index;
  9091. nullpo_retr(false,sd);
  9092. if( n < 0 || n >= MAX_INVENTORY ) {
  9093. if( equipswitch ){
  9094. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9095. }else{
  9096. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  9097. }
  9098. return false;
  9099. }
  9100. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  9101. if( equipswitch ){
  9102. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9103. }else{
  9104. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  9105. }
  9106. return false;
  9107. }
  9108. if (!(id = sd->inventory_data[n]))
  9109. return false;
  9110. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  9111. if(battle_config.battle_log && !equipswitch)
  9112. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  9113. if((res = pc_isequip(sd,n))) {
  9114. if( equipswitch ){
  9115. clif_equipswitch_add( sd, n, req_pos, res );
  9116. }else{
  9117. clif_equipitemack(sd,n,0,res); // fail
  9118. }
  9119. return false;
  9120. }
  9121. if( equipswitch && id->type == IT_AMMO ){
  9122. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9123. return false;
  9124. }
  9125. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  9126. if( equipswitch ){
  9127. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9128. }else{
  9129. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  9130. }
  9131. return false;
  9132. }
  9133. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9134. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  9135. if( equipswitch ){
  9136. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9137. }else{
  9138. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  9139. }
  9140. return false;
  9141. }
  9142. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  9143. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  9144. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  9145. clif_notify_bindOnEquip(sd,n);
  9146. }
  9147. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  9148. pos = req_pos&EQP_ACC;
  9149. if (pos == EQP_ACC) //User specified both slots.
  9150. pos = equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  9151. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  9152. if (!sd->inventory.u.items_inventory[n].card[i])
  9153. continue;
  9154. struct item_data *card_data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]);
  9155. if (card_data) {
  9156. int card_pos = card_data->equip;
  9157. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  9158. pos = card_pos; // Use the card's equip position
  9159. break;
  9160. }
  9161. }
  9162. }
  9163. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  9164. pos = (req_pos&EQP_ARMS);
  9165. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  9166. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  9167. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  9168. pos = req_pos&EQP_SHADOW_ACC;
  9169. if (pos == EQP_SHADOW_ACC)
  9170. pos = equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  9171. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  9172. pos = (req_pos&EQP_SHADOW_ARMS);
  9173. if( pos == EQP_SHADOW_ARMS )
  9174. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  9175. }
  9176. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  9177. //Update skill-block range database when weapon range changes. [Skotlex]
  9178. i = equip_index[EQI_HAND_R];
  9179. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  9180. flag = 1;
  9181. else
  9182. flag = id->range != sd->inventory_data[i]->range;
  9183. }
  9184. if( equipswitch ){
  9185. for( i = 0; i < EQI_MAX; i++ ){
  9186. if( pos&equip_bitmask[i] ){
  9187. // If there was already an item assigned to this slot
  9188. if( sd->equip_switch_index[i] >= 0 ){
  9189. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  9190. }
  9191. // Assign the new index to it
  9192. sd->equip_switch_index[i] = n;
  9193. }
  9194. }
  9195. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  9196. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  9197. return true;
  9198. }else{
  9199. for(i=0;i<EQI_MAX;i++) {
  9200. if(pos & equip_bitmask[i]) {
  9201. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  9202. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  9203. sd->equip_index[i] = n;
  9204. }
  9205. }
  9206. pc_equipswitch_remove(sd, n);
  9207. if(pos==EQP_AMMO) {
  9208. clif_arrowequip(sd,n);
  9209. clif_arrow_fail(sd,3);
  9210. }
  9211. else
  9212. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  9213. sd->inventory.u.items_inventory[n].equip = pos;
  9214. }
  9215. if(pos & EQP_HAND_R) {
  9216. if(id)
  9217. sd->weapontype1 = id->look;
  9218. else
  9219. sd->weapontype1 = 0;
  9220. pc_calcweapontype(sd);
  9221. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9222. }
  9223. if(pos & EQP_HAND_L) {
  9224. if(id) {
  9225. if(id->type == IT_WEAPON) {
  9226. sd->status.shield = 0;
  9227. sd->weapontype2 = id->look;
  9228. }
  9229. else
  9230. if(id->type == IT_ARMOR) {
  9231. sd->status.shield = id->look;
  9232. sd->weapontype2 = 0;
  9233. }
  9234. }
  9235. else
  9236. sd->status.shield = sd->weapontype2 = 0;
  9237. pc_calcweapontype(sd);
  9238. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9239. }
  9240. if(pos & EQP_SHOES)
  9241. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9242. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  9243. short idx = sd->equip_index[EQI_AMMO];
  9244. if (idx >= 0) {
  9245. switch (sd->inventory_data[idx]->look) {
  9246. case AMMO_ARROW:
  9247. if (id->look != W_BOW && id->look != W_MUSICAL && id->look != W_WHIP)
  9248. pc_unequipitem(sd, idx, 2 | 4);
  9249. break;
  9250. case AMMO_BULLET:
  9251. case AMMO_SHELL:
  9252. if (id->look != W_REVOLVER && id->look != W_RIFLE && id->look != W_GATLING && id->look != W_SHOTGUN
  9253. #ifdef RENEWAL
  9254. && id->look != W_GRENADE
  9255. #endif
  9256. )
  9257. pc_unequipitem(sd, idx, 2 | 4);
  9258. break;
  9259. #ifndef RENEWAL
  9260. case AMMO_GRENADE:
  9261. if (id->look != W_GRENADE)
  9262. pc_unequipitem(sd, idx, 2 | 4);
  9263. break;
  9264. #endif
  9265. }
  9266. }
  9267. }
  9268. pc_set_costume_view(sd);
  9269. pc_checkallowskill(sd); //Check if status changes should be halted.
  9270. iflag = sd->npc_item_flag;
  9271. // Check for combos (MUST be before status_calc_pc)
  9272. if (!id->combos.empty())
  9273. pc_checkcombo(sd, id);
  9274. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9275. ; // No cards
  9276. else {
  9277. for (i = 0; i < MAX_SLOTS; i++) {
  9278. item_data *data;
  9279. if (!sd->inventory.u.items_inventory[n].card[i])
  9280. continue;
  9281. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != nullptr) {
  9282. if (!data->combos.empty())
  9283. pc_checkcombo(sd, data);
  9284. }
  9285. }
  9286. }
  9287. status_calc_pc(sd,SCO_NONE);
  9288. if (flag) //Update skill data
  9289. clif_skillinfoblock(sd);
  9290. //OnEquip script [Skotlex]
  9291. if (id) {
  9292. //only run the script if item isn't restricted
  9293. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  9294. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9295. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9296. ; //No cards
  9297. else {
  9298. for( i = 0; i < MAX_SLOTS; i++ ) {
  9299. struct item_data *data;
  9300. if (!sd->inventory.u.items_inventory[n].card[i])
  9301. continue;
  9302. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9303. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  9304. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9305. }
  9306. }
  9307. }
  9308. }
  9309. sd->npc_item_flag = iflag;
  9310. return true;
  9311. }
  9312. /**
  9313. * Recalculate player status on unequip
  9314. * @param sd: Player data
  9315. * @param n: Item inventory index
  9316. * @param flag: Whether to recalculate a player's status or not
  9317. * @return True on success or false on failure
  9318. */
  9319. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  9320. int i, iflag;
  9321. bool status_calc = false;
  9322. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  9323. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  9324. sd->inventory.u.items_inventory[n].equip = 0;
  9325. if (!(flag & 4))
  9326. pc_checkallowskill(sd);
  9327. iflag = sd->npc_item_flag;
  9328. // Check for combos (MUST be before status_calc_pc)
  9329. if (sd->inventory_data[n]) {
  9330. if (!sd->inventory_data[n]->combos.empty()) {
  9331. if (pc_removecombo(sd, sd->inventory_data[n]))
  9332. status_calc = true;
  9333. }
  9334. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9335. ; // No cards
  9336. else {
  9337. for (i = 0; i < MAX_SLOTS; i++) {
  9338. item_data *data;
  9339. if (!sd->inventory.u.items_inventory[n].card[i])
  9340. continue;
  9341. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != nullptr) {
  9342. if (!data->combos.empty()) {
  9343. if (pc_removecombo(sd, data))
  9344. status_calc = true;
  9345. }
  9346. }
  9347. }
  9348. }
  9349. }
  9350. if (flag & 1 || status_calc) {
  9351. pc_checkallowskill(sd);
  9352. status_calc_pc(sd, SCO_NONE);
  9353. }
  9354. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  9355. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  9356. //OnUnEquip script [Skotlex]
  9357. if (sd->inventory_data[n]) {
  9358. if (sd->inventory_data[n]->unequip_script)
  9359. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9360. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9361. ; //No cards
  9362. else {
  9363. for (i = 0; i < MAX_SLOTS; i++) {
  9364. struct item_data *data;
  9365. if (!sd->inventory.u.items_inventory[n].card[i])
  9366. continue;
  9367. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9368. if (data->unequip_script)
  9369. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9370. }
  9371. }
  9372. }
  9373. }
  9374. sd->npc_item_flag = iflag;
  9375. }
  9376. /**
  9377. * Called when attempting to unequip an item from a player
  9378. * @param sd: Player data
  9379. * @param n: Item inventory index
  9380. * @param flag: Type of unequip
  9381. * 0 - only unequip
  9382. * 1 - calculate status after unequipping
  9383. * 2 - force unequip
  9384. * 4 - unequip by switching equipment
  9385. * @return True on success or false on failure
  9386. */
  9387. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  9388. int i, pos;
  9389. nullpo_retr(false,sd);
  9390. if (n < 0 || n >= MAX_INVENTORY) {
  9391. clif_unequipitemack(sd,0,0,0);
  9392. return false;
  9393. }
  9394. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  9395. clif_unequipitemack(sd,n,0,0);
  9396. return false; //Nothing to unequip
  9397. }
  9398. // status change that makes player cannot unequip equipment
  9399. if (!(flag&2) && sd->sc.count &&
  9400. (sd->sc.data[SC_BERSERK] ||
  9401. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9402. sd->sc.data[SC__BLOODYLUST] ||
  9403. sd->sc.data[SC_KYOUGAKU] ||
  9404. (sd->sc.data[SC_PYROCLASTIC] &&
  9405. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  9406. {
  9407. clif_unequipitemack(sd,n,0,0);
  9408. return false;
  9409. }
  9410. if (battle_config.battle_log)
  9411. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  9412. for(i = 0; i < EQI_MAX; i++) {
  9413. if (pos & equip_bitmask[i])
  9414. sd->equip_index[i] = -1;
  9415. }
  9416. if(pos & EQP_HAND_R) {
  9417. sd->weapontype1 = 0;
  9418. sd->status.weapon = sd->weapontype2;
  9419. pc_calcweapontype(sd);
  9420. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9421. if( !battle_config.dancing_weaponswitch_fix )
  9422. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  9423. #ifdef RENEWAL
  9424. if (battle_config.switch_remove_edp&2) {
  9425. #else
  9426. if (battle_config.switch_remove_edp&1) {
  9427. #endif
  9428. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  9429. }
  9430. }
  9431. if(pos & EQP_HAND_L) {
  9432. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  9433. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  9434. sd->status.shield = sd->weapontype2 = 0;
  9435. pc_calcweapontype(sd);
  9436. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9437. }
  9438. if(pos & EQP_SHOES)
  9439. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9440. clif_unequipitemack(sd,n,pos,1);
  9441. pc_set_costume_view(sd);
  9442. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  9443. // On weapon change (right and left hand)
  9444. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  9445. if (battle_config.ammo_unequip && !(flag & 4)) {
  9446. switch (sd->inventory_data[n]->look) {
  9447. case W_BOW:
  9448. case W_MUSICAL:
  9449. case W_WHIP:
  9450. case W_REVOLVER:
  9451. case W_RIFLE:
  9452. case W_GATLING:
  9453. case W_SHOTGUN:
  9454. case W_GRENADE: {
  9455. short idx = sd->equip_index[EQI_AMMO];
  9456. if (idx >= 0) {
  9457. sd->equip_index[EQI_AMMO] = -1;
  9458. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  9459. pc_unequipitem_sub(sd, idx, 0);
  9460. }
  9461. }
  9462. break;
  9463. }
  9464. }
  9465. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  9466. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  9467. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  9468. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  9469. }
  9470. // On armor change
  9471. if (pos & EQP_ARMOR) {
  9472. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  9473. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  9474. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  9475. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  9476. }
  9477. // On equipment change
  9478. #ifndef RENEWAL
  9479. if (!(flag&4))
  9480. status_change_end(&sd->bl, SC_CONCENTRATION, INVALID_TIMER);
  9481. #endif
  9482. // On ammo change
  9483. 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_))
  9484. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  9485. pc_unequipitem_sub(sd, n, flag);
  9486. return true;
  9487. }
  9488. int pc_equipswitch( struct map_session_data* sd, int index ){
  9489. // Get the target equip mask
  9490. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  9491. // Get the currently equipped item
  9492. short equippedItem = pc_checkequip( sd, position, true );
  9493. // No item equipped at the target
  9494. if( equippedItem == -1 ){
  9495. // Remove it from the equip switch
  9496. pc_equipswitch_remove( sd, index );
  9497. pc_equipitem( sd, index, position );
  9498. return position;
  9499. }else{
  9500. std::map<int, int> unequipped;
  9501. int unequipped_position = 0;
  9502. // Unequip all items that interfere
  9503. for( int i = 0; i < EQI_MAX; i++ ){
  9504. int unequip_index = sd->equip_index[i];
  9505. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  9506. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  9507. // Store the unequipped index and position mask for later
  9508. unequipped[unequip_index] = unequip_item->equip;
  9509. // Keep the position for later
  9510. unequipped_position |= unequip_item->equip;
  9511. // Unequip the item
  9512. pc_unequipitem( sd, unequip_index, 0 );
  9513. }
  9514. }
  9515. int all_position = position | unequipped_position;
  9516. // Equip everything that is hit by the mask
  9517. for( int i = 0; i < EQI_MAX; i++ ){
  9518. int exchange_index = sd->equip_switch_index[i];
  9519. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  9520. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  9521. // Store the target position
  9522. int exchange_position = exchange_item->equipSwitch;
  9523. // Remove the item from equip switch
  9524. pc_equipswitch_remove( sd, exchange_index );
  9525. // Equip the item at the destinated position
  9526. pc_equipitem( sd, exchange_index, exchange_position );
  9527. }
  9528. }
  9529. // Place all unequipped items into the equip switch window
  9530. for( std::pair<int, int> pair : unequipped ){
  9531. int unequipped_index = pair.first;
  9532. int unequipped_position = pair.second;
  9533. // Rebuild the index cache
  9534. for( int i = 0; i < EQI_MAX; i++ ){
  9535. if( unequipped_position & equip_bitmask[i] ){
  9536. sd->equip_switch_index[i] = unequipped_index;
  9537. }
  9538. }
  9539. // Set the correct position mask
  9540. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  9541. // Notify the client
  9542. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  9543. }
  9544. return all_position;
  9545. }
  9546. }
  9547. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  9548. struct item* item = &sd->inventory.u.items_inventory[index];
  9549. if( !item->equipSwitch ){
  9550. return;
  9551. }
  9552. for( int i = 0; i < EQI_MAX; i++ ){
  9553. // If a match is found
  9554. if( sd->equip_switch_index[i] == index ){
  9555. // Remove it from the slot
  9556. sd->equip_switch_index[i] = -1;
  9557. }
  9558. }
  9559. // Send out one packet for all slots using the current item's mask
  9560. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  9561. item->equipSwitch = 0;
  9562. }
  9563. /*==========================================
  9564. * Checking if player (sd) has an invalid item
  9565. * and is unequiped on map load (item_noequip)
  9566. *------------------------------------------*/
  9567. void pc_checkitem(struct map_session_data *sd) {
  9568. int i, calc_flag = 0;
  9569. struct item* it;
  9570. nullpo_retv(sd);
  9571. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9572. return;
  9573. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9574. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9575. it = &sd->inventory.u.items_inventory[i];
  9576. if( it->nameid == 0 )
  9577. continue;
  9578. if( !it->equip )
  9579. continue;
  9580. if( it->equip&~pc_equippoint(sd,i) ) {
  9581. pc_unequipitem(sd, i, 2);
  9582. calc_flag = 1;
  9583. continue;
  9584. }
  9585. 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) ) {
  9586. pc_unequipitem(sd, i, 2);
  9587. calc_flag = 1;
  9588. continue;
  9589. }
  9590. }
  9591. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9592. it = &sd->inventory.u.items_inventory[i];
  9593. if( it->nameid == 0 )
  9594. continue;
  9595. if( !it->equipSwitch )
  9596. continue;
  9597. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  9598. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  9599. for( int j = 0; j < EQI_MAX; j++ ){
  9600. if( sd->equip_switch_index[j] == i ){
  9601. sd->equip_switch_index[j] = -1;
  9602. }
  9603. }
  9604. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  9605. continue;
  9606. }
  9607. }
  9608. if( calc_flag && sd->state.active ) {
  9609. pc_checkallowskill(sd);
  9610. status_calc_pc(sd,SCO_NONE);
  9611. }
  9612. }
  9613. /*==========================================
  9614. * Checks for unavailable items and removes them.
  9615. * @param sd: Player data
  9616. * @param type Forced check:
  9617. * 1 - Inventory
  9618. * 2 - Cart
  9619. * 4 - Storage
  9620. *------------------------------------------*/
  9621. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9622. {
  9623. int i;
  9624. t_itemid nameid;
  9625. char output[256];
  9626. nullpo_retv(sd);
  9627. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9628. for(i = 0; i < MAX_INVENTORY; i++) {
  9629. nameid = sd->inventory.u.items_inventory[i].nameid;
  9630. if (!nameid)
  9631. continue;
  9632. if (!itemdb_available(nameid)) {
  9633. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  9634. clif_displaymessage(sd->fd, output);
  9635. 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);
  9636. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9637. continue;
  9638. }
  9639. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9640. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9641. }
  9642. }
  9643. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9644. for(i = 0; i < MAX_CART; i++) {
  9645. nameid = sd->cart.u.items_cart[i].nameid;
  9646. if (!nameid)
  9647. continue;
  9648. if (!itemdb_available(nameid)) {
  9649. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  9650. clif_displaymessage(sd->fd, output);
  9651. 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);
  9652. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9653. continue;
  9654. }
  9655. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9656. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9657. }
  9658. }
  9659. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9660. for(i = 0; i < sd->storage.max_amount; i++) {
  9661. nameid = sd->storage.u.items_storage[i].nameid;
  9662. if (!nameid)
  9663. continue;
  9664. if (!itemdb_available(nameid)) {
  9665. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  9666. clif_displaymessage(sd->fd, output);
  9667. 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);
  9668. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9669. continue;
  9670. }
  9671. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9672. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9673. }
  9674. }
  9675. }
  9676. /*==========================================
  9677. * Update PVP rank for sd1 in cmp to sd2
  9678. *------------------------------------------*/
  9679. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9680. {
  9681. struct map_session_data *sd1,*sd2;
  9682. sd1=(struct map_session_data *)bl;
  9683. sd2=va_arg(ap,struct map_session_data *);
  9684. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9685. {// cannot register pvp rank for hidden GMs
  9686. return 0;
  9687. }
  9688. if( sd1->pvp_point > sd2->pvp_point )
  9689. sd2->pvp_rank++;
  9690. return 0;
  9691. }
  9692. /*==========================================
  9693. * Calculate new rank beetween all present players (map_foreachinallarea)
  9694. * and display result
  9695. *------------------------------------------*/
  9696. int pc_calc_pvprank(struct map_session_data *sd)
  9697. {
  9698. int old = sd->pvp_rank;
  9699. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9700. sd->pvp_rank=1;
  9701. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9702. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9703. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9704. return sd->pvp_rank;
  9705. }
  9706. /*==========================================
  9707. * Calculate next sd ranking calculation from config
  9708. *------------------------------------------*/
  9709. TIMER_FUNC(pc_calc_pvprank_timer){
  9710. struct map_session_data *sd;
  9711. sd=map_id2sd(id);
  9712. if(sd==NULL)
  9713. return 0;
  9714. sd->pvp_timer = INVALID_TIMER;
  9715. if( pc_isinvisible(sd) )
  9716. {// do not calculate the pvp rank for a hidden GM
  9717. return 0;
  9718. }
  9719. if( pc_calc_pvprank(sd) > 0 )
  9720. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9721. return 0;
  9722. }
  9723. /*==========================================
  9724. * Checking if sd is married
  9725. * Return:
  9726. * partner_id = yes
  9727. * 0 = no
  9728. *------------------------------------------*/
  9729. int pc_ismarried(struct map_session_data *sd)
  9730. {
  9731. if(sd == NULL)
  9732. return -1;
  9733. if(sd->status.partner_id > 0)
  9734. return sd->status.partner_id;
  9735. else
  9736. return 0;
  9737. }
  9738. /*==========================================
  9739. * Marry player sd to player dstsd
  9740. * Return:
  9741. * false = fail
  9742. * true = success
  9743. *------------------------------------------*/
  9744. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9745. {
  9746. if(sd == NULL || dstsd == NULL ||
  9747. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9748. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9749. return false;
  9750. sd->status.partner_id = dstsd->status.char_id;
  9751. dstsd->status.partner_id = sd->status.char_id;
  9752. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9753. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9754. return true;
  9755. }
  9756. /*==========================================
  9757. * Divorce sd from its partner
  9758. * Return:
  9759. * false = fail
  9760. * true = success
  9761. *------------------------------------------*/
  9762. bool pc_divorce(struct map_session_data *sd)
  9763. {
  9764. struct map_session_data *p_sd;
  9765. int i;
  9766. if( sd == NULL || !pc_ismarried(sd) )
  9767. return false;
  9768. if( !sd->status.partner_id )
  9769. return false; // Char is not married
  9770. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9771. { // Lets char server do the divorce
  9772. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9773. return false; // No char server connected
  9774. return true;
  9775. }
  9776. // Both players online, lets do the divorce manually
  9777. sd->status.partner_id = 0;
  9778. p_sd->status.partner_id = 0;
  9779. for( i = 0; i < MAX_INVENTORY; i++ )
  9780. {
  9781. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9782. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9783. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9784. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9785. }
  9786. clif_divorced(sd, p_sd->status.name);
  9787. clif_divorced(p_sd, sd->status.name);
  9788. return true;
  9789. }
  9790. /**
  9791. * Get the partner map_session_data of a player
  9792. * @param sd : the husband|wife session
  9793. * @return partner session or NULL
  9794. */
  9795. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9796. if (!sd || !pc_ismarried(sd))
  9797. return NULL;
  9798. return map_charid2sd(sd->status.partner_id);
  9799. }
  9800. /**
  9801. * Get the father map_session_data of a player
  9802. * @param sd : the baby session
  9803. * @return father session or NULL
  9804. */
  9805. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9806. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9807. return NULL;
  9808. return map_charid2sd(sd->status.father);
  9809. }
  9810. /**
  9811. * Get the mother map_session_data of a player
  9812. * @param sd : the baby session
  9813. * @return mother session or NULL
  9814. */
  9815. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9816. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9817. return NULL;
  9818. return map_charid2sd(sd->status.mother);
  9819. }
  9820. /*==========================================
  9821. * Get sd children charid. (Need to be married)
  9822. *------------------------------------------*/
  9823. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9824. {
  9825. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9826. // charid2sd returns NULL if not found
  9827. return NULL;
  9828. return map_charid2sd(sd->status.child);
  9829. }
  9830. /*==========================================
  9831. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9832. *------------------------------------------*/
  9833. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  9834. {
  9835. int hp = 0, sp = 0;
  9836. if( pc_isdead(sd) )
  9837. return;
  9838. if (sd->hp_loss.value) {
  9839. sd->hp_loss.tick += diff_tick;
  9840. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9841. hp += sd->hp_loss.value;
  9842. sd->hp_loss.tick -= sd->hp_loss.rate;
  9843. }
  9844. if(hp >= sd->battle_status.hp)
  9845. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9846. }
  9847. if (sd->sp_loss.value) {
  9848. sd->sp_loss.tick += diff_tick;
  9849. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9850. sp += sd->sp_loss.value;
  9851. sd->sp_loss.tick -= sd->sp_loss.rate;
  9852. }
  9853. }
  9854. if (hp > 0 || sp > 0)
  9855. status_zap(&sd->bl, hp, sp);
  9856. }
  9857. //Character regen. Flag is used to know which types of regen can take place.
  9858. //&1: HP regen
  9859. //&2: SP regen
  9860. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  9861. {
  9862. int hp = 0, sp = 0;
  9863. if (sd->hp_regen.value) {
  9864. sd->hp_regen.tick += diff_tick;
  9865. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9866. hp += sd->hp_regen.value;
  9867. sd->hp_regen.tick -= sd->hp_regen.rate;
  9868. }
  9869. }
  9870. if (sd->sp_regen.value) {
  9871. sd->sp_regen.tick += diff_tick;
  9872. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9873. sp += sd->sp_regen.value;
  9874. sd->sp_regen.tick -= sd->sp_regen.rate;
  9875. }
  9876. }
  9877. if (sd->percent_hp_regen.value) {
  9878. sd->percent_hp_regen.tick += diff_tick;
  9879. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9880. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  9881. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9882. }
  9883. }
  9884. if (sd->percent_sp_regen.value) {
  9885. sd->percent_sp_regen.tick += diff_tick;
  9886. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9887. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  9888. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9889. }
  9890. }
  9891. if (hp > 0 || sp > 0)
  9892. status_heal(&sd->bl, hp, sp, 0);
  9893. }
  9894. /*==========================================
  9895. * Memo player sd savepoint. (map,x,y)
  9896. *------------------------------------------*/
  9897. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9898. {
  9899. nullpo_retv(sd);
  9900. sd->status.save_point.map = mapindex;
  9901. sd->status.save_point.x = x;
  9902. sd->status.save_point.y = y;
  9903. }
  9904. /*==========================================
  9905. * Save 1 player data at autosave interval
  9906. *------------------------------------------*/
  9907. static TIMER_FUNC(pc_autosave){
  9908. int interval;
  9909. struct s_mapiterator* iter;
  9910. struct map_session_data* sd;
  9911. static int last_save_id = 0, save_flag = 0;
  9912. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9913. save_flag = 0;
  9914. else
  9915. save_flag = 1; //Noone was saved, so save first found char.
  9916. iter = mapit_getallusers();
  9917. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9918. {
  9919. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  9920. continue;
  9921. if(sd->bl.id == last_save_id && save_flag != 1) {
  9922. save_flag = 1;
  9923. continue;
  9924. }
  9925. if(save_flag != 1) //Not our turn to save yet.
  9926. continue;
  9927. //Save char.
  9928. last_save_id = sd->bl.id;
  9929. save_flag = 2;
  9930. if (pc_isvip(sd)) // Check if we're still VIP
  9931. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9932. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9933. break;
  9934. }
  9935. mapit_free(iter);
  9936. interval = autosave_interval/(map_usercount()+1);
  9937. if(interval < minsave_interval)
  9938. interval = minsave_interval;
  9939. add_timer(gettick()+interval,pc_autosave,0,0);
  9940. return 0;
  9941. }
  9942. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9943. {
  9944. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9945. { //Night/day state does not match.
  9946. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9947. sd->state.night = night_flag;
  9948. return 1;
  9949. }
  9950. return 0;
  9951. }
  9952. /*================================================
  9953. * timer to do the day [Yor]
  9954. * data: 0 = called by timer, 1 = gmcommand/script
  9955. *------------------------------------------------*/
  9956. TIMER_FUNC(map_day_timer){
  9957. char tmp_soutput[1024];
  9958. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9959. return 0;
  9960. if (!night_flag)
  9961. return 0; //Already day.
  9962. night_flag = 0; // 0=day, 1=night [Yor]
  9963. map_foreachpc(pc_daynight_timer_sub);
  9964. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9965. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9966. return 0;
  9967. }
  9968. /*================================================
  9969. * timer to do the night [Yor]
  9970. * data: 0 = called by timer, 1 = gmcommand/script
  9971. *------------------------------------------------*/
  9972. TIMER_FUNC(map_night_timer){
  9973. char tmp_soutput[1024];
  9974. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9975. return 0;
  9976. if (night_flag)
  9977. return 0; //Already nigth.
  9978. night_flag = 1; // 0=day, 1=night [Yor]
  9979. map_foreachpc(pc_daynight_timer_sub);
  9980. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9981. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9982. return 0;
  9983. }
  9984. /**
  9985. * Attempt to stand up a player
  9986. * @param sd
  9987. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9988. * @return True if success, Fals if failed
  9989. */
  9990. bool pc_setstand(struct map_session_data *sd, bool force){
  9991. nullpo_ret(sd);
  9992. // Cannot stand yet
  9993. // TODO: Move to SCS_NOSTAND [Cydh]
  9994. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9995. return false;
  9996. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9997. clif_status_load(&sd->bl,EFST_SIT,0);
  9998. clif_standing(&sd->bl); //Inform area PC is standing
  9999. //Reset sitting tick.
  10000. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  10001. if( pc_isdead( sd ) ){
  10002. sd->state.dead_sit = sd->vd.dead_sit = 0;
  10003. clif_party_dead( sd );
  10004. }else{
  10005. sd->state.dead_sit = sd->vd.dead_sit = 0;
  10006. }
  10007. return true;
  10008. }
  10009. /**
  10010. * Calculate Overheat value
  10011. * @param sd: Player data
  10012. * @param heat: Amount of Heat to adjust
  10013. **/
  10014. void pc_overheat(struct map_session_data *sd, int16 heat) {
  10015. nullpo_retv(sd);
  10016. status_change_entry *sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT];
  10017. if (sce) {
  10018. static std::vector<int16> limit = { 150, 200, 280, 360, 450 };
  10019. uint16 skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, (uint16)(limit.size()-1));
  10020. sce->val1 += heat;
  10021. sce->val1 = cap_value(sce->val1, 0, 1000);
  10022. if (sd->sc.data[SC_OVERHEAT])
  10023. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  10024. if (sce->val1 > limit[skill_lv])
  10025. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  10026. } else if (heat > 0)
  10027. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  10028. }
  10029. /**
  10030. * Check if player is autolooting given itemID.
  10031. */
  10032. bool pc_isautolooting(struct map_session_data *sd, t_itemid nameid)
  10033. {
  10034. uint8 i = 0;
  10035. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  10036. return true;
  10037. if (!sd->state.autolooting)
  10038. return false;
  10039. if (sd->state.autolooting)
  10040. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  10041. return (i != AUTOLOOTITEM_SIZE);
  10042. }
  10043. /**
  10044. * Checks if player can use @/#command
  10045. * @param sd Player map session data
  10046. * @param command Command name without @/# and params
  10047. * @param type is it atcommand or charcommand
  10048. */
  10049. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  10050. {
  10051. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  10052. }
  10053. /**
  10054. * Checks if commands used by a player should be logged
  10055. * according to their group setting.
  10056. * @param sd Player map session data
  10057. */
  10058. bool pc_should_log_commands(struct map_session_data *sd)
  10059. {
  10060. return pc_group_should_log_commands(pc_get_group_id(sd));
  10061. }
  10062. /**
  10063. * Spirit Charm expiration timer.
  10064. * @see TimerFunc
  10065. */
  10066. static TIMER_FUNC(pc_spiritcharm_timer){
  10067. struct map_session_data *sd;
  10068. int i;
  10069. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  10070. return 1;
  10071. if (sd->spiritcharm <= 0) {
  10072. 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);
  10073. sd->spiritcharm = 0;
  10074. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10075. return 0;
  10076. }
  10077. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  10078. if (i == sd->spiritcharm) {
  10079. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  10080. return 0;
  10081. }
  10082. sd->spiritcharm--;
  10083. if (i != sd->spiritcharm)
  10084. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  10085. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10086. if (sd->spiritcharm <= 0)
  10087. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10088. clif_spiritcharm(sd);
  10089. return 0;
  10090. }
  10091. /**
  10092. * Adds a spirit charm.
  10093. * @param sd: Target character
  10094. * @param interval: Duration
  10095. * @param max: Maximum amount of charms to add
  10096. * @param type: Charm type (@see spirit_charm_types)
  10097. */
  10098. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  10099. {
  10100. int tid, i;
  10101. nullpo_retv(sd);
  10102. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  10103. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  10104. if (max > MAX_SPIRITCHARM)
  10105. max = MAX_SPIRITCHARM;
  10106. if (sd->spiritcharm < 0)
  10107. sd->spiritcharm = 0;
  10108. if (sd->spiritcharm && sd->spiritcharm >= max) {
  10109. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  10110. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  10111. sd->spiritcharm--;
  10112. if (sd->spiritcharm != 0)
  10113. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  10114. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10115. }
  10116. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  10117. 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);
  10118. if (i != sd->spiritcharm)
  10119. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  10120. sd->spiritcharm_timer[i] = tid;
  10121. sd->spiritcharm++;
  10122. sd->spiritcharm_type = type;
  10123. clif_spiritcharm(sd);
  10124. }
  10125. /**
  10126. * Removes one or more spirit charms.
  10127. * @param sd: The target character
  10128. * @param count: Amount of charms to remove
  10129. * @param type: Type of charm to remove
  10130. */
  10131. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  10132. {
  10133. int i;
  10134. nullpo_retv(sd);
  10135. if (sd->spiritcharm_type != type)
  10136. return;
  10137. if (sd->spiritcharm <= 0) {
  10138. sd->spiritcharm = 0;
  10139. return;
  10140. }
  10141. if (count <= 0)
  10142. return;
  10143. if (count > sd->spiritcharm)
  10144. count = sd->spiritcharm;
  10145. sd->spiritcharm -= count;
  10146. if (count > MAX_SPIRITCHARM)
  10147. count = MAX_SPIRITCHARM;
  10148. for (i = 0; i < count; i++) {
  10149. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  10150. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  10151. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10152. }
  10153. }
  10154. for (i = count; i < MAX_SPIRITCHARM; i++) {
  10155. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  10156. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10157. }
  10158. if (sd->spiritcharm <= 0)
  10159. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10160. clif_spiritcharm(sd);
  10161. }
  10162. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10163. /**
  10164. * Renewal EXP/Item Drop rate modifier based on level penalty
  10165. * @param level_diff: Monster and Player level difference
  10166. * @param mob_class: Monster class
  10167. * @param mode: Monster mode
  10168. * @param type: 1 - EXP, 2 - Item Drop
  10169. * @return Penalty rate
  10170. */
  10171. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  10172. {
  10173. int rate = 100;
  10174. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  10175. return rate;
  10176. if (level_diff < 0)
  10177. level_diff = MAX_LEVEL + (~level_diff + 1);
  10178. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  10179. return rate;
  10180. return 100; // Penalty not found, return default
  10181. }
  10182. #endif
  10183. int pc_split_str(char *str,char **val,int num)
  10184. {
  10185. int i;
  10186. for (i=0; i<num && str; i++){
  10187. val[i] = str;
  10188. str = strchr(str,',');
  10189. if (str && i<num-1) //Do not remove a trailing comma.
  10190. *str++=0;
  10191. }
  10192. return i;
  10193. }
  10194. int pc_split_atoi(char* str, int* val, char sep, int max)
  10195. {
  10196. int i,j;
  10197. for (i=0; i<max; i++) {
  10198. if (!str) break;
  10199. val[i] = atoi(str);
  10200. str = strchr(str,sep);
  10201. if (str)
  10202. *str++=0;
  10203. }
  10204. //Zero up the remaining.
  10205. for(j=i; j < max; j++)
  10206. val[j] = 0;
  10207. return i;
  10208. }
  10209. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  10210. {
  10211. static int warning=0;
  10212. int i,j;
  10213. for (i=0; i<max; i++) {
  10214. double f;
  10215. if (!str) break;
  10216. f = atof(str);
  10217. if (f < 0)
  10218. val[i] = 0;
  10219. else if (f > UINT_MAX) {
  10220. val[i] = UINT_MAX;
  10221. if (!warning) {
  10222. warning = 1;
  10223. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  10224. }
  10225. } else
  10226. val[i] = (unsigned int)f;
  10227. str = strchr(str,sep);
  10228. if (str)
  10229. *str++=0;
  10230. }
  10231. //Zero up the remaining.
  10232. for(j=i; j < max; j++)
  10233. val[j] = 0;
  10234. return i;
  10235. }
  10236. /*==========================================
  10237. * sub DB reading.
  10238. * Function used to read skill_tree.txt
  10239. *------------------------------------------*/
  10240. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  10241. {
  10242. uint32 baselv, joblv, baselv_max, joblv_max;
  10243. uint16 skill_id, skill_lv, skill_lv_max;
  10244. int idx, class_;
  10245. unsigned int i, offset, skill_idx;
  10246. class_ = atoi(fields[0]);
  10247. skill_id = (uint16)atoi(fields[1]);
  10248. skill_lv = (uint16)atoi(fields[2]);
  10249. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  10250. baselv = (uint32)atoi(fields[3]);
  10251. joblv = (uint32)atoi(fields[4]);
  10252. offset = 5;
  10253. }
  10254. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  10255. baselv = joblv = 0;
  10256. offset = 3;
  10257. }
  10258. else {
  10259. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  10260. return false;
  10261. }
  10262. if(!pcdb_checkid(class_))
  10263. {
  10264. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  10265. return false;
  10266. }
  10267. idx = pc_class2idx(class_);
  10268. if (!skill_get_index(skill_id)) {
  10269. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.\n", skill_id, class_);
  10270. return false;
  10271. }
  10272. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10273. ShowWarning("pc_readdb_skilltree: Skill %hu's level %hu exceeds job %d's max level %hu. Capping skill level.\n", skill_id, skill_lv, class_, skill_lv_max);
  10274. skill_lv = skill_lv_max;
  10275. }
  10276. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  10277. ShowWarning("pc_readdb_skilltree: Skill %hu's base level requirement %d exceeds job %d's max base level %d. Capping skill base level.\n", skill_id, baselv, class_, baselv_max);
  10278. baselv = baselv_max;
  10279. }
  10280. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  10281. ShowWarning("pc_readdb_skilltree: Skill %hu's job level requirement %d exceeds job %d's max job level %d. Capping skill job level.\n", skill_id, joblv, class_, joblv_max);
  10282. joblv = joblv_max;
  10283. }
  10284. //This is to avoid adding two lines for the same skill. [Skotlex]
  10285. ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].skill_id == 0 || skill_tree[idx][skill_idx].skill_id == skill_id );
  10286. if( skill_idx == MAX_SKILL_TREE )
  10287. {
  10288. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree. Maximum number of skills per job has been reached.\n", skill_id, class_);
  10289. return false;
  10290. }
  10291. else if(skill_tree[idx][skill_idx].skill_id)
  10292. {
  10293. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  10294. }
  10295. skill_tree[idx][skill_idx].skill_id = skill_id;
  10296. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  10297. skill_tree[idx][skill_idx].baselv = baselv;
  10298. skill_tree[idx][skill_idx].joblv = joblv;
  10299. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  10300. {
  10301. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  10302. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  10303. if (skill_id == 0) {
  10304. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  10305. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  10306. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  10307. }
  10308. continue;
  10309. }
  10310. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  10311. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  10312. return false;
  10313. }
  10314. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10315. ShowWarning("pc_readdb_skilltree: Skill %hu's level %hu exceeds job %d's max level %hu. Capping skill level.\n", skill_id, skill_lv, class_, skill_lv_max);
  10316. skill_lv = skill_lv_max;
  10317. }
  10318. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  10319. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  10320. }
  10321. return true;
  10322. }
  10323. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10324. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  10325. {
  10326. int type, class_, diff;
  10327. type = atoi(fields[0]); //1=experience, 2=item drop
  10328. class_ = atoi(fields[1]);
  10329. diff = atoi(fields[2]);
  10330. if( type != 1 && type != 2 ){
  10331. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  10332. return false;
  10333. }
  10334. if( !CHK_CLASS(class_) ){
  10335. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  10336. return false;
  10337. }
  10338. diff = min(diff, MAX_LEVEL);
  10339. if( diff < 0 )
  10340. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  10341. level_penalty[type][class_][diff] = atoi(fields[3]);
  10342. return true;
  10343. }
  10344. #endif
  10345. /** [Cydh]
  10346. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  10347. * @param level Base level of player
  10348. * @param class_ Job ID @see enum e_job
  10349. * @return base_hp
  10350. */
  10351. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  10352. double base_hp;
  10353. uint16 i, idx = pc_class2idx(class_);
  10354. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  10355. #ifndef RENEWAL
  10356. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  10357. #endif
  10358. for (i = 2; i <= level; i++)
  10359. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  10360. if (class_ == JOB_SUMMONER)
  10361. base_hp += floor((base_hp / 2) + 0.5);
  10362. return (unsigned int)base_hp;
  10363. }
  10364. /** [Playtester]
  10365. * Calculates base sp of player.
  10366. * @param level Base level of player
  10367. * @param class_ Job ID @see enum e_job
  10368. * @return base_sp
  10369. */
  10370. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  10371. double base_sp;
  10372. uint16 idx = pc_class2idx(class_);
  10373. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  10374. switch (class_) {
  10375. case JOB_NINJA:
  10376. if (level >= 10)
  10377. base_sp -= 22;
  10378. else
  10379. base_sp = 11 + 3*level;
  10380. break;
  10381. case JOB_GUNSLINGER:
  10382. if (level > 10)
  10383. base_sp -= 18;
  10384. else
  10385. base_sp = 9 + 3*level;
  10386. break;
  10387. case JOB_SUMMONER:
  10388. base_sp -= floor(base_sp / 2);
  10389. break;
  10390. }
  10391. return (unsigned int)base_sp;
  10392. }
  10393. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  10394. static bool pc_readdb_job1(char* fields[], int columns, int current){
  10395. int idx, class_;
  10396. unsigned int i;
  10397. class_ = atoi(fields[0]);
  10398. if (!pcdb_checkid(class_)) {
  10399. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  10400. return false;
  10401. }
  10402. idx = pc_class2idx(class_);
  10403. job_info[idx].max_weight_base = atoi(fields[1]);
  10404. job_info[idx].hp_factor = atoi(fields[2]);
  10405. job_info[idx].hp_multiplicator = atoi(fields[3]);
  10406. job_info[idx].sp_factor = atoi(fields[4]);
  10407. #ifdef RENEWAL_ASPD
  10408. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  10409. #else
  10410. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  10411. #endif
  10412. {
  10413. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  10414. }
  10415. return true;
  10416. }
  10417. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  10418. static bool pc_readdb_job2(char* fields[], int columns, int current)
  10419. {
  10420. int idx, class_, i;
  10421. class_ = atoi(fields[0]);
  10422. if(!pcdb_checkid(class_))
  10423. {
  10424. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  10425. return false;
  10426. }
  10427. idx = pc_class2idx(class_);
  10428. for(i = 1; i < columns; i++)
  10429. {
  10430. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  10431. }
  10432. return true;
  10433. }
  10434. //Reading job_exp.txt line
  10435. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  10436. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  10437. {
  10438. int idx, i, type;
  10439. int job_id,job_count,jobs[CLASS_COUNT];
  10440. unsigned int ui, maxlvl;
  10441. maxlvl = atoi(fields[0]);
  10442. if(maxlvl > MAX_LEVEL || maxlvl<1){
  10443. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  10444. return false;
  10445. }
  10446. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10447. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  10448. return false;
  10449. }
  10450. type = atoi(fields[2]);
  10451. if(type < 0 || type > 1){
  10452. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  10453. return false;
  10454. }
  10455. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  10456. if (job_count < 1)
  10457. return false;
  10458. job_id = jobs[0];
  10459. if(!pcdb_checkid(job_id)){
  10460. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  10461. return false;
  10462. }
  10463. idx = pc_class2idx(job_id);
  10464. job_info[idx].max_level[type] = maxlvl;
  10465. for(i=0; i<maxlvl; i++){
  10466. t_exp exp = strtoull( fields[3 + i], nullptr, 10 );
  10467. if( exp == 0 ){
  10468. ShowWarning( "pc_readdb_job_exp: No value defined for level %d in line %d. Defaulting to MAX_EXP...\n", i + 1, current );
  10469. exp = MAX_EXP;
  10470. }else if( exp > MAX_EXP ){
  10471. ShowWarning( "pc_readdb_job_exp: Value %" PRIu64 " is too high, capping to %" PRIu64 "...\n", exp, MAX_EXP );
  10472. exp = MAX_EXP;
  10473. }
  10474. job_info[idx].exp_table[type][i] = exp;
  10475. }
  10476. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  10477. //The reasoning behind the -2 is this... if the max level is 5, then the array
  10478. //should look like this:
  10479. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  10480. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  10481. job_info[idx].max_level[type]--;
  10482. if (job_info[idx].max_level[type] < maxlvl) {
  10483. ShowWarning("pc_readdb_job_exp: Specified max %u for job %d, but that job's exp table only goes up to level %u.\n", maxlvl, job_id, job_info[idx].max_level[type]);
  10484. ShowInfo("Filling the missing values with the last exp entry.\n");
  10485. //Fill the requested values with the last entry.
  10486. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  10487. for (; ui+2 < maxlvl; ui++)
  10488. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  10489. job_info[idx].max_level[type] = maxlvl;
  10490. }
  10491. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10492. for (i = 1; i < job_count; i++) {
  10493. job_id = jobs[i];
  10494. if (!pcdb_checkid(job_id)) {
  10495. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  10496. continue;
  10497. }
  10498. idx = pc_class2idx(job_id);
  10499. memcpy(job_info[idx].exp_table[type], job_info[pc_class2idx(jobs[0])].exp_table[type], sizeof(job_info[pc_class2idx(jobs[0])].exp_table[type]));
  10500. job_info[idx].max_level[type] = maxlvl;
  10501. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10502. }
  10503. return true;
  10504. }
  10505. /**
  10506. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  10507. * startlvl,endlvl,class,type,values...
  10508. */
  10509. #ifdef HP_SP_TABLES
  10510. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  10511. {
  10512. int i, startlvl, endlvl;
  10513. int job_count,jobs[CLASS_COUNT];
  10514. short type;
  10515. startlvl = atoi(fields[0]);
  10516. if(startlvl<1){
  10517. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10518. return false;
  10519. }
  10520. endlvl = atoi(fields[1]);
  10521. if(endlvl<1 || endlvl<startlvl){
  10522. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  10523. return false;
  10524. }
  10525. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10526. ShowError("pc_readdb_job_basehpsp: Number of columns %d (needs %d) defined is too low for start level %d, max level %d.\n",columns,(endlvl-startlvl+1+4),startlvl,endlvl);
  10527. return false;
  10528. }
  10529. type = atoi(fields[3]);
  10530. if(type < 0 || type > 1){
  10531. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  10532. return false;
  10533. }
  10534. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  10535. if (job_count < 1)
  10536. return false;
  10537. for (i = 0; i < job_count; i++) {
  10538. int idx, job_id = jobs[i], use_endlvl;
  10539. if (!pcdb_checkid(job_id)) {
  10540. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  10541. return false;
  10542. }
  10543. idx = pc_class2idx(job_id);
  10544. if (startlvl > job_info[idx].max_level[0]) {
  10545. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10546. return false;
  10547. }
  10548. //Just read until available max level for this job, don't use MAX_LEVEL!
  10549. use_endlvl = endlvl;
  10550. if (use_endlvl > job_info[idx].max_level[0])
  10551. use_endlvl = job_info[idx].max_level[0];
  10552. if(type == 0) { //hp type
  10553. uint16 j;
  10554. for(j = 0; j < use_endlvl; j++) {
  10555. if (atoi(fields[j+4])) {
  10556. uint16 lvl_idx = startlvl-1+j;
  10557. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  10558. //Tells if this HP is lower than previous level (but not for 99->100)
  10559. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  10560. ShowInfo("pc_readdb_job_basehpsp: HP value at entry %d col %d is lower than previous level (job=%d,lvl=%d,oldval=%d,val=%d).\n",
  10561. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  10562. }
  10563. }
  10564. }
  10565. else { //sp type
  10566. uint16 j;
  10567. for(j = 0; j < use_endlvl; j++) {
  10568. if (atoi(fields[j+4])) {
  10569. uint16 lvl_idx = startlvl-1+j;
  10570. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  10571. //Tells if this SP is lower than previous level (but not for 99->100)
  10572. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  10573. ShowInfo("pc_readdb_job_basehpsp: SP value at entry %d col %d is lower than previous level (job=%d,lvl=%d,oldval=%d,val=%d).\n",
  10574. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  10575. }
  10576. }
  10577. }
  10578. }
  10579. return true;
  10580. }
  10581. #endif
  10582. /** [Cydh]
  10583. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  10584. */
  10585. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  10586. {
  10587. int64 class_tmp;
  10588. int idx, class_;
  10589. uint16 str, agi, vit, int_, dex, luk;
  10590. script_get_constant(trim(fields[0]),&class_tmp);
  10591. class_ = static_cast<int>(class_tmp);
  10592. if ((idx = pc_class2idx(class_)) < 0) {
  10593. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  10594. return false;
  10595. }
  10596. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  10597. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  10598. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  10599. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  10600. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10601. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10602. job_info[idx].max_param.str = str;
  10603. job_info[idx].max_param.agi = agi;
  10604. job_info[idx].max_param.vit = vit;
  10605. job_info[idx].max_param.int_ = int_;
  10606. job_info[idx].max_param.dex = dex;
  10607. job_info[idx].max_param.luk = luk;
  10608. return true;
  10609. }
  10610. /**
  10611. * Read job_noenter_map.txt
  10612. **/
  10613. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10614. int idx, class_ = -1;
  10615. int64 class_tmp;
  10616. if (ISDIGIT(str[0][0])) {
  10617. class_ = atoi(str[0]);
  10618. } else {
  10619. if (!script_get_constant(str[0], &class_tmp)) {
  10620. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10621. return false;
  10622. }
  10623. class_ = static_cast<int>(class_tmp);
  10624. }
  10625. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10626. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  10627. return false;
  10628. }
  10629. job_info[idx].noenter_map.zone = atoi(str[1]);
  10630. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10631. return true;
  10632. }
  10633. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10634. int i=1;
  10635. char line[24000]; //FIXME this seem too big
  10636. FILE *fp;
  10637. sprintf(line, "%s/statpoint.txt", basedir);
  10638. fp=fopen(line,"r");
  10639. if(fp == NULL){
  10640. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10641. return max(last_s,i);
  10642. } else {
  10643. int entries=0;
  10644. while(fgets(line, sizeof(line), fp))
  10645. {
  10646. int stat;
  10647. trim(line);
  10648. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10649. continue;
  10650. if ((stat=strtoul(line,NULL,10))<0)
  10651. stat=0;
  10652. if (i > MAX_LEVEL)
  10653. break;
  10654. statp[i]=stat;
  10655. i++;
  10656. entries++;
  10657. }
  10658. fclose(fp);
  10659. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10660. }
  10661. return max(last_s,i);
  10662. }
  10663. /*==========================================
  10664. * pc DB reading.
  10665. * job_exp.txt - required experience values
  10666. * skill_tree.txt - skill tree for every class
  10667. * attr_fix.txt - elemental adjustment table
  10668. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10669. * job_db2.txt - job,stats bonuses/lvl
  10670. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10671. *------------------------------------------*/
  10672. void pc_readdb(void) {
  10673. int i, k, s = 1;
  10674. const char* dbsubpath[] = {
  10675. "",
  10676. "/" DBIMPORT,
  10677. //add other path here
  10678. };
  10679. //reset
  10680. memset(job_info,0,sizeof(job_info)); // job_info table
  10681. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10682. sv_readdb(db_path, DBPATH "level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10683. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10684. for( k=1; k < 3; k++ ){ // fill in the blanks
  10685. int j;
  10686. for( j = 0; j < CLASS_ALL; j++ ){
  10687. int tmp = 0;
  10688. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10689. if( i == MAX_LEVEL+1 )
  10690. tmp = level_penalty[k][j][0];// reset
  10691. if( level_penalty[k][j][i] > 0 )
  10692. tmp = level_penalty[k][j][i];
  10693. else
  10694. level_penalty[k][j][i] = tmp;
  10695. }
  10696. }
  10697. }
  10698. #endif
  10699. // reset then read statspoint
  10700. memset(statp,0,sizeof(statp));
  10701. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10702. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10703. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10704. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10705. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10706. if(i==0) {
  10707. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10708. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10709. }
  10710. else {
  10711. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10712. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10713. }
  10714. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10715. if (i == 0)
  10716. #ifdef RENEWAL_ASPD // Paths are hardcoded here to specifically pick the correct database
  10717. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10718. #else
  10719. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10720. #endif
  10721. else
  10722. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, true);
  10723. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10724. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10725. #ifdef HP_SP_TABLES
  10726. sv_readdb(dbsubpath2, "job_basehpsp_db.txt", ',', 4, 4+500, CLASS_COUNT*2, &pc_readdb_job_basehpsp, i > 0); //Make it support until lvl 500!
  10727. #endif
  10728. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10729. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10730. aFree(dbsubpath1);
  10731. aFree(dbsubpath2);
  10732. }
  10733. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10734. memset(skill_tree, 0, sizeof(skill_tree));
  10735. sv_readdb(db_path, DBPATH"skill_tree.txt", ',', 3 + MAX_PC_SKILL_REQUIRE * 2, 5 + MAX_PC_SKILL_REQUIRE * 2, -1, &pc_readdb_skilltree, 0);
  10736. sv_readdb(db_path, DBIMPORT"/skill_tree.txt", ',', 3 + MAX_PC_SKILL_REQUIRE * 2, 5 + MAX_PC_SKILL_REQUIRE * 2, -1, &pc_readdb_skilltree, 1);
  10737. // generate the remaining parts of the db if necessary
  10738. k = battle_config.use_statpoint_table; //save setting
  10739. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10740. statp[0] = 45; // seed value
  10741. for (; s <= MAX_LEVEL; s++)
  10742. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10743. battle_config.use_statpoint_table = k; //restore setting
  10744. //Checking if all class have their data
  10745. for (i = 0; i < JOB_MAX; i++) {
  10746. int idx;
  10747. uint16 j;
  10748. if (!pcdb_checkid(i))
  10749. continue;
  10750. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10751. continue; //Classes that do not need exp tables.
  10752. idx = pc_class2idx(i);
  10753. if (!job_info[idx].max_level[0])
  10754. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10755. if (!job_info[idx].max_level[1])
  10756. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10757. //Init and checking the empty value of Base HP/SP [Cydh]
  10758. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10759. if (job_info[idx].base_hp[j] == 0)
  10760. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10761. if (job_info[idx].base_sp[j] == 0)
  10762. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10763. }
  10764. }
  10765. }
  10766. // Read MOTD on startup. [Valaris]
  10767. int pc_read_motd(void)
  10768. {
  10769. FILE* fp;
  10770. // clear old MOTD
  10771. memset(motd_text, 0, sizeof(motd_text));
  10772. // read current MOTD
  10773. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10774. {
  10775. unsigned int entries = 0;
  10776. char buf[CHAT_SIZE_MAX];
  10777. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10778. {
  10779. unsigned int lines = 0;
  10780. size_t len;
  10781. lines++;
  10782. if( buf[0] == '/' && buf[1] == '/' )
  10783. continue;
  10784. len = strlen(buf);
  10785. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10786. len--;
  10787. if( len ) {
  10788. char * ptr;
  10789. buf[len] = 0;
  10790. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10791. 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);
  10792. }
  10793. else {// empty line
  10794. buf[0] = ' ';
  10795. buf[1] = 0;
  10796. }
  10797. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10798. entries++;
  10799. }
  10800. fclose(fp);
  10801. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10802. }
  10803. else
  10804. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10805. return 0;
  10806. }
  10807. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10808. int i,cursor = 0;
  10809. struct item_cd* cd = NULL;
  10810. if( load ) {
  10811. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10812. // no item cooldown is associated with this character
  10813. return;
  10814. }
  10815. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10816. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10817. sd->item_delay[cursor].tick = cd->tick[i];
  10818. sd->item_delay[cursor].nameid = cd->nameid[i];
  10819. cursor++;
  10820. }
  10821. }
  10822. idb_remove(itemcd_db,sd->status.char_id);
  10823. } else {
  10824. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10825. // create a new skill cooldown object for map storage
  10826. CREATE( cd, struct item_cd, 1 );
  10827. idb_put( itemcd_db, sd->status.char_id, cd );
  10828. }
  10829. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10830. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10831. cd->tick[cursor] = sd->item_delay[i].tick;
  10832. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10833. cursor++;
  10834. }
  10835. }
  10836. }
  10837. return;
  10838. }
  10839. /**
  10840. * Add item delay to player's item delay data
  10841. * @param sd Player
  10842. * @param id Item data
  10843. * @param tick Current tick
  10844. * @param n Item index in inventory
  10845. * @return 0: No delay, can consume item.
  10846. * 1: Has delay, cancel consumption.
  10847. **/
  10848. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10849. int i;
  10850. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10851. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10852. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10853. if( i < MAX_ITEMDELAYS ) {
  10854. if( sd->item_delay[i].nameid ) {// found
  10855. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10856. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10857. char e_msg[CHAT_SIZE_MAX];
  10858. if( e_tick > 99 )
  10859. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10860. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  10861. else
  10862. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10863. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  10864. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10865. return 1; // Delay has not expired yet
  10866. }
  10867. } else {// not yet used item (all slots are initially empty)
  10868. sd->item_delay[i].nameid = id->nameid;
  10869. }
  10870. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10871. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  10872. } else {// should not happen
  10873. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  10874. }
  10875. //clean up used delays so we can give room for more
  10876. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10877. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10878. sd->item_delay[i].tick = 0;
  10879. sd->item_delay[i].nameid = 0;
  10880. }
  10881. }
  10882. return 0;
  10883. }
  10884. /**
  10885. * Check if player has delay to reuse item
  10886. * @param sd Player
  10887. * @param id Item data
  10888. * @param tick Current tick
  10889. * @param n Item index in inventory
  10890. * @return 0: No delay, can consume item.
  10891. * 1: Has delay, cancel consumption.
  10892. **/
  10893. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10894. struct status_change *sc = NULL;
  10895. nullpo_retr(0, sd);
  10896. nullpo_retr(0, id);
  10897. // Do normal delay assignment
  10898. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  10899. return pc_itemcd_add(sd, id, tick, n);
  10900. // Send reply of delay remains
  10901. if (sc->data[id->delay.sc]) {
  10902. const struct TimerData *timer = get_timer(sc->data[id->delay.sc]->timer);
  10903. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  10904. return 1;
  10905. }
  10906. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  10907. return 0;
  10908. }
  10909. /**
  10910. * Clear the dmglog data from player
  10911. * @param sd
  10912. * @param md
  10913. **/
  10914. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10915. {
  10916. uint8 i;
  10917. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10918. if (i < DAMAGELOG_SIZE) {
  10919. md->dmglog[i].id = 0;
  10920. md->dmglog[i].dmg = 0;
  10921. md->dmglog[i].flag = 0;
  10922. }
  10923. }
  10924. /**
  10925. * Add log to player's dmglog
  10926. * @param sd
  10927. * @param id Monster's GID
  10928. **/
  10929. void pc_damage_log_add(struct map_session_data *sd, int id)
  10930. {
  10931. uint8 i = 0;
  10932. if (!sd || !id)
  10933. return;
  10934. //Only store new data, don't need to renew the old one with same id
  10935. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10936. if (i < DAMAGELOG_SIZE_PC)
  10937. return;
  10938. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10939. if (sd->dmglog[i] == 0) {
  10940. sd->dmglog[i] = id;
  10941. return;
  10942. }
  10943. }
  10944. }
  10945. /**
  10946. * Clear dmglog data from player
  10947. * @param sd
  10948. * @param id Monster's id
  10949. **/
  10950. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10951. {
  10952. uint8 i;
  10953. struct mob_data *md = NULL;
  10954. if (!sd)
  10955. return;
  10956. if (!id) {
  10957. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10958. if( !sd->dmglog[i] ) //skip the empty value
  10959. continue;
  10960. if ((md = map_id2md(sd->dmglog[i])))
  10961. pc_clear_log_damage_sub(sd->status.char_id,md);
  10962. sd->dmglog[i] = 0;
  10963. }
  10964. }
  10965. else {
  10966. if ((md = map_id2md(id)))
  10967. pc_clear_log_damage_sub(sd->status.char_id,md);
  10968. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10969. if (i < DAMAGELOG_SIZE_PC)
  10970. sd->dmglog[i] = 0;
  10971. }
  10972. }
  10973. /**
  10974. * Status change data arrived from char-server
  10975. * @param sd: Player data
  10976. */
  10977. void pc_scdata_received(struct map_session_data *sd) {
  10978. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10979. clif_weight_limit( sd );
  10980. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10981. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10982. }
  10983. sd->state.pc_loaded = true;
  10984. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10985. sd->state.connect_new = 1;
  10986. clif_parse_LoadEndAck(sd->fd, sd);
  10987. }
  10988. if (pc_iscarton(sd)) {
  10989. sd->cart_weight_max = 0; // Force a client refesh
  10990. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10991. }
  10992. if (sd->sc.data[SC_SOULENERGY])
  10993. sd->soulball = sd->sc.data[SC_SOULENERGY]->val1;
  10994. }
  10995. /**
  10996. * Check player account expiration time and rental item expirations
  10997. * @param sd: Player data
  10998. */
  10999. void pc_check_expiration(struct map_session_data *sd) {
  11000. #ifndef ENABLE_SC_SAVING
  11001. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  11002. #endif
  11003. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  11004. time_t exp_time = sd->expiration_time;
  11005. char tmpstr[1024];
  11006. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  11007. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  11008. pc_expire_check(sd);
  11009. }
  11010. }
  11011. TIMER_FUNC(pc_expiration_timer){
  11012. struct map_session_data *sd = map_id2sd(id);
  11013. if( !sd ) return 0;
  11014. sd->expiration_tid = INVALID_TIMER;
  11015. if( sd->fd )
  11016. clif_authfail_fd(sd->fd,10);
  11017. map_quit(sd);
  11018. return 0;
  11019. }
  11020. TIMER_FUNC(pc_autotrade_timer){
  11021. struct map_session_data *sd = map_id2sd(id);
  11022. if (!sd)
  11023. return 0;
  11024. sd->autotrade_tid = INVALID_TIMER;
  11025. if (sd->state.autotrade&2)
  11026. vending_reopen(sd);
  11027. if (sd->state.autotrade&4)
  11028. buyingstore_reopen(sd);
  11029. if (!sd->vender_id && !sd->buyer_id) {
  11030. sd->state.autotrade = 0;
  11031. map_quit(sd);
  11032. }
  11033. return 0;
  11034. }
  11035. /* this timer exists only when a character with a expire timer > 24h is online */
  11036. /* it loops thru online players once an hour to check whether a new < 24h is available */
  11037. TIMER_FUNC(pc_global_expiration_timer){
  11038. struct s_mapiterator* iter;
  11039. struct map_session_data* sd;
  11040. iter = mapit_getallusers();
  11041. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  11042. if( sd->expiration_time )
  11043. pc_expire_check(sd);
  11044. mapit_free(iter);
  11045. return 0;
  11046. }
  11047. void pc_expire_check(struct map_session_data *sd) {
  11048. /* ongoing timer */
  11049. if( sd->expiration_tid != INVALID_TIMER )
  11050. return;
  11051. /* not within the next 24h, enable the global check */
  11052. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  11053. /* global check not running, enable */
  11054. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  11055. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  11056. return;
  11057. }
  11058. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  11059. }
  11060. /**
  11061. * Deposit some money to bank
  11062. * @param sd
  11063. * @param money Amount of money to deposit
  11064. **/
  11065. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  11066. unsigned int limit_check = money + sd->bank_vault;
  11067. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  11068. return BDA_OVERFLOW;
  11069. } else if ( money > sd->status.zeny ) {
  11070. return BDA_NO_MONEY;
  11071. }
  11072. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  11073. return BDA_NO_MONEY;
  11074. sd->bank_vault += money;
  11075. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  11076. if( save_settings&CHARSAVE_BANK )
  11077. chrif_save(sd, CSAVE_NORMAL);
  11078. return BDA_SUCCESS;
  11079. }
  11080. /**
  11081. * Withdraw money from bank
  11082. * @param sd
  11083. * @param money Amount of money that will be withdrawn
  11084. **/
  11085. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  11086. unsigned int limit_check = money + sd->status.zeny;
  11087. if( money <= 0 ) {
  11088. return BWA_UNKNOWN_ERROR;
  11089. } else if ( money > sd->bank_vault ) {
  11090. return BWA_NO_MONEY;
  11091. } else if ( limit_check > MAX_ZENY ) {
  11092. /* no official response for this scenario exists. */
  11093. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  11094. return BWA_UNKNOWN_ERROR;
  11095. }
  11096. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  11097. return BWA_NO_MONEY;
  11098. sd->bank_vault -= money;
  11099. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  11100. if( save_settings&CHARSAVE_BANK )
  11101. chrif_save(sd, CSAVE_NORMAL);
  11102. return BWA_SUCCESS;
  11103. }
  11104. /**
  11105. * Clear Crimson Marker data from caster
  11106. * @param sd: Player
  11107. **/
  11108. void pc_crimson_marker_clear(struct map_session_data *sd) {
  11109. uint8 i;
  11110. if (!sd)
  11111. return;
  11112. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  11113. struct block_list *bl = NULL;
  11114. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  11115. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  11116. sd->c_marker[i] = 0;
  11117. }
  11118. }
  11119. /**
  11120. * Show version to player
  11121. * @param sd: Player
  11122. **/
  11123. void pc_show_version(struct map_session_data *sd) {
  11124. const char* svn = get_svn_revision();
  11125. char buf[CHAT_SIZE_MAX];
  11126. if( svn[0] != UNKNOWN_VERSION )
  11127. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  11128. else {
  11129. const char* git = get_git_hash();
  11130. if( git[0] != UNKNOWN_VERSION )
  11131. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  11132. else
  11133. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  11134. }
  11135. clif_displaymessage(sd->fd,buf);
  11136. }
  11137. /**
  11138. * Run bonus_script on player
  11139. * @param sd
  11140. * @author [Cydh]
  11141. **/
  11142. void pc_bonus_script(struct map_session_data *sd) {
  11143. t_tick now = gettick();
  11144. struct linkdb_node *node = NULL, *next = NULL;
  11145. if (!sd || !(node = sd->bonus_script.head))
  11146. return;
  11147. while (node) {
  11148. struct s_bonus_script_entry *entry = NULL;
  11149. next = node->next;
  11150. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  11151. // Only start timer for new bonus_script
  11152. if (entry->tid == INVALID_TIMER) {
  11153. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  11154. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  11155. entry->tick += now;
  11156. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  11157. }
  11158. if (entry->script)
  11159. run_script(entry->script, 0, sd->bl.id, 0);
  11160. else
  11161. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  11162. }
  11163. node = next;
  11164. }
  11165. }
  11166. /**
  11167. * Add bonus_script to player
  11168. * @param sd Player
  11169. * @param script_str Script string
  11170. * @param dur Duration in ms
  11171. * @param icon EFST
  11172. * @param flag Flags @see enum e_bonus_script_flags
  11173. * @param type 0 - None, 1 - Buff, 2 - Debuff
  11174. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  11175. * @author [Cydh]
  11176. **/
  11177. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, t_tick dur, enum efst_types icon, uint16 flag, uint8 type) {
  11178. struct script_code *script = NULL;
  11179. struct linkdb_node *node = NULL;
  11180. struct s_bonus_script_entry *entry = NULL;
  11181. if (!sd)
  11182. return NULL;
  11183. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  11184. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  11185. return NULL;
  11186. }
  11187. // Duplication checks
  11188. if ((node = sd->bonus_script.head)) {
  11189. while (node) {
  11190. entry = (struct s_bonus_script_entry *)node->data;
  11191. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  11192. t_tick newdur = gettick() + dur;
  11193. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  11194. sett_tickimer(entry->tid, newdur);
  11195. script_free_code(script);
  11196. return NULL;
  11197. }
  11198. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  11199. break;
  11200. else { // No duplicate bonus
  11201. script_free_code(script);
  11202. return NULL;
  11203. }
  11204. }
  11205. node = node->next;
  11206. }
  11207. }
  11208. CREATE(entry, struct s_bonus_script_entry, 1);
  11209. entry->script_buf = StringBuf_Malloc();
  11210. StringBuf_AppendStr(entry->script_buf, script_str);
  11211. entry->tid = INVALID_TIMER;
  11212. entry->flag = flag;
  11213. entry->icon = icon;
  11214. entry->tick = dur; // Use duration first, on run change to expire time
  11215. entry->type = type;
  11216. entry->script = script;
  11217. sd->bonus_script.count++;
  11218. return entry;
  11219. }
  11220. /**
  11221. * Remove bonus_script data from player
  11222. * @param sd: Target player
  11223. * @param list: Bonus script entry from player
  11224. * @author [Cydh]
  11225. **/
  11226. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  11227. if (entry->tid != INVALID_TIMER)
  11228. delete_timer(entry->tid, pc_bonus_script_timer);
  11229. if (entry->script)
  11230. script_free_code(entry->script);
  11231. if (entry->script_buf)
  11232. StringBuf_Free(entry->script_buf);
  11233. if (sd) {
  11234. if (entry->icon != EFST_BLANK)
  11235. clif_status_load(&sd->bl, entry->icon, 0);
  11236. if (sd->bonus_script.count > 0)
  11237. sd->bonus_script.count--;
  11238. }
  11239. aFree(entry);
  11240. }
  11241. /**
  11242. * Do final process if no entry left
  11243. * @param sd
  11244. **/
  11245. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  11246. if (sd->bonus_script.count == 0) {
  11247. if (sd->bonus_script.head && sd->bonus_script.head->data)
  11248. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  11249. linkdb_final(&sd->bonus_script.head);
  11250. }
  11251. }
  11252. /**
  11253. * Timer for bonus_script
  11254. * @param tid
  11255. * @param tick
  11256. * @param id
  11257. * @param data
  11258. * @author [Cydh]
  11259. **/
  11260. TIMER_FUNC(pc_bonus_script_timer){
  11261. struct map_session_data *sd;
  11262. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  11263. sd = map_id2sd(id);
  11264. if (!sd) {
  11265. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  11266. return 0;
  11267. }
  11268. if (tid == INVALID_TIMER)
  11269. return 0;
  11270. if (!sd->bonus_script.head || entry == NULL) {
  11271. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  11272. return 0;
  11273. }
  11274. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11275. pc_bonus_script_free_entry(sd, entry);
  11276. pc_bonus_script_check_final(sd);
  11277. status_calc_pc(sd,SCO_NONE);
  11278. return 0;
  11279. }
  11280. /**
  11281. * Check then clear all active timer(s) of bonus_script data from player based on reason
  11282. * @param sd: Target player
  11283. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  11284. * @author [Cydh]
  11285. **/
  11286. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  11287. struct linkdb_node *node = NULL;
  11288. uint16 count = 0;
  11289. if (!sd || !(node = sd->bonus_script.head))
  11290. return;
  11291. while (node) {
  11292. struct linkdb_node *next = node->next;
  11293. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  11294. if (entry && (
  11295. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  11296. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  11297. (flag&entry->flag) || // Matched flag
  11298. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  11299. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  11300. )
  11301. )
  11302. {
  11303. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11304. pc_bonus_script_free_entry(sd, entry);
  11305. count++;
  11306. }
  11307. node = next;
  11308. }
  11309. pc_bonus_script_check_final(sd);
  11310. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  11311. status_calc_pc(sd,SCO_NONE);
  11312. }
  11313. /** [Cydh]
  11314. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  11315. * @param sd: Target player
  11316. */
  11317. void pc_cell_basilica(struct map_session_data *sd) {
  11318. nullpo_retv(sd);
  11319. #ifdef RENEWAL
  11320. enum sc_type type = SC_BASILICA_CELL;
  11321. #else
  11322. enum sc_type type = SC_BASILICA;
  11323. #endif
  11324. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  11325. if (sd->sc.data[type])
  11326. status_change_end(&sd->bl, type,INVALID_TIMER);
  11327. }
  11328. else if (!sd->sc.data[type])
  11329. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  11330. }
  11331. /** [Cydh]
  11332. * Get maximum specified parameter for specified class
  11333. * @param class_: sd->class
  11334. * @param sex: sd->status.sex
  11335. * @param flag: parameter will be checked
  11336. * @return max_param
  11337. */
  11338. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  11339. int idx = -1, class_ = sd->class_;
  11340. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  11341. short max_param = 0;
  11342. switch (param) {
  11343. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  11344. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  11345. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  11346. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  11347. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  11348. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  11349. }
  11350. if (max_param > 0)
  11351. return max_param;
  11352. }
  11353. // Always check babies first
  11354. if( class_ & JOBL_BABY ){
  11355. if( class_ & JOBL_THIRD ){
  11356. return battle_config.max_baby_third_parameter;
  11357. }else{
  11358. return battle_config.max_baby_parameter;
  11359. }
  11360. }
  11361. // Summoner
  11362. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11363. return battle_config.max_summoner_parameter;
  11364. }
  11365. // Extended classes
  11366. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11367. return battle_config.max_extended_parameter;
  11368. }
  11369. // 3rd class
  11370. if( class_ & JOBL_THIRD ){
  11371. // Transcendent
  11372. if( class_ & JOBL_UPPER ){
  11373. return battle_config.max_third_trans_parameter;
  11374. }else{
  11375. return battle_config.max_third_parameter;
  11376. }
  11377. }
  11378. // Transcendent
  11379. if( class_ & JOBL_UPPER ){
  11380. return battle_config.max_trans_parameter;
  11381. }
  11382. return battle_config.max_parameter;
  11383. }
  11384. /**
  11385. * Get max ASPD for player based on Class
  11386. * @param sd Player
  11387. * @return ASPD
  11388. */
  11389. short pc_maxaspd(struct map_session_data *sd) {
  11390. nullpo_ret(sd);
  11391. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  11392. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  11393. battle_config.max_aspd ));
  11394. }
  11395. /**
  11396. * Calculates total item-group related bonuses for the given item
  11397. * @param sd Player
  11398. * @param nameid Item ID
  11399. * @return Heal rate
  11400. **/
  11401. short pc_get_itemgroup_bonus(struct map_session_data* sd, t_itemid nameid) {
  11402. if (sd->itemgrouphealrate.empty())
  11403. return 0;
  11404. short bonus = 0;
  11405. for (const auto &it : sd->itemgrouphealrate) {
  11406. uint16 group_id = it.id, i;
  11407. struct s_item_group_db *group = NULL;
  11408. if (!group_id || !(group = itemdb_group_exists(group_id)))
  11409. continue;
  11410. for (i = 0; i < group->random[0].data_qty; i++) {
  11411. if (group->random[0].data[i].nameid == nameid) {
  11412. bonus += it.val;
  11413. break;
  11414. }
  11415. }
  11416. }
  11417. return bonus;
  11418. }
  11419. /**
  11420. * Calculates total item-group related bonuses for the given item group
  11421. * @param sd Player
  11422. * @param group_id Item Group ID
  11423. * @return Heal rate
  11424. **/
  11425. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  11426. if (sd->itemgrouphealrate.empty())
  11427. return 0;
  11428. for (const auto &it : sd->itemgrouphealrate) {
  11429. if (it.id == group_id)
  11430. return it.val;
  11431. }
  11432. return 0;
  11433. }
  11434. /**
  11435. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  11436. * @param eqi Item EQI of enum equip_index
  11437. * @param *equip_index Player's equip_index[]
  11438. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  11439. * @return True if item in same inventory index, False if doesn't
  11440. */
  11441. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  11442. if (index < 0 || index >= MAX_INVENTORY)
  11443. return true;
  11444. // Dual weapon checks
  11445. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  11446. return true;
  11447. // Headgear with Mid & Low location
  11448. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  11449. return true;
  11450. // Headgear with Top & Mid or Low location
  11451. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  11452. return true;
  11453. // Headgear with Mid & Low location
  11454. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  11455. return true;
  11456. // Headgear with Top & Mid or Low location
  11457. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  11458. return true;
  11459. return false;
  11460. }
  11461. /**
  11462. * Generate Unique item ID for player
  11463. * @param sd : Player
  11464. * @return A generated Unique item ID
  11465. */
  11466. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  11467. nullpo_ret(sd);
  11468. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  11469. }
  11470. /**
  11471. * Validating skill from player after logged on
  11472. * @param sd
  11473. **/
  11474. void pc_validate_skill(struct map_session_data *sd) {
  11475. if (sd) {
  11476. uint16 i = 0, count = 0;
  11477. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  11478. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  11479. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  11480. for (i = 0; i < MAX_SKILL; i++) {
  11481. uint16 idx = 0;
  11482. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  11483. continue;
  11484. if ((idx = skill_get_index(tmp_skills[i].id))) {
  11485. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  11486. count++;
  11487. }
  11488. else
  11489. 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);
  11490. }
  11491. }
  11492. }
  11493. /**
  11494. * Toggle to remember if the questinfo is displayed yet or not.
  11495. * @param qi_display Display flag
  11496. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  11497. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  11498. **/
  11499. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct s_questinfo *qi, bool show) {
  11500. if (show) {
  11501. // Check if need to be displayed
  11502. if ((*qi_display) != 1) {
  11503. (*qi_display) = 1;
  11504. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  11505. }
  11506. }
  11507. else {
  11508. // Check if need to be hide
  11509. if ((*qi_display) != 0) {
  11510. (*qi_display) = 0;
  11511. #if PACKETVER >= 20120410
  11512. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_NONE, QMARK_NONE);
  11513. #else
  11514. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_QUEST, QMARK_NONE);
  11515. #endif
  11516. }
  11517. }
  11518. }
  11519. /**
  11520. * Show available NPC Quest / Event Icon Check [Kisuka]
  11521. * @param sd Player
  11522. **/
  11523. void pc_show_questinfo(struct map_session_data *sd) {
  11524. #if PACKETVER >= 20090218
  11525. nullpo_retv(sd);
  11526. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11527. return;
  11528. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11529. nullpo_retv(mapdata);
  11530. if (mapdata->qi_data.empty())
  11531. return;
  11532. if (mapdata->qi_data.size() != sd->qi_count)
  11533. return; // init was not called yet
  11534. struct s_questinfo *qi = nullptr;
  11535. bool show;
  11536. for (int i = 0; i < mapdata->qi_data.size(); i++) {
  11537. qi = &mapdata->qi_data[i];
  11538. if (!qi)
  11539. continue;
  11540. if (!qi->condition)
  11541. show = true;
  11542. else {
  11543. if (achievement_check_condition(qi->condition, sd))
  11544. show = true;
  11545. else
  11546. show = false;
  11547. }
  11548. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, show);
  11549. }
  11550. #endif
  11551. }
  11552. /**
  11553. * Reinit the questinfo for player when changing map
  11554. * @param sd Player
  11555. **/
  11556. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  11557. #if PACKETVER >= 20090218
  11558. nullpo_retv(sd);
  11559. if (sd->qi_display) {
  11560. aFree(sd->qi_display);
  11561. sd->qi_display = nullptr;
  11562. }
  11563. sd->qi_count = 0;
  11564. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11565. return;
  11566. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11567. nullpo_retv(mapdata);
  11568. if (mapdata->qi_data.empty())
  11569. return;
  11570. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_data.size()));
  11571. #endif
  11572. }
  11573. /**
  11574. * Check if a job is allowed to enter the map
  11575. * @param jobid Job ID see enum e_job or sd->status.class_
  11576. * @param m ID -an index- for direct indexing map[] array
  11577. * @return 1 if job is allowed, 0 otherwise
  11578. **/
  11579. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  11580. uint16 idx = 0;
  11581. // Map is other map server.
  11582. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  11583. if (m < 0)
  11584. return true;
  11585. struct map_data *mapdata = map_getmapdata(m);
  11586. if (!mapdata->cell)
  11587. return false;
  11588. if (!pcdb_checkid(jobid))
  11589. return false;
  11590. idx = pc_class2idx(jobid);
  11591. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  11592. return true;
  11593. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  11594. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  11595. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  11596. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  11597. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  11598. (job_info[idx].noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  11599. )
  11600. return false;
  11601. return true;
  11602. }
  11603. /**
  11604. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11605. * @param sd
  11606. **/
  11607. void pc_set_costume_view(struct map_session_data *sd) {
  11608. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11609. struct item_data *id = NULL;
  11610. nullpo_retv(sd);
  11611. head_low = sd->status.head_bottom;
  11612. head_mid = sd->status.head_mid;
  11613. head_top = sd->status.head_top;
  11614. robe = sd->status.robe;
  11615. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11616. //Added check to prevent sending the same look on multiple slots ->
  11617. //causes client to redraw item on top of itself. (suggested by Lupus)
  11618. // Normal headgear checks
  11619. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11620. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11621. sd->status.head_bottom = id->look;
  11622. else
  11623. sd->status.head_bottom = 0;
  11624. }
  11625. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11626. if (!(id->equip&(EQP_HEAD_TOP)))
  11627. sd->status.head_mid = id->look;
  11628. else
  11629. sd->status.head_mid = 0;
  11630. }
  11631. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11632. sd->status.head_top = id->look;
  11633. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11634. sd->status.robe = id->look;
  11635. // Costumes check
  11636. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11637. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11638. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11639. sd->status.head_bottom = id->look;
  11640. else
  11641. sd->status.head_bottom = 0;
  11642. }
  11643. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11644. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11645. sd->status.head_mid = id->look;
  11646. else
  11647. sd->status.head_mid = 0;
  11648. }
  11649. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11650. sd->status.head_top = id->look;
  11651. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11652. sd->status.robe = id->look;
  11653. }
  11654. if (sd->setlook_head_bottom)
  11655. sd->status.head_bottom = sd->setlook_head_bottom;
  11656. if (sd->setlook_head_mid)
  11657. sd->status.head_mid = sd->setlook_head_mid;
  11658. if (sd->setlook_head_top)
  11659. sd->status.head_top = sd->setlook_head_top;
  11660. if (sd->setlook_robe)
  11661. sd->status.robe = sd->setlook_robe;
  11662. if (head_low != sd->status.head_bottom)
  11663. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11664. if (head_mid != sd->status.head_mid)
  11665. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11666. if (head_top != sd->status.head_top)
  11667. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11668. if (robe != sd->status.robe)
  11669. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11670. }
  11671. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  11672. uint32 date = date_get(DT_YYYYMMDD);
  11673. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  11674. std::shared_ptr<s_attendance_period> period = pair.second;
  11675. if( period->start <= date && period->end >= date ){
  11676. return period;
  11677. }
  11678. }
  11679. return nullptr;
  11680. }
  11681. bool pc_attendance_enabled(){
  11682. // Check if the attendance feature is disabled
  11683. if( !battle_config.feature_attendance ){
  11684. return false;
  11685. }
  11686. // Check if there is a running attendance period
  11687. return pc_attendance_period() != nullptr;
  11688. }
  11689. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11690. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11691. }
  11692. int32 pc_attendance_counter( struct map_session_data* sd ){
  11693. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11694. // No running attendance period
  11695. if( period == nullptr ){
  11696. return 0;
  11697. }
  11698. // Get the counter for the current period
  11699. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11700. // Check if we have a remaining counter from a previous period
  11701. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11702. // Reset the counter to zero
  11703. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11704. return 0;
  11705. }
  11706. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11707. }
  11708. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11709. // If the user's group does not have the permission
  11710. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11711. return;
  11712. }
  11713. // Check if the attendance feature is disabled
  11714. if( !pc_attendance_enabled() ){
  11715. return;
  11716. }
  11717. // Check if the user already got his reward today
  11718. if( pc_attendance_rewarded_today( sd ) ){
  11719. return;
  11720. }
  11721. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11722. attendance_counter += 1;
  11723. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11724. if( period == nullptr ){
  11725. return;
  11726. }
  11727. if( period->rewards.size() < attendance_counter ){
  11728. return;
  11729. }
  11730. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11731. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11732. if( save_settings&CHARSAVE_ATTENDANCE )
  11733. chrif_save(sd, CSAVE_NORMAL);
  11734. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  11735. struct mail_message msg;
  11736. memset( &msg, 0, sizeof( struct mail_message ) );
  11737. msg.dest_id = sd->status.char_id;
  11738. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11739. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11740. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11741. msg.item[0].nameid = reward->item_id;
  11742. msg.item[0].amount = reward->amount;
  11743. msg.item[0].identify = 1;
  11744. msg.status = MAIL_NEW;
  11745. msg.type = MAIL_INBOX_NORMAL;
  11746. msg.timestamp = time(NULL);
  11747. intif_Mail_send(0, &msg);
  11748. clif_attendence_response( sd, attendance_counter );
  11749. }
  11750. /*==========================================
  11751. * pc Init/Terminate
  11752. *------------------------------------------*/
  11753. void do_final_pc(void) {
  11754. db_destroy(itemcd_db);
  11755. do_final_pc_groups();
  11756. ers_destroy(pc_sc_display_ers);
  11757. ers_destroy(num_reg_ers);
  11758. ers_destroy(str_reg_ers);
  11759. attendance_db.clear();
  11760. }
  11761. void do_init_pc(void) {
  11762. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11763. pc_readdb();
  11764. pc_read_motd(); // Read MOTD [Valaris]
  11765. attendance_db.load();
  11766. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11767. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11768. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11769. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11770. add_timer_func_list(pc_autosave, "pc_autosave");
  11771. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11772. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11773. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11774. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11775. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11776. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11777. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11778. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  11779. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11780. // 0=day, 1=night [Yor]
  11781. night_flag = battle_config.night_at_start ? 1 : 0;
  11782. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11783. int day_duration = battle_config.day_duration;
  11784. int night_duration = battle_config.night_duration;
  11785. // add night/day timer [Yor]
  11786. add_timer_func_list(map_day_timer, "map_day_timer");
  11787. add_timer_func_list(map_night_timer, "map_night_timer");
  11788. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11789. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11790. }
  11791. do_init_pc_groups();
  11792. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11793. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11794. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11795. ers_chunk_size(pc_sc_display_ers, 150);
  11796. ers_chunk_size(num_reg_ers, 300);
  11797. ers_chunk_size(str_reg_ers, 50);
  11798. }