pc.cpp 408 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298
  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. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  60. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  61. static unsigned int statp[MAX_LEVEL+1];
  62. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  63. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  64. #endif
  65. // h-files are for declarations, not for implementations... [Shinomori]
  66. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  67. // timer for night.day implementation
  68. int day_timer_tid = INVALID_TIMER;
  69. int night_timer_tid = INVALID_TIMER;
  70. struct eri *pc_sc_display_ers = NULL;
  71. struct eri *num_reg_ers;
  72. struct eri *str_reg_ers;
  73. int pc_expiration_tid = INVALID_TIMER;
  74. struct fame_list smith_fame_list[MAX_FAME_LIST];
  75. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  76. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  77. struct s_job_info job_info[CLASS_COUNT];
  78. const std::string AttendanceDatabase::getDefaultLocation(){
  79. return std::string(db_path) + "/attendance.yml";
  80. }
  81. /**
  82. * Reads and parses an entry from the attendance_db.
  83. * @param node: YAML node containing the entry.
  84. * @return count of successfully parsed rows
  85. */
  86. uint64 AttendanceDatabase::parseBodyNode(const YAML::Node &node){
  87. uint32 start;
  88. if( !this->asUInt32( node, "Start", start ) ){
  89. return 0;
  90. }
  91. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  92. bool exists = attendance_period != nullptr;
  93. if( !exists ){
  94. if( !this->nodeExists( node, "End" ) ){
  95. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  96. return 0;
  97. }
  98. if( !this->nodeExists( node, "Rewards" ) ){
  99. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  100. return 0;
  101. }
  102. attendance_period = std::make_shared<s_attendance_period>();
  103. attendance_period->start = start;
  104. }
  105. // If it does not exist yet, we need to check it for sure
  106. bool requiresCollisionDetection = !exists;
  107. if( this->nodeExists( node, "End" ) ){
  108. uint32 end;
  109. if( !this->asUInt32( node, "End", end ) ){
  110. return 0;
  111. }
  112. // If the period is outdated already, we do not even bother parsing
  113. if( end < date_get( DT_YYYYMMDD ) ){
  114. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  115. return 0;
  116. }
  117. if( !exists || attendance_period->end != end ){
  118. requiresCollisionDetection = true;
  119. attendance_period->end = end;
  120. }
  121. }
  122. // Collision detection
  123. if( requiresCollisionDetection ){
  124. bool collision = false;
  125. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  126. std::shared_ptr<s_attendance_period> period = pair.second;
  127. if( exists && period->start == attendance_period->start ){
  128. // Dont compare to yourself
  129. continue;
  130. }
  131. // Check if start is inside another period
  132. if( period->start <= attendance_period->start && start <= period->end ){
  133. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  134. collision = true;
  135. break;
  136. }
  137. // Check if end is inside another period
  138. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  139. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  140. collision = true;
  141. break;
  142. }
  143. }
  144. if( collision ){
  145. return 0;
  146. }
  147. }
  148. if( this->nodeExists( node, "Rewards" ) ){
  149. const YAML::Node& rewardsNode = node["Rewards"];
  150. for( const YAML::Node& rewardNode : rewardsNode ){
  151. uint32 day;
  152. if( !this->asUInt32( rewardNode, "Day", day ) ){
  153. continue;
  154. }
  155. day -= 1;
  156. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  157. bool reward_exists = reward != nullptr;
  158. if( !reward_exists ){
  159. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  160. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  161. return 0;
  162. }
  163. reward = std::make_shared<s_attendance_reward>();
  164. }
  165. if( this->nodeExists( rewardNode, "ItemId" ) ){
  166. uint16 item_id;
  167. if( !this->asUInt16( rewardNode, "ItemId", item_id ) ){
  168. continue;
  169. }
  170. if( item_id == 0 || !itemdb_exists( item_id ) ){
  171. ShowError( "pc_attendance_load: Unknown item ID %hu for day %d.\n", item_id, day + 1 );
  172. continue;
  173. }
  174. reward->item_id = item_id;
  175. }
  176. if( this->nodeExists( rewardNode, "Amount" ) ){
  177. uint16 amount;
  178. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  179. continue;
  180. }
  181. if( amount == 0 ){
  182. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  183. amount = 1;
  184. }else if( amount > MAX_AMOUNT ){
  185. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  186. amount = MAX_AMOUNT;
  187. }
  188. reward->amount = amount;
  189. }else{
  190. if( !reward_exists ){
  191. reward->amount = 1;
  192. }
  193. }
  194. if( !reward_exists ){
  195. attendance_period->rewards[day] = reward;
  196. }
  197. }
  198. bool missing_day = false;
  199. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  200. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  201. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  202. missing_day = true;
  203. break;
  204. }
  205. }
  206. if( missing_day ){
  207. return 0;
  208. }
  209. }
  210. if( !exists ){
  211. this->put( start, attendance_period );
  212. }
  213. return 1;
  214. }
  215. AttendanceDatabase attendance_db;
  216. #define MOTD_LINE_SIZE 128
  217. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  218. bool reg_load;
  219. /**
  220. * Translation table from athena equip index to aegis bitmask
  221. */
  222. unsigned int equip_bitmask[EQI_MAX] = {
  223. EQP_ACC_L, // EQI_ACC_L
  224. EQP_ACC_R, // EQI_ACC_R
  225. EQP_SHOES, // EQI_SHOES
  226. EQP_GARMENT, // EQI_GARMENT
  227. EQP_HEAD_LOW, // EQI_HEAD_LOW
  228. EQP_HEAD_MID, // EQI_HEAD_MID
  229. EQP_HEAD_TOP, // EQI_HEAD_TOP
  230. EQP_ARMOR, // EQI_ARMOR
  231. EQP_HAND_L, // EQI_HAND_L
  232. EQP_HAND_R, // EQI_HAND_R
  233. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  234. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  235. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  236. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  237. EQP_AMMO, // EQI_AMMO
  238. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  239. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  240. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  241. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  242. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  243. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  244. };
  245. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  246. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  247. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  248. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  249. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  250. };
  251. void pc_set_reg_load( bool val ){
  252. reg_load = val;
  253. }
  254. /**
  255. * Item Cool Down Delay Saving
  256. * Struct item_cd is not a member of struct map_session_data
  257. * to keep cooldowns in memory between player log-ins.
  258. * All cooldowns are reset when server is restarted.
  259. **/
  260. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  261. struct item_cd {
  262. t_tick tick[MAX_ITEMDELAYS]; //tick
  263. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  264. };
  265. /**
  266. * Converts a class to its array index for CLASS_COUNT defined arrays.
  267. * Note that it does not do a validity check for speed purposes, where parsing
  268. * player input make sure to use a pcdb_checkid first!
  269. * @param class_ Job ID see enum e_job
  270. * @return Class Index
  271. */
  272. int pc_class2idx(int class_) {
  273. if (class_ >= JOB_NOVICE_HIGH)
  274. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  275. return class_;
  276. }
  277. /**
  278. * Get player's group ID
  279. * @param sd
  280. * @return Group ID
  281. */
  282. int pc_get_group_id(struct map_session_data *sd) {
  283. return sd->group_id;
  284. }
  285. /** Get player's group Level
  286. * @param sd
  287. * @return Group Level
  288. */
  289. int pc_get_group_level(struct map_session_data *sd) {
  290. return sd->group_level;
  291. }
  292. static TIMER_FUNC(pc_invincible_timer){
  293. struct map_session_data *sd;
  294. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  295. return 1;
  296. if(sd->invincible_timer != tid){
  297. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  298. return 0;
  299. }
  300. sd->invincible_timer = INVALID_TIMER;
  301. skill_unit_move(&sd->bl,tick,1);
  302. return 0;
  303. }
  304. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  305. nullpo_retv(sd);
  306. if( sd->invincible_timer != INVALID_TIMER )
  307. delete_timer(sd->invincible_timer,pc_invincible_timer);
  308. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  309. }
  310. void pc_delinvincibletimer(struct map_session_data* sd)
  311. {
  312. nullpo_retv(sd);
  313. if( sd->invincible_timer != INVALID_TIMER )
  314. {
  315. delete_timer(sd->invincible_timer,pc_invincible_timer);
  316. sd->invincible_timer = INVALID_TIMER;
  317. skill_unit_move(&sd->bl,gettick(),1);
  318. }
  319. }
  320. static TIMER_FUNC(pc_spiritball_timer){
  321. struct map_session_data *sd;
  322. int i;
  323. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  324. return 1;
  325. if( sd->spiritball <= 0 )
  326. {
  327. 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);
  328. sd->spiritball = 0;
  329. return 0;
  330. }
  331. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  332. if( i == sd->spiritball )
  333. {
  334. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  335. return 0;
  336. }
  337. sd->spiritball--;
  338. if( i != sd->spiritball )
  339. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  340. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  341. clif_spiritball(&sd->bl);
  342. return 0;
  343. }
  344. /**
  345. * Adds a spiritball to player for 'interval' ms
  346. * @param sd
  347. * @param interval
  348. * @param max
  349. */
  350. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  351. {
  352. int tid;
  353. uint8 i;
  354. nullpo_retv(sd);
  355. if(max > MAX_SPIRITBALL)
  356. max = MAX_SPIRITBALL;
  357. if(sd->spiritball < 0)
  358. sd->spiritball = 0;
  359. if( sd->spiritball && sd->spiritball >= max )
  360. {
  361. if(sd->spirit_timer[0] != INVALID_TIMER)
  362. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  363. sd->spiritball--;
  364. if( sd->spiritball != 0 )
  365. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  366. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  367. }
  368. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  369. 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);
  370. if( i != sd->spiritball )
  371. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  372. sd->spirit_timer[i] = tid;
  373. sd->spiritball++;
  374. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  375. clif_millenniumshield(&sd->bl,sd->spiritball);
  376. else
  377. clif_spiritball(&sd->bl);
  378. }
  379. /**
  380. * Removes number of spiritball from player
  381. * @param sd
  382. * @param count
  383. * @param type 1 = doesn't give client effect
  384. */
  385. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  386. {
  387. uint8 i;
  388. nullpo_retv(sd);
  389. if(sd->spiritball <= 0) {
  390. sd->spiritball = 0;
  391. return;
  392. }
  393. if(count == 0)
  394. return;
  395. if(count > sd->spiritball)
  396. count = sd->spiritball;
  397. sd->spiritball -= count;
  398. if(count > MAX_SPIRITBALL)
  399. count = MAX_SPIRITBALL;
  400. for(i=0;i<count;i++) {
  401. if(sd->spirit_timer[i] != INVALID_TIMER) {
  402. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  403. sd->spirit_timer[i] = INVALID_TIMER;
  404. }
  405. }
  406. for(i=count;i<MAX_SPIRITBALL;i++) {
  407. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  408. sd->spirit_timer[i] = INVALID_TIMER;
  409. }
  410. if(!type) {
  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. /**
  418. * Increases a player's fame points and displays a notice to him
  419. * @param sd Player
  420. * @param count Fame point
  421. */
  422. void pc_addfame(struct map_session_data *sd,int count)
  423. {
  424. enum e_rank ranktype;
  425. nullpo_retv(sd);
  426. sd->status.fame += count;
  427. if(sd->status.fame > MAX_FAME)
  428. sd->status.fame = MAX_FAME;
  429. switch(sd->class_&MAPID_UPPERMASK){
  430. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  431. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  432. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  433. default:
  434. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  435. return;
  436. }
  437. clif_update_rankingpoint(sd,ranktype,count);
  438. chrif_updatefamelist(sd);
  439. }
  440. /**
  441. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  442. * @param sd
  443. * @param job Job use enum e_mapid
  444. * @return Rank
  445. */
  446. unsigned char pc_famerank(uint32 char_id, int job)
  447. {
  448. uint8 i;
  449. switch(job){
  450. case MAPID_BLACKSMITH: // Blacksmith
  451. for(i = 0; i < MAX_FAME_LIST; i++){
  452. if(smith_fame_list[i].id == char_id)
  453. return i + 1;
  454. }
  455. break;
  456. case MAPID_ALCHEMIST: // Alchemist
  457. for(i = 0; i < MAX_FAME_LIST; i++){
  458. if(chemist_fame_list[i].id == char_id)
  459. return i + 1;
  460. }
  461. break;
  462. case MAPID_TAEKWON: // Taekwon
  463. for(i = 0; i < MAX_FAME_LIST; i++){
  464. if(taekwon_fame_list[i].id == char_id)
  465. return i + 1;
  466. }
  467. break;
  468. }
  469. return 0;
  470. }
  471. /**
  472. * Restart player's HP & SP value
  473. * @param sd
  474. * @param type Restart type: 1 - Normal Resurection
  475. */
  476. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  477. struct status_data *status, *b_status;
  478. nullpo_retv(sd);
  479. b_status = &sd->base_status;
  480. status = &sd->battle_status;
  481. if (type&1) { //Normal resurrection
  482. status->hp = 1; //Otherwise status_heal may fail if dead.
  483. status_heal(&sd->bl, b_status->hp, 0, 1);
  484. if( status->sp < b_status->sp )
  485. status_set_sp(&sd->bl, b_status->sp, 1);
  486. } else { //Just for saving on the char-server (with values as if respawned)
  487. sd->status.hp = b_status->hp;
  488. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  489. }
  490. }
  491. /*==========================================
  492. Rental System
  493. *------------------------------------------*/
  494. /**
  495. * Ends a rental and removes the item/effect
  496. * @param tid: Tick ID
  497. * @param tick: Timer
  498. * @param id: Timer ID
  499. * @param data: Data
  500. * @return false - failure, true - success
  501. */
  502. TIMER_FUNC(pc_inventory_rental_end){
  503. struct map_session_data *sd = map_id2sd(id);
  504. if( sd == NULL )
  505. return 0;
  506. if( tid != sd->rental_timer ) {
  507. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  508. return 0;
  509. }
  510. pc_inventory_rentals(sd);
  511. return 1;
  512. }
  513. /**
  514. * Removes the rental timer from the player
  515. * @param sd: Player data
  516. */
  517. void pc_inventory_rental_clear(struct map_session_data *sd)
  518. {
  519. if( sd->rental_timer != INVALID_TIMER ) {
  520. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  521. sd->rental_timer = INVALID_TIMER;
  522. }
  523. }
  524. /**
  525. * Check for items in the player's inventory that are rental type
  526. * @param sd: Player data
  527. */
  528. void pc_inventory_rentals(struct map_session_data *sd)
  529. {
  530. int i, c = 0;
  531. unsigned int next_tick = UINT_MAX;
  532. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  533. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  534. continue; // Nothing here
  535. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  536. continue;
  537. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  538. if (sd->inventory_data[i]->unequip_script)
  539. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  540. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  541. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  542. } else {
  543. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  544. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  545. next_tick = umin(expire_tick * 1000U, next_tick);
  546. c++;
  547. }
  548. }
  549. 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
  550. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  551. else
  552. sd->rental_timer = INVALID_TIMER;
  553. }
  554. /**
  555. * Add a rental item to the player and adjusts the rental timer appropriately
  556. * @param sd: Player data
  557. * @param seconds: Rental time
  558. */
  559. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  560. {
  561. t_tick tick = seconds * 1000;
  562. if( sd == NULL )
  563. return;
  564. if( sd->rental_timer != INVALID_TIMER ) {
  565. const struct TimerData * td;
  566. td = get_timer(sd->rental_timer);
  567. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  568. pc_inventory_rental_clear(sd);
  569. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  570. }
  571. } else
  572. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  573. }
  574. /**
  575. * Check if the player can sell the current item
  576. * @param sd: map_session_data of the player
  577. * @param item: struct of the checking item
  578. * @param shoptype: NPC's sub type see enum npc_subtype
  579. * @return bool 'true' is sellable, 'false' otherwise
  580. */
  581. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  582. if (sd == NULL || item == NULL)
  583. return false;
  584. if (item->equip > 0 || item->amount < 0)
  585. return false;
  586. if (battle_config.hide_fav_sell && item->favorite)
  587. return false; //Cannot sell favs (optional config)
  588. if (!battle_config.rental_transaction && item->expire_time)
  589. return false; // Cannot Sell Rental Items
  590. if( item->equipSwitch ){
  591. return false;
  592. }
  593. if (itemdb_ishatched_egg(item))
  594. return false;
  595. switch (shoptype) {
  596. case NPCTYPE_SHOP:
  597. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  598. item->bound != BOUND_GUILD ||
  599. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  600. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  601. ))
  602. return true;
  603. break;
  604. case NPCTYPE_ITEMSHOP:
  605. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  606. item->bound != BOUND_GUILD ||
  607. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  608. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  609. ))
  610. return true;
  611. else if (!item->bound) {
  612. struct item_data *itd = itemdb_search(item->nameid);
  613. if (itd && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  614. return true;
  615. }
  616. break;
  617. }
  618. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  619. return false;
  620. if (item->bound && !pc_can_give_bounded_items(sd))
  621. return false; // Don't allow sale of bound items
  622. return true;
  623. }
  624. /**
  625. * Determines if player can give / drop / trade / vend items
  626. */
  627. bool pc_can_give_items(struct map_session_data *sd)
  628. {
  629. return pc_has_permission(sd, PC_PERM_TRADE);
  630. }
  631. /**
  632. * Determines if player can give / drop / trade / vend bounded items
  633. */
  634. bool pc_can_give_bounded_items(struct map_session_data *sd)
  635. {
  636. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  637. }
  638. /*==========================================
  639. * Prepares character for saving.
  640. * @param sd
  641. *------------------------------------------*/
  642. void pc_makesavestatus(struct map_session_data *sd) {
  643. nullpo_retv(sd);
  644. if(!battle_config.save_clothcolor)
  645. sd->status.clothes_color = 0;
  646. // Since this is currently not officially released,
  647. // its best to have a forced option to not save body styles.
  648. if(!battle_config.save_body_style)
  649. sd->status.body = 0;
  650. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  651. //status change load/saving. [Skotlex]
  652. #ifdef NEW_CARTS
  653. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  654. #else
  655. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  656. #endif
  657. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  658. if(pc_isdead(sd)){
  659. pc_setrestartvalue(sd, 0);
  660. } else {
  661. sd->status.hp = sd->battle_status.hp;
  662. sd->status.sp = sd->battle_status.sp;
  663. }
  664. sd->status.last_point.map = sd->mapindex;
  665. sd->status.last_point.x = sd->bl.x;
  666. sd->status.last_point.y = sd->bl.y;
  667. return;
  668. }
  669. if(pc_isdead(sd)) {
  670. pc_setrestartvalue(sd, 0);
  671. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  672. } else {
  673. sd->status.hp = sd->battle_status.hp;
  674. sd->status.sp = sd->battle_status.sp;
  675. sd->status.last_point.map = sd->mapindex;
  676. sd->status.last_point.x = sd->bl.x;
  677. sd->status.last_point.y = sd->bl.y;
  678. }
  679. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  680. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  681. if(mapdata->save.map)
  682. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  683. else
  684. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  685. }
  686. }
  687. /*==========================================
  688. * Off init ? Connection?
  689. *------------------------------------------*/
  690. 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) {
  691. nullpo_retv(sd);
  692. sd->bl.id = account_id;
  693. sd->status.account_id = account_id;
  694. sd->status.char_id = char_id;
  695. sd->status.sex = sex;
  696. sd->login_id1 = login_id1;
  697. sd->login_id2 = 0; // at this point, we can not know the value :(
  698. sd->client_tick = client_tick;
  699. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  700. sd->bl.type = BL_PC;
  701. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  702. sd->canlog_tick = gettick();
  703. //Required to prevent homunculus copuing a base speed of 0.
  704. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  705. }
  706. /**
  707. * Get equip point for an equip
  708. * @param sd
  709. * @param id
  710. */
  711. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  712. int ep = 0;
  713. nullpo_ret(sd);
  714. nullpo_ret(id);
  715. if (!itemdb_isequip2(id))
  716. return 0; //Not equippable by players.
  717. ep = id->equip;
  718. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  719. 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]
  720. if (ep == EQP_WEAPON)
  721. return EQP_ARMS;
  722. if (ep == EQP_SHADOW_WEAPON)
  723. return EQP_SHADOW_ARMS;
  724. }
  725. }
  726. return ep;
  727. }
  728. /**
  729. * Get equip point for an equip
  730. * @param sd
  731. * @param n Equip index in inventory
  732. */
  733. int pc_equippoint(struct map_session_data *sd,int n){
  734. nullpo_ret(sd);
  735. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  736. }
  737. /**
  738. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  739. * @param sd : player session
  740. * @return 0 sucess, 1:invalid sd
  741. */
  742. void pc_setinventorydata(struct map_session_data *sd)
  743. {
  744. uint8 i;
  745. nullpo_retv(sd);
  746. for(i = 0; i < MAX_INVENTORY; i++) {
  747. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  748. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  749. }
  750. }
  751. /**
  752. * 'Calculates' weapon type
  753. * @param sd : Player
  754. */
  755. void pc_calcweapontype(struct map_session_data *sd)
  756. {
  757. nullpo_retv(sd);
  758. // single-hand
  759. if(sd->weapontype2 == W_FIST) {
  760. sd->status.weapon = sd->weapontype1;
  761. return;
  762. }
  763. if(sd->weapontype1 == W_FIST) {
  764. sd->status.weapon = sd->weapontype2;
  765. return;
  766. }
  767. // dual-wield
  768. sd->status.weapon = 0;
  769. switch (sd->weapontype1){
  770. case W_DAGGER:
  771. switch (sd->weapontype2) {
  772. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  773. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  774. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  775. }
  776. break;
  777. case W_1HSWORD:
  778. switch (sd->weapontype2) {
  779. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  780. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  781. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  782. }
  783. break;
  784. case W_1HAXE:
  785. switch (sd->weapontype2) {
  786. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  787. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  788. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  789. }
  790. }
  791. // unknown, default to right hand type
  792. if (!sd->status.weapon)
  793. sd->status.weapon = sd->weapontype1;
  794. }
  795. /**
  796. * Set equip index
  797. * @param sd : Player
  798. */
  799. void pc_setequipindex(struct map_session_data *sd)
  800. {
  801. uint16 i;
  802. nullpo_retv(sd);
  803. for (i = 0; i < EQI_MAX; i++){
  804. sd->equip_index[i] = -1;
  805. sd->equip_switch_index[i] = -1;
  806. }
  807. for (i = 0; i < MAX_INVENTORY; i++) {
  808. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  809. continue;
  810. if (sd->inventory.u.items_inventory[i].equip) {
  811. uint8 j;
  812. for (j = 0; j < EQI_MAX; j++)
  813. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  814. sd->equip_index[j] = i;
  815. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  816. if (sd->inventory_data[i])
  817. sd->weapontype1 = sd->inventory_data[i]->look;
  818. else
  819. sd->weapontype1 = 0;
  820. }
  821. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  822. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  823. sd->weapontype2 = sd->inventory_data[i]->look;
  824. else
  825. sd->weapontype2 = 0;
  826. }
  827. }
  828. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  829. for (uint8 j = 0; j < EQI_MAX; j++) {
  830. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  831. sd->equip_switch_index[j] = i;
  832. }
  833. }
  834. }
  835. }
  836. pc_calcweapontype(sd);
  837. }
  838. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  839. //{
  840. // int i;
  841. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  842. // struct item_data *data;
  843. //
  844. // //Crafted/made/hatched items.
  845. // if (itemdb_isspecial(item->card[0]))
  846. // return 1;
  847. //
  848. // /* scan for enchant armor gems */
  849. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  850. // s = MAX_SLOTS - 1;
  851. //
  852. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  853. // return( i < s ) ? 0 : 1;
  854. //}
  855. /**
  856. * Check if an item is equiped by player
  857. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  858. * @param sd : player session
  859. * @param nameid : itemid
  860. * @return 1:yes, 0:no
  861. */
  862. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  863. {
  864. uint8 i;
  865. for( i = 0; i < EQI_MAX; i++ )
  866. {
  867. short index = sd->equip_index[i], j;
  868. if( index < 0 )
  869. continue;
  870. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  871. continue;
  872. if( !sd->inventory_data[index] )
  873. continue;
  874. if( sd->inventory_data[index]->nameid == nameid )
  875. return true;
  876. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  877. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  878. return true;
  879. }
  880. }
  881. return false;
  882. }
  883. /**
  884. * Check adoption rules
  885. * @param p1_sd: Player 1
  886. * @param p2_sd: Player 2
  887. * @param b_sd: Player that will be adopted
  888. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  889. * ADOPT_ALREADY_ADOPTED - Already adopted
  890. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  891. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  892. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  893. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  894. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  895. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  896. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  897. */
  898. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  899. {
  900. if( !p1_sd || !p2_sd || !b_sd )
  901. return ADOPT_CHARACTER_NOT_FOUND;
  902. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  903. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  904. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  905. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  906. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  907. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  908. if( p2_sd->status.party_id != p1_sd->status.party_id )
  909. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  910. // Parents need to have their ring equipped
  911. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  912. return ADOPT_EQUIP_RINGS;
  913. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  914. return ADOPT_EQUIP_RINGS;
  915. // Already adopted a baby
  916. if( p1_sd->status.child || p2_sd->status.child ) {
  917. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  918. return ADOPT_MORE_CHILDREN;
  919. }
  920. // Parents need at least lvl 70 to adopt
  921. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  922. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  923. return ADOPT_LEVEL_70;
  924. }
  925. if( b_sd->status.partner_id ) {
  926. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  927. return ADOPT_MARRIED;
  928. }
  929. 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 ) )
  930. return ADOPT_NOT_NOVICE;
  931. return ADOPT_ALLOWED;
  932. }
  933. /*==========================================
  934. * Adoption Process
  935. *------------------------------------------*/
  936. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  937. {
  938. int job, joblevel;
  939. unsigned int jobexp;
  940. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  941. return false;
  942. // Preserve current job levels and progress
  943. joblevel = b_sd->status.job_level;
  944. jobexp = b_sd->status.job_exp;
  945. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  946. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  947. { // Success, proceed to configure parents and baby skills
  948. p1_sd->status.child = b_sd->status.char_id;
  949. p2_sd->status.child = b_sd->status.char_id;
  950. b_sd->status.father = p1_sd->status.char_id;
  951. b_sd->status.mother = p2_sd->status.char_id;
  952. // Restore progress
  953. b_sd->status.job_level = joblevel;
  954. clif_updatestatus(b_sd, SP_JOBLEVEL);
  955. b_sd->status.job_exp = jobexp;
  956. clif_updatestatus(b_sd, SP_JOBEXP);
  957. // Baby Skills
  958. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  959. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  960. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  961. // Parents Skills
  962. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  963. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  964. chrif_save(p1_sd, CSAVE_NORMAL);
  965. chrif_save(p2_sd, CSAVE_NORMAL);
  966. chrif_save(b_sd, CSAVE_NORMAL);
  967. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  968. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  969. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  970. return true;
  971. }
  972. return false; // Job Change Fail
  973. }
  974. /*==========================================
  975. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  976. Returns:
  977. false : Cannot use/equip
  978. true : Can use/equip
  979. * Credits:
  980. [Inkfish] for first idea
  981. [Haru] for third-classes extension
  982. [Cydh] finishing :D
  983. *------------------------------------------*/
  984. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  985. while (1) {
  986. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  987. break;
  988. #ifndef RENEWAL
  989. //allow third classes to use trans. class items
  990. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  991. break;
  992. //third-baby classes can use same item too
  993. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  994. break;
  995. //don't need to decide specific rules for third-classes?
  996. //items for third classes can be used for all third classes
  997. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  998. break;
  999. #else
  1000. //trans. classes (exl. third-trans.)
  1001. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1002. break;
  1003. //baby classes (exl. third-baby)
  1004. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1005. break;
  1006. //third classes (exl. third-trans. and baby-third)
  1007. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  1008. break;
  1009. //trans-third classes
  1010. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  1011. break;
  1012. //third-baby classes
  1013. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  1014. break;
  1015. #endif
  1016. return false;
  1017. }
  1018. return true;
  1019. }
  1020. /*=================================================
  1021. * Checks if the player can equip the item at index n in inventory.
  1022. * @param sd
  1023. * @param n Item index in inventory
  1024. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1025. *------------------------------------------------*/
  1026. uint8 pc_isequip(struct map_session_data *sd,int n)
  1027. {
  1028. struct item_data *item;
  1029. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1030. item = sd->inventory_data[n];
  1031. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1032. return ITEM_EQUIP_ACK_OK;
  1033. if(item == NULL)
  1034. return ITEM_EQUIP_ACK_FAIL;
  1035. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1036. return ITEM_EQUIP_ACK_FAILLEVEL;
  1037. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1038. return ITEM_EQUIP_ACK_FAILLEVEL;
  1039. if(item->sex != 2 && sd->status.sex != item->sex)
  1040. return ITEM_EQUIP_ACK_FAIL;
  1041. //fail to equip if item is restricted
  1042. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1043. return ITEM_EQUIP_ACK_FAIL;
  1044. if (item->equip&EQP_AMMO) {
  1045. switch (item->look) {
  1046. case AMMO_ARROW:
  1047. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1048. clif_msg(sd, ITEM_NEED_BOW);
  1049. return ITEM_EQUIP_ACK_FAIL;
  1050. }
  1051. break;
  1052. case AMMO_THROWABLE_DAGGER:
  1053. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1054. return ITEM_EQUIP_ACK_FAIL;
  1055. break;
  1056. case AMMO_BULLET:
  1057. case AMMO_SHELL:
  1058. 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
  1059. #ifdef RENEWAL
  1060. && sd->status.weapon != W_GRENADE
  1061. #endif
  1062. ) {
  1063. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1064. return ITEM_EQUIP_ACK_FAIL;
  1065. }
  1066. break;
  1067. #ifndef RENEWAL
  1068. case AMMO_GRENADE:
  1069. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1070. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1071. return ITEM_EQUIP_ACK_FAIL;
  1072. }
  1073. break;
  1074. #endif
  1075. case AMMO_CANNONBALL:
  1076. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1077. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1078. return ITEM_EQUIP_ACK_FAIL;
  1079. }
  1080. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1081. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1082. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1083. return ITEM_EQUIP_ACK_FAIL;
  1084. }
  1085. break;
  1086. }
  1087. }
  1088. if (sd->sc.count) {
  1089. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1090. return ITEM_EQUIP_ACK_FAIL;
  1091. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1092. return ITEM_EQUIP_ACK_FAIL;
  1093. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1094. return ITEM_EQUIP_ACK_FAIL;
  1095. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1096. return ITEM_EQUIP_ACK_FAIL;
  1097. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1098. return ITEM_EQUIP_ACK_FAIL;
  1099. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1100. return ITEM_EQUIP_ACK_FAIL;
  1101. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1102. return ITEM_EQUIP_ACK_FAIL;
  1103. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1104. //Spirit of Super Novice equip bonuses. [Skotlex]
  1105. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1106. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1107. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  1108. switch(item->look) { //In weapons, the look determines type of weapon.
  1109. case W_DAGGER: //All level 4 - Daggers
  1110. case W_1HSWORD: //All level 4 - 1H Swords
  1111. case W_1HAXE: //All level 4 - 1H Axes
  1112. case W_MACE: //All level 4 - 1H Maces
  1113. case W_STAFF: //All level 4 - 1H Staves
  1114. case W_2HSTAFF: //All level 4 - 2H Staves
  1115. return ITEM_EQUIP_ACK_OK;
  1116. }
  1117. }
  1118. }
  1119. //Not equipable by class. [Skotlex]
  1120. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  1121. return ITEM_EQUIP_ACK_FAIL;
  1122. if (!pc_isItemClass(sd, item))
  1123. return ITEM_EQUIP_ACK_FAIL;
  1124. return ITEM_EQUIP_ACK_OK;
  1125. }
  1126. /*==========================================
  1127. * No problem with the session id
  1128. * set the status that has been sent from char server
  1129. *------------------------------------------*/
  1130. 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)
  1131. {
  1132. int i;
  1133. t_tick tick = gettick();
  1134. uint32 ip = session[sd->fd]->client_addr;
  1135. sd->login_id2 = login_id2;
  1136. sd->group_id = group_id;
  1137. /* load user permissions */
  1138. pc_group_pc_load(sd);
  1139. memcpy(&sd->status, st, sizeof(*st));
  1140. if (st->sex != sd->status.sex) {
  1141. clif_authfail_fd(sd->fd, 0);
  1142. return false;
  1143. }
  1144. //Set the map-server used job id. [Skotlex]
  1145. i = pc_jobid2mapid(sd->status.class_);
  1146. if (i == -1) { //Invalid class?
  1147. 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);
  1148. sd->status.class_ = JOB_NOVICE;
  1149. sd->class_ = MAPID_NOVICE;
  1150. } else
  1151. sd->class_ = i;
  1152. // Checks and fixes to character status data, that are required
  1153. // in case of configuration change or stuff, which cannot be
  1154. // checked on char-server.
  1155. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1156. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1157. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1158. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1159. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1160. sd->state.connect_new = 1;
  1161. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1162. sd->invincible_timer = INVALID_TIMER;
  1163. sd->npc_timer_id = INVALID_TIMER;
  1164. sd->pvp_timer = INVALID_TIMER;
  1165. sd->expiration_tid = INVALID_TIMER;
  1166. sd->autotrade_tid = INVALID_TIMER;
  1167. sd->respawn_tid = INVALID_TIMER;
  1168. #ifdef SECURE_NPCTIMEOUT
  1169. // Initialize to defaults/expected
  1170. sd->npc_idle_timer = INVALID_TIMER;
  1171. sd->npc_idle_tick = tick;
  1172. sd->npc_idle_type = NPCT_INPUT;
  1173. sd->state.ignoretimeout = false;
  1174. #endif
  1175. sd->canuseitem_tick = tick;
  1176. sd->canusecashfood_tick = tick;
  1177. sd->canequip_tick = tick;
  1178. sd->cantalk_tick = tick;
  1179. sd->canskill_tick = tick;
  1180. sd->cansendmail_tick = tick;
  1181. sd->idletime = last_tick;
  1182. for(i = 0; i < MAX_SPIRITBALL; i++)
  1183. sd->spirit_timer[i] = INVALID_TIMER;
  1184. if (battle_config.item_auto_get)
  1185. sd->state.autoloot = 10000;
  1186. if (battle_config.disp_experience)
  1187. sd->state.showexp = 1;
  1188. if (battle_config.disp_zeny)
  1189. sd->state.showzeny = 1;
  1190. #ifdef VIP_ENABLE
  1191. if (!battle_config.vip_disp_rate)
  1192. sd->vip.disableshowrate = 1;
  1193. #endif
  1194. if (!(battle_config.display_skill_fail&2))
  1195. sd->state.showdelay = 1;
  1196. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1197. memset(&sd->cart, 0, sizeof(struct s_storage));
  1198. memset(&sd->storage, 0, sizeof(struct s_storage));
  1199. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1200. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1201. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1202. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1203. sd->status.option &= ~OPTION_INVISIBLE;
  1204. }
  1205. status_change_init(&sd->bl);
  1206. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1207. unit_dataset(&sd->bl);
  1208. sd->guild_x = -1;
  1209. sd->guild_y = -1;
  1210. sd->delayed_damage = 0;
  1211. // Event Timers
  1212. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1213. sd->eventtimer[i] = INVALID_TIMER;
  1214. // Rental Timer
  1215. sd->rental_timer = INVALID_TIMER;
  1216. for( i = 0; i < 3; i++ )
  1217. sd->hate_mob[i] = -1;
  1218. sd->quest_log = NULL;
  1219. sd->num_quests = 0;
  1220. sd->avail_quests = 0;
  1221. sd->save_quest = false;
  1222. sd->count_rewarp = 0;
  1223. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1224. sd->regs.arrays = NULL;
  1225. sd->vars_dirty = false;
  1226. sd->vars_ok = false;
  1227. sd->vars_received = 0x0;
  1228. sd->qi_display = nullptr;
  1229. sd->qi_count = 0;
  1230. //warp player
  1231. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1232. 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);
  1233. // try warping to a default map instead (church graveyard)
  1234. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1235. // if we fail again
  1236. clif_authfail_fd(sd->fd, 0);
  1237. return false;
  1238. }
  1239. }
  1240. clif_authok(sd);
  1241. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1242. sd->die_counter=-1;
  1243. //display login notice
  1244. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1245. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1246. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1247. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1248. sd->status.name, sd->status.account_id, sd->status.char_id,
  1249. CONVIP(ip), sd->group_id);
  1250. // Send friends list
  1251. clif_friendslist_send(sd);
  1252. if( !changing_mapservers ) {
  1253. if (battle_config.display_version == 1)
  1254. pc_show_version(sd);
  1255. // Message of the Day [Valaris]
  1256. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1257. if (battle_config.motd_type)
  1258. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1259. else
  1260. clif_displaymessage(sd->fd, motd_text[i]);
  1261. }
  1262. if (expiration_time != 0)
  1263. sd->expiration_time = expiration_time;
  1264. /**
  1265. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1266. **/
  1267. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1268. }
  1269. pc_validate_skill(sd);
  1270. /* [Ind] */
  1271. sd->sc_display = NULL;
  1272. sd->sc_display_count = 0;
  1273. // Player has not yet received the CashShop list
  1274. sd->status.cashshop_sent = false;
  1275. sd->last_addeditem_index = -1;
  1276. sd->bonus_script.head = NULL;
  1277. sd->bonus_script.count = 0;
  1278. // Initialize BG queue pointer
  1279. sd->bg_queue = nullptr;
  1280. sd->bg_queue_accept_state = false;
  1281. #if PACKETVER >= 20150513
  1282. sd->hatEffectIDs = NULL;
  1283. sd->hatEffectCount = 0;
  1284. #endif
  1285. sd->catch_target_class = PET_CATCH_FAIL;
  1286. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1287. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1288. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1289. clif_updatestatus(sd, SP_BASEEXP);
  1290. }
  1291. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1292. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1293. clif_updatestatus(sd, SP_JOBEXP);
  1294. }
  1295. // Request all registries (auth is considered completed whence they arrive)
  1296. intif_request_registry(sd,7);
  1297. return true;
  1298. }
  1299. /*==========================================
  1300. * Closes a connection because it failed to be authenticated from the char server.
  1301. *------------------------------------------*/
  1302. void pc_authfail(struct map_session_data *sd)
  1303. {
  1304. clif_authfail_fd(sd->fd, 0);
  1305. return;
  1306. }
  1307. /**
  1308. * Player register a bl as hatred
  1309. * @param sd : player session
  1310. * @param pos : hate position [0;2]
  1311. * @param bl : target bl
  1312. * @return false:failed, true:success
  1313. */
  1314. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1315. {
  1316. int class_;
  1317. if (!sd || !bl || pos < 0 || pos > 2)
  1318. return false;
  1319. if (sd->hate_mob[pos] != -1)
  1320. { //Can't change hate targets.
  1321. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1322. return false;
  1323. }
  1324. class_ = status_get_class(bl);
  1325. if (!pcdb_checkid(class_)) {
  1326. unsigned int max_hp = status_get_max_hp(bl);
  1327. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1328. return false;
  1329. if (pos != status_get_size(bl))
  1330. return false; //Wrong size
  1331. }
  1332. sd->hate_mob[pos] = class_;
  1333. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1334. clif_hate_info(sd, pos, class_, 1);
  1335. return true;
  1336. }
  1337. /*==========================================
  1338. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1339. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1340. * See intif_parse_StorageReceived() for item operations [lighta]
  1341. *------------------------------------------*/
  1342. void pc_reg_received(struct map_session_data *sd)
  1343. {
  1344. uint8 i;
  1345. sd->vars_ok = true;
  1346. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1347. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1348. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1349. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1350. if (msg_checklangtype(sd->langtype,true) < 0)
  1351. sd->langtype = 0; //invalid langtype reset to default
  1352. // Cash shop
  1353. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1354. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1355. // Cooking Exp
  1356. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1357. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1358. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1359. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1360. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1361. }
  1362. if (battle_config.feature_banking)
  1363. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1364. if (battle_config.feature_roulette) {
  1365. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1366. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1367. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1368. }
  1369. sd->roulette.prizeIdx = -1;
  1370. //SG map and mob read [Komurka]
  1371. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1372. uint16 j;
  1373. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1374. sd->feel_map[i].index = j;
  1375. sd->feel_map[i].m = map_mapindex2mapid(j);
  1376. } else {
  1377. sd->feel_map[i].index = 0;
  1378. sd->feel_map[i].m = -1;
  1379. }
  1380. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1381. }
  1382. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1383. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1384. sd->cloneskill_idx = skill_get_index(skid);
  1385. if (sd->cloneskill_idx > 0) {
  1386. sd->status.skill[sd->cloneskill_idx].id = skid;
  1387. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1388. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1389. sd->status.skill[sd->cloneskill_idx].lv = i;
  1390. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1391. }
  1392. }
  1393. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1394. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1395. sd->reproduceskill_idx = skill_get_index(skid);
  1396. if (sd->reproduceskill_idx > 0) {
  1397. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1398. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1399. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1400. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1401. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1402. }
  1403. }
  1404. //Weird... maybe registries were reloaded?
  1405. if (sd->state.active)
  1406. return;
  1407. sd->state.active = 1;
  1408. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1409. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1410. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1411. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1412. if (sd->status.party_id)
  1413. party_member_joined(sd);
  1414. if (sd->status.guild_id)
  1415. guild_member_joined(sd);
  1416. if( sd->status.clan_id )
  1417. clan_member_joined(sd);
  1418. // pet
  1419. if (sd->status.pet_id > 0)
  1420. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1421. // Homunculus [albator]
  1422. if( sd->status.hom_id > 0 )
  1423. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1424. if( sd->status.mer_id > 0 )
  1425. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1426. if( sd->status.ele_id > 0 )
  1427. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1428. map_addiddb(&sd->bl);
  1429. map_delnickdb(sd->status.char_id, sd->status.name);
  1430. if (!chrif_auth_finished(sd))
  1431. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1432. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1433. chrif_bsdata_request(sd->status.char_id);
  1434. #ifdef VIP_ENABLE
  1435. sd->vip.time = 0;
  1436. sd->vip.enabled = 0;
  1437. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1438. #endif
  1439. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1440. intif_request_questlog(sd);
  1441. if (battle_config.feature_achievement) {
  1442. sd->achievement_data.total_score = 0;
  1443. sd->achievement_data.level = 0;
  1444. sd->achievement_data.save = false;
  1445. sd->achievement_data.count = 0;
  1446. sd->achievement_data.incompleteCount = 0;
  1447. sd->achievement_data.achievements = NULL;
  1448. intif_request_achievements(sd->status.char_id);
  1449. }
  1450. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1451. sd->state.connect_new = 1;
  1452. clif_parse_LoadEndAck(sd->fd, sd);
  1453. if( pc_isinvisible( sd ) ){
  1454. sd->vd.class_ = JT_INVISIBLE;
  1455. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1456. // decrement the number of pvp players on the map
  1457. map_getmapdata( sd->bl.m )->users_pvp--;
  1458. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1459. // unregister the player for ranking
  1460. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1461. sd->pvp_timer = INVALID_TIMER;
  1462. }
  1463. clif_changeoption( &sd->bl );
  1464. }
  1465. }
  1466. channel_autojoin(sd);
  1467. }
  1468. static int pc_calc_skillpoint(struct map_session_data* sd)
  1469. {
  1470. uint16 i, skill_point = 0;
  1471. nullpo_ret(sd);
  1472. for(i = 1; i < MAX_SKILL; i++) {
  1473. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1474. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1475. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1476. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1477. )
  1478. {
  1479. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1480. skill_point += sd->status.skill[i].lv;
  1481. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1482. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1483. }
  1484. }
  1485. }
  1486. return skill_point;
  1487. }
  1488. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1489. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1490. return false;
  1491. /**
  1492. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1493. * skill tree and since they have no icons they'll give resource errors
  1494. * Get ALL skills except npc/guild ones. [Skotlex]
  1495. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1496. **/
  1497. for (const auto &skill : skill_db) {
  1498. uint16 skill_id = skill.second->nameid;
  1499. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1500. continue;
  1501. switch (skill_id) {
  1502. case SM_SELFPROVOKE:
  1503. case AB_DUPLELIGHT_MELEE:
  1504. case AB_DUPLELIGHT_MAGIC:
  1505. case WL_CHAINLIGHTNING_ATK:
  1506. case WL_TETRAVORTEX_FIRE:
  1507. case WL_TETRAVORTEX_WATER:
  1508. case WL_TETRAVORTEX_WIND:
  1509. case WL_TETRAVORTEX_GROUND:
  1510. case WL_SUMMON_ATK_FIRE:
  1511. case WL_SUMMON_ATK_WIND:
  1512. case WL_SUMMON_ATK_WATER:
  1513. case WL_SUMMON_ATK_GROUND:
  1514. case LG_OVERBRAND_BRANDISH:
  1515. case LG_OVERBRAND_PLUSATK:
  1516. case WM_SEVERE_RAINSTORM_MELEE:
  1517. case RL_R_TRIP_PLUSATK:
  1518. case SG_DEVIL:
  1519. case MO_TRIPLEATTACK:
  1520. case RG_SNATCHER:
  1521. continue;
  1522. default:
  1523. {
  1524. uint8 lv = (uint8)skill_get_max(skill_id);
  1525. if (lv > 0) {
  1526. uint16 idx = skill_get_index(skill_id);
  1527. sd->status.skill[idx].id = skill_id;
  1528. if (addlv)
  1529. sd->status.skill[idx].lv = lv;
  1530. }
  1531. }
  1532. break;
  1533. }
  1534. }
  1535. return true;
  1536. }
  1537. /*==========================================
  1538. * Calculation of skill level.
  1539. * @param sd
  1540. *------------------------------------------*/
  1541. void pc_calc_skilltree(struct map_session_data *sd)
  1542. {
  1543. nullpo_retv(sd);
  1544. int job = pc_calc_skilltree_normalize_job(sd);
  1545. int class_ = pc_mapid2jobid(job, sd->status.sex);
  1546. if( class_ == -1 )
  1547. { //Unable to normalize job??
  1548. 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);
  1549. return;
  1550. }
  1551. class_ = pc_class2idx(class_);
  1552. for (const auto &skill : skill_db) {
  1553. uint16 skill_id = skill.second->nameid;
  1554. uint16 idx = skill_get_index(skill_id);
  1555. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1556. sd->status.skill[idx].id = 0; //First clear skills.
  1557. /* permanent skills that must be re-checked */
  1558. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  1559. if (skill_id == 0) {
  1560. sd->status.skill[idx].id = 0;
  1561. sd->status.skill[idx].lv = 0;
  1562. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1563. continue;
  1564. }
  1565. switch (skill_id) {
  1566. case NV_TRICKDEAD:
  1567. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1568. sd->status.skill[idx].id = 0;
  1569. sd->status.skill[idx].lv = 0;
  1570. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1571. }
  1572. break;
  1573. }
  1574. }
  1575. }
  1576. for (const auto &skill : skill_db) {
  1577. uint16 skill_id = skill.second->nameid;
  1578. uint16 idx = skill_get_index(skill_id);
  1579. // Restore original level of skills after deleting earned skills.
  1580. 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 ) {
  1581. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1582. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1583. }
  1584. //Enable Bard/Dancer spirit linked skills.
  1585. if (skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1586. continue;
  1587. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1588. //Link Dancer skills to bard.
  1589. if( sd->status.sex ) {
  1590. if( sd->status.skill[idx - 8].lv < 10 )
  1591. continue;
  1592. sd->status.skill[idx].id = skill_id;
  1593. sd->status.skill[idx].lv = sd->status.skill[idx - 8].lv; // Set the level to the same as the linking skill
  1594. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1595. }
  1596. //Link Bard skills to dancer.
  1597. else {
  1598. if( sd->status.skill[idx].lv < 10 )
  1599. continue;
  1600. sd->status.skill[idx - 8].id = skill_id - 8;
  1601. sd->status.skill[idx - 8].lv = sd->status.skill[idx].lv; // Set the level to the same as the linking skill
  1602. sd->status.skill[idx - 8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1603. }
  1604. }
  1605. }
  1606. // Removes Taekwon Ranker skill bonus
  1607. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1608. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1609. for (uint16 i = 0; i < MAX_SKILL_TREE; i++) {
  1610. uint16 sk_id = skill_tree[c_][i].skill_id;
  1611. uint16 sk_idx = 0;
  1612. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1613. continue;
  1614. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1615. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1616. continue;
  1617. sd->status.skill[sk_idx].id = 0;
  1618. }
  1619. }
  1620. }
  1621. // Grant all skills
  1622. pc_grant_allskills(sd, false);
  1623. int flag;
  1624. do {
  1625. uint16 skid = 0;
  1626. flag = 0;
  1627. for (uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++) {
  1628. bool fail = false;
  1629. uint16 sk_idx = skill_get_index(skid);
  1630. if (sd->status.skill[sk_idx].id)
  1631. continue; //Skill already known.
  1632. if (!battle_config.skillfree) {
  1633. // Checking required skills
  1634. for(uint8 j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1635. uint16 sk_need_id = skill_tree[class_][i].need[j].skill_id;
  1636. uint16 sk_need_idx = 0;
  1637. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1638. short sk_need = sk_need_id;
  1639. 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)
  1640. sk_need = 0; //Not learned.
  1641. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1642. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1643. else
  1644. sk_need = pc_checkskill(sd,sk_need_id);
  1645. if (sk_need < skill_tree[class_][i].need[j].skill_lv) {
  1646. fail = true;
  1647. break;
  1648. }
  1649. }
  1650. }
  1651. if (sd->status.base_level < skill_tree[class_][i].baselv) { //We need to get the actual class in this case
  1652. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1653. c_ = pc_class2idx(c_);
  1654. if (class_ == c_ || (class_ != c_ && sd->status.base_level < skill_tree[class_][i].baselv))
  1655. fail = true; // base level requirement wasn't satisfied
  1656. }
  1657. if (sd->status.job_level < skill_tree[class_][i].joblv) { //We need to get the actual class in this case
  1658. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1659. c_ = pc_class2idx(c_);
  1660. if (class_ == c_ || (class_ != c_ && sd->status.job_level < skill_tree[class_][i].joblv))
  1661. fail = true; // job level requirement wasn't satisfied
  1662. }
  1663. }
  1664. if (!fail) {
  1665. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1666. if (!sd->status.skill[sk_idx].lv && (
  1667. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1668. skill->inf2[INF2_ISWEDDING] ||
  1669. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1670. ))
  1671. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1672. sd->status.skill[sk_idx].id = skid;
  1673. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1674. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1675. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1676. }
  1677. flag = 1; // skill list has changed, perform another pass
  1678. }
  1679. }
  1680. } while(flag);
  1681. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1682. uint16 skid = 0;
  1683. /* Taekwon Ranker Bonus Skill Tree
  1684. ============================================
  1685. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1686. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1687. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1688. for( uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++ ) {
  1689. uint16 sk_idx = skill_get_index(skid);
  1690. if (sk_idx == 0)
  1691. continue;
  1692. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  1693. continue; //Do not include Quest/Wedding skills.
  1694. if( sd->status.skill[sk_idx].id == 0 ) {
  1695. sd->status.skill[sk_idx].id = skid;
  1696. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1697. } else if( skid != NV_BASIC )
  1698. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1699. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1700. }
  1701. }
  1702. }
  1703. //Checks if you can learn a new skill after having leveled up a skill.
  1704. static void pc_check_skilltree(struct map_session_data *sd)
  1705. {
  1706. int i, flag = 0;
  1707. int c = 0;
  1708. if (battle_config.skillfree)
  1709. return; //Function serves no purpose if this is set
  1710. i = pc_calc_skilltree_normalize_job(sd);
  1711. c = pc_mapid2jobid(i, sd->status.sex);
  1712. if (c == -1) { //Unable to normalize job??
  1713. 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);
  1714. return;
  1715. }
  1716. c = pc_class2idx(c);
  1717. do {
  1718. uint16 skid = 0;
  1719. flag = 0;
  1720. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1721. uint16 sk_idx = skill_get_index(skid);
  1722. bool fail = false;
  1723. uint8 j = 0;
  1724. if (sd->status.skill[sk_idx].id) //Already learned
  1725. continue;
  1726. // Checking required skills
  1727. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1728. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1729. uint16 sk_need_idx = 0;
  1730. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1731. short sk_need = sk_need_id;
  1732. 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)
  1733. sk_need = 0; //Not learned.
  1734. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1735. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1736. else
  1737. sk_need = pc_checkskill(sd,sk_need_id);
  1738. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1739. fail = true;
  1740. break;
  1741. }
  1742. }
  1743. }
  1744. if( fail )
  1745. continue;
  1746. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1747. continue;
  1748. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1749. if( !sd->status.skill[sk_idx].lv && (
  1750. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1751. skill->inf2[INF2_ISWEDDING] ||
  1752. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1753. ) )
  1754. continue; //Cannot be learned via normal means.
  1755. sd->status.skill[sk_idx].id = skid;
  1756. flag = 1;
  1757. }
  1758. } while(flag);
  1759. }
  1760. // Make sure all the skills are in the correct condition
  1761. // before persisting to the backend.. [MouseJstr]
  1762. void pc_clean_skilltree(struct map_session_data *sd)
  1763. {
  1764. uint16 i;
  1765. for (i = 0; i < MAX_SKILL; i++){
  1766. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1767. sd->status.skill[i].id = 0;
  1768. sd->status.skill[i].lv = 0;
  1769. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1770. }
  1771. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1772. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1773. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1774. }
  1775. }
  1776. }
  1777. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1778. {
  1779. int skill_point, novice_skills;
  1780. int c = sd->class_;
  1781. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1782. return c;
  1783. skill_point = pc_calc_skillpoint(sd);
  1784. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1785. // limit 1st class and above to novice job levels
  1786. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1787. {
  1788. c = MAPID_NOVICE;
  1789. }
  1790. // limit 2nd class and above to first class job levels (super novices are exempt)
  1791. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1792. {
  1793. // regenerate change_level_2nd
  1794. if (!sd->change_level_2nd)
  1795. {
  1796. if (sd->class_&JOBL_THIRD)
  1797. {
  1798. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1799. if (!sd->change_level_3rd)
  1800. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1801. else
  1802. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1803. - (sd->status.job_level - 1)
  1804. - (sd->change_level_3rd - 1)
  1805. - novice_skills;
  1806. }
  1807. else
  1808. {
  1809. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1810. - (sd->status.job_level - 1)
  1811. - novice_skills;
  1812. }
  1813. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1814. }
  1815. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1816. {
  1817. c &= MAPID_BASEMASK;
  1818. }
  1819. // limit 3rd class to 2nd class/trans job levels
  1820. else if(sd->class_&JOBL_THIRD)
  1821. {
  1822. // regenerate change_level_3rd
  1823. if (!sd->change_level_3rd)
  1824. {
  1825. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1826. - (sd->status.job_level - 1)
  1827. - (sd->change_level_2nd - 1)
  1828. - novice_skills;
  1829. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1830. }
  1831. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1832. c &= MAPID_UPPERMASK;
  1833. }
  1834. }
  1835. // restore non-limiting flags
  1836. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1837. return c;
  1838. }
  1839. /*==========================================
  1840. * Updates the weight status
  1841. *------------------------------------------
  1842. * 1: overweight 50% for pre-renewal and 70% for renewal
  1843. * 2: overweight 90%
  1844. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1845. */
  1846. void pc_updateweightstatus(struct map_session_data *sd)
  1847. {
  1848. int old_overweight;
  1849. int new_overweight;
  1850. nullpo_retv(sd);
  1851. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1852. #ifdef RENEWAL
  1853. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1854. #else
  1855. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1856. #endif
  1857. if( old_overweight == new_overweight )
  1858. return; // no change
  1859. // stop old status change
  1860. if( old_overweight == 1 )
  1861. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1862. else if( old_overweight == 2 )
  1863. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1864. // start new status change
  1865. if( new_overweight == 1 )
  1866. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1867. else if( new_overweight == 2 )
  1868. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1869. // update overweight status
  1870. sd->regen.state.overweight = new_overweight;
  1871. }
  1872. int pc_disguise(struct map_session_data *sd, int class_)
  1873. {
  1874. if (!class_ && !sd->disguise)
  1875. return 0;
  1876. if (class_ && sd->disguise == class_)
  1877. return 0;
  1878. if(pc_isinvisible(sd))
  1879. { //Character is invisible. Stealth class-change. [Skotlex]
  1880. sd->disguise = class_; //viewdata is set on uncloaking.
  1881. return 2;
  1882. }
  1883. if (sd->bl.prev != NULL) {
  1884. pc_stop_walking(sd, 0);
  1885. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1886. }
  1887. if (!class_) {
  1888. sd->disguise = 0;
  1889. class_ = sd->status.class_;
  1890. } else
  1891. sd->disguise=class_;
  1892. status_set_viewdata(&sd->bl, class_);
  1893. clif_changeoption(&sd->bl);
  1894. if (sd->bl.prev != NULL) {
  1895. clif_spawn(&sd->bl);
  1896. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1897. { //It seems the cart info is lost on undisguise.
  1898. clif_cartlist(sd);
  1899. clif_updatestatus(sd,SP_CARTINFO);
  1900. }
  1901. if (sd->chatID) {
  1902. struct chat_data* cd;
  1903. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1904. clif_dispchat(cd,0);
  1905. }
  1906. }
  1907. return 1;
  1908. }
  1909. /// Show error message
  1910. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1911. /// Check for valid Element, break & show error message if invalid Element
  1912. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1913. /// Check for valid Race, break & show error message if invalid Race
  1914. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1915. /// Check for valid Race2, break & show error message if invalid Race2
  1916. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1917. /// Check for valid Class, break & show error message if invalid Class
  1918. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1919. /// Check for valid Size, break & show error message if invalid Size
  1920. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1921. /// Check for valid SC, break & show error message if invalid SC
  1922. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1923. /**
  1924. * Add auto spell bonus for player while attacking/attacked
  1925. * @param spell: Spell array
  1926. * @param id: Skill to cast
  1927. * @param lv: Skill level
  1928. * @param rate: Success chance
  1929. * @param flag: Battle flag
  1930. * @param card_id: Used to prevent card stacking
  1931. */
  1932. static void pc_bonus_autospell(std::vector<s_autospell> &spell, short id, short lv, short rate, short flag, unsigned short card_id)
  1933. {
  1934. if (spell.size() == MAX_PC_BONUS) {
  1935. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  1936. return;
  1937. }
  1938. if (!rate)
  1939. return;
  1940. if (!(flag&BF_RANGEMASK))
  1941. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  1942. if (!(flag&BF_WEAPONMASK))
  1943. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  1944. if (!(flag&BF_SKILLMASK)) {
  1945. if (flag&(BF_MAGIC | BF_MISC))
  1946. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  1947. if (flag&BF_WEAPON)
  1948. flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1949. }
  1950. for (auto &it : spell) {
  1951. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.flag == flag) {
  1952. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  1953. return;
  1954. it.rate = cap_value(it.rate + rate, -10000, 10000);
  1955. return;
  1956. }
  1957. }
  1958. struct s_autospell entry = {};
  1959. if (rate < -10000 || rate > 10000)
  1960. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  1961. entry.id = id;
  1962. entry.lv = lv;
  1963. entry.rate = cap_value(rate, -10000, 10000);
  1964. entry.flag = flag;
  1965. entry.card_id = card_id;
  1966. spell.push_back(entry);
  1967. }
  1968. /**
  1969. * Add auto spell bonus for player while using skills
  1970. * @param spell: Spell array
  1971. * @param src_skill: Trigger skill
  1972. * @param id: Support or target type
  1973. * @param lv: Skill level
  1974. * @param rate: Success chance
  1975. * @param card_id: Used to prevent card stacking
  1976. */
  1977. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, short src_skill, short id, short lv, short rate, unsigned short card_id)
  1978. {
  1979. if (spell.size() == MAX_PC_BONUS) {
  1980. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  1981. return;
  1982. }
  1983. if (!rate)
  1984. return;
  1985. struct s_autospell entry = {};
  1986. if (rate < -10000 || rate > 10000)
  1987. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  1988. entry.flag = src_skill;
  1989. entry.id = id;
  1990. entry.lv = lv;
  1991. entry.rate = cap_value(rate, -10000, 10000);
  1992. entry.card_id = card_id;
  1993. spell.push_back(entry);
  1994. }
  1995. /**
  1996. * Add inflict effect bonus for player while attacking/attacked
  1997. * @param effect: Effect array
  1998. * @param sc: SC/Effect type
  1999. * @param rate: Success chance
  2000. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2001. * @param flag: Target flag
  2002. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2003. */
  2004. 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)
  2005. {
  2006. if (effect.size() == MAX_PC_BONUS) {
  2007. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2008. return;
  2009. }
  2010. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2011. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2012. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2013. flag |= ATF_TARGET; //Default target: enemy.
  2014. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2015. flag |= ATF_WEAPON; //Default type: weapon.
  2016. if (!duration)
  2017. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2018. for (auto &it : effect) {
  2019. if (it.sc == sc && it.flag == flag) {
  2020. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2021. it.arrow_rate += arrow_rate;
  2022. it.duration = umax(it.duration, duration);
  2023. return;
  2024. }
  2025. }
  2026. struct s_addeffect entry = {};
  2027. if (rate < -10000 || rate > 10000)
  2028. ShowWarning("pc_bonus_addeff: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2029. entry.sc = sc;
  2030. entry.rate = cap_value(rate, -10000, 10000);
  2031. entry.arrow_rate = arrow_rate;
  2032. entry.flag = flag;
  2033. entry.duration = duration;
  2034. effect.push_back(entry);
  2035. }
  2036. /**
  2037. * Add inflict effect bonus for player while attacking using skill
  2038. * @param effect: Effect array
  2039. * @param sc: SC/Effect type
  2040. * @param rate: Success chance
  2041. * @param skill_id: Skill to cast
  2042. * @param target: Target type
  2043. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2044. */
  2045. 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)
  2046. {
  2047. if (effect.size() == MAX_PC_BONUS) {
  2048. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2049. return;
  2050. }
  2051. if (!duration)
  2052. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2053. for (auto &it : effect) {
  2054. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2055. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2056. it.duration = umax(it.duration, duration);
  2057. return;
  2058. }
  2059. }
  2060. struct s_addeffectonskill entry = {};
  2061. if (rate < -10000 || rate > 10000)
  2062. ShowWarning("pc_bonus_addeff_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2063. entry.sc = sc;
  2064. entry.rate = cap_value(rate, -10000, 10000);
  2065. entry.skill_id = skill_id;
  2066. entry.target = target;
  2067. entry.duration = duration;
  2068. effect.push_back(entry);
  2069. }
  2070. /**
  2071. * Adjust/add drop rate modifier for player
  2072. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2073. * @param nameid: item id that will be dropped
  2074. * @param group: group id
  2075. * @param class_: target class
  2076. * @param race: target race. if < 0, means monster_id
  2077. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2078. */
  2079. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, unsigned short nameid, uint16 group, int class_, short race, int rate)
  2080. {
  2081. if (!nameid && !group) {
  2082. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2083. return;
  2084. }
  2085. if (nameid && !itemdb_exists(nameid)) {
  2086. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  2087. return;
  2088. }
  2089. if (group && !itemdb_group_exists(group)) {
  2090. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2091. return;
  2092. }
  2093. if (drop.size() == MAX_PC_BONUS) {
  2094. ShowWarning("pc_bonus_item_drop: Reached max (%d) number of added drops per character! (nameid: %hu group: %d class_: %d race: %d rate: %d)\n", MAX_PC_BONUS, nameid, group, class_, race, rate);
  2095. return;
  2096. }
  2097. //Apply config rate adjustment settings.
  2098. if (rate >= 0) { //Absolute drop.
  2099. if (battle_config.item_rate_adddrop != 100)
  2100. rate = rate*battle_config.item_rate_adddrop/100;
  2101. if (rate < battle_config.item_drop_adddrop_min)
  2102. rate = battle_config.item_drop_adddrop_min;
  2103. else if (rate > battle_config.item_drop_adddrop_max)
  2104. rate = battle_config.item_drop_adddrop_max;
  2105. } else { //Relative drop, max/min limits are applied at drop time.
  2106. if (battle_config.item_rate_adddrop != 100)
  2107. rate = rate*battle_config.item_rate_adddrop/100;
  2108. if (rate > -1)
  2109. rate = -1;
  2110. }
  2111. for (auto &it : drop) {
  2112. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2113. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2114. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2115. return;
  2116. }
  2117. }
  2118. struct s_add_drop entry = {};
  2119. if (rate < -10000 || rate > 10000)
  2120. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2121. entry.nameid = nameid;
  2122. entry.group = group;
  2123. entry.race = race;
  2124. entry.class_ = class_;
  2125. entry.rate = cap_value(rate, -10000, 10000);
  2126. drop.push_back(entry);
  2127. }
  2128. /**
  2129. * Add autobonus to player when attacking/attacked
  2130. * @param bonus: Bonus array
  2131. * @param script: Script to execute
  2132. * @param rate: Success chance
  2133. * @param dur: Duration
  2134. * @param flag: Battle flag/skill
  2135. * @param other_script: Secondary script to execute
  2136. * @param pos: Item equip position
  2137. * @param onskill: Skill used to trigger autobonus
  2138. * @return True on success or false otherwise
  2139. */
  2140. 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)
  2141. {
  2142. if (bonus.size() == MAX_PC_BONUS) {
  2143. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2144. return false;
  2145. }
  2146. if (!onskill) {
  2147. if (!(flag&BF_RANGEMASK))
  2148. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2149. if (!(flag&BF_WEAPONMASK))
  2150. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2151. if (!(flag&BF_SKILLMASK)) {
  2152. if (flag&(BF_MAGIC | BF_MISC))
  2153. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2154. if (flag&BF_WEAPON)
  2155. flag |= BF_NORMAL | BF_SKILL;
  2156. }
  2157. }
  2158. struct s_autobonus entry = {};
  2159. if (rate < -10000 || rate > 10000)
  2160. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2161. entry.rate = cap_value(rate, -10000, 10000);
  2162. entry.duration = dur;
  2163. entry.active = INVALID_TIMER;
  2164. entry.atk_type = flag;
  2165. entry.pos = pos;
  2166. entry.bonus_script = aStrdup(script);
  2167. entry.other_script = (other_script ? aStrdup(other_script) : NULL);
  2168. bonus.push_back(entry);
  2169. return true;
  2170. }
  2171. /**
  2172. * Remove an autobonus from player
  2173. * @param sd: Player data
  2174. * @param bonus: Autobonus array
  2175. * @param restore: Run script on clearing or not
  2176. */
  2177. void pc_delautobonus(struct map_session_data* sd, std::vector<s_autobonus> &bonus, bool restore)
  2178. {
  2179. nullpo_retv(sd);
  2180. std::vector<s_autobonus>::iterator it = bonus.begin();
  2181. while( it != bonus.end() ){
  2182. s_autobonus b = *it;
  2183. if (b.active != INVALID_TIMER) {
  2184. if (restore && (sd->state.autobonus&b.pos) == b.pos) {
  2185. if (b.bonus_script) {
  2186. unsigned int equip_pos_idx = 0;
  2187. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2188. for (uint8 j = 0; j < EQI_MAX; j++) {
  2189. if (sd->equip_index[j] >= 0)
  2190. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2191. }
  2192. if ((equip_pos_idx&b.pos) == b.pos)
  2193. script_run_autobonus(b.bonus_script, sd, b.pos);
  2194. }
  2195. it++;
  2196. continue;
  2197. } else { // Logout / Unequipped an item with an activated bonus
  2198. delete_timer(b.active, pc_endautobonus);
  2199. b.active = INVALID_TIMER;
  2200. }
  2201. }
  2202. if (b.bonus_script)
  2203. aFree(b.bonus_script);
  2204. if (b.other_script)
  2205. aFree(b.other_script);
  2206. it = bonus.erase(it);
  2207. }
  2208. }
  2209. /**
  2210. * Execute autobonus on player
  2211. * @param sd: Player data
  2212. * @param autobonus: Autobonus to run
  2213. */
  2214. void pc_exeautobonus(struct map_session_data *sd, std::vector<s_autobonus> *bonus, struct s_autobonus *autobonus)
  2215. {
  2216. nullpo_retv(sd);
  2217. nullpo_retv(autobonus);
  2218. if (autobonus->active != INVALID_TIMER)
  2219. delete_timer(autobonus->active, pc_endautobonus);
  2220. if( autobonus->other_script )
  2221. {
  2222. int j;
  2223. unsigned int equip_pos_idx = 0;
  2224. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2225. for(j = 0; j < EQI_MAX; j++) {
  2226. if(sd->equip_index[j] >= 0)
  2227. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2228. }
  2229. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2230. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2231. }
  2232. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)bonus);
  2233. sd->state.autobonus |= autobonus->pos;
  2234. status_calc_pc(sd,SCO_NONE);
  2235. }
  2236. /**
  2237. * Remove autobonus timer from player
  2238. */
  2239. TIMER_FUNC(pc_endautobonus){
  2240. struct map_session_data *sd = map_id2sd(id);
  2241. std::vector<s_autobonus> *bonus = (std::vector<s_autobonus> *)data;
  2242. nullpo_ret(sd);
  2243. nullpo_ret(bonus);
  2244. for( struct s_autobonus& autobonus : *bonus ){
  2245. if( autobonus.active == tid ){
  2246. autobonus.active = INVALID_TIMER;
  2247. sd->state.autobonus &= ~autobonus.pos;
  2248. break;
  2249. }
  2250. }
  2251. status_calc_pc(sd,SCO_NONE);
  2252. return 0;
  2253. }
  2254. /**
  2255. * Add element bonus to player when attacking
  2256. * @param sd: Player data
  2257. * @param ele: Element to adjust
  2258. * @param rate: Success chance
  2259. * @param flag: Battle flag
  2260. */
  2261. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2262. {
  2263. nullpo_retv(sd);
  2264. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2265. if (wd->addele2.size() == MAX_PC_BONUS) {
  2266. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2267. return;
  2268. }
  2269. if (!(flag&BF_RANGEMASK))
  2270. flag |= BF_SHORT | BF_LONG;
  2271. if (!(flag&BF_WEAPONMASK))
  2272. flag |= BF_WEAPON;
  2273. if (!(flag&BF_SKILLMASK)) {
  2274. if (flag&(BF_MAGIC | BF_MISC))
  2275. flag |= BF_SKILL;
  2276. if (flag&BF_WEAPON)
  2277. flag |= BF_NORMAL | BF_SKILL;
  2278. }
  2279. for (auto &it : wd->addele2) {
  2280. if (it.ele == ele && it.flag == flag) {
  2281. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2282. return;
  2283. }
  2284. }
  2285. struct s_addele2 entry = {};
  2286. if (rate < -10000 || rate > 10000)
  2287. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2288. entry.ele = ele;
  2289. entry.rate = cap_value(rate, -10000, 10000);
  2290. entry.flag = flag;
  2291. wd->addele2.push_back(entry);
  2292. }
  2293. /**
  2294. * Reduce element bonus to player when attacking
  2295. * @param sd: Player data
  2296. * @param ele: Element to adjust
  2297. * @param rate: Success chance
  2298. * @param flag: Battle flag
  2299. */
  2300. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2301. {
  2302. nullpo_retv(sd);
  2303. if (sd->subele2.size() == MAX_PC_BONUS) {
  2304. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2305. return;
  2306. }
  2307. if (!(flag&BF_RANGEMASK))
  2308. flag |= BF_SHORT | BF_LONG;
  2309. if (!(flag&BF_WEAPONMASK))
  2310. flag |= BF_WEAPON;
  2311. if (!(flag&BF_SKILLMASK)) {
  2312. if (flag&(BF_MAGIC | BF_MISC))
  2313. flag |= BF_SKILL;
  2314. if (flag&BF_WEAPON)
  2315. flag |= BF_NORMAL | BF_SKILL;
  2316. }
  2317. for (auto &it : sd->subele2) {
  2318. if (it.ele == ele && it.flag == flag) {
  2319. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2320. return;
  2321. }
  2322. }
  2323. struct s_addele2 entry = {};
  2324. if (rate < -10000 || rate > 10000)
  2325. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2326. entry.ele = ele;
  2327. entry.rate = cap_value(rate, -10000, 10000);
  2328. entry.flag = flag;
  2329. sd->subele2.push_back(entry);
  2330. }
  2331. /**
  2332. * General item bonus for player
  2333. * @param bonus: Bonus array
  2334. * @param id: Key
  2335. * @param val: Value
  2336. * @param cap_rate: If Value is a rate value that needs to be capped
  2337. */
  2338. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2339. {
  2340. for (auto &it : bonus) {
  2341. if (it.id == id) {
  2342. if (cap_rate)
  2343. it.val = cap_value(it.val + val, -10000, 10000);
  2344. else
  2345. it.val += val;
  2346. return;
  2347. }
  2348. }
  2349. struct s_item_bonus entry = {};
  2350. if (cap_rate && (val < -10000 || val > 10000)) {
  2351. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2352. val = cap_value(val, -10000, 10000);
  2353. }
  2354. entry.id = id;
  2355. entry.val = val;
  2356. bonus.push_back(entry);
  2357. }
  2358. /**
  2359. * Remove HP/SP to player when attacking
  2360. * @param bonus: Bonus array
  2361. * @param rate: Success chance
  2362. * @param per: Percentage of HP/SP to vanish
  2363. * @param flag: Battle flag
  2364. */
  2365. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2366. if (bonus.size() == MAX_PC_BONUS) {
  2367. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2368. return;
  2369. }
  2370. if (!(flag&BF_RANGEMASK))
  2371. flag |= BF_SHORT | BF_LONG;
  2372. if (!(flag&BF_WEAPONMASK))
  2373. flag |= BF_WEAPON;
  2374. if (!(flag&BF_SKILLMASK)) {
  2375. if (flag&(BF_MAGIC | BF_MISC))
  2376. flag |= BF_SKILL;
  2377. if (flag&BF_WEAPON)
  2378. flag |= BF_NORMAL | BF_SKILL;
  2379. }
  2380. for (auto &it : bonus) {
  2381. if (it.flag == flag) {
  2382. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2383. it.per += per;
  2384. return;
  2385. }
  2386. }
  2387. struct s_vanish_bonus entry = {};
  2388. if (rate < -10000 || rate > 10000)
  2389. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2390. entry.rate = cap_value(rate, -10000, 10000);
  2391. entry.per = per;
  2392. entry.flag = flag;
  2393. bonus.push_back(entry);
  2394. }
  2395. /*==========================================
  2396. * Add a bonus(type) to player sd
  2397. * format: bonus bBonusName,val;
  2398. * @param sd
  2399. * @param type Bonus type used by bBonusName
  2400. * @param val Value that usually for rate or fixed value
  2401. *------------------------------------------*/
  2402. void pc_bonus(struct map_session_data *sd,int type,int val)
  2403. {
  2404. struct status_data *status;
  2405. int bonus;
  2406. nullpo_retv(sd);
  2407. status = &sd->base_status;
  2408. switch(type){
  2409. case SP_STR:
  2410. case SP_AGI:
  2411. case SP_VIT:
  2412. case SP_INT:
  2413. case SP_DEX:
  2414. case SP_LUK:
  2415. if(sd->state.lr_flag != 2)
  2416. sd->param_bonus[type-SP_STR]+=val;
  2417. break;
  2418. case SP_ATK1:
  2419. if(!sd->state.lr_flag) {
  2420. bonus = status->rhw.atk + val;
  2421. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2422. }
  2423. else if(sd->state.lr_flag == 1) {
  2424. bonus = status->lhw.atk + val;
  2425. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2426. }
  2427. break;
  2428. case SP_ATK2:
  2429. if(!sd->state.lr_flag) {
  2430. bonus = status->rhw.atk2 + val;
  2431. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2432. }
  2433. else if(sd->state.lr_flag == 1) {
  2434. bonus = status->lhw.atk2 + val;
  2435. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2436. }
  2437. break;
  2438. case SP_BASE_ATK:
  2439. if(sd->state.lr_flag != 2) {
  2440. #ifdef RENEWAL
  2441. bonus = sd->bonus.eatk + val;
  2442. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2443. #else
  2444. bonus = status->batk + val;
  2445. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2446. #endif
  2447. }
  2448. break;
  2449. case SP_DEF1:
  2450. if(sd->state.lr_flag != 2) {
  2451. bonus = status->def + val;
  2452. #ifdef RENEWAL
  2453. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2454. #else
  2455. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2456. #endif
  2457. }
  2458. break;
  2459. case SP_DEF2:
  2460. if(sd->state.lr_flag != 2) {
  2461. bonus = status->def2 + val;
  2462. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2463. }
  2464. break;
  2465. case SP_MDEF1:
  2466. if(sd->state.lr_flag != 2) {
  2467. bonus = status->mdef + val;
  2468. #ifdef RENEWAL
  2469. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2470. #else
  2471. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2472. #endif
  2473. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2474. sd->bonus.shieldmdef += bonus;
  2475. }
  2476. }
  2477. break;
  2478. case SP_MDEF2:
  2479. if(sd->state.lr_flag != 2) {
  2480. bonus = status->mdef2 + val;
  2481. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2482. }
  2483. break;
  2484. case SP_HIT:
  2485. if(sd->state.lr_flag != 2) {
  2486. bonus = status->hit + val;
  2487. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2488. } else
  2489. sd->bonus.arrow_hit+=val;
  2490. break;
  2491. case SP_FLEE1:
  2492. if(sd->state.lr_flag != 2) {
  2493. bonus = status->flee + val;
  2494. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2495. }
  2496. break;
  2497. case SP_FLEE2:
  2498. if(sd->state.lr_flag != 2) {
  2499. bonus = status->flee2 + val*10;
  2500. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2501. }
  2502. break;
  2503. case SP_CRITICAL:
  2504. if(sd->state.lr_flag != 2) {
  2505. bonus = status->cri + val*10;
  2506. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2507. } else
  2508. sd->bonus.arrow_cri += val*10;
  2509. break;
  2510. case SP_ATKELE:
  2511. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2512. switch (sd->state.lr_flag)
  2513. {
  2514. case 2:
  2515. switch (sd->status.weapon) {
  2516. case W_BOW:
  2517. case W_REVOLVER:
  2518. case W_RIFLE:
  2519. case W_GATLING:
  2520. case W_SHOTGUN:
  2521. case W_GRENADE:
  2522. //Become weapon element.
  2523. status->rhw.ele=val;
  2524. break;
  2525. default: //Become arrow element.
  2526. sd->bonus.arrow_ele=val;
  2527. break;
  2528. }
  2529. break;
  2530. case 1:
  2531. status->lhw.ele=val;
  2532. break;
  2533. default:
  2534. status->rhw.ele=val;
  2535. break;
  2536. }
  2537. break;
  2538. case SP_DEFELE:
  2539. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2540. if(sd->state.lr_flag != 2)
  2541. status->def_ele=val;
  2542. break;
  2543. case SP_MAXHP:
  2544. if(sd->state.lr_flag == 2)
  2545. break;
  2546. sd->bonus.hp += val;
  2547. break;
  2548. case SP_MAXSP:
  2549. if(sd->state.lr_flag == 2)
  2550. break;
  2551. sd->bonus.sp += val;
  2552. break;
  2553. case SP_MAXHPRATE:
  2554. if(sd->state.lr_flag != 2)
  2555. sd->hprate+=val;
  2556. break;
  2557. case SP_MAXSPRATE:
  2558. if(sd->state.lr_flag != 2)
  2559. sd->sprate+=val;
  2560. break;
  2561. case SP_SPRATE:
  2562. if(sd->state.lr_flag != 2)
  2563. sd->dsprate+=val;
  2564. break;
  2565. case SP_ATTACKRANGE:
  2566. switch (sd->state.lr_flag) {
  2567. case 2:
  2568. switch (sd->status.weapon) {
  2569. case W_BOW:
  2570. case W_REVOLVER:
  2571. case W_RIFLE:
  2572. case W_GATLING:
  2573. case W_SHOTGUN:
  2574. case W_GRENADE:
  2575. status->rhw.range += val;
  2576. }
  2577. break;
  2578. case 1:
  2579. status->lhw.range += val;
  2580. break;
  2581. default:
  2582. status->rhw.range += val;
  2583. break;
  2584. }
  2585. break;
  2586. case SP_SPEED_RATE: //Non stackable increase
  2587. if(sd->state.lr_flag != 2)
  2588. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2589. break;
  2590. case SP_SPEED_ADDRATE: //Stackable increase
  2591. if(sd->state.lr_flag != 2)
  2592. sd->bonus.speed_add_rate -= val;
  2593. break;
  2594. case SP_ASPD: //Raw increase
  2595. if(sd->state.lr_flag != 2)
  2596. sd->bonus.aspd_add -= 10*val;
  2597. break;
  2598. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2599. if(sd->state.lr_flag != 2)
  2600. #ifndef RENEWAL_ASPD
  2601. status->aspd_rate -= 10*val;
  2602. #else
  2603. status->aspd_rate2 += val;
  2604. #endif
  2605. break;
  2606. case SP_HP_RECOV_RATE:
  2607. if(sd->state.lr_flag != 2)
  2608. sd->hprecov_rate += val;
  2609. break;
  2610. case SP_SP_RECOV_RATE:
  2611. if(sd->state.lr_flag != 2)
  2612. sd->sprecov_rate += val;
  2613. break;
  2614. case SP_CRITICAL_DEF:
  2615. if(sd->state.lr_flag != 2)
  2616. sd->bonus.critical_def += val;
  2617. break;
  2618. case SP_NEAR_ATK_DEF:
  2619. if(sd->state.lr_flag != 2)
  2620. sd->bonus.near_attack_def_rate += val;
  2621. break;
  2622. case SP_LONG_ATK_DEF:
  2623. if(sd->state.lr_flag != 2)
  2624. sd->bonus.long_attack_def_rate += val;
  2625. break;
  2626. case SP_DOUBLE_RATE:
  2627. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2628. sd->bonus.double_rate = val;
  2629. break;
  2630. case SP_DOUBLE_ADD_RATE:
  2631. if(sd->state.lr_flag == 0)
  2632. sd->bonus.double_add_rate += val;
  2633. break;
  2634. case SP_MATK_RATE:
  2635. if(sd->state.lr_flag != 2)
  2636. sd->matk_rate += val;
  2637. break;
  2638. case SP_IGNORE_DEF_ELE:
  2639. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2640. if(!sd->state.lr_flag)
  2641. sd->right_weapon.ignore_def_ele |= 1<<val;
  2642. else if(sd->state.lr_flag == 1)
  2643. sd->left_weapon.ignore_def_ele |= 1<<val;
  2644. break;
  2645. case SP_IGNORE_DEF_RACE:
  2646. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2647. if(!sd->state.lr_flag)
  2648. sd->right_weapon.ignore_def_race |= 1<<val;
  2649. else if(sd->state.lr_flag == 1)
  2650. sd->left_weapon.ignore_def_race |= 1<<val;
  2651. break;
  2652. case SP_IGNORE_DEF_CLASS:
  2653. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2654. if(!sd->state.lr_flag)
  2655. sd->right_weapon.ignore_def_class |= 1<<val;
  2656. else if(sd->state.lr_flag == 1)
  2657. sd->left_weapon.ignore_def_class |= 1<<val;
  2658. break;
  2659. case SP_ATK_RATE:
  2660. if(sd->state.lr_flag != 2)
  2661. sd->bonus.atk_rate += val;
  2662. break;
  2663. case SP_MAGIC_ATK_DEF:
  2664. if(sd->state.lr_flag != 2)
  2665. sd->bonus.magic_def_rate += val;
  2666. break;
  2667. case SP_MISC_ATK_DEF:
  2668. if(sd->state.lr_flag != 2)
  2669. sd->bonus.misc_def_rate += val;
  2670. break;
  2671. case SP_IGNORE_MDEF_ELE:
  2672. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2673. if(sd->state.lr_flag != 2)
  2674. sd->bonus.ignore_mdef_ele |= 1<<val;
  2675. break;
  2676. case SP_IGNORE_MDEF_RACE:
  2677. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2678. if(sd->state.lr_flag != 2)
  2679. sd->bonus.ignore_mdef_race |= 1<<val;
  2680. break;
  2681. case SP_PERFECT_HIT_RATE:
  2682. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2683. sd->bonus.perfect_hit = val;
  2684. break;
  2685. case SP_PERFECT_HIT_ADD_RATE:
  2686. if(sd->state.lr_flag != 2)
  2687. sd->bonus.perfect_hit_add += val;
  2688. break;
  2689. case SP_CRITICAL_RATE:
  2690. if(sd->state.lr_flag != 2)
  2691. sd->critical_rate+=val;
  2692. break;
  2693. case SP_DEF_RATIO_ATK_ELE:
  2694. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2695. if(!sd->state.lr_flag)
  2696. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2697. else if(sd->state.lr_flag == 1)
  2698. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2699. break;
  2700. case SP_DEF_RATIO_ATK_RACE:
  2701. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2702. if(!sd->state.lr_flag)
  2703. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2704. else if(sd->state.lr_flag == 1)
  2705. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2706. break;
  2707. case SP_DEF_RATIO_ATK_CLASS:
  2708. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2709. if(!sd->state.lr_flag)
  2710. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2711. else if(sd->state.lr_flag == 1)
  2712. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2713. break;
  2714. case SP_HIT_RATE:
  2715. if(sd->state.lr_flag != 2)
  2716. sd->hit_rate += val;
  2717. break;
  2718. case SP_FLEE_RATE:
  2719. if(sd->state.lr_flag != 2)
  2720. sd->flee_rate += val;
  2721. break;
  2722. case SP_FLEE2_RATE:
  2723. if(sd->state.lr_flag != 2)
  2724. sd->flee2_rate += val;
  2725. break;
  2726. case SP_DEF_RATE:
  2727. if(sd->state.lr_flag != 2)
  2728. sd->def_rate += val;
  2729. break;
  2730. case SP_DEF2_RATE:
  2731. if(sd->state.lr_flag != 2)
  2732. sd->def2_rate += val;
  2733. break;
  2734. case SP_MDEF_RATE:
  2735. if(sd->state.lr_flag != 2)
  2736. sd->mdef_rate += val;
  2737. break;
  2738. case SP_MDEF2_RATE:
  2739. if(sd->state.lr_flag != 2)
  2740. sd->mdef2_rate += val;
  2741. break;
  2742. case SP_RESTART_FULL_RECOVER:
  2743. if(sd->state.lr_flag != 2)
  2744. sd->special_state.restart_full_recover = 1;
  2745. break;
  2746. case SP_NO_CASTCANCEL:
  2747. if(sd->state.lr_flag != 2)
  2748. sd->special_state.no_castcancel = 1;
  2749. break;
  2750. case SP_NO_CASTCANCEL2:
  2751. if(sd->state.lr_flag != 2)
  2752. sd->special_state.no_castcancel2 = 1;
  2753. break;
  2754. case SP_NO_SIZEFIX:
  2755. if(sd->state.lr_flag != 2)
  2756. sd->special_state.no_sizefix = 1;
  2757. break;
  2758. case SP_NO_MAGIC_DAMAGE:
  2759. if(sd->state.lr_flag == 2)
  2760. break;
  2761. val+= sd->special_state.no_magic_damage;
  2762. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2763. break;
  2764. case SP_NO_WEAPON_DAMAGE:
  2765. if(sd->state.lr_flag == 2)
  2766. break;
  2767. val+= sd->special_state.no_weapon_damage;
  2768. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2769. break;
  2770. case SP_NO_MISC_DAMAGE:
  2771. if(sd->state.lr_flag == 2)
  2772. break;
  2773. val+= sd->special_state.no_misc_damage;
  2774. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2775. break;
  2776. case SP_NO_GEMSTONE:
  2777. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2778. sd->special_state.no_gemstone = 1;
  2779. break;
  2780. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2781. if(sd->state.lr_flag != 2) {
  2782. sd->special_state.intravision = 1;
  2783. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2784. }
  2785. break;
  2786. case SP_NO_KNOCKBACK:
  2787. if(sd->state.lr_flag != 2)
  2788. sd->special_state.no_knockback = 1;
  2789. break;
  2790. case SP_SPLASH_RANGE:
  2791. if(sd->bonus.splash_range < val)
  2792. sd->bonus.splash_range = val;
  2793. break;
  2794. case SP_SPLASH_ADD_RANGE:
  2795. sd->bonus.splash_add_range += val;
  2796. break;
  2797. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2798. if(sd->state.lr_flag != 2)
  2799. sd->bonus.short_weapon_damage_return += val;
  2800. break;
  2801. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2802. if(sd->state.lr_flag != 2)
  2803. sd->bonus.long_weapon_damage_return += val;
  2804. break;
  2805. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2806. if(sd->state.lr_flag != 2)
  2807. sd->bonus.magic_damage_return += val;
  2808. break;
  2809. case SP_ALL_STATS: // [Valaris]
  2810. if(sd->state.lr_flag!=2) {
  2811. sd->param_bonus[SP_STR-SP_STR]+=val;
  2812. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2813. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2814. sd->param_bonus[SP_INT-SP_STR]+=val;
  2815. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2816. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2817. }
  2818. break;
  2819. case SP_AGI_VIT: // [Valaris]
  2820. if(sd->state.lr_flag!=2) {
  2821. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2822. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2823. }
  2824. break;
  2825. case SP_AGI_DEX_STR: // [Valaris]
  2826. if(sd->state.lr_flag!=2) {
  2827. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2828. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2829. sd->param_bonus[SP_STR-SP_STR]+=val;
  2830. }
  2831. break;
  2832. case SP_PERFECT_HIDE: // [Valaris]
  2833. if(sd->state.lr_flag!=2)
  2834. sd->special_state.perfect_hiding=1;
  2835. break;
  2836. case SP_UNBREAKABLE:
  2837. if(sd->state.lr_flag!=2)
  2838. sd->bonus.unbreakable += val;
  2839. break;
  2840. case SP_UNBREAKABLE_WEAPON:
  2841. if(sd->state.lr_flag != 2)
  2842. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2843. break;
  2844. case SP_UNBREAKABLE_ARMOR:
  2845. if(sd->state.lr_flag != 2)
  2846. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2847. break;
  2848. case SP_UNBREAKABLE_HELM:
  2849. if(sd->state.lr_flag != 2)
  2850. sd->bonus.unbreakable_equip |= EQP_HELM;
  2851. break;
  2852. case SP_UNBREAKABLE_SHIELD:
  2853. if(sd->state.lr_flag != 2)
  2854. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2855. break;
  2856. case SP_UNBREAKABLE_GARMENT:
  2857. if(sd->state.lr_flag != 2)
  2858. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2859. break;
  2860. case SP_UNBREAKABLE_SHOES:
  2861. if(sd->state.lr_flag != 2)
  2862. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2863. break;
  2864. case SP_CLASSCHANGE: // [Valaris]
  2865. if(sd->state.lr_flag !=2)
  2866. sd->bonus.classchange=val;
  2867. break;
  2868. case SP_LONG_ATK_RATE:
  2869. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2870. sd->bonus.long_attack_atk_rate+=val;
  2871. break;
  2872. case SP_BREAK_WEAPON_RATE:
  2873. if(sd->state.lr_flag != 2)
  2874. sd->bonus.break_weapon_rate+=val;
  2875. break;
  2876. case SP_BREAK_ARMOR_RATE:
  2877. if(sd->state.lr_flag != 2)
  2878. sd->bonus.break_armor_rate+=val;
  2879. break;
  2880. case SP_ADD_STEAL_RATE:
  2881. if(sd->state.lr_flag != 2)
  2882. sd->bonus.add_steal_rate+=val;
  2883. break;
  2884. case SP_DELAYRATE:
  2885. if(sd->state.lr_flag != 2)
  2886. sd->delayrate+=val;
  2887. break;
  2888. case SP_CRIT_ATK_RATE:
  2889. if(sd->state.lr_flag != 2)
  2890. sd->bonus.crit_atk_rate += val;
  2891. break;
  2892. case SP_NO_REGEN:
  2893. if(sd->state.lr_flag != 2)
  2894. sd->regen.state.block|=val;
  2895. break;
  2896. case SP_UNSTRIPABLE_WEAPON:
  2897. if(sd->state.lr_flag != 2)
  2898. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2899. break;
  2900. case SP_UNSTRIPABLE:
  2901. case SP_UNSTRIPABLE_ARMOR:
  2902. if(sd->state.lr_flag != 2)
  2903. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2904. break;
  2905. case SP_UNSTRIPABLE_HELM:
  2906. if(sd->state.lr_flag != 2)
  2907. sd->bonus.unstripable_equip |= EQP_HELM;
  2908. break;
  2909. case SP_UNSTRIPABLE_SHIELD:
  2910. if(sd->state.lr_flag != 2)
  2911. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2912. break;
  2913. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2914. if(!sd->state.lr_flag) {
  2915. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2916. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2917. } else if(sd->state.lr_flag == 1) {
  2918. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2919. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2920. }
  2921. break;
  2922. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2923. if(!sd->state.lr_flag) {
  2924. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2925. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2926. } else if(sd->state.lr_flag == 1) {
  2927. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2928. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2929. }
  2930. break;
  2931. case SP_SP_GAIN_VALUE:
  2932. if(!sd->state.lr_flag)
  2933. sd->bonus.sp_gain_value += val;
  2934. break;
  2935. case SP_HP_GAIN_VALUE:
  2936. if(!sd->state.lr_flag)
  2937. sd->bonus.hp_gain_value += val;
  2938. break;
  2939. case SP_LONG_SP_GAIN_VALUE:
  2940. if(!sd->state.lr_flag)
  2941. sd->bonus.long_sp_gain_value += val;
  2942. case SP_LONG_HP_GAIN_VALUE:
  2943. if(!sd->state.lr_flag)
  2944. sd->bonus.long_hp_gain_value += val;
  2945. break;
  2946. case SP_MAGIC_SP_GAIN_VALUE:
  2947. if(!sd->state.lr_flag)
  2948. sd->bonus.magic_sp_gain_value += val;
  2949. break;
  2950. case SP_MAGIC_HP_GAIN_VALUE:
  2951. if(!sd->state.lr_flag)
  2952. sd->bonus.magic_hp_gain_value += val;
  2953. break;
  2954. case SP_ADD_HEAL_RATE:
  2955. if(sd->state.lr_flag != 2)
  2956. sd->bonus.add_heal_rate += val;
  2957. break;
  2958. case SP_ADD_HEAL2_RATE:
  2959. if(sd->state.lr_flag != 2)
  2960. sd->bonus.add_heal2_rate += val;
  2961. break;
  2962. case SP_ADD_ITEM_HEAL_RATE:
  2963. if(sd->state.lr_flag != 2)
  2964. sd->bonus.itemhealrate2 += val;
  2965. break;
  2966. case SP_EMATK:
  2967. if(sd->state.lr_flag != 2)
  2968. sd->bonus.ematk += val;
  2969. break;
  2970. case SP_ADD_VARIABLECAST:
  2971. if (sd->state.lr_flag != 2)
  2972. sd->bonus.add_varcast += val;
  2973. break;
  2974. #ifdef RENEWAL_CAST
  2975. case SP_FIXCASTRATE:
  2976. if(sd->state.lr_flag != 2)
  2977. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2978. break;
  2979. case SP_ADD_FIXEDCAST:
  2980. if(sd->state.lr_flag != 2)
  2981. sd->bonus.add_fixcast += val;
  2982. break;
  2983. case SP_CASTRATE:
  2984. case SP_VARCASTRATE:
  2985. if(sd->state.lr_flag != 2)
  2986. sd->bonus.varcastrate -= val;
  2987. break;
  2988. #else
  2989. case SP_ADD_FIXEDCAST:
  2990. case SP_FIXCASTRATE:
  2991. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2992. break;
  2993. case SP_VARCASTRATE:
  2994. case SP_CASTRATE:
  2995. if(sd->state.lr_flag != 2)
  2996. sd->castrate += val;
  2997. break;
  2998. #endif
  2999. case SP_ADDMAXWEIGHT:
  3000. if (sd->state.lr_flag != 2)
  3001. sd->add_max_weight += val;
  3002. break;
  3003. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3004. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3005. break;
  3006. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3007. if (sd->state.lr_flag != 2)
  3008. sd->bonus.critical_rangeatk += val*10;
  3009. else
  3010. sd->bonus.arrow_cri += val*10;
  3011. break;
  3012. case SP_WEAPON_ATK_RATE:
  3013. if (sd->state.lr_flag != 2)
  3014. sd->bonus.weapon_atk_rate += val;
  3015. break;
  3016. case SP_WEAPON_MATK_RATE:
  3017. if (sd->state.lr_flag != 2)
  3018. sd->bonus.weapon_matk_rate += val;
  3019. break;
  3020. case SP_NO_MADO_FUEL:
  3021. if (sd->state.lr_flag != 2)
  3022. sd->special_state.no_mado_fuel = 1;
  3023. break;
  3024. case SP_NO_WALK_DELAY:
  3025. if (sd->state.lr_flag != 2)
  3026. sd->special_state.no_walk_delay = 1;
  3027. break;
  3028. default:
  3029. if (running_npc_stat_calc_event) {
  3030. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  3031. }
  3032. else if (current_equip_combo_pos > 0) {
  3033. ShowWarning("pc_bonus: unknown bonus type %d %d in a combo with item #%d\n", type, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3034. }
  3035. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3036. ShowWarning("pc_bonus: unknown bonus type %d %d in item #%d\n", type, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3037. }
  3038. else {
  3039. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3040. }
  3041. break;
  3042. }
  3043. }
  3044. /*==========================================
  3045. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3046. * format: bonus2 bBonusName,type2,val;
  3047. * @param sd
  3048. * @param type Bonus type used by bBonusName
  3049. * @param type2
  3050. * @param val Value that usually for rate or fixed value
  3051. *------------------------------------------*/
  3052. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3053. {
  3054. nullpo_retv(sd);
  3055. switch(type){
  3056. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3057. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3058. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3059. sd->right_weapon.addele[type2]+=val;
  3060. else if(sd->state.lr_flag == 1)
  3061. sd->left_weapon.addele[type2]+=val;
  3062. else if(sd->state.lr_flag == 2)
  3063. sd->arrow_addele[type2]+=val;
  3064. break;
  3065. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3066. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3067. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3068. sd->right_weapon.addrace[type2]+=val;
  3069. else if(sd->state.lr_flag == 1)
  3070. sd->left_weapon.addrace[type2]+=val;
  3071. else if(sd->state.lr_flag == 2)
  3072. sd->arrow_addrace[type2]+=val;
  3073. break;
  3074. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3075. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3076. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3077. sd->right_weapon.addclass[type2]+=val;
  3078. else if(sd->state.lr_flag == 1)
  3079. sd->left_weapon.addclass[type2]+=val;
  3080. else if(sd->state.lr_flag == 2)
  3081. sd->arrow_addclass[type2]+=val;
  3082. break;
  3083. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3084. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3085. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3086. sd->right_weapon.addsize[type2]+=val;
  3087. else if(sd->state.lr_flag == 1)
  3088. sd->left_weapon.addsize[type2]+=val;
  3089. else if(sd->state.lr_flag == 2)
  3090. sd->arrow_addsize[type2]+=val;
  3091. break;
  3092. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3093. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3094. if(sd->state.lr_flag != 2)
  3095. sd->subele_script[type2] += val;
  3096. break;
  3097. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3098. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3099. if(sd->state.lr_flag != 2)
  3100. sd->subrace[type2]+=val;
  3101. break;
  3102. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3103. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3104. if(sd->state.lr_flag != 2)
  3105. sd->subclass[type2]+=val;
  3106. break;
  3107. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3108. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3109. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3110. break;
  3111. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3112. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3113. 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);
  3114. break;
  3115. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3116. if(sd->state.lr_flag == 2)
  3117. break;
  3118. if (sd->reseff.size() == MAX_PC_BONUS) {
  3119. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3120. break;
  3121. }
  3122. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3123. break;
  3124. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3125. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3126. if(sd->state.lr_flag != 2)
  3127. sd->magic_addele_script[type2] += val;
  3128. break;
  3129. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3130. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3131. if(sd->state.lr_flag != 2)
  3132. sd->magic_addrace[type2]+=val;
  3133. break;
  3134. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3135. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3136. if(sd->state.lr_flag != 2)
  3137. sd->magic_addclass[type2]+=val;
  3138. break;
  3139. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3140. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3141. if(sd->state.lr_flag != 2)
  3142. sd->magic_addsize[type2]+=val;
  3143. break;
  3144. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3145. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3146. if(sd->state.lr_flag != 2)
  3147. sd->magic_atk_ele[type2]+=val;
  3148. break;
  3149. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3150. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3151. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3152. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3153. break;
  3154. }
  3155. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3156. }
  3157. break;
  3158. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3159. if(sd->state.lr_flag == 2)
  3160. break;
  3161. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3162. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3163. break;
  3164. }
  3165. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3166. break;
  3167. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3168. if(sd->state.lr_flag == 2)
  3169. break;
  3170. if (sd->add_def.size() == MAX_PC_BONUS) {
  3171. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3172. break;
  3173. }
  3174. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3175. break;
  3176. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3177. if(sd->state.lr_flag == 2)
  3178. break;
  3179. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3180. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3181. break;
  3182. }
  3183. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3184. break;
  3185. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3186. if(!sd->state.lr_flag) {
  3187. sd->right_weapon.hp_drain_rate.rate += type2;
  3188. sd->right_weapon.hp_drain_rate.per += val;
  3189. }
  3190. else if(sd->state.lr_flag == 1) {
  3191. sd->left_weapon.hp_drain_rate.rate += type2;
  3192. sd->left_weapon.hp_drain_rate.per += val;
  3193. }
  3194. break;
  3195. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3196. if(!sd->state.lr_flag) {
  3197. sd->right_weapon.sp_drain_rate.rate += type2;
  3198. sd->right_weapon.sp_drain_rate.per += val;
  3199. }
  3200. else if(sd->state.lr_flag == 1) {
  3201. sd->left_weapon.sp_drain_rate.rate += type2;
  3202. sd->left_weapon.sp_drain_rate.per += val;
  3203. }
  3204. break;
  3205. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3206. if(sd->state.lr_flag != 2) {
  3207. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3208. }
  3209. break;
  3210. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3211. if(sd->state.lr_flag != 2) {
  3212. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3213. }
  3214. break;
  3215. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3216. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3217. sd->bonus.get_zeny_rate = val;
  3218. sd->bonus.get_zeny_num = type2;
  3219. }
  3220. break;
  3221. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3222. if(sd->state.lr_flag != 2) {
  3223. sd->bonus.get_zeny_rate += val;
  3224. sd->bonus.get_zeny_num += type2;
  3225. }
  3226. break;
  3227. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3228. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3229. if(sd->state.lr_flag == 2)
  3230. break;
  3231. sd->weapon_coma_ele[type2] += val;
  3232. sd->special_state.bonus_coma = 1;
  3233. break;
  3234. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3235. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3236. if(sd->state.lr_flag == 2)
  3237. break;
  3238. sd->weapon_coma_race[type2] += val;
  3239. sd->special_state.bonus_coma = 1;
  3240. break;
  3241. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3242. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3243. if(sd->state.lr_flag == 2)
  3244. break;
  3245. sd->weapon_coma_class[type2] += val;
  3246. sd->special_state.bonus_coma = 1;
  3247. break;
  3248. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3249. if(sd->state.lr_flag != 2)
  3250. sd->weapon_atk[type2]+=val;
  3251. break;
  3252. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3253. if(sd->state.lr_flag != 2)
  3254. sd->weapon_damage_rate[type2]+=val;
  3255. break;
  3256. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3257. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3258. if(sd->state.lr_flag != 2)
  3259. sd->critaddrace[type2] += val*10;
  3260. break;
  3261. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3262. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3263. if(sd->state.lr_flag != 2)
  3264. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3265. break;
  3266. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3267. if(sd->state.lr_flag == 2)
  3268. break;
  3269. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3270. 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);
  3271. break;
  3272. }
  3273. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3274. break;
  3275. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3276. if(sd->state.lr_flag == 2)
  3277. break;
  3278. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3279. 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);
  3280. break;
  3281. }
  3282. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3283. break;
  3284. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3285. if(sd->state.lr_flag == 2)
  3286. break;
  3287. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3288. 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);
  3289. break;
  3290. }
  3291. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3292. break;
  3293. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3294. if(sd->state.lr_flag == 2)
  3295. break;
  3296. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3297. 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);
  3298. break;
  3299. }
  3300. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3301. break;
  3302. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3303. if(sd->state.lr_flag != 2) {
  3304. sd->hp_loss.value = type2;
  3305. sd->hp_loss.rate = val;
  3306. }
  3307. break;
  3308. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3309. if(sd->state.lr_flag != 2) {
  3310. sd->hp_regen.value = type2;
  3311. sd->hp_regen.rate = val;
  3312. }
  3313. break;
  3314. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3315. if (sd->state.lr_flag != 2) {
  3316. sd->percent_hp_regen.value = type2;
  3317. sd->percent_hp_regen.rate = val;
  3318. }
  3319. break;
  3320. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3321. if (sd->state.lr_flag != 2) {
  3322. sd->percent_sp_regen.value = type2;
  3323. sd->percent_sp_regen.rate = val;
  3324. }
  3325. break;
  3326. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3327. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3328. if(sd->state.lr_flag != 2)
  3329. sd->right_weapon.addrace2[type2] += val;
  3330. else
  3331. sd->left_weapon.addrace2[type2] += val;
  3332. break;
  3333. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3334. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3335. if(sd->state.lr_flag != 2)
  3336. sd->subsize[type2]+=val;
  3337. break;
  3338. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3339. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3340. if(sd->state.lr_flag != 2)
  3341. sd->subrace2[type2]+=val;
  3342. break;
  3343. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3344. if(sd->state.lr_flag == 2)
  3345. break;
  3346. if (!itemdb_exists(type2)) {
  3347. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3348. break;
  3349. }
  3350. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3351. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3352. break;
  3353. }
  3354. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3355. break;
  3356. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3357. if (sd->state.lr_flag == 2)
  3358. break;
  3359. if (!type2 || !itemdb_group_exists(type2)) {
  3360. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3361. break;
  3362. }
  3363. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3364. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3365. break;
  3366. }
  3367. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3368. break;
  3369. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3370. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3371. if(sd->state.lr_flag != 2)
  3372. sd->expaddrace[type2]+=val;
  3373. break;
  3374. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3375. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3376. if(sd->state.lr_flag != 2)
  3377. sd->expaddclass[type2]+=val;
  3378. break;
  3379. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3380. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3381. if(sd->state.lr_flag != 2)
  3382. sd->sp_gain_race[type2]+=val;
  3383. break;
  3384. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3385. if (sd->state.lr_flag != 2)
  3386. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3387. break;
  3388. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3389. if (sd->state.lr_flag != 2)
  3390. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3391. break;
  3392. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3393. if(sd->state.lr_flag != 2) {
  3394. sd->sp_loss.value = type2;
  3395. sd->sp_loss.rate = val;
  3396. }
  3397. break;
  3398. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3399. if(sd->state.lr_flag != 2) {
  3400. sd->sp_regen.value = type2;
  3401. sd->sp_regen.rate = val;
  3402. }
  3403. break;
  3404. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3405. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3406. if(!sd->state.lr_flag) {
  3407. sd->right_weapon.hp_drain_race[type2] += val;
  3408. }
  3409. else if(sd->state.lr_flag == 1) {
  3410. sd->left_weapon.hp_drain_race[type2] += val;
  3411. }
  3412. break;
  3413. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3414. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3415. if(!sd->state.lr_flag) {
  3416. sd->right_weapon.sp_drain_race[type2] += val;
  3417. }
  3418. else if(sd->state.lr_flag == 1) {
  3419. sd->left_weapon.sp_drain_race[type2] += val;
  3420. }
  3421. break;
  3422. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3423. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3424. if(!sd->state.lr_flag) {
  3425. sd->right_weapon.hp_drain_class[type2] += val;
  3426. }
  3427. else if(sd->state.lr_flag == 1) {
  3428. sd->left_weapon.hp_drain_class[type2] += val;
  3429. }
  3430. break;
  3431. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3432. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3433. if(!sd->state.lr_flag) {
  3434. sd->right_weapon.sp_drain_class[type2] += val;
  3435. }
  3436. else if(sd->state.lr_flag == 1) {
  3437. sd->left_weapon.sp_drain_class[type2] += val;
  3438. }
  3439. break;
  3440. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3441. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3442. if(sd->state.lr_flag != 2)
  3443. sd->ignore_mdef_by_race[type2] += val;
  3444. break;
  3445. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3446. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3447. if(sd->state.lr_flag != 2)
  3448. sd->ignore_mdef_by_class[type2] += val;
  3449. break;
  3450. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3451. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3452. if(sd->state.lr_flag != 2)
  3453. sd->ignore_def_by_race[type2] += val;
  3454. break;
  3455. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3456. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3457. if (sd->state.lr_flag != 2)
  3458. sd->ignore_def_by_class[type2] += val;
  3459. break;
  3460. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3461. if(sd->state.lr_flag == 2)
  3462. break;
  3463. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3464. 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);
  3465. break;
  3466. }
  3467. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3468. break;
  3469. case SP_SKILL_DELAY:
  3470. if(sd->state.lr_flag == 2)
  3471. break;
  3472. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3473. 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);
  3474. break;
  3475. }
  3476. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3477. break;
  3478. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3479. if(sd->state.lr_flag == 2)
  3480. break;
  3481. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3482. 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);
  3483. break;
  3484. }
  3485. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  3486. break;
  3487. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3488. if (sd->state.lr_flag == 2)
  3489. break;
  3490. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3491. 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);
  3492. break;
  3493. }
  3494. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  3495. break;
  3496. #ifdef RENEWAL_CAST
  3497. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3498. if(sd->state.lr_flag == 2)
  3499. break;
  3500. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3501. 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);
  3502. break;
  3503. }
  3504. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  3505. break;
  3506. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3507. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3508. if(sd->state.lr_flag == 2)
  3509. break;
  3510. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3511. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3512. break;
  3513. }
  3514. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  3515. break;
  3516. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3517. if(sd->state.lr_flag == 2)
  3518. break;
  3519. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3520. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3521. break;
  3522. }
  3523. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  3524. break;
  3525. #else
  3526. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3527. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3528. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3529. break;
  3530. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3531. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3532. if(sd->state.lr_flag == 2)
  3533. break;
  3534. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3535. 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);
  3536. break;
  3537. }
  3538. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  3539. break;
  3540. #endif
  3541. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3542. if(sd->state.lr_flag == 2)
  3543. break;
  3544. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3545. 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);
  3546. break;
  3547. }
  3548. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  3549. break;
  3550. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3551. if (sd->subskill.size() == MAX_PC_BONUS) {
  3552. 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);
  3553. break;
  3554. }
  3555. pc_bonus_itembonus(sd->subskill, type2, val, false);
  3556. break;
  3557. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3558. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3559. sd->subdefele[type2] += val;
  3560. break;
  3561. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3562. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3563. sd->coma_class[type2] += val;
  3564. sd->special_state.bonus_coma = 1;
  3565. break;
  3566. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3567. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3568. sd->coma_race[type2] += val;
  3569. sd->special_state.bonus_coma = 1;
  3570. break;
  3571. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3572. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3573. if(sd->state.lr_flag != 2)
  3574. sd->magic_addrace2[type2] += val;
  3575. break;
  3576. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3577. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3578. if (sd->state.lr_flag != 2)
  3579. sd->ignore_mdef_by_race2[type2] += val;
  3580. break;
  3581. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3582. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3583. if (sd->state.lr_flag != 2)
  3584. sd->dropaddrace[type2] += val;
  3585. break;
  3586. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3587. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3588. if (sd->state.lr_flag != 2)
  3589. sd->dropaddclass[type2] += val;
  3590. break;
  3591. default:
  3592. if (running_npc_stat_calc_event) {
  3593. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3594. }
  3595. else if (current_equip_combo_pos > 0) {
  3596. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in a combo with item #%d\n", type, type2, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3597. }
  3598. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3599. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in item #%d\n", type, type2, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3600. }
  3601. else {
  3602. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3603. }
  3604. break;
  3605. }
  3606. }
  3607. /**
  3608. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  3609. * @param sd
  3610. * @param type Bonus type used by bBonusName
  3611. * @param type2
  3612. * @param type3
  3613. * @param val Value that usually for rate or fixed value
  3614. */
  3615. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3616. {
  3617. nullpo_retv(sd);
  3618. switch(type){
  3619. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3620. if(sd->state.lr_flag != 2)
  3621. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  3622. break;
  3623. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3624. if(sd->state.lr_flag != 2)
  3625. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  3626. break;
  3627. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3628. if(sd->state.lr_flag != 2)
  3629. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  3630. break;
  3631. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3632. if(sd->state.lr_flag != 2)
  3633. {
  3634. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3635. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3636. pc_bonus_autospell(sd->autospell, target?-type2:type2, type3, val, 0, current_equip_card_id);
  3637. }
  3638. break;
  3639. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3640. if(sd->state.lr_flag != 2)
  3641. {
  3642. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3643. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3644. pc_bonus_autospell(sd->autospell2, target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3645. }
  3646. break;
  3647. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3648. if (sd->state.lr_flag != 2)
  3649. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  3650. break;
  3651. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3652. if (sd->state.lr_flag != 2)
  3653. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  3654. break;
  3655. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3656. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3657. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3658. break;
  3659. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3660. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3661. if(sd->state.lr_flag != 2)
  3662. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  3663. break;
  3664. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3665. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3666. if( sd->state.lr_flag != 2 )
  3667. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  3668. break;
  3669. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3670. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3671. if (sd->state.lr_flag != 2)
  3672. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3673. break;
  3674. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3675. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3676. if (sd->state.lr_flag != 2)
  3677. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3678. break;
  3679. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3680. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3681. if(sd->state.lr_flag != 2) {
  3682. sd->sp_vanish_race[type2].rate += type3;
  3683. sd->sp_vanish_race[type2].per += val;
  3684. }
  3685. break;
  3686. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3687. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3688. if(sd->state.lr_flag != 2) {
  3689. sd->hp_vanish_race[type2].rate += type3;
  3690. sd->hp_vanish_race[type2].per += val;
  3691. }
  3692. break;
  3693. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  3694. if(sd->state.lr_flag != 2) {
  3695. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  3696. }
  3697. break;
  3698. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  3699. if(sd->state.lr_flag != 2) {
  3700. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  3701. }
  3702. break;
  3703. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3704. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3705. if (sd->state.lr_flag == 2)
  3706. break;
  3707. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3708. //if (type3 > sd->norecover_state_race[type2].rate) {
  3709. // sd->norecover_state_race[type2].rate = type3;
  3710. // sd->norecover_state_race[type2].tick = val;
  3711. // break;
  3712. //}
  3713. sd->norecover_state_race[type2].rate = type3;
  3714. sd->norecover_state_race[type2].tick = val;
  3715. break;
  3716. default:
  3717. if (running_npc_stat_calc_event) {
  3718. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3719. }
  3720. else if (current_equip_combo_pos > 0) {
  3721. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in a combo with item #%d\n", type, type2, type3, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3722. }
  3723. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3724. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in item #%d\n", type, type2, type3, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3725. }
  3726. else {
  3727. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3728. }
  3729. break;
  3730. }
  3731. }
  3732. /**
  3733. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  3734. * @param sd
  3735. * @param type Bonus type used by bBonusName
  3736. * @param type2
  3737. * @param type3
  3738. * @param type4
  3739. * @param val Value that usually for rate or fixed value
  3740. */
  3741. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3742. {
  3743. nullpo_retv(sd);
  3744. switch(type){
  3745. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3746. if(sd->state.lr_flag != 2)
  3747. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3748. break;
  3749. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3750. if(sd->state.lr_flag != 2)
  3751. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3752. break;
  3753. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3754. if(sd->state.lr_flag != 2)
  3755. {
  3756. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3757. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  3758. pc_bonus_autospell_onskill(sd->autospell3, type2, target?-type3:type3, type4, val, current_equip_card_id);
  3759. }
  3760. break;
  3761. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3762. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3763. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3764. break;
  3765. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3766. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3767. if (sd->state.lr_flag != 2)
  3768. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  3769. break;
  3770. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3771. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3772. if( sd->state.lr_flag != 2 )
  3773. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  3774. break;
  3775. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3776. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3777. if(sd->state.lr_flag == 2)
  3778. break;
  3779. sd->def_set_race[type2].rate = type3;
  3780. sd->def_set_race[type2].tick = type4;
  3781. sd->def_set_race[type2].value = val;
  3782. break;
  3783. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3784. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3785. if(sd->state.lr_flag == 2)
  3786. break;
  3787. sd->mdef_set_race[type2].rate = type3;
  3788. sd->mdef_set_race[type2].tick = type4;
  3789. sd->mdef_set_race[type2].value = val;
  3790. break;
  3791. default:
  3792. if (running_npc_stat_calc_event) {
  3793. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3794. }
  3795. else if (current_equip_combo_pos > 0) {
  3796. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in a combo with item #%d\n", type, type2, type3, type4, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3797. }
  3798. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3799. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in item #%d\n", type, type2, type3, type4, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3800. }
  3801. else {
  3802. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3803. }
  3804. break;
  3805. }
  3806. }
  3807. /**
  3808. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3809. * @param sd
  3810. * @param type Bonus type used by bBonusName
  3811. * @param type2
  3812. * @param type3
  3813. * @param type4
  3814. * @param val Value that usually for rate or fixed value
  3815. */
  3816. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3817. {
  3818. nullpo_retv(sd);
  3819. switch(type){
  3820. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3821. if(sd->state.lr_flag != 2)
  3822. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3823. break;
  3824. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3825. if(sd->state.lr_flag != 2)
  3826. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3827. break;
  3828. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3829. if(sd->state.lr_flag != 2)
  3830. pc_bonus_autospell_onskill(sd->autospell3, type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3831. break;
  3832. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3833. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3834. if( sd->state.lr_flag != 2 )
  3835. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  3836. break;
  3837. default:
  3838. if (running_npc_stat_calc_event) {
  3839. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3840. }
  3841. else if (current_equip_combo_pos > 0) {
  3842. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in a combo with item #%d\n", type, type2, type3, type4, type5, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3843. }
  3844. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3845. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in item #%d\n", type, type2, type3, type4, type5, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3846. }
  3847. else {
  3848. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3849. }
  3850. break;
  3851. }
  3852. }
  3853. /*==========================================
  3854. * Grants a player a given skill. Flag values are:
  3855. * 0 - Grant permanent skill to be bound to skill tree
  3856. * 1 - Grant an item skill (temporary)
  3857. * 2 - Like 1, except the level granted can stack with previously learned level.
  3858. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3859. *------------------------------------------*/
  3860. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3861. uint16 idx = 0;
  3862. nullpo_ret(sd);
  3863. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3864. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3865. return false;
  3866. }
  3867. if (level > MAX_SKILL_LEVEL) {
  3868. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3869. return false;
  3870. }
  3871. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3872. 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);
  3873. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3874. }
  3875. switch (type) {
  3876. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3877. sd->status.skill[idx].id = skill_id;
  3878. sd->status.skill[idx].lv = level;
  3879. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3880. if (level == 0) { //Remove skill.
  3881. sd->status.skill[idx].id = 0;
  3882. clif_deleteskill(sd,skill_id);
  3883. } else
  3884. clif_addskill(sd,skill_id);
  3885. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3886. status_calc_pc(sd, SCO_NONE);
  3887. break;
  3888. case ADDSKILL_TEMP: //Item bonus skill.
  3889. if (sd->status.skill[idx].id != 0) {
  3890. if (sd->status.skill[idx].lv >= level)
  3891. return true;
  3892. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3893. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3894. } else {
  3895. sd->status.skill[idx].id = skill_id;
  3896. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3897. }
  3898. sd->status.skill[idx].lv = level;
  3899. break;
  3900. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3901. if (sd->status.skill[idx].id != 0) {
  3902. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3903. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3904. } else {
  3905. sd->status.skill[idx].id = skill_id;
  3906. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3907. }
  3908. sd->status.skill[idx].lv += level;
  3909. break;
  3910. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3911. sd->status.skill[idx].id = skill_id;
  3912. sd->status.skill[idx].lv = level;
  3913. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3914. if (level == 0) { //Remove skill.
  3915. sd->status.skill[idx].id = 0;
  3916. clif_deleteskill(sd,skill_id);
  3917. } else
  3918. clif_addskill(sd,skill_id);
  3919. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3920. status_calc_pc(sd, SCO_NONE);
  3921. break;
  3922. default:
  3923. return false;
  3924. }
  3925. return true;
  3926. }
  3927. /*==========================================
  3928. * Append a card to an item ?
  3929. *------------------------------------------*/
  3930. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3931. {
  3932. int i;
  3933. unsigned short nameid;
  3934. nullpo_ret(sd);
  3935. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3936. return 0; //Invalid item index.
  3937. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3938. return 0; //Invalid card index.
  3939. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  3940. return 0; // target item missing
  3941. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  3942. return 0; // target card missing
  3943. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3944. return 0; // only weapons and armor are allowed
  3945. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3946. return 0; // must be a card
  3947. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  3948. return 0; // target must be identified
  3949. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  3950. return 0; // card slots reserved for other purposes
  3951. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3952. return 0; // card cannot be compounded on this item type
  3953. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3954. return 0; // attempted to place shield card on left-hand weapon.
  3955. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  3956. return 0; // item must be unequipped
  3957. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  3958. if( i == sd->inventory_data[idx_equip]->slot )
  3959. return 0; // no free slots
  3960. // remember the card id to insert
  3961. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  3962. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3963. {// failed
  3964. clif_insert_card(sd,idx_equip,idx_card,1);
  3965. }
  3966. else
  3967. {// success
  3968. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  3969. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  3970. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  3971. clif_insert_card(sd,idx_equip,idx_card,0);
  3972. }
  3973. return 0;
  3974. }
  3975. /**
  3976. * Returns the count of unidentified items with the option to identify too.
  3977. * @param sd: Player data
  3978. * @param identify_item: Whether or not to identify any unidentified items
  3979. * @return Unidentified items count
  3980. */
  3981. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  3982. {
  3983. int unidentified_count = 0;
  3984. for (int i = 0; i < MAX_INVENTORY; i++) {
  3985. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  3986. if (identify_item == true) {
  3987. sd->inventory.u.items_inventory[i].identify = 1;
  3988. clif_item_identified(sd,i,0);
  3989. }
  3990. unidentified_count++;
  3991. }
  3992. }
  3993. return unidentified_count;
  3994. }
  3995. //
  3996. // Items
  3997. //
  3998. /*==========================================
  3999. * Update buying value by skills
  4000. *------------------------------------------*/
  4001. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4002. {
  4003. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4004. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4005. rate1 = 5+skill*2-((skill==10)? 1:0);
  4006. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4007. rate2 = 5+skill*4;
  4008. if(rate1 < rate2) rate1 = rate2;
  4009. if(rate1)
  4010. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4011. if(val < battle_config.min_shop_buy)
  4012. val = battle_config.min_shop_buy;
  4013. return val;
  4014. }
  4015. /*==========================================
  4016. * Update selling value by skills
  4017. *------------------------------------------*/
  4018. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4019. {
  4020. int skill,val = orig_value,rate = 0;
  4021. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4022. rate = 5+skill*2-((skill==10)? 1:0);
  4023. if(rate)
  4024. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4025. if (val < battle_config.min_shop_sell)
  4026. val = battle_config.min_shop_sell;
  4027. return val;
  4028. }
  4029. /*==========================================
  4030. * Checking if we have enough place on inventory for new item
  4031. * Make sure to take 30k as limit (for client I guess)
  4032. * @param sd
  4033. * @param nameid
  4034. * @param amount
  4035. * @return e_chkitem_result
  4036. *------------------------------------------*/
  4037. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  4038. {
  4039. int i;
  4040. struct item_data* data;
  4041. nullpo_ret(sd);
  4042. if(amount > MAX_AMOUNT)
  4043. return CHKADDITEM_OVERAMOUNT;
  4044. data = itemdb_search(nameid);
  4045. if(!itemdb_isstackable2(data))
  4046. return CHKADDITEM_NEW;
  4047. if( data->stack.inventory && amount > data->stack.amount )
  4048. return CHKADDITEM_OVERAMOUNT;
  4049. if (data->flag.guid)
  4050. return CHKADDITEM_NEW;
  4051. for(i=0;i<MAX_INVENTORY;i++){
  4052. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4053. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4054. 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 ) )
  4055. return CHKADDITEM_OVERAMOUNT;
  4056. return CHKADDITEM_EXIST;
  4057. }
  4058. }
  4059. return CHKADDITEM_NEW;
  4060. }
  4061. /*==========================================
  4062. * Return number of available place in inventory
  4063. * Each non stackable item will reduce place by 1
  4064. * @param sd
  4065. * @return Number of empty slots
  4066. *------------------------------------------*/
  4067. uint8 pc_inventoryblank(struct map_session_data *sd)
  4068. {
  4069. uint8 i, b;
  4070. nullpo_ret(sd);
  4071. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  4072. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4073. b++;
  4074. }
  4075. return b;
  4076. }
  4077. /**
  4078. * Attempts to remove zeny from player
  4079. * @param sd: Player
  4080. * @param zeny: Zeny removed
  4081. * @param type: Log type
  4082. * @param tsd: (optional) From who to log (if null take sd)
  4083. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4084. */
  4085. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4086. {
  4087. nullpo_retr(2,sd);
  4088. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4089. if( zeny < 0 )
  4090. {
  4091. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4092. return 1;
  4093. }
  4094. if( sd->status.zeny < zeny )
  4095. return 1; //Not enough.
  4096. sd->status.zeny -= zeny;
  4097. clif_updatestatus(sd,SP_ZENY);
  4098. if(!tsd) tsd = sd;
  4099. log_zeny(sd, type, tsd, -zeny);
  4100. if( zeny > 0 && sd->state.showzeny ) {
  4101. char output[255];
  4102. sprintf(output, "Removed %dz.", zeny);
  4103. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4104. }
  4105. return 0;
  4106. }
  4107. /**
  4108. * Attempts to give zeny to player
  4109. * @param sd: Player
  4110. * @param type: Log type
  4111. * @param tsd: (optional) From who to log (if null take sd)
  4112. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4113. */
  4114. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4115. {
  4116. nullpo_retr(-1,sd);
  4117. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4118. if( zeny < 0 )
  4119. {
  4120. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4121. return 1;
  4122. }
  4123. if( zeny > MAX_ZENY - sd->status.zeny )
  4124. zeny = MAX_ZENY - sd->status.zeny;
  4125. sd->status.zeny += zeny;
  4126. clif_updatestatus(sd,SP_ZENY);
  4127. if(!tsd) tsd = sd;
  4128. log_zeny(sd, type, tsd, zeny);
  4129. if( zeny > 0 && sd->state.showzeny ) {
  4130. char output[255];
  4131. sprintf(output, "Gained %dz.", zeny);
  4132. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4133. }
  4134. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4135. return 0;
  4136. }
  4137. /**
  4138. * Attempts to remove Cash Points from player
  4139. * @param sd: Player
  4140. * @param price: Total points (cash + kafra) the player has to pay
  4141. * @param points: Kafra points the player has to pay
  4142. * @param type: Log type
  4143. * @return -1: Not enough points, otherwise success (cash+points)
  4144. */
  4145. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4146. {
  4147. int cash;
  4148. nullpo_retr(-1,sd);
  4149. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4150. cash = price-points;
  4151. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4152. {
  4153. 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);
  4154. return -1;
  4155. }
  4156. if( cash ){
  4157. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4158. sd->cashPoints -= cash;
  4159. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4160. }
  4161. if( points ){
  4162. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4163. sd->kafraPoints -= points;
  4164. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4165. }
  4166. if( battle_config.cashshop_show_points )
  4167. {
  4168. char output[CHAT_SIZE_MAX];
  4169. 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.
  4170. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4171. }
  4172. return cash+points;
  4173. }
  4174. /**
  4175. * Attempts to give Cash Points to player
  4176. * @param sd: Player
  4177. * @param cash: Cash points the player gets
  4178. * @param points: Kafra points the player gets
  4179. * @param type: Log type
  4180. * @return -1: Error, otherwise success (cash or points)
  4181. */
  4182. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4183. {
  4184. char output[CHAT_SIZE_MAX];
  4185. nullpo_retr(-1,sd);
  4186. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4187. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4188. if( cash > 0 )
  4189. {
  4190. if( cash > MAX_ZENY-sd->cashPoints )
  4191. {
  4192. 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);
  4193. cash = MAX_ZENY-sd->cashPoints;
  4194. }
  4195. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4196. sd->cashPoints += cash;
  4197. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4198. if( battle_config.cashshop_show_points )
  4199. {
  4200. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4201. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4202. }
  4203. return cash;
  4204. }
  4205. if( points > 0 )
  4206. {
  4207. if( points > MAX_ZENY-sd->kafraPoints )
  4208. {
  4209. 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);
  4210. points = MAX_ZENY-sd->kafraPoints;
  4211. }
  4212. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4213. sd->kafraPoints += points;
  4214. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4215. if( battle_config.cashshop_show_points )
  4216. {
  4217. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4218. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4219. }
  4220. return points;
  4221. }
  4222. return -1; //shouldn't happen but just in case
  4223. }
  4224. /**
  4225. * Searching a specified itemid in inventory and return his stored index
  4226. * @param sd Player
  4227. * @param nameid Find this Item!
  4228. * @return Stored index in inventory, or -1 if not found.
  4229. **/
  4230. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4231. short i;
  4232. nullpo_retr(-1, sd);
  4233. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4234. return ( i < MAX_INVENTORY ) ? i : -1;
  4235. }
  4236. /** Attempt to add a new item to player inventory
  4237. * @param sd
  4238. * @param item_data
  4239. * @param amount
  4240. * @param log_type
  4241. * @return
  4242. * 0 = success
  4243. * 1 = invalid itemid not found or negative amount
  4244. * 2 = overweight
  4245. * 3 = ?
  4246. * 4 = no free place found
  4247. * 5 = max amount reached
  4248. * 6 = ?
  4249. * 7 = stack limitation
  4250. */
  4251. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4252. struct item_data *id;
  4253. int16 i;
  4254. unsigned int w;
  4255. nullpo_retr(ADDITEM_INVALID, sd);
  4256. nullpo_retr(ADDITEM_INVALID, item);
  4257. if( item->nameid == 0 || amount <= 0 )
  4258. return ADDITEM_INVALID;
  4259. if( amount > MAX_AMOUNT )
  4260. return ADDITEM_OVERAMOUNT;
  4261. id = itemdb_search(item->nameid);
  4262. if( id->stack.inventory && amount > id->stack.amount )
  4263. {// item stack limitation
  4264. return ADDITEM_STACKLIMIT;
  4265. }
  4266. w = id->weight*amount;
  4267. if(sd->weight + w > sd->max_weight)
  4268. return ADDITEM_OVERWEIGHT;
  4269. i = MAX_INVENTORY;
  4270. if (id->flag.guid && !item->unique_id)
  4271. item->unique_id = pc_generate_unique_id(sd);
  4272. // Stackable | Non Rental
  4273. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4274. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4275. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4276. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4277. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4278. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4279. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4280. 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 ) )
  4281. return ADDITEM_OVERAMOUNT;
  4282. sd->inventory.u.items_inventory[i].amount += amount;
  4283. clif_additem(sd,i,amount,0);
  4284. break;
  4285. }
  4286. }
  4287. }
  4288. if (i >= MAX_INVENTORY) {
  4289. i = pc_search_inventory(sd,0);
  4290. if( i < 0 )
  4291. return ADDITEM_OVERITEM;
  4292. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4293. // clear equip and favorite fields first, just in case
  4294. if( item->equip )
  4295. sd->inventory.u.items_inventory[i].equip = 0;
  4296. if( item->favorite )
  4297. sd->inventory.u.items_inventory[i].favorite = 0;
  4298. if( item->equipSwitch )
  4299. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4300. sd->inventory.u.items_inventory[i].amount = amount;
  4301. sd->inventory_data[i] = id;
  4302. sd->last_addeditem_index = i;
  4303. if (!itemdb_isstackable2(id) || id->flag.guid)
  4304. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4305. clif_additem(sd,i,amount,0);
  4306. }
  4307. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4308. sd->weight += w;
  4309. clif_updatestatus(sd,SP_WEIGHT);
  4310. //Auto-equip
  4311. if(id->flag.autoequip)
  4312. pc_equipitem(sd, i, id->equip);
  4313. /* rental item check */
  4314. if( item->expire_time ) {
  4315. if( time(NULL) > item->expire_time ) {
  4316. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4317. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4318. } else {
  4319. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4320. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4321. pc_inventory_rental_add(sd, seconds);
  4322. }
  4323. }
  4324. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4325. pc_show_questinfo(sd);
  4326. return ADDITEM_SUCCESS;
  4327. }
  4328. /*==========================================
  4329. * Remove an item at index n from inventory by amount.
  4330. * @param sd
  4331. * @param n Item index in inventory
  4332. * @param amount
  4333. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4334. * @param reason Delete reason
  4335. * @param log_type e_log_pick_type
  4336. * @return 1 - invalid itemid or negative amount; 0 - Success
  4337. *------------------------------------------*/
  4338. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4339. {
  4340. nullpo_retr(1, sd);
  4341. 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)
  4342. return 1;
  4343. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4344. sd->inventory.u.items_inventory[n].amount -= amount;
  4345. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4346. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4347. if(sd->inventory.u.items_inventory[n].equip)
  4348. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4349. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4350. sd->inventory_data[n] = NULL;
  4351. }
  4352. if(!(type&1))
  4353. clif_delitem(sd,n,amount,reason);
  4354. if(!(type&2))
  4355. clif_updatestatus(sd,SP_WEIGHT);
  4356. pc_show_questinfo(sd);
  4357. return 0;
  4358. }
  4359. /*==========================================
  4360. * Attempt to drop an item.
  4361. * @param sd
  4362. * @param n Item index in inventory
  4363. * @param amount Amount of item
  4364. * @return False = fail; True = success
  4365. *------------------------------------------*/
  4366. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4367. {
  4368. nullpo_retr(1, sd);
  4369. if(n < 0 || n >= MAX_INVENTORY)
  4370. return false;
  4371. if(amount <= 0)
  4372. return false;
  4373. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4374. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4375. sd->inventory.u.items_inventory[n].amount < amount ||
  4376. sd->state.trading || sd->state.vending ||
  4377. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4378. )
  4379. return false;
  4380. if( sd->inventory.u.items_inventory[n].equipSwitch )
  4381. return false;
  4382. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4383. {
  4384. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4385. return false; //Can't drop items in nodrop mapflag maps.
  4386. }
  4387. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4388. {
  4389. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4390. return false;
  4391. }
  4392. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4393. return false;
  4394. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4395. clif_dropitem(sd, n, amount);
  4396. return true;
  4397. }
  4398. /*==========================================
  4399. * Attempt to pick up an item.
  4400. * @param sd
  4401. * @param fitem Item that will be picked
  4402. * @return False = fail; True = success
  4403. *------------------------------------------*/
  4404. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4405. {
  4406. int flag = 0;
  4407. t_tick tick = gettick();
  4408. struct party_data *p = NULL;
  4409. nullpo_ret(sd);
  4410. nullpo_ret(fitem);
  4411. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4412. return false; // Distance is too far
  4413. if (sd->sc.cant.pickup)
  4414. return false;
  4415. if (sd->status.party_id)
  4416. p = party_search(sd->status.party_id);
  4417. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4418. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4419. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4420. if (!(p && p->party.item&1 &&
  4421. first_sd && first_sd->status.party_id == sd->status.party_id
  4422. ))
  4423. return false;
  4424. }
  4425. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4426. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4427. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4428. if (!(p && p->party.item&1 &&
  4429. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4430. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4431. ))
  4432. return false;
  4433. }
  4434. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4435. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4436. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4437. if(!(p && p->party.item&1 &&
  4438. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4439. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4440. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4441. ))
  4442. return false;
  4443. }
  4444. }
  4445. }
  4446. }
  4447. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4448. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4449. clif_additem(sd,0,0,flag);
  4450. return true;
  4451. }
  4452. //Display pickup animation.
  4453. pc_stop_attack(sd);
  4454. clif_takeitem(&sd->bl,&fitem->bl);
  4455. if (fitem->mob_id &&
  4456. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4457. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4458. )
  4459. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4460. map_clearflooritem(&fitem->bl);
  4461. return true;
  4462. }
  4463. /*==========================================
  4464. * Check if item is usable.
  4465. * Return:
  4466. * false = no
  4467. * true = yes
  4468. *------------------------------------------*/
  4469. bool pc_isUseitem(struct map_session_data *sd,int n)
  4470. {
  4471. struct item_data *item;
  4472. unsigned short nameid;
  4473. nullpo_ret(sd);
  4474. item = sd->inventory_data[n];
  4475. nameid = sd->inventory.u.items_inventory[n].nameid;
  4476. if( item == NULL )
  4477. return false;
  4478. //Not consumable item
  4479. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4480. return false;
  4481. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4482. return true;
  4483. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4484. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4485. return false;
  4486. //Prevent mass item usage. [Skotlex]
  4487. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4488. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4489. )
  4490. return false;
  4491. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4492. clif_msg(sd,ITEM_NOUSE_SITTING);
  4493. return false; // You cannot use this item while sitting.
  4494. }
  4495. if (sd->state.storage_flag && item->type != IT_CASH) {
  4496. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4497. return false; // You cannot use this item while storage is open.
  4498. }
  4499. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4500. return false;
  4501. switch( nameid ) {
  4502. case ITEMID_WING_OF_FLY:
  4503. case ITEMID_GIANT_FLY_WING:
  4504. case ITEMID_N_FLY_WING:
  4505. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4506. clif_skill_teleportmessage(sd,0);
  4507. return false;
  4508. }
  4509. if (nameid == ITEMID_GIANT_FLY_WING) {
  4510. struct party_data *pd = party_search(sd->status.party_id);
  4511. if (pd) {
  4512. int i;
  4513. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4514. if (i == MAX_PARTY) { // User is not party leader
  4515. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4516. break;
  4517. }
  4518. 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));
  4519. if (i == MAX_PARTY) { // No party members found on same map
  4520. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4521. break;
  4522. }
  4523. } else {
  4524. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4525. break;
  4526. }
  4527. }
  4528. // Fall through
  4529. case ITEMID_WING_OF_BUTTERFLY:
  4530. case ITEMID_N_BUTTERFLY_WING:
  4531. case ITEMID_DUN_TELE_SCROLL1:
  4532. case ITEMID_DUN_TELE_SCROLL2:
  4533. case ITEMID_DUN_TELE_SCROLL3:
  4534. case ITEMID_WOB_RUNE:
  4535. case ITEMID_WOB_SCHWALTZ:
  4536. case ITEMID_WOB_RACHEL:
  4537. case ITEMID_WOB_LOCAL:
  4538. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4539. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4540. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4541. return false;
  4542. }
  4543. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4544. return false;
  4545. break;
  4546. case ITEMID_MERCENARY_RED_POTION:
  4547. case ITEMID_MERCENARY_BLUE_POTION:
  4548. case ITEMID_M_CENTER_POTION:
  4549. case ITEMID_M_AWAKENING_POTION:
  4550. case ITEMID_M_BERSERK_POTION:
  4551. if( sd->md == NULL || sd->md->db == NULL )
  4552. return false;
  4553. if( sd->md->sc.data[SC_BERSERK] )
  4554. return false;
  4555. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4556. return false;
  4557. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4558. return false;
  4559. break;
  4560. case ITEMID_NEURALIZER:
  4561. if( !mapdata->flag[MF_RESET] )
  4562. return false;
  4563. break;
  4564. }
  4565. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4566. return false; // Mercenary Scrolls
  4567. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4568. if( pc_is90overweight(sd) ) {
  4569. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4570. return false;
  4571. }
  4572. if( !pc_inventoryblank(sd) ) {
  4573. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4574. return false;
  4575. }
  4576. }
  4577. //Gender check
  4578. if(item->sex != 2 && sd->status.sex != item->sex)
  4579. return false;
  4580. //Required level check
  4581. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4582. return false;
  4583. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4584. return false;
  4585. //Not equipable by class. [Skotlex]
  4586. if (!(
  4587. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4588. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4589. ))
  4590. return false;
  4591. if (sd->sc.count && (
  4592. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4593. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4594. sd->sc.data[SC_TRICKDEAD] ||
  4595. sd->sc.data[SC_HIDING] ||
  4596. sd->sc.data[SC__SHADOWFORM] ||
  4597. sd->sc.data[SC__INVISIBILITY] ||
  4598. sd->sc.data[SC__MANHOLE] ||
  4599. sd->sc.data[SC_DEEPSLEEP] ||
  4600. sd->sc.data[SC_CRYSTALIZE] ||
  4601. sd->sc.data[SC_KAGEHUMI] ||
  4602. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4603. sd->sc.data[SC_KINGS_GRACE] ||
  4604. sd->sc.data[SC_SUHIDE]))
  4605. return false;
  4606. if (!pc_isItemClass(sd,item))
  4607. return false;
  4608. //Dead Branch items
  4609. if( item->flag.dead_branch )
  4610. log_branch(sd);
  4611. return true;
  4612. }
  4613. /*==========================================
  4614. * Last checks to use an item.
  4615. * Return:
  4616. * 0 = fail
  4617. * 1 = success
  4618. *------------------------------------------*/
  4619. int pc_useitem(struct map_session_data *sd,int n)
  4620. {
  4621. t_tick tick = gettick();
  4622. int amount;
  4623. unsigned short nameid;
  4624. struct script_code *script;
  4625. struct item item;
  4626. struct item_data *id;
  4627. nullpo_ret(sd);
  4628. if (sd->npc_id) {
  4629. if (sd->progressbar.npc_id) {
  4630. clif_progressbar_abort(sd);
  4631. return 0; // First item use attempt cancels the progress bar
  4632. }
  4633. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4634. #ifdef RENEWAL
  4635. clif_msg( sd, WORK_IN_PROGRESS );
  4636. #endif
  4637. return 0;
  4638. }
  4639. }
  4640. item = sd->inventory.u.items_inventory[n];
  4641. id = sd->inventory_data[n];
  4642. if (item.nameid == 0 || item.amount <= 0)
  4643. return 0;
  4644. if( !pc_isUseitem(sd,n) )
  4645. return 0;
  4646. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4647. nameid = id->nameid;
  4648. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4649. return 0;
  4650. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4651. if( id->flag.delay_consume ) {
  4652. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4653. return 0;
  4654. else if( pc_issit(sd) )
  4655. return 0;
  4656. }
  4657. //Since most delay-consume items involve using a "skill-type" target cursor,
  4658. //perform a skill-use check before going through. [Skotlex]
  4659. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4660. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  4661. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4662. return 0;
  4663. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4664. return 0;
  4665. /* on restricted maps the item is consumed but the effect is not used */
  4666. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4667. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4668. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4669. clif_useitemack(sd,n,item.amount-1,true);
  4670. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4671. }
  4672. return 0;/* regardless, effect is not run */
  4673. }
  4674. sd->itemid = item.nameid;
  4675. sd->itemindex = n;
  4676. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4677. sd->catch_target_class = PET_CATCH_FAIL;
  4678. amount = item.amount;
  4679. script = id->script;
  4680. //Check if the item is to be consumed immediately [Skotlex]
  4681. if (id->flag.delay_consume)
  4682. clif_useitemack(sd, n, amount, true);
  4683. else
  4684. {
  4685. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4686. {
  4687. clif_useitemack(sd, n, amount - 1, true);
  4688. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4689. }
  4690. else
  4691. clif_useitemack(sd, n, 0, false);
  4692. }
  4693. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4694. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4695. //Update item use time.
  4696. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4697. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4698. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4699. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4700. potion_flag = 0;
  4701. return 1;
  4702. }
  4703. /**
  4704. * Add item on cart for given index.
  4705. * @param sd
  4706. * @param item
  4707. * @param amount
  4708. * @param log_type
  4709. * @return See pc.hpp::e_additem_result
  4710. */
  4711. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4712. {
  4713. struct item_data *data;
  4714. int i,w;
  4715. nullpo_retr(ADDITEM_INVALID, sd);
  4716. nullpo_retr(ADDITEM_INVALID, item);
  4717. if(item->nameid == 0 || amount <= 0)
  4718. return ADDITEM_INVALID;
  4719. if (itemdb_ishatched_egg(item))
  4720. return ADDITEM_INVALID;
  4721. data = itemdb_search(item->nameid);
  4722. if( data->stack.cart && amount > data->stack.amount )
  4723. {// item stack limitation
  4724. return ADDITEM_STACKLIMIT;
  4725. }
  4726. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4727. { // Check item trade restrictions [Skotlex]
  4728. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4729. return ADDITEM_INVALID;
  4730. }
  4731. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4732. return ADDITEM_OVERWEIGHT;
  4733. i = MAX_CART;
  4734. if( itemdb_isstackable2(data) && !item->expire_time )
  4735. {
  4736. for (i = 0; i < MAX_CART; i++) {
  4737. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4738. && sd->cart.u.items_cart[i].bound == item->bound
  4739. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4740. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4741. )
  4742. break;
  4743. }
  4744. }
  4745. if( i < MAX_CART )
  4746. {// item already in cart, stack it
  4747. 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 ) )
  4748. return ADDITEM_OVERAMOUNT; // no slot
  4749. sd->cart.u.items_cart[i].amount += amount;
  4750. clif_cart_additem(sd,i,amount,0);
  4751. }
  4752. else
  4753. {// item not stackable or not present, add it
  4754. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4755. if( i == MAX_CART )
  4756. return ADDITEM_OVERAMOUNT; // no slot
  4757. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4758. sd->cart.u.items_cart[i].id = 0;
  4759. sd->cart.u.items_cart[i].amount = amount;
  4760. sd->cart_num++;
  4761. clif_cart_additem(sd,i,amount,0);
  4762. }
  4763. sd->cart.u.items_cart[i].favorite = 0; // clear
  4764. sd->cart.u.items_cart[i].equipSwitch = 0;
  4765. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4766. sd->cart_weight += w;
  4767. clif_updatestatus(sd,SP_CARTINFO);
  4768. return ADDITEM_SUCCESS;
  4769. }
  4770. /*==========================================
  4771. * Delete item on cart for given index.
  4772. *------------------------------------------*/
  4773. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4774. {
  4775. nullpo_retv(sd);
  4776. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4777. sd->cart.u.items_cart[n].amount < amount)
  4778. return;
  4779. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4780. sd->cart.u.items_cart[n].amount -= amount;
  4781. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4782. if(sd->cart.u.items_cart[n].amount <= 0) {
  4783. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4784. sd->cart_num--;
  4785. }
  4786. if(!type) {
  4787. clif_cart_delitem(sd,n,amount);
  4788. clif_updatestatus(sd,SP_CARTINFO);
  4789. }
  4790. }
  4791. /*==========================================
  4792. * Transfer item from inventory to cart.
  4793. *------------------------------------------*/
  4794. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4795. {
  4796. nullpo_retv(sd);
  4797. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4798. return;
  4799. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  4800. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  4801. return;
  4802. if( item_data->equipSwitch ){
  4803. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  4804. return;
  4805. }
  4806. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  4807. if (flag == ADDITEM_SUCCESS)
  4808. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4809. else {
  4810. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  4811. clif_additem(sd, idx, amount, 0);
  4812. clif_delitem(sd, idx, amount, 0);
  4813. }
  4814. }
  4815. /*==========================================
  4816. * Get number of item in cart.
  4817. * Return:
  4818. -1 = itemid not found or no amount found
  4819. x = remaining itemid on cart after get
  4820. *------------------------------------------*/
  4821. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4822. {
  4823. struct item* item_data;
  4824. nullpo_retr(-1, sd);
  4825. item_data = &sd->cart.u.items_cart[idx];
  4826. if( item_data->nameid == 0 || item_data->amount == 0 )
  4827. return -1;
  4828. return item_data->amount - amount;
  4829. }
  4830. /*==========================================
  4831. * Retrieve an item at index idx from cart.
  4832. *------------------------------------------*/
  4833. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4834. {
  4835. nullpo_retv(sd);
  4836. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4837. return;
  4838. struct item *item_data=&sd->cart.u.items_cart[idx];
  4839. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  4840. return;
  4841. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  4842. if (flag == ADDITEM_SUCCESS)
  4843. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  4844. else {
  4845. clif_cart_delitem(sd, idx, amount);
  4846. clif_additem(sd, idx, amount, flag);
  4847. clif_cart_additem(sd, idx, amount, 0);
  4848. }
  4849. }
  4850. /*==========================================
  4851. * Bound Item Check
  4852. * Type:
  4853. * 1 Account Bound
  4854. * 2 Guild Bound
  4855. * 3 Party Bound
  4856. * 4 Character Bound
  4857. *------------------------------------------*/
  4858. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4859. {
  4860. int i = 0, j = 0;
  4861. for(i = 0; i < MAX_INVENTORY; i++) {
  4862. 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) {
  4863. idxlist[j] = i;
  4864. j++;
  4865. }
  4866. }
  4867. return j;
  4868. }
  4869. /*==========================================
  4870. * Display item stolen msg to player sd
  4871. *------------------------------------------*/
  4872. int pc_show_steal(struct block_list *bl,va_list ap)
  4873. {
  4874. struct map_session_data *sd;
  4875. int itemid;
  4876. struct item_data *item=NULL;
  4877. char output[100];
  4878. sd=va_arg(ap,struct map_session_data *);
  4879. itemid=va_arg(ap,int);
  4880. if((item=itemdb_exists(itemid))==NULL)
  4881. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4882. else
  4883. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4884. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4885. return 0;
  4886. }
  4887. /**
  4888. * Steal an item from bl (mob).
  4889. * @param sd: Player data
  4890. * @param bl: Object to steal from
  4891. * @param skill_lv: Level of skill used
  4892. * @return True on success or false otherwise
  4893. */
  4894. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4895. {
  4896. int i,itemid;
  4897. double rate;
  4898. unsigned char flag = 0;
  4899. struct status_data *sd_status, *md_status;
  4900. struct mob_data *md;
  4901. struct item tmp_item;
  4902. if(!sd || !bl || bl->type!=BL_MOB)
  4903. return false;
  4904. md = (TBL_MOB *)bl;
  4905. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4906. return false;
  4907. sd_status= status_get_status_data(&sd->bl);
  4908. md_status= status_get_status_data(bl);
  4909. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4910. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  4911. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4912. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4913. ) { //Can't steal from
  4914. md->state.steal_flag = UCHAR_MAX;
  4915. return false;
  4916. }
  4917. // base skill success chance (percentual)
  4918. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4919. rate += sd->bonus.add_steal_rate;
  4920. if( rate < 1
  4921. #ifdef RENEWAL
  4922. || rnd()%100 >= rate
  4923. #endif
  4924. )
  4925. return false;
  4926. // Try dropping one item, in the order from first to last possible slot.
  4927. // Droprate is affected by the skill success rate.
  4928. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4929. 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
  4930. #ifndef RENEWAL
  4931. * rate/100.
  4932. #endif
  4933. )
  4934. break;
  4935. if( i == MAX_STEAL_DROP )
  4936. return false;
  4937. itemid = md->db->dropitem[i].nameid;
  4938. memset(&tmp_item,0,sizeof(tmp_item));
  4939. tmp_item.nameid = itemid;
  4940. tmp_item.amount = 1;
  4941. tmp_item.identify = itemdb_isidentified(itemid);
  4942. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  4943. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4944. //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?
  4945. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4946. if(flag) { //Failed to steal due to overweight
  4947. clif_additem(sd,0,0,flag);
  4948. return false;
  4949. }
  4950. if(battle_config.show_steal_in_same_party)
  4951. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4952. //Logs items, Stolen from mobs [Lupus]
  4953. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4954. //A Rare Steal Global Announce by Lupus
  4955. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4956. struct item_data *i_data;
  4957. char message[128];
  4958. i_data = itemdb_search(itemid);
  4959. sprintf (message, msg_txt(sd,542), (sd->status.name[0])?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
  4960. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4961. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4962. }
  4963. return true;
  4964. }
  4965. /*==========================================
  4966. * Stole zeny from bl (mob)
  4967. * return
  4968. * 0 = fail
  4969. * 1 = success
  4970. *------------------------------------------*/
  4971. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4972. {
  4973. int rate, target_lv;
  4974. struct mob_data *md;
  4975. if(!sd || !target || target->type != BL_MOB)
  4976. return 0;
  4977. md = (TBL_MOB*)target;
  4978. target_lv = status_get_lv(target);
  4979. 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)
  4980. return 0;
  4981. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  4982. if(rnd()%1000 < rate)
  4983. {
  4984. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  4985. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  4986. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4987. md->state.steal_coin_flag = 1;
  4988. return 1;
  4989. }
  4990. return 0;
  4991. }
  4992. /*==========================================
  4993. * Set's a player position.
  4994. * @param sd
  4995. * @param mapindex
  4996. * @param x
  4997. * @param y
  4998. * @param clrtype
  4999. * @return SETPOS_OK Success
  5000. * SETPOS_MAPINDEX Invalid map index
  5001. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5002. * SETPOS_AUTOTRADE Player is in autotrade state
  5003. *------------------------------------------*/
  5004. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5005. {
  5006. nullpo_retr(SETPOS_OK,sd);
  5007. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5008. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5009. return SETPOS_MAPINDEX;
  5010. }
  5011. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5012. return SETPOS_AUTOTRADE;
  5013. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5014. pc_setstand(sd, true);
  5015. pc_setrestartvalue(sd,1);
  5016. }
  5017. int16 m = map_mapindex2mapid(mapindex);
  5018. struct map_data *mapdata = map_getmapdata(m);
  5019. sd->state.changemap = (sd->mapindex != mapindex);
  5020. sd->state.warping = 1;
  5021. sd->state.workinprogress = WIP_DISABLE_NONE;
  5022. if( sd->state.changemap ) { // Misc map-changing settings
  5023. unsigned short curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5024. if (curr_map_instance_id != new_map_instance_id) {
  5025. if (curr_map_instance_id) // Update instance timer for the map on leave
  5026. instance_delusers(curr_map_instance_id);
  5027. if (new_map_instance_id) // Update instance timer for the map on enter
  5028. instance_addusers(new_map_instance_id);
  5029. }
  5030. if (sd->bg_id && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5031. bg_team_leave(sd, false, true);
  5032. sd->state.pmap = sd->bl.m;
  5033. if (sd->sc.count) { // Cancel some map related stuff.
  5034. if (sd->sc.data[SC_JAILED])
  5035. return SETPOS_MAPINDEX; //You may not get out!
  5036. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  5037. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  5038. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  5039. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  5040. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  5041. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  5042. if (sd->sc.data[SC_KNOWLEDGE]) {
  5043. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  5044. if (sce->timer != INVALID_TIMER)
  5045. delete_timer(sce->timer, status_change_timer);
  5046. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  5047. }
  5048. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  5049. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  5050. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  5051. }
  5052. for(int i = 0; i < EQI_MAX; i++ ) {
  5053. if( sd->equip_index[i] >= 0 )
  5054. if( pc_isequip(sd,sd->equip_index[i]) )
  5055. pc_unequipitem(sd,sd->equip_index[i],2);
  5056. }
  5057. if (battle_config.clear_unit_onwarp&BL_PC)
  5058. skill_clear_unitgroup(&sd->bl);
  5059. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5060. guild_send_dot_remove(sd);
  5061. bg_send_dot_remove(sd);
  5062. if (sd->regen.state.gc)
  5063. sd->regen.state.gc = 0;
  5064. // make sure vending is allowed here
  5065. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5066. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  5067. vending_closevending(sd);
  5068. }
  5069. channel_pcquit(sd,4); //quit map chan
  5070. }
  5071. if( m < 0 )
  5072. {
  5073. uint32 ip;
  5074. uint16 port;
  5075. struct script_state *st;
  5076. //if can't find any map-servers, just abort setting position.
  5077. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5078. return SETPOS_NO_MAPSERVER;
  5079. if (sd->npc_id){
  5080. npc_event_dequeue(sd,false);
  5081. st = sd->st;
  5082. }else{
  5083. st = nullptr;
  5084. }
  5085. npc_script_event(sd, NPCE_LOGOUT);
  5086. //remove from map, THEN change x/y coordinates
  5087. unit_remove_map_pc(sd,clrtype);
  5088. sd->mapindex = mapindex;
  5089. sd->bl.x=x;
  5090. sd->bl.y=y;
  5091. pc_clean_skilltree(sd);
  5092. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5093. chrif_changemapserver(sd, ip, (short)port);
  5094. //Free session data from this map server [Kevin]
  5095. unit_free_pc(sd);
  5096. if( st ){
  5097. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5098. st->state = END;
  5099. }
  5100. return SETPOS_OK;
  5101. }
  5102. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5103. {
  5104. 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);
  5105. x = y = 0; // make it random
  5106. }
  5107. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5108. int c=0;
  5109. do {
  5110. x = rnd()%(mapdata->xs-2)+1;
  5111. y = rnd()%(mapdata->ys-2)+1;
  5112. c++;
  5113. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5114. 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));
  5115. return SETPOS_OK; //preventing warp
  5116. //break; //allow warp anyway
  5117. }
  5118. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5119. }
  5120. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5121. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5122. vending_closevending(sd);
  5123. }
  5124. if(sd->bl.prev != NULL){
  5125. unit_remove_map_pc(sd,clrtype);
  5126. clif_changemap(sd,m,x,y); // [MouseJstr]
  5127. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5128. sd->state.rewarp = 1;
  5129. sd->mapindex = mapindex;
  5130. sd->bl.m = m;
  5131. sd->bl.x = sd->ud.to_x = x;
  5132. sd->bl.y = sd->ud.to_y = y;
  5133. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5134. { // Increased guild castle regen [Valaris]
  5135. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5136. if(gc && gc->guild_id == sd->status.guild_id)
  5137. sd->regen.state.gc = 1;
  5138. }
  5139. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5140. {
  5141. sd->pd->bl.m = m;
  5142. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5143. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5144. sd->pd->ud.dir = sd->ud.dir;
  5145. }
  5146. if( hom_is_active(sd->hd) )
  5147. {
  5148. sd->hd->bl.m = m;
  5149. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5150. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5151. sd->hd->ud.dir = sd->ud.dir;
  5152. }
  5153. if( sd->md )
  5154. {
  5155. sd->md->bl.m = m;
  5156. sd->md->bl.x = sd->md->ud.to_x = x;
  5157. sd->md->bl.y = sd->md->ud.to_y = y;
  5158. sd->md->ud.dir = sd->ud.dir;
  5159. }
  5160. if( sd->ed ) {
  5161. sd->ed->bl.m = m;
  5162. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5163. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5164. sd->ed->ud.dir = sd->ud.dir;
  5165. }
  5166. pc_cell_basilica(sd);
  5167. //check if we gonna be rewarped [lighta]
  5168. if(npc_check_areanpc(1,m,x,y,0)){
  5169. sd->count_rewarp++;
  5170. }
  5171. else
  5172. sd->count_rewarp = 0;
  5173. return SETPOS_OK;
  5174. }
  5175. /*==========================================
  5176. * Warp player sd to random location on current map.
  5177. * May fail if no walkable cell found (1000 attempts).
  5178. * Return:
  5179. * 0 = Success
  5180. * 1,2,3 = Fail
  5181. *------------------------------------------*/
  5182. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5183. {
  5184. int x,y,i=0;
  5185. nullpo_ret(sd);
  5186. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5187. if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
  5188. return 3;
  5189. do {
  5190. x = rnd()%(mapdata->xs-2)+1;
  5191. y = rnd()%(mapdata->ys-2)+1;
  5192. } 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);
  5193. if (i < 1000)
  5194. return pc_setpos(sd,mapdata->index,x,y,type);
  5195. return 3;
  5196. }
  5197. /*==========================================
  5198. * Records a memo point at sd's current position
  5199. * pos - entry to replace, (-1: shift oldest entry out)
  5200. *------------------------------------------*/
  5201. bool pc_memo(struct map_session_data* sd, int pos)
  5202. {
  5203. int skill;
  5204. nullpo_ret(sd);
  5205. // check mapflags
  5206. 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) ) {
  5207. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5208. return false;
  5209. }
  5210. // check inputs
  5211. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5212. return false; // invalid input
  5213. // check required skill level
  5214. skill = pc_checkskill(sd, AL_WARP);
  5215. if( skill < 1 ) {
  5216. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5217. return false;
  5218. }
  5219. if( skill < 2 || skill - 2 < pos ) {
  5220. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5221. return false;
  5222. }
  5223. if( pos == -1 )
  5224. {
  5225. uint8 i;
  5226. // prevent memo-ing the same map multiple times
  5227. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5228. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5229. pos = 0;
  5230. }
  5231. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5232. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5233. return false;
  5234. }
  5235. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5236. sd->status.memo_point[pos].x = sd->bl.x;
  5237. sd->status.memo_point[pos].y = sd->bl.y;
  5238. clif_skill_memomessage(sd, 0);
  5239. return true;
  5240. }
  5241. //
  5242. // Skills
  5243. //
  5244. /**
  5245. * Get the skill current cooldown for player.
  5246. * (get the db base cooldown for skill + player specific cooldown)
  5247. * @param sd : player pointer
  5248. * @param id : skill id
  5249. * @param lv : skill lv
  5250. * @return player skill cooldown
  5251. */
  5252. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5253. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  5254. if (cooldown == 0)
  5255. return 0;
  5256. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5257. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5258. for (auto &it : sd->skillcooldown) {
  5259. if (it.id == skill_id) {
  5260. cooldown += it.val;
  5261. cooldown = max(0, cooldown);
  5262. break;
  5263. }
  5264. }
  5265. return cooldown;
  5266. }
  5267. /*==========================================
  5268. * Return player sd skill_lv learned for given skill
  5269. *------------------------------------------*/
  5270. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5271. {
  5272. uint16 idx = 0;
  5273. if (sd == NULL)
  5274. return 0;
  5275. if ((idx = skill_get_index(skill_id)) == 0) {
  5276. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5277. return 0;
  5278. }
  5279. if (SKILL_CHK_GUILD(skill_id) ) {
  5280. struct guild *g;
  5281. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5282. return guild_checkskill(g,skill_id);
  5283. return 0;
  5284. }
  5285. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5286. }
  5287. /**
  5288. * Check if we still have the correct weapon to continue the skill (actually status)
  5289. * If not ending it
  5290. * @param sd
  5291. * @return 0:error, 1:check done
  5292. */
  5293. static void pc_checkallowskill(struct map_session_data *sd)
  5294. {
  5295. const enum sc_type scw_list[] = {
  5296. SC_TWOHANDQUICKEN,
  5297. SC_ONEHAND,
  5298. SC_AURABLADE,
  5299. SC_PARRYING,
  5300. SC_SPEARQUICKEN,
  5301. SC_ADRENALINE,
  5302. SC_ADRENALINE2,
  5303. SC_DANCING,
  5304. SC_GATLINGFEVER,
  5305. };
  5306. uint8 i;
  5307. nullpo_retv(sd);
  5308. if(!sd->sc.count)
  5309. return;
  5310. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5311. { // Skills requiring specific weapon types
  5312. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5313. continue;
  5314. if(sd->sc.data[scw_list[i]] &&
  5315. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5316. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5317. }
  5318. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5319. // Spurt requires bare hands (feet, in fact xD)
  5320. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5321. if(sd->status.shield <= 0) { // Skills requiring a shield
  5322. const enum sc_type scs_list[] = {
  5323. SC_AUTOGUARD,
  5324. SC_DEFENDER,
  5325. SC_REFLECTSHIELD,
  5326. SC_REFLECTDAMAGE
  5327. };
  5328. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5329. if(sd->sc.data[scs_list[i]])
  5330. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5331. }
  5332. }
  5333. /*==========================================
  5334. * Return equipped index of item on player sd at pos
  5335. * Return
  5336. * -1 : Nothing equipped
  5337. * idx : (this index could be used in inventory to found item_data)
  5338. *------------------------------------------*/
  5339. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  5340. {
  5341. uint8 i;
  5342. nullpo_retr(-1, sd);
  5343. for(i=0;i<EQI_MAX;i++){
  5344. if(pos & equip_bitmask[i]){
  5345. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  5346. // Check all if any match is found
  5347. continue;
  5348. }
  5349. return sd->equip_index[i];
  5350. }
  5351. }
  5352. return -1;
  5353. }
  5354. /*==========================================
  5355. * Check if sd has nameid equipped somewhere
  5356. * @sd : the player session
  5357. * @nameid : id of the item to check
  5358. * @min : : see pc.hpp enum equip_index from ? to @max
  5359. * @max : see pc.hpp enum equip_index for @min to ?
  5360. * -return true,false
  5361. *------------------------------------------*/
  5362. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5363. {
  5364. int i;
  5365. for(i = min; i < max; i++) {
  5366. if(equip_bitmask[i]) {
  5367. int idx = sd->equip_index[i];
  5368. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5369. return true;
  5370. }
  5371. }
  5372. return false;
  5373. }
  5374. /*==========================================
  5375. * Convert's from the client's lame Job ID system
  5376. * to the map server's 'makes sense' system. [Skotlex]
  5377. *------------------------------------------*/
  5378. int pc_jobid2mapid(unsigned short b_class)
  5379. {
  5380. switch(b_class)
  5381. {
  5382. //Novice And 1-1 Jobs
  5383. case JOB_NOVICE: return MAPID_NOVICE;
  5384. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5385. case JOB_MAGE: return MAPID_MAGE;
  5386. case JOB_ARCHER: return MAPID_ARCHER;
  5387. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5388. case JOB_MERCHANT: return MAPID_MERCHANT;
  5389. case JOB_THIEF: return MAPID_THIEF;
  5390. case JOB_TAEKWON: return MAPID_TAEKWON;
  5391. case JOB_WEDDING: return MAPID_WEDDING;
  5392. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5393. case JOB_NINJA: return MAPID_NINJA;
  5394. case JOB_XMAS: return MAPID_XMAS;
  5395. case JOB_SUMMER: return MAPID_SUMMER;
  5396. case JOB_HANBOK: return MAPID_HANBOK;
  5397. case JOB_GANGSI: return MAPID_GANGSI;
  5398. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5399. case JOB_SUMMER2: return MAPID_SUMMER2;
  5400. //2-1 Jobs
  5401. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5402. case JOB_KNIGHT: return MAPID_KNIGHT;
  5403. case JOB_WIZARD: return MAPID_WIZARD;
  5404. case JOB_HUNTER: return MAPID_HUNTER;
  5405. case JOB_PRIEST: return MAPID_PRIEST;
  5406. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5407. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5408. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5409. case JOB_KAGEROU:
  5410. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5411. case JOB_REBELLION: return MAPID_REBELLION;
  5412. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5413. //2-2 Jobs
  5414. case JOB_CRUSADER: return MAPID_CRUSADER;
  5415. case JOB_SAGE: return MAPID_SAGE;
  5416. case JOB_BARD:
  5417. case JOB_DANCER: return MAPID_BARDDANCER;
  5418. case JOB_MONK: return MAPID_MONK;
  5419. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5420. case JOB_ROGUE: return MAPID_ROGUE;
  5421. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5422. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5423. //Trans Novice And Trans 1-1 Jobs
  5424. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5425. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5426. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5427. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5428. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5429. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5430. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5431. //Trans 2-1 Jobs
  5432. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5433. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5434. case JOB_SNIPER: return MAPID_SNIPER;
  5435. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5436. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5437. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5438. //Trans 2-2 Jobs
  5439. case JOB_PALADIN: return MAPID_PALADIN;
  5440. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5441. case JOB_CLOWN:
  5442. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5443. case JOB_CHAMPION: return MAPID_CHAMPION;
  5444. case JOB_CREATOR: return MAPID_CREATOR;
  5445. case JOB_STALKER: return MAPID_STALKER;
  5446. //Baby Novice And Baby 1-1 Jobs
  5447. case JOB_BABY: return MAPID_BABY;
  5448. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5449. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5450. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5451. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5452. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5453. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5454. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5455. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5456. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5457. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5458. //Baby 2-1 Jobs
  5459. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5460. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5461. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5462. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5463. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5464. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5465. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5466. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5467. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5468. case JOB_BABY_KAGEROU:
  5469. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5470. //Baby 2-2 Jobs
  5471. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5472. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5473. case JOB_BABY_BARD:
  5474. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5475. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5476. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5477. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5478. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5479. //3-1 Jobs
  5480. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5481. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5482. case JOB_WARLOCK: return MAPID_WARLOCK;
  5483. case JOB_RANGER: return MAPID_RANGER;
  5484. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5485. case JOB_MECHANIC: return MAPID_MECHANIC;
  5486. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5487. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5488. //3-2 Jobs
  5489. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5490. case JOB_SORCERER: return MAPID_SORCERER;
  5491. case JOB_MINSTREL:
  5492. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5493. case JOB_SURA: return MAPID_SURA;
  5494. case JOB_GENETIC: return MAPID_GENETIC;
  5495. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5496. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5497. //Trans 3-1 Jobs
  5498. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5499. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5500. case JOB_RANGER_T: return MAPID_RANGER_T;
  5501. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5502. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5503. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5504. //Trans 3-2 Jobs
  5505. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5506. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5507. case JOB_MINSTREL_T:
  5508. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5509. case JOB_SURA_T: return MAPID_SURA_T;
  5510. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5511. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5512. //Baby 3-1 Jobs
  5513. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5514. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  5515. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5516. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5517. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  5518. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5519. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  5520. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5521. //Baby 3-2 Jobs
  5522. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  5523. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5524. case JOB_BABY_MINSTREL:
  5525. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5526. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5527. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5528. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  5529. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5530. //Doram Jobs
  5531. case JOB_SUMMONER: return MAPID_SUMMONER;
  5532. default:
  5533. return -1;
  5534. }
  5535. }
  5536. //Reverts the map-style class id to the client-style one.
  5537. int pc_mapid2jobid(unsigned short class_, int sex)
  5538. {
  5539. switch(class_) {
  5540. //Novice And 1-1 Jobs
  5541. case MAPID_NOVICE: return JOB_NOVICE;
  5542. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5543. case MAPID_MAGE: return JOB_MAGE;
  5544. case MAPID_ARCHER: return JOB_ARCHER;
  5545. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5546. case MAPID_MERCHANT: return JOB_MERCHANT;
  5547. case MAPID_THIEF: return JOB_THIEF;
  5548. case MAPID_TAEKWON: return JOB_TAEKWON;
  5549. case MAPID_WEDDING: return JOB_WEDDING;
  5550. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5551. case MAPID_NINJA: return JOB_NINJA;
  5552. case MAPID_XMAS: return JOB_XMAS;
  5553. case MAPID_SUMMER: return JOB_SUMMER;
  5554. case MAPID_HANBOK: return JOB_HANBOK;
  5555. case MAPID_GANGSI: return JOB_GANGSI;
  5556. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5557. case MAPID_SUMMER2: return JOB_SUMMER2;
  5558. //2-1 Jobs
  5559. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5560. case MAPID_KNIGHT: return JOB_KNIGHT;
  5561. case MAPID_WIZARD: return JOB_WIZARD;
  5562. case MAPID_HUNTER: return JOB_HUNTER;
  5563. case MAPID_PRIEST: return JOB_PRIEST;
  5564. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5565. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5566. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5567. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5568. case MAPID_REBELLION: return JOB_REBELLION;
  5569. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5570. //2-2 Jobs
  5571. case MAPID_CRUSADER: return JOB_CRUSADER;
  5572. case MAPID_SAGE: return JOB_SAGE;
  5573. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5574. case MAPID_MONK: return JOB_MONK;
  5575. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5576. case MAPID_ROGUE: return JOB_ROGUE;
  5577. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5578. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5579. //Trans Novice And Trans 2-1 Jobs
  5580. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5581. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5582. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5583. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5584. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5585. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5586. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5587. //Trans 2-1 Jobs
  5588. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5589. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5590. case MAPID_SNIPER: return JOB_SNIPER;
  5591. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5592. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5593. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5594. //Trans 2-2 Jobs
  5595. case MAPID_PALADIN: return JOB_PALADIN;
  5596. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5597. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5598. case MAPID_CHAMPION: return JOB_CHAMPION;
  5599. case MAPID_CREATOR: return JOB_CREATOR;
  5600. case MAPID_STALKER: return JOB_STALKER;
  5601. //Baby Novice And Baby 1-1 Jobs
  5602. case MAPID_BABY: return JOB_BABY;
  5603. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5604. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5605. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5606. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5607. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5608. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5609. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5610. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5611. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5612. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5613. //Baby 2-1 Jobs
  5614. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5615. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5616. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5617. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5618. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5619. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5620. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5621. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5622. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5623. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5624. //Baby 2-2 Jobs
  5625. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5626. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5627. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5628. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5629. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5630. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5631. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5632. //3-1 Jobs
  5633. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5634. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5635. case MAPID_WARLOCK: return JOB_WARLOCK;
  5636. case MAPID_RANGER: return JOB_RANGER;
  5637. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5638. case MAPID_MECHANIC: return JOB_MECHANIC;
  5639. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5640. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5641. //3-2 Jobs
  5642. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5643. case MAPID_SORCERER: return JOB_SORCERER;
  5644. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5645. case MAPID_SURA: return JOB_SURA;
  5646. case MAPID_GENETIC: return JOB_GENETIC;
  5647. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5648. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5649. //Trans 3-1 Jobs
  5650. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5651. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5652. case MAPID_RANGER_T: return JOB_RANGER_T;
  5653. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5654. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5655. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5656. //Trans 3-2 Jobs
  5657. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5658. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5659. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5660. case MAPID_SURA_T: return JOB_SURA_T;
  5661. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5662. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5663. //Baby 3-1 Jobs
  5664. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5665. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  5666. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5667. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5668. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  5669. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5670. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  5671. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5672. //Baby 3-2 Jobs
  5673. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  5674. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5675. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5676. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5677. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5678. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  5679. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5680. //Doram Jobs
  5681. case MAPID_SUMMONER: return JOB_SUMMONER;
  5682. default:
  5683. return -1;
  5684. }
  5685. }
  5686. /*====================================================
  5687. * This function return the name of the job (by [Yor])
  5688. *----------------------------------------------------*/
  5689. const char* job_name(int class_)
  5690. {
  5691. switch (class_) {
  5692. case JOB_NOVICE:
  5693. case JOB_SWORDMAN:
  5694. case JOB_MAGE:
  5695. case JOB_ARCHER:
  5696. case JOB_ACOLYTE:
  5697. case JOB_MERCHANT:
  5698. case JOB_THIEF:
  5699. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5700. case JOB_KNIGHT:
  5701. case JOB_PRIEST:
  5702. case JOB_WIZARD:
  5703. case JOB_BLACKSMITH:
  5704. case JOB_HUNTER:
  5705. case JOB_ASSASSIN:
  5706. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5707. case JOB_KNIGHT2:
  5708. return msg_txt(NULL,557);
  5709. case JOB_CRUSADER:
  5710. case JOB_MONK:
  5711. case JOB_SAGE:
  5712. case JOB_ROGUE:
  5713. case JOB_ALCHEMIST:
  5714. case JOB_BARD:
  5715. case JOB_DANCER:
  5716. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5717. case JOB_CRUSADER2:
  5718. return msg_txt(NULL,563);
  5719. case JOB_WEDDING:
  5720. case JOB_SUPER_NOVICE:
  5721. case JOB_GUNSLINGER:
  5722. case JOB_NINJA:
  5723. case JOB_XMAS:
  5724. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5725. case JOB_SUMMER:
  5726. case JOB_SUMMER2:
  5727. return msg_txt(NULL,621);
  5728. case JOB_HANBOK:
  5729. return msg_txt(NULL,694);
  5730. case JOB_OKTOBERFEST:
  5731. return msg_txt(NULL,696);
  5732. case JOB_NOVICE_HIGH:
  5733. case JOB_SWORDMAN_HIGH:
  5734. case JOB_MAGE_HIGH:
  5735. case JOB_ARCHER_HIGH:
  5736. case JOB_ACOLYTE_HIGH:
  5737. case JOB_MERCHANT_HIGH:
  5738. case JOB_THIEF_HIGH:
  5739. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5740. case JOB_LORD_KNIGHT:
  5741. case JOB_HIGH_PRIEST:
  5742. case JOB_HIGH_WIZARD:
  5743. case JOB_WHITESMITH:
  5744. case JOB_SNIPER:
  5745. case JOB_ASSASSIN_CROSS:
  5746. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5747. case JOB_LORD_KNIGHT2:
  5748. return msg_txt(NULL,582);
  5749. case JOB_PALADIN:
  5750. case JOB_CHAMPION:
  5751. case JOB_PROFESSOR:
  5752. case JOB_STALKER:
  5753. case JOB_CREATOR:
  5754. case JOB_CLOWN:
  5755. case JOB_GYPSY:
  5756. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5757. case JOB_PALADIN2:
  5758. return msg_txt(NULL,588);
  5759. case JOB_BABY:
  5760. case JOB_BABY_SWORDMAN:
  5761. case JOB_BABY_MAGE:
  5762. case JOB_BABY_ARCHER:
  5763. case JOB_BABY_ACOLYTE:
  5764. case JOB_BABY_MERCHANT:
  5765. case JOB_BABY_THIEF:
  5766. return msg_txt(NULL,595 - JOB_BABY + class_);
  5767. case JOB_BABY_KNIGHT:
  5768. case JOB_BABY_PRIEST:
  5769. case JOB_BABY_WIZARD:
  5770. case JOB_BABY_BLACKSMITH:
  5771. case JOB_BABY_HUNTER:
  5772. case JOB_BABY_ASSASSIN:
  5773. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5774. case JOB_BABY_KNIGHT2:
  5775. return msg_txt(NULL,602);
  5776. case JOB_BABY_CRUSADER:
  5777. case JOB_BABY_MONK:
  5778. case JOB_BABY_SAGE:
  5779. case JOB_BABY_ROGUE:
  5780. case JOB_BABY_ALCHEMIST:
  5781. case JOB_BABY_BARD:
  5782. case JOB_BABY_DANCER:
  5783. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5784. case JOB_BABY_CRUSADER2:
  5785. return msg_txt(NULL,608);
  5786. case JOB_SUPER_BABY:
  5787. return msg_txt(NULL,615);
  5788. case JOB_TAEKWON:
  5789. return msg_txt(NULL,616);
  5790. case JOB_STAR_GLADIATOR:
  5791. case JOB_STAR_GLADIATOR2:
  5792. return msg_txt(NULL,617);
  5793. case JOB_SOUL_LINKER:
  5794. return msg_txt(NULL,618);
  5795. case JOB_GANGSI:
  5796. case JOB_DEATH_KNIGHT:
  5797. case JOB_DARK_COLLECTOR:
  5798. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5799. case JOB_RUNE_KNIGHT:
  5800. case JOB_WARLOCK:
  5801. case JOB_RANGER:
  5802. case JOB_ARCH_BISHOP:
  5803. case JOB_MECHANIC:
  5804. case JOB_GUILLOTINE_CROSS:
  5805. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5806. case JOB_RUNE_KNIGHT_T:
  5807. case JOB_WARLOCK_T:
  5808. case JOB_RANGER_T:
  5809. case JOB_ARCH_BISHOP_T:
  5810. case JOB_MECHANIC_T:
  5811. case JOB_GUILLOTINE_CROSS_T:
  5812. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5813. case JOB_ROYAL_GUARD:
  5814. case JOB_SORCERER:
  5815. case JOB_MINSTREL:
  5816. case JOB_WANDERER:
  5817. case JOB_SURA:
  5818. case JOB_GENETIC:
  5819. case JOB_SHADOW_CHASER:
  5820. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5821. case JOB_ROYAL_GUARD_T:
  5822. case JOB_SORCERER_T:
  5823. case JOB_MINSTREL_T:
  5824. case JOB_WANDERER_T:
  5825. case JOB_SURA_T:
  5826. case JOB_GENETIC_T:
  5827. case JOB_SHADOW_CHASER_T:
  5828. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5829. case JOB_RUNE_KNIGHT2:
  5830. case JOB_RUNE_KNIGHT_T2:
  5831. return msg_txt(NULL,625);
  5832. case JOB_ROYAL_GUARD2:
  5833. case JOB_ROYAL_GUARD_T2:
  5834. return msg_txt(NULL,631);
  5835. case JOB_RANGER2:
  5836. case JOB_RANGER_T2:
  5837. return msg_txt(NULL,627);
  5838. case JOB_MECHANIC2:
  5839. case JOB_MECHANIC_T2:
  5840. return msg_txt(NULL,629);
  5841. case JOB_BABY_RUNE_KNIGHT:
  5842. case JOB_BABY_WARLOCK:
  5843. case JOB_BABY_RANGER:
  5844. case JOB_BABY_ARCH_BISHOP:
  5845. case JOB_BABY_MECHANIC:
  5846. case JOB_BABY_GUILLOTINE_CROSS:
  5847. case JOB_BABY_ROYAL_GUARD:
  5848. case JOB_BABY_SORCERER:
  5849. case JOB_BABY_MINSTREL:
  5850. case JOB_BABY_WANDERER:
  5851. case JOB_BABY_SURA:
  5852. case JOB_BABY_GENETIC:
  5853. case JOB_BABY_SHADOW_CHASER:
  5854. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  5855. case JOB_BABY_RUNE_KNIGHT2:
  5856. return msg_txt(NULL,638);
  5857. case JOB_BABY_ROYAL_GUARD2:
  5858. return msg_txt(NULL,644);
  5859. case JOB_BABY_RANGER2:
  5860. return msg_txt(NULL,640);
  5861. case JOB_BABY_MECHANIC2:
  5862. return msg_txt(NULL,642);
  5863. case JOB_SUPER_NOVICE_E:
  5864. case JOB_SUPER_BABY_E:
  5865. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5866. case JOB_KAGEROU:
  5867. case JOB_OBORO:
  5868. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5869. case JOB_REBELLION:
  5870. return msg_txt(NULL,695);
  5871. case JOB_SUMMONER:
  5872. return msg_txt(NULL,697);
  5873. case JOB_BABY_SUMMONER:
  5874. return msg_txt(NULL,698);
  5875. case JOB_BABY_NINJA:
  5876. return msg_txt(NULL,699);
  5877. case JOB_BABY_KAGEROU:
  5878. case JOB_BABY_OBORO:
  5879. case JOB_BABY_TAEKWON:
  5880. case JOB_BABY_STAR_GLADIATOR:
  5881. case JOB_BABY_SOUL_LINKER:
  5882. case JOB_BABY_GUNSLINGER:
  5883. case JOB_BABY_REBELLION:
  5884. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5885. case JOB_BABY_STAR_GLADIATOR2:
  5886. return msg_txt(NULL,756);
  5887. case JOB_STAR_EMPEROR:
  5888. case JOB_SOUL_REAPER:
  5889. case JOB_BABY_STAR_EMPEROR:
  5890. case JOB_BABY_SOUL_REAPER:
  5891. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  5892. case JOB_STAR_EMPEROR2:
  5893. return msg_txt(NULL,782);
  5894. case JOB_BABY_STAR_EMPEROR2:
  5895. return msg_txt(NULL,784);
  5896. default:
  5897. return msg_txt(NULL,655);
  5898. }
  5899. }
  5900. /*====================================================
  5901. * Timered function to make id follow a target.
  5902. * @id = bl.id (player only atm)
  5903. * target is define in sd->followtarget (bl.id)
  5904. * used by pc_follow
  5905. *----------------------------------------------------*/
  5906. TIMER_FUNC(pc_follow_timer){
  5907. struct map_session_data *sd;
  5908. struct block_list *tbl;
  5909. sd = map_id2sd(id);
  5910. nullpo_ret(sd);
  5911. if (sd->followtimer != tid){
  5912. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5913. sd->followtimer = INVALID_TIMER;
  5914. return 0;
  5915. }
  5916. sd->followtimer = INVALID_TIMER;
  5917. tbl = map_id2bl(sd->followtarget);
  5918. if (tbl == NULL || pc_isdead(sd))
  5919. {
  5920. pc_stop_following(sd);
  5921. return 0;
  5922. }
  5923. // either player or target is currently detached from map blocks (could be teleporting),
  5924. // but still connected to this map, so we'll just increment the timer and check back later
  5925. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5926. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5927. {
  5928. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5929. if (!check_distance_bl(&sd->bl, tbl, 5))
  5930. unit_walktobl(&sd->bl, tbl, 5, 0);
  5931. } else
  5932. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5933. }
  5934. sd->followtimer = add_timer(
  5935. tick + 1000, // increase time a bit to loosen up map's load
  5936. pc_follow_timer, sd->bl.id, 0);
  5937. return 0;
  5938. }
  5939. int pc_stop_following (struct map_session_data *sd)
  5940. {
  5941. nullpo_ret(sd);
  5942. if (sd->followtimer != INVALID_TIMER) {
  5943. delete_timer(sd->followtimer,pc_follow_timer);
  5944. sd->followtimer = INVALID_TIMER;
  5945. }
  5946. sd->followtarget = -1;
  5947. sd->ud.target_to = 0;
  5948. unit_stop_walking(&sd->bl, 1);
  5949. return 0;
  5950. }
  5951. int pc_follow(struct map_session_data *sd,int target_id)
  5952. {
  5953. struct block_list *bl = map_id2bl(target_id);
  5954. if (bl == NULL /*|| bl->type != BL_PC*/)
  5955. return 1;
  5956. if (sd->followtimer != INVALID_TIMER)
  5957. pc_stop_following(sd);
  5958. sd->followtarget = target_id;
  5959. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5960. return 0;
  5961. }
  5962. int pc_checkbaselevelup(struct map_session_data *sd) {
  5963. unsigned int next = pc_nextbaseexp(sd);
  5964. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5965. return 0;
  5966. do {
  5967. sd->status.base_exp -= next;
  5968. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5969. 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 )
  5970. sd->status.base_exp = next-1;
  5971. next = pc_gets_status_point(sd->status.base_level);
  5972. sd->status.base_level++;
  5973. sd->status.status_point += next;
  5974. if( pc_is_maxbaselv(sd) ){
  5975. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5976. break;
  5977. }
  5978. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5979. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5980. status_calc_pet(sd->pd,SCO_NONE);
  5981. clif_updatestatus(sd,SP_STATUSPOINT);
  5982. clif_updatestatus(sd,SP_BASELEVEL);
  5983. clif_updatestatus(sd,SP_BASEEXP);
  5984. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5985. status_calc_pc(sd,SCO_FORCE);
  5986. status_percent_heal(&sd->bl,100,100);
  5987. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5988. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5989. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5990. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5991. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5992. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5993. if (sd->state.snovice_dead_flag)
  5994. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5995. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5996. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5997. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5998. }
  5999. clif_misceffect(&sd->bl,0);
  6000. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6001. if(sd->status.party_id)
  6002. party_send_levelup(sd);
  6003. pc_baselevelchanged(sd);
  6004. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  6005. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  6006. return 1;
  6007. }
  6008. void pc_baselevelchanged(struct map_session_data *sd) {
  6009. uint8 i;
  6010. for( i = 0; i < EQI_MAX; i++ ) {
  6011. if( sd->equip_index[i] >= 0 ) {
  6012. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6013. pc_unequipitem(sd, sd->equip_index[i], 3);
  6014. }
  6015. }
  6016. pc_show_questinfo(sd);
  6017. }
  6018. int pc_checkjoblevelup(struct map_session_data *sd)
  6019. {
  6020. unsigned int next = pc_nextjobexp(sd);
  6021. nullpo_ret(sd);
  6022. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6023. return 0;
  6024. do {
  6025. sd->status.job_exp -= next;
  6026. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6027. 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 )
  6028. sd->status.job_exp = next-1;
  6029. sd->status.job_level ++;
  6030. sd->status.skill_point ++;
  6031. if( pc_is_maxjoblv(sd) ){
  6032. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6033. break;
  6034. }
  6035. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6036. clif_updatestatus(sd,SP_JOBLEVEL);
  6037. clif_updatestatus(sd,SP_JOBEXP);
  6038. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6039. clif_updatestatus(sd,SP_SKILLPOINT);
  6040. status_calc_pc(sd,SCO_FORCE);
  6041. clif_misceffect(&sd->bl,1);
  6042. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  6043. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6044. npc_script_event(sd, NPCE_JOBLVUP);
  6045. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  6046. pc_show_questinfo(sd);
  6047. return 1;
  6048. }
  6049. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6050. * @param sd Player
  6051. * @param base_exp Base EXP before peronal bonuses
  6052. * @param job_exp Job EXP before peronal bonuses
  6053. * @param src Block list that affecting the exp calculation
  6054. */
  6055. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  6056. {
  6057. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6058. if (src) {
  6059. struct status_data *status = status_get_status_data(src);
  6060. if( sd->expaddrace[status->race] )
  6061. bonus += sd->expaddrace[status->race];
  6062. if( sd->expaddrace[RC_ALL] )
  6063. bonus += sd->expaddrace[RC_ALL];
  6064. if( sd->expaddclass[status->class_] )
  6065. bonus += sd->expaddclass[status->class_];
  6066. if( sd->expaddclass[CLASS_ALL] )
  6067. bonus += sd->expaddclass[CLASS_ALL];
  6068. if (battle_config.pk_mode &&
  6069. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6070. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6071. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6072. vip_bonus_base = battle_config.vip_base_exp_increase;
  6073. vip_bonus_job = battle_config.vip_job_exp_increase;
  6074. }
  6075. }
  6076. // Give EXPBOOST for quests even if src is NULL.
  6077. if (sd->sc.data[SC_EXPBOOST]) {
  6078. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6079. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6080. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6081. }
  6082. if (*base_exp) {
  6083. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  6084. *base_exp = cap_value(exp, 1, UINT_MAX);
  6085. }
  6086. // Give JEXPBOOST for quests even if src is NULL.
  6087. if (sd->sc.data[SC_JEXPBOOST])
  6088. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6089. if (*job_exp) {
  6090. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  6091. *job_exp = cap_value(exp, 1, UINT_MAX);
  6092. }
  6093. return;
  6094. }
  6095. /**
  6096. * Show EXP gained by player in percentage by @showexp
  6097. * @param sd Player
  6098. * @param base_exp Base EXP gained/loss
  6099. * @param next_base_exp Base EXP needed for next base level
  6100. * @param job_exp Job EXP gained/loss
  6101. * @param next_job_exp Job EXP needed for next job level
  6102. * @param lost True:EXP penalty, lose EXP
  6103. **/
  6104. void pc_gainexp_disp(struct map_session_data *sd, unsigned int base_exp, unsigned int next_base_exp, unsigned int job_exp, unsigned int next_job_exp, bool lost) {
  6105. char output[CHAT_SIZE_MAX];
  6106. nullpo_retv(sd);
  6107. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6108. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6109. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6110. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6111. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6112. }
  6113. /**
  6114. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6115. * @param sd Player
  6116. * @param src EXP source
  6117. * @param base_exp Base EXP gained
  6118. * @param base_exp Job EXP gained
  6119. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6120. * @return
  6121. **/
  6122. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  6123. {
  6124. unsigned int nextb = 0, nextj = 0;
  6125. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6126. nullpo_retv(sd);
  6127. if(sd->bl.prev == NULL || pc_isdead(sd))
  6128. return;
  6129. if (!(exp_flag&2)) {
  6130. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6131. return; // no exp on pvp maps
  6132. if (sd->status.guild_id>0)
  6133. base_exp -= guild_payexp(sd,base_exp);
  6134. }
  6135. flag = ((base_exp) ? 1 : 0) |
  6136. ((job_exp) ? 2 : 0) |
  6137. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6138. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6139. if (!(exp_flag&2))
  6140. pc_calcexp(sd, &base_exp, &job_exp, src);
  6141. nextb = pc_nextbaseexp(sd);
  6142. nextj = pc_nextjobexp(sd);
  6143. if (flag&4){
  6144. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6145. base_exp = 0;
  6146. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6147. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6148. }
  6149. if (flag&8){
  6150. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6151. job_exp = 0;
  6152. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6153. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6154. }
  6155. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6156. //Note that this value should never be greater than the original
  6157. //therefore no overflow checks are needed. [Skotlex]
  6158. if (nextb > 0) {
  6159. float nextbp = (float) base_exp / (float) nextb;
  6160. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6161. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6162. }
  6163. if (nextj > 0) {
  6164. float nextjp = (float) job_exp / (float) nextj;
  6165. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6166. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6167. }
  6168. }
  6169. // Give EXP for Base Level
  6170. if (base_exp) {
  6171. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  6172. sd->status.base_exp = UINT32_MAX;
  6173. else
  6174. sd->status.base_exp += base_exp;
  6175. if (!pc_checkbaselevelup(sd))
  6176. clif_updatestatus(sd,SP_BASEEXP);
  6177. }
  6178. // Give EXP for Job Level
  6179. if (job_exp) {
  6180. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  6181. sd->status.job_exp = UINT32_MAX;
  6182. else
  6183. sd->status.job_exp += job_exp;
  6184. if (!pc_checkjoblevelup(sd))
  6185. clif_updatestatus(sd,SP_JOBEXP);
  6186. }
  6187. if (flag&1)
  6188. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6189. if (flag&2)
  6190. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6191. if (sd->state.showexp && (base_exp || job_exp))
  6192. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6193. }
  6194. /**
  6195. * Lost Base/Job EXP from a player
  6196. * @param sd Player
  6197. * @param base_exp Base EXP lost
  6198. * @param job_exp Job EXP lost
  6199. **/
  6200. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  6201. nullpo_retv(sd);
  6202. if (base_exp) {
  6203. base_exp = u32min(sd->status.base_exp, base_exp);
  6204. sd->status.base_exp -= base_exp;
  6205. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6206. clif_updatestatus(sd, SP_BASEEXP);
  6207. }
  6208. if (job_exp) {
  6209. job_exp = u32min(sd->status.job_exp, job_exp);
  6210. sd->status.job_exp -= job_exp;
  6211. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6212. clif_updatestatus(sd, SP_JOBEXP);
  6213. }
  6214. if (sd->state.showexp && (base_exp || job_exp))
  6215. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6216. }
  6217. /**
  6218. * Returns max base level for this character's class.
  6219. * @param class_: Player's class
  6220. * @return Max Base Level
  6221. */
  6222. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6223. return job_info[pc_class2idx(class_)].max_level[0];
  6224. }
  6225. /**
  6226. * Returns max base level for this character.
  6227. * @param sd Player
  6228. * @return Max Base Level
  6229. **/
  6230. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6231. return pc_class_maxbaselv(sd->status.class_);
  6232. }
  6233. /**
  6234. * Returns max job level for this character's class.
  6235. * @param class_: Player's class
  6236. * @return Max Job Level
  6237. */
  6238. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6239. return job_info[pc_class2idx(class_)].max_level[1];
  6240. }
  6241. /**
  6242. * Returns max job level for this character.
  6243. * @param sd Player
  6244. * @return Max Job Level
  6245. **/
  6246. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6247. return pc_class_maxjoblv(sd->status.class_);
  6248. }
  6249. /**
  6250. * Check if player is reached max base level
  6251. * @param sd
  6252. * @return True if reached max level
  6253. **/
  6254. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6255. nullpo_retr(false, sd);
  6256. return (sd->status.base_level >= pc_maxbaselv(sd));
  6257. }
  6258. /**
  6259. * Check if player is reached max base level
  6260. * @param sd
  6261. * @return True if reached max level
  6262. **/
  6263. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6264. nullpo_retr(false, sd);
  6265. return (sd->status.job_level >= pc_maxjoblv(sd));
  6266. }
  6267. /**
  6268. * Base exp needed for player to level up.
  6269. * @param sd
  6270. * @return Base EXP needed for next base level
  6271. **/
  6272. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6273. nullpo_ret(sd);
  6274. if (sd->status.base_level == 0) // Is this something that possible?
  6275. return 0;
  6276. if (pc_is_maxbaselv(sd))
  6277. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6278. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6279. }
  6280. /**
  6281. * Job exp needed for player to level up.
  6282. * @param sd
  6283. * @return Job EXP needed for next job level
  6284. **/
  6285. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6286. nullpo_ret(sd);
  6287. if (sd->status.job_level == 0) // Is this something that possible?
  6288. return 0;
  6289. if (pc_is_maxjoblv(sd))
  6290. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6291. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6292. }
  6293. /// Returns the value of the specified stat.
  6294. static int pc_getstat(struct map_session_data* sd, int type)
  6295. {
  6296. nullpo_retr(-1, sd);
  6297. switch( type ) {
  6298. case SP_STR: return sd->status.str;
  6299. case SP_AGI: return sd->status.agi;
  6300. case SP_VIT: return sd->status.vit;
  6301. case SP_INT: return sd->status.int_;
  6302. case SP_DEX: return sd->status.dex;
  6303. case SP_LUK: return sd->status.luk;
  6304. default:
  6305. return -1;
  6306. }
  6307. }
  6308. /// Sets the specified stat to the specified value.
  6309. /// Returns the new value.
  6310. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6311. {
  6312. nullpo_retr(-1, sd);
  6313. switch( type ) {
  6314. case SP_STR: sd->status.str = val; break;
  6315. case SP_AGI: sd->status.agi = val; break;
  6316. case SP_VIT: sd->status.vit = val; break;
  6317. case SP_INT: sd->status.int_ = val; break;
  6318. case SP_DEX: sd->status.dex = val; break;
  6319. case SP_LUK: sd->status.luk = val; break;
  6320. default:
  6321. return -1;
  6322. }
  6323. return val;
  6324. }
  6325. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6326. int pc_gets_status_point(int level)
  6327. {
  6328. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6329. return (statp[level+1] - statp[level]);
  6330. else //Default increase
  6331. return ((level+15) / 5);
  6332. }
  6333. #ifdef RENEWAL_STAT
  6334. /// Renewal status point cost formula
  6335. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6336. #else
  6337. /// Pre-Renewal status point cost formula
  6338. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6339. #endif
  6340. /// Returns the number of stat points needed to change the specified stat by val.
  6341. /// If val is negative, returns the number of stat points that would be needed to
  6342. /// raise the specified stat from (current value - val) to current value.
  6343. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6344. {
  6345. int low, high, sp = 0, max = 0;
  6346. if ( val == 0 )
  6347. return 0;
  6348. low = pc_getstat(sd,type);
  6349. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6350. if ( low >= max && val > 0 )
  6351. return 0; // Official servers show '0' when max is reached
  6352. high = low + val;
  6353. if ( val < 0 )
  6354. SWAP(low, high);
  6355. for ( ; low < high; low++ )
  6356. sp += PC_STATUS_POINT_COST(low);
  6357. return sp;
  6358. }
  6359. /**
  6360. * Returns the value the specified stat can be increased by with the current
  6361. * amount of available status points for the current character's class.
  6362. *
  6363. * @param sd The target character.
  6364. * @param type Stat to verify.
  6365. * @return Maximum value the stat could grow by.
  6366. */
  6367. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6368. {
  6369. int base, final_val, status_points, max_param;
  6370. nullpo_ret(sd);
  6371. base = final_val = pc_getstat(sd, type);
  6372. status_points = sd->status.status_point;
  6373. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6374. while (final_val <= max_param && status_points >= 0) {
  6375. status_points -= PC_STATUS_POINT_COST(final_val);
  6376. final_val++;
  6377. }
  6378. final_val--;
  6379. return (final_val > base ? final_val-base : 0);
  6380. }
  6381. /**
  6382. * Raises a stat by the specified amount.
  6383. *
  6384. * Obeys max_parameter limits.
  6385. * Subtracts status points according to the cost of the increased stat points.
  6386. *
  6387. * @param sd The target character.
  6388. * @param type The stat to change (see enum _sp)
  6389. * @param increase The stat increase (strictly positive) amount.
  6390. * @retval true if the stat was increased by any amount.
  6391. * @retval false if there were no changes.
  6392. */
  6393. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6394. {
  6395. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6396. nullpo_ret(sd);
  6397. // check conditions
  6398. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6399. clif_statusupack(sd, type, 0, 0);
  6400. return false;
  6401. }
  6402. // check limits
  6403. current = pc_getstat(sd, type);
  6404. max_increase = pc_maxparameterincrease(sd, type);
  6405. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6406. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6407. clif_statusupack(sd, type, 0, 0);
  6408. return false;
  6409. }
  6410. // check status points
  6411. needed_points = pc_need_status_point(sd, type, increase);
  6412. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6413. clif_statusupack(sd, type, 0, 0);
  6414. return false;
  6415. }
  6416. // set new values
  6417. final_value = pc_setstat(sd, type, current + increase);
  6418. sd->status.status_point -= needed_points;
  6419. status_calc_pc(sd,SCO_NONE);
  6420. // update increase cost indicator
  6421. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6422. // update statpoint count
  6423. clif_updatestatus(sd, SP_STATUSPOINT);
  6424. // update stat value
  6425. clif_statusupack(sd, type, 1, final_value); // required
  6426. if( final_value > 255 )
  6427. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6428. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6429. return true;
  6430. }
  6431. /**
  6432. * Raises a stat by the specified amount.
  6433. *
  6434. * Obeys max_parameter limits.
  6435. * Does not subtract status points for the cost of the modified stat points.
  6436. *
  6437. * @param sd The target character.
  6438. * @param type The stat to change (see enum _sp)
  6439. * @param val The stat increase (or decrease) amount.
  6440. * @return the stat increase amount.
  6441. * @retval 0 if no changes were made.
  6442. */
  6443. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6444. {
  6445. int max, need;
  6446. nullpo_ret(sd);
  6447. if( type < SP_STR || type > SP_LUK )
  6448. {
  6449. clif_statusupack(sd,type,0,0);
  6450. return 0;
  6451. }
  6452. need = pc_need_status_point(sd,type,1);
  6453. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6454. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6455. status_calc_pc(sd,SCO_NONE);
  6456. // update increase cost indicator
  6457. if( need != pc_need_status_point(sd,type,1) )
  6458. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6459. // update stat value
  6460. clif_statusupack(sd,type,1,val); // required
  6461. if( val > 255 )
  6462. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6463. return val;
  6464. }
  6465. /*==========================================
  6466. * Update skill_lv for player sd
  6467. * Skill point allocation
  6468. *------------------------------------------*/
  6469. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6470. {
  6471. uint16 idx = skill_get_index(skill_id);
  6472. nullpo_retv(sd);
  6473. if (!idx) {
  6474. if (skill_id)
  6475. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6476. return;
  6477. }
  6478. // Level up guild skill
  6479. if (SKILL_CHK_GUILD(skill_id)) {
  6480. guild_skillup(sd, skill_id);
  6481. return;
  6482. }
  6483. // Level up homunculus skill
  6484. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6485. hom_skillup(sd->hd, skill_id);
  6486. return;
  6487. }
  6488. else {
  6489. if( sd->status.skill_point > 0 &&
  6490. sd->status.skill[idx].id &&
  6491. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6492. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6493. {
  6494. int lv, range, upgradable;
  6495. sd->status.skill[idx].lv++;
  6496. sd->status.skill_point--;
  6497. if( !skill_get_inf(skill_id) )
  6498. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6499. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6500. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6501. else
  6502. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6503. lv = sd->status.skill[idx].lv;
  6504. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6505. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6506. clif_skillup(sd,skill_id,lv,range,upgradable);
  6507. clif_updatestatus(sd,SP_SKILLPOINT);
  6508. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6509. clif_updatestatus(sd,SP_CARTINFO);
  6510. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6511. clif_skillinfoblock(sd);
  6512. }
  6513. //else
  6514. // 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);
  6515. }
  6516. }
  6517. /*==========================================
  6518. * /allskill
  6519. *------------------------------------------*/
  6520. int pc_allskillup(struct map_session_data *sd)
  6521. {
  6522. int i;
  6523. nullpo_ret(sd);
  6524. for (i = 0; i < MAX_SKILL; i++) {
  6525. 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) {
  6526. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6527. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6528. if (sd->status.skill[i].lv == 0)
  6529. sd->status.skill[i].id = 0;
  6530. }
  6531. }
  6532. if (!pc_grant_allskills(sd, true)) {
  6533. uint16 sk_id;
  6534. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6535. uint16 sk_idx = skill_get_index(sk_id);
  6536. if (sk_id == 0 || sk_idx == 0)
  6537. continue;
  6538. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  6539. if (
  6540. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  6541. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  6542. sk_id == SG_DEVIL
  6543. )
  6544. continue; //Cannot be learned normally.
  6545. sd->status.skill[sk_idx].id = sk_id;
  6546. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6547. }
  6548. }
  6549. status_calc_pc(sd,SCO_NONE);
  6550. //Required because if you could level up all skills previously,
  6551. //the update will not be sent as only the lv variable changes.
  6552. clif_skillinfoblock(sd);
  6553. return 0;
  6554. }
  6555. /*==========================================
  6556. * /resetlvl
  6557. *------------------------------------------*/
  6558. int pc_resetlvl(struct map_session_data* sd,int type)
  6559. {
  6560. int i;
  6561. nullpo_ret(sd);
  6562. if (type != 3) //Also reset skills
  6563. pc_resetskill(sd, 0);
  6564. if(type == 1){
  6565. sd->status.skill_point=0;
  6566. sd->status.base_level=1;
  6567. sd->status.job_level=1;
  6568. sd->status.base_exp=0;
  6569. sd->status.job_exp=0;
  6570. if(sd->sc.option !=0)
  6571. sd->sc.option = 0;
  6572. sd->status.str=1;
  6573. sd->status.agi=1;
  6574. sd->status.vit=1;
  6575. sd->status.int_=1;
  6576. sd->status.dex=1;
  6577. sd->status.luk=1;
  6578. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6579. sd->status.status_point=100; // not 88 [celest]
  6580. // give platinum skills upon changing
  6581. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6582. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6583. }
  6584. }
  6585. if(type == 2){
  6586. sd->status.skill_point=0;
  6587. sd->status.base_level=1;
  6588. sd->status.job_level=1;
  6589. sd->status.base_exp=0;
  6590. sd->status.job_exp=0;
  6591. }
  6592. if(type == 3){
  6593. sd->status.base_level=1;
  6594. sd->status.base_exp=0;
  6595. }
  6596. if(type == 4){
  6597. sd->status.job_level=1;
  6598. sd->status.job_exp=0;
  6599. }
  6600. clif_updatestatus(sd,SP_STATUSPOINT);
  6601. clif_updatestatus(sd,SP_STR);
  6602. clif_updatestatus(sd,SP_AGI);
  6603. clif_updatestatus(sd,SP_VIT);
  6604. clif_updatestatus(sd,SP_INT);
  6605. clif_updatestatus(sd,SP_DEX);
  6606. clif_updatestatus(sd,SP_LUK);
  6607. clif_updatestatus(sd,SP_BASELEVEL);
  6608. clif_updatestatus(sd,SP_JOBLEVEL);
  6609. clif_updatestatus(sd,SP_STATUSPOINT);
  6610. clif_updatestatus(sd,SP_BASEEXP);
  6611. clif_updatestatus(sd,SP_JOBEXP);
  6612. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6613. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6614. clif_updatestatus(sd,SP_SKILLPOINT);
  6615. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6616. clif_updatestatus(sd,SP_UAGI);
  6617. clif_updatestatus(sd,SP_UVIT);
  6618. clif_updatestatus(sd,SP_UINT);
  6619. clif_updatestatus(sd,SP_UDEX);
  6620. clif_updatestatus(sd,SP_ULUK); // End Addition
  6621. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6622. if(sd->equip_index[i] >= 0)
  6623. if(pc_isequip(sd,sd->equip_index[i]))
  6624. pc_unequipitem(sd,sd->equip_index[i],2);
  6625. }
  6626. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6627. party_send_levelup(sd);
  6628. status_calc_pc(sd, SCO_FORCE);
  6629. clif_skillinfoblock(sd);
  6630. return 0;
  6631. }
  6632. /*==========================================
  6633. * /resetstate
  6634. *------------------------------------------*/
  6635. int pc_resetstate(struct map_session_data* sd)
  6636. {
  6637. nullpo_ret(sd);
  6638. if (battle_config.use_statpoint_table)
  6639. { // New statpoint table used here - Dexity
  6640. if (sd->status.base_level > MAX_LEVEL)
  6641. { //statp[] goes out of bounds, can't reset!
  6642. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6643. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6644. return 0;
  6645. }
  6646. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6647. }
  6648. else
  6649. {
  6650. int add=0;
  6651. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6652. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6653. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6654. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6655. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6656. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6657. sd->status.status_point+=add;
  6658. }
  6659. pc_setstat(sd, SP_STR, 1);
  6660. pc_setstat(sd, SP_AGI, 1);
  6661. pc_setstat(sd, SP_VIT, 1);
  6662. pc_setstat(sd, SP_INT, 1);
  6663. pc_setstat(sd, SP_DEX, 1);
  6664. pc_setstat(sd, SP_LUK, 1);
  6665. clif_updatestatus(sd,SP_STR);
  6666. clif_updatestatus(sd,SP_AGI);
  6667. clif_updatestatus(sd,SP_VIT);
  6668. clif_updatestatus(sd,SP_INT);
  6669. clif_updatestatus(sd,SP_DEX);
  6670. clif_updatestatus(sd,SP_LUK);
  6671. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6672. clif_updatestatus(sd,SP_UAGI);
  6673. clif_updatestatus(sd,SP_UVIT);
  6674. clif_updatestatus(sd,SP_UINT);
  6675. clif_updatestatus(sd,SP_UDEX);
  6676. clif_updatestatus(sd,SP_ULUK); // End Addition
  6677. clif_updatestatus(sd,SP_STATUSPOINT);
  6678. if( sd->mission_mobid ) { //bugreport:2200
  6679. sd->mission_mobid = 0;
  6680. sd->mission_count = 0;
  6681. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6682. }
  6683. status_calc_pc(sd, SCO_NONE);
  6684. return 1;
  6685. }
  6686. /*==========================================
  6687. * /resetskill
  6688. * if flag&1, perform block resync and status_calc call.
  6689. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6690. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6691. *------------------------------------------*/
  6692. int pc_resetskill(struct map_session_data* sd, int flag)
  6693. {
  6694. int i, skill_point=0;
  6695. nullpo_ret(sd);
  6696. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6697. return 0;
  6698. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6699. /**
  6700. * 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)
  6701. **/
  6702. if( pc_is_taekwon_ranker(sd) )
  6703. return 0;
  6704. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6705. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6706. i = sd->sc.option;
  6707. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6708. i &= ~OPTION_RIDING;
  6709. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6710. i &= ~OPTION_FALCON;
  6711. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6712. i &= ~OPTION_DRAGON;
  6713. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6714. i &= ~OPTION_WUG;
  6715. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6716. i &= ~OPTION_WUGRIDER;
  6717. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6718. i &= ~OPTION_MADOGEAR;
  6719. #ifndef NEW_CARTS
  6720. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6721. i &= ~OPTION_CART;
  6722. #else
  6723. if( sd->sc.data[SC_PUSH_CART] )
  6724. pc_setcart(sd, 0);
  6725. #endif
  6726. if( i != sd->sc.option )
  6727. pc_setoption(sd, i);
  6728. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6729. hom_vaporize(sd, HOM_ST_ACTIVE);
  6730. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6731. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6732. }
  6733. for (const auto &skill : skill_db) {
  6734. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  6735. uint8 lv = sd->status.skill[idx].lv;
  6736. if (lv == 0 || skill_id == 0)
  6737. continue;
  6738. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  6739. continue;
  6740. // Don't reset trick dead if not a novice/baby
  6741. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6742. {
  6743. sd->status.skill[idx].lv = 0;
  6744. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6745. continue;
  6746. }
  6747. // do not reset basic skill
  6748. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6749. continue;
  6750. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  6751. continue;
  6752. if( flag&4 && !skill_ischangesex(skill_id) )
  6753. continue;
  6754. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  6755. { //Only handle quest skills in a special way when you can't learn them manually
  6756. if( battle_config.quest_skill_reset && !(flag&2) )
  6757. { //Wipe them
  6758. sd->status.skill[idx].lv = 0;
  6759. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6760. }
  6761. continue;
  6762. }
  6763. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  6764. skill_point += lv;
  6765. else
  6766. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6767. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  6768. if( !(flag&2) )
  6769. {// reset
  6770. sd->status.skill[idx].lv = 0;
  6771. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6772. }
  6773. }
  6774. if( flag&2 || !skill_point ) return skill_point;
  6775. sd->status.skill_point += skill_point;
  6776. if (flag&1) {
  6777. clif_updatestatus(sd,SP_SKILLPOINT);
  6778. clif_skillinfoblock(sd);
  6779. status_calc_pc(sd, SCO_FORCE);
  6780. }
  6781. return skill_point;
  6782. }
  6783. /*==========================================
  6784. * /resetfeel [Komurka]
  6785. *------------------------------------------*/
  6786. int pc_resetfeel(struct map_session_data* sd)
  6787. {
  6788. int i;
  6789. nullpo_ret(sd);
  6790. for (i=0; i<MAX_PC_FEELHATE; i++)
  6791. {
  6792. sd->feel_map[i].m = -1;
  6793. sd->feel_map[i].index = 0;
  6794. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6795. }
  6796. return 0;
  6797. }
  6798. int pc_resethate(struct map_session_data* sd)
  6799. {
  6800. int i;
  6801. nullpo_ret(sd);
  6802. for (i=0; i<3; i++)
  6803. {
  6804. sd->hate_mob[i] = -1;
  6805. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6806. }
  6807. return 0;
  6808. }
  6809. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6810. {
  6811. int bonus = 0;
  6812. nullpo_ret(sd);
  6813. skill_id = skill_dummy2skill_id(skill_id);
  6814. for (auto &it : sd->skillatk) {
  6815. if (it.id == skill_id) {
  6816. bonus += it.val;
  6817. break;
  6818. }
  6819. }
  6820. return bonus;
  6821. }
  6822. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6823. {
  6824. int bonus = 0;
  6825. nullpo_ret(sd);
  6826. skill_id = skill_dummy2skill_id(skill_id);
  6827. for (auto &it : sd->subskill) {
  6828. if (it.id == skill_id) {
  6829. bonus += it.val;
  6830. break;
  6831. }
  6832. }
  6833. return bonus;
  6834. }
  6835. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6836. int bonus = sd->bonus.add_heal_rate;
  6837. nullpo_ret(sd);
  6838. skill_id = skill_dummy2skill_id(skill_id);
  6839. if( bonus ) {
  6840. switch( skill_id ) {
  6841. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6842. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6843. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6844. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6845. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6846. }
  6847. }
  6848. for (auto &it : sd->skillheal) {
  6849. if (it.id == skill_id) {
  6850. bonus += it.val;
  6851. break;
  6852. }
  6853. }
  6854. return bonus;
  6855. }
  6856. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6857. int bonus = sd->bonus.add_heal2_rate;
  6858. skill_id = skill_dummy2skill_id(skill_id);
  6859. for (auto &it : sd->skillheal2) {
  6860. if (it.id == skill_id) {
  6861. bonus += it.val;
  6862. break;
  6863. }
  6864. }
  6865. return bonus;
  6866. }
  6867. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6868. {
  6869. if( !pc_isdead(sd) )
  6870. return; // not applicable
  6871. if( sd->bg_id && bg_member_respawn(sd) )
  6872. return; // member revived by battleground
  6873. pc_setstand(sd, true);
  6874. pc_setrestartvalue(sd,3);
  6875. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6876. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6877. }
  6878. static TIMER_FUNC(pc_respawn_timer){
  6879. struct map_session_data *sd = map_id2sd(id);
  6880. if( sd != NULL )
  6881. {
  6882. sd->pvp_point=0;
  6883. sd->respawn_tid = INVALID_TIMER;
  6884. pc_respawn(sd,CLR_OUTSIGHT);
  6885. }
  6886. return 0;
  6887. }
  6888. /*==========================================
  6889. * Invoked when a player has received damage
  6890. *------------------------------------------*/
  6891. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6892. {
  6893. if (sp) clif_updatestatus(sd,SP_SP);
  6894. if (hp) clif_updatestatus(sd,SP_HP);
  6895. else return;
  6896. if (!src)
  6897. return;
  6898. if( pc_issit(sd) ) {
  6899. pc_setstand(sd, true);
  6900. skill_sit(sd,0);
  6901. }
  6902. if (sd->progressbar.npc_id)
  6903. clif_progressbar_abort(sd);
  6904. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6905. pet_target_check(sd->pd,src,1);
  6906. if( sd->status.ele_id > 0 )
  6907. elemental_set_target(sd,src);
  6908. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  6909. sd->canlog_tick = gettick();
  6910. }
  6911. TIMER_FUNC(pc_close_npc_timer){
  6912. TBL_PC *sd = map_id2sd(id);
  6913. if(sd) pc_close_npc(sd,data);
  6914. return 0;
  6915. }
  6916. /**
  6917. * Method to properly close a NPC for player and clear anything related.
  6918. * @param sd: Player attached
  6919. * @param flag: Method of closure
  6920. * 1: Produce a close button and end the NPC
  6921. * 2: End the NPC (best for no dialog windows)
  6922. */
  6923. void pc_close_npc(struct map_session_data *sd,int flag)
  6924. {
  6925. nullpo_retv(sd);
  6926. if (sd->npc_id || sd->npc_shopid) {
  6927. if (sd->state.using_fake_npc) {
  6928. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6929. sd->state.using_fake_npc = 0;
  6930. }
  6931. if (sd->st) {
  6932. if(sd->st->state == RUN){ //wait ending code execution
  6933. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6934. return;
  6935. }
  6936. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6937. sd->st->mes_active = 0;
  6938. }
  6939. sd->state.menu_or_input = 0;
  6940. sd->npc_menu = 0;
  6941. sd->npc_shopid = 0;
  6942. #ifdef SECURE_NPCTIMEOUT
  6943. sd->npc_idle_timer = INVALID_TIMER;
  6944. #endif
  6945. if (sd->st) {
  6946. if (sd->st->state == CLOSE) {
  6947. clif_scriptclose(sd, sd->npc_id);
  6948. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  6949. sd->st->state = END; // Force to end now
  6950. }
  6951. if (sd->st->state == END) { // free attached scripts that are waiting
  6952. script_free_state(sd->st);
  6953. sd->st = NULL;
  6954. sd->npc_id = 0;
  6955. }
  6956. }
  6957. }
  6958. }
  6959. /*==========================================
  6960. * Invoked when a player has negative current hp
  6961. *------------------------------------------*/
  6962. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6963. {
  6964. int i=0,k=0;
  6965. t_tick tick = gettick();
  6966. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6967. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6968. // Super Novices have no kill or die functions attached when saved by their angel
  6969. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6970. unsigned int exp = pc_nextbaseexp(sd);
  6971. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6972. sd->state.snovice_dead_flag = 1;
  6973. pc_setrestartvalue(sd,1);
  6974. status_percent_heal(&sd->bl, 100, 100);
  6975. clif_resurrection(&sd->bl, 1);
  6976. if(battle_config.pc_invincible_time)
  6977. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6978. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6979. if(mapdata_flag_gvg2(mapdata))
  6980. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6981. return 0;
  6982. }
  6983. }
  6984. for(k = 0; k < MAX_DEVOTION; k++) {
  6985. if (sd->devotion[k]){
  6986. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6987. if (devsd)
  6988. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6989. sd->devotion[k] = 0;
  6990. }
  6991. }
  6992. if(sd->shadowform_id) { //if we were target of shadowform
  6993. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6994. sd->shadowform_id = 0; //should be remove on status end anyway
  6995. }
  6996. if(sd->status.pet_id > 0 && sd->pd) {
  6997. struct pet_data *pd = sd->pd;
  6998. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  6999. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  7000. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  7001. pet_set_intimate(pd, PET_INTIMATE_NONE);
  7002. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  7003. }
  7004. if( sd->pd->target_id ) // Unlock all targets...
  7005. pet_unlocktarget(sd->pd);
  7006. }
  7007. 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)
  7008. hom_vaporize(sd, HOM_ST_ACTIVE);
  7009. if( sd->md )
  7010. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  7011. if( sd->ed )
  7012. elemental_delete(sd->ed);
  7013. // Leave duel if you die [LuzZza]
  7014. if(battle_config.duel_autoleave_when_die) {
  7015. if(sd->duel_group > 0)
  7016. duel_leave(sd->duel_group, sd);
  7017. if(sd->duel_invite > 0)
  7018. duel_reject(sd->duel_invite, sd);
  7019. }
  7020. pc_close_npc(sd,2); //close npc if we were using one
  7021. /* e.g. not killed thru pc_damage */
  7022. if( pc_issit(sd) ) {
  7023. clif_status_load(&sd->bl,EFST_SIT,0);
  7024. }
  7025. pc_setdead(sd);
  7026. clif_party_dead( sd );
  7027. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
  7028. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  7029. //Reset menu skills/item skills
  7030. if ((sd->skillitem) != 0)
  7031. sd->skillitem = sd->skillitemlv = 0;
  7032. if ((sd->menuskill_id) != 0)
  7033. sd->menuskill_id = sd->menuskill_val = 0;
  7034. //Reset ticks.
  7035. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  7036. if ( sd->spiritball !=0 )
  7037. pc_delspiritball(sd,sd->spiritball,0);
  7038. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  7039. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  7040. if (src)
  7041. switch (src->type) {
  7042. case BL_MOB:
  7043. {
  7044. struct mob_data *md=(struct mob_data *)src;
  7045. if(md->target_id==sd->bl.id)
  7046. mob_unlocktarget(md,tick);
  7047. if(battle_config.mobs_level_up && md->status.hp &&
  7048. (unsigned int)md->level < pc_maxbaselv(sd) &&
  7049. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  7050. ) { // monster level up [Valaris]
  7051. clif_misceffect(&md->bl,0);
  7052. md->level++;
  7053. status_calc_mob(md, SCO_NONE);
  7054. status_percent_heal(src,10,0);
  7055. if( battle_config.show_mob_info&4 )
  7056. {// update name with new level
  7057. clif_name_area(&md->bl);
  7058. }
  7059. }
  7060. src = battle_get_master(src); // Maybe Player Summon
  7061. }
  7062. break;
  7063. case BL_PET: //Pass on to master...
  7064. case BL_HOM:
  7065. case BL_MER:
  7066. src = battle_get_master(src);
  7067. break;
  7068. }
  7069. if (src && src->type == BL_PC) {
  7070. struct map_session_data *ssd = (struct map_session_data *)src;
  7071. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  7072. npc_script_event(ssd, NPCE_KILLPC);
  7073. if (battle_config.pk_mode&2) {
  7074. ssd->status.manner -= 5;
  7075. if(ssd->status.manner < 0)
  7076. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  7077. #if 0
  7078. // PK/Karma system code (not enabled yet) [celest]
  7079. // originally from Kade Online, so i don't know if any of these is correct ^^;
  7080. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  7081. // karma going down = more 'good' / more honourable.
  7082. // The Karma System way...
  7083. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  7084. sd->status.karma--;
  7085. ssd->status.karma--;
  7086. }
  7087. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  7088. ssd->status.karma++;
  7089. // or the PK System way...
  7090. if (sd->status.karma > 0) // player killed is dishonourable?
  7091. ssd->status.karma--; // honour points earned
  7092. sd->status.karma++; // honour points lost
  7093. // To-do: Receive exp on certain occasions
  7094. #endif
  7095. }
  7096. }
  7097. if(battle_config.bone_drop==2
  7098. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  7099. {
  7100. struct item item_tmp;
  7101. memset(&item_tmp,0,sizeof(item_tmp));
  7102. item_tmp.nameid=ITEMID_SKULL_;
  7103. item_tmp.identify=1;
  7104. item_tmp.card[0]=CARD0_CREATE;
  7105. item_tmp.card[1]=0;
  7106. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  7107. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  7108. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  7109. }
  7110. //Remove bonus_script when dead
  7111. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  7112. // changed penalty options, added death by player if pk_mode [Valaris]
  7113. if(battle_config.death_penalty_type
  7114. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  7115. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  7116. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  7117. {
  7118. uint32 base_penalty = 0;
  7119. uint32 job_penalty = 0;
  7120. uint32 zeny_penalty = 0;
  7121. if (pc_isvip(sd)) { // EXP penalty for VIP
  7122. base_penalty = battle_config.vip_exp_penalty_base;
  7123. job_penalty = battle_config.vip_exp_penalty_job;
  7124. zeny_penalty = battle_config.vip_zeny_penalty;
  7125. } else {
  7126. base_penalty = battle_config.death_penalty_base;
  7127. job_penalty = battle_config.death_penalty_job;
  7128. zeny_penalty = battle_config.zeny_penalty;
  7129. }
  7130. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7131. switch (battle_config.death_penalty_type) {
  7132. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7133. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  7134. }
  7135. if (base_penalty){ //recheck after altering to speedup
  7136. if (battle_config.pk_mode && src && src->type==BL_PC)
  7137. base_penalty *= 2;
  7138. base_penalty = u32min(sd->status.base_exp, base_penalty);
  7139. }
  7140. }
  7141. else
  7142. base_penalty = 0;
  7143. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7144. switch (battle_config.death_penalty_type) {
  7145. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7146. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7147. }
  7148. if (job_penalty) {
  7149. if (battle_config.pk_mode && src && src->type==BL_PC)
  7150. job_penalty *= 2;
  7151. job_penalty = u32min(sd->status.job_exp, job_penalty);
  7152. }
  7153. }
  7154. else
  7155. job_penalty = 0;
  7156. if (base_penalty || job_penalty)
  7157. pc_lostexp(sd, base_penalty, job_penalty);
  7158. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  7159. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7160. if(zeny_penalty)
  7161. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7162. }
  7163. }
  7164. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7165. for (const auto &it : mapdata->drop_list) {
  7166. int id = it.drop_id, per = it.drop_per;
  7167. enum e_nightmare_drop_type type = it.drop_type;
  7168. if(id == 0)
  7169. continue;
  7170. if(id == -1){
  7171. int eq_num=0,eq_n[MAX_INVENTORY];
  7172. memset(eq_n,0,sizeof(eq_n));
  7173. for(i=0;i<MAX_INVENTORY;i++) {
  7174. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7175. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7176. || type&NMDT_ALL)
  7177. {
  7178. int l;
  7179. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7180. if( l < MAX_INVENTORY )
  7181. eq_n[l] = i;
  7182. eq_num++;
  7183. }
  7184. }
  7185. if(eq_num > 0){
  7186. int n = eq_n[rnd()%eq_num];
  7187. if(rnd()%10000 < per) {
  7188. if(sd->inventory.u.items_inventory[n].equip)
  7189. pc_unequipitem(sd,n,3);
  7190. pc_dropitem(sd,n,1);
  7191. }
  7192. }
  7193. }
  7194. else if(id > 0) {
  7195. for(i=0;i<MAX_INVENTORY;i++){
  7196. if(sd->inventory.u.items_inventory[i].nameid == id
  7197. && rnd()%10000 < per
  7198. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7199. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7200. || type&NMDT_ALL) ){
  7201. if(sd->inventory.u.items_inventory[i].equip)
  7202. pc_unequipitem(sd,i,3);
  7203. pc_dropitem(sd,i,1);
  7204. break;
  7205. }
  7206. }
  7207. }
  7208. }
  7209. }
  7210. // pvp
  7211. // disable certain pvp functions on pk_mode [Valaris]
  7212. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  7213. sd->pvp_point -= 5;
  7214. sd->pvp_lost++;
  7215. if( src && src->type == BL_PC ) {
  7216. struct map_session_data *ssd = (struct map_session_data *)src;
  7217. ssd->pvp_point++;
  7218. ssd->pvp_won++;
  7219. }
  7220. if( sd->pvp_point < 0 ) {
  7221. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7222. return 1|8;
  7223. }
  7224. }
  7225. //GvG
  7226. if( mapdata_flag_gvg2(mapdata) ) {
  7227. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7228. return 1|8;
  7229. }
  7230. else if( sd->bg_id ) {
  7231. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  7232. if (bg) {
  7233. if (bg->cemetery.map > 0) { // Respawn by BG
  7234. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  7235. return 1|8;
  7236. }
  7237. }
  7238. }
  7239. //Reset "can log out" tick.
  7240. if( battle_config.prevent_logout )
  7241. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7242. return 1;
  7243. }
  7244. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7245. if(hp) clif_updatestatus(sd,SP_HP);
  7246. if(sp) clif_updatestatus(sd,SP_SP);
  7247. pc_setstand(sd, true);
  7248. if(battle_config.pc_invincible_time > 0)
  7249. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7250. if( sd->state.gmaster_flag ) {
  7251. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7252. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7253. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7254. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7255. }
  7256. }
  7257. bool pc_revive_item(struct map_session_data *sd) {
  7258. nullpo_retr(false, sd);
  7259. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  7260. return false;
  7261. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  7262. return false;
  7263. int16 item_position = itemdb_group_item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  7264. uint8 hp = 100, sp = 100;
  7265. if (item_position < 0) {
  7266. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  7267. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  7268. sp = 0;
  7269. }
  7270. else
  7271. return false;
  7272. }
  7273. if (!status_revive(&sd->bl, hp, sp))
  7274. return false;
  7275. if (item_position < 0)
  7276. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE, INVALID_TIMER);
  7277. else
  7278. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  7279. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  7280. return true;
  7281. }
  7282. // script
  7283. //
  7284. /*==========================================
  7285. * script reading pc status registry
  7286. *------------------------------------------*/
  7287. int64 pc_readparam(struct map_session_data* sd,int64 type)
  7288. {
  7289. int64 val = 0;
  7290. nullpo_ret(sd);
  7291. switch(type) {
  7292. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7293. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7294. case SP_ZENY: val = sd->status.zeny; break;
  7295. case SP_BASELEVEL: val = sd->status.base_level; break;
  7296. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7297. case SP_CLASS: val = sd->status.class_; break;
  7298. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7299. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7300. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7301. case SP_SEX: val = sd->status.sex; break;
  7302. case SP_WEIGHT: val = sd->weight; break;
  7303. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7304. case SP_BASEEXP: val = sd->status.base_exp; break;
  7305. case SP_JOBEXP: val = sd->status.job_exp; break;
  7306. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7307. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7308. case SP_HP: val = sd->battle_status.hp; break;
  7309. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7310. case SP_SP: val = sd->battle_status.sp; break;
  7311. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7312. case SP_STR: val = sd->status.str; break;
  7313. case SP_AGI: val = sd->status.agi; break;
  7314. case SP_VIT: val = sd->status.vit; break;
  7315. case SP_INT: val = sd->status.int_; break;
  7316. case SP_DEX: val = sd->status.dex; break;
  7317. case SP_LUK: val = sd->status.luk; break;
  7318. case SP_KARMA: val = sd->status.karma; break;
  7319. case SP_MANNER: val = sd->status.manner; break;
  7320. case SP_FAME: val = sd->status.fame; break;
  7321. case SP_KILLERRID: val = sd->killerrid; break;
  7322. case SP_KILLEDRID: val = sd->killedrid; break;
  7323. case SP_KILLEDGID: val = sd->killedgid; break;
  7324. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7325. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7326. case SP_CHARRENAME: val = sd->status.rename; break;
  7327. case SP_CHARFONT: val = sd->status.font; break;
  7328. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7329. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7330. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7331. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7332. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7333. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7334. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7335. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7336. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  7337. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7338. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7339. case SP_BASE_ATK:
  7340. #ifdef RENEWAL
  7341. val = sd->bonus.eatk;
  7342. #else
  7343. val = sd->battle_status.batk;
  7344. #endif
  7345. break;
  7346. case SP_DEF1: val = sd->battle_status.def; break;
  7347. case SP_DEF2: val = sd->battle_status.def2; break;
  7348. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7349. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7350. case SP_HIT: val = sd->battle_status.hit; break;
  7351. case SP_FLEE1: val = sd->battle_status.flee; break;
  7352. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7353. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7354. case SP_MAXHPRATE: val = sd->hprate; break;
  7355. case SP_MAXSPRATE: val = sd->sprate; break;
  7356. case SP_SPRATE: val = sd->dsprate; break;
  7357. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7358. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7359. case SP_ASPD_RATE:
  7360. #ifndef RENEWAL_ASPD
  7361. val = sd->battle_status.aspd_rate;
  7362. #else
  7363. val = sd->battle_status.aspd_rate2;
  7364. #endif
  7365. break;
  7366. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7367. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7368. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7369. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7370. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7371. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7372. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7373. case SP_MATK_RATE: val = sd->matk_rate; break;
  7374. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7375. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7376. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7377. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7378. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7379. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7380. case SP_HIT_RATE: val = sd->hit_rate; break;
  7381. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7382. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7383. case SP_DEF_RATE: val = sd->def_rate; break;
  7384. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7385. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7386. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7387. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7388. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7389. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7390. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7391. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7392. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7393. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7394. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7395. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7396. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7397. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  7398. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  7399. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7400. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7401. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7402. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7403. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7404. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7405. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7406. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7407. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7408. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7409. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7410. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7411. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7412. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7413. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7414. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7415. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7416. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7417. case SP_DELAYRATE: val = sd->delayrate; break;
  7418. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7419. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7420. case SP_UNSTRIPABLE:
  7421. case SP_UNSTRIPABLE_ARMOR:
  7422. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7423. break;
  7424. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7425. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7426. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7427. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7428. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  7429. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  7430. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7431. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7432. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7433. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7434. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7435. case SP_EMATK: val = sd->bonus.ematk; break;
  7436. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7437. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7438. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7439. case SP_CASTRATE:
  7440. case SP_VARCASTRATE:
  7441. #ifdef RENEWAL_CAST
  7442. val = sd->bonus.varcastrate; break;
  7443. #else
  7444. val = sd->castrate; break;
  7445. #endif
  7446. default:
  7447. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  7448. return -1;
  7449. }
  7450. return val;
  7451. }
  7452. /*==========================================
  7453. * script set pc status registry
  7454. *------------------------------------------*/
  7455. bool pc_setparam(struct map_session_data *sd,int64 type,int64 val_tmp)
  7456. {
  7457. nullpo_retr(false,sd);
  7458. int val = static_cast<unsigned int>(val_tmp);
  7459. switch(type){
  7460. case SP_BASELEVEL:
  7461. if (val > pc_maxbaselv(sd)) //Capping to max
  7462. val = pc_maxbaselv(sd);
  7463. if (val > sd->status.base_level) {
  7464. int i = 0;
  7465. int stat=0;
  7466. for (i = 0; i < (int)(val - sd->status.base_level); i++)
  7467. stat += pc_gets_status_point(sd->status.base_level + i);
  7468. sd->status.status_point += stat;
  7469. }
  7470. sd->status.base_level = val;
  7471. sd->status.base_exp = 0;
  7472. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7473. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7474. clif_updatestatus(sd, SP_STATUSPOINT);
  7475. clif_updatestatus(sd, SP_BASEEXP);
  7476. status_calc_pc(sd, SCO_FORCE);
  7477. if(sd->status.party_id)
  7478. party_send_levelup(sd);
  7479. break;
  7480. case SP_JOBLEVEL:
  7481. if (val >= sd->status.job_level) {
  7482. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7483. sd->status.skill_point += val - sd->status.job_level;
  7484. clif_updatestatus(sd, SP_SKILLPOINT);
  7485. }
  7486. sd->status.job_level = val;
  7487. sd->status.job_exp = 0;
  7488. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7489. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7490. clif_updatestatus(sd, SP_JOBEXP);
  7491. status_calc_pc(sd, SCO_FORCE);
  7492. break;
  7493. case SP_SKILLPOINT:
  7494. sd->status.skill_point = val;
  7495. break;
  7496. case SP_STATUSPOINT:
  7497. sd->status.status_point = val;
  7498. break;
  7499. case SP_ZENY:
  7500. if( val < 0 )
  7501. return false;// can't set negative zeny
  7502. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7503. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7504. break;
  7505. case SP_BASEEXP:
  7506. {
  7507. val = cap_value(val, 0, INT_MAX);
  7508. if (val < sd->status.base_exp) // Lost
  7509. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7510. else // Gained
  7511. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7512. }
  7513. return true;
  7514. case SP_JOBEXP:
  7515. {
  7516. val = cap_value(val, 0, INT_MAX);
  7517. if (val < sd->status.job_exp) // Lost
  7518. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7519. else // Gained
  7520. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7521. }
  7522. return true;
  7523. case SP_SEX:
  7524. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7525. break;
  7526. case SP_WEIGHT:
  7527. sd->weight = val;
  7528. break;
  7529. case SP_MAXWEIGHT:
  7530. sd->max_weight = val;
  7531. break;
  7532. case SP_HP:
  7533. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7534. break;
  7535. case SP_MAXHP:
  7536. if (sd->status.base_level < 100)
  7537. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7538. else if (sd->status.base_level < 151)
  7539. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7540. else
  7541. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7542. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7543. {
  7544. sd->battle_status.hp = sd->battle_status.max_hp;
  7545. clif_updatestatus(sd, SP_HP);
  7546. }
  7547. break;
  7548. case SP_SP:
  7549. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7550. break;
  7551. case SP_MAXSP:
  7552. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7553. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7554. {
  7555. sd->battle_status.sp = sd->battle_status.max_sp;
  7556. clif_updatestatus(sd, SP_SP);
  7557. }
  7558. break;
  7559. case SP_STR:
  7560. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7561. break;
  7562. case SP_AGI:
  7563. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7564. break;
  7565. case SP_VIT:
  7566. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7567. break;
  7568. case SP_INT:
  7569. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7570. break;
  7571. case SP_DEX:
  7572. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7573. break;
  7574. case SP_LUK:
  7575. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7576. break;
  7577. case SP_KARMA:
  7578. sd->status.karma = val;
  7579. break;
  7580. case SP_MANNER:
  7581. sd->status.manner = val;
  7582. if( val < 0 )
  7583. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7584. else {
  7585. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7586. clif_manner_message(sd, 5);
  7587. }
  7588. return true; // status_change_start/status_change_end already sends packets warning the client
  7589. case SP_FAME:
  7590. sd->status.fame = val;
  7591. break;
  7592. case SP_KILLERRID:
  7593. sd->killerrid = val;
  7594. return true;
  7595. case SP_KILLEDRID:
  7596. sd->killedrid = val;
  7597. return true;
  7598. case SP_KILLEDGID:
  7599. sd->killedgid = val;
  7600. return true;
  7601. case SP_CHARMOVE:
  7602. sd->status.character_moves = val;
  7603. return true;
  7604. case SP_CHARRENAME:
  7605. sd->status.rename = val;
  7606. return true;
  7607. case SP_CHARFONT:
  7608. sd->status.font = val;
  7609. clif_font(sd);
  7610. return true;
  7611. case SP_BANK_VAULT:
  7612. if (val < 0)
  7613. return false;
  7614. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7615. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7616. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7617. return true;
  7618. case SP_ROULETTE_BRONZE:
  7619. sd->roulette_point.bronze = val;
  7620. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7621. return true;
  7622. case SP_ROULETTE_SILVER:
  7623. sd->roulette_point.silver = val;
  7624. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7625. return true;
  7626. case SP_ROULETTE_GOLD:
  7627. sd->roulette_point.gold = val;
  7628. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7629. return true;
  7630. case SP_CASHPOINTS:
  7631. if (val < 0)
  7632. return false;
  7633. if (!sd->state.connect_new)
  7634. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7635. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7636. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7637. return true;
  7638. case SP_KAFRAPOINTS:
  7639. if (val < 0)
  7640. return false;
  7641. if (!sd->state.connect_new)
  7642. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7643. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7644. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7645. return true;
  7646. case SP_PCDIECOUNTER:
  7647. if (val < 0)
  7648. return false;
  7649. if (sd->die_counter == val)
  7650. return true;
  7651. sd->die_counter = val;
  7652. if (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7653. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7654. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7655. return true;
  7656. case SP_COOKMASTERY:
  7657. if (val < 0)
  7658. return false;
  7659. if (sd->cook_mastery == val)
  7660. return true;
  7661. val = cap_value(val, 0, 1999);
  7662. sd->cook_mastery = val;
  7663. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7664. return true;
  7665. default:
  7666. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  7667. return false;
  7668. }
  7669. clif_updatestatus(sd,static_cast<int>(type));
  7670. return true;
  7671. }
  7672. /*==========================================
  7673. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7674. *------------------------------------------*/
  7675. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7676. {
  7677. if (type&2) {
  7678. if (hp || type&4)
  7679. clif_heal(sd->fd,SP_HP,hp);
  7680. if (sp)
  7681. clif_heal(sd->fd,SP_SP,sp);
  7682. } else {
  7683. if(hp)
  7684. clif_updatestatus(sd,SP_HP);
  7685. if(sp)
  7686. clif_updatestatus(sd,SP_SP);
  7687. }
  7688. return;
  7689. }
  7690. /**
  7691. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7692. * @param sd: Player data
  7693. * @param itemid: Item ID
  7694. * @param hp: HP to heal
  7695. * @param sp: SP to heal
  7696. * @return Amount healed to an object
  7697. */
  7698. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7699. {
  7700. int bonus, tmp, penalty = 0;
  7701. if (hp) {
  7702. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7703. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7704. if (potion_flag == 2) {
  7705. bonus += 50;
  7706. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7707. bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
  7708. }
  7709. //All item bonuses.
  7710. bonus += sd->bonus.itemhealrate2;
  7711. //Item Group bonuses
  7712. bonus += pc_get_itemgroup_bonus(sd, itemid);
  7713. //Individual item bonuses.
  7714. for(const auto &it : sd->itemhealrate) {
  7715. if (it.id == itemid) {
  7716. bonus += it.val;
  7717. break;
  7718. }
  7719. }
  7720. // Recovery Potion
  7721. if (sd->sc.data[SC_INCHEALRATE])
  7722. bonus += sd->sc.data[SC_INCHEALRATE]->val1;
  7723. // 2014 Halloween Event : Pumpkin Bonus
  7724. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7725. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
  7726. tmp = hp * bonus / 100; // Overflow check
  7727. if (bonus != 100 && tmp > hp)
  7728. hp = tmp;
  7729. }
  7730. if (sp) {
  7731. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7732. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7733. if (potion_flag == 2)
  7734. bonus += 50;
  7735. tmp = sp * bonus / 100; // Overflow check
  7736. if (bonus != 100 && tmp > sp)
  7737. sp = tmp;
  7738. }
  7739. if (sd->sc.count) {
  7740. // Critical Wound and Death Hurt stack
  7741. if (sd->sc.data[SC_CRITICALWOUND])
  7742. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7743. if (sd->sc.data[SC_DEATHHURT])
  7744. penalty += 20;
  7745. if (sd->sc.data[SC_NORECOVER_STATE])
  7746. penalty = 100;
  7747. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7748. hp += hp / 2; // 1.5 times
  7749. sp -= sp / 2;
  7750. }
  7751. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7752. hp += hp / 10;
  7753. sp += sp / 10;
  7754. }
  7755. #ifdef RENEWAL
  7756. if (sd->sc.data[SC_APPLEIDUN])
  7757. hp += sd->sc.data[SC_APPLEIDUN]->val3 / 100;
  7758. #endif
  7759. if (penalty > 0) {
  7760. hp -= hp * penalty / 100;
  7761. sp -= sp * penalty / 100;
  7762. }
  7763. #ifdef RENEWAL
  7764. if (sd->sc.data[SC_EXTREMITYFIST2])
  7765. sp = 0;
  7766. #endif
  7767. if (sd->sc.data[SC_BITESCAR])
  7768. hp = 0;
  7769. }
  7770. return status_heal(&sd->bl, hp, sp, 1);
  7771. }
  7772. /*==========================================
  7773. * HP/SP Recovery
  7774. * Heal player hp nad/or sp by rate
  7775. *------------------------------------------*/
  7776. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7777. {
  7778. nullpo_ret(sd);
  7779. if (hp > 100) hp = 100;
  7780. else if (hp <-100) hp = -100;
  7781. if (sp > 100) sp = 100;
  7782. else if (sp <-100) sp = -100;
  7783. if(hp >= 0 && sp >= 0) //Heal
  7784. return status_percent_heal(&sd->bl, hp, sp);
  7785. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7786. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7787. //Crossed signs
  7788. if(hp) {
  7789. if(hp > 0)
  7790. status_percent_heal(&sd->bl, hp, 0);
  7791. else
  7792. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7793. }
  7794. if(sp) {
  7795. if(sp > 0)
  7796. status_percent_heal(&sd->bl, 0, sp);
  7797. else
  7798. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7799. }
  7800. return 0;
  7801. }
  7802. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7803. {
  7804. struct mob_data *md;
  7805. int flag;
  7806. md = (struct mob_data *)bl;
  7807. nullpo_ret(md);
  7808. flag = va_arg(ap, int);
  7809. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7810. status_kill(&md->bl);
  7811. return 1;
  7812. }
  7813. /**
  7814. * Called when player changes job
  7815. * Rewrote to make it tidider [Celest]
  7816. * @param sd
  7817. * @param job JOB ID. See enum e_job
  7818. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7819. * @return True if success, false if failed
  7820. **/
  7821. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7822. {
  7823. int i, fame_flag = 0;
  7824. int b_class;
  7825. nullpo_retr(false,sd);
  7826. if (job < 0)
  7827. return false;
  7828. //Normalize job.
  7829. b_class = pc_jobid2mapid(job);
  7830. if (b_class == -1)
  7831. return false;
  7832. switch (upper) {
  7833. case 1:
  7834. b_class|= JOBL_UPPER;
  7835. break;
  7836. case 2:
  7837. b_class|= JOBL_BABY;
  7838. break;
  7839. }
  7840. //This will automatically adjust bard/dancer classes to the correct gender
  7841. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7842. job = pc_mapid2jobid(b_class, sd->status.sex);
  7843. if (job == -1)
  7844. return false;
  7845. if ((unsigned short)b_class == sd->class_)
  7846. return false; //Nothing to change.
  7847. // changing from 1st to 2nd job
  7848. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7849. sd->change_level_2nd = sd->status.job_level;
  7850. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  7851. }
  7852. // changing from 2nd to 3rd job
  7853. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7854. sd->change_level_3rd = sd->status.job_level;
  7855. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  7856. }
  7857. if(sd->cloneskill_idx > 0) {
  7858. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7859. sd->status.skill[sd->cloneskill_idx].id = 0;
  7860. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7861. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7862. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM))));
  7863. }
  7864. sd->cloneskill_idx = 0;
  7865. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7866. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7867. }
  7868. if(sd->reproduceskill_idx > 0) {
  7869. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7870. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7871. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7872. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7873. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE))));
  7874. }
  7875. sd->reproduceskill_idx = 0;
  7876. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7877. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7878. }
  7879. // Give or reduce transcendent status points
  7880. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7881. sd->status.status_point += battle_config.transcendent_status_points;
  7882. clif_updatestatus(sd,SP_STATUSPOINT);
  7883. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7884. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7885. // The player already used his bonus points, so we have to reset his status points
  7886. pc_resetstate(sd);
  7887. }
  7888. sd->status.status_point -= battle_config.transcendent_status_points;
  7889. clif_updatestatus(sd,SP_STATUSPOINT);
  7890. }
  7891. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7892. const int class_ = pc_class2idx(sd->status.class_);
  7893. uint16 skill_id;
  7894. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7895. //Remove status specific to your current tree skills.
  7896. enum sc_type sc = status_skill2sc(skill_id);
  7897. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7898. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7899. }
  7900. }
  7901. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7902. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7903. pc_resetfeel(sd);
  7904. }
  7905. // Reset body style to 0 before changing job to avoid
  7906. // errors since not every job has a alternate outfit.
  7907. sd->status.body = 0;
  7908. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7909. sd->status.class_ = job;
  7910. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7911. sd->class_ = (unsigned short)b_class;
  7912. sd->status.job_level=1;
  7913. sd->status.job_exp=0;
  7914. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7915. sd->status.base_level = pc_maxbaselv(sd);
  7916. sd->status.base_exp=0;
  7917. pc_resetstate(sd);
  7918. clif_updatestatus(sd,SP_STATUSPOINT);
  7919. clif_updatestatus(sd,SP_BASELEVEL);
  7920. clif_updatestatus(sd,SP_BASEEXP);
  7921. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7922. }
  7923. clif_updatestatus(sd,SP_JOBLEVEL);
  7924. clif_updatestatus(sd,SP_JOBEXP);
  7925. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7926. for(i=0;i<EQI_MAX;i++) {
  7927. if(sd->equip_index[i] >= 0)
  7928. if(pc_isequip(sd,sd->equip_index[i]))
  7929. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7930. }
  7931. //Change look, if disguised, you need to undisguise
  7932. //to correctly calculate new job sprite without
  7933. if (sd->disguise)
  7934. pc_disguise(sd, 0);
  7935. status_set_viewdata(&sd->bl, job);
  7936. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7937. #if PACKETVER >= 20151001
  7938. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7939. #endif
  7940. if(sd->vd.cloth_color)
  7941. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7942. /*
  7943. if(sd->vd.body_style)
  7944. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7945. */
  7946. //Update skill tree.
  7947. pc_calc_skilltree(sd);
  7948. clif_skillinfoblock(sd);
  7949. if (sd->ed)
  7950. elemental_delete(sd->ed);
  7951. if (sd->state.vending)
  7952. vending_closevending(sd);
  7953. if (sd->state.buyingstore)
  7954. buyingstore_close(sd);
  7955. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7956. //Remove peco/cart/falcon
  7957. i = sd->sc.option;
  7958. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7959. i&=~OPTION_RIDING;
  7960. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7961. i&=~OPTION_FALCON;
  7962. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7963. i&=~OPTION_DRAGON;
  7964. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7965. i&=~OPTION_WUGRIDER;
  7966. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7967. i&=~OPTION_WUG;
  7968. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7969. i&=~OPTION_MADOGEAR;
  7970. #ifndef NEW_CARTS
  7971. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7972. i&=~OPTION_CART;
  7973. #else
  7974. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7975. pc_setcart(sd, 0);
  7976. #endif
  7977. if(i != sd->sc.option)
  7978. pc_setoption(sd, i);
  7979. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7980. hom_vaporize(sd, HOM_ST_ACTIVE);
  7981. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7982. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7983. if(sd->status.manner < 0)
  7984. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7985. status_calc_pc(sd,SCO_FORCE);
  7986. pc_checkallowskill(sd);
  7987. pc_equiplookall(sd);
  7988. pc_show_questinfo(sd);
  7989. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  7990. if( sd->status.party_id ){
  7991. struct party_data* p;
  7992. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  7993. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  7994. if( i < MAX_PARTY ){
  7995. p->party.member[i].class_ = sd->status.class_;
  7996. clif_party_job_and_level(sd);
  7997. }
  7998. }
  7999. }
  8000. chrif_save(sd, CSAVE_NORMAL);
  8001. //if you were previously famous, not anymore.
  8002. if (fame_flag)
  8003. chrif_buildfamelist();
  8004. else if (sd->status.fame > 0) {
  8005. //It may be that now they are famous?
  8006. switch (sd->class_&MAPID_UPPERMASK) {
  8007. case MAPID_BLACKSMITH:
  8008. case MAPID_ALCHEMIST:
  8009. case MAPID_TAEKWON:
  8010. chrif_buildfamelist();
  8011. break;
  8012. }
  8013. }
  8014. return true;
  8015. }
  8016. /*==========================================
  8017. * Tell client player sd has change equipement
  8018. *------------------------------------------*/
  8019. void pc_equiplookall(struct map_session_data *sd)
  8020. {
  8021. nullpo_retv(sd);
  8022. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8023. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8024. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8025. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8026. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8027. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  8028. }
  8029. /*==========================================
  8030. * Tell client player sd has change look (hair,equip...)
  8031. *------------------------------------------*/
  8032. void pc_changelook(struct map_session_data *sd,int type,int val) {
  8033. nullpo_retv(sd);
  8034. switch(type) {
  8035. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  8036. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  8037. if (sd->status.hair != val) {
  8038. sd->status.hair = val;
  8039. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8040. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8041. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  8042. }
  8043. break;
  8044. case LOOK_WEAPON:
  8045. sd->status.weapon = val;
  8046. break;
  8047. case LOOK_HEAD_BOTTOM:
  8048. sd->status.head_bottom = val;
  8049. sd->setlook_head_bottom = val;
  8050. break;
  8051. case LOOK_HEAD_TOP:
  8052. sd->status.head_top = val;
  8053. sd->setlook_head_top = val;
  8054. break;
  8055. case LOOK_HEAD_MID:
  8056. sd->status.head_mid = val;
  8057. sd->setlook_head_mid = val;
  8058. break;
  8059. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  8060. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  8061. if (sd->status.hair_color != val) {
  8062. sd->status.hair_color = val;
  8063. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8064. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8065. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  8066. }
  8067. break;
  8068. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  8069. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  8070. sd->status.clothes_color = val;
  8071. break;
  8072. case LOOK_SHIELD:
  8073. sd->status.shield = val;
  8074. break;
  8075. case LOOK_SHOES:
  8076. break;
  8077. case LOOK_ROBE:
  8078. sd->status.robe = val;
  8079. sd->setlook_robe = val;
  8080. break;
  8081. case LOOK_BODY2:
  8082. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  8083. sd->status.body = val;
  8084. break;
  8085. }
  8086. clif_changelook(&sd->bl, type, val);
  8087. }
  8088. /*==========================================
  8089. * Give an option (type) to player (sd) and display it to client
  8090. *------------------------------------------*/
  8091. void pc_setoption(struct map_session_data *sd,int type)
  8092. {
  8093. int p_type, new_look=0;
  8094. nullpo_retv(sd);
  8095. p_type = sd->sc.option;
  8096. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  8097. sd->sc.option=type;
  8098. clif_changeoption(&sd->bl);
  8099. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8100. { // Mounting
  8101. clif_status_load(&sd->bl,EFST_RIDING,1);
  8102. status_calc_pc(sd,SCO_NONE);
  8103. }
  8104. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8105. { // Dismount
  8106. clif_status_load(&sd->bl,EFST_RIDING,0);
  8107. status_calc_pc(sd,SCO_NONE);
  8108. }
  8109. #ifndef NEW_CARTS
  8110. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  8111. clif_cartlist(sd);
  8112. clif_updatestatus(sd, SP_CARTINFO);
  8113. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8114. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  8115. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  8116. clif_clearcart(sd->fd);
  8117. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8118. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  8119. }
  8120. #endif
  8121. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  8122. clif_status_load(&sd->bl,EFST_FALCON,1);
  8123. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  8124. clif_status_load(&sd->bl,EFST_FALCON,0);
  8125. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  8126. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  8127. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  8128. status_calc_pc(sd,SCO_NONE);
  8129. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  8130. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  8131. status_calc_pc(sd,SCO_NONE);
  8132. }
  8133. }
  8134. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  8135. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  8136. static const sc_type statuses [] = { SC_MAXIMIZEPOWER, SC_OVERTHRUST, SC_WEAPONPERFECTION, SC_ADRENALINE, SC_CARTBOOST, SC_MELTDOWN, SC_MAXOVERTHRUST };
  8137. status_calc_pc(sd,SCO_NONE);
  8138. for (uint8 i = 0; i < ARRAYLENGTH(statuses); i++) {
  8139. int skill_id = status_sc2skill(statuses[i]);
  8140. if (skill_id > 0 && !skill_get_inf2(skill_id, INF2_ALLOWONMADO))
  8141. status_change_end(&sd->bl,statuses[i],INVALID_TIMER);
  8142. }
  8143. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8144. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  8145. status_calc_pc(sd,SCO_NONE);
  8146. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  8147. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  8148. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  8149. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  8150. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  8151. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  8152. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  8153. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  8154. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8155. }
  8156. }
  8157. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  8158. new_look = JOB_STAR_GLADIATOR2;
  8159. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  8160. new_look = -1;
  8161. if (sd->disguise || !new_look)
  8162. return; //Disguises break sprite changes
  8163. if (new_look < 0) { //Restore normal look.
  8164. status_set_viewdata(&sd->bl, sd->status.class_);
  8165. new_look = sd->vd.class_;
  8166. }
  8167. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  8168. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  8169. if (sd->vd.cloth_color)
  8170. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8171. if( sd->vd.body_style )
  8172. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8173. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  8174. }
  8175. /**
  8176. * Give player a cart
  8177. * @param sd Player
  8178. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8179. **/
  8180. bool pc_setcart(struct map_session_data *sd,int type) {
  8181. #ifndef NEW_CARTS
  8182. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8183. int option;
  8184. #endif
  8185. nullpo_retr(false,sd);
  8186. if( type < 0 || type > MAX_CARTS )
  8187. return false;// Never trust the values sent by the client! [Skotlex]
  8188. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8189. return false;// Push cart is required
  8190. #ifdef NEW_CARTS
  8191. switch( type ) {
  8192. case 0:
  8193. if( !sd->sc.data[SC_PUSH_CART] )
  8194. return 0;
  8195. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8196. clif_clearcart(sd->fd);
  8197. break;
  8198. default:/* everything else is an allowed ID so we can move on */
  8199. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8200. clif_cartlist(sd);
  8201. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8202. }
  8203. clif_updatestatus(sd, SP_CARTINFO);
  8204. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8205. break;
  8206. }
  8207. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8208. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8209. #else
  8210. // Update option
  8211. option = sd->sc.option;
  8212. option &= ~OPTION_CART;// clear cart bits
  8213. option |= cart[type]; // set cart
  8214. pc_setoption(sd, option);
  8215. #endif
  8216. return true;
  8217. }
  8218. /*==========================================
  8219. * Give player a falcon
  8220. *------------------------------------------*/
  8221. void pc_setfalcon(struct map_session_data* sd, int flag)
  8222. {
  8223. if( flag ){
  8224. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8225. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8226. } else if( pc_isfalcon(sd) ){
  8227. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8228. }
  8229. }
  8230. /*==========================================
  8231. * Set player riding
  8232. *------------------------------------------*/
  8233. void pc_setriding(struct map_session_data* sd, int flag)
  8234. {
  8235. if( sd->sc.data[SC_ALL_RIDING] )
  8236. return;
  8237. if( flag ){
  8238. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8239. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8240. } else if( pc_isriding(sd) ){
  8241. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8242. }
  8243. }
  8244. /*==========================================
  8245. * Give player a mado
  8246. *------------------------------------------*/
  8247. void pc_setmadogear(struct map_session_data* sd, int flag)
  8248. {
  8249. if( flag ){
  8250. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8251. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8252. } else if( pc_ismadogear(sd) ){
  8253. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8254. }
  8255. }
  8256. /*==========================================
  8257. * Check if player can drop an item
  8258. *------------------------------------------*/
  8259. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8260. {
  8261. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  8262. return false;
  8263. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8264. return false;
  8265. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8266. }
  8267. /**
  8268. * Determines whether a player can attack based on status changes
  8269. * Why not use status_check_skilluse?
  8270. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8271. * Even ground-based attacks should be blocked by these statuses
  8272. * Called from unit_attack and unit_attack_timer_sub
  8273. * @retval true Can attack
  8274. **/
  8275. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8276. nullpo_retr(false, sd);
  8277. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8278. return false;
  8279. if (sd->state.block_action & PCBLOCK_ATTACK)
  8280. return false;
  8281. if(
  8282. #ifdef RENEWAL
  8283. sd->sc.data[SC_BASILICA_CELL] ||
  8284. #else
  8285. sd->sc.data[SC_BASILICA] ||
  8286. #endif
  8287. sd->sc.data[SC__SHADOWFORM] ||
  8288. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8289. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8290. sd->sc.data[SC_CRYSTALIZE] ||
  8291. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8292. sd->sc.data[SC_TRICKDEAD] ||
  8293. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8294. sd->sc.data[SC_BLADESTOP] ||
  8295. sd->sc.data[SC_DEEPSLEEP] ||
  8296. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8297. sd->sc.data[SC_KINGS_GRACE] )
  8298. return false;
  8299. return true;
  8300. }
  8301. /*==========================================
  8302. * Read '@type' variables (temporary numeric char reg)
  8303. *------------------------------------------*/
  8304. int64 pc_readreg(struct map_session_data* sd, int64 reg)
  8305. {
  8306. return i64db_i64get(sd->regs.vars, reg);
  8307. }
  8308. /*==========================================
  8309. * Set '@type' variables (temporary numeric char reg)
  8310. *------------------------------------------*/
  8311. bool pc_setreg(struct map_session_data* sd, int64 reg, int64 val)
  8312. {
  8313. uint32 index = script_getvaridx(reg);
  8314. nullpo_retr(false, sd);
  8315. if( val ) {
  8316. i64db_i64put(sd->regs.vars, reg, val);
  8317. if( index )
  8318. script_array_update(&sd->regs, reg, false);
  8319. } else {
  8320. i64db_remove(sd->regs.vars, reg);
  8321. if( index )
  8322. script_array_update(&sd->regs, reg, true);
  8323. }
  8324. return true;
  8325. }
  8326. /*==========================================
  8327. * Read '@type$' variables (temporary string char reg)
  8328. *------------------------------------------*/
  8329. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8330. {
  8331. struct script_reg_str *p = NULL;
  8332. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8333. return p ? p->value : NULL;
  8334. }
  8335. /*==========================================
  8336. * Set '@type$' variables (temporary string char reg)
  8337. *------------------------------------------*/
  8338. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8339. {
  8340. struct script_reg_str *p = NULL;
  8341. unsigned int index = script_getvaridx(reg);
  8342. DBData prev;
  8343. nullpo_retr(false, sd);
  8344. if( str[0] ) {
  8345. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8346. p->value = aStrdup(str);
  8347. p->flag.type = 1;
  8348. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8349. p = (struct script_reg_str *)db_data2ptr(&prev);
  8350. if( p->value )
  8351. aFree(p->value);
  8352. ers_free(str_reg_ers, p);
  8353. } else {
  8354. if( index )
  8355. script_array_update(&sd->regs, reg, false);
  8356. }
  8357. } else {
  8358. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8359. p = (struct script_reg_str *)db_data2ptr(&prev);
  8360. if( p->value )
  8361. aFree(p->value);
  8362. ers_free(str_reg_ers, p);
  8363. if( index )
  8364. script_array_update(&sd->regs, reg, true);
  8365. }
  8366. }
  8367. return true;
  8368. }
  8369. /**
  8370. * Serves the following variable types:
  8371. * - 'type' (permanent numeric char reg)
  8372. * - '#type' (permanent numeric account reg)
  8373. * - '##type' (permanent numeric account reg2)
  8374. **/
  8375. int64 pc_readregistry(struct map_session_data *sd, int64 reg)
  8376. {
  8377. struct script_reg_num *p = NULL;
  8378. if (!sd->vars_ok) {
  8379. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8380. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8381. //intif->request_registry(sd,type==3?4:type);
  8382. set_eof(sd->fd);
  8383. return 0;
  8384. }
  8385. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8386. return p ? p->value : 0;
  8387. }
  8388. /**
  8389. * Serves the following variable types:
  8390. * - 'type$' (permanent str char reg)
  8391. * - '#type$' (permanent str account reg)
  8392. * - '##type$' (permanent str account reg2)
  8393. **/
  8394. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8395. {
  8396. struct script_reg_str *p = NULL;
  8397. if (!sd->vars_ok) {
  8398. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8399. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8400. //intif->request_registry(sd,type==3?4:type);
  8401. set_eof(sd->fd);
  8402. return NULL;
  8403. }
  8404. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8405. return p ? p->value : NULL;
  8406. }
  8407. /**
  8408. * Serves the following variable types:
  8409. * - 'type' (permanent numeric char reg)
  8410. * - '#type' (permanent numeric account reg)
  8411. * - '##type' (permanent numeric account reg2)
  8412. **/
  8413. bool pc_setregistry(struct map_session_data *sd, int64 reg, int64 val)
  8414. {
  8415. struct script_reg_num *p = NULL;
  8416. const char *regname = get_str(script_getvarid(reg));
  8417. uint32 index = script_getvaridx(reg);
  8418. if ( !reg_load && !sd->vars_ok ) {
  8419. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8420. return false;
  8421. }
  8422. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8423. if( val ) {
  8424. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8425. script_array_update(&sd->regs, reg, false);
  8426. p->value = val;
  8427. } else {
  8428. p->value = 0;
  8429. if( index )
  8430. script_array_update(&sd->regs, reg, true);
  8431. }
  8432. if (!reg_load)
  8433. p->flag.update = 1;/* either way, it will require either delete or replace */
  8434. } else if( val ) {
  8435. DBData prev;
  8436. if( index )
  8437. script_array_update(&sd->regs, reg, false);
  8438. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8439. p->value = val;
  8440. if (!reg_load)
  8441. p->flag.update = 1;
  8442. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8443. p = (struct script_reg_num *)db_data2ptr(&prev);
  8444. ers_free(num_reg_ers, p);
  8445. }
  8446. }
  8447. if (!reg_load && p)
  8448. sd->vars_dirty = true;
  8449. return true;
  8450. }
  8451. /**
  8452. * Serves the following variable types:
  8453. * - 'type$' (permanent str char reg)
  8454. * - '#type$' (permanent str account reg)
  8455. * - '##type$' (permanent str account reg2)
  8456. **/
  8457. bool pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8458. {
  8459. struct script_reg_str *p = NULL;
  8460. const char *regname = get_str(script_getvarid(reg));
  8461. unsigned int index = script_getvaridx(reg);
  8462. size_t vlen = 0;
  8463. if (!reg_load && !sd->vars_ok) {
  8464. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8465. return false;
  8466. }
  8467. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8468. {
  8469. 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);
  8470. return false;
  8471. }
  8472. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8473. if( val[0] ) {
  8474. if( p->value )
  8475. aFree(p->value);
  8476. else if ( index ) // an entry that was deleted, so we reset
  8477. script_array_update(&sd->regs, reg, false);
  8478. p->value = aStrdup(val);
  8479. } else {
  8480. if (p->value)
  8481. aFree(p->value);
  8482. p->value = NULL;
  8483. if( index )
  8484. script_array_update(&sd->regs, reg, true);
  8485. }
  8486. if( !reg_load )
  8487. p->flag.update = 1; // either way, it will require either delete or replace
  8488. } else if( val[0] ) {
  8489. DBData prev;
  8490. if( index )
  8491. script_array_update(&sd->regs, reg, false);
  8492. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8493. p->value = aStrdup(val);
  8494. if( !reg_load )
  8495. p->flag.update = 1;
  8496. p->flag.type = 1;
  8497. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8498. p = (struct script_reg_str *)db_data2ptr(&prev);
  8499. if( p->value )
  8500. aFree(p->value);
  8501. ers_free(str_reg_ers, p);
  8502. }
  8503. }
  8504. if( !reg_load && p )
  8505. sd->vars_dirty = true;
  8506. return true;
  8507. }
  8508. /**
  8509. * Set value of player variable
  8510. * @param sd Player
  8511. * @param reg Variable name
  8512. * @param value
  8513. * @return True if success, false if failed.
  8514. **/
  8515. bool pc_setreg2(struct map_session_data *sd, const char *reg, int64 val) {
  8516. char prefix = reg[0];
  8517. nullpo_retr(false, sd);
  8518. if (reg[strlen(reg)-1] == '$') {
  8519. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8520. return false;
  8521. }
  8522. val = cap_value(val, INT_MIN, INT_MAX);
  8523. switch (prefix) {
  8524. case '.':
  8525. case '\'':
  8526. case '$':
  8527. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8528. return false;
  8529. case '@':
  8530. return pc_setreg(sd, add_str(reg), val);
  8531. case '#':
  8532. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  8533. default:
  8534. return pc_setglobalreg(sd, add_str(reg), val);
  8535. }
  8536. return false;
  8537. }
  8538. /**
  8539. * Get value of player variable
  8540. * @param sd Player
  8541. * @param reg Variable name
  8542. * @return Variable value or 0 if failed.
  8543. **/
  8544. int64 pc_readreg2(struct map_session_data *sd, const char *reg) {
  8545. char prefix = reg[0];
  8546. nullpo_ret(sd);
  8547. if (reg[strlen(reg)-1] == '$') {
  8548. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8549. return 0;
  8550. }
  8551. switch (prefix) {
  8552. case '.':
  8553. case '\'':
  8554. case '$':
  8555. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8556. return 0;
  8557. case '@':
  8558. return pc_readreg(sd, add_str(reg));
  8559. case '#':
  8560. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8561. default:
  8562. return pc_readglobalreg(sd, add_str(reg));
  8563. }
  8564. return 0;
  8565. }
  8566. /*==========================================
  8567. * Exec eventtimer for player sd (retrieved from map_session (id))
  8568. *------------------------------------------*/
  8569. static TIMER_FUNC(pc_eventtimer){
  8570. struct map_session_data *sd=map_id2sd(id);
  8571. char *p = (char *)data;
  8572. int i;
  8573. if(sd==NULL)
  8574. return 0;
  8575. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8576. if( i < MAX_EVENTTIMER )
  8577. {
  8578. sd->eventtimer[i] = INVALID_TIMER;
  8579. sd->eventcount--;
  8580. npc_event(sd,p,0);
  8581. }
  8582. else
  8583. ShowError("pc_eventtimer: no such event timer\n");
  8584. if (p) aFree(p);
  8585. return 0;
  8586. }
  8587. /*==========================================
  8588. * Add eventtimer for player sd ?
  8589. *------------------------------------------*/
  8590. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8591. {
  8592. int i;
  8593. nullpo_retr(false,sd);
  8594. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8595. if( i == MAX_EVENTTIMER )
  8596. return false;
  8597. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8598. sd->eventcount++;
  8599. return true;
  8600. }
  8601. /*==========================================
  8602. * Del eventtimer for player sd ?
  8603. *------------------------------------------*/
  8604. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8605. {
  8606. char* p = NULL;
  8607. int i;
  8608. nullpo_retr(false,sd);
  8609. if (sd->eventcount == 0)
  8610. return false;
  8611. // find the named event timer
  8612. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8613. sd->eventtimer[i] != INVALID_TIMER &&
  8614. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8615. strcmp(p, name) == 0
  8616. );
  8617. if( i == MAX_EVENTTIMER )
  8618. return false; // not found
  8619. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8620. sd->eventtimer[i] = INVALID_TIMER;
  8621. if(sd->eventcount > 0)
  8622. sd->eventcount--;
  8623. aFree(p);
  8624. return true;
  8625. }
  8626. /*==========================================
  8627. * Update eventtimer count for player sd
  8628. *------------------------------------------*/
  8629. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8630. {
  8631. int i;
  8632. nullpo_retv(sd);
  8633. for(i=0;i<MAX_EVENTTIMER;i++)
  8634. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8635. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8636. addt_tickimer(sd->eventtimer[i],tick);
  8637. break;
  8638. }
  8639. }
  8640. /*==========================================
  8641. * Remove all eventtimer for player sd
  8642. *------------------------------------------*/
  8643. void pc_cleareventtimer(struct map_session_data *sd)
  8644. {
  8645. int i;
  8646. nullpo_retv(sd);
  8647. if (sd->eventcount == 0)
  8648. return;
  8649. for(i=0;i<MAX_EVENTTIMER;i++){
  8650. if( sd->eventtimer[i] != INVALID_TIMER ){
  8651. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8652. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8653. sd->eventtimer[i] = INVALID_TIMER;
  8654. if(sd->eventcount > 0) //avoid looping to max val
  8655. sd->eventcount--;
  8656. if (p) aFree(p);
  8657. }
  8658. }
  8659. }
  8660. /**
  8661. * Called when an item with combo is worn
  8662. * @param *sd
  8663. * @param *data struct item_data
  8664. * @return success numbers of succeed combo
  8665. */
  8666. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8667. uint16 i;
  8668. int success = 0;
  8669. for( i = 0; i < data->combos_count; i++ ) {
  8670. struct itemchk {
  8671. int idx;
  8672. unsigned short nameid;
  8673. short card[MAX_SLOTS];
  8674. } *combo_idx;
  8675. int idx, j;
  8676. int nb_itemCombo;
  8677. unsigned int pos = 0;
  8678. /* ensure this isn't a duplicate combo */
  8679. if( sd->combos.bonus != NULL ) {
  8680. int x;
  8681. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8682. /* found a match, skip this combo */
  8683. if( x < sd->combos.count )
  8684. continue;
  8685. }
  8686. nb_itemCombo = data->combos[i]->count;
  8687. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8688. continue;
  8689. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8690. for(j=0; j < nb_itemCombo; j++){
  8691. combo_idx[j].idx=-1;
  8692. combo_idx[j].nameid=-1;
  8693. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8694. }
  8695. for( j = 0; j < nb_itemCombo; j++ ) {
  8696. uint16 id = data->combos[i]->nameid[j], k;
  8697. bool found = false;
  8698. for( k = 0; k < EQI_MAX; k++ ) {
  8699. short index = sd->equip_index[k];
  8700. if( index < 0 )
  8701. continue;
  8702. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8703. continue;
  8704. if (!sd->inventory_data[index] )
  8705. continue;
  8706. if ( itemdb_type(id) != IT_CARD ) {
  8707. if ( sd->inventory_data[index]->nameid != id )
  8708. continue;
  8709. if(j>0){ //check if this item not already used
  8710. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8711. uint8 z;
  8712. for (z = 0; z < nb_itemCombo-1; z++)
  8713. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8714. do_continue=true;
  8715. if(do_continue)
  8716. continue;
  8717. }
  8718. combo_idx[j].nameid = id;
  8719. combo_idx[j].idx = index;
  8720. pos |= sd->inventory.u.items_inventory[index].equip;
  8721. found = true;
  8722. break;
  8723. } else { //Cards and enchants
  8724. uint16 z;
  8725. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8726. continue;
  8727. for (z = 0; z < MAX_SLOTS; z++) {
  8728. bool do_continue=false;
  8729. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8730. continue;
  8731. if(j>0){
  8732. int c1, c2;
  8733. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8734. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8735. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8736. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8737. do_continue = true;
  8738. break;
  8739. }
  8740. }
  8741. }
  8742. }
  8743. }
  8744. if(do_continue)
  8745. continue;
  8746. combo_idx[j].nameid = id;
  8747. combo_idx[j].idx = index;
  8748. combo_idx[j].card[z] = id;
  8749. pos |= sd->inventory.u.items_inventory[index].equip;
  8750. found = true;
  8751. break;
  8752. }
  8753. }
  8754. }
  8755. if( !found )
  8756. break;/* we haven't found all the ids for this combo, so we can return */
  8757. }
  8758. aFree(combo_idx);
  8759. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8760. if( j < nb_itemCombo )
  8761. continue;
  8762. /* we got here, means all items in the combo are matching */
  8763. idx = sd->combos.count;
  8764. if( sd->combos.bonus == NULL ) {
  8765. CREATE(sd->combos.bonus, struct script_code *, 1);
  8766. CREATE(sd->combos.id, unsigned short, 1);
  8767. CREATE(sd->combos.pos, unsigned int, 1);
  8768. sd->combos.count = 1;
  8769. } else {
  8770. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8771. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8772. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8773. }
  8774. /* we simply copy the pointer */
  8775. sd->combos.bonus[idx] = data->combos[i]->script;
  8776. /* save this combo's id */
  8777. sd->combos.id[idx] = data->combos[i]->id;
  8778. /* save pos of combo*/
  8779. sd->combos.pos[idx] = pos;
  8780. success++;
  8781. }
  8782. return success;
  8783. }
  8784. /**
  8785. * Called when an item with combo is removed
  8786. * @param *sd
  8787. * @param *data struct item_data
  8788. * @return retval numbers of removed combo
  8789. */
  8790. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8791. int i, retval = 0;
  8792. if( sd->combos.bonus == NULL )
  8793. return 0;/* nothing to do here, player has no combos */
  8794. for( i = 0; i < data->combos_count; i++ ) {
  8795. /* check if this combo exists in this user */
  8796. int x = 0, cursor = 0, j;
  8797. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8798. /* no match, skip this combo */
  8799. if(x >= sd->combos.count)
  8800. continue;
  8801. sd->combos.bonus[x] = NULL;
  8802. sd->combos.id[x] = 0;
  8803. sd->combos.pos[x] = 0;
  8804. retval++;
  8805. /* check if combo requirements still fit */
  8806. if( pc_checkcombo( sd, data ) )
  8807. continue;
  8808. /* move next value to empty slot */
  8809. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8810. if( sd->combos.bonus[j] == NULL )
  8811. continue;
  8812. if( cursor != j ) {
  8813. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8814. sd->combos.id[cursor] = sd->combos.id[j];
  8815. sd->combos.pos[cursor] = sd->combos.pos[j];
  8816. }
  8817. cursor++;
  8818. }
  8819. /* it's empty, we can clear all the memory */
  8820. if( (sd->combos.count = cursor) == 0 ) {
  8821. aFree(sd->combos.bonus);
  8822. aFree(sd->combos.id);
  8823. aFree(sd->combos.pos);
  8824. sd->combos.bonus = NULL;
  8825. sd->combos.id = NULL;
  8826. sd->combos.pos = NULL;
  8827. return retval; /* we also can return at this point for we have no more combos to check */
  8828. }
  8829. }
  8830. return retval;
  8831. }
  8832. /**
  8833. * Load combo data(s) of player
  8834. * @param *sd
  8835. * @return ret numbers of succeed combo
  8836. */
  8837. int pc_load_combo(struct map_session_data *sd) {
  8838. int i, ret = 0;
  8839. for( i = 0; i < EQI_MAX; i++ ) {
  8840. struct item_data *id = NULL;
  8841. short idx = sd->equip_index[i];
  8842. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8843. continue;
  8844. if( id->combos_count )
  8845. ret += pc_checkcombo(sd,id);
  8846. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8847. struct item_data *data;
  8848. int j;
  8849. for( j = 0; j < MAX_SLOTS; j++ ) {
  8850. if (!sd->inventory.u.items_inventory[idx].card[j])
  8851. continue;
  8852. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8853. if( data->combos_count )
  8854. ret += pc_checkcombo(sd,data);
  8855. }
  8856. }
  8857. }
  8858. }
  8859. return ret;
  8860. }
  8861. /*==========================================
  8862. * Equip item on player sd at req_pos from inventory index n
  8863. * return: false - fail; true - success
  8864. *------------------------------------------*/
  8865. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  8866. {
  8867. int i, pos, flag = 0, iflag;
  8868. struct item_data *id;
  8869. uint8 res = ITEM_EQUIP_ACK_OK;
  8870. short* equip_index;
  8871. nullpo_retr(false,sd);
  8872. if( n < 0 || n >= MAX_INVENTORY ) {
  8873. if( equipswitch ){
  8874. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8875. }else{
  8876. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8877. }
  8878. return false;
  8879. }
  8880. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8881. if( equipswitch ){
  8882. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8883. }else{
  8884. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8885. }
  8886. return false;
  8887. }
  8888. if (!(id = sd->inventory_data[n]))
  8889. return false;
  8890. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8891. if(battle_config.battle_log && !equipswitch)
  8892. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  8893. if((res = pc_isequip(sd,n))) {
  8894. if( equipswitch ){
  8895. clif_equipswitch_add( sd, n, req_pos, res );
  8896. }else{
  8897. clif_equipitemack(sd,n,0,res); // fail
  8898. }
  8899. return false;
  8900. }
  8901. if( equipswitch && id->type == IT_AMMO ){
  8902. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8903. return false;
  8904. }
  8905. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  8906. if( equipswitch ){
  8907. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8908. }else{
  8909. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8910. }
  8911. return false;
  8912. }
  8913. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8914. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8915. if( equipswitch ){
  8916. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8917. }else{
  8918. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8919. }
  8920. return false;
  8921. }
  8922. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  8923. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  8924. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8925. clif_notify_bindOnEquip(sd,n);
  8926. }
  8927. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  8928. pos = req_pos&EQP_ACC;
  8929. if (pos == EQP_ACC) //User specified both slots.
  8930. pos = equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8931. for (i = 0; i < sd->inventory_data[n]->slot; i++) { // Accessories that have cards that force equip location
  8932. if (!sd->inventory.u.items_inventory[n].card[i])
  8933. continue;
  8934. struct item_data *card_data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]);
  8935. if (card_data) {
  8936. int card_pos = card_data->equip;
  8937. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  8938. pos = card_pos; // Use the card's equip position
  8939. break;
  8940. }
  8941. }
  8942. }
  8943. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8944. pos = (req_pos&EQP_ARMS);
  8945. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8946. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8947. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  8948. pos = req_pos&EQP_SHADOW_ACC;
  8949. if (pos == EQP_SHADOW_ACC)
  8950. pos = equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8951. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  8952. pos = (req_pos&EQP_SHADOW_ARMS);
  8953. if( pos == EQP_SHADOW_ARMS )
  8954. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  8955. }
  8956. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8957. //Update skill-block range database when weapon range changes. [Skotlex]
  8958. i = equip_index[EQI_HAND_R];
  8959. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8960. flag = 1;
  8961. else
  8962. flag = id->range != sd->inventory_data[i]->range;
  8963. }
  8964. if( equipswitch ){
  8965. for( i = 0; i < EQI_MAX; i++ ){
  8966. if( pos&equip_bitmask[i] ){
  8967. // If there was already an item assigned to this slot
  8968. if( sd->equip_switch_index[i] >= 0 ){
  8969. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  8970. }
  8971. // Assign the new index to it
  8972. sd->equip_switch_index[i] = n;
  8973. }
  8974. }
  8975. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  8976. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  8977. return true;
  8978. }else{
  8979. for(i=0;i<EQI_MAX;i++) {
  8980. if(pos & equip_bitmask[i]) {
  8981. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8982. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  8983. sd->equip_index[i] = n;
  8984. }
  8985. }
  8986. pc_equipswitch_remove(sd, n);
  8987. if(pos==EQP_AMMO) {
  8988. clif_arrowequip(sd,n);
  8989. clif_arrow_fail(sd,3);
  8990. }
  8991. else
  8992. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8993. sd->inventory.u.items_inventory[n].equip = pos;
  8994. }
  8995. if(pos & EQP_HAND_R) {
  8996. if(id)
  8997. sd->weapontype1 = id->look;
  8998. else
  8999. sd->weapontype1 = 0;
  9000. pc_calcweapontype(sd);
  9001. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9002. }
  9003. if(pos & EQP_HAND_L) {
  9004. if(id) {
  9005. if(id->type == IT_WEAPON) {
  9006. sd->status.shield = 0;
  9007. sd->weapontype2 = id->look;
  9008. }
  9009. else
  9010. if(id->type == IT_ARMOR) {
  9011. sd->status.shield = id->look;
  9012. sd->weapontype2 = 0;
  9013. }
  9014. }
  9015. else
  9016. sd->status.shield = sd->weapontype2 = 0;
  9017. pc_calcweapontype(sd);
  9018. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9019. }
  9020. if(pos & EQP_SHOES)
  9021. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9022. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  9023. short idx = sd->equip_index[EQI_AMMO];
  9024. if (idx >= 0) {
  9025. switch (sd->inventory_data[idx]->look) {
  9026. case AMMO_ARROW:
  9027. if (id->look != W_BOW && id->look != W_MUSICAL && id->look != W_WHIP)
  9028. pc_unequipitem(sd, idx, 2 | 4);
  9029. break;
  9030. case AMMO_BULLET:
  9031. case AMMO_SHELL:
  9032. if (id->look != W_REVOLVER && id->look != W_RIFLE && id->look != W_GATLING && id->look != W_SHOTGUN
  9033. #ifdef RENEWAL
  9034. && id->look != W_GRENADE
  9035. #endif
  9036. )
  9037. pc_unequipitem(sd, idx, 2 | 4);
  9038. break;
  9039. #ifndef RENEWAL
  9040. case AMMO_GRENADE:
  9041. if (id->look != W_GRENADE)
  9042. pc_unequipitem(sd, idx, 2 | 4);
  9043. break;
  9044. #endif
  9045. }
  9046. }
  9047. }
  9048. pc_set_costume_view(sd);
  9049. pc_checkallowskill(sd); //Check if status changes should be halted.
  9050. iflag = sd->npc_item_flag;
  9051. /* check for combos (MUST be before status_calc_pc) */
  9052. if( id->combos_count )
  9053. pc_checkcombo(sd,id);
  9054. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9055. ; //No cards
  9056. else {
  9057. for( i = 0; i < MAX_SLOTS; i++ ) {
  9058. struct item_data *data;
  9059. if (!sd->inventory.u.items_inventory[n].card[i])
  9060. continue;
  9061. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9062. if( data->combos_count )
  9063. pc_checkcombo(sd,data);
  9064. }
  9065. }
  9066. }
  9067. status_calc_pc(sd,SCO_NONE);
  9068. if (flag) //Update skill data
  9069. clif_skillinfoblock(sd);
  9070. //OnEquip script [Skotlex]
  9071. if (id) {
  9072. //only run the script if item isn't restricted
  9073. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  9074. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9075. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9076. ; //No cards
  9077. else {
  9078. for( i = 0; i < MAX_SLOTS; i++ ) {
  9079. struct item_data *data;
  9080. if (!sd->inventory.u.items_inventory[n].card[i])
  9081. continue;
  9082. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9083. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  9084. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9085. }
  9086. }
  9087. }
  9088. }
  9089. sd->npc_item_flag = iflag;
  9090. return true;
  9091. }
  9092. /**
  9093. * Recalculate player status on unequip
  9094. * @param sd: Player data
  9095. * @param n: Item inventory index
  9096. * @param flag: Whether to recalculate a player's status or not
  9097. * @return True on success or false on failure
  9098. */
  9099. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  9100. int i, iflag;
  9101. bool status_calc = false;
  9102. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  9103. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  9104. sd->inventory.u.items_inventory[n].equip = 0;
  9105. pc_checkallowskill(sd);
  9106. iflag = sd->npc_item_flag;
  9107. /* check for combos (MUST be before status_calc_pc) */
  9108. if (sd->inventory_data[n]) {
  9109. if (sd->inventory_data[n]->combos_count) {
  9110. if (pc_removecombo(sd, sd->inventory_data[n]))
  9111. status_calc = true;
  9112. }
  9113. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9114. ; //No cards
  9115. else {
  9116. for (i = 0; i < MAX_SLOTS; i++) {
  9117. struct item_data *data;
  9118. if (!sd->inventory.u.items_inventory[n].card[i])
  9119. continue;
  9120. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9121. if (data->combos_count) {
  9122. if (pc_removecombo(sd, data))
  9123. status_calc = true;
  9124. }
  9125. }
  9126. }
  9127. }
  9128. }
  9129. if (flag & 1 || status_calc) {
  9130. pc_checkallowskill(sd);
  9131. status_calc_pc(sd, SCO_NONE);
  9132. }
  9133. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  9134. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  9135. //OnUnEquip script [Skotlex]
  9136. if (sd->inventory_data[n]) {
  9137. if (sd->inventory_data[n]->unequip_script)
  9138. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9139. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9140. ; //No cards
  9141. else {
  9142. for (i = 0; i < MAX_SLOTS; i++) {
  9143. struct item_data *data;
  9144. if (!sd->inventory.u.items_inventory[n].card[i])
  9145. continue;
  9146. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9147. if (data->unequip_script)
  9148. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9149. }
  9150. }
  9151. }
  9152. }
  9153. sd->npc_item_flag = iflag;
  9154. }
  9155. /**
  9156. * Called when attempting to unequip an item from a player
  9157. * @param sd: Player data
  9158. * @param n: Item inventory index
  9159. * @param flag: Type of unequip
  9160. * 0 - only unequip
  9161. * 1 - calculate status after unequipping
  9162. * 2 - force unequip
  9163. * 4 - unequip by switching equipment
  9164. * @return True on success or false on failure
  9165. */
  9166. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  9167. int i, pos;
  9168. nullpo_retr(false,sd);
  9169. if (n < 0 || n >= MAX_INVENTORY) {
  9170. clif_unequipitemack(sd,0,0,0);
  9171. return false;
  9172. }
  9173. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  9174. clif_unequipitemack(sd,n,0,0);
  9175. return false; //Nothing to unequip
  9176. }
  9177. // status change that makes player cannot unequip equipment
  9178. if (!(flag&2) && sd->sc.count &&
  9179. (sd->sc.data[SC_BERSERK] ||
  9180. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9181. sd->sc.data[SC__BLOODYLUST] ||
  9182. sd->sc.data[SC_KYOUGAKU] ||
  9183. (sd->sc.data[SC_PYROCLASTIC] &&
  9184. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  9185. {
  9186. clif_unequipitemack(sd,n,0,0);
  9187. return false;
  9188. }
  9189. if (battle_config.battle_log)
  9190. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  9191. for(i = 0; i < EQI_MAX; i++) {
  9192. if (pos & equip_bitmask[i])
  9193. sd->equip_index[i] = -1;
  9194. }
  9195. if(pos & EQP_HAND_R) {
  9196. sd->weapontype1 = 0;
  9197. sd->status.weapon = sd->weapontype2;
  9198. pc_calcweapontype(sd);
  9199. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9200. if( !battle_config.dancing_weaponswitch_fix )
  9201. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  9202. #ifdef RENEWAL
  9203. if (battle_config.switch_remove_edp&2) {
  9204. #else
  9205. if (battle_config.switch_remove_edp&1) {
  9206. #endif
  9207. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  9208. }
  9209. }
  9210. if(pos & EQP_HAND_L) {
  9211. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  9212. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  9213. sd->status.shield = sd->weapontype2 = 0;
  9214. pc_calcweapontype(sd);
  9215. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9216. }
  9217. if(pos & EQP_SHOES)
  9218. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9219. clif_unequipitemack(sd,n,pos,1);
  9220. pc_set_costume_view(sd);
  9221. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  9222. // On weapon change (right and left hand)
  9223. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  9224. if (battle_config.ammo_unequip && !(flag & 4)) {
  9225. switch (sd->inventory_data[n]->look) {
  9226. case W_BOW:
  9227. case W_MUSICAL:
  9228. case W_WHIP:
  9229. case W_REVOLVER:
  9230. case W_RIFLE:
  9231. case W_GATLING:
  9232. case W_SHOTGUN:
  9233. case W_GRENADE: {
  9234. short idx = sd->equip_index[EQI_AMMO];
  9235. if (idx >= 0) {
  9236. sd->equip_index[EQI_AMMO] = -1;
  9237. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  9238. pc_unequipitem_sub(sd, idx, 0);
  9239. }
  9240. }
  9241. break;
  9242. }
  9243. }
  9244. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  9245. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  9246. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  9247. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  9248. }
  9249. // On armor change
  9250. if (pos & EQP_ARMOR) {
  9251. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  9252. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  9253. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  9254. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  9255. }
  9256. // On equipment change
  9257. #ifndef RENEWAL
  9258. if (!(flag&4))
  9259. status_change_end(&sd->bl, SC_CONCENTRATION, INVALID_TIMER);
  9260. #endif
  9261. // On ammo change
  9262. 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_))
  9263. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  9264. pc_unequipitem_sub(sd, n, flag);
  9265. return true;
  9266. }
  9267. int pc_equipswitch( struct map_session_data* sd, int index ){
  9268. // Get the target equip mask
  9269. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  9270. // Get the currently equipped item
  9271. short equippedItem = pc_checkequip( sd, position, true );
  9272. // No item equipped at the target
  9273. if( equippedItem == -1 ){
  9274. // Remove it from the equip switch
  9275. pc_equipswitch_remove( sd, index );
  9276. pc_equipitem( sd, index, position );
  9277. return position;
  9278. }else{
  9279. std::map<int, int> unequipped;
  9280. int unequipped_position = 0;
  9281. // Unequip all items that interfere
  9282. for( int i = 0; i < EQI_MAX; i++ ){
  9283. int unequip_index = sd->equip_index[i];
  9284. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  9285. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  9286. // Store the unequipped index and position mask for later
  9287. unequipped[unequip_index] = unequip_item->equip;
  9288. // Keep the position for later
  9289. unequipped_position |= unequip_item->equip;
  9290. // Unequip the item
  9291. pc_unequipitem( sd, unequip_index, 0 );
  9292. }
  9293. }
  9294. int all_position = position | unequipped_position;
  9295. // Equip everything that is hit by the mask
  9296. for( int i = 0; i < EQI_MAX; i++ ){
  9297. int exchange_index = sd->equip_switch_index[i];
  9298. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  9299. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  9300. // Store the target position
  9301. int exchange_position = exchange_item->equipSwitch;
  9302. // Remove the item from equip switch
  9303. pc_equipswitch_remove( sd, exchange_index );
  9304. // Equip the item at the destinated position
  9305. pc_equipitem( sd, exchange_index, exchange_position );
  9306. }
  9307. }
  9308. // Place all unequipped items into the equip switch window
  9309. for( std::pair<int, int> pair : unequipped ){
  9310. int unequipped_index = pair.first;
  9311. int unequipped_position = pair.second;
  9312. // Rebuild the index cache
  9313. for( int i = 0; i < EQI_MAX; i++ ){
  9314. if( unequipped_position & equip_bitmask[i] ){
  9315. sd->equip_switch_index[i] = unequipped_index;
  9316. }
  9317. }
  9318. // Set the correct position mask
  9319. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  9320. // Notify the client
  9321. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  9322. }
  9323. return all_position;
  9324. }
  9325. }
  9326. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  9327. struct item* item = &sd->inventory.u.items_inventory[index];
  9328. if( !item->equipSwitch ){
  9329. return;
  9330. }
  9331. for( int i = 0; i < EQI_MAX; i++ ){
  9332. // If a match is found
  9333. if( sd->equip_switch_index[i] == index ){
  9334. // Remove it from the slot
  9335. sd->equip_switch_index[i] = -1;
  9336. }
  9337. }
  9338. // Send out one packet for all slots using the current item's mask
  9339. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  9340. item->equipSwitch = 0;
  9341. }
  9342. /*==========================================
  9343. * Checking if player (sd) has an invalid item
  9344. * and is unequiped on map load (item_noequip)
  9345. *------------------------------------------*/
  9346. void pc_checkitem(struct map_session_data *sd) {
  9347. int i, calc_flag = 0;
  9348. struct item* it;
  9349. nullpo_retv(sd);
  9350. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9351. return;
  9352. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9353. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9354. it = &sd->inventory.u.items_inventory[i];
  9355. if( it->nameid == 0 )
  9356. continue;
  9357. if( !it->equip )
  9358. continue;
  9359. if( it->equip&~pc_equippoint(sd,i) ) {
  9360. pc_unequipitem(sd, i, 2);
  9361. calc_flag = 1;
  9362. continue;
  9363. }
  9364. 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) ) {
  9365. pc_unequipitem(sd, i, 2);
  9366. calc_flag = 1;
  9367. continue;
  9368. }
  9369. }
  9370. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9371. it = &sd->inventory.u.items_inventory[i];
  9372. if( it->nameid == 0 )
  9373. continue;
  9374. if( !it->equipSwitch )
  9375. continue;
  9376. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  9377. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  9378. for( int j = 0; j < EQI_MAX; j++ ){
  9379. if( sd->equip_switch_index[j] == i ){
  9380. sd->equip_switch_index[j] = -1;
  9381. }
  9382. }
  9383. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  9384. continue;
  9385. }
  9386. }
  9387. if( calc_flag && sd->state.active ) {
  9388. pc_checkallowskill(sd);
  9389. status_calc_pc(sd,SCO_NONE);
  9390. }
  9391. }
  9392. /*==========================================
  9393. * Checks for unavailable items and removes them.
  9394. * @param sd: Player data
  9395. * @param type Forced check:
  9396. * 1 - Inventory
  9397. * 2 - Cart
  9398. * 4 - Storage
  9399. *------------------------------------------*/
  9400. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9401. {
  9402. int i;
  9403. unsigned short nameid;
  9404. char output[256];
  9405. nullpo_retv(sd);
  9406. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9407. for(i = 0; i < MAX_INVENTORY; i++) {
  9408. nameid = sd->inventory.u.items_inventory[i].nameid;
  9409. if (!nameid)
  9410. continue;
  9411. if (!itemdb_available(nameid)) {
  9412. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  9413. clif_displaymessage(sd->fd, output);
  9414. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from inventory (char_id: %d).\n", nameid, sd->inventory.u.items_inventory[i].amount, sd->status.char_id);
  9415. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9416. continue;
  9417. }
  9418. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9419. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9420. }
  9421. }
  9422. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9423. for(i = 0; i < MAX_CART; i++) {
  9424. nameid = sd->cart.u.items_cart[i].nameid;
  9425. if (!nameid)
  9426. continue;
  9427. if (!itemdb_available(nameid)) {
  9428. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  9429. clif_displaymessage(sd->fd, output);
  9430. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from cart (char_id: %d).\n", nameid, sd->cart.u.items_cart[i].amount, sd->status.char_id);
  9431. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9432. continue;
  9433. }
  9434. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9435. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9436. }
  9437. }
  9438. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9439. for(i = 0; i < sd->storage.max_amount; i++) {
  9440. nameid = sd->storage.u.items_storage[i].nameid;
  9441. if (!nameid)
  9442. continue;
  9443. if (!itemdb_available(nameid)) {
  9444. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  9445. clif_displaymessage(sd->fd, output);
  9446. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from storage (char_id: %d).\n", nameid, sd->storage.u.items_storage[i].amount, sd->status.char_id);
  9447. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9448. continue;
  9449. }
  9450. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9451. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9452. }
  9453. }
  9454. }
  9455. /*==========================================
  9456. * Update PVP rank for sd1 in cmp to sd2
  9457. *------------------------------------------*/
  9458. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9459. {
  9460. struct map_session_data *sd1,*sd2;
  9461. sd1=(struct map_session_data *)bl;
  9462. sd2=va_arg(ap,struct map_session_data *);
  9463. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9464. {// cannot register pvp rank for hidden GMs
  9465. return 0;
  9466. }
  9467. if( sd1->pvp_point > sd2->pvp_point )
  9468. sd2->pvp_rank++;
  9469. return 0;
  9470. }
  9471. /*==========================================
  9472. * Calculate new rank beetween all present players (map_foreachinallarea)
  9473. * and display result
  9474. *------------------------------------------*/
  9475. int pc_calc_pvprank(struct map_session_data *sd)
  9476. {
  9477. int old = sd->pvp_rank;
  9478. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9479. sd->pvp_rank=1;
  9480. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9481. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9482. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9483. return sd->pvp_rank;
  9484. }
  9485. /*==========================================
  9486. * Calculate next sd ranking calculation from config
  9487. *------------------------------------------*/
  9488. TIMER_FUNC(pc_calc_pvprank_timer){
  9489. struct map_session_data *sd;
  9490. sd=map_id2sd(id);
  9491. if(sd==NULL)
  9492. return 0;
  9493. sd->pvp_timer = INVALID_TIMER;
  9494. if( pc_isinvisible(sd) )
  9495. {// do not calculate the pvp rank for a hidden GM
  9496. return 0;
  9497. }
  9498. if( pc_calc_pvprank(sd) > 0 )
  9499. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9500. return 0;
  9501. }
  9502. /*==========================================
  9503. * Checking if sd is married
  9504. * Return:
  9505. * partner_id = yes
  9506. * 0 = no
  9507. *------------------------------------------*/
  9508. int pc_ismarried(struct map_session_data *sd)
  9509. {
  9510. if(sd == NULL)
  9511. return -1;
  9512. if(sd->status.partner_id > 0)
  9513. return sd->status.partner_id;
  9514. else
  9515. return 0;
  9516. }
  9517. /*==========================================
  9518. * Marry player sd to player dstsd
  9519. * Return:
  9520. * false = fail
  9521. * true = success
  9522. *------------------------------------------*/
  9523. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9524. {
  9525. if(sd == NULL || dstsd == NULL ||
  9526. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9527. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9528. return false;
  9529. sd->status.partner_id = dstsd->status.char_id;
  9530. dstsd->status.partner_id = sd->status.char_id;
  9531. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9532. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9533. return true;
  9534. }
  9535. /*==========================================
  9536. * Divorce sd from its partner
  9537. * Return:
  9538. * false = fail
  9539. * true = success
  9540. *------------------------------------------*/
  9541. bool pc_divorce(struct map_session_data *sd)
  9542. {
  9543. struct map_session_data *p_sd;
  9544. int i;
  9545. if( sd == NULL || !pc_ismarried(sd) )
  9546. return false;
  9547. if( !sd->status.partner_id )
  9548. return false; // Char is not married
  9549. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9550. { // Lets char server do the divorce
  9551. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9552. return false; // No char server connected
  9553. return true;
  9554. }
  9555. // Both players online, lets do the divorce manually
  9556. sd->status.partner_id = 0;
  9557. p_sd->status.partner_id = 0;
  9558. for( i = 0; i < MAX_INVENTORY; i++ )
  9559. {
  9560. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9561. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9562. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9563. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9564. }
  9565. clif_divorced(sd, p_sd->status.name);
  9566. clif_divorced(p_sd, sd->status.name);
  9567. return true;
  9568. }
  9569. /**
  9570. * Get the partner map_session_data of a player
  9571. * @param sd : the husband|wife session
  9572. * @return partner session or NULL
  9573. */
  9574. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9575. if (!sd || !pc_ismarried(sd))
  9576. return NULL;
  9577. return map_charid2sd(sd->status.partner_id);
  9578. }
  9579. /**
  9580. * Get the father map_session_data of a player
  9581. * @param sd : the baby session
  9582. * @return father session or NULL
  9583. */
  9584. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9585. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9586. return NULL;
  9587. return map_charid2sd(sd->status.father);
  9588. }
  9589. /**
  9590. * Get the mother map_session_data of a player
  9591. * @param sd : the baby session
  9592. * @return mother session or NULL
  9593. */
  9594. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9595. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9596. return NULL;
  9597. return map_charid2sd(sd->status.mother);
  9598. }
  9599. /*==========================================
  9600. * Get sd children charid. (Need to be married)
  9601. *------------------------------------------*/
  9602. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9603. {
  9604. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9605. // charid2sd returns NULL if not found
  9606. return NULL;
  9607. return map_charid2sd(sd->status.child);
  9608. }
  9609. /*==========================================
  9610. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9611. *------------------------------------------*/
  9612. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  9613. {
  9614. int hp = 0, sp = 0;
  9615. if( pc_isdead(sd) )
  9616. return;
  9617. if (sd->hp_loss.value) {
  9618. sd->hp_loss.tick += diff_tick;
  9619. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9620. hp += sd->hp_loss.value;
  9621. sd->hp_loss.tick -= sd->hp_loss.rate;
  9622. }
  9623. if(hp >= sd->battle_status.hp)
  9624. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9625. }
  9626. if (sd->sp_loss.value) {
  9627. sd->sp_loss.tick += diff_tick;
  9628. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9629. sp += sd->sp_loss.value;
  9630. sd->sp_loss.tick -= sd->sp_loss.rate;
  9631. }
  9632. }
  9633. if (hp > 0 || sp > 0)
  9634. status_zap(&sd->bl, hp, sp);
  9635. }
  9636. //Character regen. Flag is used to know which types of regen can take place.
  9637. //&1: HP regen
  9638. //&2: SP regen
  9639. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  9640. {
  9641. int hp = 0, sp = 0;
  9642. if (sd->hp_regen.value) {
  9643. sd->hp_regen.tick += diff_tick;
  9644. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9645. hp += sd->hp_regen.value;
  9646. sd->hp_regen.tick -= sd->hp_regen.rate;
  9647. }
  9648. }
  9649. if (sd->sp_regen.value) {
  9650. sd->sp_regen.tick += diff_tick;
  9651. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9652. sp += sd->sp_regen.value;
  9653. sd->sp_regen.tick -= sd->sp_regen.rate;
  9654. }
  9655. }
  9656. if (sd->percent_hp_regen.value) {
  9657. sd->percent_hp_regen.tick += diff_tick;
  9658. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9659. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  9660. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9661. }
  9662. }
  9663. if (sd->percent_sp_regen.value) {
  9664. sd->percent_sp_regen.tick += diff_tick;
  9665. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9666. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  9667. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9668. }
  9669. }
  9670. if (hp > 0 || sp > 0)
  9671. status_heal(&sd->bl, hp, sp, 0);
  9672. }
  9673. /*==========================================
  9674. * Memo player sd savepoint. (map,x,y)
  9675. *------------------------------------------*/
  9676. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9677. {
  9678. nullpo_retv(sd);
  9679. sd->status.save_point.map = mapindex;
  9680. sd->status.save_point.x = x;
  9681. sd->status.save_point.y = y;
  9682. }
  9683. /*==========================================
  9684. * Save 1 player data at autosave interval
  9685. *------------------------------------------*/
  9686. static TIMER_FUNC(pc_autosave){
  9687. int interval;
  9688. struct s_mapiterator* iter;
  9689. struct map_session_data* sd;
  9690. static int last_save_id = 0, save_flag = 0;
  9691. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9692. save_flag = 0;
  9693. else
  9694. save_flag = 1; //Noone was saved, so save first found char.
  9695. iter = mapit_getallusers();
  9696. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9697. {
  9698. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  9699. continue;
  9700. if(sd->bl.id == last_save_id && save_flag != 1) {
  9701. save_flag = 1;
  9702. continue;
  9703. }
  9704. if(save_flag != 1) //Not our turn to save yet.
  9705. continue;
  9706. //Save char.
  9707. last_save_id = sd->bl.id;
  9708. save_flag = 2;
  9709. if (pc_isvip(sd)) // Check if we're still VIP
  9710. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9711. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9712. break;
  9713. }
  9714. mapit_free(iter);
  9715. interval = autosave_interval/(map_usercount()+1);
  9716. if(interval < minsave_interval)
  9717. interval = minsave_interval;
  9718. add_timer(gettick()+interval,pc_autosave,0,0);
  9719. return 0;
  9720. }
  9721. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9722. {
  9723. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9724. { //Night/day state does not match.
  9725. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9726. sd->state.night = night_flag;
  9727. return 1;
  9728. }
  9729. return 0;
  9730. }
  9731. /*================================================
  9732. * timer to do the day [Yor]
  9733. * data: 0 = called by timer, 1 = gmcommand/script
  9734. *------------------------------------------------*/
  9735. TIMER_FUNC(map_day_timer){
  9736. char tmp_soutput[1024];
  9737. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9738. return 0;
  9739. if (!night_flag)
  9740. return 0; //Already day.
  9741. night_flag = 0; // 0=day, 1=night [Yor]
  9742. map_foreachpc(pc_daynight_timer_sub);
  9743. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9744. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9745. return 0;
  9746. }
  9747. /*================================================
  9748. * timer to do the night [Yor]
  9749. * data: 0 = called by timer, 1 = gmcommand/script
  9750. *------------------------------------------------*/
  9751. TIMER_FUNC(map_night_timer){
  9752. char tmp_soutput[1024];
  9753. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9754. return 0;
  9755. if (night_flag)
  9756. return 0; //Already nigth.
  9757. night_flag = 1; // 0=day, 1=night [Yor]
  9758. map_foreachpc(pc_daynight_timer_sub);
  9759. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9760. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9761. return 0;
  9762. }
  9763. /**
  9764. * Attempt to stand up a player
  9765. * @param sd
  9766. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9767. * @return True if success, Fals if failed
  9768. */
  9769. bool pc_setstand(struct map_session_data *sd, bool force){
  9770. nullpo_ret(sd);
  9771. // Cannot stand yet
  9772. // TODO: Move to SCS_NOSTAND [Cydh]
  9773. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9774. return false;
  9775. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9776. clif_status_load(&sd->bl,EFST_SIT,0);
  9777. clif_standing(&sd->bl); //Inform area PC is standing
  9778. //Reset sitting tick.
  9779. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9780. if( pc_isdead( sd ) ){
  9781. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9782. clif_party_dead( sd );
  9783. }else{
  9784. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9785. }
  9786. return true;
  9787. }
  9788. /**
  9789. * Calculate Overheat value
  9790. * @param sd: Player data
  9791. * @param heat: Amount of Heat to adjust
  9792. **/
  9793. void pc_overheat(struct map_session_data *sd, int16 heat) {
  9794. struct status_change_entry *sce = NULL;
  9795. int16 limit[] = { 150, 200, 280, 360, 450 };
  9796. uint16 skill_lv;
  9797. nullpo_retv(sd);
  9798. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  9799. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  9800. sce->val1 += heat;
  9801. sce->val1 = cap_value(sce->val1, 0, 1000);
  9802. if (sd->sc.data[SC_OVERHEAT])
  9803. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  9804. if (sce->val1 > limit[skill_lv])
  9805. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  9806. } else if (heat > 0)
  9807. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  9808. }
  9809. /**
  9810. * Check if player is autolooting given itemID.
  9811. */
  9812. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9813. {
  9814. uint8 i = 0;
  9815. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9816. return true;
  9817. if (!sd->state.autolooting)
  9818. return false;
  9819. if (sd->state.autolooting)
  9820. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9821. return (i != AUTOLOOTITEM_SIZE);
  9822. }
  9823. /**
  9824. * Checks if player can use @/#command
  9825. * @param sd Player map session data
  9826. * @param command Command name without @/# and params
  9827. * @param type is it atcommand or charcommand
  9828. */
  9829. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9830. {
  9831. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9832. }
  9833. /**
  9834. * Checks if commands used by a player should be logged
  9835. * according to their group setting.
  9836. * @param sd Player map session data
  9837. */
  9838. bool pc_should_log_commands(struct map_session_data *sd)
  9839. {
  9840. return pc_group_should_log_commands(pc_get_group_id(sd));
  9841. }
  9842. /**
  9843. * Spirit Charm expiration timer.
  9844. * @see TimerFunc
  9845. */
  9846. static TIMER_FUNC(pc_spiritcharm_timer){
  9847. struct map_session_data *sd;
  9848. int i;
  9849. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9850. return 1;
  9851. if (sd->spiritcharm <= 0) {
  9852. 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);
  9853. sd->spiritcharm = 0;
  9854. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9855. return 0;
  9856. }
  9857. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9858. if (i == sd->spiritcharm) {
  9859. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9860. return 0;
  9861. }
  9862. sd->spiritcharm--;
  9863. if (i != sd->spiritcharm)
  9864. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9865. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9866. if (sd->spiritcharm <= 0)
  9867. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9868. clif_spiritcharm(sd);
  9869. return 0;
  9870. }
  9871. /**
  9872. * Adds a spirit charm.
  9873. * @param sd: Target character
  9874. * @param interval: Duration
  9875. * @param max: Maximum amount of charms to add
  9876. * @param type: Charm type (@see spirit_charm_types)
  9877. */
  9878. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9879. {
  9880. int tid, i;
  9881. nullpo_retv(sd);
  9882. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9883. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9884. if (max > MAX_SPIRITCHARM)
  9885. max = MAX_SPIRITCHARM;
  9886. if (sd->spiritcharm < 0)
  9887. sd->spiritcharm = 0;
  9888. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9889. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9890. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9891. sd->spiritcharm--;
  9892. if (sd->spiritcharm != 0)
  9893. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9894. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9895. }
  9896. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9897. 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);
  9898. if (i != sd->spiritcharm)
  9899. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9900. sd->spiritcharm_timer[i] = tid;
  9901. sd->spiritcharm++;
  9902. sd->spiritcharm_type = type;
  9903. clif_spiritcharm(sd);
  9904. }
  9905. /**
  9906. * Removes one or more spirit charms.
  9907. * @param sd: The target character
  9908. * @param count: Amount of charms to remove
  9909. * @param type: Type of charm to remove
  9910. */
  9911. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9912. {
  9913. int i;
  9914. nullpo_retv(sd);
  9915. if (sd->spiritcharm_type != type)
  9916. return;
  9917. if (sd->spiritcharm <= 0) {
  9918. sd->spiritcharm = 0;
  9919. return;
  9920. }
  9921. if (count <= 0)
  9922. return;
  9923. if (count > sd->spiritcharm)
  9924. count = sd->spiritcharm;
  9925. sd->spiritcharm -= count;
  9926. if (count > MAX_SPIRITCHARM)
  9927. count = MAX_SPIRITCHARM;
  9928. for (i = 0; i < count; i++) {
  9929. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9930. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9931. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9932. }
  9933. }
  9934. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9935. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9936. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9937. }
  9938. if (sd->spiritcharm <= 0)
  9939. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9940. clif_spiritcharm(sd);
  9941. }
  9942. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9943. /**
  9944. * Renewal EXP/Item Drop rate modifier based on level penalty
  9945. * @param level_diff: Monster and Player level difference
  9946. * @param mob_class: Monster class
  9947. * @param mode: Monster mode
  9948. * @param type: 1 - EXP, 2 - Item Drop
  9949. * @return Penalty rate
  9950. */
  9951. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9952. {
  9953. int rate = 100;
  9954. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9955. return rate;
  9956. if (level_diff < 0)
  9957. level_diff = MAX_LEVEL + (~level_diff + 1);
  9958. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9959. return rate;
  9960. return 100; // Penalty not found, return default
  9961. }
  9962. #endif
  9963. int pc_split_str(char *str,char **val,int num)
  9964. {
  9965. int i;
  9966. for (i=0; i<num && str; i++){
  9967. val[i] = str;
  9968. str = strchr(str,',');
  9969. if (str && i<num-1) //Do not remove a trailing comma.
  9970. *str++=0;
  9971. }
  9972. return i;
  9973. }
  9974. int pc_split_atoi(char* str, int* val, char sep, int max)
  9975. {
  9976. int i,j;
  9977. for (i=0; i<max; i++) {
  9978. if (!str) break;
  9979. val[i] = atoi(str);
  9980. str = strchr(str,sep);
  9981. if (str)
  9982. *str++=0;
  9983. }
  9984. //Zero up the remaining.
  9985. for(j=i; j < max; j++)
  9986. val[j] = 0;
  9987. return i;
  9988. }
  9989. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9990. {
  9991. static int warning=0;
  9992. int i,j;
  9993. for (i=0; i<max; i++) {
  9994. double f;
  9995. if (!str) break;
  9996. f = atof(str);
  9997. if (f < 0)
  9998. val[i] = 0;
  9999. else if (f > UINT_MAX) {
  10000. val[i] = UINT_MAX;
  10001. if (!warning) {
  10002. warning = 1;
  10003. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  10004. }
  10005. } else
  10006. val[i] = (unsigned int)f;
  10007. str = strchr(str,sep);
  10008. if (str)
  10009. *str++=0;
  10010. }
  10011. //Zero up the remaining.
  10012. for(j=i; j < max; j++)
  10013. val[j] = 0;
  10014. return i;
  10015. }
  10016. /*==========================================
  10017. * sub DB reading.
  10018. * Function used to read skill_tree.txt
  10019. *------------------------------------------*/
  10020. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  10021. {
  10022. uint32 baselv, joblv, baselv_max, joblv_max;
  10023. uint16 skill_id, skill_lv, skill_lv_max;
  10024. int idx, class_;
  10025. unsigned int i, offset, skill_idx;
  10026. class_ = atoi(fields[0]);
  10027. skill_id = (uint16)atoi(fields[1]);
  10028. skill_lv = (uint16)atoi(fields[2]);
  10029. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  10030. baselv = (uint32)atoi(fields[3]);
  10031. joblv = (uint32)atoi(fields[4]);
  10032. offset = 5;
  10033. }
  10034. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  10035. baselv = joblv = 0;
  10036. offset = 3;
  10037. }
  10038. else {
  10039. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  10040. return false;
  10041. }
  10042. if(!pcdb_checkid(class_))
  10043. {
  10044. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  10045. return false;
  10046. }
  10047. idx = pc_class2idx(class_);
  10048. if (!skill_get_index(skill_id)) {
  10049. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.\n", skill_id, class_);
  10050. return false;
  10051. }
  10052. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10053. 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);
  10054. skill_lv = skill_lv_max;
  10055. }
  10056. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  10057. 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);
  10058. baselv = baselv_max;
  10059. }
  10060. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  10061. 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);
  10062. joblv = joblv_max;
  10063. }
  10064. //This is to avoid adding two lines for the same skill. [Skotlex]
  10065. 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 );
  10066. if( skill_idx == MAX_SKILL_TREE )
  10067. {
  10068. 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_);
  10069. return false;
  10070. }
  10071. else if(skill_tree[idx][skill_idx].skill_id)
  10072. {
  10073. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  10074. }
  10075. skill_tree[idx][skill_idx].skill_id = skill_id;
  10076. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  10077. skill_tree[idx][skill_idx].baselv = baselv;
  10078. skill_tree[idx][skill_idx].joblv = joblv;
  10079. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  10080. {
  10081. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  10082. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  10083. if (skill_id == 0) {
  10084. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  10085. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  10086. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  10087. }
  10088. continue;
  10089. }
  10090. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  10091. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  10092. return false;
  10093. }
  10094. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10095. 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);
  10096. skill_lv = skill_lv_max;
  10097. }
  10098. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  10099. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  10100. }
  10101. return true;
  10102. }
  10103. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10104. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  10105. {
  10106. int type, class_, diff;
  10107. type = atoi(fields[0]); //1=experience, 2=item drop
  10108. class_ = atoi(fields[1]);
  10109. diff = atoi(fields[2]);
  10110. if( type != 1 && type != 2 ){
  10111. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  10112. return false;
  10113. }
  10114. if( !CHK_CLASS(class_) ){
  10115. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  10116. return false;
  10117. }
  10118. diff = min(diff, MAX_LEVEL);
  10119. if( diff < 0 )
  10120. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  10121. level_penalty[type][class_][diff] = atoi(fields[3]);
  10122. return true;
  10123. }
  10124. #endif
  10125. /** [Cydh]
  10126. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  10127. * @param level Base level of player
  10128. * @param class_ Job ID @see enum e_job
  10129. * @return base_hp
  10130. */
  10131. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  10132. double base_hp;
  10133. uint16 i, idx = pc_class2idx(class_);
  10134. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  10135. #ifndef RENEWAL
  10136. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  10137. #endif
  10138. for (i = 2; i <= level; i++)
  10139. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  10140. if (class_ == JOB_SUMMONER)
  10141. base_hp += floor((base_hp / 2) + 0.5);
  10142. return (unsigned int)base_hp;
  10143. }
  10144. /** [Playtester]
  10145. * Calculates base sp of player.
  10146. * @param level Base level of player
  10147. * @param class_ Job ID @see enum e_job
  10148. * @return base_sp
  10149. */
  10150. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  10151. double base_sp;
  10152. uint16 idx = pc_class2idx(class_);
  10153. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  10154. switch (class_) {
  10155. case JOB_NINJA:
  10156. if (level >= 10)
  10157. base_sp -= 22;
  10158. else
  10159. base_sp = 11 + 3*level;
  10160. break;
  10161. case JOB_GUNSLINGER:
  10162. if (level > 10)
  10163. base_sp -= 18;
  10164. else
  10165. base_sp = 9 + 3*level;
  10166. break;
  10167. case JOB_SUMMONER:
  10168. base_sp -= floor(base_sp / 2);
  10169. break;
  10170. }
  10171. return (unsigned int)base_sp;
  10172. }
  10173. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  10174. static bool pc_readdb_job1(char* fields[], int columns, int current){
  10175. int idx, class_;
  10176. unsigned int i;
  10177. class_ = atoi(fields[0]);
  10178. if (!pcdb_checkid(class_)) {
  10179. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  10180. return false;
  10181. }
  10182. idx = pc_class2idx(class_);
  10183. job_info[idx].max_weight_base = atoi(fields[1]);
  10184. job_info[idx].hp_factor = atoi(fields[2]);
  10185. job_info[idx].hp_multiplicator = atoi(fields[3]);
  10186. job_info[idx].sp_factor = atoi(fields[4]);
  10187. #ifdef RENEWAL_ASPD
  10188. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  10189. #else
  10190. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  10191. #endif
  10192. {
  10193. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  10194. }
  10195. return true;
  10196. }
  10197. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  10198. static bool pc_readdb_job2(char* fields[], int columns, int current)
  10199. {
  10200. int idx, class_, i;
  10201. class_ = atoi(fields[0]);
  10202. if(!pcdb_checkid(class_))
  10203. {
  10204. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  10205. return false;
  10206. }
  10207. idx = pc_class2idx(class_);
  10208. for(i = 1; i < columns; i++)
  10209. {
  10210. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  10211. }
  10212. return true;
  10213. }
  10214. //Reading job_exp.txt line
  10215. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  10216. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  10217. {
  10218. int idx, i, type;
  10219. int job_id,job_count,jobs[CLASS_COUNT];
  10220. unsigned int ui, maxlvl;
  10221. maxlvl = atoi(fields[0]);
  10222. if(maxlvl > MAX_LEVEL || maxlvl<1){
  10223. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  10224. return false;
  10225. }
  10226. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10227. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  10228. return false;
  10229. }
  10230. type = atoi(fields[2]);
  10231. if(type < 0 || type > 1){
  10232. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  10233. return false;
  10234. }
  10235. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  10236. if (job_count < 1)
  10237. return false;
  10238. job_id = jobs[0];
  10239. if(!pcdb_checkid(job_id)){
  10240. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  10241. return false;
  10242. }
  10243. idx = pc_class2idx(job_id);
  10244. job_info[idx].max_level[type] = maxlvl;
  10245. for(i=0; i<maxlvl; i++)
  10246. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  10247. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  10248. //The reasoning behind the -2 is this... if the max level is 5, then the array
  10249. //should look like this:
  10250. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  10251. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  10252. job_info[idx].max_level[type]--;
  10253. if (job_info[idx].max_level[type] < maxlvl) {
  10254. 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]);
  10255. ShowInfo("Filling the missing values with the last exp entry.\n");
  10256. //Fill the requested values with the last entry.
  10257. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  10258. for (; ui+2 < maxlvl; ui++)
  10259. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  10260. job_info[idx].max_level[type] = maxlvl;
  10261. }
  10262. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10263. for (i = 1; i < job_count; i++) {
  10264. job_id = jobs[i];
  10265. if (!pcdb_checkid(job_id)) {
  10266. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  10267. continue;
  10268. }
  10269. idx = pc_class2idx(job_id);
  10270. 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]));
  10271. job_info[idx].max_level[type] = maxlvl;
  10272. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10273. }
  10274. return true;
  10275. }
  10276. /**
  10277. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  10278. * startlvl,endlvl,class,type,values...
  10279. */
  10280. #ifdef HP_SP_TABLES
  10281. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  10282. {
  10283. int i, startlvl, endlvl;
  10284. int job_count,jobs[CLASS_COUNT];
  10285. short type;
  10286. startlvl = atoi(fields[0]);
  10287. if(startlvl<1){
  10288. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10289. return false;
  10290. }
  10291. endlvl = atoi(fields[1]);
  10292. if(endlvl<1 || endlvl<startlvl){
  10293. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  10294. return false;
  10295. }
  10296. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10297. 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);
  10298. return false;
  10299. }
  10300. type = atoi(fields[3]);
  10301. if(type < 0 || type > 1){
  10302. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  10303. return false;
  10304. }
  10305. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  10306. if (job_count < 1)
  10307. return false;
  10308. for (i = 0; i < job_count; i++) {
  10309. int idx, job_id = jobs[i], use_endlvl;
  10310. if (!pcdb_checkid(job_id)) {
  10311. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  10312. return false;
  10313. }
  10314. idx = pc_class2idx(job_id);
  10315. if (startlvl > job_info[idx].max_level[0]) {
  10316. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10317. return false;
  10318. }
  10319. //Just read until available max level for this job, don't use MAX_LEVEL!
  10320. use_endlvl = endlvl;
  10321. if (use_endlvl > job_info[idx].max_level[0])
  10322. use_endlvl = job_info[idx].max_level[0];
  10323. if(type == 0) { //hp type
  10324. uint16 j;
  10325. for(j = 0; j < use_endlvl; j++) {
  10326. if (atoi(fields[j+4])) {
  10327. uint16 lvl_idx = startlvl-1+j;
  10328. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  10329. //Tells if this HP is lower than previous level (but not for 99->100)
  10330. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  10331. 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",
  10332. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  10333. }
  10334. }
  10335. }
  10336. else { //sp type
  10337. uint16 j;
  10338. for(j = 0; j < use_endlvl; j++) {
  10339. if (atoi(fields[j+4])) {
  10340. uint16 lvl_idx = startlvl-1+j;
  10341. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  10342. //Tells if this SP is lower than previous level (but not for 99->100)
  10343. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  10344. 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",
  10345. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  10346. }
  10347. }
  10348. }
  10349. }
  10350. return true;
  10351. }
  10352. #endif
  10353. /** [Cydh]
  10354. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  10355. */
  10356. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  10357. {
  10358. int64 class_tmp;
  10359. int idx, class_;
  10360. uint16 str, agi, vit, int_, dex, luk;
  10361. script_get_constant(trim(fields[0]),&class_tmp);
  10362. class_ = static_cast<int>(class_tmp);
  10363. if ((idx = pc_class2idx(class_)) < 0) {
  10364. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  10365. return false;
  10366. }
  10367. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  10368. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  10369. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  10370. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  10371. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10372. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10373. job_info[idx].max_param.str = str;
  10374. job_info[idx].max_param.agi = agi;
  10375. job_info[idx].max_param.vit = vit;
  10376. job_info[idx].max_param.int_ = int_;
  10377. job_info[idx].max_param.dex = dex;
  10378. job_info[idx].max_param.luk = luk;
  10379. return true;
  10380. }
  10381. /**
  10382. * Read job_noenter_map.txt
  10383. **/
  10384. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10385. int idx, class_ = -1;
  10386. int64 class_tmp;
  10387. if (ISDIGIT(str[0][0])) {
  10388. class_ = atoi(str[0]);
  10389. } else {
  10390. if (!script_get_constant(str[0], &class_tmp)) {
  10391. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10392. return false;
  10393. }
  10394. class_ = static_cast<int>(class_tmp);
  10395. }
  10396. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10397. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  10398. return false;
  10399. }
  10400. job_info[idx].noenter_map.zone = atoi(str[1]);
  10401. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10402. return true;
  10403. }
  10404. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10405. int i=1;
  10406. char line[24000]; //FIXME this seem too big
  10407. FILE *fp;
  10408. sprintf(line, "%s/statpoint.txt", basedir);
  10409. fp=fopen(line,"r");
  10410. if(fp == NULL){
  10411. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10412. return max(last_s,i);
  10413. } else {
  10414. int entries=0;
  10415. while(fgets(line, sizeof(line), fp))
  10416. {
  10417. int stat;
  10418. trim(line);
  10419. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10420. continue;
  10421. if ((stat=strtoul(line,NULL,10))<0)
  10422. stat=0;
  10423. if (i > MAX_LEVEL)
  10424. break;
  10425. statp[i]=stat;
  10426. i++;
  10427. entries++;
  10428. }
  10429. fclose(fp);
  10430. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10431. }
  10432. return max(last_s,i);
  10433. }
  10434. /*==========================================
  10435. * pc DB reading.
  10436. * job_exp.txt - required experience values
  10437. * skill_tree.txt - skill tree for every class
  10438. * attr_fix.txt - elemental adjustment table
  10439. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10440. * job_db2.txt - job,stats bonuses/lvl
  10441. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10442. *------------------------------------------*/
  10443. void pc_readdb(void) {
  10444. int i, k, s = 1;
  10445. const char* dbsubpath[] = {
  10446. "",
  10447. "/" DBIMPORT,
  10448. //add other path here
  10449. };
  10450. //reset
  10451. memset(job_info,0,sizeof(job_info)); // job_info table
  10452. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10453. sv_readdb(db_path, DBPATH "level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10454. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10455. for( k=1; k < 3; k++ ){ // fill in the blanks
  10456. int j;
  10457. for( j = 0; j < CLASS_ALL; j++ ){
  10458. int tmp = 0;
  10459. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10460. if( i == MAX_LEVEL+1 )
  10461. tmp = level_penalty[k][j][0];// reset
  10462. if( level_penalty[k][j][i] > 0 )
  10463. tmp = level_penalty[k][j][i];
  10464. else
  10465. level_penalty[k][j][i] = tmp;
  10466. }
  10467. }
  10468. }
  10469. #endif
  10470. // reset then read statspoint
  10471. memset(statp,0,sizeof(statp));
  10472. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10473. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10474. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10475. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10476. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10477. if(i==0) {
  10478. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10479. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10480. }
  10481. else {
  10482. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10483. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10484. }
  10485. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10486. if (i == 0)
  10487. #ifdef RENEWAL_ASPD // Paths are hardcoded here to specifically pick the correct database
  10488. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10489. #else
  10490. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10491. #endif
  10492. else
  10493. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, true);
  10494. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10495. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10496. #ifdef HP_SP_TABLES
  10497. 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!
  10498. #endif
  10499. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10500. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10501. aFree(dbsubpath1);
  10502. aFree(dbsubpath2);
  10503. }
  10504. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10505. memset(skill_tree, 0, sizeof(skill_tree));
  10506. 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);
  10507. 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);
  10508. // generate the remaining parts of the db if necessary
  10509. k = battle_config.use_statpoint_table; //save setting
  10510. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10511. statp[0] = 45; // seed value
  10512. for (; s <= MAX_LEVEL; s++)
  10513. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10514. battle_config.use_statpoint_table = k; //restore setting
  10515. //Checking if all class have their data
  10516. for (i = 0; i < JOB_MAX; i++) {
  10517. int idx;
  10518. uint16 j;
  10519. if (!pcdb_checkid(i))
  10520. continue;
  10521. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10522. continue; //Classes that do not need exp tables.
  10523. idx = pc_class2idx(i);
  10524. if (!job_info[idx].max_level[0])
  10525. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10526. if (!job_info[idx].max_level[1])
  10527. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10528. //Init and checking the empty value of Base HP/SP [Cydh]
  10529. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10530. if (job_info[idx].base_hp[j] == 0)
  10531. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10532. if (job_info[idx].base_sp[j] == 0)
  10533. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10534. }
  10535. }
  10536. }
  10537. // Read MOTD on startup. [Valaris]
  10538. int pc_read_motd(void)
  10539. {
  10540. FILE* fp;
  10541. // clear old MOTD
  10542. memset(motd_text, 0, sizeof(motd_text));
  10543. // read current MOTD
  10544. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10545. {
  10546. unsigned int entries = 0;
  10547. char buf[CHAT_SIZE_MAX];
  10548. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10549. {
  10550. unsigned int lines = 0;
  10551. size_t len;
  10552. lines++;
  10553. if( buf[0] == '/' && buf[1] == '/' )
  10554. continue;
  10555. len = strlen(buf);
  10556. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10557. len--;
  10558. if( len ) {
  10559. char * ptr;
  10560. buf[len] = 0;
  10561. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10562. 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);
  10563. }
  10564. else {// empty line
  10565. buf[0] = ' ';
  10566. buf[1] = 0;
  10567. }
  10568. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10569. entries++;
  10570. }
  10571. fclose(fp);
  10572. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10573. }
  10574. else
  10575. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10576. return 0;
  10577. }
  10578. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10579. int i,cursor = 0;
  10580. struct item_cd* cd = NULL;
  10581. if( load ) {
  10582. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10583. // no item cooldown is associated with this character
  10584. return;
  10585. }
  10586. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10587. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10588. sd->item_delay[cursor].tick = cd->tick[i];
  10589. sd->item_delay[cursor].nameid = cd->nameid[i];
  10590. cursor++;
  10591. }
  10592. }
  10593. idb_remove(itemcd_db,sd->status.char_id);
  10594. } else {
  10595. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10596. // create a new skill cooldown object for map storage
  10597. CREATE( cd, struct item_cd, 1 );
  10598. idb_put( itemcd_db, sd->status.char_id, cd );
  10599. }
  10600. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10601. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10602. cd->tick[cursor] = sd->item_delay[i].tick;
  10603. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10604. cursor++;
  10605. }
  10606. }
  10607. }
  10608. return;
  10609. }
  10610. /**
  10611. * Add item delay to player's item delay data
  10612. * @param sd Player
  10613. * @param id Item data
  10614. * @param tick Current tick
  10615. * @param n Item index in inventory
  10616. * @return 0: No delay, can consume item.
  10617. * 1: Has delay, cancel consumption.
  10618. **/
  10619. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10620. int i;
  10621. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10622. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10623. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10624. if( i < MAX_ITEMDELAYS ) {
  10625. if( sd->item_delay[i].nameid ) {// found
  10626. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10627. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10628. char e_msg[CHAT_SIZE_MAX];
  10629. if( e_tick > 99 )
  10630. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10631. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10632. else
  10633. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10634. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10635. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10636. return 1; // Delay has not expired yet
  10637. }
  10638. } else {// not yet used item (all slots are initially empty)
  10639. sd->item_delay[i].nameid = id->nameid;
  10640. }
  10641. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10642. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10643. } else {// should not happen
  10644. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10645. }
  10646. //clean up used delays so we can give room for more
  10647. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10648. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10649. sd->item_delay[i].tick = 0;
  10650. sd->item_delay[i].nameid = 0;
  10651. }
  10652. }
  10653. return 0;
  10654. }
  10655. /**
  10656. * Check if player has delay to reuse item
  10657. * @param sd Player
  10658. * @param id Item data
  10659. * @param tick Current tick
  10660. * @param n Item index in inventory
  10661. * @return 0: No delay, can consume item.
  10662. * 1: Has delay, cancel consumption.
  10663. **/
  10664. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10665. struct status_change *sc = NULL;
  10666. nullpo_retr(0, sd);
  10667. nullpo_retr(0, id);
  10668. // Do normal delay assignment
  10669. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10670. return pc_itemcd_add(sd, id, tick, n);
  10671. // Send reply of delay remains
  10672. if (sc->data[id->delay_sc]) {
  10673. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10674. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  10675. return 1;
  10676. }
  10677. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10678. return 0;
  10679. }
  10680. /**
  10681. * Clear the dmglog data from player
  10682. * @param sd
  10683. * @param md
  10684. **/
  10685. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10686. {
  10687. uint8 i;
  10688. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10689. if (i < DAMAGELOG_SIZE) {
  10690. md->dmglog[i].id = 0;
  10691. md->dmglog[i].dmg = 0;
  10692. md->dmglog[i].flag = 0;
  10693. }
  10694. }
  10695. /**
  10696. * Add log to player's dmglog
  10697. * @param sd
  10698. * @param id Monster's GID
  10699. **/
  10700. void pc_damage_log_add(struct map_session_data *sd, int id)
  10701. {
  10702. uint8 i = 0;
  10703. if (!sd || !id)
  10704. return;
  10705. //Only store new data, don't need to renew the old one with same id
  10706. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10707. if (i < DAMAGELOG_SIZE_PC)
  10708. return;
  10709. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10710. if (sd->dmglog[i] == 0) {
  10711. sd->dmglog[i] = id;
  10712. return;
  10713. }
  10714. }
  10715. }
  10716. /**
  10717. * Clear dmglog data from player
  10718. * @param sd
  10719. * @param id Monster's id
  10720. **/
  10721. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10722. {
  10723. uint8 i;
  10724. struct mob_data *md = NULL;
  10725. if (!sd)
  10726. return;
  10727. if (!id) {
  10728. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10729. if( !sd->dmglog[i] ) //skip the empty value
  10730. continue;
  10731. if ((md = map_id2md(sd->dmglog[i])))
  10732. pc_clear_log_damage_sub(sd->status.char_id,md);
  10733. sd->dmglog[i] = 0;
  10734. }
  10735. }
  10736. else {
  10737. if ((md = map_id2md(id)))
  10738. pc_clear_log_damage_sub(sd->status.char_id,md);
  10739. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10740. if (i < DAMAGELOG_SIZE_PC)
  10741. sd->dmglog[i] = 0;
  10742. }
  10743. }
  10744. /**
  10745. * Status change data arrived from char-server
  10746. * @param sd: Player data
  10747. */
  10748. void pc_scdata_received(struct map_session_data *sd) {
  10749. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10750. clif_weight_limit( sd );
  10751. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10752. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10753. }
  10754. sd->state.pc_loaded = true;
  10755. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10756. sd->state.connect_new = 1;
  10757. clif_parse_LoadEndAck(sd->fd, sd);
  10758. }
  10759. if (pc_iscarton(sd)) {
  10760. sd->cart_weight_max = 0; // Force a client refesh
  10761. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10762. }
  10763. }
  10764. /**
  10765. * Check player account expiration time and rental item expirations
  10766. * @param sd: Player data
  10767. */
  10768. void pc_check_expiration(struct map_session_data *sd) {
  10769. #ifndef ENABLE_SC_SAVING
  10770. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10771. #endif
  10772. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10773. time_t exp_time = sd->expiration_time;
  10774. char tmpstr[1024];
  10775. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10776. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  10777. pc_expire_check(sd);
  10778. }
  10779. }
  10780. TIMER_FUNC(pc_expiration_timer){
  10781. struct map_session_data *sd = map_id2sd(id);
  10782. if( !sd ) return 0;
  10783. sd->expiration_tid = INVALID_TIMER;
  10784. if( sd->fd )
  10785. clif_authfail_fd(sd->fd,10);
  10786. map_quit(sd);
  10787. return 0;
  10788. }
  10789. TIMER_FUNC(pc_autotrade_timer){
  10790. struct map_session_data *sd = map_id2sd(id);
  10791. if (!sd)
  10792. return 0;
  10793. sd->autotrade_tid = INVALID_TIMER;
  10794. if (sd->state.autotrade&2)
  10795. vending_reopen(sd);
  10796. if (sd->state.autotrade&4)
  10797. buyingstore_reopen(sd);
  10798. if (!sd->vender_id && !sd->buyer_id) {
  10799. sd->state.autotrade = 0;
  10800. map_quit(sd);
  10801. }
  10802. return 0;
  10803. }
  10804. /* this timer exists only when a character with a expire timer > 24h is online */
  10805. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10806. TIMER_FUNC(pc_global_expiration_timer){
  10807. struct s_mapiterator* iter;
  10808. struct map_session_data* sd;
  10809. iter = mapit_getallusers();
  10810. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10811. if( sd->expiration_time )
  10812. pc_expire_check(sd);
  10813. mapit_free(iter);
  10814. return 0;
  10815. }
  10816. void pc_expire_check(struct map_session_data *sd) {
  10817. /* ongoing timer */
  10818. if( sd->expiration_tid != INVALID_TIMER )
  10819. return;
  10820. /* not within the next 24h, enable the global check */
  10821. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10822. /* global check not running, enable */
  10823. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10824. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10825. return;
  10826. }
  10827. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10828. }
  10829. /**
  10830. * Deposit some money to bank
  10831. * @param sd
  10832. * @param money Amount of money to deposit
  10833. **/
  10834. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10835. unsigned int limit_check = money + sd->bank_vault;
  10836. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10837. return BDA_OVERFLOW;
  10838. } else if ( money > sd->status.zeny ) {
  10839. return BDA_NO_MONEY;
  10840. }
  10841. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10842. return BDA_NO_MONEY;
  10843. sd->bank_vault += money;
  10844. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10845. if( save_settings&CHARSAVE_BANK )
  10846. chrif_save(sd, CSAVE_NORMAL);
  10847. return BDA_SUCCESS;
  10848. }
  10849. /**
  10850. * Withdraw money from bank
  10851. * @param sd
  10852. * @param money Amount of money that will be withdrawn
  10853. **/
  10854. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10855. unsigned int limit_check = money + sd->status.zeny;
  10856. if( money <= 0 ) {
  10857. return BWA_UNKNOWN_ERROR;
  10858. } else if ( money > sd->bank_vault ) {
  10859. return BWA_NO_MONEY;
  10860. } else if ( limit_check > MAX_ZENY ) {
  10861. /* no official response for this scenario exists. */
  10862. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10863. return BWA_UNKNOWN_ERROR;
  10864. }
  10865. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10866. return BWA_NO_MONEY;
  10867. sd->bank_vault -= money;
  10868. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10869. if( save_settings&CHARSAVE_BANK )
  10870. chrif_save(sd, CSAVE_NORMAL);
  10871. return BWA_SUCCESS;
  10872. }
  10873. /**
  10874. * Clear Crimson Marker data from caster
  10875. * @param sd: Player
  10876. **/
  10877. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10878. uint8 i;
  10879. if (!sd)
  10880. return;
  10881. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10882. struct block_list *bl = NULL;
  10883. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10884. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10885. sd->c_marker[i] = 0;
  10886. }
  10887. }
  10888. /**
  10889. * Show version to player
  10890. * @param sd: Player
  10891. **/
  10892. void pc_show_version(struct map_session_data *sd) {
  10893. const char* svn = get_svn_revision();
  10894. char buf[CHAT_SIZE_MAX];
  10895. if( svn[0] != UNKNOWN_VERSION )
  10896. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10897. else {
  10898. const char* git = get_git_hash();
  10899. if( git[0] != UNKNOWN_VERSION )
  10900. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10901. else
  10902. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10903. }
  10904. clif_displaymessage(sd->fd,buf);
  10905. }
  10906. /**
  10907. * Run bonus_script on player
  10908. * @param sd
  10909. * @author [Cydh]
  10910. **/
  10911. void pc_bonus_script(struct map_session_data *sd) {
  10912. t_tick now = gettick();
  10913. struct linkdb_node *node = NULL, *next = NULL;
  10914. if (!sd || !(node = sd->bonus_script.head))
  10915. return;
  10916. while (node) {
  10917. struct s_bonus_script_entry *entry = NULL;
  10918. next = node->next;
  10919. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10920. // Only start timer for new bonus_script
  10921. if (entry->tid == INVALID_TIMER) {
  10922. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  10923. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10924. entry->tick += now;
  10925. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10926. }
  10927. if (entry->script)
  10928. run_script(entry->script, 0, sd->bl.id, 0);
  10929. else
  10930. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10931. }
  10932. node = next;
  10933. }
  10934. }
  10935. /**
  10936. * Add bonus_script to player
  10937. * @param sd Player
  10938. * @param script_str Script string
  10939. * @param dur Duration in ms
  10940. * @param icon EFST
  10941. * @param flag Flags @see enum e_bonus_script_flags
  10942. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10943. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10944. * @author [Cydh]
  10945. **/
  10946. 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) {
  10947. struct script_code *script = NULL;
  10948. struct linkdb_node *node = NULL;
  10949. struct s_bonus_script_entry *entry = NULL;
  10950. if (!sd)
  10951. return NULL;
  10952. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10953. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10954. return NULL;
  10955. }
  10956. // Duplication checks
  10957. if ((node = sd->bonus_script.head)) {
  10958. while (node) {
  10959. entry = (struct s_bonus_script_entry *)node->data;
  10960. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10961. t_tick newdur = gettick() + dur;
  10962. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10963. sett_tickimer(entry->tid, newdur);
  10964. script_free_code(script);
  10965. return NULL;
  10966. }
  10967. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10968. break;
  10969. else { // No duplicate bonus
  10970. script_free_code(script);
  10971. return NULL;
  10972. }
  10973. }
  10974. node = node->next;
  10975. }
  10976. }
  10977. CREATE(entry, struct s_bonus_script_entry, 1);
  10978. entry->script_buf = StringBuf_Malloc();
  10979. StringBuf_AppendStr(entry->script_buf, script_str);
  10980. entry->tid = INVALID_TIMER;
  10981. entry->flag = flag;
  10982. entry->icon = icon;
  10983. entry->tick = dur; // Use duration first, on run change to expire time
  10984. entry->type = type;
  10985. entry->script = script;
  10986. sd->bonus_script.count++;
  10987. return entry;
  10988. }
  10989. /**
  10990. * Remove bonus_script data from player
  10991. * @param sd: Target player
  10992. * @param list: Bonus script entry from player
  10993. * @author [Cydh]
  10994. **/
  10995. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10996. if (entry->tid != INVALID_TIMER)
  10997. delete_timer(entry->tid, pc_bonus_script_timer);
  10998. if (entry->script)
  10999. script_free_code(entry->script);
  11000. if (entry->script_buf)
  11001. StringBuf_Free(entry->script_buf);
  11002. if (sd) {
  11003. if (entry->icon != EFST_BLANK)
  11004. clif_status_load(&sd->bl, entry->icon, 0);
  11005. if (sd->bonus_script.count > 0)
  11006. sd->bonus_script.count--;
  11007. }
  11008. aFree(entry);
  11009. }
  11010. /**
  11011. * Do final process if no entry left
  11012. * @param sd
  11013. **/
  11014. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  11015. if (sd->bonus_script.count == 0) {
  11016. if (sd->bonus_script.head && sd->bonus_script.head->data)
  11017. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  11018. linkdb_final(&sd->bonus_script.head);
  11019. }
  11020. }
  11021. /**
  11022. * Timer for bonus_script
  11023. * @param tid
  11024. * @param tick
  11025. * @param id
  11026. * @param data
  11027. * @author [Cydh]
  11028. **/
  11029. TIMER_FUNC(pc_bonus_script_timer){
  11030. struct map_session_data *sd;
  11031. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  11032. sd = map_id2sd(id);
  11033. if (!sd) {
  11034. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  11035. return 0;
  11036. }
  11037. if (tid == INVALID_TIMER)
  11038. return 0;
  11039. if (!sd->bonus_script.head || entry == NULL) {
  11040. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  11041. return 0;
  11042. }
  11043. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11044. pc_bonus_script_free_entry(sd, entry);
  11045. pc_bonus_script_check_final(sd);
  11046. status_calc_pc(sd,SCO_NONE);
  11047. return 0;
  11048. }
  11049. /**
  11050. * Check then clear all active timer(s) of bonus_script data from player based on reason
  11051. * @param sd: Target player
  11052. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  11053. * @author [Cydh]
  11054. **/
  11055. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  11056. struct linkdb_node *node = NULL;
  11057. uint16 count = 0;
  11058. if (!sd || !(node = sd->bonus_script.head))
  11059. return;
  11060. while (node) {
  11061. struct linkdb_node *next = node->next;
  11062. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  11063. if (entry && (
  11064. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  11065. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  11066. (flag&entry->flag) || // Matched flag
  11067. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  11068. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  11069. )
  11070. )
  11071. {
  11072. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11073. pc_bonus_script_free_entry(sd, entry);
  11074. count++;
  11075. }
  11076. node = next;
  11077. }
  11078. pc_bonus_script_check_final(sd);
  11079. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  11080. status_calc_pc(sd,SCO_NONE);
  11081. }
  11082. /** [Cydh]
  11083. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  11084. * @param sd: Target player
  11085. */
  11086. void pc_cell_basilica(struct map_session_data *sd) {
  11087. nullpo_retv(sd);
  11088. #ifdef RENEWAL
  11089. enum sc_type type = SC_BASILICA_CELL;
  11090. #else
  11091. enum sc_type type = SC_BASILICA;
  11092. #endif
  11093. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  11094. if (sd->sc.data[type])
  11095. status_change_end(&sd->bl, type,INVALID_TIMER);
  11096. }
  11097. else if (!sd->sc.data[type])
  11098. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  11099. }
  11100. /** [Cydh]
  11101. * Get maximum specified parameter for specified class
  11102. * @param class_: sd->class
  11103. * @param sex: sd->status.sex
  11104. * @param flag: parameter will be checked
  11105. * @return max_param
  11106. */
  11107. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  11108. int idx = -1, class_ = sd->class_;
  11109. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  11110. short max_param = 0;
  11111. switch (param) {
  11112. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  11113. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  11114. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  11115. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  11116. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  11117. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  11118. }
  11119. if (max_param > 0)
  11120. return max_param;
  11121. }
  11122. // Always check babies first
  11123. if( class_ & JOBL_BABY ){
  11124. if( class_ & JOBL_THIRD ){
  11125. return battle_config.max_baby_third_parameter;
  11126. }else{
  11127. return battle_config.max_baby_parameter;
  11128. }
  11129. }
  11130. // Summoner
  11131. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11132. return battle_config.max_summoner_parameter;
  11133. }
  11134. // Extended classes
  11135. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11136. return battle_config.max_extended_parameter;
  11137. }
  11138. // 3rd class
  11139. if( class_ & JOBL_THIRD ){
  11140. // Transcendent
  11141. if( class_ & JOBL_UPPER ){
  11142. return battle_config.max_third_trans_parameter;
  11143. }else{
  11144. return battle_config.max_third_parameter;
  11145. }
  11146. }
  11147. // Transcendent
  11148. if( class_ & JOBL_UPPER ){
  11149. return battle_config.max_trans_parameter;
  11150. }
  11151. return battle_config.max_parameter;
  11152. }
  11153. /**
  11154. * Get max ASPD for player based on Class
  11155. * @param sd Player
  11156. * @return ASPD
  11157. */
  11158. short pc_maxaspd(struct map_session_data *sd) {
  11159. nullpo_ret(sd);
  11160. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  11161. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  11162. battle_config.max_aspd ));
  11163. }
  11164. /**
  11165. * Calculates total item-group related bonuses for the given item
  11166. * @param sd Player
  11167. * @param nameid Item ID
  11168. * @return Heal rate
  11169. **/
  11170. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  11171. if (sd->itemgrouphealrate.empty())
  11172. return 0;
  11173. short bonus = 0;
  11174. for (const auto &it : sd->itemgrouphealrate) {
  11175. uint16 group_id = it.id, i;
  11176. struct s_item_group_db *group = NULL;
  11177. if (!group_id || !(group = itemdb_group_exists(group_id)))
  11178. continue;
  11179. for (i = 0; i < group->random[0].data_qty; i++) {
  11180. if (group->random[0].data[i].nameid == nameid) {
  11181. bonus += it.val;
  11182. break;
  11183. }
  11184. }
  11185. }
  11186. return bonus;
  11187. }
  11188. /**
  11189. * Calculates total item-group related bonuses for the given item group
  11190. * @param sd Player
  11191. * @param group_id Item Group ID
  11192. * @return Heal rate
  11193. **/
  11194. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  11195. if (sd->itemgrouphealrate.empty())
  11196. return 0;
  11197. for (const auto &it : sd->itemgrouphealrate) {
  11198. if (it.id == group_id)
  11199. return it.val;
  11200. }
  11201. return 0;
  11202. }
  11203. /**
  11204. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  11205. * @param eqi Item EQI of enum equip_index
  11206. * @param *equip_index Player's equip_index[]
  11207. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  11208. * @return True if item in same inventory index, False if doesn't
  11209. */
  11210. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  11211. if (index < 0 || index >= MAX_INVENTORY)
  11212. return true;
  11213. // Dual weapon checks
  11214. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  11215. return true;
  11216. // Headgear with Mid & Low location
  11217. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  11218. return true;
  11219. // Headgear with Top & Mid or Low location
  11220. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  11221. return true;
  11222. // Headgear with Mid & Low location
  11223. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  11224. return true;
  11225. // Headgear with Top & Mid or Low location
  11226. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  11227. return true;
  11228. return false;
  11229. }
  11230. /**
  11231. * Generate Unique item ID for player
  11232. * @param sd : Player
  11233. * @return A generated Unique item ID
  11234. */
  11235. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  11236. nullpo_ret(sd);
  11237. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  11238. }
  11239. /**
  11240. * Validating skill from player after logged on
  11241. * @param sd
  11242. **/
  11243. void pc_validate_skill(struct map_session_data *sd) {
  11244. if (sd) {
  11245. uint16 i = 0, count = 0;
  11246. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  11247. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  11248. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  11249. for (i = 0; i < MAX_SKILL; i++) {
  11250. uint16 idx = 0;
  11251. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  11252. continue;
  11253. if ((idx = skill_get_index(tmp_skills[i].id))) {
  11254. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  11255. count++;
  11256. }
  11257. else
  11258. 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);
  11259. }
  11260. }
  11261. }
  11262. /**
  11263. * Toggle to remember if the questinfo is displayed yet or not.
  11264. * @param qi_display Display flag
  11265. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  11266. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  11267. **/
  11268. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct s_questinfo *qi, bool show) {
  11269. if (show) {
  11270. // Check if need to be displayed
  11271. if ((*qi_display) != 1) {
  11272. (*qi_display) = 1;
  11273. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  11274. }
  11275. }
  11276. else {
  11277. // Check if need to be hide
  11278. if ((*qi_display) != 0) {
  11279. (*qi_display) = 0;
  11280. #if PACKETVER >= 20120410
  11281. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_NONE, QMARK_NONE);
  11282. #else
  11283. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_QUEST, QMARK_NONE);
  11284. #endif
  11285. }
  11286. }
  11287. }
  11288. /**
  11289. * Show available NPC Quest / Event Icon Check [Kisuka]
  11290. * @param sd Player
  11291. **/
  11292. void pc_show_questinfo(struct map_session_data *sd) {
  11293. #if PACKETVER >= 20090218
  11294. nullpo_retv(sd);
  11295. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11296. return;
  11297. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11298. nullpo_retv(mapdata);
  11299. if (mapdata->qi_data.empty())
  11300. return;
  11301. if (mapdata->qi_data.size() != sd->qi_count)
  11302. return; // init was not called yet
  11303. struct s_questinfo *qi = nullptr;
  11304. bool show;
  11305. for (int i = 0; i < mapdata->qi_data.size(); i++) {
  11306. qi = &mapdata->qi_data[i];
  11307. if (!qi)
  11308. continue;
  11309. if (!qi->condition)
  11310. show = true;
  11311. else {
  11312. if (achievement_check_condition(qi->condition, sd))
  11313. show = true;
  11314. else
  11315. show = false;
  11316. }
  11317. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, show);
  11318. }
  11319. #endif
  11320. }
  11321. /**
  11322. * Reinit the questinfo for player when changing map
  11323. * @param sd Player
  11324. **/
  11325. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  11326. #if PACKETVER >= 20090218
  11327. nullpo_retv(sd);
  11328. if (sd->qi_display) {
  11329. aFree(sd->qi_display);
  11330. sd->qi_display = nullptr;
  11331. }
  11332. sd->qi_count = 0;
  11333. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11334. return;
  11335. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11336. nullpo_retv(mapdata);
  11337. if (mapdata->qi_data.empty())
  11338. return;
  11339. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_data.size()));
  11340. #endif
  11341. }
  11342. /**
  11343. * Check if a job is allowed to enter the map
  11344. * @param jobid Job ID see enum e_job or sd->status.class_
  11345. * @param m ID -an index- for direct indexing map[] array
  11346. * @return 1 if job is allowed, 0 otherwise
  11347. **/
  11348. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  11349. uint16 idx = 0;
  11350. // Map is other map server.
  11351. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  11352. if (m < 0)
  11353. return true;
  11354. struct map_data *mapdata = map_getmapdata(m);
  11355. if (!mapdata->cell)
  11356. return false;
  11357. if (!pcdb_checkid(jobid))
  11358. return false;
  11359. idx = pc_class2idx(jobid);
  11360. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  11361. return true;
  11362. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  11363. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  11364. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  11365. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  11366. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  11367. (job_info[idx].noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  11368. )
  11369. return false;
  11370. return true;
  11371. }
  11372. /**
  11373. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11374. * @param sd
  11375. **/
  11376. void pc_set_costume_view(struct map_session_data *sd) {
  11377. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11378. struct item_data *id = NULL;
  11379. nullpo_retv(sd);
  11380. head_low = sd->status.head_bottom;
  11381. head_mid = sd->status.head_mid;
  11382. head_top = sd->status.head_top;
  11383. robe = sd->status.robe;
  11384. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11385. //Added check to prevent sending the same look on multiple slots ->
  11386. //causes client to redraw item on top of itself. (suggested by Lupus)
  11387. // Normal headgear checks
  11388. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11389. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11390. sd->status.head_bottom = id->look;
  11391. else
  11392. sd->status.head_bottom = 0;
  11393. }
  11394. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11395. if (!(id->equip&(EQP_HEAD_TOP)))
  11396. sd->status.head_mid = id->look;
  11397. else
  11398. sd->status.head_mid = 0;
  11399. }
  11400. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11401. sd->status.head_top = id->look;
  11402. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11403. sd->status.robe = id->look;
  11404. // Costumes check
  11405. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11406. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11407. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11408. sd->status.head_bottom = id->look;
  11409. else
  11410. sd->status.head_bottom = 0;
  11411. }
  11412. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11413. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11414. sd->status.head_mid = id->look;
  11415. else
  11416. sd->status.head_mid = 0;
  11417. }
  11418. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11419. sd->status.head_top = id->look;
  11420. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11421. sd->status.robe = id->look;
  11422. }
  11423. if (sd->setlook_head_bottom)
  11424. sd->status.head_bottom = sd->setlook_head_bottom;
  11425. if (sd->setlook_head_mid)
  11426. sd->status.head_mid = sd->setlook_head_mid;
  11427. if (sd->setlook_head_top)
  11428. sd->status.head_top = sd->setlook_head_top;
  11429. if (sd->setlook_robe)
  11430. sd->status.robe = sd->setlook_robe;
  11431. if (head_low != sd->status.head_bottom)
  11432. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11433. if (head_mid != sd->status.head_mid)
  11434. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11435. if (head_top != sd->status.head_top)
  11436. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11437. if (robe != sd->status.robe)
  11438. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11439. }
  11440. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  11441. uint32 date = date_get(DT_YYYYMMDD);
  11442. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  11443. std::shared_ptr<s_attendance_period> period = pair.second;
  11444. if( period->start <= date && period->end >= date ){
  11445. return period;
  11446. }
  11447. }
  11448. return nullptr;
  11449. }
  11450. bool pc_attendance_enabled(){
  11451. // Check if the attendance feature is disabled
  11452. if( !battle_config.feature_attendance ){
  11453. return false;
  11454. }
  11455. // Check if there is a running attendance period
  11456. return pc_attendance_period() != nullptr;
  11457. }
  11458. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11459. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11460. }
  11461. int32 pc_attendance_counter( struct map_session_data* sd ){
  11462. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11463. // No running attendance period
  11464. if( period == nullptr ){
  11465. return 0;
  11466. }
  11467. // Get the counter for the current period
  11468. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11469. // Check if we have a remaining counter from a previous period
  11470. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11471. // Reset the counter to zero
  11472. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11473. return 0;
  11474. }
  11475. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11476. }
  11477. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11478. // If the user's group does not have the permission
  11479. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11480. return;
  11481. }
  11482. // Check if the attendance feature is disabled
  11483. if( !pc_attendance_enabled() ){
  11484. return;
  11485. }
  11486. // Check if the user already got his reward today
  11487. if( pc_attendance_rewarded_today( sd ) ){
  11488. return;
  11489. }
  11490. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11491. attendance_counter += 1;
  11492. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11493. if( period == nullptr ){
  11494. return;
  11495. }
  11496. if( period->rewards.size() < attendance_counter ){
  11497. return;
  11498. }
  11499. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11500. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11501. if( save_settings&CHARSAVE_ATTENDANCE )
  11502. chrif_save(sd, CSAVE_NORMAL);
  11503. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  11504. struct mail_message msg;
  11505. memset( &msg, 0, sizeof( struct mail_message ) );
  11506. msg.dest_id = sd->status.char_id;
  11507. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11508. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11509. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11510. msg.item[0].nameid = reward->item_id;
  11511. msg.item[0].amount = reward->amount;
  11512. msg.item[0].identify = 1;
  11513. msg.status = MAIL_NEW;
  11514. msg.type = MAIL_INBOX_NORMAL;
  11515. msg.timestamp = time(NULL);
  11516. intif_Mail_send(0, &msg);
  11517. clif_attendence_response( sd, attendance_counter );
  11518. }
  11519. /*==========================================
  11520. * pc Init/Terminate
  11521. *------------------------------------------*/
  11522. void do_final_pc(void) {
  11523. db_destroy(itemcd_db);
  11524. do_final_pc_groups();
  11525. ers_destroy(pc_sc_display_ers);
  11526. ers_destroy(num_reg_ers);
  11527. ers_destroy(str_reg_ers);
  11528. attendance_db.clear();
  11529. }
  11530. void do_init_pc(void) {
  11531. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11532. pc_readdb();
  11533. pc_read_motd(); // Read MOTD [Valaris]
  11534. attendance_db.load();
  11535. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11536. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11537. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11538. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11539. add_timer_func_list(pc_autosave, "pc_autosave");
  11540. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11541. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11542. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11543. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11544. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11545. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11546. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11547. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11548. // 0=day, 1=night [Yor]
  11549. night_flag = battle_config.night_at_start ? 1 : 0;
  11550. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11551. int day_duration = battle_config.day_duration;
  11552. int night_duration = battle_config.night_duration;
  11553. // add night/day timer [Yor]
  11554. add_timer_func_list(map_day_timer, "map_day_timer");
  11555. add_timer_func_list(map_night_timer, "map_night_timer");
  11556. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11557. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11558. }
  11559. do_init_pc_groups();
  11560. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11561. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11562. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11563. ers_chunk_size(pc_sc_display_ers, 150);
  11564. ers_chunk_size(num_reg_ers, 300);
  11565. ers_chunk_size(str_reg_ers, 50);
  11566. }