pc.cpp 412 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454
  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. static TIMER_FUNC(pc_soulball_timer)
  418. {
  419. struct map_session_data *sd;
  420. if ((sd = (struct map_session_data *)map_id2sd(id)) == nullptr || sd->bl.type != BL_PC)
  421. return 1;
  422. if (sd->soulball <= 0) {
  423. ShowError("pc_soulball_timer: %d soulball's available. (aid=%d tid=%d)\n", sd->soulball, sd->status.account_id, tid);
  424. sd->soulball = 0;
  425. return 0;
  426. }
  427. int i;
  428. ARR_FIND(0, sd->soulball, i, sd->soul_timer[i] == tid);
  429. if (i == sd->soulball) {
  430. ShowError("pc_soulball_timer: timer not found (aid=%d tid=%d)\n", sd->status.account_id, tid);
  431. return 0;
  432. }
  433. sd->soulball--;
  434. if (i != sd->soulball)
  435. memmove(sd->soul_timer + i, sd->soul_timer + i + 1, (sd->soulball - i) * sizeof(int));
  436. sd->soul_timer[sd->soulball] = INVALID_TIMER;
  437. clif_soulball(sd);
  438. return 0;
  439. }
  440. /**
  441. * Adds a soulball to player for 'interval' ms
  442. * @param sd: Player data
  443. * @param interval: Duration
  444. * @param max: Max amount of soulballs
  445. */
  446. int pc_addsoulball(struct map_session_data *sd, int interval, int max)
  447. {
  448. nullpo_ret(sd);
  449. max = min(max, MAX_SOUL_BALL);
  450. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  451. if (sd->soulball && sd->soulball >= max) {
  452. if (sd->soul_timer[0] != INVALID_TIMER)
  453. delete_timer(sd->soul_timer[0], pc_soulball_timer);
  454. sd->soulball--;
  455. if (sd->soulball != 0)
  456. memmove(sd->soul_timer + 0, sd->soul_timer + 1, (sd->soulball) * sizeof(int));
  457. sd->soul_timer[sd->soulball] = INVALID_TIMER;
  458. }
  459. if (interval > 0) {
  460. int tid = add_timer(gettick() + interval, pc_soulball_timer, sd->bl.id, 0), i;
  461. ARR_FIND(0, sd->soulball, i, sd->soul_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->soul_timer[i])->tick) < 0);
  462. if (i != sd->soulball)
  463. memmove(sd->soul_timer + i + 1, sd->soul_timer + i, (sd->soulball - i) * sizeof(int));
  464. sd->soul_timer[i] = tid;
  465. }
  466. sd->soulball++;
  467. clif_soulball(sd);
  468. return 0;
  469. }
  470. /**
  471. * Removes number of soulball from player
  472. * @param sd: Player data
  473. * @param count: Amount to remove
  474. * @param type: 1 = doesn't give client effect
  475. */
  476. int pc_delsoulball(struct map_session_data *sd, int count, int type)
  477. {
  478. nullpo_ret(sd);
  479. if (sd->soulball <= 0) {
  480. sd->soulball = 0;
  481. return 0;
  482. }
  483. if (count <= 0)
  484. return 0;
  485. if (count > sd->soulball)
  486. count = sd->soulball;
  487. sd->soulball -= count;
  488. if (count > MAX_SKILL_LEVEL)
  489. count = MAX_SKILL_LEVEL;
  490. for (int i = 0; i < count; i++) {
  491. if (sd->soul_timer[i] != INVALID_TIMER) {
  492. delete_timer(sd->soul_timer[i], pc_soulball_timer);
  493. sd->soul_timer[i] = INVALID_TIMER;
  494. }
  495. }
  496. for (int i = count; i < MAX_SKILL_LEVEL; i++) {
  497. sd->soul_timer[i - count] = sd->soul_timer[i];
  498. sd->soul_timer[i] = INVALID_TIMER;
  499. }
  500. if (!type)
  501. clif_soulball(sd);
  502. return 0;
  503. }
  504. /**
  505. * Increases a player's fame points and displays a notice to him
  506. * @param sd Player
  507. * @param count Fame point
  508. */
  509. void pc_addfame(struct map_session_data *sd,int count)
  510. {
  511. enum e_rank ranktype;
  512. nullpo_retv(sd);
  513. sd->status.fame += count;
  514. if(sd->status.fame > MAX_FAME)
  515. sd->status.fame = MAX_FAME;
  516. switch(sd->class_&MAPID_UPPERMASK){
  517. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  518. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  519. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  520. default:
  521. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  522. return;
  523. }
  524. clif_update_rankingpoint(sd,ranktype,count);
  525. chrif_updatefamelist(sd);
  526. }
  527. /**
  528. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  529. * @param sd
  530. * @param job Job use enum e_mapid
  531. * @return Rank
  532. */
  533. unsigned char pc_famerank(uint32 char_id, int job)
  534. {
  535. uint8 i;
  536. switch(job){
  537. case MAPID_BLACKSMITH: // Blacksmith
  538. for(i = 0; i < MAX_FAME_LIST; i++){
  539. if(smith_fame_list[i].id == char_id)
  540. return i + 1;
  541. }
  542. break;
  543. case MAPID_ALCHEMIST: // Alchemist
  544. for(i = 0; i < MAX_FAME_LIST; i++){
  545. if(chemist_fame_list[i].id == char_id)
  546. return i + 1;
  547. }
  548. break;
  549. case MAPID_TAEKWON: // Taekwon
  550. for(i = 0; i < MAX_FAME_LIST; i++){
  551. if(taekwon_fame_list[i].id == char_id)
  552. return i + 1;
  553. }
  554. break;
  555. }
  556. return 0;
  557. }
  558. /**
  559. * Restart player's HP & SP value
  560. * @param sd
  561. * @param type Restart type: 1 - Normal Resurection
  562. */
  563. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  564. struct status_data *status, *b_status;
  565. nullpo_retv(sd);
  566. b_status = &sd->base_status;
  567. status = &sd->battle_status;
  568. if (type&1) { //Normal resurrection
  569. status->hp = 1; //Otherwise status_heal may fail if dead.
  570. status_heal(&sd->bl, b_status->hp, 0, 1);
  571. if( status->sp < b_status->sp )
  572. status_set_sp(&sd->bl, b_status->sp, 1);
  573. } else { //Just for saving on the char-server (with values as if respawned)
  574. sd->status.hp = b_status->hp;
  575. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  576. }
  577. }
  578. /*==========================================
  579. Rental System
  580. *------------------------------------------*/
  581. /**
  582. * Ends a rental and removes the item/effect
  583. * @param tid: Tick ID
  584. * @param tick: Timer
  585. * @param id: Timer ID
  586. * @param data: Data
  587. * @return false - failure, true - success
  588. */
  589. TIMER_FUNC(pc_inventory_rental_end){
  590. struct map_session_data *sd = map_id2sd(id);
  591. if( sd == NULL )
  592. return 0;
  593. if( tid != sd->rental_timer ) {
  594. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  595. return 0;
  596. }
  597. pc_inventory_rentals(sd);
  598. return 1;
  599. }
  600. /**
  601. * Removes the rental timer from the player
  602. * @param sd: Player data
  603. */
  604. void pc_inventory_rental_clear(struct map_session_data *sd)
  605. {
  606. if( sd->rental_timer != INVALID_TIMER ) {
  607. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  608. sd->rental_timer = INVALID_TIMER;
  609. }
  610. }
  611. /**
  612. * Check for items in the player's inventory that are rental type
  613. * @param sd: Player data
  614. */
  615. void pc_inventory_rentals(struct map_session_data *sd)
  616. {
  617. int i, c = 0;
  618. unsigned int next_tick = UINT_MAX;
  619. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  620. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  621. continue; // Nothing here
  622. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  623. continue;
  624. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  625. if (sd->inventory_data[i]->unequip_script)
  626. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  627. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  628. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  629. } else {
  630. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  631. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  632. next_tick = umin(expire_tick * 1000U, next_tick);
  633. c++;
  634. }
  635. }
  636. 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
  637. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  638. else
  639. sd->rental_timer = INVALID_TIMER;
  640. }
  641. /**
  642. * Add a rental item to the player and adjusts the rental timer appropriately
  643. * @param sd: Player data
  644. * @param seconds: Rental time
  645. */
  646. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  647. {
  648. t_tick tick = seconds * 1000;
  649. if( sd == NULL )
  650. return;
  651. if( sd->rental_timer != INVALID_TIMER ) {
  652. const struct TimerData * td;
  653. td = get_timer(sd->rental_timer);
  654. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  655. pc_inventory_rental_clear(sd);
  656. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  657. }
  658. } else
  659. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  660. }
  661. /**
  662. * Check if the player can sell the current item
  663. * @param sd: map_session_data of the player
  664. * @param item: struct of the checking item
  665. * @param shoptype: NPC's sub type see enum npc_subtype
  666. * @return bool 'true' is sellable, 'false' otherwise
  667. */
  668. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  669. if (sd == NULL || item == NULL)
  670. return false;
  671. if (item->equip > 0 || item->amount < 0)
  672. return false;
  673. if (battle_config.hide_fav_sell && item->favorite)
  674. return false; //Cannot sell favs (optional config)
  675. if (!battle_config.rental_transaction && item->expire_time)
  676. return false; // Cannot Sell Rental Items
  677. if( item->equipSwitch ){
  678. return false;
  679. }
  680. if (itemdb_ishatched_egg(item))
  681. return false;
  682. switch (shoptype) {
  683. case NPCTYPE_SHOP:
  684. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  685. item->bound != BOUND_GUILD ||
  686. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  687. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  688. ))
  689. return true;
  690. break;
  691. case NPCTYPE_ITEMSHOP:
  692. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  693. item->bound != BOUND_GUILD ||
  694. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  695. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  696. ))
  697. return true;
  698. else if (!item->bound) {
  699. struct item_data *itd = itemdb_search(item->nameid);
  700. if (itd && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  701. return true;
  702. }
  703. break;
  704. }
  705. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  706. return false;
  707. if (item->bound && !pc_can_give_bounded_items(sd))
  708. return false; // Don't allow sale of bound items
  709. return true;
  710. }
  711. /**
  712. * Determines if player can give / drop / trade / vend items
  713. */
  714. bool pc_can_give_items(struct map_session_data *sd)
  715. {
  716. return pc_has_permission(sd, PC_PERM_TRADE);
  717. }
  718. /**
  719. * Determines if player can give / drop / trade / vend bounded items
  720. */
  721. bool pc_can_give_bounded_items(struct map_session_data *sd)
  722. {
  723. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  724. }
  725. /*==========================================
  726. * Prepares character for saving.
  727. * @param sd
  728. *------------------------------------------*/
  729. void pc_makesavestatus(struct map_session_data *sd) {
  730. nullpo_retv(sd);
  731. if(!battle_config.save_clothcolor)
  732. sd->status.clothes_color = 0;
  733. // Since this is currently not officially released,
  734. // its best to have a forced option to not save body styles.
  735. if(!battle_config.save_body_style)
  736. sd->status.body = 0;
  737. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  738. //status change load/saving. [Skotlex]
  739. #ifdef NEW_CARTS
  740. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  741. #else
  742. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  743. #endif
  744. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  745. if(pc_isdead(sd)){
  746. pc_setrestartvalue(sd, 0);
  747. } else {
  748. sd->status.hp = sd->battle_status.hp;
  749. sd->status.sp = sd->battle_status.sp;
  750. }
  751. sd->status.last_point.map = sd->mapindex;
  752. sd->status.last_point.x = sd->bl.x;
  753. sd->status.last_point.y = sd->bl.y;
  754. return;
  755. }
  756. if(pc_isdead(sd)) {
  757. pc_setrestartvalue(sd, 0);
  758. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  759. } else {
  760. sd->status.hp = sd->battle_status.hp;
  761. sd->status.sp = sd->battle_status.sp;
  762. sd->status.last_point.map = sd->mapindex;
  763. sd->status.last_point.x = sd->bl.x;
  764. sd->status.last_point.y = sd->bl.y;
  765. }
  766. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  767. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  768. if(mapdata->save.map)
  769. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  770. else
  771. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  772. }
  773. }
  774. /*==========================================
  775. * Off init ? Connection?
  776. *------------------------------------------*/
  777. 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) {
  778. nullpo_retv(sd);
  779. sd->bl.id = account_id;
  780. sd->status.account_id = account_id;
  781. sd->status.char_id = char_id;
  782. sd->status.sex = sex;
  783. sd->login_id1 = login_id1;
  784. sd->login_id2 = 0; // at this point, we can not know the value :(
  785. sd->client_tick = client_tick;
  786. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  787. sd->bl.type = BL_PC;
  788. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  789. sd->canlog_tick = gettick();
  790. //Required to prevent homunculus copuing a base speed of 0.
  791. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  792. }
  793. /**
  794. * Get equip point for an equip
  795. * @param sd
  796. * @param id
  797. */
  798. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  799. int ep = 0;
  800. nullpo_ret(sd);
  801. nullpo_ret(id);
  802. if (!itemdb_isequip2(id))
  803. return 0; //Not equippable by players.
  804. ep = id->equip;
  805. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  806. 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]
  807. if (ep == EQP_WEAPON)
  808. return EQP_ARMS;
  809. if (ep == EQP_SHADOW_WEAPON)
  810. return EQP_SHADOW_ARMS;
  811. }
  812. }
  813. return ep;
  814. }
  815. /**
  816. * Get equip point for an equip
  817. * @param sd
  818. * @param n Equip index in inventory
  819. */
  820. int pc_equippoint(struct map_session_data *sd,int n){
  821. nullpo_ret(sd);
  822. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  823. }
  824. /**
  825. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  826. * @param sd : player session
  827. * @return 0 sucess, 1:invalid sd
  828. */
  829. void pc_setinventorydata(struct map_session_data *sd)
  830. {
  831. uint8 i;
  832. nullpo_retv(sd);
  833. for(i = 0; i < MAX_INVENTORY; i++) {
  834. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  835. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  836. }
  837. }
  838. /**
  839. * 'Calculates' weapon type
  840. * @param sd : Player
  841. */
  842. void pc_calcweapontype(struct map_session_data *sd)
  843. {
  844. nullpo_retv(sd);
  845. // single-hand
  846. if(sd->weapontype2 == W_FIST) {
  847. sd->status.weapon = sd->weapontype1;
  848. return;
  849. }
  850. if(sd->weapontype1 == W_FIST) {
  851. sd->status.weapon = sd->weapontype2;
  852. return;
  853. }
  854. // dual-wield
  855. sd->status.weapon = 0;
  856. switch (sd->weapontype1){
  857. case W_DAGGER:
  858. switch (sd->weapontype2) {
  859. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  860. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  861. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  862. }
  863. break;
  864. case W_1HSWORD:
  865. switch (sd->weapontype2) {
  866. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  867. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  868. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  869. }
  870. break;
  871. case W_1HAXE:
  872. switch (sd->weapontype2) {
  873. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  874. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  875. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  876. }
  877. }
  878. // unknown, default to right hand type
  879. if (!sd->status.weapon)
  880. sd->status.weapon = sd->weapontype1;
  881. }
  882. /**
  883. * Set equip index
  884. * @param sd : Player
  885. */
  886. void pc_setequipindex(struct map_session_data *sd)
  887. {
  888. uint16 i;
  889. nullpo_retv(sd);
  890. for (i = 0; i < EQI_MAX; i++){
  891. sd->equip_index[i] = -1;
  892. sd->equip_switch_index[i] = -1;
  893. }
  894. for (i = 0; i < MAX_INVENTORY; i++) {
  895. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  896. continue;
  897. if (sd->inventory.u.items_inventory[i].equip) {
  898. uint8 j;
  899. for (j = 0; j < EQI_MAX; j++)
  900. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  901. sd->equip_index[j] = i;
  902. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  903. if (sd->inventory_data[i])
  904. sd->weapontype1 = sd->inventory_data[i]->look;
  905. else
  906. sd->weapontype1 = 0;
  907. }
  908. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  909. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  910. sd->weapontype2 = sd->inventory_data[i]->look;
  911. else
  912. sd->weapontype2 = 0;
  913. }
  914. }
  915. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  916. for (uint8 j = 0; j < EQI_MAX; j++) {
  917. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  918. sd->equip_switch_index[j] = i;
  919. }
  920. }
  921. }
  922. }
  923. pc_calcweapontype(sd);
  924. }
  925. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  926. //{
  927. // int i;
  928. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  929. // struct item_data *data;
  930. //
  931. // //Crafted/made/hatched items.
  932. // if (itemdb_isspecial(item->card[0]))
  933. // return 1;
  934. //
  935. // /* scan for enchant armor gems */
  936. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  937. // s = MAX_SLOTS - 1;
  938. //
  939. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  940. // return( i < s ) ? 0 : 1;
  941. //}
  942. /**
  943. * Check if an item is equiped by player
  944. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  945. * @param sd : player session
  946. * @param nameid : itemid
  947. * @return 1:yes, 0:no
  948. */
  949. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  950. {
  951. uint8 i;
  952. for( i = 0; i < EQI_MAX; i++ )
  953. {
  954. short index = sd->equip_index[i], j;
  955. if( index < 0 )
  956. continue;
  957. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  958. continue;
  959. if( !sd->inventory_data[index] )
  960. continue;
  961. if( sd->inventory_data[index]->nameid == nameid )
  962. return true;
  963. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  964. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  965. return true;
  966. }
  967. }
  968. return false;
  969. }
  970. /**
  971. * Check adoption rules
  972. * @param p1_sd: Player 1
  973. * @param p2_sd: Player 2
  974. * @param b_sd: Player that will be adopted
  975. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  976. * ADOPT_ALREADY_ADOPTED - Already adopted
  977. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  978. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  979. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  980. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  981. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  982. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  983. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  984. */
  985. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  986. {
  987. if( !p1_sd || !p2_sd || !b_sd )
  988. return ADOPT_CHARACTER_NOT_FOUND;
  989. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  990. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  991. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  992. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  993. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  994. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  995. if( p2_sd->status.party_id != p1_sd->status.party_id )
  996. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  997. // Parents need to have their ring equipped
  998. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  999. return ADOPT_EQUIP_RINGS;
  1000. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1001. return ADOPT_EQUIP_RINGS;
  1002. // Already adopted a baby
  1003. if( p1_sd->status.child || p2_sd->status.child ) {
  1004. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1005. return ADOPT_MORE_CHILDREN;
  1006. }
  1007. // Parents need at least lvl 70 to adopt
  1008. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1009. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1010. return ADOPT_LEVEL_70;
  1011. }
  1012. if( b_sd->status.partner_id ) {
  1013. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1014. return ADOPT_MARRIED;
  1015. }
  1016. 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 ) )
  1017. return ADOPT_NOT_NOVICE;
  1018. return ADOPT_ALLOWED;
  1019. }
  1020. /*==========================================
  1021. * Adoption Process
  1022. *------------------------------------------*/
  1023. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1024. {
  1025. int job, joblevel;
  1026. unsigned int jobexp;
  1027. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1028. return false;
  1029. // Preserve current job levels and progress
  1030. joblevel = b_sd->status.job_level;
  1031. jobexp = b_sd->status.job_exp;
  1032. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1033. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1034. { // Success, proceed to configure parents and baby skills
  1035. p1_sd->status.child = b_sd->status.char_id;
  1036. p2_sd->status.child = b_sd->status.char_id;
  1037. b_sd->status.father = p1_sd->status.char_id;
  1038. b_sd->status.mother = p2_sd->status.char_id;
  1039. // Restore progress
  1040. b_sd->status.job_level = joblevel;
  1041. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1042. b_sd->status.job_exp = jobexp;
  1043. clif_updatestatus(b_sd, SP_JOBEXP);
  1044. // Baby Skills
  1045. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1046. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1047. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1048. // Parents Skills
  1049. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1050. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1051. chrif_save(p1_sd, CSAVE_NORMAL);
  1052. chrif_save(p2_sd, CSAVE_NORMAL);
  1053. chrif_save(b_sd, CSAVE_NORMAL);
  1054. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1055. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1056. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1057. return true;
  1058. }
  1059. return false; // Job Change Fail
  1060. }
  1061. /*==========================================
  1062. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1063. Returns:
  1064. false : Cannot use/equip
  1065. true : Can use/equip
  1066. * Credits:
  1067. [Inkfish] for first idea
  1068. [Haru] for third-classes extension
  1069. [Cydh] finishing :D
  1070. *------------------------------------------*/
  1071. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  1072. while (1) {
  1073. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  1074. break;
  1075. #ifndef RENEWAL
  1076. //allow third classes to use trans. class items
  1077. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1078. break;
  1079. //third-baby classes can use same item too
  1080. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1081. break;
  1082. //don't need to decide specific rules for third-classes?
  1083. //items for third classes can be used for all third classes
  1084. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1085. break;
  1086. #else
  1087. //trans. classes (exl. third-trans.)
  1088. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1089. break;
  1090. //baby classes (exl. third-baby)
  1091. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1092. break;
  1093. //third classes (exl. third-trans. and baby-third)
  1094. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  1095. break;
  1096. //trans-third classes
  1097. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  1098. break;
  1099. //third-baby classes
  1100. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  1101. break;
  1102. #endif
  1103. return false;
  1104. }
  1105. return true;
  1106. }
  1107. /*=================================================
  1108. * Checks if the player can equip the item at index n in inventory.
  1109. * @param sd
  1110. * @param n Item index in inventory
  1111. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1112. *------------------------------------------------*/
  1113. uint8 pc_isequip(struct map_session_data *sd,int n)
  1114. {
  1115. struct item_data *item;
  1116. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1117. item = sd->inventory_data[n];
  1118. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1119. return ITEM_EQUIP_ACK_OK;
  1120. if(item == NULL)
  1121. return ITEM_EQUIP_ACK_FAIL;
  1122. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1123. return ITEM_EQUIP_ACK_FAILLEVEL;
  1124. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1125. return ITEM_EQUIP_ACK_FAILLEVEL;
  1126. if(item->sex != 2 && sd->status.sex != item->sex)
  1127. return ITEM_EQUIP_ACK_FAIL;
  1128. //fail to equip if item is restricted
  1129. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1130. return ITEM_EQUIP_ACK_FAIL;
  1131. if (item->equip&EQP_AMMO) {
  1132. switch (item->look) {
  1133. case AMMO_ARROW:
  1134. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1135. clif_msg(sd, ITEM_NEED_BOW);
  1136. return ITEM_EQUIP_ACK_FAIL;
  1137. }
  1138. break;
  1139. case AMMO_THROWABLE_DAGGER:
  1140. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1141. return ITEM_EQUIP_ACK_FAIL;
  1142. break;
  1143. case AMMO_BULLET:
  1144. case AMMO_SHELL:
  1145. 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
  1146. #ifdef RENEWAL
  1147. && sd->status.weapon != W_GRENADE
  1148. #endif
  1149. ) {
  1150. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1151. return ITEM_EQUIP_ACK_FAIL;
  1152. }
  1153. break;
  1154. #ifndef RENEWAL
  1155. case AMMO_GRENADE:
  1156. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1157. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1158. return ITEM_EQUIP_ACK_FAIL;
  1159. }
  1160. break;
  1161. #endif
  1162. case AMMO_CANNONBALL:
  1163. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1164. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1165. return ITEM_EQUIP_ACK_FAIL;
  1166. }
  1167. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1168. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1169. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1170. return ITEM_EQUIP_ACK_FAIL;
  1171. }
  1172. break;
  1173. }
  1174. }
  1175. if (sd->sc.count) {
  1176. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1177. return ITEM_EQUIP_ACK_FAIL;
  1178. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1179. return ITEM_EQUIP_ACK_FAIL;
  1180. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1181. return ITEM_EQUIP_ACK_FAIL;
  1182. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1183. return ITEM_EQUIP_ACK_FAIL;
  1184. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1185. return ITEM_EQUIP_ACK_FAIL;
  1186. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1187. return ITEM_EQUIP_ACK_FAIL;
  1188. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1189. return ITEM_EQUIP_ACK_FAIL;
  1190. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1191. //Spirit of Super Novice equip bonuses. [Skotlex]
  1192. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1193. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1194. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  1195. switch(item->look) { //In weapons, the look determines type of weapon.
  1196. case W_DAGGER: //All level 4 - Daggers
  1197. case W_1HSWORD: //All level 4 - 1H Swords
  1198. case W_1HAXE: //All level 4 - 1H Axes
  1199. case W_MACE: //All level 4 - 1H Maces
  1200. case W_STAFF: //All level 4 - 1H Staves
  1201. case W_2HSTAFF: //All level 4 - 2H Staves
  1202. return ITEM_EQUIP_ACK_OK;
  1203. }
  1204. }
  1205. }
  1206. //Not equipable by class. [Skotlex]
  1207. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  1208. return ITEM_EQUIP_ACK_FAIL;
  1209. if (!pc_isItemClass(sd, item))
  1210. return ITEM_EQUIP_ACK_FAIL;
  1211. return ITEM_EQUIP_ACK_OK;
  1212. }
  1213. /*==========================================
  1214. * No problem with the session id
  1215. * set the status that has been sent from char server
  1216. *------------------------------------------*/
  1217. 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)
  1218. {
  1219. int i;
  1220. t_tick tick = gettick();
  1221. uint32 ip = session[sd->fd]->client_addr;
  1222. sd->login_id2 = login_id2;
  1223. sd->group_id = group_id;
  1224. /* load user permissions */
  1225. pc_group_pc_load(sd);
  1226. memcpy(&sd->status, st, sizeof(*st));
  1227. if (st->sex != sd->status.sex) {
  1228. clif_authfail_fd(sd->fd, 0);
  1229. return false;
  1230. }
  1231. //Set the map-server used job id. [Skotlex]
  1232. i = pc_jobid2mapid(sd->status.class_);
  1233. if (i == -1) { //Invalid class?
  1234. 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);
  1235. sd->status.class_ = JOB_NOVICE;
  1236. sd->class_ = MAPID_NOVICE;
  1237. } else
  1238. sd->class_ = i;
  1239. // Checks and fixes to character status data, that are required
  1240. // in case of configuration change or stuff, which cannot be
  1241. // checked on char-server.
  1242. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1243. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1244. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1245. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1246. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1247. sd->state.connect_new = 1;
  1248. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1249. sd->invincible_timer = INVALID_TIMER;
  1250. sd->npc_timer_id = INVALID_TIMER;
  1251. sd->pvp_timer = INVALID_TIMER;
  1252. sd->expiration_tid = INVALID_TIMER;
  1253. sd->autotrade_tid = INVALID_TIMER;
  1254. sd->respawn_tid = INVALID_TIMER;
  1255. #ifdef SECURE_NPCTIMEOUT
  1256. // Initialize to defaults/expected
  1257. sd->npc_idle_timer = INVALID_TIMER;
  1258. sd->npc_idle_tick = tick;
  1259. sd->npc_idle_type = NPCT_INPUT;
  1260. sd->state.ignoretimeout = false;
  1261. #endif
  1262. sd->canuseitem_tick = tick;
  1263. sd->canusecashfood_tick = tick;
  1264. sd->canequip_tick = tick;
  1265. sd->cantalk_tick = tick;
  1266. sd->canskill_tick = tick;
  1267. sd->cansendmail_tick = tick;
  1268. sd->idletime = last_tick;
  1269. for(i = 0; i < MAX_SPIRITBALL; i++)
  1270. sd->spirit_timer[i] = INVALID_TIMER;
  1271. for (i = 0; i < MAX_SOUL_BALL; i++)
  1272. sd->soul_timer[i] = INVALID_TIMER;
  1273. if (battle_config.item_auto_get)
  1274. sd->state.autoloot = 10000;
  1275. if (battle_config.disp_experience)
  1276. sd->state.showexp = 1;
  1277. if (battle_config.disp_zeny)
  1278. sd->state.showzeny = 1;
  1279. #ifdef VIP_ENABLE
  1280. if (!battle_config.vip_disp_rate)
  1281. sd->vip.disableshowrate = 1;
  1282. #endif
  1283. if (!(battle_config.display_skill_fail&2))
  1284. sd->state.showdelay = 1;
  1285. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1286. memset(&sd->cart, 0, sizeof(struct s_storage));
  1287. memset(&sd->storage, 0, sizeof(struct s_storage));
  1288. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1289. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1290. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1291. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1292. sd->status.option &= ~OPTION_INVISIBLE;
  1293. }
  1294. status_change_init(&sd->bl);
  1295. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1296. unit_dataset(&sd->bl);
  1297. sd->guild_x = -1;
  1298. sd->guild_y = -1;
  1299. sd->delayed_damage = 0;
  1300. // Event Timers
  1301. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1302. sd->eventtimer[i] = INVALID_TIMER;
  1303. // Rental Timer
  1304. sd->rental_timer = INVALID_TIMER;
  1305. for( i = 0; i < 3; i++ )
  1306. sd->hate_mob[i] = -1;
  1307. sd->quest_log = NULL;
  1308. sd->num_quests = 0;
  1309. sd->avail_quests = 0;
  1310. sd->save_quest = false;
  1311. sd->count_rewarp = 0;
  1312. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1313. sd->regs.arrays = NULL;
  1314. sd->vars_dirty = false;
  1315. sd->vars_ok = false;
  1316. sd->vars_received = 0x0;
  1317. sd->qi_display = nullptr;
  1318. sd->qi_count = 0;
  1319. //warp player
  1320. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1321. 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);
  1322. // try warping to a default map instead (church graveyard)
  1323. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1324. // if we fail again
  1325. clif_authfail_fd(sd->fd, 0);
  1326. return false;
  1327. }
  1328. }
  1329. clif_authok(sd);
  1330. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1331. sd->die_counter=-1;
  1332. //display login notice
  1333. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1334. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1335. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1336. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1337. sd->status.name, sd->status.account_id, sd->status.char_id,
  1338. CONVIP(ip), sd->group_id);
  1339. // Send friends list
  1340. clif_friendslist_send(sd);
  1341. if( !changing_mapservers ) {
  1342. if (battle_config.display_version == 1)
  1343. pc_show_version(sd);
  1344. // Message of the Day [Valaris]
  1345. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1346. if (battle_config.motd_type)
  1347. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1348. else
  1349. clif_displaymessage(sd->fd, motd_text[i]);
  1350. }
  1351. if (expiration_time != 0)
  1352. sd->expiration_time = expiration_time;
  1353. /**
  1354. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1355. **/
  1356. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1357. }
  1358. pc_validate_skill(sd);
  1359. /* [Ind] */
  1360. sd->sc_display = NULL;
  1361. sd->sc_display_count = 0;
  1362. // Player has not yet received the CashShop list
  1363. sd->status.cashshop_sent = false;
  1364. sd->last_addeditem_index = -1;
  1365. sd->bonus_script.head = NULL;
  1366. sd->bonus_script.count = 0;
  1367. // Initialize BG queue pointer
  1368. sd->bg_queue = nullptr;
  1369. sd->bg_queue_accept_state = false;
  1370. #if PACKETVER >= 20150513
  1371. sd->hatEffectIDs = NULL;
  1372. sd->hatEffectCount = 0;
  1373. #endif
  1374. sd->catch_target_class = PET_CATCH_FAIL;
  1375. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1376. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1377. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1378. clif_updatestatus(sd, SP_BASEEXP);
  1379. }
  1380. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1381. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1382. clif_updatestatus(sd, SP_JOBEXP);
  1383. }
  1384. // Request all registries (auth is considered completed whence they arrive)
  1385. intif_request_registry(sd,7);
  1386. return true;
  1387. }
  1388. /*==========================================
  1389. * Closes a connection because it failed to be authenticated from the char server.
  1390. *------------------------------------------*/
  1391. void pc_authfail(struct map_session_data *sd)
  1392. {
  1393. clif_authfail_fd(sd->fd, 0);
  1394. return;
  1395. }
  1396. /**
  1397. * Player register a bl as hatred
  1398. * @param sd : player session
  1399. * @param pos : hate position [0;2]
  1400. * @param bl : target bl
  1401. * @return false:failed, true:success
  1402. */
  1403. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1404. {
  1405. int class_;
  1406. if (!sd || !bl || pos < 0 || pos > 2)
  1407. return false;
  1408. if (sd->hate_mob[pos] != -1)
  1409. { //Can't change hate targets.
  1410. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1411. return false;
  1412. }
  1413. class_ = status_get_class(bl);
  1414. if (!pcdb_checkid(class_)) {
  1415. unsigned int max_hp = status_get_max_hp(bl);
  1416. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1417. return false;
  1418. if (pos != status_get_size(bl))
  1419. return false; //Wrong size
  1420. }
  1421. sd->hate_mob[pos] = class_;
  1422. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1423. clif_hate_info(sd, pos, class_, 1);
  1424. return true;
  1425. }
  1426. /*==========================================
  1427. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1428. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1429. * See intif_parse_StorageReceived() for item operations [lighta]
  1430. *------------------------------------------*/
  1431. void pc_reg_received(struct map_session_data *sd)
  1432. {
  1433. uint8 i;
  1434. sd->vars_ok = true;
  1435. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1436. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1437. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1438. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1439. if (msg_checklangtype(sd->langtype,true) < 0)
  1440. sd->langtype = 0; //invalid langtype reset to default
  1441. // Cash shop
  1442. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1443. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1444. // Cooking Exp
  1445. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1446. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1447. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1448. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1449. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1450. }
  1451. if (battle_config.feature_banking)
  1452. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1453. if (battle_config.feature_roulette) {
  1454. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1455. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1456. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1457. }
  1458. sd->roulette.prizeIdx = -1;
  1459. //SG map and mob read [Komurka]
  1460. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1461. uint16 j;
  1462. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1463. sd->feel_map[i].index = j;
  1464. sd->feel_map[i].m = map_mapindex2mapid(j);
  1465. } else {
  1466. sd->feel_map[i].index = 0;
  1467. sd->feel_map[i].m = -1;
  1468. }
  1469. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1470. }
  1471. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1472. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1473. sd->cloneskill_idx = skill_get_index(skid);
  1474. if (sd->cloneskill_idx > 0) {
  1475. sd->status.skill[sd->cloneskill_idx].id = skid;
  1476. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1477. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1478. sd->status.skill[sd->cloneskill_idx].lv = i;
  1479. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1480. }
  1481. }
  1482. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1483. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1484. sd->reproduceskill_idx = skill_get_index(skid);
  1485. if (sd->reproduceskill_idx > 0) {
  1486. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1487. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1488. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1489. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1490. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1491. }
  1492. }
  1493. //Weird... maybe registries were reloaded?
  1494. if (sd->state.active)
  1495. return;
  1496. sd->state.active = 1;
  1497. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1498. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1499. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1500. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1501. if (sd->status.party_id)
  1502. party_member_joined(sd);
  1503. if (sd->status.guild_id)
  1504. guild_member_joined(sd);
  1505. if( sd->status.clan_id )
  1506. clan_member_joined(sd);
  1507. // pet
  1508. if (sd->status.pet_id > 0)
  1509. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1510. // Homunculus [albator]
  1511. if( sd->status.hom_id > 0 )
  1512. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1513. if( sd->status.mer_id > 0 )
  1514. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1515. if( sd->status.ele_id > 0 )
  1516. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1517. map_addiddb(&sd->bl);
  1518. map_delnickdb(sd->status.char_id, sd->status.name);
  1519. if (!chrif_auth_finished(sd))
  1520. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1521. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1522. chrif_bsdata_request(sd->status.char_id);
  1523. #ifdef VIP_ENABLE
  1524. sd->vip.time = 0;
  1525. sd->vip.enabled = 0;
  1526. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1527. #endif
  1528. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1529. intif_request_questlog(sd);
  1530. if (battle_config.feature_achievement) {
  1531. sd->achievement_data.total_score = 0;
  1532. sd->achievement_data.level = 0;
  1533. sd->achievement_data.save = false;
  1534. sd->achievement_data.count = 0;
  1535. sd->achievement_data.incompleteCount = 0;
  1536. sd->achievement_data.achievements = NULL;
  1537. intif_request_achievements(sd->status.char_id);
  1538. }
  1539. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1540. sd->state.connect_new = 1;
  1541. clif_parse_LoadEndAck(sd->fd, sd);
  1542. if( pc_isinvisible( sd ) ){
  1543. sd->vd.class_ = JT_INVISIBLE;
  1544. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1545. // decrement the number of pvp players on the map
  1546. map_getmapdata( sd->bl.m )->users_pvp--;
  1547. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1548. // unregister the player for ranking
  1549. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1550. sd->pvp_timer = INVALID_TIMER;
  1551. }
  1552. clif_changeoption( &sd->bl );
  1553. }
  1554. }
  1555. channel_autojoin(sd);
  1556. }
  1557. static int pc_calc_skillpoint(struct map_session_data* sd)
  1558. {
  1559. uint16 i, skill_point = 0;
  1560. nullpo_ret(sd);
  1561. for(i = 1; i < MAX_SKILL; i++) {
  1562. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1563. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1564. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1565. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1566. )
  1567. {
  1568. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1569. skill_point += sd->status.skill[i].lv;
  1570. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1571. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1572. }
  1573. }
  1574. }
  1575. return skill_point;
  1576. }
  1577. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1578. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1579. return false;
  1580. /**
  1581. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1582. * skill tree and since they have no icons they'll give resource errors
  1583. * Get ALL skills except npc/guild ones. [Skotlex]
  1584. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1585. **/
  1586. for (const auto &skill : skill_db) {
  1587. uint16 skill_id = skill.second->nameid;
  1588. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1589. continue;
  1590. switch (skill_id) {
  1591. case SM_SELFPROVOKE:
  1592. case AB_DUPLELIGHT_MELEE:
  1593. case AB_DUPLELIGHT_MAGIC:
  1594. case WL_CHAINLIGHTNING_ATK:
  1595. case WL_TETRAVORTEX_FIRE:
  1596. case WL_TETRAVORTEX_WATER:
  1597. case WL_TETRAVORTEX_WIND:
  1598. case WL_TETRAVORTEX_GROUND:
  1599. case WL_SUMMON_ATK_FIRE:
  1600. case WL_SUMMON_ATK_WIND:
  1601. case WL_SUMMON_ATK_WATER:
  1602. case WL_SUMMON_ATK_GROUND:
  1603. case LG_OVERBRAND_BRANDISH:
  1604. case LG_OVERBRAND_PLUSATK:
  1605. case WM_SEVERE_RAINSTORM_MELEE:
  1606. case RL_R_TRIP_PLUSATK:
  1607. case SG_DEVIL:
  1608. case MO_TRIPLEATTACK:
  1609. case RG_SNATCHER:
  1610. continue;
  1611. default:
  1612. {
  1613. uint8 lv = (uint8)skill_get_max(skill_id);
  1614. if (lv > 0) {
  1615. uint16 idx = skill_get_index(skill_id);
  1616. sd->status.skill[idx].id = skill_id;
  1617. if (addlv)
  1618. sd->status.skill[idx].lv = lv;
  1619. }
  1620. }
  1621. break;
  1622. }
  1623. }
  1624. return true;
  1625. }
  1626. /*==========================================
  1627. * Calculation of skill level.
  1628. * @param sd
  1629. *------------------------------------------*/
  1630. void pc_calc_skilltree(struct map_session_data *sd)
  1631. {
  1632. nullpo_retv(sd);
  1633. int job = pc_calc_skilltree_normalize_job(sd);
  1634. int class_ = pc_mapid2jobid(job, sd->status.sex);
  1635. if( class_ == -1 )
  1636. { //Unable to normalize job??
  1637. 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);
  1638. return;
  1639. }
  1640. class_ = pc_class2idx(class_);
  1641. for (const auto &skill : skill_db) {
  1642. uint16 skill_id = skill.second->nameid;
  1643. uint16 idx = skill_get_index(skill_id);
  1644. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1645. sd->status.skill[idx].id = 0; //First clear skills.
  1646. /* permanent skills that must be re-checked */
  1647. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  1648. if (skill_id == 0) {
  1649. sd->status.skill[idx].id = 0;
  1650. sd->status.skill[idx].lv = 0;
  1651. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1652. continue;
  1653. }
  1654. switch (skill_id) {
  1655. case NV_TRICKDEAD:
  1656. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1657. sd->status.skill[idx].id = 0;
  1658. sd->status.skill[idx].lv = 0;
  1659. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1660. }
  1661. break;
  1662. }
  1663. }
  1664. }
  1665. for (const auto &skill : skill_db) {
  1666. uint16 skill_id = skill.second->nameid;
  1667. uint16 idx = skill_get_index(skill_id);
  1668. // Restore original level of skills after deleting earned skills.
  1669. 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 ) {
  1670. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1671. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1672. }
  1673. //Enable Bard/Dancer spirit linked skills.
  1674. if (skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1675. continue;
  1676. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1677. //Link Dancer skills to bard.
  1678. if( sd->status.sex ) {
  1679. if( sd->status.skill[idx - 8].lv < 10 )
  1680. continue;
  1681. sd->status.skill[idx].id = skill_id;
  1682. sd->status.skill[idx].lv = sd->status.skill[idx - 8].lv; // Set the level to the same as the linking skill
  1683. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1684. }
  1685. //Link Bard skills to dancer.
  1686. else {
  1687. if( sd->status.skill[idx].lv < 10 )
  1688. continue;
  1689. sd->status.skill[idx - 8].id = skill_id - 8;
  1690. sd->status.skill[idx - 8].lv = sd->status.skill[idx].lv; // Set the level to the same as the linking skill
  1691. sd->status.skill[idx - 8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1692. }
  1693. }
  1694. }
  1695. // Removes Taekwon Ranker skill bonus
  1696. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1697. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1698. for (uint16 i = 0; i < MAX_SKILL_TREE; i++) {
  1699. uint16 sk_id = skill_tree[c_][i].skill_id;
  1700. uint16 sk_idx = 0;
  1701. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1702. continue;
  1703. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1704. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1705. continue;
  1706. sd->status.skill[sk_idx].id = 0;
  1707. }
  1708. }
  1709. }
  1710. // Grant all skills
  1711. pc_grant_allskills(sd, false);
  1712. int flag;
  1713. do {
  1714. uint16 skid = 0;
  1715. flag = 0;
  1716. for (uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++) {
  1717. bool fail = false;
  1718. uint16 sk_idx = skill_get_index(skid);
  1719. if (sd->status.skill[sk_idx].id)
  1720. continue; //Skill already known.
  1721. if (!battle_config.skillfree) {
  1722. // Checking required skills
  1723. for(uint8 j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1724. uint16 sk_need_id = skill_tree[class_][i].need[j].skill_id;
  1725. uint16 sk_need_idx = 0;
  1726. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1727. short sk_need = sk_need_id;
  1728. 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)
  1729. sk_need = 0; //Not learned.
  1730. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1731. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1732. else
  1733. sk_need = pc_checkskill(sd,sk_need_id);
  1734. if (sk_need < skill_tree[class_][i].need[j].skill_lv) {
  1735. fail = true;
  1736. break;
  1737. }
  1738. }
  1739. }
  1740. if (sd->status.base_level < skill_tree[class_][i].baselv) { //We need to get the actual class in this case
  1741. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1742. c_ = pc_class2idx(c_);
  1743. if (class_ == c_ || (class_ != c_ && sd->status.base_level < skill_tree[class_][i].baselv))
  1744. fail = true; // base level requirement wasn't satisfied
  1745. }
  1746. if (sd->status.job_level < skill_tree[class_][i].joblv) { //We need to get the actual class in this case
  1747. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1748. c_ = pc_class2idx(c_);
  1749. if (class_ == c_ || (class_ != c_ && sd->status.job_level < skill_tree[class_][i].joblv))
  1750. fail = true; // job level requirement wasn't satisfied
  1751. }
  1752. }
  1753. if (!fail) {
  1754. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1755. if (!sd->status.skill[sk_idx].lv && (
  1756. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1757. skill->inf2[INF2_ISWEDDING] ||
  1758. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1759. ))
  1760. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1761. sd->status.skill[sk_idx].id = skid;
  1762. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1763. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1764. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1765. }
  1766. flag = 1; // skill list has changed, perform another pass
  1767. }
  1768. }
  1769. } while(flag);
  1770. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1771. uint16 skid = 0;
  1772. /* Taekwon Ranker Bonus Skill Tree
  1773. ============================================
  1774. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1775. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1776. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1777. for( uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++ ) {
  1778. uint16 sk_idx = skill_get_index(skid);
  1779. if (sk_idx == 0)
  1780. continue;
  1781. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  1782. continue; //Do not include Quest/Wedding skills.
  1783. if( sd->status.skill[sk_idx].id == 0 ) {
  1784. sd->status.skill[sk_idx].id = skid;
  1785. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1786. } else if( skid != NV_BASIC )
  1787. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1788. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1789. }
  1790. }
  1791. }
  1792. //Checks if you can learn a new skill after having leveled up a skill.
  1793. static void pc_check_skilltree(struct map_session_data *sd)
  1794. {
  1795. int i, flag = 0;
  1796. int c = 0;
  1797. if (battle_config.skillfree)
  1798. return; //Function serves no purpose if this is set
  1799. i = pc_calc_skilltree_normalize_job(sd);
  1800. c = pc_mapid2jobid(i, sd->status.sex);
  1801. if (c == -1) { //Unable to normalize job??
  1802. 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);
  1803. return;
  1804. }
  1805. c = pc_class2idx(c);
  1806. do {
  1807. uint16 skid = 0;
  1808. flag = 0;
  1809. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1810. uint16 sk_idx = skill_get_index(skid);
  1811. bool fail = false;
  1812. uint8 j = 0;
  1813. if (sd->status.skill[sk_idx].id) //Already learned
  1814. continue;
  1815. // Checking required skills
  1816. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1817. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1818. uint16 sk_need_idx = 0;
  1819. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1820. short sk_need = sk_need_id;
  1821. 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)
  1822. sk_need = 0; //Not learned.
  1823. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1824. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1825. else
  1826. sk_need = pc_checkskill(sd,sk_need_id);
  1827. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1828. fail = true;
  1829. break;
  1830. }
  1831. }
  1832. }
  1833. if( fail )
  1834. continue;
  1835. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1836. continue;
  1837. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1838. if( !sd->status.skill[sk_idx].lv && (
  1839. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1840. skill->inf2[INF2_ISWEDDING] ||
  1841. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1842. ) )
  1843. continue; //Cannot be learned via normal means.
  1844. sd->status.skill[sk_idx].id = skid;
  1845. flag = 1;
  1846. }
  1847. } while(flag);
  1848. }
  1849. // Make sure all the skills are in the correct condition
  1850. // before persisting to the backend.. [MouseJstr]
  1851. void pc_clean_skilltree(struct map_session_data *sd)
  1852. {
  1853. uint16 i;
  1854. for (i = 0; i < MAX_SKILL; i++){
  1855. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1856. sd->status.skill[i].id = 0;
  1857. sd->status.skill[i].lv = 0;
  1858. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1859. }
  1860. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1861. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1862. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1863. }
  1864. }
  1865. }
  1866. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1867. {
  1868. int skill_point, novice_skills;
  1869. int c = sd->class_;
  1870. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1871. return c;
  1872. skill_point = pc_calc_skillpoint(sd);
  1873. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1874. // limit 1st class and above to novice job levels
  1875. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1876. {
  1877. c = MAPID_NOVICE;
  1878. }
  1879. // limit 2nd class and above to first class job levels (super novices are exempt)
  1880. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1881. {
  1882. // regenerate change_level_2nd
  1883. if (!sd->change_level_2nd)
  1884. {
  1885. if (sd->class_&JOBL_THIRD)
  1886. {
  1887. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1888. if (!sd->change_level_3rd)
  1889. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1890. else
  1891. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1892. - (sd->status.job_level - 1)
  1893. - (sd->change_level_3rd - 1)
  1894. - novice_skills;
  1895. }
  1896. else
  1897. {
  1898. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1899. - (sd->status.job_level - 1)
  1900. - novice_skills;
  1901. }
  1902. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1903. }
  1904. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1905. {
  1906. c &= MAPID_BASEMASK;
  1907. }
  1908. // limit 3rd class to 2nd class/trans job levels
  1909. else if(sd->class_&JOBL_THIRD)
  1910. {
  1911. // regenerate change_level_3rd
  1912. if (!sd->change_level_3rd)
  1913. {
  1914. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1915. - (sd->status.job_level - 1)
  1916. - (sd->change_level_2nd - 1)
  1917. - novice_skills;
  1918. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1919. }
  1920. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1921. c &= MAPID_UPPERMASK;
  1922. }
  1923. }
  1924. // restore non-limiting flags
  1925. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1926. return c;
  1927. }
  1928. /*==========================================
  1929. * Updates the weight status
  1930. *------------------------------------------
  1931. * 1: overweight 50% for pre-renewal and 70% for renewal
  1932. * 2: overweight 90%
  1933. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1934. */
  1935. void pc_updateweightstatus(struct map_session_data *sd)
  1936. {
  1937. int old_overweight;
  1938. int new_overweight;
  1939. nullpo_retv(sd);
  1940. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1941. #ifdef RENEWAL
  1942. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1943. #else
  1944. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1945. #endif
  1946. if( old_overweight == new_overweight )
  1947. return; // no change
  1948. // stop old status change
  1949. if( old_overweight == 1 )
  1950. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1951. else if( old_overweight == 2 )
  1952. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1953. // start new status change
  1954. if( new_overweight == 1 )
  1955. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1956. else if( new_overweight == 2 )
  1957. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1958. // update overweight status
  1959. sd->regen.state.overweight = new_overweight;
  1960. }
  1961. int pc_disguise(struct map_session_data *sd, int class_)
  1962. {
  1963. if (!class_ && !sd->disguise)
  1964. return 0;
  1965. if (class_ && sd->disguise == class_)
  1966. return 0;
  1967. if(pc_isinvisible(sd))
  1968. { //Character is invisible. Stealth class-change. [Skotlex]
  1969. sd->disguise = class_; //viewdata is set on uncloaking.
  1970. return 2;
  1971. }
  1972. if (sd->bl.prev != NULL) {
  1973. pc_stop_walking(sd, 0);
  1974. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1975. }
  1976. if (!class_) {
  1977. sd->disguise = 0;
  1978. class_ = sd->status.class_;
  1979. } else
  1980. sd->disguise=class_;
  1981. status_set_viewdata(&sd->bl, class_);
  1982. clif_changeoption(&sd->bl);
  1983. if (sd->bl.prev != NULL) {
  1984. clif_spawn(&sd->bl);
  1985. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1986. { //It seems the cart info is lost on undisguise.
  1987. clif_cartlist(sd);
  1988. clif_updatestatus(sd,SP_CARTINFO);
  1989. }
  1990. if (sd->chatID) {
  1991. struct chat_data* cd;
  1992. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1993. clif_dispchat(cd,0);
  1994. }
  1995. }
  1996. return 1;
  1997. }
  1998. /// Show error message
  1999. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2000. /// Check for valid Element, break & show error message if invalid Element
  2001. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2002. /// Check for valid Race, break & show error message if invalid Race
  2003. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2004. /// Check for valid Race2, break & show error message if invalid Race2
  2005. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2006. /// Check for valid Class, break & show error message if invalid Class
  2007. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2008. /// Check for valid Size, break & show error message if invalid Size
  2009. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2010. /// Check for valid SC, break & show error message if invalid SC
  2011. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2012. /**
  2013. * Add auto spell bonus for player while attacking/attacked
  2014. * @param spell: Spell array
  2015. * @param id: Skill to cast
  2016. * @param lv: Skill level
  2017. * @param rate: Success chance
  2018. * @param flag: Battle flag
  2019. * @param card_id: Used to prevent card stacking
  2020. */
  2021. static void pc_bonus_autospell(std::vector<s_autospell> &spell, short id, short lv, short rate, short flag, unsigned short card_id)
  2022. {
  2023. if (spell.size() == MAX_PC_BONUS) {
  2024. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2025. return;
  2026. }
  2027. if (!rate)
  2028. return;
  2029. if (!(flag&BF_RANGEMASK))
  2030. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2031. if (!(flag&BF_WEAPONMASK))
  2032. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2033. if (!(flag&BF_SKILLMASK)) {
  2034. if (flag&(BF_MAGIC | BF_MISC))
  2035. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2036. if (flag&BF_WEAPON)
  2037. flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2038. }
  2039. for (auto &it : spell) {
  2040. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.flag == flag) {
  2041. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2042. return;
  2043. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2044. return;
  2045. }
  2046. }
  2047. struct s_autospell entry = {};
  2048. if (rate < -10000 || rate > 10000)
  2049. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2050. entry.id = id;
  2051. entry.lv = lv;
  2052. entry.rate = cap_value(rate, -10000, 10000);
  2053. entry.flag = flag;
  2054. entry.card_id = card_id;
  2055. spell.push_back(entry);
  2056. }
  2057. /**
  2058. * Add auto spell bonus for player while using skills
  2059. * @param spell: Spell array
  2060. * @param src_skill: Trigger skill
  2061. * @param id: Support or target type
  2062. * @param lv: Skill level
  2063. * @param rate: Success chance
  2064. * @param card_id: Used to prevent card stacking
  2065. */
  2066. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, short src_skill, short id, short lv, short rate, unsigned short card_id)
  2067. {
  2068. if (spell.size() == MAX_PC_BONUS) {
  2069. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2070. return;
  2071. }
  2072. if (!rate)
  2073. return;
  2074. struct s_autospell entry = {};
  2075. if (rate < -10000 || rate > 10000)
  2076. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2077. entry.flag = src_skill;
  2078. entry.id = id;
  2079. entry.lv = lv;
  2080. entry.rate = cap_value(rate, -10000, 10000);
  2081. entry.card_id = card_id;
  2082. spell.push_back(entry);
  2083. }
  2084. /**
  2085. * Add inflict effect bonus for player while attacking/attacked
  2086. * @param effect: Effect array
  2087. * @param sc: SC/Effect type
  2088. * @param rate: Success chance
  2089. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2090. * @param flag: Target flag
  2091. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2092. */
  2093. 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)
  2094. {
  2095. if (effect.size() == MAX_PC_BONUS) {
  2096. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2097. return;
  2098. }
  2099. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2100. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2101. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2102. flag |= ATF_TARGET; //Default target: enemy.
  2103. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2104. flag |= ATF_WEAPON; //Default type: weapon.
  2105. if (!duration)
  2106. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2107. for (auto &it : effect) {
  2108. if (it.sc == sc && it.flag == flag) {
  2109. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2110. it.arrow_rate += arrow_rate;
  2111. it.duration = umax(it.duration, duration);
  2112. return;
  2113. }
  2114. }
  2115. struct s_addeffect entry = {};
  2116. if (rate < -10000 || rate > 10000)
  2117. ShowWarning("pc_bonus_addeff: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2118. entry.sc = sc;
  2119. entry.rate = cap_value(rate, -10000, 10000);
  2120. entry.arrow_rate = arrow_rate;
  2121. entry.flag = flag;
  2122. entry.duration = duration;
  2123. effect.push_back(entry);
  2124. }
  2125. /**
  2126. * Add inflict effect bonus for player while attacking using skill
  2127. * @param effect: Effect array
  2128. * @param sc: SC/Effect type
  2129. * @param rate: Success chance
  2130. * @param skill_id: Skill to cast
  2131. * @param target: Target type
  2132. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2133. */
  2134. 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)
  2135. {
  2136. if (effect.size() == MAX_PC_BONUS) {
  2137. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2138. return;
  2139. }
  2140. if (!duration)
  2141. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2142. for (auto &it : effect) {
  2143. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2144. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2145. it.duration = umax(it.duration, duration);
  2146. return;
  2147. }
  2148. }
  2149. struct s_addeffectonskill entry = {};
  2150. if (rate < -10000 || rate > 10000)
  2151. ShowWarning("pc_bonus_addeff_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2152. entry.sc = sc;
  2153. entry.rate = cap_value(rate, -10000, 10000);
  2154. entry.skill_id = skill_id;
  2155. entry.target = target;
  2156. entry.duration = duration;
  2157. effect.push_back(entry);
  2158. }
  2159. /**
  2160. * Adjust/add drop rate modifier for player
  2161. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2162. * @param nameid: item id that will be dropped
  2163. * @param group: group id
  2164. * @param class_: target class
  2165. * @param race: target race. if < 0, means monster_id
  2166. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2167. */
  2168. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, unsigned short nameid, uint16 group, int class_, short race, int rate)
  2169. {
  2170. if (!nameid && !group) {
  2171. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2172. return;
  2173. }
  2174. if (nameid && !itemdb_exists(nameid)) {
  2175. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  2176. return;
  2177. }
  2178. if (group && !itemdb_group_exists(group)) {
  2179. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2180. return;
  2181. }
  2182. if (drop.size() == MAX_PC_BONUS) {
  2183. 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);
  2184. return;
  2185. }
  2186. //Apply config rate adjustment settings.
  2187. if (rate >= 0) { //Absolute drop.
  2188. if (battle_config.item_rate_adddrop != 100)
  2189. rate = rate*battle_config.item_rate_adddrop/100;
  2190. if (rate < battle_config.item_drop_adddrop_min)
  2191. rate = battle_config.item_drop_adddrop_min;
  2192. else if (rate > battle_config.item_drop_adddrop_max)
  2193. rate = battle_config.item_drop_adddrop_max;
  2194. } else { //Relative drop, max/min limits are applied at drop time.
  2195. if (battle_config.item_rate_adddrop != 100)
  2196. rate = rate*battle_config.item_rate_adddrop/100;
  2197. if (rate > -1)
  2198. rate = -1;
  2199. }
  2200. for (auto &it : drop) {
  2201. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2202. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2203. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2204. return;
  2205. }
  2206. }
  2207. struct s_add_drop entry = {};
  2208. if (rate < -10000 || rate > 10000)
  2209. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2210. entry.nameid = nameid;
  2211. entry.group = group;
  2212. entry.race = race;
  2213. entry.class_ = class_;
  2214. entry.rate = cap_value(rate, -10000, 10000);
  2215. drop.push_back(entry);
  2216. }
  2217. /**
  2218. * Add autobonus to player when attacking/attacked
  2219. * @param bonus: Bonus array
  2220. * @param script: Script to execute
  2221. * @param rate: Success chance
  2222. * @param dur: Duration
  2223. * @param flag: Battle flag/skill
  2224. * @param other_script: Secondary script to execute
  2225. * @param pos: Item equip position
  2226. * @param onskill: Skill used to trigger autobonus
  2227. * @return True on success or false otherwise
  2228. */
  2229. 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)
  2230. {
  2231. if (bonus.size() == MAX_PC_BONUS) {
  2232. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2233. return false;
  2234. }
  2235. if (!onskill) {
  2236. if (!(flag&BF_RANGEMASK))
  2237. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2238. if (!(flag&BF_WEAPONMASK))
  2239. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2240. if (!(flag&BF_SKILLMASK)) {
  2241. if (flag&(BF_MAGIC | BF_MISC))
  2242. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2243. if (flag&BF_WEAPON)
  2244. flag |= BF_NORMAL | BF_SKILL;
  2245. }
  2246. }
  2247. struct s_autobonus entry = {};
  2248. if (rate < -10000 || rate > 10000)
  2249. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2250. entry.rate = cap_value(rate, -10000, 10000);
  2251. entry.duration = dur;
  2252. entry.active = INVALID_TIMER;
  2253. entry.atk_type = flag;
  2254. entry.pos = pos;
  2255. entry.bonus_script = aStrdup(script);
  2256. entry.other_script = (other_script ? aStrdup(other_script) : NULL);
  2257. bonus.push_back(entry);
  2258. return true;
  2259. }
  2260. /**
  2261. * Remove an autobonus from player
  2262. * @param sd: Player data
  2263. * @param bonus: Autobonus array
  2264. * @param restore: Run script on clearing or not
  2265. */
  2266. void pc_delautobonus(struct map_session_data* sd, std::vector<s_autobonus> &bonus, bool restore)
  2267. {
  2268. nullpo_retv(sd);
  2269. std::vector<s_autobonus>::iterator it = bonus.begin();
  2270. while( it != bonus.end() ){
  2271. s_autobonus b = *it;
  2272. if (b.active != INVALID_TIMER) {
  2273. if (restore && (sd->state.autobonus&b.pos) == b.pos) {
  2274. if (b.bonus_script) {
  2275. unsigned int equip_pos_idx = 0;
  2276. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2277. for (uint8 j = 0; j < EQI_MAX; j++) {
  2278. if (sd->equip_index[j] >= 0)
  2279. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2280. }
  2281. if ((equip_pos_idx&b.pos) == b.pos)
  2282. script_run_autobonus(b.bonus_script, sd, b.pos);
  2283. }
  2284. it++;
  2285. continue;
  2286. } else { // Logout / Unequipped an item with an activated bonus
  2287. delete_timer(b.active, pc_endautobonus);
  2288. b.active = INVALID_TIMER;
  2289. }
  2290. }
  2291. if (b.bonus_script)
  2292. aFree(b.bonus_script);
  2293. if (b.other_script)
  2294. aFree(b.other_script);
  2295. it = bonus.erase(it);
  2296. }
  2297. }
  2298. /**
  2299. * Execute autobonus on player
  2300. * @param sd: Player data
  2301. * @param autobonus: Autobonus to run
  2302. */
  2303. void pc_exeautobonus(struct map_session_data *sd, std::vector<s_autobonus> *bonus, struct s_autobonus *autobonus)
  2304. {
  2305. nullpo_retv(sd);
  2306. nullpo_retv(autobonus);
  2307. if (autobonus->active != INVALID_TIMER)
  2308. delete_timer(autobonus->active, pc_endautobonus);
  2309. if( autobonus->other_script )
  2310. {
  2311. int j;
  2312. unsigned int equip_pos_idx = 0;
  2313. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2314. for(j = 0; j < EQI_MAX; j++) {
  2315. if(sd->equip_index[j] >= 0)
  2316. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2317. }
  2318. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2319. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2320. }
  2321. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)bonus);
  2322. sd->state.autobonus |= autobonus->pos;
  2323. status_calc_pc(sd,SCO_NONE);
  2324. }
  2325. /**
  2326. * Remove autobonus timer from player
  2327. */
  2328. TIMER_FUNC(pc_endautobonus){
  2329. struct map_session_data *sd = map_id2sd(id);
  2330. std::vector<s_autobonus> *bonus = (std::vector<s_autobonus> *)data;
  2331. nullpo_ret(sd);
  2332. nullpo_ret(bonus);
  2333. for( struct s_autobonus& autobonus : *bonus ){
  2334. if( autobonus.active == tid ){
  2335. autobonus.active = INVALID_TIMER;
  2336. sd->state.autobonus &= ~autobonus.pos;
  2337. break;
  2338. }
  2339. }
  2340. status_calc_pc(sd,SCO_NONE);
  2341. return 0;
  2342. }
  2343. /**
  2344. * Add element bonus to player when attacking
  2345. * @param sd: Player data
  2346. * @param ele: Element to adjust
  2347. * @param rate: Success chance
  2348. * @param flag: Battle flag
  2349. */
  2350. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2351. {
  2352. nullpo_retv(sd);
  2353. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2354. if (wd->addele2.size() == MAX_PC_BONUS) {
  2355. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2356. return;
  2357. }
  2358. if (!(flag&BF_RANGEMASK))
  2359. flag |= BF_SHORT | BF_LONG;
  2360. if (!(flag&BF_WEAPONMASK))
  2361. flag |= BF_WEAPON;
  2362. if (!(flag&BF_SKILLMASK)) {
  2363. if (flag&(BF_MAGIC | BF_MISC))
  2364. flag |= BF_SKILL;
  2365. if (flag&BF_WEAPON)
  2366. flag |= BF_NORMAL | BF_SKILL;
  2367. }
  2368. for (auto &it : wd->addele2) {
  2369. if (it.ele == ele && it.flag == flag) {
  2370. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2371. return;
  2372. }
  2373. }
  2374. struct s_addele2 entry = {};
  2375. if (rate < -10000 || rate > 10000)
  2376. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2377. entry.ele = ele;
  2378. entry.rate = cap_value(rate, -10000, 10000);
  2379. entry.flag = flag;
  2380. wd->addele2.push_back(entry);
  2381. }
  2382. /**
  2383. * Reduce element bonus to player when attacking
  2384. * @param sd: Player data
  2385. * @param ele: Element to adjust
  2386. * @param rate: Success chance
  2387. * @param flag: Battle flag
  2388. */
  2389. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2390. {
  2391. nullpo_retv(sd);
  2392. if (sd->subele2.size() == MAX_PC_BONUS) {
  2393. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2394. return;
  2395. }
  2396. if (!(flag&BF_RANGEMASK))
  2397. flag |= BF_SHORT | BF_LONG;
  2398. if (!(flag&BF_WEAPONMASK))
  2399. flag |= BF_WEAPON;
  2400. if (!(flag&BF_SKILLMASK)) {
  2401. if (flag&(BF_MAGIC | BF_MISC))
  2402. flag |= BF_SKILL;
  2403. if (flag&BF_WEAPON)
  2404. flag |= BF_NORMAL | BF_SKILL;
  2405. }
  2406. for (auto &it : sd->subele2) {
  2407. if (it.ele == ele && it.flag == flag) {
  2408. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2409. return;
  2410. }
  2411. }
  2412. struct s_addele2 entry = {};
  2413. if (rate < -10000 || rate > 10000)
  2414. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2415. entry.ele = ele;
  2416. entry.rate = cap_value(rate, -10000, 10000);
  2417. entry.flag = flag;
  2418. sd->subele2.push_back(entry);
  2419. }
  2420. /**
  2421. * General item bonus for player
  2422. * @param bonus: Bonus array
  2423. * @param id: Key
  2424. * @param val: Value
  2425. * @param cap_rate: If Value is a rate value that needs to be capped
  2426. */
  2427. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2428. {
  2429. for (auto &it : bonus) {
  2430. if (it.id == id) {
  2431. if (cap_rate)
  2432. it.val = cap_value(it.val + val, -10000, 10000);
  2433. else
  2434. it.val += val;
  2435. return;
  2436. }
  2437. }
  2438. struct s_item_bonus entry = {};
  2439. if (cap_rate && (val < -10000 || val > 10000)) {
  2440. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2441. val = cap_value(val, -10000, 10000);
  2442. }
  2443. entry.id = id;
  2444. entry.val = val;
  2445. bonus.push_back(entry);
  2446. }
  2447. /**
  2448. * Remove HP/SP to player when attacking
  2449. * @param bonus: Bonus array
  2450. * @param rate: Success chance
  2451. * @param per: Percentage of HP/SP to vanish
  2452. * @param flag: Battle flag
  2453. */
  2454. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2455. if (bonus.size() == MAX_PC_BONUS) {
  2456. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2457. return;
  2458. }
  2459. if (!(flag&BF_RANGEMASK))
  2460. flag |= BF_SHORT | BF_LONG;
  2461. if (!(flag&BF_WEAPONMASK))
  2462. flag |= BF_WEAPON;
  2463. if (!(flag&BF_SKILLMASK)) {
  2464. if (flag&(BF_MAGIC | BF_MISC))
  2465. flag |= BF_SKILL;
  2466. if (flag&BF_WEAPON)
  2467. flag |= BF_NORMAL | BF_SKILL;
  2468. }
  2469. for (auto &it : bonus) {
  2470. if (it.flag == flag) {
  2471. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2472. it.per += per;
  2473. return;
  2474. }
  2475. }
  2476. struct s_vanish_bonus entry = {};
  2477. if (rate < -10000 || rate > 10000)
  2478. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2479. entry.rate = cap_value(rate, -10000, 10000);
  2480. entry.per = per;
  2481. entry.flag = flag;
  2482. bonus.push_back(entry);
  2483. }
  2484. /*==========================================
  2485. * Add a bonus(type) to player sd
  2486. * format: bonus bBonusName,val;
  2487. * @param sd
  2488. * @param type Bonus type used by bBonusName
  2489. * @param val Value that usually for rate or fixed value
  2490. *------------------------------------------*/
  2491. void pc_bonus(struct map_session_data *sd,int type,int val)
  2492. {
  2493. struct status_data *status;
  2494. int bonus;
  2495. nullpo_retv(sd);
  2496. status = &sd->base_status;
  2497. switch(type){
  2498. case SP_STR:
  2499. case SP_AGI:
  2500. case SP_VIT:
  2501. case SP_INT:
  2502. case SP_DEX:
  2503. case SP_LUK:
  2504. if(sd->state.lr_flag != 2)
  2505. sd->param_bonus[type-SP_STR]+=val;
  2506. break;
  2507. case SP_ATK1:
  2508. if(!sd->state.lr_flag) {
  2509. bonus = status->rhw.atk + val;
  2510. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2511. }
  2512. else if(sd->state.lr_flag == 1) {
  2513. bonus = status->lhw.atk + val;
  2514. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2515. }
  2516. break;
  2517. case SP_ATK2:
  2518. if(!sd->state.lr_flag) {
  2519. bonus = status->rhw.atk2 + val;
  2520. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2521. }
  2522. else if(sd->state.lr_flag == 1) {
  2523. bonus = status->lhw.atk2 + val;
  2524. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2525. }
  2526. break;
  2527. case SP_BASE_ATK:
  2528. if(sd->state.lr_flag != 2) {
  2529. #ifdef RENEWAL
  2530. bonus = sd->bonus.eatk + val;
  2531. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2532. #else
  2533. bonus = status->batk + val;
  2534. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2535. #endif
  2536. }
  2537. break;
  2538. case SP_DEF1:
  2539. if(sd->state.lr_flag != 2) {
  2540. bonus = status->def + val;
  2541. #ifdef RENEWAL
  2542. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2543. #else
  2544. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2545. #endif
  2546. }
  2547. break;
  2548. case SP_DEF2:
  2549. if(sd->state.lr_flag != 2) {
  2550. bonus = status->def2 + val;
  2551. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2552. }
  2553. break;
  2554. case SP_MDEF1:
  2555. if(sd->state.lr_flag != 2) {
  2556. bonus = status->mdef + val;
  2557. #ifdef RENEWAL
  2558. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2559. #else
  2560. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2561. #endif
  2562. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2563. sd->bonus.shieldmdef += bonus;
  2564. }
  2565. }
  2566. break;
  2567. case SP_MDEF2:
  2568. if(sd->state.lr_flag != 2) {
  2569. bonus = status->mdef2 + val;
  2570. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2571. }
  2572. break;
  2573. case SP_HIT:
  2574. if(sd->state.lr_flag != 2) {
  2575. bonus = status->hit + val;
  2576. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2577. } else
  2578. sd->bonus.arrow_hit+=val;
  2579. break;
  2580. case SP_FLEE1:
  2581. if(sd->state.lr_flag != 2) {
  2582. bonus = status->flee + val;
  2583. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2584. }
  2585. break;
  2586. case SP_FLEE2:
  2587. if(sd->state.lr_flag != 2) {
  2588. bonus = status->flee2 + val*10;
  2589. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2590. }
  2591. break;
  2592. case SP_CRITICAL:
  2593. if(sd->state.lr_flag != 2) {
  2594. bonus = status->cri + val*10;
  2595. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2596. } else
  2597. sd->bonus.arrow_cri += val*10;
  2598. break;
  2599. case SP_ATKELE:
  2600. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2601. switch (sd->state.lr_flag)
  2602. {
  2603. case 2:
  2604. switch (sd->status.weapon) {
  2605. case W_BOW:
  2606. case W_REVOLVER:
  2607. case W_RIFLE:
  2608. case W_GATLING:
  2609. case W_SHOTGUN:
  2610. case W_GRENADE:
  2611. //Become weapon element.
  2612. status->rhw.ele=val;
  2613. break;
  2614. default: //Become arrow element.
  2615. sd->bonus.arrow_ele=val;
  2616. break;
  2617. }
  2618. break;
  2619. case 1:
  2620. status->lhw.ele=val;
  2621. break;
  2622. default:
  2623. status->rhw.ele=val;
  2624. break;
  2625. }
  2626. break;
  2627. case SP_DEFELE:
  2628. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2629. if(sd->state.lr_flag != 2)
  2630. status->def_ele=val;
  2631. break;
  2632. case SP_MAXHP:
  2633. if(sd->state.lr_flag == 2)
  2634. break;
  2635. sd->bonus.hp += val;
  2636. break;
  2637. case SP_MAXSP:
  2638. if(sd->state.lr_flag == 2)
  2639. break;
  2640. sd->bonus.sp += val;
  2641. break;
  2642. case SP_MAXHPRATE:
  2643. if(sd->state.lr_flag != 2)
  2644. sd->hprate+=val;
  2645. break;
  2646. case SP_MAXSPRATE:
  2647. if(sd->state.lr_flag != 2)
  2648. sd->sprate+=val;
  2649. break;
  2650. case SP_SPRATE:
  2651. if(sd->state.lr_flag != 2)
  2652. sd->dsprate+=val;
  2653. break;
  2654. case SP_ATTACKRANGE:
  2655. switch (sd->state.lr_flag) {
  2656. case 2:
  2657. switch (sd->status.weapon) {
  2658. case W_BOW:
  2659. case W_REVOLVER:
  2660. case W_RIFLE:
  2661. case W_GATLING:
  2662. case W_SHOTGUN:
  2663. case W_GRENADE:
  2664. status->rhw.range += val;
  2665. }
  2666. break;
  2667. case 1:
  2668. status->lhw.range += val;
  2669. break;
  2670. default:
  2671. status->rhw.range += val;
  2672. break;
  2673. }
  2674. break;
  2675. case SP_SPEED_RATE: //Non stackable increase
  2676. if(sd->state.lr_flag != 2)
  2677. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2678. break;
  2679. case SP_SPEED_ADDRATE: //Stackable increase
  2680. if(sd->state.lr_flag != 2)
  2681. sd->bonus.speed_add_rate -= val;
  2682. break;
  2683. case SP_ASPD: //Raw increase
  2684. if(sd->state.lr_flag != 2)
  2685. sd->bonus.aspd_add -= 10*val;
  2686. break;
  2687. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2688. if(sd->state.lr_flag != 2)
  2689. #ifndef RENEWAL_ASPD
  2690. status->aspd_rate -= 10*val;
  2691. #else
  2692. status->aspd_rate2 += val;
  2693. #endif
  2694. break;
  2695. case SP_HP_RECOV_RATE:
  2696. if(sd->state.lr_flag != 2)
  2697. sd->hprecov_rate += val;
  2698. break;
  2699. case SP_SP_RECOV_RATE:
  2700. if(sd->state.lr_flag != 2)
  2701. sd->sprecov_rate += val;
  2702. break;
  2703. case SP_CRITICAL_DEF:
  2704. if(sd->state.lr_flag != 2)
  2705. sd->bonus.critical_def += val;
  2706. break;
  2707. case SP_NEAR_ATK_DEF:
  2708. if(sd->state.lr_flag != 2)
  2709. sd->bonus.near_attack_def_rate += val;
  2710. break;
  2711. case SP_LONG_ATK_DEF:
  2712. if(sd->state.lr_flag != 2)
  2713. sd->bonus.long_attack_def_rate += val;
  2714. break;
  2715. case SP_DOUBLE_RATE:
  2716. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2717. sd->bonus.double_rate = val;
  2718. break;
  2719. case SP_DOUBLE_ADD_RATE:
  2720. if(sd->state.lr_flag == 0)
  2721. sd->bonus.double_add_rate += val;
  2722. break;
  2723. case SP_MATK_RATE:
  2724. if(sd->state.lr_flag != 2)
  2725. sd->matk_rate += val;
  2726. break;
  2727. case SP_IGNORE_DEF_ELE:
  2728. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2729. if(!sd->state.lr_flag)
  2730. sd->right_weapon.ignore_def_ele |= 1<<val;
  2731. else if(sd->state.lr_flag == 1)
  2732. sd->left_weapon.ignore_def_ele |= 1<<val;
  2733. break;
  2734. case SP_IGNORE_DEF_RACE:
  2735. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2736. if(!sd->state.lr_flag)
  2737. sd->right_weapon.ignore_def_race |= 1<<val;
  2738. else if(sd->state.lr_flag == 1)
  2739. sd->left_weapon.ignore_def_race |= 1<<val;
  2740. break;
  2741. case SP_IGNORE_DEF_CLASS:
  2742. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2743. if(!sd->state.lr_flag)
  2744. sd->right_weapon.ignore_def_class |= 1<<val;
  2745. else if(sd->state.lr_flag == 1)
  2746. sd->left_weapon.ignore_def_class |= 1<<val;
  2747. break;
  2748. case SP_ATK_RATE:
  2749. if(sd->state.lr_flag != 2)
  2750. sd->bonus.atk_rate += val;
  2751. break;
  2752. case SP_MAGIC_ATK_DEF:
  2753. if(sd->state.lr_flag != 2)
  2754. sd->bonus.magic_def_rate += val;
  2755. break;
  2756. case SP_MISC_ATK_DEF:
  2757. if(sd->state.lr_flag != 2)
  2758. sd->bonus.misc_def_rate += val;
  2759. break;
  2760. case SP_IGNORE_MDEF_ELE:
  2761. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2762. if(sd->state.lr_flag != 2)
  2763. sd->bonus.ignore_mdef_ele |= 1<<val;
  2764. break;
  2765. case SP_IGNORE_MDEF_RACE:
  2766. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2767. if(sd->state.lr_flag != 2)
  2768. sd->bonus.ignore_mdef_race |= 1<<val;
  2769. break;
  2770. case SP_PERFECT_HIT_RATE:
  2771. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2772. sd->bonus.perfect_hit = val;
  2773. break;
  2774. case SP_PERFECT_HIT_ADD_RATE:
  2775. if(sd->state.lr_flag != 2)
  2776. sd->bonus.perfect_hit_add += val;
  2777. break;
  2778. case SP_CRITICAL_RATE:
  2779. if(sd->state.lr_flag != 2)
  2780. sd->critical_rate+=val;
  2781. break;
  2782. case SP_DEF_RATIO_ATK_ELE:
  2783. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2784. if(!sd->state.lr_flag)
  2785. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2786. else if(sd->state.lr_flag == 1)
  2787. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2788. break;
  2789. case SP_DEF_RATIO_ATK_RACE:
  2790. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2791. if(!sd->state.lr_flag)
  2792. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2793. else if(sd->state.lr_flag == 1)
  2794. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2795. break;
  2796. case SP_DEF_RATIO_ATK_CLASS:
  2797. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2798. if(!sd->state.lr_flag)
  2799. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2800. else if(sd->state.lr_flag == 1)
  2801. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2802. break;
  2803. case SP_HIT_RATE:
  2804. if(sd->state.lr_flag != 2)
  2805. sd->hit_rate += val;
  2806. break;
  2807. case SP_FLEE_RATE:
  2808. if(sd->state.lr_flag != 2)
  2809. sd->flee_rate += val;
  2810. break;
  2811. case SP_FLEE2_RATE:
  2812. if(sd->state.lr_flag != 2)
  2813. sd->flee2_rate += val;
  2814. break;
  2815. case SP_DEF_RATE:
  2816. if(sd->state.lr_flag != 2)
  2817. sd->def_rate += val;
  2818. break;
  2819. case SP_DEF2_RATE:
  2820. if(sd->state.lr_flag != 2)
  2821. sd->def2_rate += val;
  2822. break;
  2823. case SP_MDEF_RATE:
  2824. if(sd->state.lr_flag != 2)
  2825. sd->mdef_rate += val;
  2826. break;
  2827. case SP_MDEF2_RATE:
  2828. if(sd->state.lr_flag != 2)
  2829. sd->mdef2_rate += val;
  2830. break;
  2831. case SP_RESTART_FULL_RECOVER:
  2832. if(sd->state.lr_flag != 2)
  2833. sd->special_state.restart_full_recover = 1;
  2834. break;
  2835. case SP_NO_CASTCANCEL:
  2836. if(sd->state.lr_flag != 2)
  2837. sd->special_state.no_castcancel = 1;
  2838. break;
  2839. case SP_NO_CASTCANCEL2:
  2840. if(sd->state.lr_flag != 2)
  2841. sd->special_state.no_castcancel2 = 1;
  2842. break;
  2843. case SP_NO_SIZEFIX:
  2844. if(sd->state.lr_flag != 2)
  2845. sd->special_state.no_sizefix = 1;
  2846. break;
  2847. case SP_NO_MAGIC_DAMAGE:
  2848. if(sd->state.lr_flag == 2)
  2849. break;
  2850. val+= sd->special_state.no_magic_damage;
  2851. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2852. break;
  2853. case SP_NO_WEAPON_DAMAGE:
  2854. if(sd->state.lr_flag == 2)
  2855. break;
  2856. val+= sd->special_state.no_weapon_damage;
  2857. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2858. break;
  2859. case SP_NO_MISC_DAMAGE:
  2860. if(sd->state.lr_flag == 2)
  2861. break;
  2862. val+= sd->special_state.no_misc_damage;
  2863. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2864. break;
  2865. case SP_NO_GEMSTONE:
  2866. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2867. sd->special_state.no_gemstone = 1;
  2868. break;
  2869. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2870. if(sd->state.lr_flag != 2) {
  2871. sd->special_state.intravision = 1;
  2872. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2873. }
  2874. break;
  2875. case SP_NO_KNOCKBACK:
  2876. if(sd->state.lr_flag != 2)
  2877. sd->special_state.no_knockback = 1;
  2878. break;
  2879. case SP_SPLASH_RANGE:
  2880. if(sd->bonus.splash_range < val)
  2881. sd->bonus.splash_range = val;
  2882. break;
  2883. case SP_SPLASH_ADD_RANGE:
  2884. sd->bonus.splash_add_range += val;
  2885. break;
  2886. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2887. if(sd->state.lr_flag != 2)
  2888. sd->bonus.short_weapon_damage_return += val;
  2889. break;
  2890. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2891. if(sd->state.lr_flag != 2)
  2892. sd->bonus.long_weapon_damage_return += val;
  2893. break;
  2894. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2895. if(sd->state.lr_flag != 2)
  2896. sd->bonus.magic_damage_return += val;
  2897. break;
  2898. case SP_ALL_STATS: // [Valaris]
  2899. if(sd->state.lr_flag!=2) {
  2900. sd->param_bonus[SP_STR-SP_STR]+=val;
  2901. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2902. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2903. sd->param_bonus[SP_INT-SP_STR]+=val;
  2904. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2905. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2906. }
  2907. break;
  2908. case SP_AGI_VIT: // [Valaris]
  2909. if(sd->state.lr_flag!=2) {
  2910. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2911. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2912. }
  2913. break;
  2914. case SP_AGI_DEX_STR: // [Valaris]
  2915. if(sd->state.lr_flag!=2) {
  2916. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2917. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2918. sd->param_bonus[SP_STR-SP_STR]+=val;
  2919. }
  2920. break;
  2921. case SP_PERFECT_HIDE: // [Valaris]
  2922. if(sd->state.lr_flag!=2)
  2923. sd->special_state.perfect_hiding=1;
  2924. break;
  2925. case SP_UNBREAKABLE:
  2926. if(sd->state.lr_flag!=2)
  2927. sd->bonus.unbreakable += val;
  2928. break;
  2929. case SP_UNBREAKABLE_WEAPON:
  2930. if(sd->state.lr_flag != 2)
  2931. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2932. break;
  2933. case SP_UNBREAKABLE_ARMOR:
  2934. if(sd->state.lr_flag != 2)
  2935. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2936. break;
  2937. case SP_UNBREAKABLE_HELM:
  2938. if(sd->state.lr_flag != 2)
  2939. sd->bonus.unbreakable_equip |= EQP_HELM;
  2940. break;
  2941. case SP_UNBREAKABLE_SHIELD:
  2942. if(sd->state.lr_flag != 2)
  2943. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2944. break;
  2945. case SP_UNBREAKABLE_GARMENT:
  2946. if(sd->state.lr_flag != 2)
  2947. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2948. break;
  2949. case SP_UNBREAKABLE_SHOES:
  2950. if(sd->state.lr_flag != 2)
  2951. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2952. break;
  2953. case SP_CLASSCHANGE: // [Valaris]
  2954. if(sd->state.lr_flag !=2)
  2955. sd->bonus.classchange=val;
  2956. break;
  2957. case SP_LONG_ATK_RATE:
  2958. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2959. sd->bonus.long_attack_atk_rate+=val;
  2960. break;
  2961. case SP_BREAK_WEAPON_RATE:
  2962. if(sd->state.lr_flag != 2)
  2963. sd->bonus.break_weapon_rate+=val;
  2964. break;
  2965. case SP_BREAK_ARMOR_RATE:
  2966. if(sd->state.lr_flag != 2)
  2967. sd->bonus.break_armor_rate+=val;
  2968. break;
  2969. case SP_ADD_STEAL_RATE:
  2970. if(sd->state.lr_flag != 2)
  2971. sd->bonus.add_steal_rate+=val;
  2972. break;
  2973. case SP_DELAYRATE:
  2974. if(sd->state.lr_flag != 2)
  2975. sd->delayrate+=val;
  2976. break;
  2977. case SP_CRIT_ATK_RATE:
  2978. if(sd->state.lr_flag != 2)
  2979. sd->bonus.crit_atk_rate += val;
  2980. break;
  2981. case SP_NO_REGEN:
  2982. if(sd->state.lr_flag != 2)
  2983. sd->regen.state.block|=val;
  2984. break;
  2985. case SP_UNSTRIPABLE_WEAPON:
  2986. if(sd->state.lr_flag != 2)
  2987. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2988. break;
  2989. case SP_UNSTRIPABLE:
  2990. case SP_UNSTRIPABLE_ARMOR:
  2991. if(sd->state.lr_flag != 2)
  2992. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2993. break;
  2994. case SP_UNSTRIPABLE_HELM:
  2995. if(sd->state.lr_flag != 2)
  2996. sd->bonus.unstripable_equip |= EQP_HELM;
  2997. break;
  2998. case SP_UNSTRIPABLE_SHIELD:
  2999. if(sd->state.lr_flag != 2)
  3000. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3001. break;
  3002. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3003. if(!sd->state.lr_flag) {
  3004. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3005. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3006. } else if(sd->state.lr_flag == 1) {
  3007. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3008. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3009. }
  3010. break;
  3011. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3012. if(!sd->state.lr_flag) {
  3013. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3014. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3015. } else if(sd->state.lr_flag == 1) {
  3016. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3017. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3018. }
  3019. break;
  3020. case SP_SP_GAIN_VALUE:
  3021. if(!sd->state.lr_flag)
  3022. sd->bonus.sp_gain_value += val;
  3023. break;
  3024. case SP_HP_GAIN_VALUE:
  3025. if(!sd->state.lr_flag)
  3026. sd->bonus.hp_gain_value += val;
  3027. break;
  3028. case SP_LONG_SP_GAIN_VALUE:
  3029. if(!sd->state.lr_flag)
  3030. sd->bonus.long_sp_gain_value += val;
  3031. case SP_LONG_HP_GAIN_VALUE:
  3032. if(!sd->state.lr_flag)
  3033. sd->bonus.long_hp_gain_value += val;
  3034. break;
  3035. case SP_MAGIC_SP_GAIN_VALUE:
  3036. if(!sd->state.lr_flag)
  3037. sd->bonus.magic_sp_gain_value += val;
  3038. break;
  3039. case SP_MAGIC_HP_GAIN_VALUE:
  3040. if(!sd->state.lr_flag)
  3041. sd->bonus.magic_hp_gain_value += val;
  3042. break;
  3043. case SP_ADD_HEAL_RATE:
  3044. if(sd->state.lr_flag != 2)
  3045. sd->bonus.add_heal_rate += val;
  3046. break;
  3047. case SP_ADD_HEAL2_RATE:
  3048. if(sd->state.lr_flag != 2)
  3049. sd->bonus.add_heal2_rate += val;
  3050. break;
  3051. case SP_ADD_ITEM_HEAL_RATE:
  3052. if(sd->state.lr_flag != 2)
  3053. sd->bonus.itemhealrate2 += val;
  3054. break;
  3055. case SP_EMATK:
  3056. if(sd->state.lr_flag != 2)
  3057. sd->bonus.ematk += val;
  3058. break;
  3059. case SP_ADD_VARIABLECAST:
  3060. if (sd->state.lr_flag != 2)
  3061. sd->bonus.add_varcast += val;
  3062. break;
  3063. #ifdef RENEWAL_CAST
  3064. case SP_FIXCASTRATE:
  3065. if(sd->state.lr_flag != 2)
  3066. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3067. break;
  3068. case SP_ADD_FIXEDCAST:
  3069. if(sd->state.lr_flag != 2)
  3070. sd->bonus.add_fixcast += val;
  3071. break;
  3072. case SP_CASTRATE:
  3073. case SP_VARCASTRATE:
  3074. if(sd->state.lr_flag != 2)
  3075. sd->bonus.varcastrate -= val;
  3076. break;
  3077. #else
  3078. case SP_ADD_FIXEDCAST:
  3079. case SP_FIXCASTRATE:
  3080. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3081. break;
  3082. case SP_VARCASTRATE:
  3083. case SP_CASTRATE:
  3084. if(sd->state.lr_flag != 2)
  3085. sd->castrate += val;
  3086. break;
  3087. #endif
  3088. case SP_ADDMAXWEIGHT:
  3089. if (sd->state.lr_flag != 2)
  3090. sd->add_max_weight += val;
  3091. break;
  3092. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3093. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3094. break;
  3095. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3096. if (sd->state.lr_flag != 2)
  3097. sd->bonus.critical_rangeatk += val*10;
  3098. else
  3099. sd->bonus.arrow_cri += val*10;
  3100. break;
  3101. case SP_WEAPON_ATK_RATE:
  3102. if (sd->state.lr_flag != 2)
  3103. sd->bonus.weapon_atk_rate += val;
  3104. break;
  3105. case SP_WEAPON_MATK_RATE:
  3106. if (sd->state.lr_flag != 2)
  3107. sd->bonus.weapon_matk_rate += val;
  3108. break;
  3109. case SP_NO_MADO_FUEL:
  3110. if (sd->state.lr_flag != 2)
  3111. sd->special_state.no_mado_fuel = 1;
  3112. break;
  3113. case SP_NO_WALK_DELAY:
  3114. if (sd->state.lr_flag != 2)
  3115. sd->special_state.no_walk_delay = 1;
  3116. break;
  3117. default:
  3118. if (running_npc_stat_calc_event) {
  3119. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  3120. }
  3121. else if (current_equip_combo_pos > 0) {
  3122. 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);
  3123. }
  3124. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3125. 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);
  3126. }
  3127. else {
  3128. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3129. }
  3130. break;
  3131. }
  3132. }
  3133. /*==========================================
  3134. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3135. * format: bonus2 bBonusName,type2,val;
  3136. * @param sd
  3137. * @param type Bonus type used by bBonusName
  3138. * @param type2
  3139. * @param val Value that usually for rate or fixed value
  3140. *------------------------------------------*/
  3141. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3142. {
  3143. nullpo_retv(sd);
  3144. switch(type){
  3145. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3146. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3147. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3148. sd->right_weapon.addele[type2]+=val;
  3149. else if(sd->state.lr_flag == 1)
  3150. sd->left_weapon.addele[type2]+=val;
  3151. else if(sd->state.lr_flag == 2)
  3152. sd->arrow_addele[type2]+=val;
  3153. break;
  3154. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3155. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3156. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3157. sd->right_weapon.addrace[type2]+=val;
  3158. else if(sd->state.lr_flag == 1)
  3159. sd->left_weapon.addrace[type2]+=val;
  3160. else if(sd->state.lr_flag == 2)
  3161. sd->arrow_addrace[type2]+=val;
  3162. break;
  3163. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3164. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3165. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3166. sd->right_weapon.addclass[type2]+=val;
  3167. else if(sd->state.lr_flag == 1)
  3168. sd->left_weapon.addclass[type2]+=val;
  3169. else if(sd->state.lr_flag == 2)
  3170. sd->arrow_addclass[type2]+=val;
  3171. break;
  3172. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3173. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3174. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3175. sd->right_weapon.addsize[type2]+=val;
  3176. else if(sd->state.lr_flag == 1)
  3177. sd->left_weapon.addsize[type2]+=val;
  3178. else if(sd->state.lr_flag == 2)
  3179. sd->arrow_addsize[type2]+=val;
  3180. break;
  3181. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3182. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3183. if(sd->state.lr_flag != 2)
  3184. sd->subele_script[type2] += val;
  3185. break;
  3186. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3187. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3188. if(sd->state.lr_flag != 2)
  3189. sd->subrace[type2]+=val;
  3190. break;
  3191. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3192. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3193. if(sd->state.lr_flag != 2)
  3194. sd->subclass[type2]+=val;
  3195. break;
  3196. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3197. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3198. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3199. break;
  3200. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3201. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3202. 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);
  3203. break;
  3204. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3205. if(sd->state.lr_flag == 2)
  3206. break;
  3207. if (sd->reseff.size() == MAX_PC_BONUS) {
  3208. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3209. break;
  3210. }
  3211. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3212. break;
  3213. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3214. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3215. if(sd->state.lr_flag != 2)
  3216. sd->magic_addele_script[type2] += val;
  3217. break;
  3218. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3219. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3220. if(sd->state.lr_flag != 2)
  3221. sd->magic_addrace[type2]+=val;
  3222. break;
  3223. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3224. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3225. if(sd->state.lr_flag != 2)
  3226. sd->magic_addclass[type2]+=val;
  3227. break;
  3228. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3229. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3230. if(sd->state.lr_flag != 2)
  3231. sd->magic_addsize[type2]+=val;
  3232. break;
  3233. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3234. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3235. if(sd->state.lr_flag != 2)
  3236. sd->magic_atk_ele[type2]+=val;
  3237. break;
  3238. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3239. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3240. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3241. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3242. break;
  3243. }
  3244. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3245. }
  3246. break;
  3247. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3248. if(sd->state.lr_flag == 2)
  3249. break;
  3250. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3251. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3252. break;
  3253. }
  3254. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3255. break;
  3256. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3257. if(sd->state.lr_flag == 2)
  3258. break;
  3259. if (sd->add_def.size() == MAX_PC_BONUS) {
  3260. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3261. break;
  3262. }
  3263. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3264. break;
  3265. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3266. if(sd->state.lr_flag == 2)
  3267. break;
  3268. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3269. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3270. break;
  3271. }
  3272. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3273. break;
  3274. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3275. if(!sd->state.lr_flag) {
  3276. sd->right_weapon.hp_drain_rate.rate += type2;
  3277. sd->right_weapon.hp_drain_rate.per += val;
  3278. }
  3279. else if(sd->state.lr_flag == 1) {
  3280. sd->left_weapon.hp_drain_rate.rate += type2;
  3281. sd->left_weapon.hp_drain_rate.per += val;
  3282. }
  3283. break;
  3284. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3285. if(!sd->state.lr_flag) {
  3286. sd->right_weapon.sp_drain_rate.rate += type2;
  3287. sd->right_weapon.sp_drain_rate.per += val;
  3288. }
  3289. else if(sd->state.lr_flag == 1) {
  3290. sd->left_weapon.sp_drain_rate.rate += type2;
  3291. sd->left_weapon.sp_drain_rate.per += val;
  3292. }
  3293. break;
  3294. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3295. if(sd->state.lr_flag != 2) {
  3296. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3297. }
  3298. break;
  3299. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3300. if(sd->state.lr_flag != 2) {
  3301. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3302. }
  3303. break;
  3304. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3305. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3306. sd->bonus.get_zeny_rate = val;
  3307. sd->bonus.get_zeny_num = type2;
  3308. }
  3309. break;
  3310. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3311. if(sd->state.lr_flag != 2) {
  3312. sd->bonus.get_zeny_rate += val;
  3313. sd->bonus.get_zeny_num += type2;
  3314. }
  3315. break;
  3316. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3317. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3318. if(sd->state.lr_flag == 2)
  3319. break;
  3320. sd->weapon_coma_ele[type2] += val;
  3321. sd->special_state.bonus_coma = 1;
  3322. break;
  3323. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3324. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3325. if(sd->state.lr_flag == 2)
  3326. break;
  3327. sd->weapon_coma_race[type2] += val;
  3328. sd->special_state.bonus_coma = 1;
  3329. break;
  3330. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3331. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3332. if(sd->state.lr_flag == 2)
  3333. break;
  3334. sd->weapon_coma_class[type2] += val;
  3335. sd->special_state.bonus_coma = 1;
  3336. break;
  3337. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3338. if(sd->state.lr_flag != 2)
  3339. sd->weapon_atk[type2]+=val;
  3340. break;
  3341. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3342. if(sd->state.lr_flag != 2)
  3343. sd->weapon_damage_rate[type2]+=val;
  3344. break;
  3345. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3346. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3347. if(sd->state.lr_flag != 2)
  3348. sd->critaddrace[type2] += val*10;
  3349. break;
  3350. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3351. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3352. if(sd->state.lr_flag != 2)
  3353. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3354. break;
  3355. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3356. if(sd->state.lr_flag == 2)
  3357. break;
  3358. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3359. 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);
  3360. break;
  3361. }
  3362. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3363. break;
  3364. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3365. if(sd->state.lr_flag == 2)
  3366. break;
  3367. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3368. 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);
  3369. break;
  3370. }
  3371. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3372. break;
  3373. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3374. if(sd->state.lr_flag == 2)
  3375. break;
  3376. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3377. 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);
  3378. break;
  3379. }
  3380. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3381. break;
  3382. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3383. if(sd->state.lr_flag == 2)
  3384. break;
  3385. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3386. 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);
  3387. break;
  3388. }
  3389. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3390. break;
  3391. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3392. if(sd->state.lr_flag != 2) {
  3393. sd->hp_loss.value = type2;
  3394. sd->hp_loss.rate = val;
  3395. }
  3396. break;
  3397. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3398. if(sd->state.lr_flag != 2) {
  3399. sd->hp_regen.value = type2;
  3400. sd->hp_regen.rate = val;
  3401. }
  3402. break;
  3403. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3404. if (sd->state.lr_flag != 2) {
  3405. sd->percent_hp_regen.value = type2;
  3406. sd->percent_hp_regen.rate = val;
  3407. }
  3408. break;
  3409. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3410. if (sd->state.lr_flag != 2) {
  3411. sd->percent_sp_regen.value = type2;
  3412. sd->percent_sp_regen.rate = val;
  3413. }
  3414. break;
  3415. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3416. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3417. if(sd->state.lr_flag != 2)
  3418. sd->right_weapon.addrace2[type2] += val;
  3419. else
  3420. sd->left_weapon.addrace2[type2] += val;
  3421. break;
  3422. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3423. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3424. if(sd->state.lr_flag != 2)
  3425. sd->subsize[type2]+=val;
  3426. break;
  3427. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3428. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3429. if(sd->state.lr_flag != 2)
  3430. sd->subrace2[type2]+=val;
  3431. break;
  3432. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3433. if(sd->state.lr_flag == 2)
  3434. break;
  3435. if (!itemdb_exists(type2)) {
  3436. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3437. break;
  3438. }
  3439. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3440. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3441. break;
  3442. }
  3443. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3444. break;
  3445. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3446. if (sd->state.lr_flag == 2)
  3447. break;
  3448. if (!type2 || !itemdb_group_exists(type2)) {
  3449. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3450. break;
  3451. }
  3452. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3453. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3454. break;
  3455. }
  3456. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3457. break;
  3458. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3459. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3460. if(sd->state.lr_flag != 2)
  3461. sd->expaddrace[type2]+=val;
  3462. break;
  3463. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3464. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3465. if(sd->state.lr_flag != 2)
  3466. sd->expaddclass[type2]+=val;
  3467. break;
  3468. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3469. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3470. if(sd->state.lr_flag != 2)
  3471. sd->sp_gain_race[type2]+=val;
  3472. break;
  3473. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3474. if (sd->state.lr_flag != 2)
  3475. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3476. break;
  3477. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3478. if (sd->state.lr_flag != 2)
  3479. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3480. break;
  3481. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3482. if(sd->state.lr_flag != 2) {
  3483. sd->sp_loss.value = type2;
  3484. sd->sp_loss.rate = val;
  3485. }
  3486. break;
  3487. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3488. if(sd->state.lr_flag != 2) {
  3489. sd->sp_regen.value = type2;
  3490. sd->sp_regen.rate = val;
  3491. }
  3492. break;
  3493. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3494. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3495. if(!sd->state.lr_flag) {
  3496. sd->right_weapon.hp_drain_race[type2] += val;
  3497. }
  3498. else if(sd->state.lr_flag == 1) {
  3499. sd->left_weapon.hp_drain_race[type2] += val;
  3500. }
  3501. break;
  3502. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3503. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3504. if(!sd->state.lr_flag) {
  3505. sd->right_weapon.sp_drain_race[type2] += val;
  3506. }
  3507. else if(sd->state.lr_flag == 1) {
  3508. sd->left_weapon.sp_drain_race[type2] += val;
  3509. }
  3510. break;
  3511. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3512. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3513. if(!sd->state.lr_flag) {
  3514. sd->right_weapon.hp_drain_class[type2] += val;
  3515. }
  3516. else if(sd->state.lr_flag == 1) {
  3517. sd->left_weapon.hp_drain_class[type2] += val;
  3518. }
  3519. break;
  3520. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3521. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3522. if(!sd->state.lr_flag) {
  3523. sd->right_weapon.sp_drain_class[type2] += val;
  3524. }
  3525. else if(sd->state.lr_flag == 1) {
  3526. sd->left_weapon.sp_drain_class[type2] += val;
  3527. }
  3528. break;
  3529. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3530. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3531. if(sd->state.lr_flag != 2)
  3532. sd->ignore_mdef_by_race[type2] += val;
  3533. break;
  3534. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3535. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3536. if(sd->state.lr_flag != 2)
  3537. sd->ignore_mdef_by_class[type2] += val;
  3538. break;
  3539. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3540. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3541. if(sd->state.lr_flag != 2)
  3542. sd->ignore_def_by_race[type2] += val;
  3543. break;
  3544. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3545. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3546. if (sd->state.lr_flag != 2)
  3547. sd->ignore_def_by_class[type2] += val;
  3548. break;
  3549. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3550. if(sd->state.lr_flag == 2)
  3551. break;
  3552. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3553. 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);
  3554. break;
  3555. }
  3556. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3557. break;
  3558. case SP_SKILL_DELAY:
  3559. if(sd->state.lr_flag == 2)
  3560. break;
  3561. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3562. 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);
  3563. break;
  3564. }
  3565. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3566. break;
  3567. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3568. if(sd->state.lr_flag == 2)
  3569. break;
  3570. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3571. 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);
  3572. break;
  3573. }
  3574. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  3575. break;
  3576. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3577. if (sd->state.lr_flag == 2)
  3578. break;
  3579. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3580. 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);
  3581. break;
  3582. }
  3583. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  3584. break;
  3585. #ifdef RENEWAL_CAST
  3586. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3587. if(sd->state.lr_flag == 2)
  3588. break;
  3589. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3590. 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);
  3591. break;
  3592. }
  3593. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  3594. break;
  3595. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3596. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3597. if(sd->state.lr_flag == 2)
  3598. break;
  3599. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3600. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3601. break;
  3602. }
  3603. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  3604. break;
  3605. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3606. if(sd->state.lr_flag == 2)
  3607. break;
  3608. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3609. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3610. break;
  3611. }
  3612. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  3613. break;
  3614. #else
  3615. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3616. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3617. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3618. break;
  3619. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3620. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3621. if(sd->state.lr_flag == 2)
  3622. break;
  3623. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3624. 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);
  3625. break;
  3626. }
  3627. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  3628. break;
  3629. #endif
  3630. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3631. if(sd->state.lr_flag == 2)
  3632. break;
  3633. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3634. 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);
  3635. break;
  3636. }
  3637. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  3638. break;
  3639. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3640. if (sd->subskill.size() == MAX_PC_BONUS) {
  3641. 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);
  3642. break;
  3643. }
  3644. pc_bonus_itembonus(sd->subskill, type2, val, false);
  3645. break;
  3646. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3647. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3648. sd->subdefele[type2] += val;
  3649. break;
  3650. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3651. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3652. sd->coma_class[type2] += val;
  3653. sd->special_state.bonus_coma = 1;
  3654. break;
  3655. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3656. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3657. sd->coma_race[type2] += val;
  3658. sd->special_state.bonus_coma = 1;
  3659. break;
  3660. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3661. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3662. if(sd->state.lr_flag != 2)
  3663. sd->magic_addrace2[type2] += val;
  3664. break;
  3665. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3666. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3667. if (sd->state.lr_flag != 2)
  3668. sd->ignore_mdef_by_race2[type2] += val;
  3669. break;
  3670. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3671. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3672. if (sd->state.lr_flag != 2)
  3673. sd->dropaddrace[type2] += val;
  3674. break;
  3675. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3676. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3677. if (sd->state.lr_flag != 2)
  3678. sd->dropaddclass[type2] += val;
  3679. break;
  3680. default:
  3681. if (running_npc_stat_calc_event) {
  3682. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3683. }
  3684. else if (current_equip_combo_pos > 0) {
  3685. 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);
  3686. }
  3687. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3688. 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);
  3689. }
  3690. else {
  3691. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3692. }
  3693. break;
  3694. }
  3695. }
  3696. /**
  3697. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  3698. * @param sd
  3699. * @param type Bonus type used by bBonusName
  3700. * @param type2
  3701. * @param type3
  3702. * @param val Value that usually for rate or fixed value
  3703. */
  3704. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3705. {
  3706. nullpo_retv(sd);
  3707. switch(type){
  3708. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3709. if(sd->state.lr_flag != 2)
  3710. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  3711. break;
  3712. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3713. if(sd->state.lr_flag != 2)
  3714. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  3715. break;
  3716. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3717. if(sd->state.lr_flag != 2)
  3718. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  3719. break;
  3720. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3721. if(sd->state.lr_flag != 2)
  3722. {
  3723. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3724. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3725. pc_bonus_autospell(sd->autospell, target?-type2:type2, type3, val, 0, current_equip_card_id);
  3726. }
  3727. break;
  3728. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3729. if(sd->state.lr_flag != 2)
  3730. {
  3731. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3732. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3733. pc_bonus_autospell(sd->autospell2, target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3734. }
  3735. break;
  3736. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3737. if (sd->state.lr_flag != 2)
  3738. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  3739. break;
  3740. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3741. if (sd->state.lr_flag != 2)
  3742. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  3743. break;
  3744. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3745. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3746. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3747. break;
  3748. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3749. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3750. if(sd->state.lr_flag != 2)
  3751. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  3752. break;
  3753. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3754. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3755. if( sd->state.lr_flag != 2 )
  3756. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  3757. break;
  3758. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3759. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3760. if (sd->state.lr_flag != 2)
  3761. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3762. break;
  3763. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3764. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3765. if (sd->state.lr_flag != 2)
  3766. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3767. break;
  3768. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3769. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3770. if(sd->state.lr_flag != 2) {
  3771. sd->sp_vanish_race[type2].rate += type3;
  3772. sd->sp_vanish_race[type2].per += val;
  3773. }
  3774. break;
  3775. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3776. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3777. if(sd->state.lr_flag != 2) {
  3778. sd->hp_vanish_race[type2].rate += type3;
  3779. sd->hp_vanish_race[type2].per += val;
  3780. }
  3781. break;
  3782. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  3783. if(sd->state.lr_flag != 2) {
  3784. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  3785. }
  3786. break;
  3787. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  3788. if(sd->state.lr_flag != 2) {
  3789. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  3790. }
  3791. break;
  3792. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3793. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3794. if (sd->state.lr_flag == 2)
  3795. break;
  3796. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3797. //if (type3 > sd->norecover_state_race[type2].rate) {
  3798. // sd->norecover_state_race[type2].rate = type3;
  3799. // sd->norecover_state_race[type2].tick = val;
  3800. // break;
  3801. //}
  3802. sd->norecover_state_race[type2].rate = type3;
  3803. sd->norecover_state_race[type2].tick = val;
  3804. break;
  3805. default:
  3806. if (running_npc_stat_calc_event) {
  3807. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3808. }
  3809. else if (current_equip_combo_pos > 0) {
  3810. 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);
  3811. }
  3812. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3813. 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);
  3814. }
  3815. else {
  3816. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3817. }
  3818. break;
  3819. }
  3820. }
  3821. /**
  3822. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  3823. * @param sd
  3824. * @param type Bonus type used by bBonusName
  3825. * @param type2
  3826. * @param type3
  3827. * @param type4
  3828. * @param val Value that usually for rate or fixed value
  3829. */
  3830. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3831. {
  3832. nullpo_retv(sd);
  3833. switch(type){
  3834. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3835. if(sd->state.lr_flag != 2)
  3836. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3837. break;
  3838. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3839. if(sd->state.lr_flag != 2)
  3840. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3841. break;
  3842. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3843. if(sd->state.lr_flag != 2)
  3844. {
  3845. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3846. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  3847. pc_bonus_autospell_onskill(sd->autospell3, type2, target?-type3:type3, type4, val, current_equip_card_id);
  3848. }
  3849. break;
  3850. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3851. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3852. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3853. break;
  3854. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3855. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3856. if (sd->state.lr_flag != 2)
  3857. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  3858. break;
  3859. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3860. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3861. if( sd->state.lr_flag != 2 )
  3862. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  3863. break;
  3864. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3865. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3866. if(sd->state.lr_flag == 2)
  3867. break;
  3868. sd->def_set_race[type2].rate = type3;
  3869. sd->def_set_race[type2].tick = type4;
  3870. sd->def_set_race[type2].value = val;
  3871. break;
  3872. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3873. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3874. if(sd->state.lr_flag == 2)
  3875. break;
  3876. sd->mdef_set_race[type2].rate = type3;
  3877. sd->mdef_set_race[type2].tick = type4;
  3878. sd->mdef_set_race[type2].value = val;
  3879. break;
  3880. default:
  3881. if (running_npc_stat_calc_event) {
  3882. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3883. }
  3884. else if (current_equip_combo_pos > 0) {
  3885. 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);
  3886. }
  3887. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3888. 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);
  3889. }
  3890. else {
  3891. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3892. }
  3893. break;
  3894. }
  3895. }
  3896. /**
  3897. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3898. * @param sd
  3899. * @param type Bonus type used by bBonusName
  3900. * @param type2
  3901. * @param type3
  3902. * @param type4
  3903. * @param val Value that usually for rate or fixed value
  3904. */
  3905. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3906. {
  3907. nullpo_retv(sd);
  3908. switch(type){
  3909. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3910. if(sd->state.lr_flag != 2)
  3911. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3912. break;
  3913. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3914. if(sd->state.lr_flag != 2)
  3915. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3916. break;
  3917. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3918. if(sd->state.lr_flag != 2)
  3919. pc_bonus_autospell_onskill(sd->autospell3, type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3920. break;
  3921. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3922. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3923. if( sd->state.lr_flag != 2 )
  3924. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  3925. break;
  3926. default:
  3927. if (running_npc_stat_calc_event) {
  3928. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3929. }
  3930. else if (current_equip_combo_pos > 0) {
  3931. 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);
  3932. }
  3933. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3934. 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);
  3935. }
  3936. else {
  3937. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3938. }
  3939. break;
  3940. }
  3941. }
  3942. /*==========================================
  3943. * Grants a player a given skill. Flag values are:
  3944. * 0 - Grant permanent skill to be bound to skill tree
  3945. * 1 - Grant an item skill (temporary)
  3946. * 2 - Like 1, except the level granted can stack with previously learned level.
  3947. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3948. *------------------------------------------*/
  3949. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3950. uint16 idx = 0;
  3951. nullpo_ret(sd);
  3952. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3953. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3954. return false;
  3955. }
  3956. if (level > MAX_SKILL_LEVEL) {
  3957. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3958. return false;
  3959. }
  3960. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3961. 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);
  3962. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3963. }
  3964. switch (type) {
  3965. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3966. sd->status.skill[idx].id = skill_id;
  3967. sd->status.skill[idx].lv = level;
  3968. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3969. if (level == 0) { //Remove skill.
  3970. sd->status.skill[idx].id = 0;
  3971. clif_deleteskill(sd,skill_id);
  3972. } else
  3973. clif_addskill(sd,skill_id);
  3974. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3975. status_calc_pc(sd, SCO_NONE);
  3976. break;
  3977. case ADDSKILL_TEMP: //Item bonus skill.
  3978. if (sd->status.skill[idx].id != 0) {
  3979. if (sd->status.skill[idx].lv >= level)
  3980. return true;
  3981. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3982. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3983. } else {
  3984. sd->status.skill[idx].id = skill_id;
  3985. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3986. }
  3987. sd->status.skill[idx].lv = level;
  3988. break;
  3989. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3990. if (sd->status.skill[idx].id != 0) {
  3991. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3992. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3993. } else {
  3994. sd->status.skill[idx].id = skill_id;
  3995. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3996. }
  3997. sd->status.skill[idx].lv += level;
  3998. break;
  3999. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4000. sd->status.skill[idx].id = skill_id;
  4001. sd->status.skill[idx].lv = level;
  4002. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4003. if (level == 0) { //Remove skill.
  4004. sd->status.skill[idx].id = 0;
  4005. clif_deleteskill(sd,skill_id);
  4006. } else
  4007. clif_addskill(sd,skill_id);
  4008. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  4009. status_calc_pc(sd, SCO_NONE);
  4010. break;
  4011. default:
  4012. return false;
  4013. }
  4014. return true;
  4015. }
  4016. /*==========================================
  4017. * Append a card to an item ?
  4018. *------------------------------------------*/
  4019. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  4020. {
  4021. int i;
  4022. unsigned short nameid;
  4023. nullpo_ret(sd);
  4024. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  4025. return 0; //Invalid item index.
  4026. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  4027. return 0; //Invalid card index.
  4028. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4029. return 0; // target item missing
  4030. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4031. return 0; // target card missing
  4032. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  4033. return 0; // only weapons and armor are allowed
  4034. if( sd->inventory_data[idx_card]->type != IT_CARD )
  4035. return 0; // must be a card
  4036. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4037. return 0; // target must be identified
  4038. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4039. return 0; // card slots reserved for other purposes
  4040. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  4041. return 0; // card cannot be compounded on this item type
  4042. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  4043. return 0; // attempted to place shield card on left-hand weapon.
  4044. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4045. return 0; // item must be unequipped
  4046. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4047. if( i == sd->inventory_data[idx_equip]->slot )
  4048. return 0; // no free slots
  4049. // remember the card id to insert
  4050. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4051. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4052. {// failed
  4053. clif_insert_card(sd,idx_equip,idx_card,1);
  4054. }
  4055. else
  4056. {// success
  4057. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4058. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4059. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4060. clif_insert_card(sd,idx_equip,idx_card,0);
  4061. }
  4062. return 0;
  4063. }
  4064. /**
  4065. * Returns the count of unidentified items with the option to identify too.
  4066. * @param sd: Player data
  4067. * @param identify_item: Whether or not to identify any unidentified items
  4068. * @return Unidentified items count
  4069. */
  4070. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  4071. {
  4072. int unidentified_count = 0;
  4073. for (int i = 0; i < MAX_INVENTORY; i++) {
  4074. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4075. if (identify_item == true) {
  4076. sd->inventory.u.items_inventory[i].identify = 1;
  4077. clif_item_identified(sd,i,0);
  4078. }
  4079. unidentified_count++;
  4080. }
  4081. }
  4082. return unidentified_count;
  4083. }
  4084. //
  4085. // Items
  4086. //
  4087. /*==========================================
  4088. * Update buying value by skills
  4089. *------------------------------------------*/
  4090. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4091. {
  4092. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4093. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4094. rate1 = 5+skill*2-((skill==10)? 1:0);
  4095. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4096. rate2 = 5+skill*4;
  4097. if(rate1 < rate2) rate1 = rate2;
  4098. if(rate1)
  4099. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4100. if(val < battle_config.min_shop_buy)
  4101. val = battle_config.min_shop_buy;
  4102. return val;
  4103. }
  4104. /*==========================================
  4105. * Update selling value by skills
  4106. *------------------------------------------*/
  4107. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4108. {
  4109. int skill,val = orig_value,rate = 0;
  4110. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4111. rate = 5+skill*2-((skill==10)? 1:0);
  4112. if(rate)
  4113. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4114. if (val < battle_config.min_shop_sell)
  4115. val = battle_config.min_shop_sell;
  4116. return val;
  4117. }
  4118. /*==========================================
  4119. * Checking if we have enough place on inventory for new item
  4120. * Make sure to take 30k as limit (for client I guess)
  4121. * @param sd
  4122. * @param nameid
  4123. * @param amount
  4124. * @return e_chkitem_result
  4125. *------------------------------------------*/
  4126. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  4127. {
  4128. int i;
  4129. struct item_data* data;
  4130. nullpo_ret(sd);
  4131. if(amount > MAX_AMOUNT)
  4132. return CHKADDITEM_OVERAMOUNT;
  4133. data = itemdb_search(nameid);
  4134. if(!itemdb_isstackable2(data))
  4135. return CHKADDITEM_NEW;
  4136. if( data->stack.inventory && amount > data->stack.amount )
  4137. return CHKADDITEM_OVERAMOUNT;
  4138. if (data->flag.guid)
  4139. return CHKADDITEM_NEW;
  4140. for(i=0;i<MAX_INVENTORY;i++){
  4141. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4142. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4143. 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 ) )
  4144. return CHKADDITEM_OVERAMOUNT;
  4145. return CHKADDITEM_EXIST;
  4146. }
  4147. }
  4148. return CHKADDITEM_NEW;
  4149. }
  4150. /*==========================================
  4151. * Return number of available place in inventory
  4152. * Each non stackable item will reduce place by 1
  4153. * @param sd
  4154. * @return Number of empty slots
  4155. *------------------------------------------*/
  4156. uint8 pc_inventoryblank(struct map_session_data *sd)
  4157. {
  4158. uint8 i, b;
  4159. nullpo_ret(sd);
  4160. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  4161. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4162. b++;
  4163. }
  4164. return b;
  4165. }
  4166. /**
  4167. * Attempts to remove zeny from player
  4168. * @param sd: Player
  4169. * @param zeny: Zeny removed
  4170. * @param type: Log type
  4171. * @param tsd: (optional) From who to log (if null take sd)
  4172. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4173. */
  4174. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4175. {
  4176. nullpo_retr(2,sd);
  4177. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4178. if( zeny < 0 )
  4179. {
  4180. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4181. return 1;
  4182. }
  4183. if( sd->status.zeny < zeny )
  4184. return 1; //Not enough.
  4185. sd->status.zeny -= zeny;
  4186. clif_updatestatus(sd,SP_ZENY);
  4187. if(!tsd) tsd = sd;
  4188. log_zeny(sd, type, tsd, -zeny);
  4189. if( zeny > 0 && sd->state.showzeny ) {
  4190. char output[255];
  4191. sprintf(output, "Removed %dz.", zeny);
  4192. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4193. }
  4194. return 0;
  4195. }
  4196. /**
  4197. * Attempts to give zeny to player
  4198. * @param sd: Player
  4199. * @param type: Log type
  4200. * @param tsd: (optional) From who to log (if null take sd)
  4201. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4202. */
  4203. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4204. {
  4205. nullpo_retr(-1,sd);
  4206. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4207. if( zeny < 0 )
  4208. {
  4209. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4210. return 1;
  4211. }
  4212. if( zeny > MAX_ZENY - sd->status.zeny )
  4213. zeny = MAX_ZENY - sd->status.zeny;
  4214. sd->status.zeny += zeny;
  4215. clif_updatestatus(sd,SP_ZENY);
  4216. if(!tsd) tsd = sd;
  4217. log_zeny(sd, type, tsd, zeny);
  4218. if( zeny > 0 && sd->state.showzeny ) {
  4219. char output[255];
  4220. sprintf(output, "Gained %dz.", zeny);
  4221. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4222. }
  4223. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4224. return 0;
  4225. }
  4226. /**
  4227. * Attempts to remove Cash Points from player
  4228. * @param sd: Player
  4229. * @param price: Total points (cash + kafra) the player has to pay
  4230. * @param points: Kafra points the player has to pay
  4231. * @param type: Log type
  4232. * @return -1: Not enough points, otherwise success (cash+points)
  4233. */
  4234. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4235. {
  4236. int cash;
  4237. nullpo_retr(-1,sd);
  4238. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4239. cash = price-points;
  4240. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4241. {
  4242. 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);
  4243. return -1;
  4244. }
  4245. if( cash ){
  4246. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4247. sd->cashPoints -= cash;
  4248. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4249. }
  4250. if( points ){
  4251. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4252. sd->kafraPoints -= points;
  4253. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4254. }
  4255. if( battle_config.cashshop_show_points )
  4256. {
  4257. char output[CHAT_SIZE_MAX];
  4258. 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.
  4259. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4260. }
  4261. return cash+points;
  4262. }
  4263. /**
  4264. * Attempts to give Cash Points to player
  4265. * @param sd: Player
  4266. * @param cash: Cash points the player gets
  4267. * @param points: Kafra points the player gets
  4268. * @param type: Log type
  4269. * @return -1: Error, otherwise success (cash or points)
  4270. */
  4271. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4272. {
  4273. char output[CHAT_SIZE_MAX];
  4274. nullpo_retr(-1,sd);
  4275. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4276. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4277. if( cash > 0 )
  4278. {
  4279. if( cash > MAX_ZENY-sd->cashPoints )
  4280. {
  4281. 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);
  4282. cash = MAX_ZENY-sd->cashPoints;
  4283. }
  4284. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4285. sd->cashPoints += cash;
  4286. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4287. if( battle_config.cashshop_show_points )
  4288. {
  4289. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4290. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4291. }
  4292. return cash;
  4293. }
  4294. if( points > 0 )
  4295. {
  4296. if( points > MAX_ZENY-sd->kafraPoints )
  4297. {
  4298. 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);
  4299. points = MAX_ZENY-sd->kafraPoints;
  4300. }
  4301. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4302. sd->kafraPoints += points;
  4303. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4304. if( battle_config.cashshop_show_points )
  4305. {
  4306. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4307. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4308. }
  4309. return points;
  4310. }
  4311. return -1; //shouldn't happen but just in case
  4312. }
  4313. /**
  4314. * Searching a specified itemid in inventory and return his stored index
  4315. * @param sd Player
  4316. * @param nameid Find this Item!
  4317. * @return Stored index in inventory, or -1 if not found.
  4318. **/
  4319. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4320. short i;
  4321. nullpo_retr(-1, sd);
  4322. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4323. return ( i < MAX_INVENTORY ) ? i : -1;
  4324. }
  4325. /** Attempt to add a new item to player inventory
  4326. * @param sd
  4327. * @param item_data
  4328. * @param amount
  4329. * @param log_type
  4330. * @return
  4331. * 0 = success
  4332. * 1 = invalid itemid not found or negative amount
  4333. * 2 = overweight
  4334. * 3 = ?
  4335. * 4 = no free place found
  4336. * 5 = max amount reached
  4337. * 6 = ?
  4338. * 7 = stack limitation
  4339. */
  4340. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4341. struct item_data *id;
  4342. int16 i;
  4343. unsigned int w;
  4344. nullpo_retr(ADDITEM_INVALID, sd);
  4345. nullpo_retr(ADDITEM_INVALID, item);
  4346. if( item->nameid == 0 || amount <= 0 )
  4347. return ADDITEM_INVALID;
  4348. if( amount > MAX_AMOUNT )
  4349. return ADDITEM_OVERAMOUNT;
  4350. id = itemdb_search(item->nameid);
  4351. if( id->stack.inventory && amount > id->stack.amount )
  4352. {// item stack limitation
  4353. return ADDITEM_STACKLIMIT;
  4354. }
  4355. w = id->weight*amount;
  4356. if(sd->weight + w > sd->max_weight)
  4357. return ADDITEM_OVERWEIGHT;
  4358. i = MAX_INVENTORY;
  4359. if (id->flag.guid && !item->unique_id)
  4360. item->unique_id = pc_generate_unique_id(sd);
  4361. // Stackable | Non Rental
  4362. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4363. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4364. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4365. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4366. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4367. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4368. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4369. 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 ) )
  4370. return ADDITEM_OVERAMOUNT;
  4371. sd->inventory.u.items_inventory[i].amount += amount;
  4372. clif_additem(sd,i,amount,0);
  4373. break;
  4374. }
  4375. }
  4376. }
  4377. if (i >= MAX_INVENTORY) {
  4378. i = pc_search_inventory(sd,0);
  4379. if( i < 0 )
  4380. return ADDITEM_OVERITEM;
  4381. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4382. // clear equip and favorite fields first, just in case
  4383. if( item->equip )
  4384. sd->inventory.u.items_inventory[i].equip = 0;
  4385. if( item->favorite )
  4386. sd->inventory.u.items_inventory[i].favorite = 0;
  4387. if( item->equipSwitch )
  4388. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4389. sd->inventory.u.items_inventory[i].amount = amount;
  4390. sd->inventory_data[i] = id;
  4391. sd->last_addeditem_index = i;
  4392. if (!itemdb_isstackable2(id) || id->flag.guid)
  4393. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4394. clif_additem(sd,i,amount,0);
  4395. }
  4396. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4397. sd->weight += w;
  4398. clif_updatestatus(sd,SP_WEIGHT);
  4399. //Auto-equip
  4400. if(id->flag.autoequip)
  4401. pc_equipitem(sd, i, id->equip);
  4402. /* rental item check */
  4403. if( item->expire_time ) {
  4404. if( time(NULL) > item->expire_time ) {
  4405. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4406. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4407. } else {
  4408. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4409. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4410. pc_inventory_rental_add(sd, seconds);
  4411. }
  4412. }
  4413. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4414. pc_show_questinfo(sd);
  4415. return ADDITEM_SUCCESS;
  4416. }
  4417. /*==========================================
  4418. * Remove an item at index n from inventory by amount.
  4419. * @param sd
  4420. * @param n Item index in inventory
  4421. * @param amount
  4422. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4423. * @param reason Delete reason
  4424. * @param log_type e_log_pick_type
  4425. * @return 1 - invalid itemid or negative amount; 0 - Success
  4426. *------------------------------------------*/
  4427. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4428. {
  4429. nullpo_retr(1, sd);
  4430. 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)
  4431. return 1;
  4432. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4433. sd->inventory.u.items_inventory[n].amount -= amount;
  4434. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4435. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4436. if(sd->inventory.u.items_inventory[n].equip)
  4437. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4438. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4439. sd->inventory_data[n] = NULL;
  4440. }
  4441. if(!(type&1))
  4442. clif_delitem(sd,n,amount,reason);
  4443. if(!(type&2))
  4444. clif_updatestatus(sd,SP_WEIGHT);
  4445. pc_show_questinfo(sd);
  4446. return 0;
  4447. }
  4448. /*==========================================
  4449. * Attempt to drop an item.
  4450. * @param sd
  4451. * @param n Item index in inventory
  4452. * @param amount Amount of item
  4453. * @return False = fail; True = success
  4454. *------------------------------------------*/
  4455. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4456. {
  4457. nullpo_retr(1, sd);
  4458. if(n < 0 || n >= MAX_INVENTORY)
  4459. return false;
  4460. if(amount <= 0)
  4461. return false;
  4462. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4463. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4464. sd->inventory.u.items_inventory[n].amount < amount ||
  4465. sd->state.trading || sd->state.vending ||
  4466. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4467. )
  4468. return false;
  4469. if( sd->inventory.u.items_inventory[n].equipSwitch )
  4470. return false;
  4471. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4472. {
  4473. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4474. return false; //Can't drop items in nodrop mapflag maps.
  4475. }
  4476. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4477. {
  4478. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4479. return false;
  4480. }
  4481. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4482. return false;
  4483. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4484. clif_dropitem(sd, n, amount);
  4485. return true;
  4486. }
  4487. /*==========================================
  4488. * Attempt to pick up an item.
  4489. * @param sd
  4490. * @param fitem Item that will be picked
  4491. * @return False = fail; True = success
  4492. *------------------------------------------*/
  4493. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4494. {
  4495. int flag = 0;
  4496. t_tick tick = gettick();
  4497. struct party_data *p = NULL;
  4498. nullpo_ret(sd);
  4499. nullpo_ret(fitem);
  4500. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4501. return false; // Distance is too far
  4502. if (sd->sc.cant.pickup)
  4503. return false;
  4504. if (sd->status.party_id)
  4505. p = party_search(sd->status.party_id);
  4506. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4507. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4508. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4509. if (!(p && p->party.item&1 &&
  4510. first_sd && first_sd->status.party_id == sd->status.party_id
  4511. ))
  4512. return false;
  4513. }
  4514. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4515. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4516. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4517. if (!(p && p->party.item&1 &&
  4518. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4519. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4520. ))
  4521. return false;
  4522. }
  4523. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4524. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4525. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4526. if(!(p && p->party.item&1 &&
  4527. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4528. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4529. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4530. ))
  4531. return false;
  4532. }
  4533. }
  4534. }
  4535. }
  4536. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4537. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4538. clif_additem(sd,0,0,flag);
  4539. return true;
  4540. }
  4541. //Display pickup animation.
  4542. pc_stop_attack(sd);
  4543. clif_takeitem(&sd->bl,&fitem->bl);
  4544. if (fitem->mob_id &&
  4545. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4546. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4547. )
  4548. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4549. map_clearflooritem(&fitem->bl);
  4550. return true;
  4551. }
  4552. /*==========================================
  4553. * Check if item is usable.
  4554. * Return:
  4555. * false = no
  4556. * true = yes
  4557. *------------------------------------------*/
  4558. bool pc_isUseitem(struct map_session_data *sd,int n)
  4559. {
  4560. struct item_data *item;
  4561. unsigned short nameid;
  4562. nullpo_ret(sd);
  4563. item = sd->inventory_data[n];
  4564. nameid = sd->inventory.u.items_inventory[n].nameid;
  4565. if( item == NULL )
  4566. return false;
  4567. //Not consumable item
  4568. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4569. return false;
  4570. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4571. return true;
  4572. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4573. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4574. return false;
  4575. //Prevent mass item usage. [Skotlex]
  4576. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4577. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4578. )
  4579. return false;
  4580. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4581. clif_msg(sd,ITEM_NOUSE_SITTING);
  4582. return false; // You cannot use this item while sitting.
  4583. }
  4584. if (sd->state.storage_flag && item->type != IT_CASH) {
  4585. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4586. return false; // You cannot use this item while storage is open.
  4587. }
  4588. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4589. return false;
  4590. switch( nameid ) {
  4591. case ITEMID_WING_OF_FLY:
  4592. case ITEMID_GIANT_FLY_WING:
  4593. case ITEMID_N_FLY_WING:
  4594. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4595. clif_skill_teleportmessage(sd,0);
  4596. return false;
  4597. }
  4598. if (nameid == ITEMID_GIANT_FLY_WING) {
  4599. struct party_data *pd = party_search(sd->status.party_id);
  4600. if (pd) {
  4601. int i;
  4602. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4603. if (i == MAX_PARTY) { // User is not party leader
  4604. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4605. break;
  4606. }
  4607. 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));
  4608. if (i == MAX_PARTY) { // No party members found on same map
  4609. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4610. break;
  4611. }
  4612. } else {
  4613. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4614. break;
  4615. }
  4616. }
  4617. // Fall through
  4618. case ITEMID_WING_OF_BUTTERFLY:
  4619. case ITEMID_N_BUTTERFLY_WING:
  4620. case ITEMID_DUN_TELE_SCROLL1:
  4621. case ITEMID_DUN_TELE_SCROLL2:
  4622. case ITEMID_DUN_TELE_SCROLL3:
  4623. case ITEMID_WOB_RUNE:
  4624. case ITEMID_WOB_SCHWALTZ:
  4625. case ITEMID_WOB_RACHEL:
  4626. case ITEMID_WOB_LOCAL:
  4627. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4628. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4629. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4630. return false;
  4631. }
  4632. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4633. return false;
  4634. break;
  4635. case ITEMID_MERCENARY_RED_POTION:
  4636. case ITEMID_MERCENARY_BLUE_POTION:
  4637. case ITEMID_M_CENTER_POTION:
  4638. case ITEMID_M_AWAKENING_POTION:
  4639. case ITEMID_M_BERSERK_POTION:
  4640. if( sd->md == NULL || sd->md->db == NULL )
  4641. return false;
  4642. if( sd->md->sc.data[SC_BERSERK] )
  4643. return false;
  4644. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4645. return false;
  4646. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4647. return false;
  4648. break;
  4649. case ITEMID_NEURALIZER:
  4650. if( !mapdata->flag[MF_RESET] )
  4651. return false;
  4652. break;
  4653. }
  4654. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4655. return false; // Mercenary Scrolls
  4656. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4657. if( pc_is90overweight(sd) ) {
  4658. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4659. return false;
  4660. }
  4661. if( !pc_inventoryblank(sd) ) {
  4662. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4663. return false;
  4664. }
  4665. }
  4666. //Gender check
  4667. if(item->sex != 2 && sd->status.sex != item->sex)
  4668. return false;
  4669. //Required level check
  4670. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4671. return false;
  4672. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4673. return false;
  4674. //Not equipable by class. [Skotlex]
  4675. if (!(
  4676. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4677. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4678. ))
  4679. return false;
  4680. if (sd->sc.count && (
  4681. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4682. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4683. sd->sc.data[SC_TRICKDEAD] ||
  4684. sd->sc.data[SC_HIDING] ||
  4685. sd->sc.data[SC__SHADOWFORM] ||
  4686. sd->sc.data[SC__INVISIBILITY] ||
  4687. sd->sc.data[SC__MANHOLE] ||
  4688. sd->sc.data[SC_DEEPSLEEP] ||
  4689. sd->sc.data[SC_CRYSTALIZE] ||
  4690. sd->sc.data[SC_KAGEHUMI] ||
  4691. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4692. sd->sc.data[SC_KINGS_GRACE] ||
  4693. sd->sc.data[SC_SUHIDE]))
  4694. return false;
  4695. if (!pc_isItemClass(sd,item))
  4696. return false;
  4697. //Dead Branch items
  4698. if( item->flag.dead_branch )
  4699. log_branch(sd);
  4700. return true;
  4701. }
  4702. /*==========================================
  4703. * Last checks to use an item.
  4704. * Return:
  4705. * 0 = fail
  4706. * 1 = success
  4707. *------------------------------------------*/
  4708. int pc_useitem(struct map_session_data *sd,int n)
  4709. {
  4710. t_tick tick = gettick();
  4711. int amount;
  4712. unsigned short nameid;
  4713. struct script_code *script;
  4714. struct item item;
  4715. struct item_data *id;
  4716. nullpo_ret(sd);
  4717. if (sd->state.mail_writing)
  4718. return 0;
  4719. if (sd->npc_id) {
  4720. if (sd->progressbar.npc_id) {
  4721. clif_progressbar_abort(sd);
  4722. return 0; // First item use attempt cancels the progress bar
  4723. }
  4724. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4725. #ifdef RENEWAL
  4726. clif_msg( sd, WORK_IN_PROGRESS );
  4727. #endif
  4728. return 0;
  4729. }
  4730. }
  4731. item = sd->inventory.u.items_inventory[n];
  4732. id = sd->inventory_data[n];
  4733. if (item.nameid == 0 || item.amount <= 0)
  4734. return 0;
  4735. if( !pc_isUseitem(sd,n) )
  4736. return 0;
  4737. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4738. nameid = id->nameid;
  4739. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4740. return 0;
  4741. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4742. if( id->flag.delay_consume ) {
  4743. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4744. return 0;
  4745. else if( pc_issit(sd) )
  4746. return 0;
  4747. }
  4748. //Since most delay-consume items involve using a "skill-type" target cursor,
  4749. //perform a skill-use check before going through. [Skotlex]
  4750. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4751. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  4752. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4753. return 0;
  4754. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4755. return 0;
  4756. /* on restricted maps the item is consumed but the effect is not used */
  4757. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4758. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4759. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4760. clif_useitemack(sd,n,item.amount-1,true);
  4761. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4762. }
  4763. return 0;/* regardless, effect is not run */
  4764. }
  4765. sd->itemid = item.nameid;
  4766. sd->itemindex = n;
  4767. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4768. sd->catch_target_class = PET_CATCH_FAIL;
  4769. amount = item.amount;
  4770. script = id->script;
  4771. //Check if the item is to be consumed immediately [Skotlex]
  4772. if (id->flag.delay_consume)
  4773. clif_useitemack(sd, n, amount, true);
  4774. else
  4775. {
  4776. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4777. {
  4778. clif_useitemack(sd, n, amount - 1, true);
  4779. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4780. }
  4781. else
  4782. clif_useitemack(sd, n, 0, false);
  4783. }
  4784. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4785. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4786. //Update item use time.
  4787. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4788. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4789. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4790. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4791. potion_flag = 0;
  4792. return 1;
  4793. }
  4794. /**
  4795. * Add item on cart for given index.
  4796. * @param sd
  4797. * @param item
  4798. * @param amount
  4799. * @param log_type
  4800. * @return See pc.hpp::e_additem_result
  4801. */
  4802. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4803. {
  4804. struct item_data *data;
  4805. int i,w;
  4806. nullpo_retr(ADDITEM_INVALID, sd);
  4807. nullpo_retr(ADDITEM_INVALID, item);
  4808. if(item->nameid == 0 || amount <= 0)
  4809. return ADDITEM_INVALID;
  4810. if (itemdb_ishatched_egg(item))
  4811. return ADDITEM_INVALID;
  4812. data = itemdb_search(item->nameid);
  4813. if( data->stack.cart && amount > data->stack.amount )
  4814. {// item stack limitation
  4815. return ADDITEM_STACKLIMIT;
  4816. }
  4817. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4818. { // Check item trade restrictions [Skotlex]
  4819. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4820. return ADDITEM_INVALID;
  4821. }
  4822. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4823. return ADDITEM_OVERWEIGHT;
  4824. i = MAX_CART;
  4825. if( itemdb_isstackable2(data) && !item->expire_time )
  4826. {
  4827. for (i = 0; i < MAX_CART; i++) {
  4828. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4829. && sd->cart.u.items_cart[i].bound == item->bound
  4830. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4831. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4832. )
  4833. break;
  4834. }
  4835. }
  4836. if( i < MAX_CART )
  4837. {// item already in cart, stack it
  4838. 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 ) )
  4839. return ADDITEM_OVERAMOUNT; // no slot
  4840. sd->cart.u.items_cart[i].amount += amount;
  4841. clif_cart_additem(sd,i,amount,0);
  4842. }
  4843. else
  4844. {// item not stackable or not present, add it
  4845. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4846. if( i == MAX_CART )
  4847. return ADDITEM_OVERAMOUNT; // no slot
  4848. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4849. sd->cart.u.items_cart[i].id = 0;
  4850. sd->cart.u.items_cart[i].amount = amount;
  4851. sd->cart_num++;
  4852. clif_cart_additem(sd,i,amount,0);
  4853. }
  4854. sd->cart.u.items_cart[i].favorite = 0; // clear
  4855. sd->cart.u.items_cart[i].equipSwitch = 0;
  4856. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4857. sd->cart_weight += w;
  4858. clif_updatestatus(sd,SP_CARTINFO);
  4859. return ADDITEM_SUCCESS;
  4860. }
  4861. /*==========================================
  4862. * Delete item on cart for given index.
  4863. *------------------------------------------*/
  4864. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4865. {
  4866. nullpo_retv(sd);
  4867. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4868. sd->cart.u.items_cart[n].amount < amount)
  4869. return;
  4870. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4871. sd->cart.u.items_cart[n].amount -= amount;
  4872. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4873. if(sd->cart.u.items_cart[n].amount <= 0) {
  4874. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4875. sd->cart_num--;
  4876. }
  4877. if(!type) {
  4878. clif_cart_delitem(sd,n,amount);
  4879. clif_updatestatus(sd,SP_CARTINFO);
  4880. }
  4881. }
  4882. /*==========================================
  4883. * Transfer item from inventory to cart.
  4884. *------------------------------------------*/
  4885. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4886. {
  4887. nullpo_retv(sd);
  4888. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4889. return;
  4890. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  4891. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  4892. return;
  4893. if( item_data->equipSwitch ){
  4894. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  4895. return;
  4896. }
  4897. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  4898. if (flag == ADDITEM_SUCCESS)
  4899. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4900. else {
  4901. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  4902. clif_additem(sd, idx, amount, 0);
  4903. clif_delitem(sd, idx, amount, 0);
  4904. }
  4905. }
  4906. /*==========================================
  4907. * Get number of item in cart.
  4908. * Return:
  4909. -1 = itemid not found or no amount found
  4910. x = remaining itemid on cart after get
  4911. *------------------------------------------*/
  4912. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4913. {
  4914. struct item* item_data;
  4915. nullpo_retr(-1, sd);
  4916. item_data = &sd->cart.u.items_cart[idx];
  4917. if( item_data->nameid == 0 || item_data->amount == 0 )
  4918. return -1;
  4919. return item_data->amount - amount;
  4920. }
  4921. /*==========================================
  4922. * Retrieve an item at index idx from cart.
  4923. *------------------------------------------*/
  4924. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4925. {
  4926. nullpo_retv(sd);
  4927. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4928. return;
  4929. struct item *item_data=&sd->cart.u.items_cart[idx];
  4930. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  4931. return;
  4932. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  4933. if (flag == ADDITEM_SUCCESS)
  4934. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  4935. else {
  4936. clif_cart_delitem(sd, idx, amount);
  4937. clif_additem(sd, idx, amount, flag);
  4938. clif_cart_additem(sd, idx, amount, 0);
  4939. }
  4940. }
  4941. /*==========================================
  4942. * Bound Item Check
  4943. * Type:
  4944. * 1 Account Bound
  4945. * 2 Guild Bound
  4946. * 3 Party Bound
  4947. * 4 Character Bound
  4948. *------------------------------------------*/
  4949. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4950. {
  4951. int i = 0, j = 0;
  4952. for(i = 0; i < MAX_INVENTORY; i++) {
  4953. 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) {
  4954. idxlist[j] = i;
  4955. j++;
  4956. }
  4957. }
  4958. return j;
  4959. }
  4960. /*==========================================
  4961. * Display item stolen msg to player sd
  4962. *------------------------------------------*/
  4963. int pc_show_steal(struct block_list *bl,va_list ap)
  4964. {
  4965. struct map_session_data *sd;
  4966. int itemid;
  4967. struct item_data *item=NULL;
  4968. char output[100];
  4969. sd=va_arg(ap,struct map_session_data *);
  4970. itemid=va_arg(ap,int);
  4971. if((item=itemdb_exists(itemid))==NULL)
  4972. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4973. else
  4974. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4975. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4976. return 0;
  4977. }
  4978. /**
  4979. * Steal an item from bl (mob).
  4980. * @param sd: Player data
  4981. * @param bl: Object to steal from
  4982. * @param skill_lv: Level of skill used
  4983. * @return True on success or false otherwise
  4984. */
  4985. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4986. {
  4987. int i,itemid;
  4988. double rate;
  4989. unsigned char flag = 0;
  4990. struct status_data *sd_status, *md_status;
  4991. struct mob_data *md;
  4992. struct item tmp_item;
  4993. if(!sd || !bl || bl->type!=BL_MOB)
  4994. return false;
  4995. md = (TBL_MOB *)bl;
  4996. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4997. return false;
  4998. sd_status= status_get_status_data(&sd->bl);
  4999. md_status= status_get_status_data(bl);
  5000. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  5001. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5002. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5003. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5004. ) { //Can't steal from
  5005. md->state.steal_flag = UCHAR_MAX;
  5006. return false;
  5007. }
  5008. // base skill success chance (percentual)
  5009. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5010. rate += sd->bonus.add_steal_rate;
  5011. if( rate < 1
  5012. #ifdef RENEWAL
  5013. || rnd()%100 >= rate
  5014. #endif
  5015. )
  5016. return false;
  5017. // Try dropping one item, in the order from first to last possible slot.
  5018. // Droprate is affected by the skill success rate.
  5019. for( i = 0; i < MAX_STEAL_DROP; i++ )
  5020. 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
  5021. #ifndef RENEWAL
  5022. * rate/100.
  5023. #endif
  5024. )
  5025. break;
  5026. if( i == MAX_STEAL_DROP )
  5027. return false;
  5028. itemid = md->db->dropitem[i].nameid;
  5029. memset(&tmp_item,0,sizeof(tmp_item));
  5030. tmp_item.nameid = itemid;
  5031. tmp_item.amount = 1;
  5032. tmp_item.identify = itemdb_isidentified(itemid);
  5033. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  5034. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5035. //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?
  5036. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5037. if(flag) { //Failed to steal due to overweight
  5038. clif_additem(sd,0,0,flag);
  5039. return false;
  5040. }
  5041. if(battle_config.show_steal_in_same_party)
  5042. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5043. //Logs items, Stolen from mobs [Lupus]
  5044. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5045. //A Rare Steal Global Announce by Lupus
  5046. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  5047. struct item_data *i_data;
  5048. char message[128];
  5049. i_data = itemdb_search(itemid);
  5050. 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);
  5051. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5052. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5053. }
  5054. return true;
  5055. }
  5056. /*==========================================
  5057. * Stole zeny from bl (mob)
  5058. * return
  5059. * 0 = fail
  5060. * 1 = success
  5061. *------------------------------------------*/
  5062. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  5063. {
  5064. int rate, target_lv;
  5065. struct mob_data *md;
  5066. if(!sd || !target || target->type != BL_MOB)
  5067. return 0;
  5068. md = (TBL_MOB*)target;
  5069. target_lv = status_get_lv(target);
  5070. 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)
  5071. return 0;
  5072. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5073. if(rnd()%1000 < rate)
  5074. {
  5075. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5076. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5077. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5078. md->state.steal_coin_flag = 1;
  5079. return 1;
  5080. }
  5081. return 0;
  5082. }
  5083. /*==========================================
  5084. * Set's a player position.
  5085. * @param sd
  5086. * @param mapindex
  5087. * @param x
  5088. * @param y
  5089. * @param clrtype
  5090. * @return SETPOS_OK Success
  5091. * SETPOS_MAPINDEX Invalid map index
  5092. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5093. * SETPOS_AUTOTRADE Player is in autotrade state
  5094. *------------------------------------------*/
  5095. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5096. {
  5097. nullpo_retr(SETPOS_OK,sd);
  5098. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5099. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5100. return SETPOS_MAPINDEX;
  5101. }
  5102. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5103. return SETPOS_AUTOTRADE;
  5104. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5105. pc_setstand(sd, true);
  5106. pc_setrestartvalue(sd,1);
  5107. }
  5108. int16 m = map_mapindex2mapid(mapindex);
  5109. struct map_data *mapdata = map_getmapdata(m);
  5110. sd->state.changemap = (sd->mapindex != mapindex);
  5111. sd->state.warping = 1;
  5112. sd->state.workinprogress = WIP_DISABLE_NONE;
  5113. if( sd->state.changemap ) { // Misc map-changing settings
  5114. unsigned short curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5115. if (curr_map_instance_id != new_map_instance_id) {
  5116. if (curr_map_instance_id) // Update instance timer for the map on leave
  5117. instance_delusers(curr_map_instance_id);
  5118. if (new_map_instance_id) // Update instance timer for the map on enter
  5119. instance_addusers(new_map_instance_id);
  5120. }
  5121. if (sd->bg_id && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5122. bg_team_leave(sd, false, true);
  5123. sd->state.pmap = sd->bl.m;
  5124. if (sd->sc.count) { // Cancel some map related stuff.
  5125. if (sd->sc.data[SC_JAILED])
  5126. return SETPOS_MAPINDEX; //You may not get out!
  5127. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  5128. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  5129. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  5130. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  5131. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  5132. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  5133. if (sd->sc.data[SC_KNOWLEDGE]) {
  5134. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  5135. if (sce->timer != INVALID_TIMER)
  5136. delete_timer(sce->timer, status_change_timer);
  5137. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  5138. }
  5139. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  5140. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  5141. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  5142. }
  5143. for(int i = 0; i < EQI_MAX; i++ ) {
  5144. if( sd->equip_index[i] >= 0 )
  5145. if( pc_isequip(sd,sd->equip_index[i]) )
  5146. pc_unequipitem(sd,sd->equip_index[i],2);
  5147. }
  5148. if (battle_config.clear_unit_onwarp&BL_PC)
  5149. skill_clear_unitgroup(&sd->bl);
  5150. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5151. guild_send_dot_remove(sd);
  5152. bg_send_dot_remove(sd);
  5153. if (sd->regen.state.gc)
  5154. sd->regen.state.gc = 0;
  5155. // make sure vending is allowed here
  5156. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5157. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  5158. vending_closevending(sd);
  5159. }
  5160. channel_pcquit(sd,4); //quit map chan
  5161. // Remove Cloaked NPC if changing to another map
  5162. for (auto it = sd->cloaked_npc.begin(); it != sd->cloaked_npc.end(); ++it) {
  5163. block_list *npc_bl = map_id2bl(*it);
  5164. if (npc_bl && npc_bl->m != m) {
  5165. sd->cloaked_npc.erase(it);
  5166. break;
  5167. }
  5168. }
  5169. }
  5170. if( m < 0 )
  5171. {
  5172. uint32 ip;
  5173. uint16 port;
  5174. struct script_state *st;
  5175. //if can't find any map-servers, just abort setting position.
  5176. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5177. return SETPOS_NO_MAPSERVER;
  5178. if (sd->npc_id){
  5179. npc_event_dequeue(sd,false);
  5180. st = sd->st;
  5181. }else{
  5182. st = nullptr;
  5183. }
  5184. npc_script_event(sd, NPCE_LOGOUT);
  5185. //remove from map, THEN change x/y coordinates
  5186. unit_remove_map_pc(sd,clrtype);
  5187. sd->mapindex = mapindex;
  5188. sd->bl.x=x;
  5189. sd->bl.y=y;
  5190. pc_clean_skilltree(sd);
  5191. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5192. chrif_changemapserver(sd, ip, (short)port);
  5193. //Free session data from this map server [Kevin]
  5194. unit_free_pc(sd);
  5195. if( st ){
  5196. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5197. st->state = END;
  5198. }
  5199. return SETPOS_OK;
  5200. }
  5201. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5202. {
  5203. 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);
  5204. x = y = 0; // make it random
  5205. }
  5206. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5207. int c=0;
  5208. do {
  5209. x = rnd()%(mapdata->xs-2)+1;
  5210. y = rnd()%(mapdata->ys-2)+1;
  5211. c++;
  5212. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5213. 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));
  5214. return SETPOS_OK; //preventing warp
  5215. //break; //allow warp anyway
  5216. }
  5217. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5218. }
  5219. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5220. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5221. vending_closevending(sd);
  5222. }
  5223. if(sd->bl.prev != NULL){
  5224. unit_remove_map_pc(sd,clrtype);
  5225. clif_changemap(sd,m,x,y); // [MouseJstr]
  5226. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5227. sd->state.rewarp = 1;
  5228. sd->mapindex = mapindex;
  5229. sd->bl.m = m;
  5230. sd->bl.x = sd->ud.to_x = x;
  5231. sd->bl.y = sd->ud.to_y = y;
  5232. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5233. { // Increased guild castle regen [Valaris]
  5234. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5235. if(gc && gc->guild_id == sd->status.guild_id)
  5236. sd->regen.state.gc = 1;
  5237. }
  5238. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5239. {
  5240. sd->pd->bl.m = m;
  5241. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5242. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5243. sd->pd->ud.dir = sd->ud.dir;
  5244. }
  5245. if( hom_is_active(sd->hd) )
  5246. {
  5247. sd->hd->bl.m = m;
  5248. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5249. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5250. sd->hd->ud.dir = sd->ud.dir;
  5251. }
  5252. if( sd->md )
  5253. {
  5254. sd->md->bl.m = m;
  5255. sd->md->bl.x = sd->md->ud.to_x = x;
  5256. sd->md->bl.y = sd->md->ud.to_y = y;
  5257. sd->md->ud.dir = sd->ud.dir;
  5258. }
  5259. if( sd->ed ) {
  5260. sd->ed->bl.m = m;
  5261. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5262. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5263. sd->ed->ud.dir = sd->ud.dir;
  5264. }
  5265. pc_cell_basilica(sd);
  5266. //check if we gonna be rewarped [lighta]
  5267. if(npc_check_areanpc(1,m,x,y,0)){
  5268. sd->count_rewarp++;
  5269. }
  5270. else
  5271. sd->count_rewarp = 0;
  5272. return SETPOS_OK;
  5273. }
  5274. /*==========================================
  5275. * Warp player sd to random location on current map.
  5276. * May fail if no walkable cell found (1000 attempts).
  5277. * Return:
  5278. * 0 = Success
  5279. * 1,2,3 = Fail
  5280. *------------------------------------------*/
  5281. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5282. {
  5283. int x,y,i=0;
  5284. nullpo_ret(sd);
  5285. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5286. if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
  5287. return 3;
  5288. do {
  5289. x = rnd()%(mapdata->xs-2)+1;
  5290. y = rnd()%(mapdata->ys-2)+1;
  5291. } 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);
  5292. if (i < 1000)
  5293. return pc_setpos(sd,mapdata->index,x,y,type);
  5294. return 3;
  5295. }
  5296. /*==========================================
  5297. * Records a memo point at sd's current position
  5298. * pos - entry to replace, (-1: shift oldest entry out)
  5299. *------------------------------------------*/
  5300. bool pc_memo(struct map_session_data* sd, int pos)
  5301. {
  5302. int skill;
  5303. nullpo_ret(sd);
  5304. // check mapflags
  5305. 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) ) {
  5306. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5307. return false;
  5308. }
  5309. // check inputs
  5310. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5311. return false; // invalid input
  5312. // check required skill level
  5313. skill = pc_checkskill(sd, AL_WARP);
  5314. if( skill < 1 ) {
  5315. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5316. return false;
  5317. }
  5318. if( skill < 2 || skill - 2 < pos ) {
  5319. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5320. return false;
  5321. }
  5322. if( pos == -1 )
  5323. {
  5324. uint8 i;
  5325. // prevent memo-ing the same map multiple times
  5326. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5327. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5328. pos = 0;
  5329. }
  5330. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5331. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5332. return false;
  5333. }
  5334. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5335. sd->status.memo_point[pos].x = sd->bl.x;
  5336. sd->status.memo_point[pos].y = sd->bl.y;
  5337. clif_skill_memomessage(sd, 0);
  5338. return true;
  5339. }
  5340. //
  5341. // Skills
  5342. //
  5343. /**
  5344. * Get the skill current cooldown for player.
  5345. * (get the db base cooldown for skill + player specific cooldown)
  5346. * @param sd : player pointer
  5347. * @param id : skill id
  5348. * @param lv : skill lv
  5349. * @return player skill cooldown
  5350. */
  5351. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5352. if (skill_id == SJ_NOVAEXPLOSING) {
  5353. struct status_change *sc = status_get_sc(&sd->bl);
  5354. if (sc && sc->data[SC_DIMENSION])
  5355. return 0;
  5356. }
  5357. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  5358. if (cooldown == 0)
  5359. return 0;
  5360. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5361. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5362. for (auto &it : sd->skillcooldown) {
  5363. if (it.id == skill_id) {
  5364. cooldown += it.val;
  5365. cooldown = max(0, cooldown);
  5366. break;
  5367. }
  5368. }
  5369. return cooldown;
  5370. }
  5371. /*==========================================
  5372. * Return player sd skill_lv learned for given skill
  5373. *------------------------------------------*/
  5374. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5375. {
  5376. uint16 idx = 0;
  5377. if (sd == NULL)
  5378. return 0;
  5379. if ((idx = skill_get_index(skill_id)) == 0) {
  5380. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5381. return 0;
  5382. }
  5383. if (SKILL_CHK_GUILD(skill_id) ) {
  5384. struct guild *g;
  5385. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5386. return guild_checkskill(g,skill_id);
  5387. return 0;
  5388. }
  5389. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5390. }
  5391. /**
  5392. * Check if we still have the correct weapon to continue the skill (actually status)
  5393. * If not ending it
  5394. * @param sd
  5395. * @return 0:error, 1:check done
  5396. */
  5397. static void pc_checkallowskill(struct map_session_data *sd)
  5398. {
  5399. const enum sc_type scw_list[] = {
  5400. SC_TWOHANDQUICKEN,
  5401. SC_ONEHAND,
  5402. SC_AURABLADE,
  5403. SC_PARRYING,
  5404. SC_SPEARQUICKEN,
  5405. SC_ADRENALINE,
  5406. SC_ADRENALINE2,
  5407. SC_DANCING,
  5408. SC_GATLINGFEVER,
  5409. };
  5410. uint8 i;
  5411. nullpo_retv(sd);
  5412. if(!sd->sc.count)
  5413. return;
  5414. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5415. { // Skills requiring specific weapon types
  5416. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5417. continue;
  5418. if(sd->sc.data[scw_list[i]] &&
  5419. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5420. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5421. }
  5422. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5423. // Spurt requires bare hands (feet, in fact xD)
  5424. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5425. if(sd->status.shield <= 0) { // Skills requiring a shield
  5426. const enum sc_type scs_list[] = {
  5427. SC_AUTOGUARD,
  5428. SC_DEFENDER,
  5429. SC_REFLECTSHIELD,
  5430. SC_REFLECTDAMAGE
  5431. };
  5432. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5433. if(sd->sc.data[scs_list[i]])
  5434. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5435. }
  5436. }
  5437. /*==========================================
  5438. * Return equipped index of item on player sd at pos
  5439. * Return
  5440. * -1 : Nothing equipped
  5441. * idx : (this index could be used in inventory to found item_data)
  5442. *------------------------------------------*/
  5443. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  5444. {
  5445. uint8 i;
  5446. nullpo_retr(-1, sd);
  5447. for(i=0;i<EQI_MAX;i++){
  5448. if(pos & equip_bitmask[i]){
  5449. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  5450. // Check all if any match is found
  5451. continue;
  5452. }
  5453. return sd->equip_index[i];
  5454. }
  5455. }
  5456. return -1;
  5457. }
  5458. /*==========================================
  5459. * Check if sd has nameid equipped somewhere
  5460. * @sd : the player session
  5461. * @nameid : id of the item to check
  5462. * @min : : see pc.hpp enum equip_index from ? to @max
  5463. * @max : see pc.hpp enum equip_index for @min to ?
  5464. * -return true,false
  5465. *------------------------------------------*/
  5466. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5467. {
  5468. int i;
  5469. for(i = min; i < max; i++) {
  5470. if(equip_bitmask[i]) {
  5471. int idx = sd->equip_index[i];
  5472. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5473. return true;
  5474. }
  5475. }
  5476. return false;
  5477. }
  5478. /*==========================================
  5479. * Convert's from the client's lame Job ID system
  5480. * to the map server's 'makes sense' system. [Skotlex]
  5481. *------------------------------------------*/
  5482. int pc_jobid2mapid(unsigned short b_class)
  5483. {
  5484. switch(b_class)
  5485. {
  5486. //Novice And 1-1 Jobs
  5487. case JOB_NOVICE: return MAPID_NOVICE;
  5488. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5489. case JOB_MAGE: return MAPID_MAGE;
  5490. case JOB_ARCHER: return MAPID_ARCHER;
  5491. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5492. case JOB_MERCHANT: return MAPID_MERCHANT;
  5493. case JOB_THIEF: return MAPID_THIEF;
  5494. case JOB_TAEKWON: return MAPID_TAEKWON;
  5495. case JOB_WEDDING: return MAPID_WEDDING;
  5496. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5497. case JOB_NINJA: return MAPID_NINJA;
  5498. case JOB_XMAS: return MAPID_XMAS;
  5499. case JOB_SUMMER: return MAPID_SUMMER;
  5500. case JOB_HANBOK: return MAPID_HANBOK;
  5501. case JOB_GANGSI: return MAPID_GANGSI;
  5502. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5503. case JOB_SUMMER2: return MAPID_SUMMER2;
  5504. //2-1 Jobs
  5505. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5506. case JOB_KNIGHT: return MAPID_KNIGHT;
  5507. case JOB_WIZARD: return MAPID_WIZARD;
  5508. case JOB_HUNTER: return MAPID_HUNTER;
  5509. case JOB_PRIEST: return MAPID_PRIEST;
  5510. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5511. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5512. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5513. case JOB_KAGEROU:
  5514. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5515. case JOB_REBELLION: return MAPID_REBELLION;
  5516. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5517. //2-2 Jobs
  5518. case JOB_CRUSADER: return MAPID_CRUSADER;
  5519. case JOB_SAGE: return MAPID_SAGE;
  5520. case JOB_BARD:
  5521. case JOB_DANCER: return MAPID_BARDDANCER;
  5522. case JOB_MONK: return MAPID_MONK;
  5523. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5524. case JOB_ROGUE: return MAPID_ROGUE;
  5525. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5526. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5527. //Trans Novice And Trans 1-1 Jobs
  5528. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5529. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5530. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5531. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5532. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5533. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5534. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5535. //Trans 2-1 Jobs
  5536. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5537. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5538. case JOB_SNIPER: return MAPID_SNIPER;
  5539. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5540. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5541. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5542. //Trans 2-2 Jobs
  5543. case JOB_PALADIN: return MAPID_PALADIN;
  5544. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5545. case JOB_CLOWN:
  5546. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5547. case JOB_CHAMPION: return MAPID_CHAMPION;
  5548. case JOB_CREATOR: return MAPID_CREATOR;
  5549. case JOB_STALKER: return MAPID_STALKER;
  5550. //Baby Novice And Baby 1-1 Jobs
  5551. case JOB_BABY: return MAPID_BABY;
  5552. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5553. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5554. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5555. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5556. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5557. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5558. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5559. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5560. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5561. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5562. //Baby 2-1 Jobs
  5563. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5564. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5565. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5566. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5567. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5568. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5569. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5570. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5571. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5572. case JOB_BABY_KAGEROU:
  5573. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5574. //Baby 2-2 Jobs
  5575. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5576. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5577. case JOB_BABY_BARD:
  5578. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5579. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5580. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5581. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5582. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5583. //3-1 Jobs
  5584. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5585. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5586. case JOB_WARLOCK: return MAPID_WARLOCK;
  5587. case JOB_RANGER: return MAPID_RANGER;
  5588. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5589. case JOB_MECHANIC: return MAPID_MECHANIC;
  5590. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5591. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5592. //3-2 Jobs
  5593. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5594. case JOB_SORCERER: return MAPID_SORCERER;
  5595. case JOB_MINSTREL:
  5596. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5597. case JOB_SURA: return MAPID_SURA;
  5598. case JOB_GENETIC: return MAPID_GENETIC;
  5599. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5600. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5601. //Trans 3-1 Jobs
  5602. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5603. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5604. case JOB_RANGER_T: return MAPID_RANGER_T;
  5605. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5606. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5607. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5608. //Trans 3-2 Jobs
  5609. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5610. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5611. case JOB_MINSTREL_T:
  5612. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5613. case JOB_SURA_T: return MAPID_SURA_T;
  5614. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5615. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5616. //Baby 3-1 Jobs
  5617. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5618. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  5619. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5620. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5621. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  5622. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5623. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  5624. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5625. //Baby 3-2 Jobs
  5626. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  5627. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5628. case JOB_BABY_MINSTREL:
  5629. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5630. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5631. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5632. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  5633. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5634. //Doram Jobs
  5635. case JOB_SUMMONER: return MAPID_SUMMONER;
  5636. default:
  5637. return -1;
  5638. }
  5639. }
  5640. //Reverts the map-style class id to the client-style one.
  5641. int pc_mapid2jobid(unsigned short class_, int sex)
  5642. {
  5643. switch(class_) {
  5644. //Novice And 1-1 Jobs
  5645. case MAPID_NOVICE: return JOB_NOVICE;
  5646. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5647. case MAPID_MAGE: return JOB_MAGE;
  5648. case MAPID_ARCHER: return JOB_ARCHER;
  5649. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5650. case MAPID_MERCHANT: return JOB_MERCHANT;
  5651. case MAPID_THIEF: return JOB_THIEF;
  5652. case MAPID_TAEKWON: return JOB_TAEKWON;
  5653. case MAPID_WEDDING: return JOB_WEDDING;
  5654. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5655. case MAPID_NINJA: return JOB_NINJA;
  5656. case MAPID_XMAS: return JOB_XMAS;
  5657. case MAPID_SUMMER: return JOB_SUMMER;
  5658. case MAPID_HANBOK: return JOB_HANBOK;
  5659. case MAPID_GANGSI: return JOB_GANGSI;
  5660. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5661. case MAPID_SUMMER2: return JOB_SUMMER2;
  5662. //2-1 Jobs
  5663. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5664. case MAPID_KNIGHT: return JOB_KNIGHT;
  5665. case MAPID_WIZARD: return JOB_WIZARD;
  5666. case MAPID_HUNTER: return JOB_HUNTER;
  5667. case MAPID_PRIEST: return JOB_PRIEST;
  5668. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5669. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5670. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5671. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5672. case MAPID_REBELLION: return JOB_REBELLION;
  5673. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5674. //2-2 Jobs
  5675. case MAPID_CRUSADER: return JOB_CRUSADER;
  5676. case MAPID_SAGE: return JOB_SAGE;
  5677. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5678. case MAPID_MONK: return JOB_MONK;
  5679. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5680. case MAPID_ROGUE: return JOB_ROGUE;
  5681. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5682. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5683. //Trans Novice And Trans 2-1 Jobs
  5684. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5685. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5686. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5687. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5688. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5689. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5690. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5691. //Trans 2-1 Jobs
  5692. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5693. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5694. case MAPID_SNIPER: return JOB_SNIPER;
  5695. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5696. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5697. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5698. //Trans 2-2 Jobs
  5699. case MAPID_PALADIN: return JOB_PALADIN;
  5700. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5701. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5702. case MAPID_CHAMPION: return JOB_CHAMPION;
  5703. case MAPID_CREATOR: return JOB_CREATOR;
  5704. case MAPID_STALKER: return JOB_STALKER;
  5705. //Baby Novice And Baby 1-1 Jobs
  5706. case MAPID_BABY: return JOB_BABY;
  5707. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5708. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5709. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5710. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5711. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5712. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5713. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5714. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5715. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5716. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5717. //Baby 2-1 Jobs
  5718. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5719. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5720. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5721. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5722. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5723. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5724. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5725. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5726. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5727. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5728. //Baby 2-2 Jobs
  5729. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5730. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5731. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5732. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5733. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5734. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5735. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5736. //3-1 Jobs
  5737. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5738. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5739. case MAPID_WARLOCK: return JOB_WARLOCK;
  5740. case MAPID_RANGER: return JOB_RANGER;
  5741. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5742. case MAPID_MECHANIC: return JOB_MECHANIC;
  5743. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5744. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5745. //3-2 Jobs
  5746. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5747. case MAPID_SORCERER: return JOB_SORCERER;
  5748. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5749. case MAPID_SURA: return JOB_SURA;
  5750. case MAPID_GENETIC: return JOB_GENETIC;
  5751. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5752. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5753. //Trans 3-1 Jobs
  5754. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5755. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5756. case MAPID_RANGER_T: return JOB_RANGER_T;
  5757. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5758. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5759. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5760. //Trans 3-2 Jobs
  5761. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5762. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5763. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5764. case MAPID_SURA_T: return JOB_SURA_T;
  5765. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5766. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5767. //Baby 3-1 Jobs
  5768. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5769. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  5770. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5771. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5772. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  5773. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5774. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  5775. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5776. //Baby 3-2 Jobs
  5777. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  5778. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5779. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5780. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5781. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5782. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  5783. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5784. //Doram Jobs
  5785. case MAPID_SUMMONER: return JOB_SUMMONER;
  5786. default:
  5787. return -1;
  5788. }
  5789. }
  5790. /*====================================================
  5791. * This function return the name of the job (by [Yor])
  5792. *----------------------------------------------------*/
  5793. const char* job_name(int class_)
  5794. {
  5795. switch (class_) {
  5796. case JOB_NOVICE:
  5797. case JOB_SWORDMAN:
  5798. case JOB_MAGE:
  5799. case JOB_ARCHER:
  5800. case JOB_ACOLYTE:
  5801. case JOB_MERCHANT:
  5802. case JOB_THIEF:
  5803. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5804. case JOB_KNIGHT:
  5805. case JOB_PRIEST:
  5806. case JOB_WIZARD:
  5807. case JOB_BLACKSMITH:
  5808. case JOB_HUNTER:
  5809. case JOB_ASSASSIN:
  5810. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5811. case JOB_KNIGHT2:
  5812. return msg_txt(NULL,557);
  5813. case JOB_CRUSADER:
  5814. case JOB_MONK:
  5815. case JOB_SAGE:
  5816. case JOB_ROGUE:
  5817. case JOB_ALCHEMIST:
  5818. case JOB_BARD:
  5819. case JOB_DANCER:
  5820. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5821. case JOB_CRUSADER2:
  5822. return msg_txt(NULL,563);
  5823. case JOB_WEDDING:
  5824. case JOB_SUPER_NOVICE:
  5825. case JOB_GUNSLINGER:
  5826. case JOB_NINJA:
  5827. case JOB_XMAS:
  5828. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5829. case JOB_SUMMER:
  5830. case JOB_SUMMER2:
  5831. return msg_txt(NULL,621);
  5832. case JOB_HANBOK:
  5833. return msg_txt(NULL,694);
  5834. case JOB_OKTOBERFEST:
  5835. return msg_txt(NULL,696);
  5836. case JOB_NOVICE_HIGH:
  5837. case JOB_SWORDMAN_HIGH:
  5838. case JOB_MAGE_HIGH:
  5839. case JOB_ARCHER_HIGH:
  5840. case JOB_ACOLYTE_HIGH:
  5841. case JOB_MERCHANT_HIGH:
  5842. case JOB_THIEF_HIGH:
  5843. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5844. case JOB_LORD_KNIGHT:
  5845. case JOB_HIGH_PRIEST:
  5846. case JOB_HIGH_WIZARD:
  5847. case JOB_WHITESMITH:
  5848. case JOB_SNIPER:
  5849. case JOB_ASSASSIN_CROSS:
  5850. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5851. case JOB_LORD_KNIGHT2:
  5852. return msg_txt(NULL,582);
  5853. case JOB_PALADIN:
  5854. case JOB_CHAMPION:
  5855. case JOB_PROFESSOR:
  5856. case JOB_STALKER:
  5857. case JOB_CREATOR:
  5858. case JOB_CLOWN:
  5859. case JOB_GYPSY:
  5860. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5861. case JOB_PALADIN2:
  5862. return msg_txt(NULL,588);
  5863. case JOB_BABY:
  5864. case JOB_BABY_SWORDMAN:
  5865. case JOB_BABY_MAGE:
  5866. case JOB_BABY_ARCHER:
  5867. case JOB_BABY_ACOLYTE:
  5868. case JOB_BABY_MERCHANT:
  5869. case JOB_BABY_THIEF:
  5870. return msg_txt(NULL,595 - JOB_BABY + class_);
  5871. case JOB_BABY_KNIGHT:
  5872. case JOB_BABY_PRIEST:
  5873. case JOB_BABY_WIZARD:
  5874. case JOB_BABY_BLACKSMITH:
  5875. case JOB_BABY_HUNTER:
  5876. case JOB_BABY_ASSASSIN:
  5877. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5878. case JOB_BABY_KNIGHT2:
  5879. return msg_txt(NULL,602);
  5880. case JOB_BABY_CRUSADER:
  5881. case JOB_BABY_MONK:
  5882. case JOB_BABY_SAGE:
  5883. case JOB_BABY_ROGUE:
  5884. case JOB_BABY_ALCHEMIST:
  5885. case JOB_BABY_BARD:
  5886. case JOB_BABY_DANCER:
  5887. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5888. case JOB_BABY_CRUSADER2:
  5889. return msg_txt(NULL,608);
  5890. case JOB_SUPER_BABY:
  5891. return msg_txt(NULL,615);
  5892. case JOB_TAEKWON:
  5893. return msg_txt(NULL,616);
  5894. case JOB_STAR_GLADIATOR:
  5895. case JOB_STAR_GLADIATOR2:
  5896. return msg_txt(NULL,617);
  5897. case JOB_SOUL_LINKER:
  5898. return msg_txt(NULL,618);
  5899. case JOB_GANGSI:
  5900. case JOB_DEATH_KNIGHT:
  5901. case JOB_DARK_COLLECTOR:
  5902. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5903. case JOB_RUNE_KNIGHT:
  5904. case JOB_WARLOCK:
  5905. case JOB_RANGER:
  5906. case JOB_ARCH_BISHOP:
  5907. case JOB_MECHANIC:
  5908. case JOB_GUILLOTINE_CROSS:
  5909. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5910. case JOB_RUNE_KNIGHT_T:
  5911. case JOB_WARLOCK_T:
  5912. case JOB_RANGER_T:
  5913. case JOB_ARCH_BISHOP_T:
  5914. case JOB_MECHANIC_T:
  5915. case JOB_GUILLOTINE_CROSS_T:
  5916. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5917. case JOB_ROYAL_GUARD:
  5918. case JOB_SORCERER:
  5919. case JOB_MINSTREL:
  5920. case JOB_WANDERER:
  5921. case JOB_SURA:
  5922. case JOB_GENETIC:
  5923. case JOB_SHADOW_CHASER:
  5924. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5925. case JOB_ROYAL_GUARD_T:
  5926. case JOB_SORCERER_T:
  5927. case JOB_MINSTREL_T:
  5928. case JOB_WANDERER_T:
  5929. case JOB_SURA_T:
  5930. case JOB_GENETIC_T:
  5931. case JOB_SHADOW_CHASER_T:
  5932. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5933. case JOB_RUNE_KNIGHT2:
  5934. case JOB_RUNE_KNIGHT_T2:
  5935. return msg_txt(NULL,625);
  5936. case JOB_ROYAL_GUARD2:
  5937. case JOB_ROYAL_GUARD_T2:
  5938. return msg_txt(NULL,631);
  5939. case JOB_RANGER2:
  5940. case JOB_RANGER_T2:
  5941. return msg_txt(NULL,627);
  5942. case JOB_MECHANIC2:
  5943. case JOB_MECHANIC_T2:
  5944. return msg_txt(NULL,629);
  5945. case JOB_BABY_RUNE_KNIGHT:
  5946. case JOB_BABY_WARLOCK:
  5947. case JOB_BABY_RANGER:
  5948. case JOB_BABY_ARCH_BISHOP:
  5949. case JOB_BABY_MECHANIC:
  5950. case JOB_BABY_GUILLOTINE_CROSS:
  5951. case JOB_BABY_ROYAL_GUARD:
  5952. case JOB_BABY_SORCERER:
  5953. case JOB_BABY_MINSTREL:
  5954. case JOB_BABY_WANDERER:
  5955. case JOB_BABY_SURA:
  5956. case JOB_BABY_GENETIC:
  5957. case JOB_BABY_SHADOW_CHASER:
  5958. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  5959. case JOB_BABY_RUNE_KNIGHT2:
  5960. return msg_txt(NULL,638);
  5961. case JOB_BABY_ROYAL_GUARD2:
  5962. return msg_txt(NULL,644);
  5963. case JOB_BABY_RANGER2:
  5964. return msg_txt(NULL,640);
  5965. case JOB_BABY_MECHANIC2:
  5966. return msg_txt(NULL,642);
  5967. case JOB_SUPER_NOVICE_E:
  5968. case JOB_SUPER_BABY_E:
  5969. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5970. case JOB_KAGEROU:
  5971. case JOB_OBORO:
  5972. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5973. case JOB_REBELLION:
  5974. return msg_txt(NULL,695);
  5975. case JOB_SUMMONER:
  5976. return msg_txt(NULL,697);
  5977. case JOB_BABY_SUMMONER:
  5978. return msg_txt(NULL,698);
  5979. case JOB_BABY_NINJA:
  5980. return msg_txt(NULL,699);
  5981. case JOB_BABY_KAGEROU:
  5982. case JOB_BABY_OBORO:
  5983. case JOB_BABY_TAEKWON:
  5984. case JOB_BABY_STAR_GLADIATOR:
  5985. case JOB_BABY_SOUL_LINKER:
  5986. case JOB_BABY_GUNSLINGER:
  5987. case JOB_BABY_REBELLION:
  5988. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5989. case JOB_BABY_STAR_GLADIATOR2:
  5990. return msg_txt(NULL,756);
  5991. case JOB_STAR_EMPEROR:
  5992. case JOB_SOUL_REAPER:
  5993. case JOB_BABY_STAR_EMPEROR:
  5994. case JOB_BABY_SOUL_REAPER:
  5995. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  5996. case JOB_STAR_EMPEROR2:
  5997. return msg_txt(NULL,782);
  5998. case JOB_BABY_STAR_EMPEROR2:
  5999. return msg_txt(NULL,784);
  6000. default:
  6001. return msg_txt(NULL,655);
  6002. }
  6003. }
  6004. /*====================================================
  6005. * Timered function to make id follow a target.
  6006. * @id = bl.id (player only atm)
  6007. * target is define in sd->followtarget (bl.id)
  6008. * used by pc_follow
  6009. *----------------------------------------------------*/
  6010. TIMER_FUNC(pc_follow_timer){
  6011. struct map_session_data *sd;
  6012. struct block_list *tbl;
  6013. sd = map_id2sd(id);
  6014. nullpo_ret(sd);
  6015. if (sd->followtimer != tid){
  6016. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6017. sd->followtimer = INVALID_TIMER;
  6018. return 0;
  6019. }
  6020. sd->followtimer = INVALID_TIMER;
  6021. tbl = map_id2bl(sd->followtarget);
  6022. if (tbl == NULL || pc_isdead(sd))
  6023. {
  6024. pc_stop_following(sd);
  6025. return 0;
  6026. }
  6027. // either player or target is currently detached from map blocks (could be teleporting),
  6028. // but still connected to this map, so we'll just increment the timer and check back later
  6029. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  6030. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6031. {
  6032. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  6033. if (!check_distance_bl(&sd->bl, tbl, 5))
  6034. unit_walktobl(&sd->bl, tbl, 5, 0);
  6035. } else
  6036. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  6037. }
  6038. sd->followtimer = add_timer(
  6039. tick + 1000, // increase time a bit to loosen up map's load
  6040. pc_follow_timer, sd->bl.id, 0);
  6041. return 0;
  6042. }
  6043. int pc_stop_following (struct map_session_data *sd)
  6044. {
  6045. nullpo_ret(sd);
  6046. if (sd->followtimer != INVALID_TIMER) {
  6047. delete_timer(sd->followtimer,pc_follow_timer);
  6048. sd->followtimer = INVALID_TIMER;
  6049. }
  6050. sd->followtarget = -1;
  6051. sd->ud.target_to = 0;
  6052. unit_stop_walking(&sd->bl, 1);
  6053. return 0;
  6054. }
  6055. int pc_follow(struct map_session_data *sd,int target_id)
  6056. {
  6057. struct block_list *bl = map_id2bl(target_id);
  6058. if (bl == NULL /*|| bl->type != BL_PC*/)
  6059. return 1;
  6060. if (sd->followtimer != INVALID_TIMER)
  6061. pc_stop_following(sd);
  6062. sd->followtarget = target_id;
  6063. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6064. return 0;
  6065. }
  6066. int pc_checkbaselevelup(struct map_session_data *sd) {
  6067. unsigned int next = pc_nextbaseexp(sd);
  6068. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  6069. return 0;
  6070. do {
  6071. sd->status.base_exp -= next;
  6072. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6073. 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 )
  6074. sd->status.base_exp = next-1;
  6075. next = pc_gets_status_point(sd->status.base_level);
  6076. sd->status.base_level++;
  6077. sd->status.status_point += next;
  6078. if( pc_is_maxbaselv(sd) ){
  6079. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  6080. break;
  6081. }
  6082. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  6083. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  6084. status_calc_pet(sd->pd,SCO_NONE);
  6085. clif_updatestatus(sd,SP_STATUSPOINT);
  6086. clif_updatestatus(sd,SP_BASELEVEL);
  6087. clif_updatestatus(sd,SP_BASEEXP);
  6088. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6089. status_calc_pc(sd,SCO_FORCE);
  6090. status_percent_heal(&sd->bl,100,100);
  6091. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6092. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  6093. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  6094. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  6095. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  6096. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  6097. if (sd->state.snovice_dead_flag)
  6098. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6099. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6100. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  6101. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  6102. }
  6103. clif_misceffect(&sd->bl,0);
  6104. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6105. if(sd->status.party_id)
  6106. party_send_levelup(sd);
  6107. pc_baselevelchanged(sd);
  6108. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  6109. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  6110. return 1;
  6111. }
  6112. void pc_baselevelchanged(struct map_session_data *sd) {
  6113. uint8 i;
  6114. for( i = 0; i < EQI_MAX; i++ ) {
  6115. if( sd->equip_index[i] >= 0 ) {
  6116. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6117. pc_unequipitem(sd, sd->equip_index[i], 3);
  6118. }
  6119. }
  6120. pc_show_questinfo(sd);
  6121. }
  6122. int pc_checkjoblevelup(struct map_session_data *sd)
  6123. {
  6124. unsigned int next = pc_nextjobexp(sd);
  6125. nullpo_ret(sd);
  6126. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6127. return 0;
  6128. do {
  6129. sd->status.job_exp -= next;
  6130. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6131. 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 )
  6132. sd->status.job_exp = next-1;
  6133. sd->status.job_level ++;
  6134. sd->status.skill_point ++;
  6135. if( pc_is_maxjoblv(sd) ){
  6136. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6137. break;
  6138. }
  6139. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6140. clif_updatestatus(sd,SP_JOBLEVEL);
  6141. clif_updatestatus(sd,SP_JOBEXP);
  6142. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6143. clif_updatestatus(sd,SP_SKILLPOINT);
  6144. status_calc_pc(sd,SCO_FORCE);
  6145. clif_misceffect(&sd->bl,1);
  6146. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6147. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6148. npc_script_event(sd, NPCE_JOBLVUP);
  6149. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  6150. pc_show_questinfo(sd);
  6151. return 1;
  6152. }
  6153. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6154. * @param sd Player
  6155. * @param base_exp Base EXP before peronal bonuses
  6156. * @param job_exp Job EXP before peronal bonuses
  6157. * @param src Block list that affecting the exp calculation
  6158. */
  6159. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  6160. {
  6161. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6162. if (src) {
  6163. struct status_data *status = status_get_status_data(src);
  6164. if( sd->expaddrace[status->race] )
  6165. bonus += sd->expaddrace[status->race];
  6166. if( sd->expaddrace[RC_ALL] )
  6167. bonus += sd->expaddrace[RC_ALL];
  6168. if( sd->expaddclass[status->class_] )
  6169. bonus += sd->expaddclass[status->class_];
  6170. if( sd->expaddclass[CLASS_ALL] )
  6171. bonus += sd->expaddclass[CLASS_ALL];
  6172. if (battle_config.pk_mode &&
  6173. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6174. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6175. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6176. vip_bonus_base = battle_config.vip_base_exp_increase;
  6177. vip_bonus_job = battle_config.vip_job_exp_increase;
  6178. }
  6179. }
  6180. // Give EXPBOOST for quests even if src is NULL.
  6181. if (sd->sc.data[SC_EXPBOOST]) {
  6182. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6183. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6184. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6185. }
  6186. if (*base_exp) {
  6187. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  6188. *base_exp = cap_value(exp, 1, UINT_MAX);
  6189. }
  6190. // Give JEXPBOOST for quests even if src is NULL.
  6191. if (sd->sc.data[SC_JEXPBOOST])
  6192. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6193. if (*job_exp) {
  6194. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  6195. *job_exp = cap_value(exp, 1, UINT_MAX);
  6196. }
  6197. return;
  6198. }
  6199. /**
  6200. * Show EXP gained by player in percentage by @showexp
  6201. * @param sd Player
  6202. * @param base_exp Base EXP gained/loss
  6203. * @param next_base_exp Base EXP needed for next base level
  6204. * @param job_exp Job EXP gained/loss
  6205. * @param next_job_exp Job EXP needed for next job level
  6206. * @param lost True:EXP penalty, lose EXP
  6207. **/
  6208. 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) {
  6209. char output[CHAT_SIZE_MAX];
  6210. nullpo_retv(sd);
  6211. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6212. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6213. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6214. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6215. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6216. }
  6217. /**
  6218. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6219. * @param sd Player
  6220. * @param src EXP source
  6221. * @param base_exp Base EXP gained
  6222. * @param base_exp Job EXP gained
  6223. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6224. * @return
  6225. **/
  6226. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  6227. {
  6228. unsigned int nextb = 0, nextj = 0;
  6229. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6230. nullpo_retv(sd);
  6231. if(sd->bl.prev == NULL || pc_isdead(sd))
  6232. return;
  6233. if (!(exp_flag&2)) {
  6234. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6235. return; // no exp on pvp maps
  6236. if (sd->status.guild_id>0)
  6237. base_exp -= guild_payexp(sd,base_exp);
  6238. }
  6239. flag = ((base_exp) ? 1 : 0) |
  6240. ((job_exp) ? 2 : 0) |
  6241. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6242. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6243. if (!(exp_flag&2))
  6244. pc_calcexp(sd, &base_exp, &job_exp, src);
  6245. nextb = pc_nextbaseexp(sd);
  6246. nextj = pc_nextjobexp(sd);
  6247. if (flag&4){
  6248. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6249. base_exp = 0;
  6250. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6251. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6252. }
  6253. if (flag&8){
  6254. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6255. job_exp = 0;
  6256. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6257. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6258. }
  6259. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6260. //Note that this value should never be greater than the original
  6261. //therefore no overflow checks are needed. [Skotlex]
  6262. if (nextb > 0) {
  6263. float nextbp = (float) base_exp / (float) nextb;
  6264. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6265. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6266. }
  6267. if (nextj > 0) {
  6268. float nextjp = (float) job_exp / (float) nextj;
  6269. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6270. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6271. }
  6272. }
  6273. // Give EXP for Base Level
  6274. if (base_exp) {
  6275. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  6276. sd->status.base_exp = UINT32_MAX;
  6277. else
  6278. sd->status.base_exp += base_exp;
  6279. if (!pc_checkbaselevelup(sd))
  6280. clif_updatestatus(sd,SP_BASEEXP);
  6281. }
  6282. // Give EXP for Job Level
  6283. if (job_exp) {
  6284. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  6285. sd->status.job_exp = UINT32_MAX;
  6286. else
  6287. sd->status.job_exp += job_exp;
  6288. if (!pc_checkjoblevelup(sd))
  6289. clif_updatestatus(sd,SP_JOBEXP);
  6290. }
  6291. if (flag&1)
  6292. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6293. if (flag&2)
  6294. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6295. if (sd->state.showexp && (base_exp || job_exp))
  6296. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6297. }
  6298. /**
  6299. * Lost Base/Job EXP from a player
  6300. * @param sd Player
  6301. * @param base_exp Base EXP lost
  6302. * @param job_exp Job EXP lost
  6303. **/
  6304. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  6305. nullpo_retv(sd);
  6306. if (base_exp) {
  6307. base_exp = u32min(sd->status.base_exp, base_exp);
  6308. sd->status.base_exp -= base_exp;
  6309. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6310. clif_updatestatus(sd, SP_BASEEXP);
  6311. }
  6312. if (job_exp) {
  6313. job_exp = u32min(sd->status.job_exp, job_exp);
  6314. sd->status.job_exp -= job_exp;
  6315. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6316. clif_updatestatus(sd, SP_JOBEXP);
  6317. }
  6318. if (sd->state.showexp && (base_exp || job_exp))
  6319. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6320. }
  6321. /**
  6322. * Returns max base level for this character's class.
  6323. * @param class_: Player's class
  6324. * @return Max Base Level
  6325. */
  6326. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6327. return job_info[pc_class2idx(class_)].max_level[0];
  6328. }
  6329. /**
  6330. * Returns max base level for this character.
  6331. * @param sd Player
  6332. * @return Max Base Level
  6333. **/
  6334. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6335. return pc_class_maxbaselv(sd->status.class_);
  6336. }
  6337. /**
  6338. * Returns max job level for this character's class.
  6339. * @param class_: Player's class
  6340. * @return Max Job Level
  6341. */
  6342. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6343. return job_info[pc_class2idx(class_)].max_level[1];
  6344. }
  6345. /**
  6346. * Returns max job level for this character.
  6347. * @param sd Player
  6348. * @return Max Job Level
  6349. **/
  6350. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6351. return pc_class_maxjoblv(sd->status.class_);
  6352. }
  6353. /**
  6354. * Check if player is reached max base level
  6355. * @param sd
  6356. * @return True if reached max level
  6357. **/
  6358. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6359. nullpo_retr(false, sd);
  6360. return (sd->status.base_level >= pc_maxbaselv(sd));
  6361. }
  6362. /**
  6363. * Check if player is reached max base level
  6364. * @param sd
  6365. * @return True if reached max level
  6366. **/
  6367. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6368. nullpo_retr(false, sd);
  6369. return (sd->status.job_level >= pc_maxjoblv(sd));
  6370. }
  6371. /**
  6372. * Base exp needed for player to level up.
  6373. * @param sd
  6374. * @return Base EXP needed for next base level
  6375. **/
  6376. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6377. nullpo_ret(sd);
  6378. if (sd->status.base_level == 0) // Is this something that possible?
  6379. return 0;
  6380. if (pc_is_maxbaselv(sd))
  6381. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6382. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6383. }
  6384. /**
  6385. * Job exp needed for player to level up.
  6386. * @param sd
  6387. * @return Job EXP needed for next job level
  6388. **/
  6389. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6390. nullpo_ret(sd);
  6391. if (sd->status.job_level == 0) // Is this something that possible?
  6392. return 0;
  6393. if (pc_is_maxjoblv(sd))
  6394. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6395. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6396. }
  6397. /// Returns the value of the specified stat.
  6398. static int pc_getstat(struct map_session_data* sd, int type)
  6399. {
  6400. nullpo_retr(-1, sd);
  6401. switch( type ) {
  6402. case SP_STR: return sd->status.str;
  6403. case SP_AGI: return sd->status.agi;
  6404. case SP_VIT: return sd->status.vit;
  6405. case SP_INT: return sd->status.int_;
  6406. case SP_DEX: return sd->status.dex;
  6407. case SP_LUK: return sd->status.luk;
  6408. default:
  6409. return -1;
  6410. }
  6411. }
  6412. /// Sets the specified stat to the specified value.
  6413. /// Returns the new value.
  6414. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6415. {
  6416. nullpo_retr(-1, sd);
  6417. switch( type ) {
  6418. case SP_STR: sd->status.str = val; break;
  6419. case SP_AGI: sd->status.agi = val; break;
  6420. case SP_VIT: sd->status.vit = val; break;
  6421. case SP_INT: sd->status.int_ = val; break;
  6422. case SP_DEX: sd->status.dex = val; break;
  6423. case SP_LUK: sd->status.luk = val; break;
  6424. default:
  6425. return -1;
  6426. }
  6427. return val;
  6428. }
  6429. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6430. int pc_gets_status_point(int level)
  6431. {
  6432. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6433. return (statp[level+1] - statp[level]);
  6434. else //Default increase
  6435. return ((level+15) / 5);
  6436. }
  6437. #ifdef RENEWAL_STAT
  6438. /// Renewal status point cost formula
  6439. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6440. #else
  6441. /// Pre-Renewal status point cost formula
  6442. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6443. #endif
  6444. /// Returns the number of stat points needed to change the specified stat by val.
  6445. /// If val is negative, returns the number of stat points that would be needed to
  6446. /// raise the specified stat from (current value - val) to current value.
  6447. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6448. {
  6449. int low, high, sp = 0, max = 0;
  6450. if ( val == 0 )
  6451. return 0;
  6452. low = pc_getstat(sd,type);
  6453. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6454. if ( low >= max && val > 0 )
  6455. return 0; // Official servers show '0' when max is reached
  6456. high = low + val;
  6457. if ( val < 0 )
  6458. SWAP(low, high);
  6459. for ( ; low < high; low++ )
  6460. sp += PC_STATUS_POINT_COST(low);
  6461. return sp;
  6462. }
  6463. /**
  6464. * Returns the value the specified stat can be increased by with the current
  6465. * amount of available status points for the current character's class.
  6466. *
  6467. * @param sd The target character.
  6468. * @param type Stat to verify.
  6469. * @return Maximum value the stat could grow by.
  6470. */
  6471. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6472. {
  6473. int base, final_val, status_points, max_param;
  6474. nullpo_ret(sd);
  6475. base = final_val = pc_getstat(sd, type);
  6476. status_points = sd->status.status_point;
  6477. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6478. while (final_val <= max_param && status_points >= 0) {
  6479. status_points -= PC_STATUS_POINT_COST(final_val);
  6480. final_val++;
  6481. }
  6482. final_val--;
  6483. return (final_val > base ? final_val-base : 0);
  6484. }
  6485. /**
  6486. * Raises a stat by the specified amount.
  6487. *
  6488. * Obeys max_parameter limits.
  6489. * Subtracts status points according to the cost of the increased stat points.
  6490. *
  6491. * @param sd The target character.
  6492. * @param type The stat to change (see enum _sp)
  6493. * @param increase The stat increase (strictly positive) amount.
  6494. * @retval true if the stat was increased by any amount.
  6495. * @retval false if there were no changes.
  6496. */
  6497. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6498. {
  6499. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6500. nullpo_ret(sd);
  6501. // check conditions
  6502. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6503. clif_statusupack(sd, type, 0, 0);
  6504. return false;
  6505. }
  6506. // check limits
  6507. current = pc_getstat(sd, type);
  6508. max_increase = pc_maxparameterincrease(sd, type);
  6509. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6510. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6511. clif_statusupack(sd, type, 0, 0);
  6512. return false;
  6513. }
  6514. // check status points
  6515. needed_points = pc_need_status_point(sd, type, increase);
  6516. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6517. clif_statusupack(sd, type, 0, 0);
  6518. return false;
  6519. }
  6520. // set new values
  6521. final_value = pc_setstat(sd, type, current + increase);
  6522. sd->status.status_point -= needed_points;
  6523. status_calc_pc(sd,SCO_NONE);
  6524. // update increase cost indicator
  6525. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6526. // update statpoint count
  6527. clif_updatestatus(sd, SP_STATUSPOINT);
  6528. // update stat value
  6529. clif_statusupack(sd, type, 1, final_value); // required
  6530. if( final_value > 255 )
  6531. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6532. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6533. return true;
  6534. }
  6535. /**
  6536. * Raises a stat by the specified amount.
  6537. *
  6538. * Obeys max_parameter limits.
  6539. * Does not subtract status points for the cost of the modified stat points.
  6540. *
  6541. * @param sd The target character.
  6542. * @param type The stat to change (see enum _sp)
  6543. * @param val The stat increase (or decrease) amount.
  6544. * @return the stat increase amount.
  6545. * @retval 0 if no changes were made.
  6546. */
  6547. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6548. {
  6549. int max, need;
  6550. nullpo_ret(sd);
  6551. if( type < SP_STR || type > SP_LUK )
  6552. {
  6553. clif_statusupack(sd,type,0,0);
  6554. return 0;
  6555. }
  6556. need = pc_need_status_point(sd,type,1);
  6557. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6558. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6559. status_calc_pc(sd,SCO_NONE);
  6560. // update increase cost indicator
  6561. if( need != pc_need_status_point(sd,type,1) )
  6562. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6563. // update stat value
  6564. clif_statusupack(sd,type,1,val); // required
  6565. if( val > 255 )
  6566. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6567. return val;
  6568. }
  6569. /*==========================================
  6570. * Update skill_lv for player sd
  6571. * Skill point allocation
  6572. *------------------------------------------*/
  6573. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6574. {
  6575. uint16 idx = skill_get_index(skill_id);
  6576. nullpo_retv(sd);
  6577. if (!idx) {
  6578. if (skill_id)
  6579. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6580. return;
  6581. }
  6582. // Level up guild skill
  6583. if (SKILL_CHK_GUILD(skill_id)) {
  6584. guild_skillup(sd, skill_id);
  6585. return;
  6586. }
  6587. // Level up homunculus skill
  6588. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6589. hom_skillup(sd->hd, skill_id);
  6590. return;
  6591. }
  6592. else {
  6593. if( sd->status.skill_point > 0 &&
  6594. sd->status.skill[idx].id &&
  6595. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6596. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6597. {
  6598. int lv, range, upgradable;
  6599. sd->status.skill[idx].lv++;
  6600. sd->status.skill_point--;
  6601. if( !skill_get_inf(skill_id) )
  6602. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6603. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6604. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6605. else
  6606. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6607. lv = sd->status.skill[idx].lv;
  6608. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6609. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6610. clif_skillup(sd,skill_id,lv,range,upgradable);
  6611. clif_updatestatus(sd,SP_SKILLPOINT);
  6612. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6613. clif_updatestatus(sd,SP_CARTINFO);
  6614. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  6615. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6616. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6617. clif_skillinfoblock(sd);
  6618. }
  6619. //else
  6620. // 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);
  6621. }
  6622. }
  6623. /*==========================================
  6624. * /allskill
  6625. *------------------------------------------*/
  6626. int pc_allskillup(struct map_session_data *sd)
  6627. {
  6628. int i;
  6629. nullpo_ret(sd);
  6630. for (i = 0; i < MAX_SKILL; i++) {
  6631. 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) {
  6632. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6633. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6634. if (sd->status.skill[i].lv == 0)
  6635. sd->status.skill[i].id = 0;
  6636. }
  6637. }
  6638. if (!pc_grant_allskills(sd, true)) {
  6639. uint16 sk_id;
  6640. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6641. uint16 sk_idx = skill_get_index(sk_id);
  6642. if (sk_id == 0 || sk_idx == 0)
  6643. continue;
  6644. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  6645. if (
  6646. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  6647. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  6648. sk_id == SG_DEVIL
  6649. )
  6650. continue; //Cannot be learned normally.
  6651. sd->status.skill[sk_idx].id = sk_id;
  6652. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6653. }
  6654. }
  6655. status_calc_pc(sd,SCO_NONE);
  6656. //Required because if you could level up all skills previously,
  6657. //the update will not be sent as only the lv variable changes.
  6658. clif_skillinfoblock(sd);
  6659. return 0;
  6660. }
  6661. /*==========================================
  6662. * /resetlvl
  6663. *------------------------------------------*/
  6664. int pc_resetlvl(struct map_session_data* sd,int type)
  6665. {
  6666. int i;
  6667. nullpo_ret(sd);
  6668. if (type != 3) //Also reset skills
  6669. pc_resetskill(sd, 0);
  6670. if(type == 1){
  6671. sd->status.skill_point=0;
  6672. sd->status.base_level=1;
  6673. sd->status.job_level=1;
  6674. sd->status.base_exp=0;
  6675. sd->status.job_exp=0;
  6676. if(sd->sc.option !=0)
  6677. sd->sc.option = 0;
  6678. sd->status.str=1;
  6679. sd->status.agi=1;
  6680. sd->status.vit=1;
  6681. sd->status.int_=1;
  6682. sd->status.dex=1;
  6683. sd->status.luk=1;
  6684. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6685. sd->status.status_point=100; // not 88 [celest]
  6686. // give platinum skills upon changing
  6687. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6688. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6689. }
  6690. }
  6691. if(type == 2){
  6692. sd->status.skill_point=0;
  6693. sd->status.base_level=1;
  6694. sd->status.job_level=1;
  6695. sd->status.base_exp=0;
  6696. sd->status.job_exp=0;
  6697. }
  6698. if(type == 3){
  6699. sd->status.base_level=1;
  6700. sd->status.base_exp=0;
  6701. }
  6702. if(type == 4){
  6703. sd->status.job_level=1;
  6704. sd->status.job_exp=0;
  6705. }
  6706. clif_updatestatus(sd,SP_STATUSPOINT);
  6707. clif_updatestatus(sd,SP_STR);
  6708. clif_updatestatus(sd,SP_AGI);
  6709. clif_updatestatus(sd,SP_VIT);
  6710. clif_updatestatus(sd,SP_INT);
  6711. clif_updatestatus(sd,SP_DEX);
  6712. clif_updatestatus(sd,SP_LUK);
  6713. clif_updatestatus(sd,SP_BASELEVEL);
  6714. clif_updatestatus(sd,SP_JOBLEVEL);
  6715. clif_updatestatus(sd,SP_STATUSPOINT);
  6716. clif_updatestatus(sd,SP_BASEEXP);
  6717. clif_updatestatus(sd,SP_JOBEXP);
  6718. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6719. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6720. clif_updatestatus(sd,SP_SKILLPOINT);
  6721. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6722. clif_updatestatus(sd,SP_UAGI);
  6723. clif_updatestatus(sd,SP_UVIT);
  6724. clif_updatestatus(sd,SP_UINT);
  6725. clif_updatestatus(sd,SP_UDEX);
  6726. clif_updatestatus(sd,SP_ULUK); // End Addition
  6727. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6728. if(sd->equip_index[i] >= 0)
  6729. if(pc_isequip(sd,sd->equip_index[i]))
  6730. pc_unequipitem(sd,sd->equip_index[i],2);
  6731. }
  6732. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6733. party_send_levelup(sd);
  6734. status_calc_pc(sd, SCO_FORCE);
  6735. clif_skillinfoblock(sd);
  6736. return 0;
  6737. }
  6738. /*==========================================
  6739. * /resetstate
  6740. *------------------------------------------*/
  6741. int pc_resetstate(struct map_session_data* sd)
  6742. {
  6743. nullpo_ret(sd);
  6744. if (battle_config.use_statpoint_table)
  6745. { // New statpoint table used here - Dexity
  6746. if (sd->status.base_level > MAX_LEVEL)
  6747. { //statp[] goes out of bounds, can't reset!
  6748. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6749. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6750. return 0;
  6751. }
  6752. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6753. }
  6754. else
  6755. {
  6756. int add=0;
  6757. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6758. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6759. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6760. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6761. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6762. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6763. sd->status.status_point+=add;
  6764. }
  6765. pc_setstat(sd, SP_STR, 1);
  6766. pc_setstat(sd, SP_AGI, 1);
  6767. pc_setstat(sd, SP_VIT, 1);
  6768. pc_setstat(sd, SP_INT, 1);
  6769. pc_setstat(sd, SP_DEX, 1);
  6770. pc_setstat(sd, SP_LUK, 1);
  6771. clif_updatestatus(sd,SP_STR);
  6772. clif_updatestatus(sd,SP_AGI);
  6773. clif_updatestatus(sd,SP_VIT);
  6774. clif_updatestatus(sd,SP_INT);
  6775. clif_updatestatus(sd,SP_DEX);
  6776. clif_updatestatus(sd,SP_LUK);
  6777. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6778. clif_updatestatus(sd,SP_UAGI);
  6779. clif_updatestatus(sd,SP_UVIT);
  6780. clif_updatestatus(sd,SP_UINT);
  6781. clif_updatestatus(sd,SP_UDEX);
  6782. clif_updatestatus(sd,SP_ULUK); // End Addition
  6783. clif_updatestatus(sd,SP_STATUSPOINT);
  6784. if( sd->mission_mobid ) { //bugreport:2200
  6785. sd->mission_mobid = 0;
  6786. sd->mission_count = 0;
  6787. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6788. }
  6789. status_calc_pc(sd, SCO_NONE);
  6790. return 1;
  6791. }
  6792. /*==========================================
  6793. * /resetskill
  6794. * if flag&1, perform block resync and status_calc call.
  6795. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6796. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6797. *------------------------------------------*/
  6798. int pc_resetskill(struct map_session_data* sd, int flag)
  6799. {
  6800. int i, skill_point=0;
  6801. nullpo_ret(sd);
  6802. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6803. return 0;
  6804. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6805. /**
  6806. * 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)
  6807. **/
  6808. if( pc_is_taekwon_ranker(sd) )
  6809. return 0;
  6810. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6811. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6812. i = sd->sc.option;
  6813. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6814. i &= ~OPTION_RIDING;
  6815. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6816. i &= ~OPTION_FALCON;
  6817. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6818. i &= ~OPTION_DRAGON;
  6819. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6820. i &= ~OPTION_WUG;
  6821. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6822. i &= ~OPTION_WUGRIDER;
  6823. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6824. i &= ~OPTION_MADOGEAR;
  6825. #ifndef NEW_CARTS
  6826. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6827. i &= ~OPTION_CART;
  6828. #else
  6829. if( sd->sc.data[SC_PUSH_CART] )
  6830. pc_setcart(sd, 0);
  6831. #endif
  6832. if( i != sd->sc.option )
  6833. pc_setoption(sd, i);
  6834. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6835. hom_vaporize(sd, HOM_ST_ACTIVE);
  6836. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6837. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6838. }
  6839. for (const auto &skill : skill_db) {
  6840. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  6841. uint8 lv = sd->status.skill[idx].lv;
  6842. if (lv == 0 || skill_id == 0)
  6843. continue;
  6844. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  6845. continue;
  6846. // Don't reset trick dead if not a novice/baby
  6847. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6848. {
  6849. sd->status.skill[idx].lv = 0;
  6850. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6851. continue;
  6852. }
  6853. // do not reset basic skill
  6854. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6855. continue;
  6856. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  6857. continue;
  6858. if( flag&4 && !skill_ischangesex(skill_id) )
  6859. continue;
  6860. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  6861. { //Only handle quest skills in a special way when you can't learn them manually
  6862. if( battle_config.quest_skill_reset && !(flag&2) )
  6863. { //Wipe them
  6864. sd->status.skill[idx].lv = 0;
  6865. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6866. }
  6867. continue;
  6868. }
  6869. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  6870. skill_point += lv;
  6871. else
  6872. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6873. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  6874. if( !(flag&2) )
  6875. {// reset
  6876. sd->status.skill[idx].lv = 0;
  6877. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6878. }
  6879. }
  6880. if( flag&2 || !skill_point ) return skill_point;
  6881. sd->status.skill_point += skill_point;
  6882. if (flag&1) {
  6883. clif_updatestatus(sd,SP_SKILLPOINT);
  6884. clif_skillinfoblock(sd);
  6885. status_calc_pc(sd, SCO_FORCE);
  6886. }
  6887. return skill_point;
  6888. }
  6889. /*==========================================
  6890. * /resetfeel [Komurka]
  6891. *------------------------------------------*/
  6892. int pc_resetfeel(struct map_session_data* sd)
  6893. {
  6894. int i;
  6895. nullpo_ret(sd);
  6896. for (i=0; i<MAX_PC_FEELHATE; i++)
  6897. {
  6898. sd->feel_map[i].m = -1;
  6899. sd->feel_map[i].index = 0;
  6900. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6901. }
  6902. return 0;
  6903. }
  6904. int pc_resethate(struct map_session_data* sd)
  6905. {
  6906. int i;
  6907. nullpo_ret(sd);
  6908. for (i=0; i<MAX_PC_FEELHATE; i++)
  6909. {
  6910. sd->hate_mob[i] = -1;
  6911. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6912. }
  6913. return 0;
  6914. }
  6915. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6916. {
  6917. int bonus = 0;
  6918. nullpo_ret(sd);
  6919. skill_id = skill_dummy2skill_id(skill_id);
  6920. for (auto &it : sd->skillatk) {
  6921. if (it.id == skill_id) {
  6922. bonus += it.val;
  6923. break;
  6924. }
  6925. }
  6926. return bonus;
  6927. }
  6928. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6929. {
  6930. int bonus = 0;
  6931. nullpo_ret(sd);
  6932. skill_id = skill_dummy2skill_id(skill_id);
  6933. for (auto &it : sd->subskill) {
  6934. if (it.id == skill_id) {
  6935. bonus += it.val;
  6936. break;
  6937. }
  6938. }
  6939. return bonus;
  6940. }
  6941. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6942. int bonus = sd->bonus.add_heal_rate;
  6943. nullpo_ret(sd);
  6944. skill_id = skill_dummy2skill_id(skill_id);
  6945. if( bonus ) {
  6946. switch( skill_id ) {
  6947. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6948. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6949. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6950. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6951. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6952. }
  6953. }
  6954. for (auto &it : sd->skillheal) {
  6955. if (it.id == skill_id) {
  6956. bonus += it.val;
  6957. break;
  6958. }
  6959. }
  6960. return bonus;
  6961. }
  6962. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6963. int bonus = sd->bonus.add_heal2_rate;
  6964. skill_id = skill_dummy2skill_id(skill_id);
  6965. for (auto &it : sd->skillheal2) {
  6966. if (it.id == skill_id) {
  6967. bonus += it.val;
  6968. break;
  6969. }
  6970. }
  6971. return bonus;
  6972. }
  6973. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6974. {
  6975. if( !pc_isdead(sd) )
  6976. return; // not applicable
  6977. if( sd->bg_id && bg_member_respawn(sd) )
  6978. return; // member revived by battleground
  6979. pc_setstand(sd, true);
  6980. pc_setrestartvalue(sd,3);
  6981. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6982. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6983. }
  6984. static TIMER_FUNC(pc_respawn_timer){
  6985. struct map_session_data *sd = map_id2sd(id);
  6986. if( sd != NULL )
  6987. {
  6988. sd->pvp_point=0;
  6989. sd->respawn_tid = INVALID_TIMER;
  6990. pc_respawn(sd,CLR_OUTSIGHT);
  6991. }
  6992. return 0;
  6993. }
  6994. /*==========================================
  6995. * Invoked when a player has received damage
  6996. *------------------------------------------*/
  6997. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6998. {
  6999. if (sp) clif_updatestatus(sd,SP_SP);
  7000. if (hp) clif_updatestatus(sd,SP_HP);
  7001. else return;
  7002. if (!src)
  7003. return;
  7004. if( pc_issit(sd) ) {
  7005. pc_setstand(sd, true);
  7006. skill_sit(sd,0);
  7007. }
  7008. if (sd->progressbar.npc_id)
  7009. clif_progressbar_abort(sd);
  7010. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  7011. pet_target_check(sd->pd,src,1);
  7012. if( sd->status.ele_id > 0 )
  7013. elemental_set_target(sd,src);
  7014. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  7015. sd->canlog_tick = gettick();
  7016. }
  7017. TIMER_FUNC(pc_close_npc_timer){
  7018. TBL_PC *sd = map_id2sd(id);
  7019. if(sd) pc_close_npc(sd,data);
  7020. return 0;
  7021. }
  7022. /**
  7023. * Method to properly close a NPC for player and clear anything related.
  7024. * @param sd: Player attached
  7025. * @param flag: Method of closure
  7026. * 1: Produce a close button and end the NPC
  7027. * 2: End the NPC (best for no dialog windows)
  7028. */
  7029. void pc_close_npc(struct map_session_data *sd,int flag)
  7030. {
  7031. nullpo_retv(sd);
  7032. if (sd->npc_id || sd->npc_shopid) {
  7033. if (sd->state.using_fake_npc) {
  7034. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  7035. sd->state.using_fake_npc = 0;
  7036. }
  7037. if (sd->st) {
  7038. if(sd->st->state == RUN){ //wait ending code execution
  7039. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  7040. return;
  7041. }
  7042. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  7043. sd->st->mes_active = 0;
  7044. }
  7045. sd->state.menu_or_input = 0;
  7046. sd->npc_menu = 0;
  7047. sd->npc_shopid = 0;
  7048. #ifdef SECURE_NPCTIMEOUT
  7049. sd->npc_idle_timer = INVALID_TIMER;
  7050. #endif
  7051. if (sd->st) {
  7052. if (sd->st->state == CLOSE) {
  7053. clif_scriptclose(sd, sd->npc_id);
  7054. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  7055. sd->st->state = END; // Force to end now
  7056. }
  7057. if (sd->st->state == END) { // free attached scripts that are waiting
  7058. script_free_state(sd->st);
  7059. sd->st = NULL;
  7060. sd->npc_id = 0;
  7061. }
  7062. }
  7063. }
  7064. }
  7065. /*==========================================
  7066. * Invoked when a player has negative current hp
  7067. *------------------------------------------*/
  7068. int pc_dead(struct map_session_data *sd,struct block_list *src)
  7069. {
  7070. int i=0,k=0;
  7071. t_tick tick = gettick();
  7072. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  7073. // Activate Steel body if a super novice dies at 99+% exp [celest]
  7074. // Super Novices have no kill or die functions attached when saved by their angel
  7075. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7076. unsigned int exp = pc_nextbaseexp(sd);
  7077. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  7078. sd->state.snovice_dead_flag = 1;
  7079. pc_setrestartvalue(sd,1);
  7080. status_percent_heal(&sd->bl, 100, 100);
  7081. clif_resurrection(&sd->bl, 1);
  7082. if(battle_config.pc_invincible_time)
  7083. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7084. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  7085. if(mapdata_flag_gvg2(mapdata))
  7086. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7087. return 0;
  7088. }
  7089. }
  7090. for(k = 0; k < MAX_DEVOTION; k++) {
  7091. if (sd->devotion[k]){
  7092. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  7093. if (devsd)
  7094. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  7095. sd->devotion[k] = 0;
  7096. }
  7097. }
  7098. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  7099. if (sd->stellar_mark[k]) {
  7100. struct map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  7101. if (smarksd)
  7102. status_change_end(&smarksd->bl, SC_FLASHKICK, INVALID_TIMER);
  7103. sd->stellar_mark[k] = 0;
  7104. }
  7105. }
  7106. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  7107. if (sd->united_soul[k]) {
  7108. struct map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  7109. if (usoulsd)
  7110. status_change_end(&usoulsd->bl, SC_SOULUNITY, INVALID_TIMER);
  7111. sd->united_soul[k] = 0;
  7112. }
  7113. }
  7114. if(sd->shadowform_id) { //if we were target of shadowform
  7115. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  7116. sd->shadowform_id = 0; //should be remove on status end anyway
  7117. }
  7118. if(sd->status.pet_id > 0 && sd->pd) {
  7119. struct pet_data *pd = sd->pd;
  7120. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  7121. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  7122. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  7123. pet_set_intimate(pd, PET_INTIMATE_NONE);
  7124. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  7125. }
  7126. if( sd->pd->target_id ) // Unlock all targets...
  7127. pet_unlocktarget(sd->pd);
  7128. }
  7129. 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)
  7130. hom_vaporize(sd, HOM_ST_ACTIVE);
  7131. if( sd->md )
  7132. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  7133. if( sd->ed )
  7134. elemental_delete(sd->ed);
  7135. // Leave duel if you die [LuzZza]
  7136. if(battle_config.duel_autoleave_when_die) {
  7137. if(sd->duel_group > 0)
  7138. duel_leave(sd->duel_group, sd);
  7139. if(sd->duel_invite > 0)
  7140. duel_reject(sd->duel_invite, sd);
  7141. }
  7142. pc_close_npc(sd,2); //close npc if we were using one
  7143. /* e.g. not killed thru pc_damage */
  7144. if( pc_issit(sd) ) {
  7145. clif_status_load(&sd->bl,EFST_SIT,0);
  7146. }
  7147. pc_setdead(sd);
  7148. clif_party_dead( sd );
  7149. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
  7150. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  7151. //Reset menu skills/item skills
  7152. if ((sd->skillitem) != 0)
  7153. sd->skillitem = sd->skillitemlv = 0;
  7154. if ((sd->menuskill_id) != 0)
  7155. sd->menuskill_id = sd->menuskill_val = 0;
  7156. //Reset ticks.
  7157. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  7158. if ( sd->spiritball !=0 )
  7159. pc_delspiritball(sd,sd->spiritball,0);
  7160. if (sd->soulball != 0)
  7161. pc_delsoulball(sd, sd->soulball, 0);
  7162. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  7163. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  7164. if (src)
  7165. switch (src->type) {
  7166. case BL_MOB:
  7167. {
  7168. struct mob_data *md=(struct mob_data *)src;
  7169. if(md->target_id==sd->bl.id)
  7170. mob_unlocktarget(md,tick);
  7171. if(battle_config.mobs_level_up && md->status.hp &&
  7172. (unsigned int)md->level < pc_maxbaselv(sd) &&
  7173. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  7174. ) { // monster level up [Valaris]
  7175. clif_misceffect(&md->bl,0);
  7176. md->level++;
  7177. status_calc_mob(md, SCO_NONE);
  7178. status_percent_heal(src,10,0);
  7179. if( battle_config.show_mob_info&4 )
  7180. {// update name with new level
  7181. clif_name_area(&md->bl);
  7182. }
  7183. }
  7184. src = battle_get_master(src); // Maybe Player Summon
  7185. }
  7186. break;
  7187. case BL_PET: //Pass on to master...
  7188. case BL_HOM:
  7189. case BL_MER:
  7190. src = battle_get_master(src);
  7191. break;
  7192. }
  7193. if (src && src->type == BL_PC) {
  7194. struct map_session_data *ssd = (struct map_session_data *)src;
  7195. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  7196. npc_script_event(ssd, NPCE_KILLPC);
  7197. if (battle_config.pk_mode&2) {
  7198. ssd->status.manner -= 5;
  7199. if(ssd->status.manner < 0)
  7200. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  7201. #if 0
  7202. // PK/Karma system code (not enabled yet) [celest]
  7203. // originally from Kade Online, so i don't know if any of these is correct ^^;
  7204. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  7205. // karma going down = more 'good' / more honourable.
  7206. // The Karma System way...
  7207. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  7208. sd->status.karma--;
  7209. ssd->status.karma--;
  7210. }
  7211. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  7212. ssd->status.karma++;
  7213. // or the PK System way...
  7214. if (sd->status.karma > 0) // player killed is dishonourable?
  7215. ssd->status.karma--; // honour points earned
  7216. sd->status.karma++; // honour points lost
  7217. // To-do: Receive exp on certain occasions
  7218. #endif
  7219. }
  7220. }
  7221. if(battle_config.bone_drop==2
  7222. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  7223. {
  7224. struct item item_tmp;
  7225. memset(&item_tmp,0,sizeof(item_tmp));
  7226. item_tmp.nameid=ITEMID_SKULL_;
  7227. item_tmp.identify=1;
  7228. item_tmp.card[0]=CARD0_CREATE;
  7229. item_tmp.card[1]=0;
  7230. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  7231. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  7232. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  7233. }
  7234. //Remove bonus_script when dead
  7235. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  7236. // changed penalty options, added death by player if pk_mode [Valaris]
  7237. if(battle_config.death_penalty_type
  7238. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  7239. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  7240. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  7241. {
  7242. uint32 base_penalty = 0;
  7243. uint32 job_penalty = 0;
  7244. uint32 zeny_penalty = 0;
  7245. if (pc_isvip(sd)) { // EXP penalty for VIP
  7246. base_penalty = battle_config.vip_exp_penalty_base;
  7247. job_penalty = battle_config.vip_exp_penalty_job;
  7248. zeny_penalty = battle_config.vip_zeny_penalty;
  7249. } else {
  7250. base_penalty = battle_config.death_penalty_base;
  7251. job_penalty = battle_config.death_penalty_job;
  7252. zeny_penalty = battle_config.zeny_penalty;
  7253. }
  7254. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7255. switch (battle_config.death_penalty_type) {
  7256. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7257. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  7258. }
  7259. if (base_penalty){ //recheck after altering to speedup
  7260. if (battle_config.pk_mode && src && src->type==BL_PC)
  7261. base_penalty *= 2;
  7262. base_penalty = u32min(sd->status.base_exp, base_penalty);
  7263. }
  7264. }
  7265. else
  7266. base_penalty = 0;
  7267. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7268. switch (battle_config.death_penalty_type) {
  7269. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7270. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7271. }
  7272. if (job_penalty) {
  7273. if (battle_config.pk_mode && src && src->type==BL_PC)
  7274. job_penalty *= 2;
  7275. job_penalty = u32min(sd->status.job_exp, job_penalty);
  7276. }
  7277. }
  7278. else
  7279. job_penalty = 0;
  7280. if (base_penalty || job_penalty)
  7281. pc_lostexp(sd, base_penalty, job_penalty);
  7282. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  7283. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7284. if(zeny_penalty)
  7285. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7286. }
  7287. }
  7288. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7289. for (const auto &it : mapdata->drop_list) {
  7290. int id = it.drop_id, per = it.drop_per;
  7291. enum e_nightmare_drop_type type = it.drop_type;
  7292. if(id == 0)
  7293. continue;
  7294. if(id == -1){
  7295. int eq_num=0,eq_n[MAX_INVENTORY];
  7296. memset(eq_n,0,sizeof(eq_n));
  7297. for(i=0;i<MAX_INVENTORY;i++) {
  7298. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7299. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7300. || type&NMDT_ALL)
  7301. {
  7302. int l;
  7303. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7304. if( l < MAX_INVENTORY )
  7305. eq_n[l] = i;
  7306. eq_num++;
  7307. }
  7308. }
  7309. if(eq_num > 0){
  7310. int n = eq_n[rnd()%eq_num];
  7311. if(rnd()%10000 < per) {
  7312. if(sd->inventory.u.items_inventory[n].equip)
  7313. pc_unequipitem(sd,n,3);
  7314. pc_dropitem(sd,n,1);
  7315. }
  7316. }
  7317. }
  7318. else if(id > 0) {
  7319. for(i=0;i<MAX_INVENTORY;i++){
  7320. if(sd->inventory.u.items_inventory[i].nameid == id
  7321. && rnd()%10000 < per
  7322. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7323. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7324. || type&NMDT_ALL) ){
  7325. if(sd->inventory.u.items_inventory[i].equip)
  7326. pc_unequipitem(sd,i,3);
  7327. pc_dropitem(sd,i,1);
  7328. break;
  7329. }
  7330. }
  7331. }
  7332. }
  7333. }
  7334. // pvp
  7335. // disable certain pvp functions on pk_mode [Valaris]
  7336. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  7337. sd->pvp_point -= 5;
  7338. sd->pvp_lost++;
  7339. if( src && src->type == BL_PC ) {
  7340. struct map_session_data *ssd = (struct map_session_data *)src;
  7341. ssd->pvp_point++;
  7342. ssd->pvp_won++;
  7343. }
  7344. if( sd->pvp_point < 0 ) {
  7345. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7346. return 1|8;
  7347. }
  7348. }
  7349. //GvG
  7350. if( mapdata_flag_gvg2(mapdata) ) {
  7351. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7352. return 1|8;
  7353. }
  7354. else if( sd->bg_id ) {
  7355. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  7356. if (bg) {
  7357. if (bg->cemetery.map > 0) { // Respawn by BG
  7358. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  7359. return 1|8;
  7360. }
  7361. }
  7362. }
  7363. //Reset "can log out" tick.
  7364. if( battle_config.prevent_logout )
  7365. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7366. return 1;
  7367. }
  7368. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7369. if(hp) clif_updatestatus(sd,SP_HP);
  7370. if(sp) clif_updatestatus(sd,SP_SP);
  7371. pc_setstand(sd, true);
  7372. if(battle_config.pc_invincible_time > 0)
  7373. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7374. if( sd->state.gmaster_flag ) {
  7375. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7376. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7377. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7378. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7379. }
  7380. }
  7381. bool pc_revive_item(struct map_session_data *sd) {
  7382. nullpo_retr(false, sd);
  7383. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  7384. return false;
  7385. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  7386. return false;
  7387. int16 item_position = itemdb_group_item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  7388. uint8 hp = 100, sp = 100;
  7389. if (item_position < 0) {
  7390. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  7391. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  7392. sp = 0;
  7393. }
  7394. else
  7395. return false;
  7396. }
  7397. if (!status_revive(&sd->bl, hp, sp))
  7398. return false;
  7399. if (item_position < 0)
  7400. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE, INVALID_TIMER);
  7401. else
  7402. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  7403. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  7404. return true;
  7405. }
  7406. // script
  7407. //
  7408. /*==========================================
  7409. * script reading pc status registry
  7410. *------------------------------------------*/
  7411. int64 pc_readparam(struct map_session_data* sd,int64 type)
  7412. {
  7413. int64 val = 0;
  7414. nullpo_ret(sd);
  7415. switch(type) {
  7416. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7417. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7418. case SP_ZENY: val = sd->status.zeny; break;
  7419. case SP_BASELEVEL: val = sd->status.base_level; break;
  7420. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7421. case SP_CLASS: val = sd->status.class_; break;
  7422. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7423. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7424. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7425. case SP_SEX: val = sd->status.sex; break;
  7426. case SP_WEIGHT: val = sd->weight; break;
  7427. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7428. case SP_BASEEXP: val = sd->status.base_exp; break;
  7429. case SP_JOBEXP: val = sd->status.job_exp; break;
  7430. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7431. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7432. case SP_HP: val = sd->battle_status.hp; break;
  7433. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7434. case SP_SP: val = sd->battle_status.sp; break;
  7435. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7436. case SP_STR: val = sd->status.str; break;
  7437. case SP_AGI: val = sd->status.agi; break;
  7438. case SP_VIT: val = sd->status.vit; break;
  7439. case SP_INT: val = sd->status.int_; break;
  7440. case SP_DEX: val = sd->status.dex; break;
  7441. case SP_LUK: val = sd->status.luk; break;
  7442. case SP_KARMA: val = sd->status.karma; break;
  7443. case SP_MANNER: val = sd->status.manner; break;
  7444. case SP_FAME: val = sd->status.fame; break;
  7445. case SP_KILLERRID: val = sd->killerrid; break;
  7446. case SP_KILLEDRID: val = sd->killedrid; break;
  7447. case SP_KILLEDGID: val = sd->killedgid; break;
  7448. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7449. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7450. case SP_CHARRENAME: val = sd->status.rename; break;
  7451. case SP_CHARFONT: val = sd->status.font; break;
  7452. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7453. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7454. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7455. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7456. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7457. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7458. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7459. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7460. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  7461. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7462. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7463. case SP_BASE_ATK:
  7464. #ifdef RENEWAL
  7465. val = sd->bonus.eatk;
  7466. #else
  7467. val = sd->battle_status.batk;
  7468. #endif
  7469. break;
  7470. case SP_DEF1: val = sd->battle_status.def; break;
  7471. case SP_DEF2: val = sd->battle_status.def2; break;
  7472. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7473. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7474. case SP_HIT: val = sd->battle_status.hit; break;
  7475. case SP_FLEE1: val = sd->battle_status.flee; break;
  7476. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7477. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7478. case SP_MAXHPRATE: val = sd->hprate; break;
  7479. case SP_MAXSPRATE: val = sd->sprate; break;
  7480. case SP_SPRATE: val = sd->dsprate; break;
  7481. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7482. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7483. case SP_ASPD_RATE:
  7484. #ifndef RENEWAL_ASPD
  7485. val = sd->battle_status.aspd_rate;
  7486. #else
  7487. val = sd->battle_status.aspd_rate2;
  7488. #endif
  7489. break;
  7490. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7491. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7492. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7493. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7494. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7495. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7496. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7497. case SP_MATK_RATE: val = sd->matk_rate; break;
  7498. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7499. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7500. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7501. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7502. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7503. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7504. case SP_HIT_RATE: val = sd->hit_rate; break;
  7505. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7506. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7507. case SP_DEF_RATE: val = sd->def_rate; break;
  7508. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7509. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7510. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7511. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7512. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7513. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7514. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7515. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7516. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7517. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7518. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7519. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7520. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7521. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  7522. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  7523. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7524. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7525. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7526. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7527. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7528. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7529. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7530. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7531. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7532. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7533. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7534. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7535. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7536. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7537. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7538. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7539. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7540. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7541. case SP_DELAYRATE: val = sd->delayrate; break;
  7542. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7543. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7544. case SP_UNSTRIPABLE:
  7545. case SP_UNSTRIPABLE_ARMOR:
  7546. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7547. break;
  7548. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7549. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7550. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7551. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7552. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  7553. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  7554. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7555. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7556. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7557. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7558. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7559. case SP_EMATK: val = sd->bonus.ematk; break;
  7560. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7561. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7562. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7563. case SP_CASTRATE:
  7564. case SP_VARCASTRATE:
  7565. #ifdef RENEWAL_CAST
  7566. val = sd->bonus.varcastrate; break;
  7567. #else
  7568. val = sd->castrate; break;
  7569. #endif
  7570. default:
  7571. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  7572. return -1;
  7573. }
  7574. return val;
  7575. }
  7576. /*==========================================
  7577. * script set pc status registry
  7578. *------------------------------------------*/
  7579. bool pc_setparam(struct map_session_data *sd,int64 type,int64 val_tmp)
  7580. {
  7581. nullpo_retr(false,sd);
  7582. int val = static_cast<unsigned int>(val_tmp);
  7583. switch(type){
  7584. case SP_BASELEVEL:
  7585. if (val > pc_maxbaselv(sd)) //Capping to max
  7586. val = pc_maxbaselv(sd);
  7587. if (val > sd->status.base_level) {
  7588. int i = 0;
  7589. int stat=0;
  7590. for (i = 0; i < (int)(val - sd->status.base_level); i++)
  7591. stat += pc_gets_status_point(sd->status.base_level + i);
  7592. sd->status.status_point += stat;
  7593. }
  7594. sd->status.base_level = val;
  7595. sd->status.base_exp = 0;
  7596. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7597. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7598. clif_updatestatus(sd, SP_STATUSPOINT);
  7599. clif_updatestatus(sd, SP_BASEEXP);
  7600. status_calc_pc(sd, SCO_FORCE);
  7601. if(sd->status.party_id)
  7602. party_send_levelup(sd);
  7603. break;
  7604. case SP_JOBLEVEL:
  7605. if (val >= sd->status.job_level) {
  7606. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7607. sd->status.skill_point += val - sd->status.job_level;
  7608. clif_updatestatus(sd, SP_SKILLPOINT);
  7609. }
  7610. sd->status.job_level = val;
  7611. sd->status.job_exp = 0;
  7612. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7613. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7614. clif_updatestatus(sd, SP_JOBEXP);
  7615. status_calc_pc(sd, SCO_FORCE);
  7616. break;
  7617. case SP_SKILLPOINT:
  7618. sd->status.skill_point = val;
  7619. break;
  7620. case SP_STATUSPOINT:
  7621. sd->status.status_point = val;
  7622. break;
  7623. case SP_ZENY:
  7624. if( val < 0 )
  7625. return false;// can't set negative zeny
  7626. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7627. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7628. break;
  7629. case SP_BASEEXP:
  7630. {
  7631. val = cap_value(val, 0, INT_MAX);
  7632. if (val < sd->status.base_exp) // Lost
  7633. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7634. else // Gained
  7635. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7636. }
  7637. return true;
  7638. case SP_JOBEXP:
  7639. {
  7640. val = cap_value(val, 0, INT_MAX);
  7641. if (val < sd->status.job_exp) // Lost
  7642. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7643. else // Gained
  7644. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7645. }
  7646. return true;
  7647. case SP_SEX:
  7648. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7649. break;
  7650. case SP_WEIGHT:
  7651. sd->weight = val;
  7652. break;
  7653. case SP_MAXWEIGHT:
  7654. sd->max_weight = val;
  7655. break;
  7656. case SP_HP:
  7657. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7658. break;
  7659. case SP_MAXHP:
  7660. if (sd->status.base_level < 100)
  7661. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7662. else if (sd->status.base_level < 151)
  7663. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7664. else
  7665. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7666. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7667. {
  7668. sd->battle_status.hp = sd->battle_status.max_hp;
  7669. clif_updatestatus(sd, SP_HP);
  7670. }
  7671. break;
  7672. case SP_SP:
  7673. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7674. break;
  7675. case SP_MAXSP:
  7676. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7677. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7678. {
  7679. sd->battle_status.sp = sd->battle_status.max_sp;
  7680. clif_updatestatus(sd, SP_SP);
  7681. }
  7682. break;
  7683. case SP_STR:
  7684. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7685. break;
  7686. case SP_AGI:
  7687. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7688. break;
  7689. case SP_VIT:
  7690. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7691. break;
  7692. case SP_INT:
  7693. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7694. break;
  7695. case SP_DEX:
  7696. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7697. break;
  7698. case SP_LUK:
  7699. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7700. break;
  7701. case SP_KARMA:
  7702. sd->status.karma = val;
  7703. break;
  7704. case SP_MANNER:
  7705. sd->status.manner = val;
  7706. if( val < 0 )
  7707. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7708. else {
  7709. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7710. clif_manner_message(sd, 5);
  7711. }
  7712. return true; // status_change_start/status_change_end already sends packets warning the client
  7713. case SP_FAME:
  7714. sd->status.fame = val;
  7715. break;
  7716. case SP_KILLERRID:
  7717. sd->killerrid = val;
  7718. return true;
  7719. case SP_KILLEDRID:
  7720. sd->killedrid = val;
  7721. return true;
  7722. case SP_KILLEDGID:
  7723. sd->killedgid = val;
  7724. return true;
  7725. case SP_CHARMOVE:
  7726. sd->status.character_moves = val;
  7727. return true;
  7728. case SP_CHARRENAME:
  7729. sd->status.rename = val;
  7730. return true;
  7731. case SP_CHARFONT:
  7732. sd->status.font = val;
  7733. clif_font(sd);
  7734. return true;
  7735. case SP_BANK_VAULT:
  7736. if (val < 0)
  7737. return false;
  7738. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7739. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7740. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7741. return true;
  7742. case SP_ROULETTE_BRONZE:
  7743. sd->roulette_point.bronze = val;
  7744. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7745. return true;
  7746. case SP_ROULETTE_SILVER:
  7747. sd->roulette_point.silver = val;
  7748. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7749. return true;
  7750. case SP_ROULETTE_GOLD:
  7751. sd->roulette_point.gold = val;
  7752. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7753. return true;
  7754. case SP_CASHPOINTS:
  7755. if (val < 0)
  7756. return false;
  7757. if (!sd->state.connect_new)
  7758. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7759. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7760. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7761. return true;
  7762. case SP_KAFRAPOINTS:
  7763. if (val < 0)
  7764. return false;
  7765. if (!sd->state.connect_new)
  7766. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7767. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7768. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7769. return true;
  7770. case SP_PCDIECOUNTER:
  7771. if (val < 0)
  7772. return false;
  7773. if (sd->die_counter == val)
  7774. return true;
  7775. sd->die_counter = val;
  7776. if (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7777. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7778. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7779. return true;
  7780. case SP_COOKMASTERY:
  7781. if (val < 0)
  7782. return false;
  7783. if (sd->cook_mastery == val)
  7784. return true;
  7785. val = cap_value(val, 0, 1999);
  7786. sd->cook_mastery = val;
  7787. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7788. return true;
  7789. default:
  7790. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  7791. return false;
  7792. }
  7793. clif_updatestatus(sd,static_cast<int>(type));
  7794. return true;
  7795. }
  7796. /*==========================================
  7797. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7798. *------------------------------------------*/
  7799. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7800. {
  7801. if (type&2) {
  7802. if (hp || type&4)
  7803. clif_heal(sd->fd,SP_HP,hp);
  7804. if (sp)
  7805. clif_heal(sd->fd,SP_SP,sp);
  7806. } else {
  7807. if(hp)
  7808. clif_updatestatus(sd,SP_HP);
  7809. if(sp)
  7810. clif_updatestatus(sd,SP_SP);
  7811. }
  7812. return;
  7813. }
  7814. /**
  7815. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7816. * @param sd: Player data
  7817. * @param itemid: Item ID
  7818. * @param hp: HP to heal
  7819. * @param sp: SP to heal
  7820. * @return Amount healed to an object
  7821. */
  7822. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7823. {
  7824. int bonus, tmp, penalty = 0;
  7825. if (hp) {
  7826. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7827. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7828. if (potion_flag == 2) {
  7829. bonus += bonus * 50 / 100;
  7830. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7831. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  7832. }
  7833. //All item bonuses.
  7834. bonus += sd->bonus.itemhealrate2;
  7835. //Item Group bonuses
  7836. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid) / 100;
  7837. //Individual item bonuses.
  7838. for(const auto &it : sd->itemhealrate) {
  7839. if (it.id == itemid) {
  7840. bonus += bonus * it.val / 100;
  7841. break;
  7842. }
  7843. }
  7844. // Recovery Potion
  7845. if (sd->sc.data[SC_INCHEALRATE])
  7846. bonus += bonus * sd->sc.data[SC_INCHEALRATE]->val1 / 100;
  7847. // 2014 Halloween Event : Pumpkin Bonus
  7848. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7849. bonus += bonus * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100;
  7850. tmp = hp * bonus / 100; // Overflow check
  7851. if (bonus != 100 && tmp > hp)
  7852. hp = tmp;
  7853. }
  7854. if (sp) {
  7855. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7856. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7857. if (potion_flag == 2)
  7858. bonus += bonus * 50 / 100;
  7859. tmp = sp * bonus / 100; // Overflow check
  7860. if (bonus != 100 && tmp > sp)
  7861. sp = tmp;
  7862. }
  7863. if (sd->sc.count) {
  7864. // Critical Wound and Death Hurt stack
  7865. if (sd->sc.data[SC_CRITICALWOUND])
  7866. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7867. if (sd->sc.data[SC_DEATHHURT])
  7868. penalty += 20;
  7869. if (sd->sc.data[SC_NORECOVER_STATE])
  7870. penalty = 100;
  7871. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7872. hp += hp / 2; // 1.5 times
  7873. sp -= sp / 2;
  7874. }
  7875. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7876. hp += hp / 10;
  7877. sp += sp / 10;
  7878. }
  7879. #ifdef RENEWAL
  7880. if (sd->sc.data[SC_APPLEIDUN])
  7881. hp += sd->sc.data[SC_APPLEIDUN]->val3 / 100;
  7882. #endif
  7883. if (penalty > 0) {
  7884. hp -= hp * penalty / 100;
  7885. sp -= sp * penalty / 100;
  7886. }
  7887. #ifdef RENEWAL
  7888. if (sd->sc.data[SC_EXTREMITYFIST2])
  7889. sp = 0;
  7890. #endif
  7891. if (sd->sc.data[SC_BITESCAR])
  7892. hp = 0;
  7893. }
  7894. return status_heal(&sd->bl, hp, sp, 1);
  7895. }
  7896. /*==========================================
  7897. * HP/SP Recovery
  7898. * Heal player hp nad/or sp by rate
  7899. *------------------------------------------*/
  7900. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7901. {
  7902. nullpo_ret(sd);
  7903. if (hp > 100) hp = 100;
  7904. else if (hp <-100) hp = -100;
  7905. if (sp > 100) sp = 100;
  7906. else if (sp <-100) sp = -100;
  7907. if(hp >= 0 && sp >= 0) //Heal
  7908. return status_percent_heal(&sd->bl, hp, sp);
  7909. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7910. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7911. //Crossed signs
  7912. if(hp) {
  7913. if(hp > 0)
  7914. status_percent_heal(&sd->bl, hp, 0);
  7915. else
  7916. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7917. }
  7918. if(sp) {
  7919. if(sp > 0)
  7920. status_percent_heal(&sd->bl, 0, sp);
  7921. else
  7922. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7923. }
  7924. return 0;
  7925. }
  7926. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7927. {
  7928. struct mob_data *md;
  7929. int flag;
  7930. md = (struct mob_data *)bl;
  7931. nullpo_ret(md);
  7932. flag = va_arg(ap, int);
  7933. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7934. status_kill(&md->bl);
  7935. return 1;
  7936. }
  7937. /**
  7938. * Called when player changes job
  7939. * Rewrote to make it tidider [Celest]
  7940. * @param sd
  7941. * @param job JOB ID. See enum e_job
  7942. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7943. * @return True if success, false if failed
  7944. **/
  7945. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7946. {
  7947. int i, fame_flag = 0;
  7948. int b_class;
  7949. nullpo_retr(false,sd);
  7950. if (job < 0)
  7951. return false;
  7952. //Normalize job.
  7953. b_class = pc_jobid2mapid(job);
  7954. if (b_class == -1)
  7955. return false;
  7956. switch (upper) {
  7957. case 1:
  7958. b_class|= JOBL_UPPER;
  7959. break;
  7960. case 2:
  7961. b_class|= JOBL_BABY;
  7962. break;
  7963. }
  7964. //This will automatically adjust bard/dancer classes to the correct gender
  7965. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7966. job = pc_mapid2jobid(b_class, sd->status.sex);
  7967. if (job == -1)
  7968. return false;
  7969. if ((unsigned short)b_class == sd->class_)
  7970. return false; //Nothing to change.
  7971. // changing from 1st to 2nd job
  7972. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7973. sd->change_level_2nd = sd->status.job_level;
  7974. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  7975. }
  7976. // changing from 2nd to 3rd job
  7977. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7978. sd->change_level_3rd = sd->status.job_level;
  7979. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  7980. }
  7981. if(sd->cloneskill_idx > 0) {
  7982. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7983. sd->status.skill[sd->cloneskill_idx].id = 0;
  7984. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7985. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7986. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM))));
  7987. }
  7988. sd->cloneskill_idx = 0;
  7989. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7990. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7991. }
  7992. if(sd->reproduceskill_idx > 0) {
  7993. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7994. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7995. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7996. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7997. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE))));
  7998. }
  7999. sd->reproduceskill_idx = 0;
  8000. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  8001. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  8002. }
  8003. // Give or reduce transcendent status points
  8004. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  8005. sd->status.status_point += battle_config.transcendent_status_points;
  8006. clif_updatestatus(sd,SP_STATUSPOINT);
  8007. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  8008. if( sd->status.status_point < battle_config.transcendent_status_points ){
  8009. // The player already used his bonus points, so we have to reset his status points
  8010. pc_resetstate(sd);
  8011. }
  8012. sd->status.status_point -= battle_config.transcendent_status_points;
  8013. clif_updatestatus(sd,SP_STATUSPOINT);
  8014. }
  8015. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  8016. const int class_ = pc_class2idx(sd->status.class_);
  8017. uint16 skill_id;
  8018. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  8019. //Remove status specific to your current tree skills.
  8020. enum sc_type sc = status_skill2sc(skill_id);
  8021. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  8022. status_change_end(&sd->bl, sc, INVALID_TIMER);
  8023. }
  8024. }
  8025. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  8026. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  8027. pc_resetfeel(sd);
  8028. }
  8029. // Reset body style to 0 before changing job to avoid
  8030. // errors since not every job has a alternate outfit.
  8031. sd->status.body = 0;
  8032. clif_changelook(&sd->bl,LOOK_BODY2,0);
  8033. sd->status.class_ = job;
  8034. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  8035. sd->class_ = (unsigned short)b_class;
  8036. sd->status.job_level=1;
  8037. sd->status.job_exp=0;
  8038. if (sd->status.base_level > pc_maxbaselv(sd)) {
  8039. sd->status.base_level = pc_maxbaselv(sd);
  8040. sd->status.base_exp=0;
  8041. pc_resetstate(sd);
  8042. clif_updatestatus(sd,SP_STATUSPOINT);
  8043. clif_updatestatus(sd,SP_BASELEVEL);
  8044. clif_updatestatus(sd,SP_BASEEXP);
  8045. clif_updatestatus(sd,SP_NEXTBASEEXP);
  8046. }
  8047. clif_updatestatus(sd,SP_JOBLEVEL);
  8048. clif_updatestatus(sd,SP_JOBEXP);
  8049. clif_updatestatus(sd,SP_NEXTJOBEXP);
  8050. for(i=0;i<EQI_MAX;i++) {
  8051. if(sd->equip_index[i] >= 0)
  8052. if(pc_isequip(sd,sd->equip_index[i]))
  8053. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  8054. }
  8055. //Change look, if disguised, you need to undisguise
  8056. //to correctly calculate new job sprite without
  8057. if (sd->disguise)
  8058. pc_disguise(sd, 0);
  8059. status_set_viewdata(&sd->bl, job);
  8060. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  8061. #if PACKETVER >= 20151001
  8062. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  8063. #endif
  8064. if(sd->vd.cloth_color)
  8065. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8066. /*
  8067. if(sd->vd.body_style)
  8068. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8069. */
  8070. //Update skill tree.
  8071. pc_calc_skilltree(sd);
  8072. clif_skillinfoblock(sd);
  8073. if (sd->ed)
  8074. elemental_delete(sd->ed);
  8075. if (sd->state.vending)
  8076. vending_closevending(sd);
  8077. if (sd->state.buyingstore)
  8078. buyingstore_close(sd);
  8079. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  8080. //Remove peco/cart/falcon
  8081. i = sd->sc.option;
  8082. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  8083. i&=~OPTION_RIDING;
  8084. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  8085. i&=~OPTION_FALCON;
  8086. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  8087. i&=~OPTION_DRAGON;
  8088. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  8089. i&=~OPTION_WUGRIDER;
  8090. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  8091. i&=~OPTION_WUG;
  8092. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  8093. i&=~OPTION_MADOGEAR;
  8094. #ifndef NEW_CARTS
  8095. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  8096. i&=~OPTION_CART;
  8097. #else
  8098. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  8099. pc_setcart(sd, 0);
  8100. #endif
  8101. if(i != sd->sc.option)
  8102. pc_setoption(sd, i);
  8103. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  8104. hom_vaporize(sd, HOM_ST_ACTIVE);
  8105. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  8106. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  8107. if(sd->status.manner < 0)
  8108. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  8109. status_calc_pc(sd,SCO_FORCE);
  8110. pc_checkallowskill(sd);
  8111. pc_equiplookall(sd);
  8112. pc_show_questinfo(sd);
  8113. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  8114. if( sd->status.party_id ){
  8115. struct party_data* p;
  8116. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  8117. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  8118. if( i < MAX_PARTY ){
  8119. p->party.member[i].class_ = sd->status.class_;
  8120. clif_party_job_and_level(sd);
  8121. }
  8122. }
  8123. }
  8124. chrif_save(sd, CSAVE_NORMAL);
  8125. //if you were previously famous, not anymore.
  8126. if (fame_flag)
  8127. chrif_buildfamelist();
  8128. else if (sd->status.fame > 0) {
  8129. //It may be that now they are famous?
  8130. switch (sd->class_&MAPID_UPPERMASK) {
  8131. case MAPID_BLACKSMITH:
  8132. case MAPID_ALCHEMIST:
  8133. case MAPID_TAEKWON:
  8134. chrif_buildfamelist();
  8135. break;
  8136. }
  8137. }
  8138. return true;
  8139. }
  8140. /*==========================================
  8141. * Tell client player sd has change equipement
  8142. *------------------------------------------*/
  8143. void pc_equiplookall(struct map_session_data *sd)
  8144. {
  8145. nullpo_retv(sd);
  8146. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8147. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8148. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8149. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8150. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8151. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  8152. }
  8153. /*==========================================
  8154. * Tell client player sd has change look (hair,equip...)
  8155. *------------------------------------------*/
  8156. void pc_changelook(struct map_session_data *sd,int type,int val) {
  8157. nullpo_retv(sd);
  8158. switch(type) {
  8159. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  8160. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  8161. if (sd->status.hair != val) {
  8162. sd->status.hair = val;
  8163. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8164. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8165. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  8166. }
  8167. break;
  8168. case LOOK_WEAPON:
  8169. sd->status.weapon = val;
  8170. break;
  8171. case LOOK_HEAD_BOTTOM:
  8172. sd->status.head_bottom = val;
  8173. sd->setlook_head_bottom = val;
  8174. break;
  8175. case LOOK_HEAD_TOP:
  8176. sd->status.head_top = val;
  8177. sd->setlook_head_top = val;
  8178. break;
  8179. case LOOK_HEAD_MID:
  8180. sd->status.head_mid = val;
  8181. sd->setlook_head_mid = val;
  8182. break;
  8183. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  8184. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  8185. if (sd->status.hair_color != val) {
  8186. sd->status.hair_color = val;
  8187. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8188. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8189. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  8190. }
  8191. break;
  8192. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  8193. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  8194. sd->status.clothes_color = val;
  8195. break;
  8196. case LOOK_SHIELD:
  8197. sd->status.shield = val;
  8198. break;
  8199. case LOOK_SHOES:
  8200. break;
  8201. case LOOK_ROBE:
  8202. sd->status.robe = val;
  8203. sd->setlook_robe = val;
  8204. break;
  8205. case LOOK_BODY2:
  8206. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  8207. sd->status.body = val;
  8208. break;
  8209. }
  8210. clif_changelook(&sd->bl, type, val);
  8211. }
  8212. /*==========================================
  8213. * Give an option (type) to player (sd) and display it to client
  8214. *------------------------------------------*/
  8215. void pc_setoption(struct map_session_data *sd,int type)
  8216. {
  8217. int p_type, new_look=0;
  8218. nullpo_retv(sd);
  8219. p_type = sd->sc.option;
  8220. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  8221. sd->sc.option=type;
  8222. clif_changeoption(&sd->bl);
  8223. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8224. { // Mounting
  8225. clif_status_load(&sd->bl,EFST_RIDING,1);
  8226. status_calc_pc(sd,SCO_NONE);
  8227. }
  8228. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8229. { // Dismount
  8230. clif_status_load(&sd->bl,EFST_RIDING,0);
  8231. status_calc_pc(sd,SCO_NONE);
  8232. }
  8233. #ifndef NEW_CARTS
  8234. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  8235. clif_cartlist(sd);
  8236. clif_updatestatus(sd, SP_CARTINFO);
  8237. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8238. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  8239. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  8240. clif_clearcart(sd->fd);
  8241. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8242. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  8243. }
  8244. #endif
  8245. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  8246. clif_status_load(&sd->bl,EFST_FALCON,1);
  8247. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  8248. clif_status_load(&sd->bl,EFST_FALCON,0);
  8249. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  8250. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  8251. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  8252. status_calc_pc(sd,SCO_NONE);
  8253. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  8254. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  8255. status_calc_pc(sd,SCO_NONE);
  8256. }
  8257. }
  8258. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  8259. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  8260. static const sc_type statuses [] = { SC_MAXIMIZEPOWER, SC_OVERTHRUST, SC_WEAPONPERFECTION, SC_ADRENALINE, SC_CARTBOOST, SC_MELTDOWN, SC_MAXOVERTHRUST };
  8261. status_calc_pc(sd,SCO_NONE);
  8262. for (uint8 i = 0; i < ARRAYLENGTH(statuses); i++) {
  8263. int skill_id = status_sc2skill(statuses[i]);
  8264. if (skill_id > 0 && !skill_get_inf2(skill_id, INF2_ALLOWONMADO))
  8265. status_change_end(&sd->bl,statuses[i],INVALID_TIMER);
  8266. }
  8267. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8268. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  8269. status_calc_pc(sd,SCO_NONE);
  8270. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  8271. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  8272. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  8273. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  8274. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  8275. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  8276. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  8277. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  8278. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8279. }
  8280. }
  8281. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  8282. new_look = JOB_STAR_GLADIATOR2;
  8283. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  8284. new_look = -1;
  8285. if (sd->disguise || !new_look)
  8286. return; //Disguises break sprite changes
  8287. if (new_look < 0) { //Restore normal look.
  8288. status_set_viewdata(&sd->bl, sd->status.class_);
  8289. new_look = sd->vd.class_;
  8290. }
  8291. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  8292. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  8293. if (sd->vd.cloth_color)
  8294. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8295. if( sd->vd.body_style )
  8296. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8297. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  8298. }
  8299. /**
  8300. * Give player a cart
  8301. * @param sd Player
  8302. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8303. **/
  8304. bool pc_setcart(struct map_session_data *sd,int type) {
  8305. #ifndef NEW_CARTS
  8306. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8307. int option;
  8308. #endif
  8309. nullpo_retr(false,sd);
  8310. if( type < 0 || type > MAX_CARTS )
  8311. return false;// Never trust the values sent by the client! [Skotlex]
  8312. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8313. return false;// Push cart is required
  8314. #ifdef NEW_CARTS
  8315. switch( type ) {
  8316. case 0:
  8317. if( !sd->sc.data[SC_PUSH_CART] )
  8318. return 0;
  8319. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8320. clif_clearcart(sd->fd);
  8321. break;
  8322. default:/* everything else is an allowed ID so we can move on */
  8323. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8324. clif_cartlist(sd);
  8325. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8326. }
  8327. clif_updatestatus(sd, SP_CARTINFO);
  8328. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8329. break;
  8330. }
  8331. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8332. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8333. #else
  8334. // Update option
  8335. option = sd->sc.option;
  8336. option &= ~OPTION_CART;// clear cart bits
  8337. option |= cart[type]; // set cart
  8338. pc_setoption(sd, option);
  8339. #endif
  8340. return true;
  8341. }
  8342. /*==========================================
  8343. * Give player a falcon
  8344. *------------------------------------------*/
  8345. void pc_setfalcon(struct map_session_data* sd, int flag)
  8346. {
  8347. if( flag ){
  8348. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8349. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8350. } else if( pc_isfalcon(sd) ){
  8351. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8352. }
  8353. }
  8354. /*==========================================
  8355. * Set player riding
  8356. *------------------------------------------*/
  8357. void pc_setriding(struct map_session_data* sd, int flag)
  8358. {
  8359. if( sd->sc.data[SC_ALL_RIDING] )
  8360. return;
  8361. if( flag ){
  8362. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8363. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8364. } else if( pc_isriding(sd) ){
  8365. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8366. }
  8367. }
  8368. /*==========================================
  8369. * Give player a mado
  8370. *------------------------------------------*/
  8371. void pc_setmadogear(struct map_session_data* sd, int flag)
  8372. {
  8373. if( flag ){
  8374. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8375. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8376. } else if( pc_ismadogear(sd) ){
  8377. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8378. }
  8379. }
  8380. /*==========================================
  8381. * Check if player can drop an item
  8382. *------------------------------------------*/
  8383. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8384. {
  8385. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  8386. return false;
  8387. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8388. return false;
  8389. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8390. }
  8391. /**
  8392. * Determines whether a player can attack based on status changes
  8393. * Why not use status_check_skilluse?
  8394. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8395. * Even ground-based attacks should be blocked by these statuses
  8396. * Called from unit_attack and unit_attack_timer_sub
  8397. * @retval true Can attack
  8398. **/
  8399. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8400. nullpo_retr(false, sd);
  8401. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8402. return false;
  8403. if (sd->state.block_action & PCBLOCK_ATTACK)
  8404. return false;
  8405. if(
  8406. #ifdef RENEWAL
  8407. sd->sc.data[SC_BASILICA_CELL] ||
  8408. #else
  8409. sd->sc.data[SC_BASILICA] ||
  8410. #endif
  8411. sd->sc.data[SC__SHADOWFORM] ||
  8412. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8413. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8414. sd->sc.data[SC_CRYSTALIZE] ||
  8415. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8416. sd->sc.data[SC_TRICKDEAD] ||
  8417. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8418. sd->sc.data[SC_BLADESTOP] ||
  8419. sd->sc.data[SC_DEEPSLEEP] ||
  8420. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8421. sd->sc.data[SC_KINGS_GRACE] )
  8422. return false;
  8423. return true;
  8424. }
  8425. /*==========================================
  8426. * Read '@type' variables (temporary numeric char reg)
  8427. *------------------------------------------*/
  8428. int64 pc_readreg(struct map_session_data* sd, int64 reg)
  8429. {
  8430. return i64db_i64get(sd->regs.vars, reg);
  8431. }
  8432. /*==========================================
  8433. * Set '@type' variables (temporary numeric char reg)
  8434. *------------------------------------------*/
  8435. bool pc_setreg(struct map_session_data* sd, int64 reg, int64 val)
  8436. {
  8437. uint32 index = script_getvaridx(reg);
  8438. nullpo_retr(false, sd);
  8439. if( val ) {
  8440. i64db_i64put(sd->regs.vars, reg, val);
  8441. if( index )
  8442. script_array_update(&sd->regs, reg, false);
  8443. } else {
  8444. i64db_remove(sd->regs.vars, reg);
  8445. if( index )
  8446. script_array_update(&sd->regs, reg, true);
  8447. }
  8448. return true;
  8449. }
  8450. /*==========================================
  8451. * Read '@type$' variables (temporary string char reg)
  8452. *------------------------------------------*/
  8453. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8454. {
  8455. struct script_reg_str *p = NULL;
  8456. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8457. return p ? p->value : NULL;
  8458. }
  8459. /*==========================================
  8460. * Set '@type$' variables (temporary string char reg)
  8461. *------------------------------------------*/
  8462. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8463. {
  8464. struct script_reg_str *p = NULL;
  8465. unsigned int index = script_getvaridx(reg);
  8466. DBData prev;
  8467. nullpo_retr(false, sd);
  8468. if( str[0] ) {
  8469. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8470. p->value = aStrdup(str);
  8471. p->flag.type = 1;
  8472. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8473. p = (struct script_reg_str *)db_data2ptr(&prev);
  8474. if( p->value )
  8475. aFree(p->value);
  8476. ers_free(str_reg_ers, p);
  8477. } else {
  8478. if( index )
  8479. script_array_update(&sd->regs, reg, false);
  8480. }
  8481. } else {
  8482. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8483. p = (struct script_reg_str *)db_data2ptr(&prev);
  8484. if( p->value )
  8485. aFree(p->value);
  8486. ers_free(str_reg_ers, p);
  8487. if( index )
  8488. script_array_update(&sd->regs, reg, true);
  8489. }
  8490. }
  8491. return true;
  8492. }
  8493. /**
  8494. * Serves the following variable types:
  8495. * - 'type' (permanent numeric char reg)
  8496. * - '#type' (permanent numeric account reg)
  8497. * - '##type' (permanent numeric account reg2)
  8498. **/
  8499. int64 pc_readregistry(struct map_session_data *sd, int64 reg)
  8500. {
  8501. struct script_reg_num *p = NULL;
  8502. if (!sd->vars_ok) {
  8503. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8504. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8505. //intif->request_registry(sd,type==3?4:type);
  8506. set_eof(sd->fd);
  8507. return 0;
  8508. }
  8509. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8510. return p ? p->value : 0;
  8511. }
  8512. /**
  8513. * Serves the following variable types:
  8514. * - 'type$' (permanent str char reg)
  8515. * - '#type$' (permanent str account reg)
  8516. * - '##type$' (permanent str account reg2)
  8517. **/
  8518. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8519. {
  8520. struct script_reg_str *p = NULL;
  8521. if (!sd->vars_ok) {
  8522. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8523. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8524. //intif->request_registry(sd,type==3?4:type);
  8525. set_eof(sd->fd);
  8526. return NULL;
  8527. }
  8528. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8529. return p ? p->value : NULL;
  8530. }
  8531. /**
  8532. * Serves the following variable types:
  8533. * - 'type' (permanent numeric char reg)
  8534. * - '#type' (permanent numeric account reg)
  8535. * - '##type' (permanent numeric account reg2)
  8536. **/
  8537. bool pc_setregistry(struct map_session_data *sd, int64 reg, int64 val)
  8538. {
  8539. struct script_reg_num *p = NULL;
  8540. const char *regname = get_str(script_getvarid(reg));
  8541. uint32 index = script_getvaridx(reg);
  8542. if ( !reg_load && !sd->vars_ok ) {
  8543. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8544. return false;
  8545. }
  8546. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8547. if( val ) {
  8548. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8549. script_array_update(&sd->regs, reg, false);
  8550. p->value = val;
  8551. } else {
  8552. p->value = 0;
  8553. if( index )
  8554. script_array_update(&sd->regs, reg, true);
  8555. }
  8556. if (!reg_load)
  8557. p->flag.update = 1;/* either way, it will require either delete or replace */
  8558. } else if( val ) {
  8559. DBData prev;
  8560. if( index )
  8561. script_array_update(&sd->regs, reg, false);
  8562. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8563. p->value = val;
  8564. if (!reg_load)
  8565. p->flag.update = 1;
  8566. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8567. p = (struct script_reg_num *)db_data2ptr(&prev);
  8568. ers_free(num_reg_ers, p);
  8569. }
  8570. }
  8571. if (!reg_load && p)
  8572. sd->vars_dirty = true;
  8573. return true;
  8574. }
  8575. /**
  8576. * Serves the following variable types:
  8577. * - 'type$' (permanent str char reg)
  8578. * - '#type$' (permanent str account reg)
  8579. * - '##type$' (permanent str account reg2)
  8580. **/
  8581. bool pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8582. {
  8583. struct script_reg_str *p = NULL;
  8584. const char *regname = get_str(script_getvarid(reg));
  8585. unsigned int index = script_getvaridx(reg);
  8586. size_t vlen = 0;
  8587. if (!reg_load && !sd->vars_ok) {
  8588. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8589. return false;
  8590. }
  8591. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8592. {
  8593. 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);
  8594. return false;
  8595. }
  8596. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8597. if( val[0] ) {
  8598. if( p->value )
  8599. aFree(p->value);
  8600. else if ( index ) // an entry that was deleted, so we reset
  8601. script_array_update(&sd->regs, reg, false);
  8602. p->value = aStrdup(val);
  8603. } else {
  8604. if (p->value)
  8605. aFree(p->value);
  8606. p->value = NULL;
  8607. if( index )
  8608. script_array_update(&sd->regs, reg, true);
  8609. }
  8610. if( !reg_load )
  8611. p->flag.update = 1; // either way, it will require either delete or replace
  8612. } else if( val[0] ) {
  8613. DBData prev;
  8614. if( index )
  8615. script_array_update(&sd->regs, reg, false);
  8616. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8617. p->value = aStrdup(val);
  8618. if( !reg_load )
  8619. p->flag.update = 1;
  8620. p->flag.type = 1;
  8621. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8622. p = (struct script_reg_str *)db_data2ptr(&prev);
  8623. if( p->value )
  8624. aFree(p->value);
  8625. ers_free(str_reg_ers, p);
  8626. }
  8627. }
  8628. if( !reg_load && p )
  8629. sd->vars_dirty = true;
  8630. return true;
  8631. }
  8632. /**
  8633. * Set value of player variable
  8634. * @param sd Player
  8635. * @param reg Variable name
  8636. * @param value
  8637. * @return True if success, false if failed.
  8638. **/
  8639. bool pc_setreg2(struct map_session_data *sd, const char *reg, int64 val) {
  8640. char prefix = reg[0];
  8641. nullpo_retr(false, sd);
  8642. if (reg[strlen(reg)-1] == '$') {
  8643. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8644. return false;
  8645. }
  8646. val = cap_value(val, INT_MIN, INT_MAX);
  8647. switch (prefix) {
  8648. case '.':
  8649. case '\'':
  8650. case '$':
  8651. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8652. return false;
  8653. case '@':
  8654. return pc_setreg(sd, add_str(reg), val);
  8655. case '#':
  8656. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  8657. default:
  8658. return pc_setglobalreg(sd, add_str(reg), val);
  8659. }
  8660. return false;
  8661. }
  8662. /**
  8663. * Get value of player variable
  8664. * @param sd Player
  8665. * @param reg Variable name
  8666. * @return Variable value or 0 if failed.
  8667. **/
  8668. int64 pc_readreg2(struct map_session_data *sd, const char *reg) {
  8669. char prefix = reg[0];
  8670. nullpo_ret(sd);
  8671. if (reg[strlen(reg)-1] == '$') {
  8672. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8673. return 0;
  8674. }
  8675. switch (prefix) {
  8676. case '.':
  8677. case '\'':
  8678. case '$':
  8679. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8680. return 0;
  8681. case '@':
  8682. return pc_readreg(sd, add_str(reg));
  8683. case '#':
  8684. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8685. default:
  8686. return pc_readglobalreg(sd, add_str(reg));
  8687. }
  8688. return 0;
  8689. }
  8690. /*==========================================
  8691. * Exec eventtimer for player sd (retrieved from map_session (id))
  8692. *------------------------------------------*/
  8693. static TIMER_FUNC(pc_eventtimer){
  8694. struct map_session_data *sd=map_id2sd(id);
  8695. char *p = (char *)data;
  8696. int i;
  8697. if(sd==NULL)
  8698. return 0;
  8699. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8700. if( i < MAX_EVENTTIMER )
  8701. {
  8702. sd->eventtimer[i] = INVALID_TIMER;
  8703. sd->eventcount--;
  8704. npc_event(sd,p,0);
  8705. }
  8706. else
  8707. ShowError("pc_eventtimer: no such event timer\n");
  8708. if (p) aFree(p);
  8709. return 0;
  8710. }
  8711. /*==========================================
  8712. * Add eventtimer for player sd ?
  8713. *------------------------------------------*/
  8714. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8715. {
  8716. int i;
  8717. nullpo_retr(false,sd);
  8718. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8719. if( i == MAX_EVENTTIMER )
  8720. return false;
  8721. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8722. sd->eventcount++;
  8723. return true;
  8724. }
  8725. /*==========================================
  8726. * Del eventtimer for player sd ?
  8727. *------------------------------------------*/
  8728. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8729. {
  8730. char* p = NULL;
  8731. int i;
  8732. nullpo_retr(false,sd);
  8733. if (sd->eventcount == 0)
  8734. return false;
  8735. // find the named event timer
  8736. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8737. sd->eventtimer[i] != INVALID_TIMER &&
  8738. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8739. strcmp(p, name) == 0
  8740. );
  8741. if( i == MAX_EVENTTIMER )
  8742. return false; // not found
  8743. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8744. sd->eventtimer[i] = INVALID_TIMER;
  8745. if(sd->eventcount > 0)
  8746. sd->eventcount--;
  8747. aFree(p);
  8748. return true;
  8749. }
  8750. /*==========================================
  8751. * Update eventtimer count for player sd
  8752. *------------------------------------------*/
  8753. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8754. {
  8755. int i;
  8756. nullpo_retv(sd);
  8757. for(i=0;i<MAX_EVENTTIMER;i++)
  8758. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8759. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8760. addt_tickimer(sd->eventtimer[i],tick);
  8761. break;
  8762. }
  8763. }
  8764. /*==========================================
  8765. * Remove all eventtimer for player sd
  8766. *------------------------------------------*/
  8767. void pc_cleareventtimer(struct map_session_data *sd)
  8768. {
  8769. int i;
  8770. nullpo_retv(sd);
  8771. if (sd->eventcount == 0)
  8772. return;
  8773. for(i=0;i<MAX_EVENTTIMER;i++){
  8774. if( sd->eventtimer[i] != INVALID_TIMER ){
  8775. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8776. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8777. sd->eventtimer[i] = INVALID_TIMER;
  8778. if(sd->eventcount > 0) //avoid looping to max val
  8779. sd->eventcount--;
  8780. if (p) aFree(p);
  8781. }
  8782. }
  8783. }
  8784. /**
  8785. * Called when an item with combo is worn
  8786. * @param *sd
  8787. * @param *data struct item_data
  8788. * @return success numbers of succeed combo
  8789. */
  8790. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8791. uint16 i;
  8792. int success = 0;
  8793. for( i = 0; i < data->combos_count; i++ ) {
  8794. struct itemchk {
  8795. int idx;
  8796. unsigned short nameid;
  8797. short card[MAX_SLOTS];
  8798. } *combo_idx;
  8799. int idx, j;
  8800. int nb_itemCombo;
  8801. unsigned int pos = 0;
  8802. /* ensure this isn't a duplicate combo */
  8803. if( sd->combos.bonus != NULL ) {
  8804. int x;
  8805. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8806. /* found a match, skip this combo */
  8807. if( x < sd->combos.count )
  8808. continue;
  8809. }
  8810. nb_itemCombo = data->combos[i]->count;
  8811. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8812. continue;
  8813. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8814. for(j=0; j < nb_itemCombo; j++){
  8815. combo_idx[j].idx=-1;
  8816. combo_idx[j].nameid=-1;
  8817. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8818. }
  8819. for( j = 0; j < nb_itemCombo; j++ ) {
  8820. uint16 id = data->combos[i]->nameid[j], k;
  8821. bool found = false;
  8822. for( k = 0; k < EQI_MAX; k++ ) {
  8823. short index = sd->equip_index[k];
  8824. if( index < 0 )
  8825. continue;
  8826. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8827. continue;
  8828. if (!sd->inventory_data[index] )
  8829. continue;
  8830. if ( itemdb_type(id) != IT_CARD ) {
  8831. if ( sd->inventory_data[index]->nameid != id )
  8832. continue;
  8833. if(j>0){ //check if this item not already used
  8834. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8835. uint8 z;
  8836. for (z = 0; z < nb_itemCombo-1; z++)
  8837. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8838. do_continue=true;
  8839. if(do_continue)
  8840. continue;
  8841. }
  8842. combo_idx[j].nameid = id;
  8843. combo_idx[j].idx = index;
  8844. pos |= sd->inventory.u.items_inventory[index].equip;
  8845. found = true;
  8846. break;
  8847. } else { //Cards and enchants
  8848. uint16 z;
  8849. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8850. continue;
  8851. for (z = 0; z < MAX_SLOTS; z++) {
  8852. bool do_continue=false;
  8853. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8854. continue;
  8855. if(j>0){
  8856. int c1, c2;
  8857. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8858. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8859. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8860. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8861. do_continue = true;
  8862. break;
  8863. }
  8864. }
  8865. }
  8866. }
  8867. }
  8868. if(do_continue)
  8869. continue;
  8870. combo_idx[j].nameid = id;
  8871. combo_idx[j].idx = index;
  8872. combo_idx[j].card[z] = id;
  8873. pos |= sd->inventory.u.items_inventory[index].equip;
  8874. found = true;
  8875. break;
  8876. }
  8877. }
  8878. }
  8879. if( !found )
  8880. break;/* we haven't found all the ids for this combo, so we can return */
  8881. }
  8882. aFree(combo_idx);
  8883. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8884. if( j < nb_itemCombo )
  8885. continue;
  8886. /* we got here, means all items in the combo are matching */
  8887. idx = sd->combos.count;
  8888. if( sd->combos.bonus == NULL ) {
  8889. CREATE(sd->combos.bonus, struct script_code *, 1);
  8890. CREATE(sd->combos.id, unsigned short, 1);
  8891. CREATE(sd->combos.pos, unsigned int, 1);
  8892. sd->combos.count = 1;
  8893. } else {
  8894. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8895. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8896. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8897. }
  8898. /* we simply copy the pointer */
  8899. sd->combos.bonus[idx] = data->combos[i]->script;
  8900. /* save this combo's id */
  8901. sd->combos.id[idx] = data->combos[i]->id;
  8902. /* save pos of combo*/
  8903. sd->combos.pos[idx] = pos;
  8904. success++;
  8905. }
  8906. return success;
  8907. }
  8908. /**
  8909. * Called when an item with combo is removed
  8910. * @param *sd
  8911. * @param *data struct item_data
  8912. * @return retval numbers of removed combo
  8913. */
  8914. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8915. int i, retval = 0;
  8916. if( sd->combos.bonus == NULL )
  8917. return 0;/* nothing to do here, player has no combos */
  8918. for( i = 0; i < data->combos_count; i++ ) {
  8919. /* check if this combo exists in this user */
  8920. int x = 0, cursor = 0, j;
  8921. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8922. /* no match, skip this combo */
  8923. if(x >= sd->combos.count)
  8924. continue;
  8925. sd->combos.bonus[x] = NULL;
  8926. sd->combos.id[x] = 0;
  8927. sd->combos.pos[x] = 0;
  8928. retval++;
  8929. /* check if combo requirements still fit */
  8930. if( pc_checkcombo( sd, data ) )
  8931. continue;
  8932. /* move next value to empty slot */
  8933. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8934. if( sd->combos.bonus[j] == NULL )
  8935. continue;
  8936. if( cursor != j ) {
  8937. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8938. sd->combos.id[cursor] = sd->combos.id[j];
  8939. sd->combos.pos[cursor] = sd->combos.pos[j];
  8940. }
  8941. cursor++;
  8942. }
  8943. /* it's empty, we can clear all the memory */
  8944. if( (sd->combos.count = cursor) == 0 ) {
  8945. aFree(sd->combos.bonus);
  8946. aFree(sd->combos.id);
  8947. aFree(sd->combos.pos);
  8948. sd->combos.bonus = NULL;
  8949. sd->combos.id = NULL;
  8950. sd->combos.pos = NULL;
  8951. return retval; /* we also can return at this point for we have no more combos to check */
  8952. }
  8953. }
  8954. return retval;
  8955. }
  8956. /**
  8957. * Load combo data(s) of player
  8958. * @param *sd
  8959. * @return ret numbers of succeed combo
  8960. */
  8961. int pc_load_combo(struct map_session_data *sd) {
  8962. int i, ret = 0;
  8963. for( i = 0; i < EQI_MAX; i++ ) {
  8964. struct item_data *id = NULL;
  8965. short idx = sd->equip_index[i];
  8966. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8967. continue;
  8968. if( id->combos_count )
  8969. ret += pc_checkcombo(sd,id);
  8970. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8971. struct item_data *data;
  8972. int j;
  8973. for( j = 0; j < MAX_SLOTS; j++ ) {
  8974. if (!sd->inventory.u.items_inventory[idx].card[j])
  8975. continue;
  8976. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8977. if( data->combos_count )
  8978. ret += pc_checkcombo(sd,data);
  8979. }
  8980. }
  8981. }
  8982. }
  8983. return ret;
  8984. }
  8985. /*==========================================
  8986. * Equip item on player sd at req_pos from inventory index n
  8987. * return: false - fail; true - success
  8988. *------------------------------------------*/
  8989. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  8990. {
  8991. int i, pos, flag = 0, iflag;
  8992. struct item_data *id;
  8993. uint8 res = ITEM_EQUIP_ACK_OK;
  8994. short* equip_index;
  8995. nullpo_retr(false,sd);
  8996. if( n < 0 || n >= MAX_INVENTORY ) {
  8997. if( equipswitch ){
  8998. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8999. }else{
  9000. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  9001. }
  9002. return false;
  9003. }
  9004. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  9005. if( equipswitch ){
  9006. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9007. }else{
  9008. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  9009. }
  9010. return false;
  9011. }
  9012. if (!(id = sd->inventory_data[n]))
  9013. return false;
  9014. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  9015. if(battle_config.battle_log && !equipswitch)
  9016. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  9017. if((res = pc_isequip(sd,n))) {
  9018. if( equipswitch ){
  9019. clif_equipswitch_add( sd, n, req_pos, res );
  9020. }else{
  9021. clif_equipitemack(sd,n,0,res); // fail
  9022. }
  9023. return false;
  9024. }
  9025. if( equipswitch && id->type == IT_AMMO ){
  9026. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9027. return false;
  9028. }
  9029. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  9030. if( equipswitch ){
  9031. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9032. }else{
  9033. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  9034. }
  9035. return false;
  9036. }
  9037. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9038. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  9039. if( equipswitch ){
  9040. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9041. }else{
  9042. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  9043. }
  9044. return false;
  9045. }
  9046. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  9047. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  9048. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  9049. clif_notify_bindOnEquip(sd,n);
  9050. }
  9051. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  9052. pos = req_pos&EQP_ACC;
  9053. if (pos == EQP_ACC) //User specified both slots.
  9054. pos = equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  9055. for (i = 0; i < sd->inventory_data[n]->slot; i++) { // Accessories that have cards that force equip location
  9056. if (!sd->inventory.u.items_inventory[n].card[i])
  9057. continue;
  9058. struct item_data *card_data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]);
  9059. if (card_data) {
  9060. int card_pos = card_data->equip;
  9061. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  9062. pos = card_pos; // Use the card's equip position
  9063. break;
  9064. }
  9065. }
  9066. }
  9067. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  9068. pos = (req_pos&EQP_ARMS);
  9069. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  9070. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  9071. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  9072. pos = req_pos&EQP_SHADOW_ACC;
  9073. if (pos == EQP_SHADOW_ACC)
  9074. pos = equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  9075. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  9076. pos = (req_pos&EQP_SHADOW_ARMS);
  9077. if( pos == EQP_SHADOW_ARMS )
  9078. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  9079. }
  9080. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  9081. //Update skill-block range database when weapon range changes. [Skotlex]
  9082. i = equip_index[EQI_HAND_R];
  9083. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  9084. flag = 1;
  9085. else
  9086. flag = id->range != sd->inventory_data[i]->range;
  9087. }
  9088. if( equipswitch ){
  9089. for( i = 0; i < EQI_MAX; i++ ){
  9090. if( pos&equip_bitmask[i] ){
  9091. // If there was already an item assigned to this slot
  9092. if( sd->equip_switch_index[i] >= 0 ){
  9093. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  9094. }
  9095. // Assign the new index to it
  9096. sd->equip_switch_index[i] = n;
  9097. }
  9098. }
  9099. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  9100. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  9101. return true;
  9102. }else{
  9103. for(i=0;i<EQI_MAX;i++) {
  9104. if(pos & equip_bitmask[i]) {
  9105. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  9106. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  9107. sd->equip_index[i] = n;
  9108. }
  9109. }
  9110. pc_equipswitch_remove(sd, n);
  9111. if(pos==EQP_AMMO) {
  9112. clif_arrowequip(sd,n);
  9113. clif_arrow_fail(sd,3);
  9114. }
  9115. else
  9116. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  9117. sd->inventory.u.items_inventory[n].equip = pos;
  9118. }
  9119. if(pos & EQP_HAND_R) {
  9120. if(id)
  9121. sd->weapontype1 = id->look;
  9122. else
  9123. sd->weapontype1 = 0;
  9124. pc_calcweapontype(sd);
  9125. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9126. }
  9127. if(pos & EQP_HAND_L) {
  9128. if(id) {
  9129. if(id->type == IT_WEAPON) {
  9130. sd->status.shield = 0;
  9131. sd->weapontype2 = id->look;
  9132. }
  9133. else
  9134. if(id->type == IT_ARMOR) {
  9135. sd->status.shield = id->look;
  9136. sd->weapontype2 = 0;
  9137. }
  9138. }
  9139. else
  9140. sd->status.shield = sd->weapontype2 = 0;
  9141. pc_calcweapontype(sd);
  9142. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9143. }
  9144. if(pos & EQP_SHOES)
  9145. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9146. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  9147. short idx = sd->equip_index[EQI_AMMO];
  9148. if (idx >= 0) {
  9149. switch (sd->inventory_data[idx]->look) {
  9150. case AMMO_ARROW:
  9151. if (id->look != W_BOW && id->look != W_MUSICAL && id->look != W_WHIP)
  9152. pc_unequipitem(sd, idx, 2 | 4);
  9153. break;
  9154. case AMMO_BULLET:
  9155. case AMMO_SHELL:
  9156. if (id->look != W_REVOLVER && id->look != W_RIFLE && id->look != W_GATLING && id->look != W_SHOTGUN
  9157. #ifdef RENEWAL
  9158. && id->look != W_GRENADE
  9159. #endif
  9160. )
  9161. pc_unequipitem(sd, idx, 2 | 4);
  9162. break;
  9163. #ifndef RENEWAL
  9164. case AMMO_GRENADE:
  9165. if (id->look != W_GRENADE)
  9166. pc_unequipitem(sd, idx, 2 | 4);
  9167. break;
  9168. #endif
  9169. }
  9170. }
  9171. }
  9172. pc_set_costume_view(sd);
  9173. pc_checkallowskill(sd); //Check if status changes should be halted.
  9174. iflag = sd->npc_item_flag;
  9175. /* check for combos (MUST be before status_calc_pc) */
  9176. if( id->combos_count )
  9177. pc_checkcombo(sd,id);
  9178. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9179. ; //No cards
  9180. else {
  9181. for( i = 0; i < MAX_SLOTS; i++ ) {
  9182. struct item_data *data;
  9183. if (!sd->inventory.u.items_inventory[n].card[i])
  9184. continue;
  9185. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9186. if( data->combos_count )
  9187. pc_checkcombo(sd,data);
  9188. }
  9189. }
  9190. }
  9191. status_calc_pc(sd,SCO_NONE);
  9192. if (flag) //Update skill data
  9193. clif_skillinfoblock(sd);
  9194. //OnEquip script [Skotlex]
  9195. if (id) {
  9196. //only run the script if item isn't restricted
  9197. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  9198. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9199. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9200. ; //No cards
  9201. else {
  9202. for( i = 0; i < MAX_SLOTS; i++ ) {
  9203. struct item_data *data;
  9204. if (!sd->inventory.u.items_inventory[n].card[i])
  9205. continue;
  9206. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9207. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  9208. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9209. }
  9210. }
  9211. }
  9212. }
  9213. sd->npc_item_flag = iflag;
  9214. return true;
  9215. }
  9216. /**
  9217. * Recalculate player status on unequip
  9218. * @param sd: Player data
  9219. * @param n: Item inventory index
  9220. * @param flag: Whether to recalculate a player's status or not
  9221. * @return True on success or false on failure
  9222. */
  9223. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  9224. int i, iflag;
  9225. bool status_calc = false;
  9226. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  9227. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  9228. sd->inventory.u.items_inventory[n].equip = 0;
  9229. pc_checkallowskill(sd);
  9230. iflag = sd->npc_item_flag;
  9231. /* check for combos (MUST be before status_calc_pc) */
  9232. if (sd->inventory_data[n]) {
  9233. if (sd->inventory_data[n]->combos_count) {
  9234. if (pc_removecombo(sd, sd->inventory_data[n]))
  9235. status_calc = true;
  9236. }
  9237. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9238. ; //No cards
  9239. else {
  9240. for (i = 0; i < MAX_SLOTS; i++) {
  9241. struct item_data *data;
  9242. if (!sd->inventory.u.items_inventory[n].card[i])
  9243. continue;
  9244. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9245. if (data->combos_count) {
  9246. if (pc_removecombo(sd, data))
  9247. status_calc = true;
  9248. }
  9249. }
  9250. }
  9251. }
  9252. }
  9253. if (flag & 1 || status_calc) {
  9254. pc_checkallowskill(sd);
  9255. status_calc_pc(sd, SCO_NONE);
  9256. }
  9257. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  9258. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  9259. //OnUnEquip script [Skotlex]
  9260. if (sd->inventory_data[n]) {
  9261. if (sd->inventory_data[n]->unequip_script)
  9262. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9263. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9264. ; //No cards
  9265. else {
  9266. for (i = 0; i < MAX_SLOTS; i++) {
  9267. struct item_data *data;
  9268. if (!sd->inventory.u.items_inventory[n].card[i])
  9269. continue;
  9270. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9271. if (data->unequip_script)
  9272. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9273. }
  9274. }
  9275. }
  9276. }
  9277. sd->npc_item_flag = iflag;
  9278. }
  9279. /**
  9280. * Called when attempting to unequip an item from a player
  9281. * @param sd: Player data
  9282. * @param n: Item inventory index
  9283. * @param flag: Type of unequip
  9284. * 0 - only unequip
  9285. * 1 - calculate status after unequipping
  9286. * 2 - force unequip
  9287. * 4 - unequip by switching equipment
  9288. * @return True on success or false on failure
  9289. */
  9290. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  9291. int i, pos;
  9292. nullpo_retr(false,sd);
  9293. if (n < 0 || n >= MAX_INVENTORY) {
  9294. clif_unequipitemack(sd,0,0,0);
  9295. return false;
  9296. }
  9297. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  9298. clif_unequipitemack(sd,n,0,0);
  9299. return false; //Nothing to unequip
  9300. }
  9301. // status change that makes player cannot unequip equipment
  9302. if (!(flag&2) && sd->sc.count &&
  9303. (sd->sc.data[SC_BERSERK] ||
  9304. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9305. sd->sc.data[SC__BLOODYLUST] ||
  9306. sd->sc.data[SC_KYOUGAKU] ||
  9307. (sd->sc.data[SC_PYROCLASTIC] &&
  9308. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  9309. {
  9310. clif_unequipitemack(sd,n,0,0);
  9311. return false;
  9312. }
  9313. if (battle_config.battle_log)
  9314. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  9315. for(i = 0; i < EQI_MAX; i++) {
  9316. if (pos & equip_bitmask[i])
  9317. sd->equip_index[i] = -1;
  9318. }
  9319. if(pos & EQP_HAND_R) {
  9320. sd->weapontype1 = 0;
  9321. sd->status.weapon = sd->weapontype2;
  9322. pc_calcweapontype(sd);
  9323. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9324. if( !battle_config.dancing_weaponswitch_fix )
  9325. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  9326. #ifdef RENEWAL
  9327. if (battle_config.switch_remove_edp&2) {
  9328. #else
  9329. if (battle_config.switch_remove_edp&1) {
  9330. #endif
  9331. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  9332. }
  9333. }
  9334. if(pos & EQP_HAND_L) {
  9335. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  9336. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  9337. sd->status.shield = sd->weapontype2 = 0;
  9338. pc_calcweapontype(sd);
  9339. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9340. }
  9341. if(pos & EQP_SHOES)
  9342. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9343. clif_unequipitemack(sd,n,pos,1);
  9344. pc_set_costume_view(sd);
  9345. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  9346. // On weapon change (right and left hand)
  9347. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  9348. if (battle_config.ammo_unequip && !(flag & 4)) {
  9349. switch (sd->inventory_data[n]->look) {
  9350. case W_BOW:
  9351. case W_MUSICAL:
  9352. case W_WHIP:
  9353. case W_REVOLVER:
  9354. case W_RIFLE:
  9355. case W_GATLING:
  9356. case W_SHOTGUN:
  9357. case W_GRENADE: {
  9358. short idx = sd->equip_index[EQI_AMMO];
  9359. if (idx >= 0) {
  9360. sd->equip_index[EQI_AMMO] = -1;
  9361. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  9362. pc_unequipitem_sub(sd, idx, 0);
  9363. }
  9364. }
  9365. break;
  9366. }
  9367. }
  9368. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  9369. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  9370. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  9371. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  9372. }
  9373. // On armor change
  9374. if (pos & EQP_ARMOR) {
  9375. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  9376. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  9377. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  9378. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  9379. }
  9380. // On equipment change
  9381. #ifndef RENEWAL
  9382. if (!(flag&4))
  9383. status_change_end(&sd->bl, SC_CONCENTRATION, INVALID_TIMER);
  9384. #endif
  9385. // On ammo change
  9386. 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_))
  9387. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  9388. pc_unequipitem_sub(sd, n, flag);
  9389. return true;
  9390. }
  9391. int pc_equipswitch( struct map_session_data* sd, int index ){
  9392. // Get the target equip mask
  9393. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  9394. // Get the currently equipped item
  9395. short equippedItem = pc_checkequip( sd, position, true );
  9396. // No item equipped at the target
  9397. if( equippedItem == -1 ){
  9398. // Remove it from the equip switch
  9399. pc_equipswitch_remove( sd, index );
  9400. pc_equipitem( sd, index, position );
  9401. return position;
  9402. }else{
  9403. std::map<int, int> unequipped;
  9404. int unequipped_position = 0;
  9405. // Unequip all items that interfere
  9406. for( int i = 0; i < EQI_MAX; i++ ){
  9407. int unequip_index = sd->equip_index[i];
  9408. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  9409. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  9410. // Store the unequipped index and position mask for later
  9411. unequipped[unequip_index] = unequip_item->equip;
  9412. // Keep the position for later
  9413. unequipped_position |= unequip_item->equip;
  9414. // Unequip the item
  9415. pc_unequipitem( sd, unequip_index, 0 );
  9416. }
  9417. }
  9418. int all_position = position | unequipped_position;
  9419. // Equip everything that is hit by the mask
  9420. for( int i = 0; i < EQI_MAX; i++ ){
  9421. int exchange_index = sd->equip_switch_index[i];
  9422. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  9423. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  9424. // Store the target position
  9425. int exchange_position = exchange_item->equipSwitch;
  9426. // Remove the item from equip switch
  9427. pc_equipswitch_remove( sd, exchange_index );
  9428. // Equip the item at the destinated position
  9429. pc_equipitem( sd, exchange_index, exchange_position );
  9430. }
  9431. }
  9432. // Place all unequipped items into the equip switch window
  9433. for( std::pair<int, int> pair : unequipped ){
  9434. int unequipped_index = pair.first;
  9435. int unequipped_position = pair.second;
  9436. // Rebuild the index cache
  9437. for( int i = 0; i < EQI_MAX; i++ ){
  9438. if( unequipped_position & equip_bitmask[i] ){
  9439. sd->equip_switch_index[i] = unequipped_index;
  9440. }
  9441. }
  9442. // Set the correct position mask
  9443. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  9444. // Notify the client
  9445. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  9446. }
  9447. return all_position;
  9448. }
  9449. }
  9450. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  9451. struct item* item = &sd->inventory.u.items_inventory[index];
  9452. if( !item->equipSwitch ){
  9453. return;
  9454. }
  9455. for( int i = 0; i < EQI_MAX; i++ ){
  9456. // If a match is found
  9457. if( sd->equip_switch_index[i] == index ){
  9458. // Remove it from the slot
  9459. sd->equip_switch_index[i] = -1;
  9460. }
  9461. }
  9462. // Send out one packet for all slots using the current item's mask
  9463. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  9464. item->equipSwitch = 0;
  9465. }
  9466. /*==========================================
  9467. * Checking if player (sd) has an invalid item
  9468. * and is unequiped on map load (item_noequip)
  9469. *------------------------------------------*/
  9470. void pc_checkitem(struct map_session_data *sd) {
  9471. int i, calc_flag = 0;
  9472. struct item* it;
  9473. nullpo_retv(sd);
  9474. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9475. return;
  9476. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9477. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9478. it = &sd->inventory.u.items_inventory[i];
  9479. if( it->nameid == 0 )
  9480. continue;
  9481. if( !it->equip )
  9482. continue;
  9483. if( it->equip&~pc_equippoint(sd,i) ) {
  9484. pc_unequipitem(sd, i, 2);
  9485. calc_flag = 1;
  9486. continue;
  9487. }
  9488. 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) ) {
  9489. pc_unequipitem(sd, i, 2);
  9490. calc_flag = 1;
  9491. continue;
  9492. }
  9493. }
  9494. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9495. it = &sd->inventory.u.items_inventory[i];
  9496. if( it->nameid == 0 )
  9497. continue;
  9498. if( !it->equipSwitch )
  9499. continue;
  9500. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  9501. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  9502. for( int j = 0; j < EQI_MAX; j++ ){
  9503. if( sd->equip_switch_index[j] == i ){
  9504. sd->equip_switch_index[j] = -1;
  9505. }
  9506. }
  9507. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  9508. continue;
  9509. }
  9510. }
  9511. if( calc_flag && sd->state.active ) {
  9512. pc_checkallowskill(sd);
  9513. status_calc_pc(sd,SCO_NONE);
  9514. }
  9515. }
  9516. /*==========================================
  9517. * Checks for unavailable items and removes them.
  9518. * @param sd: Player data
  9519. * @param type Forced check:
  9520. * 1 - Inventory
  9521. * 2 - Cart
  9522. * 4 - Storage
  9523. *------------------------------------------*/
  9524. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9525. {
  9526. int i;
  9527. unsigned short nameid;
  9528. char output[256];
  9529. nullpo_retv(sd);
  9530. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9531. for(i = 0; i < MAX_INVENTORY; i++) {
  9532. nameid = sd->inventory.u.items_inventory[i].nameid;
  9533. if (!nameid)
  9534. continue;
  9535. if (!itemdb_available(nameid)) {
  9536. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  9537. clif_displaymessage(sd->fd, output);
  9538. 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);
  9539. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9540. continue;
  9541. }
  9542. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9543. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9544. }
  9545. }
  9546. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9547. for(i = 0; i < MAX_CART; i++) {
  9548. nameid = sd->cart.u.items_cart[i].nameid;
  9549. if (!nameid)
  9550. continue;
  9551. if (!itemdb_available(nameid)) {
  9552. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  9553. clif_displaymessage(sd->fd, output);
  9554. 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);
  9555. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9556. continue;
  9557. }
  9558. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9559. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9560. }
  9561. }
  9562. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9563. for(i = 0; i < sd->storage.max_amount; i++) {
  9564. nameid = sd->storage.u.items_storage[i].nameid;
  9565. if (!nameid)
  9566. continue;
  9567. if (!itemdb_available(nameid)) {
  9568. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  9569. clif_displaymessage(sd->fd, output);
  9570. 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);
  9571. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9572. continue;
  9573. }
  9574. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9575. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9576. }
  9577. }
  9578. }
  9579. /*==========================================
  9580. * Update PVP rank for sd1 in cmp to sd2
  9581. *------------------------------------------*/
  9582. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9583. {
  9584. struct map_session_data *sd1,*sd2;
  9585. sd1=(struct map_session_data *)bl;
  9586. sd2=va_arg(ap,struct map_session_data *);
  9587. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9588. {// cannot register pvp rank for hidden GMs
  9589. return 0;
  9590. }
  9591. if( sd1->pvp_point > sd2->pvp_point )
  9592. sd2->pvp_rank++;
  9593. return 0;
  9594. }
  9595. /*==========================================
  9596. * Calculate new rank beetween all present players (map_foreachinallarea)
  9597. * and display result
  9598. *------------------------------------------*/
  9599. int pc_calc_pvprank(struct map_session_data *sd)
  9600. {
  9601. int old = sd->pvp_rank;
  9602. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9603. sd->pvp_rank=1;
  9604. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9605. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9606. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9607. return sd->pvp_rank;
  9608. }
  9609. /*==========================================
  9610. * Calculate next sd ranking calculation from config
  9611. *------------------------------------------*/
  9612. TIMER_FUNC(pc_calc_pvprank_timer){
  9613. struct map_session_data *sd;
  9614. sd=map_id2sd(id);
  9615. if(sd==NULL)
  9616. return 0;
  9617. sd->pvp_timer = INVALID_TIMER;
  9618. if( pc_isinvisible(sd) )
  9619. {// do not calculate the pvp rank for a hidden GM
  9620. return 0;
  9621. }
  9622. if( pc_calc_pvprank(sd) > 0 )
  9623. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9624. return 0;
  9625. }
  9626. /*==========================================
  9627. * Checking if sd is married
  9628. * Return:
  9629. * partner_id = yes
  9630. * 0 = no
  9631. *------------------------------------------*/
  9632. int pc_ismarried(struct map_session_data *sd)
  9633. {
  9634. if(sd == NULL)
  9635. return -1;
  9636. if(sd->status.partner_id > 0)
  9637. return sd->status.partner_id;
  9638. else
  9639. return 0;
  9640. }
  9641. /*==========================================
  9642. * Marry player sd to player dstsd
  9643. * Return:
  9644. * false = fail
  9645. * true = success
  9646. *------------------------------------------*/
  9647. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9648. {
  9649. if(sd == NULL || dstsd == NULL ||
  9650. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9651. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9652. return false;
  9653. sd->status.partner_id = dstsd->status.char_id;
  9654. dstsd->status.partner_id = sd->status.char_id;
  9655. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9656. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9657. return true;
  9658. }
  9659. /*==========================================
  9660. * Divorce sd from its partner
  9661. * Return:
  9662. * false = fail
  9663. * true = success
  9664. *------------------------------------------*/
  9665. bool pc_divorce(struct map_session_data *sd)
  9666. {
  9667. struct map_session_data *p_sd;
  9668. int i;
  9669. if( sd == NULL || !pc_ismarried(sd) )
  9670. return false;
  9671. if( !sd->status.partner_id )
  9672. return false; // Char is not married
  9673. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9674. { // Lets char server do the divorce
  9675. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9676. return false; // No char server connected
  9677. return true;
  9678. }
  9679. // Both players online, lets do the divorce manually
  9680. sd->status.partner_id = 0;
  9681. p_sd->status.partner_id = 0;
  9682. for( i = 0; i < MAX_INVENTORY; i++ )
  9683. {
  9684. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9685. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9686. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9687. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9688. }
  9689. clif_divorced(sd, p_sd->status.name);
  9690. clif_divorced(p_sd, sd->status.name);
  9691. return true;
  9692. }
  9693. /**
  9694. * Get the partner map_session_data of a player
  9695. * @param sd : the husband|wife session
  9696. * @return partner session or NULL
  9697. */
  9698. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9699. if (!sd || !pc_ismarried(sd))
  9700. return NULL;
  9701. return map_charid2sd(sd->status.partner_id);
  9702. }
  9703. /**
  9704. * Get the father map_session_data of a player
  9705. * @param sd : the baby session
  9706. * @return father session or NULL
  9707. */
  9708. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9709. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9710. return NULL;
  9711. return map_charid2sd(sd->status.father);
  9712. }
  9713. /**
  9714. * Get the mother map_session_data of a player
  9715. * @param sd : the baby session
  9716. * @return mother session or NULL
  9717. */
  9718. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9719. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9720. return NULL;
  9721. return map_charid2sd(sd->status.mother);
  9722. }
  9723. /*==========================================
  9724. * Get sd children charid. (Need to be married)
  9725. *------------------------------------------*/
  9726. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9727. {
  9728. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9729. // charid2sd returns NULL if not found
  9730. return NULL;
  9731. return map_charid2sd(sd->status.child);
  9732. }
  9733. /*==========================================
  9734. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9735. *------------------------------------------*/
  9736. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  9737. {
  9738. int hp = 0, sp = 0;
  9739. if( pc_isdead(sd) )
  9740. return;
  9741. if (sd->hp_loss.value) {
  9742. sd->hp_loss.tick += diff_tick;
  9743. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9744. hp += sd->hp_loss.value;
  9745. sd->hp_loss.tick -= sd->hp_loss.rate;
  9746. }
  9747. if(hp >= sd->battle_status.hp)
  9748. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9749. }
  9750. if (sd->sp_loss.value) {
  9751. sd->sp_loss.tick += diff_tick;
  9752. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9753. sp += sd->sp_loss.value;
  9754. sd->sp_loss.tick -= sd->sp_loss.rate;
  9755. }
  9756. }
  9757. if (hp > 0 || sp > 0)
  9758. status_zap(&sd->bl, hp, sp);
  9759. }
  9760. //Character regen. Flag is used to know which types of regen can take place.
  9761. //&1: HP regen
  9762. //&2: SP regen
  9763. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  9764. {
  9765. int hp = 0, sp = 0;
  9766. if (sd->hp_regen.value) {
  9767. sd->hp_regen.tick += diff_tick;
  9768. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9769. hp += sd->hp_regen.value;
  9770. sd->hp_regen.tick -= sd->hp_regen.rate;
  9771. }
  9772. }
  9773. if (sd->sp_regen.value) {
  9774. sd->sp_regen.tick += diff_tick;
  9775. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9776. sp += sd->sp_regen.value;
  9777. sd->sp_regen.tick -= sd->sp_regen.rate;
  9778. }
  9779. }
  9780. if (sd->percent_hp_regen.value) {
  9781. sd->percent_hp_regen.tick += diff_tick;
  9782. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9783. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  9784. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9785. }
  9786. }
  9787. if (sd->percent_sp_regen.value) {
  9788. sd->percent_sp_regen.tick += diff_tick;
  9789. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9790. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  9791. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9792. }
  9793. }
  9794. if (hp > 0 || sp > 0)
  9795. status_heal(&sd->bl, hp, sp, 0);
  9796. }
  9797. /*==========================================
  9798. * Memo player sd savepoint. (map,x,y)
  9799. *------------------------------------------*/
  9800. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9801. {
  9802. nullpo_retv(sd);
  9803. sd->status.save_point.map = mapindex;
  9804. sd->status.save_point.x = x;
  9805. sd->status.save_point.y = y;
  9806. }
  9807. /*==========================================
  9808. * Save 1 player data at autosave interval
  9809. *------------------------------------------*/
  9810. static TIMER_FUNC(pc_autosave){
  9811. int interval;
  9812. struct s_mapiterator* iter;
  9813. struct map_session_data* sd;
  9814. static int last_save_id = 0, save_flag = 0;
  9815. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9816. save_flag = 0;
  9817. else
  9818. save_flag = 1; //Noone was saved, so save first found char.
  9819. iter = mapit_getallusers();
  9820. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9821. {
  9822. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  9823. continue;
  9824. if(sd->bl.id == last_save_id && save_flag != 1) {
  9825. save_flag = 1;
  9826. continue;
  9827. }
  9828. if(save_flag != 1) //Not our turn to save yet.
  9829. continue;
  9830. //Save char.
  9831. last_save_id = sd->bl.id;
  9832. save_flag = 2;
  9833. if (pc_isvip(sd)) // Check if we're still VIP
  9834. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9835. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9836. break;
  9837. }
  9838. mapit_free(iter);
  9839. interval = autosave_interval/(map_usercount()+1);
  9840. if(interval < minsave_interval)
  9841. interval = minsave_interval;
  9842. add_timer(gettick()+interval,pc_autosave,0,0);
  9843. return 0;
  9844. }
  9845. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9846. {
  9847. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9848. { //Night/day state does not match.
  9849. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9850. sd->state.night = night_flag;
  9851. return 1;
  9852. }
  9853. return 0;
  9854. }
  9855. /*================================================
  9856. * timer to do the day [Yor]
  9857. * data: 0 = called by timer, 1 = gmcommand/script
  9858. *------------------------------------------------*/
  9859. TIMER_FUNC(map_day_timer){
  9860. char tmp_soutput[1024];
  9861. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9862. return 0;
  9863. if (!night_flag)
  9864. return 0; //Already day.
  9865. night_flag = 0; // 0=day, 1=night [Yor]
  9866. map_foreachpc(pc_daynight_timer_sub);
  9867. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9868. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9869. return 0;
  9870. }
  9871. /*================================================
  9872. * timer to do the night [Yor]
  9873. * data: 0 = called by timer, 1 = gmcommand/script
  9874. *------------------------------------------------*/
  9875. TIMER_FUNC(map_night_timer){
  9876. char tmp_soutput[1024];
  9877. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9878. return 0;
  9879. if (night_flag)
  9880. return 0; //Already nigth.
  9881. night_flag = 1; // 0=day, 1=night [Yor]
  9882. map_foreachpc(pc_daynight_timer_sub);
  9883. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9884. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9885. return 0;
  9886. }
  9887. /**
  9888. * Attempt to stand up a player
  9889. * @param sd
  9890. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9891. * @return True if success, Fals if failed
  9892. */
  9893. bool pc_setstand(struct map_session_data *sd, bool force){
  9894. nullpo_ret(sd);
  9895. // Cannot stand yet
  9896. // TODO: Move to SCS_NOSTAND [Cydh]
  9897. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9898. return false;
  9899. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9900. clif_status_load(&sd->bl,EFST_SIT,0);
  9901. clif_standing(&sd->bl); //Inform area PC is standing
  9902. //Reset sitting tick.
  9903. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9904. if( pc_isdead( sd ) ){
  9905. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9906. clif_party_dead( sd );
  9907. }else{
  9908. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9909. }
  9910. return true;
  9911. }
  9912. /**
  9913. * Calculate Overheat value
  9914. * @param sd: Player data
  9915. * @param heat: Amount of Heat to adjust
  9916. **/
  9917. void pc_overheat(struct map_session_data *sd, int16 heat) {
  9918. struct status_change_entry *sce = NULL;
  9919. int16 limit[] = { 150, 200, 280, 360, 450 };
  9920. uint16 skill_lv;
  9921. nullpo_retv(sd);
  9922. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  9923. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  9924. sce->val1 += heat;
  9925. sce->val1 = cap_value(sce->val1, 0, 1000);
  9926. if (sd->sc.data[SC_OVERHEAT])
  9927. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  9928. if (sce->val1 > limit[skill_lv])
  9929. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  9930. } else if (heat > 0)
  9931. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  9932. }
  9933. /**
  9934. * Check if player is autolooting given itemID.
  9935. */
  9936. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9937. {
  9938. uint8 i = 0;
  9939. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9940. return true;
  9941. if (!sd->state.autolooting)
  9942. return false;
  9943. if (sd->state.autolooting)
  9944. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9945. return (i != AUTOLOOTITEM_SIZE);
  9946. }
  9947. /**
  9948. * Checks if player can use @/#command
  9949. * @param sd Player map session data
  9950. * @param command Command name without @/# and params
  9951. * @param type is it atcommand or charcommand
  9952. */
  9953. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9954. {
  9955. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9956. }
  9957. /**
  9958. * Checks if commands used by a player should be logged
  9959. * according to their group setting.
  9960. * @param sd Player map session data
  9961. */
  9962. bool pc_should_log_commands(struct map_session_data *sd)
  9963. {
  9964. return pc_group_should_log_commands(pc_get_group_id(sd));
  9965. }
  9966. /**
  9967. * Spirit Charm expiration timer.
  9968. * @see TimerFunc
  9969. */
  9970. static TIMER_FUNC(pc_spiritcharm_timer){
  9971. struct map_session_data *sd;
  9972. int i;
  9973. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9974. return 1;
  9975. if (sd->spiritcharm <= 0) {
  9976. 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);
  9977. sd->spiritcharm = 0;
  9978. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9979. return 0;
  9980. }
  9981. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9982. if (i == sd->spiritcharm) {
  9983. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9984. return 0;
  9985. }
  9986. sd->spiritcharm--;
  9987. if (i != sd->spiritcharm)
  9988. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9989. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9990. if (sd->spiritcharm <= 0)
  9991. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9992. clif_spiritcharm(sd);
  9993. return 0;
  9994. }
  9995. /**
  9996. * Adds a spirit charm.
  9997. * @param sd: Target character
  9998. * @param interval: Duration
  9999. * @param max: Maximum amount of charms to add
  10000. * @param type: Charm type (@see spirit_charm_types)
  10001. */
  10002. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  10003. {
  10004. int tid, i;
  10005. nullpo_retv(sd);
  10006. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  10007. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  10008. if (max > MAX_SPIRITCHARM)
  10009. max = MAX_SPIRITCHARM;
  10010. if (sd->spiritcharm < 0)
  10011. sd->spiritcharm = 0;
  10012. if (sd->spiritcharm && sd->spiritcharm >= max) {
  10013. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  10014. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  10015. sd->spiritcharm--;
  10016. if (sd->spiritcharm != 0)
  10017. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  10018. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10019. }
  10020. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  10021. 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);
  10022. if (i != sd->spiritcharm)
  10023. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  10024. sd->spiritcharm_timer[i] = tid;
  10025. sd->spiritcharm++;
  10026. sd->spiritcharm_type = type;
  10027. clif_spiritcharm(sd);
  10028. }
  10029. /**
  10030. * Removes one or more spirit charms.
  10031. * @param sd: The target character
  10032. * @param count: Amount of charms to remove
  10033. * @param type: Type of charm to remove
  10034. */
  10035. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  10036. {
  10037. int i;
  10038. nullpo_retv(sd);
  10039. if (sd->spiritcharm_type != type)
  10040. return;
  10041. if (sd->spiritcharm <= 0) {
  10042. sd->spiritcharm = 0;
  10043. return;
  10044. }
  10045. if (count <= 0)
  10046. return;
  10047. if (count > sd->spiritcharm)
  10048. count = sd->spiritcharm;
  10049. sd->spiritcharm -= count;
  10050. if (count > MAX_SPIRITCHARM)
  10051. count = MAX_SPIRITCHARM;
  10052. for (i = 0; i < count; i++) {
  10053. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  10054. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  10055. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10056. }
  10057. }
  10058. for (i = count; i < MAX_SPIRITCHARM; i++) {
  10059. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  10060. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10061. }
  10062. if (sd->spiritcharm <= 0)
  10063. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10064. clif_spiritcharm(sd);
  10065. }
  10066. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10067. /**
  10068. * Renewal EXP/Item Drop rate modifier based on level penalty
  10069. * @param level_diff: Monster and Player level difference
  10070. * @param mob_class: Monster class
  10071. * @param mode: Monster mode
  10072. * @param type: 1 - EXP, 2 - Item Drop
  10073. * @return Penalty rate
  10074. */
  10075. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  10076. {
  10077. int rate = 100;
  10078. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  10079. return rate;
  10080. if (level_diff < 0)
  10081. level_diff = MAX_LEVEL + (~level_diff + 1);
  10082. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  10083. return rate;
  10084. return 100; // Penalty not found, return default
  10085. }
  10086. #endif
  10087. int pc_split_str(char *str,char **val,int num)
  10088. {
  10089. int i;
  10090. for (i=0; i<num && str; i++){
  10091. val[i] = str;
  10092. str = strchr(str,',');
  10093. if (str && i<num-1) //Do not remove a trailing comma.
  10094. *str++=0;
  10095. }
  10096. return i;
  10097. }
  10098. int pc_split_atoi(char* str, int* val, char sep, int max)
  10099. {
  10100. int i,j;
  10101. for (i=0; i<max; i++) {
  10102. if (!str) break;
  10103. val[i] = atoi(str);
  10104. str = strchr(str,sep);
  10105. if (str)
  10106. *str++=0;
  10107. }
  10108. //Zero up the remaining.
  10109. for(j=i; j < max; j++)
  10110. val[j] = 0;
  10111. return i;
  10112. }
  10113. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  10114. {
  10115. static int warning=0;
  10116. int i,j;
  10117. for (i=0; i<max; i++) {
  10118. double f;
  10119. if (!str) break;
  10120. f = atof(str);
  10121. if (f < 0)
  10122. val[i] = 0;
  10123. else if (f > UINT_MAX) {
  10124. val[i] = UINT_MAX;
  10125. if (!warning) {
  10126. warning = 1;
  10127. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  10128. }
  10129. } else
  10130. val[i] = (unsigned int)f;
  10131. str = strchr(str,sep);
  10132. if (str)
  10133. *str++=0;
  10134. }
  10135. //Zero up the remaining.
  10136. for(j=i; j < max; j++)
  10137. val[j] = 0;
  10138. return i;
  10139. }
  10140. /*==========================================
  10141. * sub DB reading.
  10142. * Function used to read skill_tree.txt
  10143. *------------------------------------------*/
  10144. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  10145. {
  10146. uint32 baselv, joblv, baselv_max, joblv_max;
  10147. uint16 skill_id, skill_lv, skill_lv_max;
  10148. int idx, class_;
  10149. unsigned int i, offset, skill_idx;
  10150. class_ = atoi(fields[0]);
  10151. skill_id = (uint16)atoi(fields[1]);
  10152. skill_lv = (uint16)atoi(fields[2]);
  10153. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  10154. baselv = (uint32)atoi(fields[3]);
  10155. joblv = (uint32)atoi(fields[4]);
  10156. offset = 5;
  10157. }
  10158. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  10159. baselv = joblv = 0;
  10160. offset = 3;
  10161. }
  10162. else {
  10163. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  10164. return false;
  10165. }
  10166. if(!pcdb_checkid(class_))
  10167. {
  10168. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  10169. return false;
  10170. }
  10171. idx = pc_class2idx(class_);
  10172. if (!skill_get_index(skill_id)) {
  10173. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.\n", skill_id, class_);
  10174. return false;
  10175. }
  10176. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10177. 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);
  10178. skill_lv = skill_lv_max;
  10179. }
  10180. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  10181. 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);
  10182. baselv = baselv_max;
  10183. }
  10184. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  10185. 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);
  10186. joblv = joblv_max;
  10187. }
  10188. //This is to avoid adding two lines for the same skill. [Skotlex]
  10189. 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 );
  10190. if( skill_idx == MAX_SKILL_TREE )
  10191. {
  10192. 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_);
  10193. return false;
  10194. }
  10195. else if(skill_tree[idx][skill_idx].skill_id)
  10196. {
  10197. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  10198. }
  10199. skill_tree[idx][skill_idx].skill_id = skill_id;
  10200. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  10201. skill_tree[idx][skill_idx].baselv = baselv;
  10202. skill_tree[idx][skill_idx].joblv = joblv;
  10203. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  10204. {
  10205. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  10206. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  10207. if (skill_id == 0) {
  10208. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  10209. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  10210. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  10211. }
  10212. continue;
  10213. }
  10214. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  10215. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  10216. return false;
  10217. }
  10218. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10219. 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);
  10220. skill_lv = skill_lv_max;
  10221. }
  10222. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  10223. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  10224. }
  10225. return true;
  10226. }
  10227. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10228. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  10229. {
  10230. int type, class_, diff;
  10231. type = atoi(fields[0]); //1=experience, 2=item drop
  10232. class_ = atoi(fields[1]);
  10233. diff = atoi(fields[2]);
  10234. if( type != 1 && type != 2 ){
  10235. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  10236. return false;
  10237. }
  10238. if( !CHK_CLASS(class_) ){
  10239. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  10240. return false;
  10241. }
  10242. diff = min(diff, MAX_LEVEL);
  10243. if( diff < 0 )
  10244. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  10245. level_penalty[type][class_][diff] = atoi(fields[3]);
  10246. return true;
  10247. }
  10248. #endif
  10249. /** [Cydh]
  10250. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  10251. * @param level Base level of player
  10252. * @param class_ Job ID @see enum e_job
  10253. * @return base_hp
  10254. */
  10255. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  10256. double base_hp;
  10257. uint16 i, idx = pc_class2idx(class_);
  10258. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  10259. #ifndef RENEWAL
  10260. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  10261. #endif
  10262. for (i = 2; i <= level; i++)
  10263. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  10264. if (class_ == JOB_SUMMONER)
  10265. base_hp += floor((base_hp / 2) + 0.5);
  10266. return (unsigned int)base_hp;
  10267. }
  10268. /** [Playtester]
  10269. * Calculates base sp of player.
  10270. * @param level Base level of player
  10271. * @param class_ Job ID @see enum e_job
  10272. * @return base_sp
  10273. */
  10274. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  10275. double base_sp;
  10276. uint16 idx = pc_class2idx(class_);
  10277. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  10278. switch (class_) {
  10279. case JOB_NINJA:
  10280. if (level >= 10)
  10281. base_sp -= 22;
  10282. else
  10283. base_sp = 11 + 3*level;
  10284. break;
  10285. case JOB_GUNSLINGER:
  10286. if (level > 10)
  10287. base_sp -= 18;
  10288. else
  10289. base_sp = 9 + 3*level;
  10290. break;
  10291. case JOB_SUMMONER:
  10292. base_sp -= floor(base_sp / 2);
  10293. break;
  10294. }
  10295. return (unsigned int)base_sp;
  10296. }
  10297. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  10298. static bool pc_readdb_job1(char* fields[], int columns, int current){
  10299. int idx, class_;
  10300. unsigned int i;
  10301. class_ = atoi(fields[0]);
  10302. if (!pcdb_checkid(class_)) {
  10303. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  10304. return false;
  10305. }
  10306. idx = pc_class2idx(class_);
  10307. job_info[idx].max_weight_base = atoi(fields[1]);
  10308. job_info[idx].hp_factor = atoi(fields[2]);
  10309. job_info[idx].hp_multiplicator = atoi(fields[3]);
  10310. job_info[idx].sp_factor = atoi(fields[4]);
  10311. #ifdef RENEWAL_ASPD
  10312. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  10313. #else
  10314. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  10315. #endif
  10316. {
  10317. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  10318. }
  10319. return true;
  10320. }
  10321. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  10322. static bool pc_readdb_job2(char* fields[], int columns, int current)
  10323. {
  10324. int idx, class_, i;
  10325. class_ = atoi(fields[0]);
  10326. if(!pcdb_checkid(class_))
  10327. {
  10328. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  10329. return false;
  10330. }
  10331. idx = pc_class2idx(class_);
  10332. for(i = 1; i < columns; i++)
  10333. {
  10334. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  10335. }
  10336. return true;
  10337. }
  10338. //Reading job_exp.txt line
  10339. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  10340. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  10341. {
  10342. int idx, i, type;
  10343. int job_id,job_count,jobs[CLASS_COUNT];
  10344. unsigned int ui, maxlvl;
  10345. maxlvl = atoi(fields[0]);
  10346. if(maxlvl > MAX_LEVEL || maxlvl<1){
  10347. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  10348. return false;
  10349. }
  10350. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10351. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  10352. return false;
  10353. }
  10354. type = atoi(fields[2]);
  10355. if(type < 0 || type > 1){
  10356. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  10357. return false;
  10358. }
  10359. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  10360. if (job_count < 1)
  10361. return false;
  10362. job_id = jobs[0];
  10363. if(!pcdb_checkid(job_id)){
  10364. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  10365. return false;
  10366. }
  10367. idx = pc_class2idx(job_id);
  10368. job_info[idx].max_level[type] = maxlvl;
  10369. for(i=0; i<maxlvl; i++)
  10370. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  10371. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  10372. //The reasoning behind the -2 is this... if the max level is 5, then the array
  10373. //should look like this:
  10374. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  10375. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  10376. job_info[idx].max_level[type]--;
  10377. if (job_info[idx].max_level[type] < maxlvl) {
  10378. 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]);
  10379. ShowInfo("Filling the missing values with the last exp entry.\n");
  10380. //Fill the requested values with the last entry.
  10381. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  10382. for (; ui+2 < maxlvl; ui++)
  10383. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  10384. job_info[idx].max_level[type] = maxlvl;
  10385. }
  10386. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10387. for (i = 1; i < job_count; i++) {
  10388. job_id = jobs[i];
  10389. if (!pcdb_checkid(job_id)) {
  10390. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  10391. continue;
  10392. }
  10393. idx = pc_class2idx(job_id);
  10394. 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]));
  10395. job_info[idx].max_level[type] = maxlvl;
  10396. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10397. }
  10398. return true;
  10399. }
  10400. /**
  10401. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  10402. * startlvl,endlvl,class,type,values...
  10403. */
  10404. #ifdef HP_SP_TABLES
  10405. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  10406. {
  10407. int i, startlvl, endlvl;
  10408. int job_count,jobs[CLASS_COUNT];
  10409. short type;
  10410. startlvl = atoi(fields[0]);
  10411. if(startlvl<1){
  10412. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10413. return false;
  10414. }
  10415. endlvl = atoi(fields[1]);
  10416. if(endlvl<1 || endlvl<startlvl){
  10417. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  10418. return false;
  10419. }
  10420. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10421. 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);
  10422. return false;
  10423. }
  10424. type = atoi(fields[3]);
  10425. if(type < 0 || type > 1){
  10426. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  10427. return false;
  10428. }
  10429. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  10430. if (job_count < 1)
  10431. return false;
  10432. for (i = 0; i < job_count; i++) {
  10433. int idx, job_id = jobs[i], use_endlvl;
  10434. if (!pcdb_checkid(job_id)) {
  10435. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  10436. return false;
  10437. }
  10438. idx = pc_class2idx(job_id);
  10439. if (startlvl > job_info[idx].max_level[0]) {
  10440. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10441. return false;
  10442. }
  10443. //Just read until available max level for this job, don't use MAX_LEVEL!
  10444. use_endlvl = endlvl;
  10445. if (use_endlvl > job_info[idx].max_level[0])
  10446. use_endlvl = job_info[idx].max_level[0];
  10447. if(type == 0) { //hp type
  10448. uint16 j;
  10449. for(j = 0; j < use_endlvl; j++) {
  10450. if (atoi(fields[j+4])) {
  10451. uint16 lvl_idx = startlvl-1+j;
  10452. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  10453. //Tells if this HP is lower than previous level (but not for 99->100)
  10454. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  10455. 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",
  10456. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  10457. }
  10458. }
  10459. }
  10460. else { //sp type
  10461. uint16 j;
  10462. for(j = 0; j < use_endlvl; j++) {
  10463. if (atoi(fields[j+4])) {
  10464. uint16 lvl_idx = startlvl-1+j;
  10465. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  10466. //Tells if this SP is lower than previous level (but not for 99->100)
  10467. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  10468. 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",
  10469. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  10470. }
  10471. }
  10472. }
  10473. }
  10474. return true;
  10475. }
  10476. #endif
  10477. /** [Cydh]
  10478. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  10479. */
  10480. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  10481. {
  10482. int64 class_tmp;
  10483. int idx, class_;
  10484. uint16 str, agi, vit, int_, dex, luk;
  10485. script_get_constant(trim(fields[0]),&class_tmp);
  10486. class_ = static_cast<int>(class_tmp);
  10487. if ((idx = pc_class2idx(class_)) < 0) {
  10488. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  10489. return false;
  10490. }
  10491. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  10492. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  10493. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  10494. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  10495. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10496. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10497. job_info[idx].max_param.str = str;
  10498. job_info[idx].max_param.agi = agi;
  10499. job_info[idx].max_param.vit = vit;
  10500. job_info[idx].max_param.int_ = int_;
  10501. job_info[idx].max_param.dex = dex;
  10502. job_info[idx].max_param.luk = luk;
  10503. return true;
  10504. }
  10505. /**
  10506. * Read job_noenter_map.txt
  10507. **/
  10508. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10509. int idx, class_ = -1;
  10510. int64 class_tmp;
  10511. if (ISDIGIT(str[0][0])) {
  10512. class_ = atoi(str[0]);
  10513. } else {
  10514. if (!script_get_constant(str[0], &class_tmp)) {
  10515. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10516. return false;
  10517. }
  10518. class_ = static_cast<int>(class_tmp);
  10519. }
  10520. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10521. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  10522. return false;
  10523. }
  10524. job_info[idx].noenter_map.zone = atoi(str[1]);
  10525. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10526. return true;
  10527. }
  10528. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10529. int i=1;
  10530. char line[24000]; //FIXME this seem too big
  10531. FILE *fp;
  10532. sprintf(line, "%s/statpoint.txt", basedir);
  10533. fp=fopen(line,"r");
  10534. if(fp == NULL){
  10535. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10536. return max(last_s,i);
  10537. } else {
  10538. int entries=0;
  10539. while(fgets(line, sizeof(line), fp))
  10540. {
  10541. int stat;
  10542. trim(line);
  10543. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10544. continue;
  10545. if ((stat=strtoul(line,NULL,10))<0)
  10546. stat=0;
  10547. if (i > MAX_LEVEL)
  10548. break;
  10549. statp[i]=stat;
  10550. i++;
  10551. entries++;
  10552. }
  10553. fclose(fp);
  10554. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10555. }
  10556. return max(last_s,i);
  10557. }
  10558. /*==========================================
  10559. * pc DB reading.
  10560. * job_exp.txt - required experience values
  10561. * skill_tree.txt - skill tree for every class
  10562. * attr_fix.txt - elemental adjustment table
  10563. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10564. * job_db2.txt - job,stats bonuses/lvl
  10565. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10566. *------------------------------------------*/
  10567. void pc_readdb(void) {
  10568. int i, k, s = 1;
  10569. const char* dbsubpath[] = {
  10570. "",
  10571. "/" DBIMPORT,
  10572. //add other path here
  10573. };
  10574. //reset
  10575. memset(job_info,0,sizeof(job_info)); // job_info table
  10576. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10577. sv_readdb(db_path, DBPATH "level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10578. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10579. for( k=1; k < 3; k++ ){ // fill in the blanks
  10580. int j;
  10581. for( j = 0; j < CLASS_ALL; j++ ){
  10582. int tmp = 0;
  10583. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10584. if( i == MAX_LEVEL+1 )
  10585. tmp = level_penalty[k][j][0];// reset
  10586. if( level_penalty[k][j][i] > 0 )
  10587. tmp = level_penalty[k][j][i];
  10588. else
  10589. level_penalty[k][j][i] = tmp;
  10590. }
  10591. }
  10592. }
  10593. #endif
  10594. // reset then read statspoint
  10595. memset(statp,0,sizeof(statp));
  10596. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10597. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10598. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10599. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10600. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10601. if(i==0) {
  10602. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10603. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10604. }
  10605. else {
  10606. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10607. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10608. }
  10609. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10610. if (i == 0)
  10611. #ifdef RENEWAL_ASPD // Paths are hardcoded here to specifically pick the correct database
  10612. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10613. #else
  10614. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10615. #endif
  10616. else
  10617. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, true);
  10618. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10619. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10620. #ifdef HP_SP_TABLES
  10621. 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!
  10622. #endif
  10623. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10624. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10625. aFree(dbsubpath1);
  10626. aFree(dbsubpath2);
  10627. }
  10628. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10629. memset(skill_tree, 0, sizeof(skill_tree));
  10630. 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);
  10631. 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);
  10632. // generate the remaining parts of the db if necessary
  10633. k = battle_config.use_statpoint_table; //save setting
  10634. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10635. statp[0] = 45; // seed value
  10636. for (; s <= MAX_LEVEL; s++)
  10637. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10638. battle_config.use_statpoint_table = k; //restore setting
  10639. //Checking if all class have their data
  10640. for (i = 0; i < JOB_MAX; i++) {
  10641. int idx;
  10642. uint16 j;
  10643. if (!pcdb_checkid(i))
  10644. continue;
  10645. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10646. continue; //Classes that do not need exp tables.
  10647. idx = pc_class2idx(i);
  10648. if (!job_info[idx].max_level[0])
  10649. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10650. if (!job_info[idx].max_level[1])
  10651. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10652. //Init and checking the empty value of Base HP/SP [Cydh]
  10653. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10654. if (job_info[idx].base_hp[j] == 0)
  10655. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10656. if (job_info[idx].base_sp[j] == 0)
  10657. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10658. }
  10659. }
  10660. }
  10661. // Read MOTD on startup. [Valaris]
  10662. int pc_read_motd(void)
  10663. {
  10664. FILE* fp;
  10665. // clear old MOTD
  10666. memset(motd_text, 0, sizeof(motd_text));
  10667. // read current MOTD
  10668. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10669. {
  10670. unsigned int entries = 0;
  10671. char buf[CHAT_SIZE_MAX];
  10672. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10673. {
  10674. unsigned int lines = 0;
  10675. size_t len;
  10676. lines++;
  10677. if( buf[0] == '/' && buf[1] == '/' )
  10678. continue;
  10679. len = strlen(buf);
  10680. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10681. len--;
  10682. if( len ) {
  10683. char * ptr;
  10684. buf[len] = 0;
  10685. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10686. 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);
  10687. }
  10688. else {// empty line
  10689. buf[0] = ' ';
  10690. buf[1] = 0;
  10691. }
  10692. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10693. entries++;
  10694. }
  10695. fclose(fp);
  10696. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10697. }
  10698. else
  10699. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10700. return 0;
  10701. }
  10702. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10703. int i,cursor = 0;
  10704. struct item_cd* cd = NULL;
  10705. if( load ) {
  10706. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10707. // no item cooldown is associated with this character
  10708. return;
  10709. }
  10710. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10711. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10712. sd->item_delay[cursor].tick = cd->tick[i];
  10713. sd->item_delay[cursor].nameid = cd->nameid[i];
  10714. cursor++;
  10715. }
  10716. }
  10717. idb_remove(itemcd_db,sd->status.char_id);
  10718. } else {
  10719. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10720. // create a new skill cooldown object for map storage
  10721. CREATE( cd, struct item_cd, 1 );
  10722. idb_put( itemcd_db, sd->status.char_id, cd );
  10723. }
  10724. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10725. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10726. cd->tick[cursor] = sd->item_delay[i].tick;
  10727. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10728. cursor++;
  10729. }
  10730. }
  10731. }
  10732. return;
  10733. }
  10734. /**
  10735. * Add item delay to player's item delay data
  10736. * @param sd Player
  10737. * @param id Item data
  10738. * @param tick Current tick
  10739. * @param n Item index in inventory
  10740. * @return 0: No delay, can consume item.
  10741. * 1: Has delay, cancel consumption.
  10742. **/
  10743. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10744. int i;
  10745. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10746. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10747. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10748. if( i < MAX_ITEMDELAYS ) {
  10749. if( sd->item_delay[i].nameid ) {// found
  10750. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10751. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10752. char e_msg[CHAT_SIZE_MAX];
  10753. if( e_tick > 99 )
  10754. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10755. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10756. else
  10757. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10758. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10759. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10760. return 1; // Delay has not expired yet
  10761. }
  10762. } else {// not yet used item (all slots are initially empty)
  10763. sd->item_delay[i].nameid = id->nameid;
  10764. }
  10765. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10766. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10767. } else {// should not happen
  10768. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10769. }
  10770. //clean up used delays so we can give room for more
  10771. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10772. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10773. sd->item_delay[i].tick = 0;
  10774. sd->item_delay[i].nameid = 0;
  10775. }
  10776. }
  10777. return 0;
  10778. }
  10779. /**
  10780. * Check if player has delay to reuse item
  10781. * @param sd Player
  10782. * @param id Item data
  10783. * @param tick Current tick
  10784. * @param n Item index in inventory
  10785. * @return 0: No delay, can consume item.
  10786. * 1: Has delay, cancel consumption.
  10787. **/
  10788. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10789. struct status_change *sc = NULL;
  10790. nullpo_retr(0, sd);
  10791. nullpo_retr(0, id);
  10792. // Do normal delay assignment
  10793. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10794. return pc_itemcd_add(sd, id, tick, n);
  10795. // Send reply of delay remains
  10796. if (sc->data[id->delay_sc]) {
  10797. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10798. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  10799. return 1;
  10800. }
  10801. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10802. return 0;
  10803. }
  10804. /**
  10805. * Clear the dmglog data from player
  10806. * @param sd
  10807. * @param md
  10808. **/
  10809. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10810. {
  10811. uint8 i;
  10812. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10813. if (i < DAMAGELOG_SIZE) {
  10814. md->dmglog[i].id = 0;
  10815. md->dmglog[i].dmg = 0;
  10816. md->dmglog[i].flag = 0;
  10817. }
  10818. }
  10819. /**
  10820. * Add log to player's dmglog
  10821. * @param sd
  10822. * @param id Monster's GID
  10823. **/
  10824. void pc_damage_log_add(struct map_session_data *sd, int id)
  10825. {
  10826. uint8 i = 0;
  10827. if (!sd || !id)
  10828. return;
  10829. //Only store new data, don't need to renew the old one with same id
  10830. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10831. if (i < DAMAGELOG_SIZE_PC)
  10832. return;
  10833. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10834. if (sd->dmglog[i] == 0) {
  10835. sd->dmglog[i] = id;
  10836. return;
  10837. }
  10838. }
  10839. }
  10840. /**
  10841. * Clear dmglog data from player
  10842. * @param sd
  10843. * @param id Monster's id
  10844. **/
  10845. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10846. {
  10847. uint8 i;
  10848. struct mob_data *md = NULL;
  10849. if (!sd)
  10850. return;
  10851. if (!id) {
  10852. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10853. if( !sd->dmglog[i] ) //skip the empty value
  10854. continue;
  10855. if ((md = map_id2md(sd->dmglog[i])))
  10856. pc_clear_log_damage_sub(sd->status.char_id,md);
  10857. sd->dmglog[i] = 0;
  10858. }
  10859. }
  10860. else {
  10861. if ((md = map_id2md(id)))
  10862. pc_clear_log_damage_sub(sd->status.char_id,md);
  10863. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10864. if (i < DAMAGELOG_SIZE_PC)
  10865. sd->dmglog[i] = 0;
  10866. }
  10867. }
  10868. /**
  10869. * Status change data arrived from char-server
  10870. * @param sd: Player data
  10871. */
  10872. void pc_scdata_received(struct map_session_data *sd) {
  10873. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10874. clif_weight_limit( sd );
  10875. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10876. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10877. }
  10878. sd->state.pc_loaded = true;
  10879. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10880. sd->state.connect_new = 1;
  10881. clif_parse_LoadEndAck(sd->fd, sd);
  10882. }
  10883. if (pc_iscarton(sd)) {
  10884. sd->cart_weight_max = 0; // Force a client refesh
  10885. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10886. }
  10887. }
  10888. /**
  10889. * Check player account expiration time and rental item expirations
  10890. * @param sd: Player data
  10891. */
  10892. void pc_check_expiration(struct map_session_data *sd) {
  10893. #ifndef ENABLE_SC_SAVING
  10894. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10895. #endif
  10896. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10897. time_t exp_time = sd->expiration_time;
  10898. char tmpstr[1024];
  10899. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10900. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  10901. pc_expire_check(sd);
  10902. }
  10903. }
  10904. TIMER_FUNC(pc_expiration_timer){
  10905. struct map_session_data *sd = map_id2sd(id);
  10906. if( !sd ) return 0;
  10907. sd->expiration_tid = INVALID_TIMER;
  10908. if( sd->fd )
  10909. clif_authfail_fd(sd->fd,10);
  10910. map_quit(sd);
  10911. return 0;
  10912. }
  10913. TIMER_FUNC(pc_autotrade_timer){
  10914. struct map_session_data *sd = map_id2sd(id);
  10915. if (!sd)
  10916. return 0;
  10917. sd->autotrade_tid = INVALID_TIMER;
  10918. if (sd->state.autotrade&2)
  10919. vending_reopen(sd);
  10920. if (sd->state.autotrade&4)
  10921. buyingstore_reopen(sd);
  10922. if (!sd->vender_id && !sd->buyer_id) {
  10923. sd->state.autotrade = 0;
  10924. map_quit(sd);
  10925. }
  10926. return 0;
  10927. }
  10928. /* this timer exists only when a character with a expire timer > 24h is online */
  10929. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10930. TIMER_FUNC(pc_global_expiration_timer){
  10931. struct s_mapiterator* iter;
  10932. struct map_session_data* sd;
  10933. iter = mapit_getallusers();
  10934. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10935. if( sd->expiration_time )
  10936. pc_expire_check(sd);
  10937. mapit_free(iter);
  10938. return 0;
  10939. }
  10940. void pc_expire_check(struct map_session_data *sd) {
  10941. /* ongoing timer */
  10942. if( sd->expiration_tid != INVALID_TIMER )
  10943. return;
  10944. /* not within the next 24h, enable the global check */
  10945. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10946. /* global check not running, enable */
  10947. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10948. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10949. return;
  10950. }
  10951. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10952. }
  10953. /**
  10954. * Deposit some money to bank
  10955. * @param sd
  10956. * @param money Amount of money to deposit
  10957. **/
  10958. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10959. unsigned int limit_check = money + sd->bank_vault;
  10960. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10961. return BDA_OVERFLOW;
  10962. } else if ( money > sd->status.zeny ) {
  10963. return BDA_NO_MONEY;
  10964. }
  10965. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10966. return BDA_NO_MONEY;
  10967. sd->bank_vault += money;
  10968. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10969. if( save_settings&CHARSAVE_BANK )
  10970. chrif_save(sd, CSAVE_NORMAL);
  10971. return BDA_SUCCESS;
  10972. }
  10973. /**
  10974. * Withdraw money from bank
  10975. * @param sd
  10976. * @param money Amount of money that will be withdrawn
  10977. **/
  10978. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10979. unsigned int limit_check = money + sd->status.zeny;
  10980. if( money <= 0 ) {
  10981. return BWA_UNKNOWN_ERROR;
  10982. } else if ( money > sd->bank_vault ) {
  10983. return BWA_NO_MONEY;
  10984. } else if ( limit_check > MAX_ZENY ) {
  10985. /* no official response for this scenario exists. */
  10986. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10987. return BWA_UNKNOWN_ERROR;
  10988. }
  10989. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10990. return BWA_NO_MONEY;
  10991. sd->bank_vault -= money;
  10992. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10993. if( save_settings&CHARSAVE_BANK )
  10994. chrif_save(sd, CSAVE_NORMAL);
  10995. return BWA_SUCCESS;
  10996. }
  10997. /**
  10998. * Clear Crimson Marker data from caster
  10999. * @param sd: Player
  11000. **/
  11001. void pc_crimson_marker_clear(struct map_session_data *sd) {
  11002. uint8 i;
  11003. if (!sd)
  11004. return;
  11005. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  11006. struct block_list *bl = NULL;
  11007. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  11008. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  11009. sd->c_marker[i] = 0;
  11010. }
  11011. }
  11012. /**
  11013. * Show version to player
  11014. * @param sd: Player
  11015. **/
  11016. void pc_show_version(struct map_session_data *sd) {
  11017. const char* svn = get_svn_revision();
  11018. char buf[CHAT_SIZE_MAX];
  11019. if( svn[0] != UNKNOWN_VERSION )
  11020. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  11021. else {
  11022. const char* git = get_git_hash();
  11023. if( git[0] != UNKNOWN_VERSION )
  11024. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  11025. else
  11026. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  11027. }
  11028. clif_displaymessage(sd->fd,buf);
  11029. }
  11030. /**
  11031. * Run bonus_script on player
  11032. * @param sd
  11033. * @author [Cydh]
  11034. **/
  11035. void pc_bonus_script(struct map_session_data *sd) {
  11036. t_tick now = gettick();
  11037. struct linkdb_node *node = NULL, *next = NULL;
  11038. if (!sd || !(node = sd->bonus_script.head))
  11039. return;
  11040. while (node) {
  11041. struct s_bonus_script_entry *entry = NULL;
  11042. next = node->next;
  11043. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  11044. // Only start timer for new bonus_script
  11045. if (entry->tid == INVALID_TIMER) {
  11046. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  11047. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  11048. entry->tick += now;
  11049. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  11050. }
  11051. if (entry->script)
  11052. run_script(entry->script, 0, sd->bl.id, 0);
  11053. else
  11054. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  11055. }
  11056. node = next;
  11057. }
  11058. }
  11059. /**
  11060. * Add bonus_script to player
  11061. * @param sd Player
  11062. * @param script_str Script string
  11063. * @param dur Duration in ms
  11064. * @param icon EFST
  11065. * @param flag Flags @see enum e_bonus_script_flags
  11066. * @param type 0 - None, 1 - Buff, 2 - Debuff
  11067. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  11068. * @author [Cydh]
  11069. **/
  11070. 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) {
  11071. struct script_code *script = NULL;
  11072. struct linkdb_node *node = NULL;
  11073. struct s_bonus_script_entry *entry = NULL;
  11074. if (!sd)
  11075. return NULL;
  11076. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  11077. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  11078. return NULL;
  11079. }
  11080. // Duplication checks
  11081. if ((node = sd->bonus_script.head)) {
  11082. while (node) {
  11083. entry = (struct s_bonus_script_entry *)node->data;
  11084. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  11085. t_tick newdur = gettick() + dur;
  11086. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  11087. sett_tickimer(entry->tid, newdur);
  11088. script_free_code(script);
  11089. return NULL;
  11090. }
  11091. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  11092. break;
  11093. else { // No duplicate bonus
  11094. script_free_code(script);
  11095. return NULL;
  11096. }
  11097. }
  11098. node = node->next;
  11099. }
  11100. }
  11101. CREATE(entry, struct s_bonus_script_entry, 1);
  11102. entry->script_buf = StringBuf_Malloc();
  11103. StringBuf_AppendStr(entry->script_buf, script_str);
  11104. entry->tid = INVALID_TIMER;
  11105. entry->flag = flag;
  11106. entry->icon = icon;
  11107. entry->tick = dur; // Use duration first, on run change to expire time
  11108. entry->type = type;
  11109. entry->script = script;
  11110. sd->bonus_script.count++;
  11111. return entry;
  11112. }
  11113. /**
  11114. * Remove bonus_script data from player
  11115. * @param sd: Target player
  11116. * @param list: Bonus script entry from player
  11117. * @author [Cydh]
  11118. **/
  11119. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  11120. if (entry->tid != INVALID_TIMER)
  11121. delete_timer(entry->tid, pc_bonus_script_timer);
  11122. if (entry->script)
  11123. script_free_code(entry->script);
  11124. if (entry->script_buf)
  11125. StringBuf_Free(entry->script_buf);
  11126. if (sd) {
  11127. if (entry->icon != EFST_BLANK)
  11128. clif_status_load(&sd->bl, entry->icon, 0);
  11129. if (sd->bonus_script.count > 0)
  11130. sd->bonus_script.count--;
  11131. }
  11132. aFree(entry);
  11133. }
  11134. /**
  11135. * Do final process if no entry left
  11136. * @param sd
  11137. **/
  11138. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  11139. if (sd->bonus_script.count == 0) {
  11140. if (sd->bonus_script.head && sd->bonus_script.head->data)
  11141. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  11142. linkdb_final(&sd->bonus_script.head);
  11143. }
  11144. }
  11145. /**
  11146. * Timer for bonus_script
  11147. * @param tid
  11148. * @param tick
  11149. * @param id
  11150. * @param data
  11151. * @author [Cydh]
  11152. **/
  11153. TIMER_FUNC(pc_bonus_script_timer){
  11154. struct map_session_data *sd;
  11155. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  11156. sd = map_id2sd(id);
  11157. if (!sd) {
  11158. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  11159. return 0;
  11160. }
  11161. if (tid == INVALID_TIMER)
  11162. return 0;
  11163. if (!sd->bonus_script.head || entry == NULL) {
  11164. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  11165. return 0;
  11166. }
  11167. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11168. pc_bonus_script_free_entry(sd, entry);
  11169. pc_bonus_script_check_final(sd);
  11170. status_calc_pc(sd,SCO_NONE);
  11171. return 0;
  11172. }
  11173. /**
  11174. * Check then clear all active timer(s) of bonus_script data from player based on reason
  11175. * @param sd: Target player
  11176. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  11177. * @author [Cydh]
  11178. **/
  11179. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  11180. struct linkdb_node *node = NULL;
  11181. uint16 count = 0;
  11182. if (!sd || !(node = sd->bonus_script.head))
  11183. return;
  11184. while (node) {
  11185. struct linkdb_node *next = node->next;
  11186. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  11187. if (entry && (
  11188. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  11189. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  11190. (flag&entry->flag) || // Matched flag
  11191. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  11192. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  11193. )
  11194. )
  11195. {
  11196. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11197. pc_bonus_script_free_entry(sd, entry);
  11198. count++;
  11199. }
  11200. node = next;
  11201. }
  11202. pc_bonus_script_check_final(sd);
  11203. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  11204. status_calc_pc(sd,SCO_NONE);
  11205. }
  11206. /** [Cydh]
  11207. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  11208. * @param sd: Target player
  11209. */
  11210. void pc_cell_basilica(struct map_session_data *sd) {
  11211. nullpo_retv(sd);
  11212. #ifdef RENEWAL
  11213. enum sc_type type = SC_BASILICA_CELL;
  11214. #else
  11215. enum sc_type type = SC_BASILICA;
  11216. #endif
  11217. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  11218. if (sd->sc.data[type])
  11219. status_change_end(&sd->bl, type,INVALID_TIMER);
  11220. }
  11221. else if (!sd->sc.data[type])
  11222. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  11223. }
  11224. /** [Cydh]
  11225. * Get maximum specified parameter for specified class
  11226. * @param class_: sd->class
  11227. * @param sex: sd->status.sex
  11228. * @param flag: parameter will be checked
  11229. * @return max_param
  11230. */
  11231. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  11232. int idx = -1, class_ = sd->class_;
  11233. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  11234. short max_param = 0;
  11235. switch (param) {
  11236. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  11237. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  11238. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  11239. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  11240. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  11241. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  11242. }
  11243. if (max_param > 0)
  11244. return max_param;
  11245. }
  11246. // Always check babies first
  11247. if( class_ & JOBL_BABY ){
  11248. if( class_ & JOBL_THIRD ){
  11249. return battle_config.max_baby_third_parameter;
  11250. }else{
  11251. return battle_config.max_baby_parameter;
  11252. }
  11253. }
  11254. // Summoner
  11255. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11256. return battle_config.max_summoner_parameter;
  11257. }
  11258. // Extended classes
  11259. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11260. return battle_config.max_extended_parameter;
  11261. }
  11262. // 3rd class
  11263. if( class_ & JOBL_THIRD ){
  11264. // Transcendent
  11265. if( class_ & JOBL_UPPER ){
  11266. return battle_config.max_third_trans_parameter;
  11267. }else{
  11268. return battle_config.max_third_parameter;
  11269. }
  11270. }
  11271. // Transcendent
  11272. if( class_ & JOBL_UPPER ){
  11273. return battle_config.max_trans_parameter;
  11274. }
  11275. return battle_config.max_parameter;
  11276. }
  11277. /**
  11278. * Get max ASPD for player based on Class
  11279. * @param sd Player
  11280. * @return ASPD
  11281. */
  11282. short pc_maxaspd(struct map_session_data *sd) {
  11283. nullpo_ret(sd);
  11284. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  11285. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  11286. battle_config.max_aspd ));
  11287. }
  11288. /**
  11289. * Calculates total item-group related bonuses for the given item
  11290. * @param sd Player
  11291. * @param nameid Item ID
  11292. * @return Heal rate
  11293. **/
  11294. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  11295. if (sd->itemgrouphealrate.empty())
  11296. return 0;
  11297. short bonus = 0;
  11298. for (const auto &it : sd->itemgrouphealrate) {
  11299. uint16 group_id = it.id, i;
  11300. struct s_item_group_db *group = NULL;
  11301. if (!group_id || !(group = itemdb_group_exists(group_id)))
  11302. continue;
  11303. for (i = 0; i < group->random[0].data_qty; i++) {
  11304. if (group->random[0].data[i].nameid == nameid) {
  11305. bonus += it.val;
  11306. break;
  11307. }
  11308. }
  11309. }
  11310. return bonus;
  11311. }
  11312. /**
  11313. * Calculates total item-group related bonuses for the given item group
  11314. * @param sd Player
  11315. * @param group_id Item Group ID
  11316. * @return Heal rate
  11317. **/
  11318. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  11319. if (sd->itemgrouphealrate.empty())
  11320. return 0;
  11321. for (const auto &it : sd->itemgrouphealrate) {
  11322. if (it.id == group_id)
  11323. return it.val;
  11324. }
  11325. return 0;
  11326. }
  11327. /**
  11328. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  11329. * @param eqi Item EQI of enum equip_index
  11330. * @param *equip_index Player's equip_index[]
  11331. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  11332. * @return True if item in same inventory index, False if doesn't
  11333. */
  11334. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  11335. if (index < 0 || index >= MAX_INVENTORY)
  11336. return true;
  11337. // Dual weapon checks
  11338. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  11339. return true;
  11340. // Headgear with Mid & Low location
  11341. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  11342. return true;
  11343. // Headgear with Top & Mid or Low location
  11344. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  11345. return true;
  11346. // Headgear with Mid & Low location
  11347. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  11348. return true;
  11349. // Headgear with Top & Mid or Low location
  11350. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  11351. return true;
  11352. return false;
  11353. }
  11354. /**
  11355. * Generate Unique item ID for player
  11356. * @param sd : Player
  11357. * @return A generated Unique item ID
  11358. */
  11359. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  11360. nullpo_ret(sd);
  11361. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  11362. }
  11363. /**
  11364. * Validating skill from player after logged on
  11365. * @param sd
  11366. **/
  11367. void pc_validate_skill(struct map_session_data *sd) {
  11368. if (sd) {
  11369. uint16 i = 0, count = 0;
  11370. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  11371. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  11372. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  11373. for (i = 0; i < MAX_SKILL; i++) {
  11374. uint16 idx = 0;
  11375. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  11376. continue;
  11377. if ((idx = skill_get_index(tmp_skills[i].id))) {
  11378. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  11379. count++;
  11380. }
  11381. else
  11382. 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);
  11383. }
  11384. }
  11385. }
  11386. /**
  11387. * Toggle to remember if the questinfo is displayed yet or not.
  11388. * @param qi_display Display flag
  11389. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  11390. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  11391. **/
  11392. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct s_questinfo *qi, bool show) {
  11393. if (show) {
  11394. // Check if need to be displayed
  11395. if ((*qi_display) != 1) {
  11396. (*qi_display) = 1;
  11397. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  11398. }
  11399. }
  11400. else {
  11401. // Check if need to be hide
  11402. if ((*qi_display) != 0) {
  11403. (*qi_display) = 0;
  11404. #if PACKETVER >= 20120410
  11405. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_NONE, QMARK_NONE);
  11406. #else
  11407. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_QUEST, QMARK_NONE);
  11408. #endif
  11409. }
  11410. }
  11411. }
  11412. /**
  11413. * Show available NPC Quest / Event Icon Check [Kisuka]
  11414. * @param sd Player
  11415. **/
  11416. void pc_show_questinfo(struct map_session_data *sd) {
  11417. #if PACKETVER >= 20090218
  11418. nullpo_retv(sd);
  11419. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11420. return;
  11421. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11422. nullpo_retv(mapdata);
  11423. if (mapdata->qi_data.empty())
  11424. return;
  11425. if (mapdata->qi_data.size() != sd->qi_count)
  11426. return; // init was not called yet
  11427. struct s_questinfo *qi = nullptr;
  11428. bool show;
  11429. for (int i = 0; i < mapdata->qi_data.size(); i++) {
  11430. qi = &mapdata->qi_data[i];
  11431. if (!qi)
  11432. continue;
  11433. if (!qi->condition)
  11434. show = true;
  11435. else {
  11436. if (achievement_check_condition(qi->condition, sd))
  11437. show = true;
  11438. else
  11439. show = false;
  11440. }
  11441. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, show);
  11442. }
  11443. #endif
  11444. }
  11445. /**
  11446. * Reinit the questinfo for player when changing map
  11447. * @param sd Player
  11448. **/
  11449. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  11450. #if PACKETVER >= 20090218
  11451. nullpo_retv(sd);
  11452. if (sd->qi_display) {
  11453. aFree(sd->qi_display);
  11454. sd->qi_display = nullptr;
  11455. }
  11456. sd->qi_count = 0;
  11457. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11458. return;
  11459. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11460. nullpo_retv(mapdata);
  11461. if (mapdata->qi_data.empty())
  11462. return;
  11463. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_data.size()));
  11464. #endif
  11465. }
  11466. /**
  11467. * Check if a job is allowed to enter the map
  11468. * @param jobid Job ID see enum e_job or sd->status.class_
  11469. * @param m ID -an index- for direct indexing map[] array
  11470. * @return 1 if job is allowed, 0 otherwise
  11471. **/
  11472. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  11473. uint16 idx = 0;
  11474. // Map is other map server.
  11475. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  11476. if (m < 0)
  11477. return true;
  11478. struct map_data *mapdata = map_getmapdata(m);
  11479. if (!mapdata->cell)
  11480. return false;
  11481. if (!pcdb_checkid(jobid))
  11482. return false;
  11483. idx = pc_class2idx(jobid);
  11484. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  11485. return true;
  11486. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  11487. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  11488. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  11489. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  11490. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  11491. (job_info[idx].noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  11492. )
  11493. return false;
  11494. return true;
  11495. }
  11496. /**
  11497. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11498. * @param sd
  11499. **/
  11500. void pc_set_costume_view(struct map_session_data *sd) {
  11501. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11502. struct item_data *id = NULL;
  11503. nullpo_retv(sd);
  11504. head_low = sd->status.head_bottom;
  11505. head_mid = sd->status.head_mid;
  11506. head_top = sd->status.head_top;
  11507. robe = sd->status.robe;
  11508. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11509. //Added check to prevent sending the same look on multiple slots ->
  11510. //causes client to redraw item on top of itself. (suggested by Lupus)
  11511. // Normal headgear checks
  11512. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11513. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11514. sd->status.head_bottom = id->look;
  11515. else
  11516. sd->status.head_bottom = 0;
  11517. }
  11518. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11519. if (!(id->equip&(EQP_HEAD_TOP)))
  11520. sd->status.head_mid = id->look;
  11521. else
  11522. sd->status.head_mid = 0;
  11523. }
  11524. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11525. sd->status.head_top = id->look;
  11526. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11527. sd->status.robe = id->look;
  11528. // Costumes check
  11529. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11530. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11531. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11532. sd->status.head_bottom = id->look;
  11533. else
  11534. sd->status.head_bottom = 0;
  11535. }
  11536. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11537. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11538. sd->status.head_mid = id->look;
  11539. else
  11540. sd->status.head_mid = 0;
  11541. }
  11542. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11543. sd->status.head_top = id->look;
  11544. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11545. sd->status.robe = id->look;
  11546. }
  11547. if (sd->setlook_head_bottom)
  11548. sd->status.head_bottom = sd->setlook_head_bottom;
  11549. if (sd->setlook_head_mid)
  11550. sd->status.head_mid = sd->setlook_head_mid;
  11551. if (sd->setlook_head_top)
  11552. sd->status.head_top = sd->setlook_head_top;
  11553. if (sd->setlook_robe)
  11554. sd->status.robe = sd->setlook_robe;
  11555. if (head_low != sd->status.head_bottom)
  11556. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11557. if (head_mid != sd->status.head_mid)
  11558. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11559. if (head_top != sd->status.head_top)
  11560. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11561. if (robe != sd->status.robe)
  11562. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11563. }
  11564. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  11565. uint32 date = date_get(DT_YYYYMMDD);
  11566. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  11567. std::shared_ptr<s_attendance_period> period = pair.second;
  11568. if( period->start <= date && period->end >= date ){
  11569. return period;
  11570. }
  11571. }
  11572. return nullptr;
  11573. }
  11574. bool pc_attendance_enabled(){
  11575. // Check if the attendance feature is disabled
  11576. if( !battle_config.feature_attendance ){
  11577. return false;
  11578. }
  11579. // Check if there is a running attendance period
  11580. return pc_attendance_period() != nullptr;
  11581. }
  11582. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11583. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11584. }
  11585. int32 pc_attendance_counter( struct map_session_data* sd ){
  11586. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11587. // No running attendance period
  11588. if( period == nullptr ){
  11589. return 0;
  11590. }
  11591. // Get the counter for the current period
  11592. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11593. // Check if we have a remaining counter from a previous period
  11594. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11595. // Reset the counter to zero
  11596. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11597. return 0;
  11598. }
  11599. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11600. }
  11601. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11602. // If the user's group does not have the permission
  11603. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11604. return;
  11605. }
  11606. // Check if the attendance feature is disabled
  11607. if( !pc_attendance_enabled() ){
  11608. return;
  11609. }
  11610. // Check if the user already got his reward today
  11611. if( pc_attendance_rewarded_today( sd ) ){
  11612. return;
  11613. }
  11614. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11615. attendance_counter += 1;
  11616. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11617. if( period == nullptr ){
  11618. return;
  11619. }
  11620. if( period->rewards.size() < attendance_counter ){
  11621. return;
  11622. }
  11623. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11624. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11625. if( save_settings&CHARSAVE_ATTENDANCE )
  11626. chrif_save(sd, CSAVE_NORMAL);
  11627. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  11628. struct mail_message msg;
  11629. memset( &msg, 0, sizeof( struct mail_message ) );
  11630. msg.dest_id = sd->status.char_id;
  11631. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11632. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11633. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11634. msg.item[0].nameid = reward->item_id;
  11635. msg.item[0].amount = reward->amount;
  11636. msg.item[0].identify = 1;
  11637. msg.status = MAIL_NEW;
  11638. msg.type = MAIL_INBOX_NORMAL;
  11639. msg.timestamp = time(NULL);
  11640. intif_Mail_send(0, &msg);
  11641. clif_attendence_response( sd, attendance_counter );
  11642. }
  11643. /*==========================================
  11644. * pc Init/Terminate
  11645. *------------------------------------------*/
  11646. void do_final_pc(void) {
  11647. db_destroy(itemcd_db);
  11648. do_final_pc_groups();
  11649. ers_destroy(pc_sc_display_ers);
  11650. ers_destroy(num_reg_ers);
  11651. ers_destroy(str_reg_ers);
  11652. attendance_db.clear();
  11653. }
  11654. void do_init_pc(void) {
  11655. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11656. pc_readdb();
  11657. pc_read_motd(); // Read MOTD [Valaris]
  11658. attendance_db.load();
  11659. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11660. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11661. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11662. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11663. add_timer_func_list(pc_autosave, "pc_autosave");
  11664. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11665. add_timer_func_list(pc_soulball_timer, "pc_soulball_timer");
  11666. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11667. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11668. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11669. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11670. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11671. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11672. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11673. // 0=day, 1=night [Yor]
  11674. night_flag = battle_config.night_at_start ? 1 : 0;
  11675. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11676. int day_duration = battle_config.day_duration;
  11677. int night_duration = battle_config.night_duration;
  11678. // add night/day timer [Yor]
  11679. add_timer_func_list(map_day_timer, "map_day_timer");
  11680. add_timer_func_list(map_night_timer, "map_night_timer");
  11681. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11682. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11683. }
  11684. do_init_pc_groups();
  11685. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11686. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11687. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11688. ers_chunk_size(pc_sc_display_ers, 150);
  11689. ers_chunk_size(num_reg_ers, 300);
  11690. ers_chunk_size(str_reg_ers, 50);
  11691. }