pc.cpp 401 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935
  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/ers.hpp" // ers_destroy
  11. #include "../common/malloc.hpp"
  12. #include "../common/mmo.hpp" //NAME_LENGTH
  13. #include "../common/nullpo.hpp"
  14. #include "../common/random.hpp"
  15. #include "../common/showmsg.hpp"
  16. #include "../common/socket.hpp" // session[]
  17. #include "../common/strlib.hpp" // safestrncpy()
  18. #include "../common/timer.hpp"
  19. #include "../common/utilities.hpp"
  20. #include "../common/utils.hpp"
  21. #include "achievement.hpp"
  22. #include "atcommand.hpp" // get_atcommand_level()
  23. #include "battle.hpp" // battle_config
  24. #include "battleground.hpp"
  25. #include "buyingstore.hpp" // struct s_buyingstore
  26. #include "channel.hpp"
  27. #include "chat.hpp"
  28. #include "chrif.hpp"
  29. #include "clan.hpp"
  30. #include "clif.hpp"
  31. #include "date.hpp" // is_day_of_*()
  32. #include "duel.hpp"
  33. #include "elemental.hpp"
  34. #include "guild.hpp"
  35. #include "homunculus.hpp"
  36. #include "instance.hpp"
  37. #include "intif.hpp"
  38. #include "itemdb.hpp" // MAX_ITEMGROUP
  39. #include "log.hpp"
  40. #include "map.hpp"
  41. #include "mercenary.hpp"
  42. #include "mob.hpp"
  43. #include "npc.hpp"
  44. #include "party.hpp" // party_search()
  45. #include "pc_groups.hpp"
  46. #include "pet.hpp" // pet_unlocktarget()
  47. #include "quest.hpp"
  48. #include "script.hpp" // struct script_reg, struct script_regstr
  49. #include "searchstore.hpp" // struct s_search_store_info
  50. #include "status.hpp" // OPTION_*, struct weapon_atk
  51. #include "storage.hpp"
  52. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  53. #include "vending.hpp" // struct s_vending
  54. using namespace rathena;
  55. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  56. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
  57. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  58. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  59. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  60. static unsigned int statp[MAX_LEVEL+1];
  61. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  62. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  63. #endif
  64. // h-files are for declarations, not for implementations... [Shinomori]
  65. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  66. // timer for night.day implementation
  67. int day_timer_tid = INVALID_TIMER;
  68. int night_timer_tid = INVALID_TIMER;
  69. struct eri *pc_sc_display_ers = NULL;
  70. struct eri *pc_itemgrouphealrate_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. struct s_attendance_reward{
  79. uint16 item_id;
  80. uint16 amount;
  81. };
  82. struct s_attendance_period{
  83. uint32 start;
  84. uint32 end;
  85. std::map<int,struct s_attendance_reward> rewards;
  86. };
  87. std::vector<struct s_attendance_period> attendance_periods;
  88. #define MOTD_LINE_SIZE 128
  89. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  90. bool reg_load;
  91. /**
  92. * Translation table from athena equip index to aegis bitmask
  93. */
  94. unsigned int equip_bitmask[EQI_MAX] = {
  95. EQP_ACC_L, // EQI_ACC_L
  96. EQP_ACC_R, // EQI_ACC_R
  97. EQP_SHOES, // EQI_SHOES
  98. EQP_GARMENT, // EQI_GARMENT
  99. EQP_HEAD_LOW, // EQI_HEAD_LOW
  100. EQP_HEAD_MID, // EQI_HEAD_MID
  101. EQP_HEAD_TOP, // EQI_HEAD_TOP
  102. EQP_ARMOR, // EQI_ARMOR
  103. EQP_HAND_L, // EQI_HAND_L
  104. EQP_HAND_R, // EQI_HAND_R
  105. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  106. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  107. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  108. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  109. EQP_AMMO, // EQI_AMMO
  110. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  111. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  112. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  113. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  114. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  115. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  116. };
  117. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  118. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  119. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  120. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  121. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  122. };
  123. void pc_set_reg_load( bool val ){
  124. reg_load = val;
  125. }
  126. /**
  127. * Item Cool Down Delay Saving
  128. * Struct item_cd is not a member of struct map_session_data
  129. * to keep cooldowns in memory between player log-ins.
  130. * All cooldowns are reset when server is restarted.
  131. **/
  132. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  133. struct item_cd {
  134. unsigned int tick[MAX_ITEMDELAYS]; //tick
  135. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  136. };
  137. /**
  138. * Converts a class to its array index for CLASS_COUNT defined arrays.
  139. * Note that it does not do a validity check for speed purposes, where parsing
  140. * player input make sure to use a pcdb_checkid first!
  141. * @param class_ Job ID see enum e_job
  142. * @return Class Index
  143. */
  144. int pc_class2idx(int class_) {
  145. if (class_ >= JOB_NOVICE_HIGH)
  146. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  147. return class_;
  148. }
  149. /**
  150. * Get player's group ID
  151. * @param sd
  152. * @return Group ID
  153. */
  154. int pc_get_group_id(struct map_session_data *sd) {
  155. return sd->group_id;
  156. }
  157. /** Get player's group Level
  158. * @param sd
  159. * @return Group Level
  160. */
  161. int pc_get_group_level(struct map_session_data *sd) {
  162. return sd->group_level;
  163. }
  164. static TIMER_FUNC(pc_invincible_timer){
  165. struct map_session_data *sd;
  166. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  167. return 1;
  168. if(sd->invincible_timer != tid){
  169. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  170. return 0;
  171. }
  172. sd->invincible_timer = INVALID_TIMER;
  173. skill_unit_move(&sd->bl,tick,1);
  174. return 0;
  175. }
  176. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  177. nullpo_retv(sd);
  178. if( sd->invincible_timer != INVALID_TIMER )
  179. delete_timer(sd->invincible_timer,pc_invincible_timer);
  180. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  181. }
  182. void pc_delinvincibletimer(struct map_session_data* sd)
  183. {
  184. nullpo_retv(sd);
  185. if( sd->invincible_timer != INVALID_TIMER )
  186. {
  187. delete_timer(sd->invincible_timer,pc_invincible_timer);
  188. sd->invincible_timer = INVALID_TIMER;
  189. skill_unit_move(&sd->bl,gettick(),1);
  190. }
  191. }
  192. static TIMER_FUNC(pc_spiritball_timer){
  193. struct map_session_data *sd;
  194. int i;
  195. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  196. return 1;
  197. if( sd->spiritball <= 0 )
  198. {
  199. 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);
  200. sd->spiritball = 0;
  201. return 0;
  202. }
  203. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  204. if( i == sd->spiritball )
  205. {
  206. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  207. return 0;
  208. }
  209. sd->spiritball--;
  210. if( i != sd->spiritball )
  211. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  212. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  213. clif_spiritball(&sd->bl);
  214. return 0;
  215. }
  216. /**
  217. * Adds a spiritball to player for 'interval' ms
  218. * @param sd
  219. * @param interval
  220. * @param max
  221. */
  222. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  223. {
  224. int tid;
  225. uint8 i;
  226. nullpo_retv(sd);
  227. if(max > MAX_SPIRITBALL)
  228. max = MAX_SPIRITBALL;
  229. if(sd->spiritball < 0)
  230. sd->spiritball = 0;
  231. if( sd->spiritball && sd->spiritball >= max )
  232. {
  233. if(sd->spirit_timer[0] != INVALID_TIMER)
  234. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  235. sd->spiritball--;
  236. if( sd->spiritball != 0 )
  237. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  238. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  239. }
  240. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  241. 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);
  242. if( i != sd->spiritball )
  243. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  244. sd->spirit_timer[i] = tid;
  245. sd->spiritball++;
  246. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  247. clif_millenniumshield(&sd->bl,sd->spiritball);
  248. else
  249. clif_spiritball(&sd->bl);
  250. }
  251. /**
  252. * Removes number of spiritball from player
  253. * @param sd
  254. * @param count
  255. * @param type 1 = doesn't give client effect
  256. */
  257. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  258. {
  259. uint8 i;
  260. nullpo_retv(sd);
  261. if(sd->spiritball <= 0) {
  262. sd->spiritball = 0;
  263. return;
  264. }
  265. if(count == 0)
  266. return;
  267. if(count > sd->spiritball)
  268. count = sd->spiritball;
  269. sd->spiritball -= count;
  270. if(count > MAX_SPIRITBALL)
  271. count = MAX_SPIRITBALL;
  272. for(i=0;i<count;i++) {
  273. if(sd->spirit_timer[i] != INVALID_TIMER) {
  274. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  275. sd->spirit_timer[i] = INVALID_TIMER;
  276. }
  277. }
  278. for(i=count;i<MAX_SPIRITBALL;i++) {
  279. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  280. sd->spirit_timer[i] = INVALID_TIMER;
  281. }
  282. if(!type) {
  283. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  284. clif_millenniumshield(&sd->bl,sd->spiritball);
  285. else
  286. clif_spiritball(&sd->bl);
  287. }
  288. }
  289. /**
  290. * Increases a player's fame points and displays a notice to him
  291. * @param sd Player
  292. * @param count Fame point
  293. */
  294. void pc_addfame(struct map_session_data *sd,int count)
  295. {
  296. enum e_rank ranktype;
  297. nullpo_retv(sd);
  298. sd->status.fame += count;
  299. if(sd->status.fame > MAX_FAME)
  300. sd->status.fame = MAX_FAME;
  301. switch(sd->class_&MAPID_UPPERMASK){
  302. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  303. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  304. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  305. default:
  306. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  307. return;
  308. }
  309. clif_update_rankingpoint(sd,ranktype,count);
  310. chrif_updatefamelist(sd);
  311. }
  312. /**
  313. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  314. * @param sd
  315. * @param job Job use enum e_mapid
  316. * @return Rank
  317. */
  318. unsigned char pc_famerank(uint32 char_id, int job)
  319. {
  320. uint8 i;
  321. switch(job){
  322. case MAPID_BLACKSMITH: // Blacksmith
  323. for(i = 0; i < MAX_FAME_LIST; i++){
  324. if(smith_fame_list[i].id == char_id)
  325. return i + 1;
  326. }
  327. break;
  328. case MAPID_ALCHEMIST: // Alchemist
  329. for(i = 0; i < MAX_FAME_LIST; i++){
  330. if(chemist_fame_list[i].id == char_id)
  331. return i + 1;
  332. }
  333. break;
  334. case MAPID_TAEKWON: // Taekwon
  335. for(i = 0; i < MAX_FAME_LIST; i++){
  336. if(taekwon_fame_list[i].id == char_id)
  337. return i + 1;
  338. }
  339. break;
  340. }
  341. return 0;
  342. }
  343. /**
  344. * Restart player's HP & SP value
  345. * @param sd
  346. * @param type Restart type: 1 - Normal Resurection
  347. */
  348. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  349. struct status_data *status, *b_status;
  350. nullpo_retv(sd);
  351. b_status = &sd->base_status;
  352. status = &sd->battle_status;
  353. if (type&1) { //Normal resurrection
  354. status->hp = 1; //Otherwise status_heal may fail if dead.
  355. status_heal(&sd->bl, b_status->hp, 0, 1);
  356. if( status->sp < b_status->sp )
  357. status_set_sp(&sd->bl, b_status->sp, 1);
  358. } else { //Just for saving on the char-server (with values as if respawned)
  359. sd->status.hp = b_status->hp;
  360. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  361. }
  362. }
  363. /*==========================================
  364. Rental System
  365. *------------------------------------------*/
  366. /**
  367. * Ends a rental and removes the item/effect
  368. * @param tid: Tick ID
  369. * @param tick: Timer
  370. * @param id: Timer ID
  371. * @param data: Data
  372. * @return false - failure, true - success
  373. */
  374. TIMER_FUNC(pc_inventory_rental_end){
  375. struct map_session_data *sd = map_id2sd(id);
  376. if( sd == NULL )
  377. return 0;
  378. if( tid != sd->rental_timer ) {
  379. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  380. return 0;
  381. }
  382. pc_inventory_rentals(sd);
  383. return 1;
  384. }
  385. /**
  386. * Removes the rental timer from the player
  387. * @param sd: Player data
  388. */
  389. void pc_inventory_rental_clear(struct map_session_data *sd)
  390. {
  391. if( sd->rental_timer != INVALID_TIMER ) {
  392. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  393. sd->rental_timer = INVALID_TIMER;
  394. }
  395. }
  396. /**
  397. * Check for items in the player's inventory that are rental type
  398. * @param sd: Player data
  399. */
  400. void pc_inventory_rentals(struct map_session_data *sd)
  401. {
  402. int i, c = 0;
  403. unsigned int next_tick = UINT_MAX;
  404. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  405. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  406. continue; // Nothing here
  407. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  408. continue;
  409. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  410. if (sd->inventory_data[i]->unequip_script)
  411. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  412. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  413. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  414. } else {
  415. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  416. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  417. next_tick = umin(expire_tick * 1000U, next_tick);
  418. c++;
  419. }
  420. }
  421. 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
  422. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  423. else
  424. sd->rental_timer = INVALID_TIMER;
  425. }
  426. /**
  427. * Add a rental item to the player and adjusts the rental timer appropriately
  428. * @param sd: Player data
  429. * @param seconds: Rental time
  430. */
  431. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  432. {
  433. unsigned int tick = seconds * 1000;
  434. if( sd == NULL )
  435. return;
  436. if( sd->rental_timer != INVALID_TIMER ) {
  437. const struct TimerData * td;
  438. td = get_timer(sd->rental_timer);
  439. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  440. pc_inventory_rental_clear(sd);
  441. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  442. }
  443. } else
  444. sd->rental_timer = add_timer(gettick() + umin(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  445. }
  446. /**
  447. * Check if the player can sell the current item
  448. * @param sd: map_session_data of the player
  449. * @param item: struct of the checking item
  450. * @param shoptype: NPC's sub type see enum npc_subtype
  451. * @return bool 'true' is sellable, 'false' otherwise
  452. */
  453. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  454. if (sd == NULL || item == NULL)
  455. return false;
  456. if (item->equip > 0 || item->amount < 0)
  457. return false;
  458. if (battle_config.hide_fav_sell && item->favorite)
  459. return false; //Cannot sell favs (optional config)
  460. if (!battle_config.rental_transaction && item->expire_time)
  461. return false; // Cannot Sell Rental Items
  462. switch (shoptype) {
  463. case NPCTYPE_SHOP:
  464. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  465. item->bound != BOUND_GUILD ||
  466. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  467. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  468. ))
  469. return true;
  470. break;
  471. case NPCTYPE_ITEMSHOP:
  472. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  473. item->bound != BOUND_GUILD ||
  474. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  475. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  476. ))
  477. return true;
  478. else if (!item->bound) {
  479. struct item_data *itd = itemdb_search(item->nameid);
  480. if (itd && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  481. return true;
  482. }
  483. break;
  484. }
  485. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  486. return false;
  487. if (item->bound && !pc_can_give_bounded_items(sd))
  488. return false; // Don't allow sale of bound items
  489. return true;
  490. }
  491. /**
  492. * Determines if player can give / drop / trade / vend items
  493. */
  494. bool pc_can_give_items(struct map_session_data *sd)
  495. {
  496. return pc_has_permission(sd, PC_PERM_TRADE);
  497. }
  498. /**
  499. * Determines if player can give / drop / trade / vend bounded items
  500. */
  501. bool pc_can_give_bounded_items(struct map_session_data *sd)
  502. {
  503. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  504. }
  505. /*==========================================
  506. * Prepares character for saving.
  507. * @param sd
  508. *------------------------------------------*/
  509. void pc_makesavestatus(struct map_session_data *sd) {
  510. nullpo_retv(sd);
  511. if(!battle_config.save_clothcolor)
  512. sd->status.clothes_color = 0;
  513. // Since this is currently not officially released,
  514. // its best to have a forced option to not save body styles.
  515. if(!battle_config.save_body_style)
  516. sd->status.body = 0;
  517. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  518. //status change load/saving. [Skotlex]
  519. #ifdef NEW_CARTS
  520. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  521. #else
  522. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  523. #endif
  524. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  525. if(pc_isdead(sd)){
  526. pc_setrestartvalue(sd, 0);
  527. } else {
  528. sd->status.hp = sd->battle_status.hp;
  529. sd->status.sp = sd->battle_status.sp;
  530. }
  531. sd->status.last_point.map = sd->mapindex;
  532. sd->status.last_point.x = sd->bl.x;
  533. sd->status.last_point.y = sd->bl.y;
  534. return;
  535. }
  536. if(pc_isdead(sd)) {
  537. pc_setrestartvalue(sd, 0);
  538. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  539. } else {
  540. sd->status.hp = sd->battle_status.hp;
  541. sd->status.sp = sd->battle_status.sp;
  542. sd->status.last_point.map = sd->mapindex;
  543. sd->status.last_point.x = sd->bl.x;
  544. sd->status.last_point.y = sd->bl.y;
  545. }
  546. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  547. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  548. if(mapdata->save.map)
  549. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  550. else
  551. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  552. }
  553. }
  554. /*==========================================
  555. * Off init ? Connection?
  556. *------------------------------------------*/
  557. void pc_setnewpc(struct map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, unsigned int client_tick, int sex, int fd) {
  558. nullpo_retv(sd);
  559. sd->bl.id = account_id;
  560. sd->status.account_id = account_id;
  561. sd->status.char_id = char_id;
  562. sd->status.sex = sex;
  563. sd->login_id1 = login_id1;
  564. sd->login_id2 = 0; // at this point, we can not know the value :(
  565. sd->client_tick = client_tick;
  566. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  567. sd->bl.type = BL_PC;
  568. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  569. sd->canlog_tick = gettick();
  570. //Required to prevent homunculus copuing a base speed of 0.
  571. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  572. }
  573. /**
  574. * Get equip point for an equip
  575. * @param sd
  576. * @param id
  577. */
  578. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  579. int ep = 0;
  580. nullpo_ret(sd);
  581. nullpo_ret(id);
  582. if (!itemdb_isequip2(id))
  583. return 0; //Not equippable by players.
  584. ep = id->equip;
  585. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  586. 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]
  587. if (ep == EQP_WEAPON)
  588. return EQP_ARMS;
  589. if (ep == EQP_SHADOW_WEAPON)
  590. return EQP_SHADOW_ARMS;
  591. }
  592. }
  593. return ep;
  594. }
  595. /**
  596. * Get equip point for an equip
  597. * @param sd
  598. * @param n Equip index in inventory
  599. */
  600. int pc_equippoint(struct map_session_data *sd,int n){
  601. nullpo_ret(sd);
  602. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  603. }
  604. /**
  605. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  606. * @param sd : player session
  607. * @return 0 sucess, 1:invalid sd
  608. */
  609. void pc_setinventorydata(struct map_session_data *sd)
  610. {
  611. uint8 i;
  612. nullpo_retv(sd);
  613. for(i = 0; i < MAX_INVENTORY; i++) {
  614. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  615. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  616. }
  617. }
  618. /**
  619. * 'Calculates' weapon type
  620. * @param sd : Player
  621. */
  622. void pc_calcweapontype(struct map_session_data *sd)
  623. {
  624. nullpo_retv(sd);
  625. // single-hand
  626. if(sd->weapontype2 == W_FIST) {
  627. sd->status.weapon = sd->weapontype1;
  628. return;
  629. }
  630. if(sd->weapontype1 == W_FIST) {
  631. sd->status.weapon = sd->weapontype2;
  632. return;
  633. }
  634. // dual-wield
  635. sd->status.weapon = 0;
  636. switch (sd->weapontype1){
  637. case W_DAGGER:
  638. switch (sd->weapontype2) {
  639. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  640. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  641. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  642. }
  643. break;
  644. case W_1HSWORD:
  645. switch (sd->weapontype2) {
  646. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  647. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  648. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  649. }
  650. break;
  651. case W_1HAXE:
  652. switch (sd->weapontype2) {
  653. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  654. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  655. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  656. }
  657. }
  658. // unknown, default to right hand type
  659. if (!sd->status.weapon)
  660. sd->status.weapon = sd->weapontype1;
  661. }
  662. /**
  663. * Set equip index
  664. * @param sd : Player
  665. */
  666. void pc_setequipindex(struct map_session_data *sd)
  667. {
  668. uint16 i;
  669. nullpo_retv(sd);
  670. for (i = 0; i < EQI_MAX; i++)
  671. sd->equip_index[i] = -1;
  672. for (i = 0; i < MAX_INVENTORY; i++) {
  673. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  674. continue;
  675. if (sd->inventory.u.items_inventory[i].equip) {
  676. uint8 j;
  677. for (j = 0; j < EQI_MAX; j++)
  678. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  679. sd->equip_index[j] = i;
  680. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  681. if (sd->inventory_data[i])
  682. sd->weapontype1 = sd->inventory_data[i]->look;
  683. else
  684. sd->weapontype1 = 0;
  685. }
  686. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  687. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  688. sd->weapontype2 = sd->inventory_data[i]->look;
  689. else
  690. sd->weapontype2 = 0;
  691. }
  692. }
  693. }
  694. pc_calcweapontype(sd);
  695. }
  696. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  697. //{
  698. // int i;
  699. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  700. // struct item_data *data;
  701. //
  702. // //Crafted/made/hatched items.
  703. // if (itemdb_isspecial(item->card[0]))
  704. // return 1;
  705. //
  706. // /* scan for enchant armor gems */
  707. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  708. // s = MAX_SLOTS - 1;
  709. //
  710. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  711. // return( i < s ) ? 0 : 1;
  712. //}
  713. /**
  714. * Check if an item is equiped by player
  715. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  716. * @param sd : player session
  717. * @param nameid : itemid
  718. * @return 1:yes, 0:no
  719. */
  720. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  721. {
  722. uint8 i;
  723. for( i = 0; i < EQI_MAX; i++ )
  724. {
  725. short index = sd->equip_index[i], j;
  726. if( index < 0 )
  727. continue;
  728. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  729. continue;
  730. if( !sd->inventory_data[index] )
  731. continue;
  732. if( sd->inventory_data[index]->nameid == nameid )
  733. return true;
  734. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  735. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  736. return true;
  737. }
  738. }
  739. return false;
  740. }
  741. /**
  742. * Check adoption rules
  743. * @param p1_sd: Player 1
  744. * @param p2_sd: Player 2
  745. * @param b_sd: Player that will be adopted
  746. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  747. * ADOPT_ALREADY_ADOPTED - Already adopted
  748. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  749. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  750. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  751. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  752. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  753. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  754. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  755. */
  756. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  757. {
  758. if( !p1_sd || !p2_sd || !b_sd )
  759. return ADOPT_CHARACTER_NOT_FOUND;
  760. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  761. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  762. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  763. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  764. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  765. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  766. if( p2_sd->status.party_id != p1_sd->status.party_id )
  767. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  768. // Parents need to have their ring equipped
  769. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  770. return ADOPT_EQUIP_RINGS;
  771. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  772. return ADOPT_EQUIP_RINGS;
  773. // Already adopted a baby
  774. if( p1_sd->status.child || p2_sd->status.child ) {
  775. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  776. return ADOPT_MORE_CHILDREN;
  777. }
  778. // Parents need at least lvl 70 to adopt
  779. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  780. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  781. return ADOPT_LEVEL_70;
  782. }
  783. if( b_sd->status.partner_id ) {
  784. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  785. return ADOPT_MARRIED;
  786. }
  787. 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 ) )
  788. return ADOPT_NOT_NOVICE;
  789. return ADOPT_ALLOWED;
  790. }
  791. /*==========================================
  792. * Adoption Process
  793. *------------------------------------------*/
  794. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  795. {
  796. int job, joblevel;
  797. unsigned int jobexp;
  798. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  799. return false;
  800. // Preserve current job levels and progress
  801. joblevel = b_sd->status.job_level;
  802. jobexp = b_sd->status.job_exp;
  803. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  804. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  805. { // Success, proceed to configure parents and baby skills
  806. p1_sd->status.child = b_sd->status.char_id;
  807. p2_sd->status.child = b_sd->status.char_id;
  808. b_sd->status.father = p1_sd->status.char_id;
  809. b_sd->status.mother = p2_sd->status.char_id;
  810. // Restore progress
  811. b_sd->status.job_level = joblevel;
  812. clif_updatestatus(b_sd, SP_JOBLEVEL);
  813. b_sd->status.job_exp = jobexp;
  814. clif_updatestatus(b_sd, SP_JOBEXP);
  815. // Baby Skills
  816. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  817. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  818. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  819. // Parents Skills
  820. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  821. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  822. chrif_save(p1_sd, CSAVE_NORMAL);
  823. chrif_save(p2_sd, CSAVE_NORMAL);
  824. chrif_save(b_sd, CSAVE_NORMAL);
  825. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  826. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  827. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  828. return true;
  829. }
  830. return false; // Job Change Fail
  831. }
  832. /*==========================================
  833. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  834. Returns:
  835. false : Cannot use/equip
  836. true : Can use/equip
  837. * Credits:
  838. [Inkfish] for first idea
  839. [Haru] for third-classes extension
  840. [Cydh] finishing :D
  841. *------------------------------------------*/
  842. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  843. while (1) {
  844. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  845. break;
  846. #ifndef RENEWAL
  847. //allow third classes to use trans. class items
  848. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  849. break;
  850. //third-baby classes can use same item too
  851. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  852. break;
  853. //don't need to decide specific rules for third-classes?
  854. //items for third classes can be used for all third classes
  855. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  856. break;
  857. #else
  858. //trans. classes (exl. third-trans.)
  859. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  860. break;
  861. //baby classes (exl. third-baby)
  862. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  863. break;
  864. //third classes (exl. third-trans. and baby-third)
  865. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  866. break;
  867. //trans-third classes
  868. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  869. break;
  870. //third-baby classes
  871. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  872. break;
  873. #endif
  874. return false;
  875. }
  876. return true;
  877. }
  878. /*=================================================
  879. * Checks if the player can equip the item at index n in inventory.
  880. * @param sd
  881. * @param n Item index in inventory
  882. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  883. *------------------------------------------------*/
  884. uint8 pc_isequip(struct map_session_data *sd,int n)
  885. {
  886. struct item_data *item;
  887. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  888. item = sd->inventory_data[n];
  889. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  890. return ITEM_EQUIP_ACK_OK;
  891. if(item == NULL)
  892. return ITEM_EQUIP_ACK_FAIL;
  893. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  894. return ITEM_EQUIP_ACK_FAILLEVEL;
  895. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  896. return ITEM_EQUIP_ACK_FAILLEVEL;
  897. if(item->sex != 2 && sd->status.sex != item->sex)
  898. return ITEM_EQUIP_ACK_FAIL;
  899. //fail to equip if item is restricted
  900. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  901. return ITEM_EQUIP_ACK_FAIL;
  902. if (item->equip&EQP_AMMO) {
  903. switch (item->look) {
  904. case AMMO_ARROW:
  905. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  906. clif_msg(sd, ITEM_NEED_BOW);
  907. return ITEM_EQUIP_ACK_FAIL;
  908. }
  909. break;
  910. case AMMO_THROWABLE_DAGGER:
  911. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  912. return ITEM_EQUIP_ACK_FAIL;
  913. break;
  914. case AMMO_BULLET:
  915. case AMMO_SHELL:
  916. 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
  917. #ifdef RENEWAL
  918. && sd->status.weapon != W_GRENADE
  919. #endif
  920. ) {
  921. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  922. return ITEM_EQUIP_ACK_FAIL;
  923. }
  924. break;
  925. #ifndef RENEWAL
  926. case AMMO_GRENADE:
  927. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  928. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  929. return ITEM_EQUIP_ACK_FAIL;
  930. }
  931. break;
  932. #endif
  933. case AMMO_CANNONBALL:
  934. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  935. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  936. return ITEM_EQUIP_ACK_FAIL;
  937. }
  938. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  939. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  940. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  941. return ITEM_EQUIP_ACK_FAIL;
  942. }
  943. break;
  944. }
  945. }
  946. if (sd->sc.count) {
  947. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  948. return ITEM_EQUIP_ACK_FAIL;
  949. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  950. return ITEM_EQUIP_ACK_FAIL;
  951. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  952. return ITEM_EQUIP_ACK_FAIL;
  953. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  954. return ITEM_EQUIP_ACK_FAIL;
  955. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  956. return ITEM_EQUIP_ACK_FAIL;
  957. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  958. return ITEM_EQUIP_ACK_FAIL;
  959. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  960. return ITEM_EQUIP_ACK_FAIL;
  961. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  962. //Spirit of Super Novice equip bonuses. [Skotlex]
  963. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  964. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  965. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  966. switch(item->look) { //In weapons, the look determines type of weapon.
  967. case W_DAGGER: //All level 4 - Daggers
  968. case W_1HSWORD: //All level 4 - 1H Swords
  969. case W_1HAXE: //All level 4 - 1H Axes
  970. case W_MACE: //All level 4 - 1H Maces
  971. case W_STAFF: //All level 4 - 1H Staves
  972. case W_2HSTAFF: //All level 4 - 2H Staves
  973. return ITEM_EQUIP_ACK_OK;
  974. }
  975. }
  976. }
  977. //Not equipable by class. [Skotlex]
  978. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  979. return ITEM_EQUIP_ACK_FAIL;
  980. if (!pc_isItemClass(sd, item))
  981. return ITEM_EQUIP_ACK_FAIL;
  982. return ITEM_EQUIP_ACK_OK;
  983. }
  984. /*==========================================
  985. * No problem with the session id
  986. * set the status that has been sent from char server
  987. *------------------------------------------*/
  988. 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)
  989. {
  990. int i;
  991. unsigned long tick = gettick();
  992. uint32 ip = session[sd->fd]->client_addr;
  993. sd->login_id2 = login_id2;
  994. sd->group_id = group_id;
  995. /* load user permissions */
  996. pc_group_pc_load(sd);
  997. memcpy(&sd->status, st, sizeof(*st));
  998. if (st->sex != sd->status.sex) {
  999. clif_authfail_fd(sd->fd, 0);
  1000. return false;
  1001. }
  1002. //Set the map-server used job id. [Skotlex]
  1003. i = pc_jobid2mapid(sd->status.class_);
  1004. if (i == -1) { //Invalid class?
  1005. 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);
  1006. sd->status.class_ = JOB_NOVICE;
  1007. sd->class_ = MAPID_NOVICE;
  1008. } else
  1009. sd->class_ = i;
  1010. // Checks and fixes to character status data, that are required
  1011. // in case of configuration change or stuff, which cannot be
  1012. // checked on char-server.
  1013. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1014. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1015. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1016. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1017. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1018. sd->state.connect_new = 1;
  1019. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1020. sd->invincible_timer = INVALID_TIMER;
  1021. sd->npc_timer_id = INVALID_TIMER;
  1022. sd->pvp_timer = INVALID_TIMER;
  1023. sd->expiration_tid = INVALID_TIMER;
  1024. sd->autotrade_tid = INVALID_TIMER;
  1025. #ifdef SECURE_NPCTIMEOUT
  1026. // Initialize to defaults/expected
  1027. sd->npc_idle_timer = INVALID_TIMER;
  1028. sd->npc_idle_tick = tick;
  1029. sd->npc_idle_type = NPCT_INPUT;
  1030. sd->state.ignoretimeout = false;
  1031. #endif
  1032. sd->canuseitem_tick = tick;
  1033. sd->canusecashfood_tick = tick;
  1034. sd->canequip_tick = tick;
  1035. sd->cantalk_tick = tick;
  1036. sd->canskill_tick = tick;
  1037. sd->cansendmail_tick = tick;
  1038. sd->idletime = last_tick;
  1039. for(i = 0; i < MAX_SPIRITBALL; i++)
  1040. sd->spirit_timer[i] = INVALID_TIMER;
  1041. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  1042. sd->autobonus[i].active = INVALID_TIMER;
  1043. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  1044. sd->autobonus2[i].active = INVALID_TIMER;
  1045. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  1046. sd->autobonus3[i].active = INVALID_TIMER;
  1047. if (battle_config.item_auto_get)
  1048. sd->state.autoloot = 10000;
  1049. if (battle_config.disp_experience)
  1050. sd->state.showexp = 1;
  1051. if (battle_config.disp_zeny)
  1052. sd->state.showzeny = 1;
  1053. #ifdef VIP_ENABLE
  1054. if (!battle_config.vip_disp_rate)
  1055. sd->vip.disableshowrate = 1;
  1056. #endif
  1057. if (!(battle_config.display_skill_fail&2))
  1058. sd->state.showdelay = 1;
  1059. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1060. memset(&sd->cart, 0, sizeof(struct s_storage));
  1061. memset(&sd->storage, 0, sizeof(struct s_storage));
  1062. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1063. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1064. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1065. sd->status.option &= ~OPTION_INVISIBLE;
  1066. }
  1067. status_change_init(&sd->bl);
  1068. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1069. unit_dataset(&sd->bl);
  1070. sd->guild_x = -1;
  1071. sd->guild_y = -1;
  1072. sd->delayed_damage = 0;
  1073. // Event Timers
  1074. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1075. sd->eventtimer[i] = INVALID_TIMER;
  1076. // Rental Timer
  1077. sd->rental_timer = INVALID_TIMER;
  1078. for( i = 0; i < 3; i++ )
  1079. sd->hate_mob[i] = -1;
  1080. sd->quest_log = NULL;
  1081. sd->num_quests = 0;
  1082. sd->avail_quests = 0;
  1083. sd->save_quest = false;
  1084. sd->count_rewarp = 0;
  1085. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1086. sd->regs.arrays = NULL;
  1087. sd->vars_dirty = false;
  1088. sd->vars_ok = false;
  1089. sd->vars_received = 0x0;
  1090. sd->qi_display = NULL;
  1091. sd->qi_count = 0;
  1092. //warp player
  1093. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1094. 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);
  1095. // try warping to a default map instead (church graveyard)
  1096. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1097. // if we fail again
  1098. clif_authfail_fd(sd->fd, 0);
  1099. return false;
  1100. }
  1101. }
  1102. clif_authok(sd);
  1103. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1104. sd->die_counter=-1;
  1105. //display login notice
  1106. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1107. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1108. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1109. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1110. sd->status.name, sd->status.account_id, sd->status.char_id,
  1111. CONVIP(ip), sd->group_id);
  1112. // Send friends list
  1113. clif_friendslist_send(sd);
  1114. if( !changing_mapservers ) {
  1115. if (battle_config.display_version == 1)
  1116. pc_show_version(sd);
  1117. // Message of the Day [Valaris]
  1118. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1119. if (battle_config.motd_type)
  1120. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1121. else
  1122. clif_displaymessage(sd->fd, motd_text[i]);
  1123. }
  1124. if (expiration_time != 0)
  1125. sd->expiration_time = expiration_time;
  1126. /**
  1127. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1128. **/
  1129. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1130. }
  1131. pc_validate_skill(sd);
  1132. /* [Ind] */
  1133. sd->sc_display = NULL;
  1134. sd->sc_display_count = 0;
  1135. // Player has not yet received the CashShop list
  1136. sd->status.cashshop_sent = false;
  1137. sd->last_addeditem_index = -1;
  1138. sd->bonus_script.head = NULL;
  1139. sd->bonus_script.count = 0;
  1140. #if PACKETVER >= 20150513
  1141. sd->hatEffectIDs = NULL;
  1142. sd->hatEffectCount = 0;
  1143. #endif
  1144. sd->catch_target_class = PET_CATCH_FAIL;
  1145. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1146. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1147. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1148. clif_updatestatus(sd, SP_BASEEXP);
  1149. }
  1150. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1151. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1152. clif_updatestatus(sd, SP_JOBEXP);
  1153. }
  1154. // Request all registries (auth is considered completed whence they arrive)
  1155. intif_request_registry(sd,7);
  1156. return true;
  1157. }
  1158. /*==========================================
  1159. * Closes a connection because it failed to be authenticated from the char server.
  1160. *------------------------------------------*/
  1161. void pc_authfail(struct map_session_data *sd)
  1162. {
  1163. clif_authfail_fd(sd->fd, 0);
  1164. return;
  1165. }
  1166. /**
  1167. * Player register a bl as hatred
  1168. * @param sd : player session
  1169. * @param pos : hate position [0;2]
  1170. * @param bl : target bl
  1171. * @return false:failed, true:success
  1172. */
  1173. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1174. {
  1175. int class_;
  1176. if (!sd || !bl || pos < 0 || pos > 2)
  1177. return false;
  1178. if (sd->hate_mob[pos] != -1)
  1179. { //Can't change hate targets.
  1180. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1181. return false;
  1182. }
  1183. class_ = status_get_class(bl);
  1184. if (!pcdb_checkid(class_)) {
  1185. unsigned int max_hp = status_get_max_hp(bl);
  1186. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1187. return false;
  1188. if (pos != status_get_size(bl))
  1189. return false; //Wrong size
  1190. }
  1191. sd->hate_mob[pos] = class_;
  1192. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1193. clif_hate_info(sd, pos, class_, 1);
  1194. return true;
  1195. }
  1196. /*==========================================
  1197. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1198. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1199. * See intif_parse_StorageReceived() for item operations [lighta]
  1200. *------------------------------------------*/
  1201. void pc_reg_received(struct map_session_data *sd)
  1202. {
  1203. uint8 i;
  1204. sd->vars_ok = true;
  1205. sd->change_level_2nd = pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR));
  1206. sd->change_level_3rd = pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR));
  1207. sd->die_counter = pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR));
  1208. sd->langtype = pc_readaccountreg(sd, add_str(LANGTYPE_VAR));
  1209. if (msg_checklangtype(sd->langtype,true) < 0)
  1210. sd->langtype = 0; //invalid langtype reset to default
  1211. // Cash shop
  1212. sd->cashPoints = pc_readaccountreg(sd, add_str(CASHPOINT_VAR));
  1213. sd->kafraPoints = pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR));
  1214. // Cooking Exp
  1215. sd->cook_mastery = pc_readglobalreg(sd, add_str(COOKMASTERY_VAR));
  1216. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1217. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1218. sd->mission_mobid = pc_readglobalreg(sd, add_str(TKMISSIONID_VAR));
  1219. sd->mission_count = pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR));
  1220. }
  1221. if (battle_config.feature_banking)
  1222. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1223. if (battle_config.feature_roulette) {
  1224. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1225. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1226. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1227. }
  1228. sd->roulette.prizeIdx = -1;
  1229. //SG map and mob read [Komurka]
  1230. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1231. uint16 j;
  1232. if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
  1233. sd->feel_map[i].index = j;
  1234. sd->feel_map[i].m = map_mapindex2mapid(j);
  1235. } else {
  1236. sd->feel_map[i].index = 0;
  1237. sd->feel_map[i].m = -1;
  1238. }
  1239. sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
  1240. }
  1241. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1242. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
  1243. sd->cloneskill_idx = skill_get_index(skid);
  1244. if (sd->cloneskill_idx > 0) {
  1245. sd->status.skill[sd->cloneskill_idx].id = skid;
  1246. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
  1247. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1248. sd->status.skill[sd->cloneskill_idx].lv = i;
  1249. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1250. }
  1251. }
  1252. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1253. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
  1254. sd->reproduceskill_idx = skill_get_index(skid);
  1255. if (sd->reproduceskill_idx > 0) {
  1256. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1257. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
  1258. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1259. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1260. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1261. }
  1262. }
  1263. //Weird... maybe registries were reloaded?
  1264. if (sd->state.active)
  1265. return;
  1266. sd->state.active = 1;
  1267. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1268. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1269. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1270. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1271. if (sd->status.party_id)
  1272. party_member_joined(sd);
  1273. if (sd->status.guild_id)
  1274. guild_member_joined(sd);
  1275. if( sd->status.clan_id )
  1276. clan_member_joined(sd);
  1277. // pet
  1278. if (sd->status.pet_id > 0)
  1279. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1280. // Homunculus [albator]
  1281. if( sd->status.hom_id > 0 )
  1282. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1283. if( sd->status.mer_id > 0 )
  1284. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1285. if( sd->status.ele_id > 0 )
  1286. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1287. map_addiddb(&sd->bl);
  1288. map_delnickdb(sd->status.char_id, sd->status.name);
  1289. if (!chrif_auth_finished(sd))
  1290. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1291. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1292. chrif_bsdata_request(sd->status.char_id);
  1293. #ifdef VIP_ENABLE
  1294. sd->vip.time = 0;
  1295. sd->vip.enabled = 0;
  1296. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1297. #endif
  1298. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1299. intif_request_questlog(sd);
  1300. if (battle_config.feature_achievement) {
  1301. sd->achievement_data.total_score = 0;
  1302. sd->achievement_data.level = 0;
  1303. sd->achievement_data.save = false;
  1304. sd->achievement_data.count = 0;
  1305. sd->achievement_data.incompleteCount = 0;
  1306. sd->achievement_data.achievements = NULL;
  1307. intif_request_achievements(sd->status.char_id);
  1308. }
  1309. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1310. sd->state.connect_new = 1;
  1311. clif_parse_LoadEndAck(sd->fd, sd);
  1312. }
  1313. if( pc_isinvisible(sd) ) {
  1314. sd->vd.class_ = JT_INVISIBLE;
  1315. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1316. // decrement the number of pvp players on the map
  1317. map_getmapdata(sd->bl.m)->users_pvp--;
  1318. if( map_getmapflag(sd->bl.m, MF_PVP) && !map_getmapflag(sd->bl.m, MF_PVP_NOCALCRANK) && sd->pvp_timer != INVALID_TIMER ){
  1319. // unregister the player for ranking
  1320. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1321. sd->pvp_timer = INVALID_TIMER;
  1322. }
  1323. clif_changeoption( &sd->bl );
  1324. }
  1325. channel_autojoin(sd);
  1326. }
  1327. static int pc_calc_skillpoint(struct map_session_data* sd)
  1328. {
  1329. uint16 i, skill_point = 0;
  1330. nullpo_ret(sd);
  1331. for(i = 1; i < MAX_SKILL; i++) {
  1332. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1333. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1334. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1335. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1336. )
  1337. {
  1338. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1339. skill_point += sd->status.skill[i].lv;
  1340. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1341. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1342. }
  1343. }
  1344. }
  1345. return skill_point;
  1346. }
  1347. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1348. uint16 i = 0;
  1349. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1350. return false;
  1351. /**
  1352. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1353. * skill tree and since they have no icons they'll give resource errors
  1354. * Get ALL skills except npc/guild ones. [Skotlex]
  1355. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1356. **/
  1357. for( i = 0; i < MAX_SKILL; i++ ) {
  1358. uint16 skill_id = skill_idx2id(i);
  1359. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1360. continue;
  1361. switch (skill_id) {
  1362. case SM_SELFPROVOKE:
  1363. case AB_DUPLELIGHT_MELEE:
  1364. case AB_DUPLELIGHT_MAGIC:
  1365. case WL_CHAINLIGHTNING_ATK:
  1366. case WL_TETRAVORTEX_FIRE:
  1367. case WL_TETRAVORTEX_WATER:
  1368. case WL_TETRAVORTEX_WIND:
  1369. case WL_TETRAVORTEX_GROUND:
  1370. case WL_SUMMON_ATK_FIRE:
  1371. case WL_SUMMON_ATK_WIND:
  1372. case WL_SUMMON_ATK_WATER:
  1373. case WL_SUMMON_ATK_GROUND:
  1374. case LG_OVERBRAND_BRANDISH:
  1375. case LG_OVERBRAND_PLUSATK:
  1376. case WM_SEVERE_RAINSTORM_MELEE:
  1377. case RL_R_TRIP_PLUSATK:
  1378. case SG_DEVIL:
  1379. case MO_TRIPLEATTACK:
  1380. case RG_SNATCHER:
  1381. continue;
  1382. default:
  1383. {
  1384. uint8 lv = (uint8)skill_get_max(skill_id);
  1385. if (lv > 0) {
  1386. sd->status.skill[i].id = skill_id;
  1387. if (addlv)
  1388. sd->status.skill[i].lv = lv;
  1389. }
  1390. }
  1391. break;
  1392. }
  1393. }
  1394. return true;
  1395. }
  1396. /*==========================================
  1397. * Calculation of skill level.
  1398. * @param sd
  1399. *------------------------------------------*/
  1400. void pc_calc_skilltree(struct map_session_data *sd)
  1401. {
  1402. int i, flag;
  1403. int c = 0;
  1404. nullpo_retv(sd);
  1405. i = pc_calc_skilltree_normalize_job(sd);
  1406. c = pc_mapid2jobid(i, sd->status.sex);
  1407. if( c == -1 )
  1408. { //Unable to normalize job??
  1409. ShowError("pc_calc_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", i, sd->status.name, sd->status.account_id, sd->status.char_id);
  1410. return;
  1411. }
  1412. c = pc_class2idx(c);
  1413. for( i = 0; i < MAX_SKILL; i++ ) {
  1414. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1415. sd->status.skill[i].id = 0; //First clear skills.
  1416. /* permanent skills that must be re-checked */
  1417. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1418. uint16 sk_id = skill_idx2id(i);
  1419. if (!sk_id) {
  1420. sd->status.skill[i].id = 0;
  1421. sd->status.skill[i].lv = 0;
  1422. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1423. continue;
  1424. }
  1425. switch (sk_id) {
  1426. case NV_TRICKDEAD:
  1427. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1428. sd->status.skill[i].id = 0;
  1429. sd->status.skill[i].lv = 0;
  1430. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1431. }
  1432. break;
  1433. }
  1434. }
  1435. }
  1436. for( i = 0; i < MAX_SKILL; i++ ) {
  1437. uint16 skill_id = 0;
  1438. // Restore original level of skills after deleting earned skills.
  1439. 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 ) {
  1440. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1441. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1442. }
  1443. //Enable Bard/Dancer spirit linked skills.
  1444. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1445. continue;
  1446. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1447. //Link Dancer skills to bard.
  1448. if( sd->status.sex ) {
  1449. if( sd->status.skill[i-8].lv < 10 )
  1450. continue;
  1451. sd->status.skill[i].id = skill_id;
  1452. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1453. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1454. }
  1455. //Link Bard skills to dancer.
  1456. else {
  1457. if( sd->status.skill[i].lv < 10 )
  1458. continue;
  1459. sd->status.skill[i-8].id = skill_id - 8;
  1460. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1461. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1462. }
  1463. }
  1464. }
  1465. // Removes Taekwon Ranker skill bonus
  1466. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1467. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1468. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1469. uint16 sk_id = skill_tree[c_][i].skill_id;
  1470. uint16 sk_idx = 0;
  1471. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1472. continue;
  1473. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1474. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1475. continue;
  1476. sd->status.skill[sk_idx].id = 0;
  1477. }
  1478. }
  1479. }
  1480. // Grant all skills
  1481. pc_grant_allskills(sd, false);
  1482. do {
  1483. uint16 skid = 0;
  1484. flag = 0;
  1485. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++) {
  1486. bool fail = false;
  1487. uint16 sk_idx = skill_get_index(skid);
  1488. if (sd->status.skill[sk_idx].id)
  1489. continue; //Skill already known.
  1490. if (!battle_config.skillfree) {
  1491. uint8 j;
  1492. // Checking required skills
  1493. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1494. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1495. uint16 sk_need_idx = 0;
  1496. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1497. short sk_need = sk_need_id;
  1498. 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)
  1499. sk_need = 0; //Not learned.
  1500. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1501. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1502. else
  1503. sk_need = pc_checkskill(sd,sk_need_id);
  1504. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1505. fail = true;
  1506. break;
  1507. }
  1508. }
  1509. }
  1510. if (sd->status.base_level < skill_tree[c][i].baselv) { //We need to get the actual class in this case
  1511. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1512. class_ = pc_class2idx(class_);
  1513. if (class_ == c || (class_ != c && sd->status.base_level < skill_tree[class_][i].baselv))
  1514. fail = true; // base level requirement wasn't satisfied
  1515. }
  1516. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1517. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1518. class_ = pc_class2idx(class_);
  1519. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1520. fail = true; // job level requirement wasn't satisfied
  1521. }
  1522. }
  1523. if (!fail) {
  1524. int inf2 = skill_get_inf2(skid);
  1525. if (!sd->status.skill[sk_idx].lv && (
  1526. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1527. inf2&INF2_WEDDING_SKILL ||
  1528. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1529. ))
  1530. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1531. sd->status.skill[sk_idx].id = skid;
  1532. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1533. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1534. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1535. }
  1536. flag = 1; // skill list has changed, perform another pass
  1537. }
  1538. }
  1539. } while(flag);
  1540. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1541. unsigned short skid = 0;
  1542. /* Taekwon Ranker Bonus Skill Tree
  1543. ============================================
  1544. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1545. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1546. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1547. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1548. uint16 sk_idx = 0;
  1549. if (!(sk_idx = skill_get_index(skid)))
  1550. continue;
  1551. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1552. continue; //Do not include Quest/Wedding skills.
  1553. if( sd->status.skill[sk_idx].id == 0 ) {
  1554. sd->status.skill[sk_idx].id = skid;
  1555. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1556. } else if( skid != NV_BASIC )
  1557. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1558. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1559. }
  1560. }
  1561. }
  1562. //Checks if you can learn a new skill after having leveled up a skill.
  1563. static void pc_check_skilltree(struct map_session_data *sd)
  1564. {
  1565. int i, flag = 0;
  1566. int c = 0;
  1567. if (battle_config.skillfree)
  1568. return; //Function serves no purpose if this is set
  1569. i = pc_calc_skilltree_normalize_job(sd);
  1570. c = pc_mapid2jobid(i, sd->status.sex);
  1571. if (c == -1) { //Unable to normalize job??
  1572. 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);
  1573. return;
  1574. }
  1575. c = pc_class2idx(c);
  1576. do {
  1577. uint16 skid = 0;
  1578. flag = 0;
  1579. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1580. uint16 sk_idx = skill_get_index(skid);
  1581. bool fail = false;
  1582. uint8 j = 0;
  1583. if (sd->status.skill[sk_idx].id) //Already learned
  1584. continue;
  1585. // Checking required skills
  1586. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1587. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1588. uint16 sk_need_idx = 0;
  1589. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1590. short sk_need = sk_need_id;
  1591. 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)
  1592. sk_need = 0; //Not learned.
  1593. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1594. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1595. else
  1596. sk_need = pc_checkskill(sd,sk_need_id);
  1597. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1598. fail = true;
  1599. break;
  1600. }
  1601. }
  1602. }
  1603. if( fail )
  1604. continue;
  1605. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1606. continue;
  1607. j = skill_get_inf2(skid);
  1608. if( !sd->status.skill[sk_idx].lv && (
  1609. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1610. j&INF2_WEDDING_SKILL ||
  1611. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1612. ) )
  1613. continue; //Cannot be learned via normal means.
  1614. sd->status.skill[sk_idx].id = skid;
  1615. flag = 1;
  1616. }
  1617. } while(flag);
  1618. }
  1619. // Make sure all the skills are in the correct condition
  1620. // before persisting to the backend.. [MouseJstr]
  1621. void pc_clean_skilltree(struct map_session_data *sd)
  1622. {
  1623. uint16 i;
  1624. for (i = 0; i < MAX_SKILL; i++){
  1625. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1626. sd->status.skill[i].id = 0;
  1627. sd->status.skill[i].lv = 0;
  1628. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1629. }
  1630. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1631. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1632. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1633. }
  1634. }
  1635. }
  1636. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1637. {
  1638. int skill_point, novice_skills;
  1639. int c = sd->class_;
  1640. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1641. return c;
  1642. skill_point = pc_calc_skillpoint(sd);
  1643. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1644. // limit 1st class and above to novice job levels
  1645. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1646. {
  1647. c = MAPID_NOVICE;
  1648. }
  1649. // limit 2nd class and above to first class job levels (super novices are exempt)
  1650. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1651. {
  1652. // regenerate change_level_2nd
  1653. if (!sd->change_level_2nd)
  1654. {
  1655. if (sd->class_&JOBL_THIRD)
  1656. {
  1657. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1658. if (!sd->change_level_3rd)
  1659. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1660. else
  1661. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1662. - (sd->status.job_level - 1)
  1663. - (sd->change_level_3rd - 1)
  1664. - novice_skills;
  1665. }
  1666. else
  1667. {
  1668. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1669. - (sd->status.job_level - 1)
  1670. - novice_skills;
  1671. }
  1672. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1673. }
  1674. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1675. {
  1676. c &= MAPID_BASEMASK;
  1677. }
  1678. // limit 3rd class to 2nd class/trans job levels
  1679. else if(sd->class_&JOBL_THIRD)
  1680. {
  1681. // regenerate change_level_3rd
  1682. if (!sd->change_level_3rd)
  1683. {
  1684. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1685. - (sd->status.job_level - 1)
  1686. - (sd->change_level_2nd - 1)
  1687. - novice_skills;
  1688. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1689. }
  1690. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1691. c &= MAPID_UPPERMASK;
  1692. }
  1693. }
  1694. // restore non-limiting flags
  1695. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1696. return c;
  1697. }
  1698. /*==========================================
  1699. * Updates the weight status
  1700. *------------------------------------------
  1701. * 1: overweight 50% for pre-renewal and 70% for renewal
  1702. * 2: overweight 90%
  1703. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1704. */
  1705. void pc_updateweightstatus(struct map_session_data *sd)
  1706. {
  1707. int old_overweight;
  1708. int new_overweight;
  1709. nullpo_retv(sd);
  1710. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1711. #ifdef RENEWAL
  1712. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1713. #else
  1714. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1715. #endif
  1716. if( old_overweight == new_overweight )
  1717. return; // no change
  1718. // stop old status change
  1719. if( old_overweight == 1 )
  1720. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1721. else if( old_overweight == 2 )
  1722. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1723. // start new status change
  1724. if( new_overweight == 1 )
  1725. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1726. else if( new_overweight == 2 )
  1727. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1728. // update overweight status
  1729. sd->regen.state.overweight = new_overweight;
  1730. }
  1731. int pc_disguise(struct map_session_data *sd, int class_)
  1732. {
  1733. if (!class_ && !sd->disguise)
  1734. return 0;
  1735. if (class_ && sd->disguise == class_)
  1736. return 0;
  1737. if(pc_isinvisible(sd))
  1738. { //Character is invisible. Stealth class-change. [Skotlex]
  1739. sd->disguise = class_; //viewdata is set on uncloaking.
  1740. return 2;
  1741. }
  1742. if (sd->bl.prev != NULL) {
  1743. pc_stop_walking(sd, 0);
  1744. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1745. }
  1746. if (!class_) {
  1747. sd->disguise = 0;
  1748. class_ = sd->status.class_;
  1749. } else
  1750. sd->disguise=class_;
  1751. status_set_viewdata(&sd->bl, class_);
  1752. clif_changeoption(&sd->bl);
  1753. if (sd->bl.prev != NULL) {
  1754. clif_spawn(&sd->bl);
  1755. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1756. { //It seems the cart info is lost on undisguise.
  1757. clif_cartlist(sd);
  1758. clif_updatestatus(sd,SP_CARTINFO);
  1759. }
  1760. if (sd->chatID) {
  1761. struct chat_data* cd;
  1762. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1763. clif_dispchat(cd,0);
  1764. }
  1765. }
  1766. return 1;
  1767. }
  1768. /// Show error message
  1769. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1770. /// Check for valid Element, break & show error message if invalid Element
  1771. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1772. /// Check for valid Race, break & show error message if invalid Race
  1773. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1774. /// Check for valid Race2, break & show error message if invalid Race2
  1775. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1776. /// Check for valid Class, break & show error message if invalid Class
  1777. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1778. /// Check for valid Size, break & show error message if invalid Size
  1779. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1780. /// Check for valid SC, break & show error message if invalid SC
  1781. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1782. static void pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, unsigned short card_id)
  1783. {
  1784. uint8 i;
  1785. if( !rate )
  1786. return;
  1787. for( i = 0; i < max && spell[i].id; i++ )
  1788. {
  1789. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1790. {
  1791. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1792. return;
  1793. rate += spell[i].rate;
  1794. break;
  1795. }
  1796. }
  1797. if (i == max) {
  1798. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", max);
  1799. return;
  1800. }
  1801. spell[i].id = id;
  1802. spell[i].lv = lv;
  1803. spell[i].rate = rate;
  1804. //Auto-update flag value.
  1805. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1806. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1807. if (!(flag&BF_SKILLMASK)) {
  1808. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1809. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1810. }
  1811. spell[i].flag|= flag;
  1812. spell[i].card_id = card_id;
  1813. }
  1814. static void pc_bonus_autospell_onskill(struct s_autospell *spell, int max, short src_skill, short id, short lv, short rate, unsigned short card_id)
  1815. {
  1816. uint8 i;
  1817. if( !rate )
  1818. return;
  1819. for( i = 0; i < max && spell[i].id; i++ )
  1820. {
  1821. ; // each autospell works independently
  1822. }
  1823. if( i == max )
  1824. {
  1825. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", max);
  1826. return;
  1827. }
  1828. spell[i].flag = src_skill;
  1829. spell[i].id = id;
  1830. spell[i].lv = lv;
  1831. spell[i].rate = rate;
  1832. spell[i].card_id = card_id;
  1833. return;
  1834. }
  1835. /**
  1836. * Add inflict effect bonus for player while attacking/atatcked
  1837. * @param effect Effect array
  1838. * @param pmax Max array
  1839. * @param sc SC/Effect type
  1840. * @param rate Success chance
  1841. * @param arrow_rate success chance if bonus comes from arrow-type item
  1842. * @param flag Target flag
  1843. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1844. **/
  1845. static void pc_bonus_addeff(struct s_addeffect* effect, int pmax, enum sc_type sc, short rate, short arrow_rate, unsigned char flag, unsigned int duration)
  1846. {
  1847. uint16 i;
  1848. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1849. flag |= ATF_SHORT|ATF_LONG; //Default range: both
  1850. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1851. flag |= ATF_TARGET; //Default target: enemy.
  1852. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1853. flag |= ATF_WEAPON; //Default type: weapon.
  1854. if (!duration)
  1855. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1856. for (i = 0; i < pmax && effect[i].flag; i++) {
  1857. if (effect[i].sc == sc && effect[i].flag == flag) {
  1858. effect[i].rate += rate;
  1859. effect[i].arrow_rate += arrow_rate;
  1860. effect[i].duration = umax(effect[i].duration, duration);
  1861. return;
  1862. }
  1863. }
  1864. if (i == pmax) {
  1865. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", pmax);
  1866. return;
  1867. }
  1868. effect[i].sc = sc;
  1869. effect[i].rate = rate;
  1870. effect[i].arrow_rate = arrow_rate;
  1871. effect[i].flag = flag;
  1872. effect[i].duration = duration;
  1873. }
  1874. /**
  1875. * Add inflict effect bonus for player while attacking using skill
  1876. * @param effect Effect array
  1877. * @param pmax Max array
  1878. * @param sc SC/Effect type
  1879. * @param rate Success chance
  1880. * @param flag Target flag
  1881. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1882. **/
  1883. static void pc_bonus_addeff_onskill(struct s_addeffectonskill* effect, int pmax, enum sc_type sc, short rate, short skill_id, unsigned char target, unsigned int duration)
  1884. {
  1885. uint8 i;
  1886. if (!duration)
  1887. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1888. for( i = 0; i < pmax && effect[i].skill_id; i++ ) {
  1889. if( effect[i].sc == sc && effect[i].skill_id == skill_id && effect[i].target == target ) {
  1890. effect[i].rate += rate;
  1891. effect[i].duration = umax(effect[i].duration, duration);
  1892. return;
  1893. }
  1894. }
  1895. if( i == pmax ) {
  1896. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects on skill per character!\n", pmax);
  1897. return;
  1898. }
  1899. effect[i].sc = sc;
  1900. effect[i].rate = rate;
  1901. effect[i].skill_id = skill_id;
  1902. effect[i].target = target;
  1903. effect[i].duration = duration;
  1904. }
  1905. /** Adjust/add drop rate modifier for player
  1906. * @param drop: Player's sd->add_drop (struct s_add_drop)
  1907. * @param max: Max bonus can be received
  1908. * @param nameid: item id that will be dropped
  1909. * @param group: group id
  1910. * @param class_: target class
  1911. * @param race: target race. if < 0, means monster_id
  1912. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  1913. */
  1914. static void pc_bonus_item_drop(struct s_add_drop *drop, const short max, unsigned short nameid, uint16 group, int class_, short race, int rate)
  1915. {
  1916. uint8 i;
  1917. if (!nameid && !group) {
  1918. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  1919. return;
  1920. }
  1921. if (nameid && !itemdb_exists(nameid)) {
  1922. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  1923. return;
  1924. }
  1925. if (group && !itemdb_group_exists(group)) {
  1926. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  1927. return;
  1928. }
  1929. //Apply config rate adjustment settings.
  1930. if (rate >= 0) { //Absolute drop.
  1931. if (battle_config.item_rate_adddrop != 100)
  1932. rate = rate*battle_config.item_rate_adddrop/100;
  1933. if (rate < battle_config.item_drop_adddrop_min)
  1934. rate = battle_config.item_drop_adddrop_min;
  1935. else if (rate > battle_config.item_drop_adddrop_max)
  1936. rate = battle_config.item_drop_adddrop_max;
  1937. } else { //Relative drop, max/min limits are applied at drop time.
  1938. if (battle_config.item_rate_adddrop != 100)
  1939. rate = rate*battle_config.item_rate_adddrop/100;
  1940. if (rate > -1)
  1941. rate = -1;
  1942. }
  1943. //Find match entry, and adjust the rate only
  1944. for (i = 0; i < max; i++) {
  1945. if (!&drop[i] || (!drop[i].nameid && !drop[i].group))
  1946. continue;
  1947. if (drop[i].nameid == nameid &&
  1948. drop[i].group == group &&
  1949. drop[i].race == race &&
  1950. drop[i].class_ == class_
  1951. )
  1952. {
  1953. //Adjust the rate if it has same classification
  1954. if ((rate < 0 && drop[i].rate < 0) ||
  1955. (rate > 0 && drop[i].rate > 0))
  1956. {
  1957. drop[i].rate += rate;
  1958. return;
  1959. }
  1960. }
  1961. }
  1962. ARR_FIND(0,max,i,!&drop[i] || (drop[i].nameid == 0 && drop[i].group == 0));
  1963. if (i >= max) {
  1964. 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,nameid,group,class_,race,rate);
  1965. return;
  1966. }
  1967. drop[i].nameid = nameid;
  1968. drop[i].group = group;
  1969. drop[i].race = race;
  1970. drop[i].class_ = class_;
  1971. drop[i].rate = rate;
  1972. }
  1973. bool pc_addautobonus(struct s_autobonus *bonus,char max,const char *script,short rate,unsigned int dur,short flag,const char *other_script,unsigned int pos,bool onskill)
  1974. {
  1975. int i;
  1976. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1977. if( i == max )
  1978. {
  1979. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1980. return false;
  1981. }
  1982. if( !onskill )
  1983. {
  1984. if( !(flag&BF_RANGEMASK) )
  1985. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1986. if( !(flag&BF_WEAPONMASK) )
  1987. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1988. if( !(flag&BF_SKILLMASK) )
  1989. {
  1990. if( flag&(BF_MAGIC|BF_MISC) )
  1991. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1992. if( flag&BF_WEAPON )
  1993. flag|=BF_NORMAL|BF_SKILL;
  1994. }
  1995. }
  1996. bonus[i].rate = rate;
  1997. bonus[i].duration = dur;
  1998. bonus[i].active = INVALID_TIMER;
  1999. bonus[i].atk_type = flag;
  2000. bonus[i].pos = pos;
  2001. bonus[i].bonus_script = aStrdup(script);
  2002. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  2003. return true;
  2004. }
  2005. void pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  2006. {
  2007. int i;
  2008. if (!sd)
  2009. return;
  2010. for( i = 0; i < max; i++ )
  2011. {
  2012. if( autobonus[i].active != INVALID_TIMER )
  2013. {
  2014. if( restore && (sd->state.autobonus&autobonus[i].pos) == autobonus[i].pos)
  2015. {
  2016. if( autobonus[i].bonus_script )
  2017. {
  2018. int j;
  2019. unsigned int equip_pos_idx = 0;
  2020. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2021. for(j = 0; j < EQI_MAX; j++) {
  2022. if(sd->equip_index[j] >= 0)
  2023. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2024. }
  2025. if((equip_pos_idx&autobonus[i].pos) == autobonus[i].pos)
  2026. script_run_autobonus(autobonus[i].bonus_script,sd,autobonus[i].pos);
  2027. }
  2028. continue;
  2029. }
  2030. else
  2031. { // Logout / Unequipped an item with an activated bonus
  2032. delete_timer(autobonus[i].active,pc_endautobonus);
  2033. autobonus[i].active = INVALID_TIMER;
  2034. }
  2035. }
  2036. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  2037. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  2038. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  2039. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  2040. autobonus[i].active = INVALID_TIMER;
  2041. }
  2042. }
  2043. void pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  2044. {
  2045. if (!sd || !autobonus)
  2046. return;
  2047. if (autobonus->active != INVALID_TIMER)
  2048. delete_timer(autobonus->active, pc_endautobonus);
  2049. if( autobonus->other_script )
  2050. {
  2051. int j;
  2052. unsigned int equip_pos_idx = 0;
  2053. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2054. for(j = 0; j < EQI_MAX; j++) {
  2055. if(sd->equip_index[j] >= 0)
  2056. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2057. }
  2058. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2059. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2060. }
  2061. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  2062. sd->state.autobonus |= autobonus->pos;
  2063. status_calc_pc(sd,SCO_FORCE);
  2064. }
  2065. TIMER_FUNC(pc_endautobonus){
  2066. struct map_session_data *sd = map_id2sd(id);
  2067. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  2068. nullpo_ret(sd);
  2069. nullpo_ret(autobonus);
  2070. autobonus->active = INVALID_TIMER;
  2071. sd->state.autobonus &= ~autobonus->pos;
  2072. status_calc_pc(sd,SCO_FORCE);
  2073. return 0;
  2074. }
  2075. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2076. {
  2077. uint8 i;
  2078. struct weapon_data* wd;
  2079. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2080. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  2081. if (i == MAX_PC_BONUS)
  2082. {
  2083. ShowWarning("pc_bonus_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2084. return;
  2085. }
  2086. if (!(flag&BF_RANGEMASK))
  2087. flag |= BF_SHORT|BF_LONG;
  2088. if (!(flag&BF_WEAPONMASK))
  2089. flag |= BF_WEAPON;
  2090. if (!(flag&BF_SKILLMASK))
  2091. {
  2092. if (flag&(BF_MAGIC|BF_MISC))
  2093. flag |= BF_SKILL;
  2094. if (flag&BF_WEAPON)
  2095. flag |= BF_NORMAL|BF_SKILL;
  2096. }
  2097. wd->addele2[i].ele = ele;
  2098. wd->addele2[i].rate = rate;
  2099. wd->addele2[i].flag = flag;
  2100. }
  2101. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2102. {
  2103. uint8 i;
  2104. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  2105. if (i == MAX_PC_BONUS)
  2106. {
  2107. ShowWarning("pc_bonus_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2108. return;
  2109. }
  2110. if (!(flag&BF_RANGEMASK))
  2111. flag |= BF_SHORT|BF_LONG;
  2112. if (!(flag&BF_WEAPONMASK))
  2113. flag |= BF_WEAPON;
  2114. if (!(flag&BF_SKILLMASK))
  2115. {
  2116. if (flag&(BF_MAGIC|BF_MISC))
  2117. flag |= BF_SKILL;
  2118. if (flag&BF_WEAPON)
  2119. flag |= BF_NORMAL|BF_SKILL;
  2120. }
  2121. sd->subele2[i].ele = ele;
  2122. sd->subele2[i].rate = rate;
  2123. sd->subele2[i].flag = flag;
  2124. }
  2125. /** Add item group heal rate bonus to player
  2126. * @param sd Player
  2127. * @param group_id Item Group ID
  2128. * @param rate
  2129. * @author Cydh
  2130. */
  2131. void pc_itemgrouphealrate(struct map_session_data *sd, uint16 group_id, short rate) {
  2132. struct s_pc_itemgrouphealrate *entry;
  2133. uint8 i;
  2134. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  2135. if (sd->itemgrouphealrate[i]->group_id == group_id)
  2136. break;
  2137. }
  2138. if (i != sd->itemgrouphealrate_count) {
  2139. sd->itemgrouphealrate[i]->rate += rate;
  2140. return;
  2141. }
  2142. if (i >= UINT8_MAX) {
  2143. ShowError("pc_itemgrouphealrate_add: Reached max (%d) possible bonuses for this player %d\n", UINT8_MAX, sd->status.char_id);
  2144. return;
  2145. }
  2146. entry = ers_alloc(pc_itemgrouphealrate_ers, struct s_pc_itemgrouphealrate);
  2147. entry->group_id = group_id;
  2148. entry->rate = rate;
  2149. RECREATE(sd->itemgrouphealrate, struct s_pc_itemgrouphealrate *, sd->itemgrouphealrate_count+1);
  2150. sd->itemgrouphealrate[sd->itemgrouphealrate_count++] = entry;
  2151. }
  2152. /** Clear item group heal rate from player
  2153. * @param sd Player
  2154. * @author Cydh
  2155. */
  2156. void pc_itemgrouphealrate_clear(struct map_session_data *sd) {
  2157. if (!sd || !sd->itemgrouphealrate_count)
  2158. return;
  2159. else {
  2160. uint8 i;
  2161. for( i = 0; i < sd->itemgrouphealrate_count; i++ )
  2162. ers_free(pc_itemgrouphealrate_ers, sd->itemgrouphealrate[i]);
  2163. sd->itemgrouphealrate_count = 0;
  2164. aFree(sd->itemgrouphealrate);
  2165. sd->itemgrouphealrate = NULL;
  2166. }
  2167. }
  2168. /*==========================================
  2169. * Add a bonus(type) to player sd
  2170. * format: bonus bBonusName,val;
  2171. * @param sd
  2172. * @param type Bonus type used by bBonusName
  2173. * @param val Value that usually for rate or fixed value
  2174. *------------------------------------------*/
  2175. void pc_bonus(struct map_session_data *sd,int type,int val)
  2176. {
  2177. struct status_data *status;
  2178. int bonus;
  2179. nullpo_retv(sd);
  2180. status = &sd->base_status;
  2181. switch(type){
  2182. case SP_STR:
  2183. case SP_AGI:
  2184. case SP_VIT:
  2185. case SP_INT:
  2186. case SP_DEX:
  2187. case SP_LUK:
  2188. if(sd->state.lr_flag != 2)
  2189. sd->param_bonus[type-SP_STR]+=val;
  2190. break;
  2191. case SP_ATK1:
  2192. if(!sd->state.lr_flag) {
  2193. bonus = status->rhw.atk + val;
  2194. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2195. }
  2196. else if(sd->state.lr_flag == 1) {
  2197. bonus = status->lhw.atk + val;
  2198. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2199. }
  2200. break;
  2201. case SP_ATK2:
  2202. if(!sd->state.lr_flag) {
  2203. bonus = status->rhw.atk2 + val;
  2204. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2205. }
  2206. else if(sd->state.lr_flag == 1) {
  2207. bonus = status->lhw.atk2 + val;
  2208. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2209. }
  2210. break;
  2211. case SP_BASE_ATK:
  2212. if(sd->state.lr_flag != 2) {
  2213. #ifdef RENEWAL
  2214. bonus = sd->bonus.eatk + val;
  2215. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2216. #else
  2217. bonus = status->batk + val;
  2218. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2219. #endif
  2220. }
  2221. break;
  2222. case SP_DEF1:
  2223. if(sd->state.lr_flag != 2) {
  2224. bonus = status->def + val;
  2225. #ifdef RENEWAL
  2226. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2227. #else
  2228. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2229. #endif
  2230. }
  2231. break;
  2232. case SP_DEF2:
  2233. if(sd->state.lr_flag != 2) {
  2234. bonus = status->def2 + val;
  2235. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2236. }
  2237. break;
  2238. case SP_MDEF1:
  2239. if(sd->state.lr_flag != 2) {
  2240. bonus = status->mdef + val;
  2241. #ifdef RENEWAL
  2242. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2243. #else
  2244. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2245. #endif
  2246. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2247. sd->bonus.shieldmdef += bonus;
  2248. }
  2249. }
  2250. break;
  2251. case SP_MDEF2:
  2252. if(sd->state.lr_flag != 2) {
  2253. bonus = status->mdef2 + val;
  2254. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2255. }
  2256. break;
  2257. case SP_HIT:
  2258. if(sd->state.lr_flag != 2) {
  2259. bonus = status->hit + val;
  2260. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2261. } else
  2262. sd->bonus.arrow_hit+=val;
  2263. break;
  2264. case SP_FLEE1:
  2265. if(sd->state.lr_flag != 2) {
  2266. bonus = status->flee + val;
  2267. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2268. }
  2269. break;
  2270. case SP_FLEE2:
  2271. if(sd->state.lr_flag != 2) {
  2272. bonus = status->flee2 + val*10;
  2273. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2274. }
  2275. break;
  2276. case SP_CRITICAL:
  2277. if(sd->state.lr_flag != 2) {
  2278. bonus = status->cri + val*10;
  2279. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2280. } else
  2281. sd->bonus.arrow_cri += val*10;
  2282. break;
  2283. case SP_ATKELE:
  2284. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2285. switch (sd->state.lr_flag)
  2286. {
  2287. case 2:
  2288. switch (sd->status.weapon) {
  2289. case W_BOW:
  2290. case W_REVOLVER:
  2291. case W_RIFLE:
  2292. case W_GATLING:
  2293. case W_SHOTGUN:
  2294. case W_GRENADE:
  2295. //Become weapon element.
  2296. status->rhw.ele=val;
  2297. break;
  2298. default: //Become arrow element.
  2299. sd->bonus.arrow_ele=val;
  2300. break;
  2301. }
  2302. break;
  2303. case 1:
  2304. status->lhw.ele=val;
  2305. break;
  2306. default:
  2307. status->rhw.ele=val;
  2308. break;
  2309. }
  2310. break;
  2311. case SP_DEFELE:
  2312. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2313. if(sd->state.lr_flag != 2)
  2314. status->def_ele=val;
  2315. break;
  2316. case SP_MAXHP:
  2317. if(sd->state.lr_flag == 2)
  2318. break;
  2319. sd->bonus.hp += val;
  2320. break;
  2321. case SP_MAXSP:
  2322. if(sd->state.lr_flag == 2)
  2323. break;
  2324. sd->bonus.sp += val;
  2325. break;
  2326. case SP_MAXHPRATE:
  2327. if(sd->state.lr_flag != 2)
  2328. sd->hprate+=val;
  2329. break;
  2330. case SP_MAXSPRATE:
  2331. if(sd->state.lr_flag != 2)
  2332. sd->sprate+=val;
  2333. break;
  2334. case SP_SPRATE:
  2335. if(sd->state.lr_flag != 2)
  2336. sd->dsprate+=val;
  2337. break;
  2338. case SP_ATTACKRANGE:
  2339. switch (sd->state.lr_flag) {
  2340. case 2:
  2341. switch (sd->status.weapon) {
  2342. case W_BOW:
  2343. case W_REVOLVER:
  2344. case W_RIFLE:
  2345. case W_GATLING:
  2346. case W_SHOTGUN:
  2347. case W_GRENADE:
  2348. status->rhw.range += val;
  2349. }
  2350. break;
  2351. case 1:
  2352. status->lhw.range += val;
  2353. break;
  2354. default:
  2355. status->rhw.range += val;
  2356. break;
  2357. }
  2358. break;
  2359. case SP_SPEED_RATE: //Non stackable increase
  2360. if(sd->state.lr_flag != 2)
  2361. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2362. break;
  2363. case SP_SPEED_ADDRATE: //Stackable increase
  2364. if(sd->state.lr_flag != 2)
  2365. sd->bonus.speed_add_rate -= val;
  2366. break;
  2367. case SP_ASPD: //Raw increase
  2368. if(sd->state.lr_flag != 2)
  2369. sd->bonus.aspd_add -= 10*val;
  2370. break;
  2371. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2372. if(sd->state.lr_flag != 2)
  2373. #ifndef RENEWAL_ASPD
  2374. status->aspd_rate -= 10*val;
  2375. #else
  2376. status->aspd_rate2 += val;
  2377. #endif
  2378. break;
  2379. case SP_HP_RECOV_RATE:
  2380. if(sd->state.lr_flag != 2)
  2381. sd->hprecov_rate += val;
  2382. break;
  2383. case SP_SP_RECOV_RATE:
  2384. if(sd->state.lr_flag != 2)
  2385. sd->sprecov_rate += val;
  2386. break;
  2387. case SP_CRITICAL_DEF:
  2388. if(sd->state.lr_flag != 2)
  2389. sd->bonus.critical_def += val;
  2390. break;
  2391. case SP_NEAR_ATK_DEF:
  2392. if(sd->state.lr_flag != 2)
  2393. sd->bonus.near_attack_def_rate += val;
  2394. break;
  2395. case SP_LONG_ATK_DEF:
  2396. if(sd->state.lr_flag != 2)
  2397. sd->bonus.long_attack_def_rate += val;
  2398. break;
  2399. case SP_DOUBLE_RATE:
  2400. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2401. sd->bonus.double_rate = val;
  2402. break;
  2403. case SP_DOUBLE_ADD_RATE:
  2404. if(sd->state.lr_flag == 0)
  2405. sd->bonus.double_add_rate += val;
  2406. break;
  2407. case SP_MATK_RATE:
  2408. if(sd->state.lr_flag != 2)
  2409. sd->matk_rate += val;
  2410. break;
  2411. case SP_IGNORE_DEF_ELE:
  2412. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2413. if(!sd->state.lr_flag)
  2414. sd->right_weapon.ignore_def_ele |= 1<<val;
  2415. else if(sd->state.lr_flag == 1)
  2416. sd->left_weapon.ignore_def_ele |= 1<<val;
  2417. break;
  2418. case SP_IGNORE_DEF_RACE:
  2419. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2420. if(!sd->state.lr_flag)
  2421. sd->right_weapon.ignore_def_race |= 1<<val;
  2422. else if(sd->state.lr_flag == 1)
  2423. sd->left_weapon.ignore_def_race |= 1<<val;
  2424. break;
  2425. case SP_IGNORE_DEF_CLASS:
  2426. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2427. if(!sd->state.lr_flag)
  2428. sd->right_weapon.ignore_def_class |= 1<<val;
  2429. else if(sd->state.lr_flag == 1)
  2430. sd->left_weapon.ignore_def_class |= 1<<val;
  2431. break;
  2432. case SP_ATK_RATE:
  2433. if(sd->state.lr_flag != 2)
  2434. sd->bonus.atk_rate += val;
  2435. break;
  2436. case SP_MAGIC_ATK_DEF:
  2437. if(sd->state.lr_flag != 2)
  2438. sd->bonus.magic_def_rate += val;
  2439. break;
  2440. case SP_MISC_ATK_DEF:
  2441. if(sd->state.lr_flag != 2)
  2442. sd->bonus.misc_def_rate += val;
  2443. break;
  2444. case SP_IGNORE_MDEF_ELE:
  2445. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2446. if(sd->state.lr_flag != 2)
  2447. sd->bonus.ignore_mdef_ele |= 1<<val;
  2448. break;
  2449. case SP_IGNORE_MDEF_RACE:
  2450. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2451. if(sd->state.lr_flag != 2)
  2452. sd->bonus.ignore_mdef_race |= 1<<val;
  2453. break;
  2454. case SP_PERFECT_HIT_RATE:
  2455. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2456. sd->bonus.perfect_hit = val;
  2457. break;
  2458. case SP_PERFECT_HIT_ADD_RATE:
  2459. if(sd->state.lr_flag != 2)
  2460. sd->bonus.perfect_hit_add += val;
  2461. break;
  2462. case SP_CRITICAL_RATE:
  2463. if(sd->state.lr_flag != 2)
  2464. sd->critical_rate+=val;
  2465. break;
  2466. case SP_DEF_RATIO_ATK_ELE:
  2467. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2468. if(!sd->state.lr_flag)
  2469. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2470. else if(sd->state.lr_flag == 1)
  2471. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2472. break;
  2473. case SP_DEF_RATIO_ATK_RACE:
  2474. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2475. if(!sd->state.lr_flag)
  2476. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2477. else if(sd->state.lr_flag == 1)
  2478. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2479. break;
  2480. case SP_DEF_RATIO_ATK_CLASS:
  2481. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2482. if(!sd->state.lr_flag)
  2483. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2484. else if(sd->state.lr_flag == 1)
  2485. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2486. break;
  2487. case SP_HIT_RATE:
  2488. if(sd->state.lr_flag != 2)
  2489. sd->hit_rate += val;
  2490. break;
  2491. case SP_FLEE_RATE:
  2492. if(sd->state.lr_flag != 2)
  2493. sd->flee_rate += val;
  2494. break;
  2495. case SP_FLEE2_RATE:
  2496. if(sd->state.lr_flag != 2)
  2497. sd->flee2_rate += val;
  2498. break;
  2499. case SP_DEF_RATE:
  2500. if(sd->state.lr_flag != 2)
  2501. sd->def_rate += val;
  2502. break;
  2503. case SP_DEF2_RATE:
  2504. if(sd->state.lr_flag != 2)
  2505. sd->def2_rate += val;
  2506. break;
  2507. case SP_MDEF_RATE:
  2508. if(sd->state.lr_flag != 2)
  2509. sd->mdef_rate += val;
  2510. break;
  2511. case SP_MDEF2_RATE:
  2512. if(sd->state.lr_flag != 2)
  2513. sd->mdef2_rate += val;
  2514. break;
  2515. case SP_RESTART_FULL_RECOVER:
  2516. if(sd->state.lr_flag != 2)
  2517. sd->special_state.restart_full_recover = 1;
  2518. break;
  2519. case SP_NO_CASTCANCEL:
  2520. if(sd->state.lr_flag != 2)
  2521. sd->special_state.no_castcancel = 1;
  2522. break;
  2523. case SP_NO_CASTCANCEL2:
  2524. if(sd->state.lr_flag != 2)
  2525. sd->special_state.no_castcancel2 = 1;
  2526. break;
  2527. case SP_NO_SIZEFIX:
  2528. if(sd->state.lr_flag != 2)
  2529. sd->special_state.no_sizefix = 1;
  2530. break;
  2531. case SP_NO_MAGIC_DAMAGE:
  2532. if(sd->state.lr_flag == 2)
  2533. break;
  2534. val+= sd->special_state.no_magic_damage;
  2535. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2536. break;
  2537. case SP_NO_WEAPON_DAMAGE:
  2538. if(sd->state.lr_flag == 2)
  2539. break;
  2540. val+= sd->special_state.no_weapon_damage;
  2541. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2542. break;
  2543. case SP_NO_MISC_DAMAGE:
  2544. if(sd->state.lr_flag == 2)
  2545. break;
  2546. val+= sd->special_state.no_misc_damage;
  2547. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2548. break;
  2549. case SP_NO_GEMSTONE:
  2550. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2551. sd->special_state.no_gemstone = 1;
  2552. break;
  2553. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2554. if(sd->state.lr_flag != 2) {
  2555. sd->special_state.intravision = 1;
  2556. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2557. }
  2558. break;
  2559. case SP_NO_KNOCKBACK:
  2560. if(sd->state.lr_flag != 2)
  2561. sd->special_state.no_knockback = 1;
  2562. break;
  2563. case SP_SPLASH_RANGE:
  2564. if(sd->bonus.splash_range < val)
  2565. sd->bonus.splash_range = val;
  2566. break;
  2567. case SP_SPLASH_ADD_RANGE:
  2568. sd->bonus.splash_add_range += val;
  2569. break;
  2570. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2571. if(sd->state.lr_flag != 2)
  2572. sd->bonus.short_weapon_damage_return += val;
  2573. break;
  2574. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2575. if(sd->state.lr_flag != 2)
  2576. sd->bonus.long_weapon_damage_return += val;
  2577. break;
  2578. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2579. if(sd->state.lr_flag != 2)
  2580. sd->bonus.magic_damage_return += val;
  2581. break;
  2582. case SP_ALL_STATS: // [Valaris]
  2583. if(sd->state.lr_flag!=2) {
  2584. sd->param_bonus[SP_STR-SP_STR]+=val;
  2585. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2586. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2587. sd->param_bonus[SP_INT-SP_STR]+=val;
  2588. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2589. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2590. }
  2591. break;
  2592. case SP_AGI_VIT: // [Valaris]
  2593. if(sd->state.lr_flag!=2) {
  2594. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2595. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2596. }
  2597. break;
  2598. case SP_AGI_DEX_STR: // [Valaris]
  2599. if(sd->state.lr_flag!=2) {
  2600. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2601. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2602. sd->param_bonus[SP_STR-SP_STR]+=val;
  2603. }
  2604. break;
  2605. case SP_PERFECT_HIDE: // [Valaris]
  2606. if(sd->state.lr_flag!=2)
  2607. sd->special_state.perfect_hiding=1;
  2608. break;
  2609. case SP_UNBREAKABLE:
  2610. if(sd->state.lr_flag!=2)
  2611. sd->bonus.unbreakable += val;
  2612. break;
  2613. case SP_UNBREAKABLE_WEAPON:
  2614. if(sd->state.lr_flag != 2)
  2615. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2616. break;
  2617. case SP_UNBREAKABLE_ARMOR:
  2618. if(sd->state.lr_flag != 2)
  2619. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2620. break;
  2621. case SP_UNBREAKABLE_HELM:
  2622. if(sd->state.lr_flag != 2)
  2623. sd->bonus.unbreakable_equip |= EQP_HELM;
  2624. break;
  2625. case SP_UNBREAKABLE_SHIELD:
  2626. if(sd->state.lr_flag != 2)
  2627. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2628. break;
  2629. case SP_UNBREAKABLE_GARMENT:
  2630. if(sd->state.lr_flag != 2)
  2631. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2632. break;
  2633. case SP_UNBREAKABLE_SHOES:
  2634. if(sd->state.lr_flag != 2)
  2635. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2636. break;
  2637. case SP_CLASSCHANGE: // [Valaris]
  2638. if(sd->state.lr_flag !=2)
  2639. sd->bonus.classchange=val;
  2640. break;
  2641. case SP_LONG_ATK_RATE:
  2642. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2643. sd->bonus.long_attack_atk_rate+=val;
  2644. break;
  2645. case SP_BREAK_WEAPON_RATE:
  2646. if(sd->state.lr_flag != 2)
  2647. sd->bonus.break_weapon_rate+=val;
  2648. break;
  2649. case SP_BREAK_ARMOR_RATE:
  2650. if(sd->state.lr_flag != 2)
  2651. sd->bonus.break_armor_rate+=val;
  2652. break;
  2653. case SP_ADD_STEAL_RATE:
  2654. if(sd->state.lr_flag != 2)
  2655. sd->bonus.add_steal_rate+=val;
  2656. break;
  2657. case SP_DELAYRATE:
  2658. if(sd->state.lr_flag != 2)
  2659. sd->delayrate+=val;
  2660. break;
  2661. case SP_CRIT_ATK_RATE:
  2662. if(sd->state.lr_flag != 2)
  2663. sd->bonus.crit_atk_rate += val;
  2664. break;
  2665. case SP_NO_REGEN:
  2666. if(sd->state.lr_flag != 2)
  2667. sd->regen.state.block|=val;
  2668. break;
  2669. case SP_UNSTRIPABLE_WEAPON:
  2670. if(sd->state.lr_flag != 2)
  2671. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2672. break;
  2673. case SP_UNSTRIPABLE:
  2674. case SP_UNSTRIPABLE_ARMOR:
  2675. if(sd->state.lr_flag != 2)
  2676. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2677. break;
  2678. case SP_UNSTRIPABLE_HELM:
  2679. if(sd->state.lr_flag != 2)
  2680. sd->bonus.unstripable_equip |= EQP_HELM;
  2681. break;
  2682. case SP_UNSTRIPABLE_SHIELD:
  2683. if(sd->state.lr_flag != 2)
  2684. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2685. break;
  2686. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2687. if(!sd->state.lr_flag) {
  2688. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2689. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2690. } else if(sd->state.lr_flag == 1) {
  2691. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2692. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2693. }
  2694. break;
  2695. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2696. if(!sd->state.lr_flag) {
  2697. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2698. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2699. } else if(sd->state.lr_flag == 1) {
  2700. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2701. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2702. }
  2703. break;
  2704. case SP_SP_GAIN_VALUE:
  2705. if(!sd->state.lr_flag)
  2706. sd->bonus.sp_gain_value += val;
  2707. break;
  2708. case SP_HP_GAIN_VALUE:
  2709. if(!sd->state.lr_flag)
  2710. sd->bonus.hp_gain_value += val;
  2711. break;
  2712. case SP_MAGIC_SP_GAIN_VALUE:
  2713. if(!sd->state.lr_flag)
  2714. sd->bonus.magic_sp_gain_value += val;
  2715. break;
  2716. case SP_MAGIC_HP_GAIN_VALUE:
  2717. if(!sd->state.lr_flag)
  2718. sd->bonus.magic_hp_gain_value += val;
  2719. break;
  2720. case SP_ADD_HEAL_RATE:
  2721. if(sd->state.lr_flag != 2)
  2722. sd->bonus.add_heal_rate += val;
  2723. break;
  2724. case SP_ADD_HEAL2_RATE:
  2725. if(sd->state.lr_flag != 2)
  2726. sd->bonus.add_heal2_rate += val;
  2727. break;
  2728. case SP_ADD_ITEM_HEAL_RATE:
  2729. if(sd->state.lr_flag != 2)
  2730. sd->bonus.itemhealrate2 += val;
  2731. break;
  2732. case SP_EMATK:
  2733. if(sd->state.lr_flag != 2)
  2734. sd->bonus.ematk += val;
  2735. break;
  2736. #ifdef RENEWAL_CAST
  2737. case SP_FIXCASTRATE:
  2738. if(sd->state.lr_flag != 2)
  2739. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2740. break;
  2741. case SP_ADD_FIXEDCAST:
  2742. if(sd->state.lr_flag != 2)
  2743. sd->bonus.add_fixcast += val;
  2744. break;
  2745. case SP_CASTRATE:
  2746. case SP_VARCASTRATE:
  2747. if(sd->state.lr_flag != 2)
  2748. sd->bonus.varcastrate -= val;
  2749. break;
  2750. case SP_ADD_VARIABLECAST:
  2751. if(sd->state.lr_flag != 2)
  2752. sd->bonus.add_varcast += val;
  2753. break;
  2754. #else
  2755. case SP_ADD_FIXEDCAST:
  2756. case SP_FIXCASTRATE:
  2757. case SP_ADD_VARIABLECAST:
  2758. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2759. break;
  2760. case SP_VARCASTRATE:
  2761. case SP_CASTRATE:
  2762. if(sd->state.lr_flag != 2)
  2763. sd->castrate += val;
  2764. break;
  2765. #endif
  2766. case SP_ADDMAXWEIGHT:
  2767. if (sd->state.lr_flag != 2)
  2768. sd->add_max_weight += val;
  2769. break;
  2770. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  2771. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  2772. break;
  2773. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  2774. if (sd->state.lr_flag != 2)
  2775. sd->bonus.critical_rangeatk += val*10;
  2776. else
  2777. sd->bonus.arrow_cri += val*10;
  2778. break;
  2779. case SP_WEAPON_ATK_RATE:
  2780. if (sd->state.lr_flag != 2)
  2781. sd->bonus.weapon_atk_rate += val;
  2782. break;
  2783. case SP_WEAPON_MATK_RATE:
  2784. if (sd->state.lr_flag != 2)
  2785. sd->bonus.weapon_matk_rate += val;
  2786. break;
  2787. case SP_NO_MADO_FUEL:
  2788. if (sd->state.lr_flag != 2)
  2789. sd->special_state.no_mado_fuel = 1;
  2790. break;
  2791. default:
  2792. if (running_npc_stat_calc_event) {
  2793. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  2794. }
  2795. else if (current_equip_combo_pos > 0) {
  2796. 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);
  2797. }
  2798. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  2799. 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);
  2800. }
  2801. else {
  2802. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  2803. }
  2804. break;
  2805. }
  2806. }
  2807. /*==========================================
  2808. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2809. * format: bonus2 bBonusName,type2,val;
  2810. * @param sd
  2811. * @param type Bonus type used by bBonusName
  2812. * @param type2
  2813. * @param val Value that usually for rate or fixed value
  2814. *------------------------------------------*/
  2815. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2816. {
  2817. int i;
  2818. nullpo_retv(sd);
  2819. switch(type){
  2820. case SP_ADDELE: // bonus2 bAddEle,e,x;
  2821. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  2822. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  2823. sd->right_weapon.addele[type2]+=val;
  2824. else if(sd->state.lr_flag == 1)
  2825. sd->left_weapon.addele[type2]+=val;
  2826. else if(sd->state.lr_flag == 2)
  2827. sd->arrow_addele[type2]+=val;
  2828. break;
  2829. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  2830. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  2831. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  2832. sd->right_weapon.addrace[type2]+=val;
  2833. else if(sd->state.lr_flag == 1)
  2834. sd->left_weapon.addrace[type2]+=val;
  2835. else if(sd->state.lr_flag == 2)
  2836. sd->arrow_addrace[type2]+=val;
  2837. break;
  2838. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  2839. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  2840. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  2841. sd->right_weapon.addclass[type2]+=val;
  2842. else if(sd->state.lr_flag == 1)
  2843. sd->left_weapon.addclass[type2]+=val;
  2844. else if(sd->state.lr_flag == 2)
  2845. sd->arrow_addclass[type2]+=val;
  2846. break;
  2847. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  2848. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  2849. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  2850. sd->right_weapon.addsize[type2]+=val;
  2851. else if(sd->state.lr_flag == 1)
  2852. sd->left_weapon.addsize[type2]+=val;
  2853. else if(sd->state.lr_flag == 2)
  2854. sd->arrow_addsize[type2]+=val;
  2855. break;
  2856. case SP_SUBELE: // bonus2 bSubEle,e,x;
  2857. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  2858. if(sd->state.lr_flag != 2)
  2859. sd->subele_script[type2] += val;
  2860. break;
  2861. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  2862. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  2863. if(sd->state.lr_flag != 2)
  2864. sd->subrace[type2]+=val;
  2865. break;
  2866. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  2867. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  2868. if(sd->state.lr_flag != 2)
  2869. sd->subclass[type2]+=val;
  2870. break;
  2871. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  2872. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  2873. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2874. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  2875. break;
  2876. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  2877. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  2878. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2879. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  2880. break;
  2881. case SP_RESEFF: // bonus2 bResEff,eff,n;
  2882. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2883. ShowError("pc_bonus2: SP_RESEFF: %d is invalid effect.\n", type2);
  2884. break;
  2885. }
  2886. if(sd->state.lr_flag == 2)
  2887. break;
  2888. i = sd->reseff[type2]+val;
  2889. sd->reseff[type2]= cap_value(i, -10000, 10000);
  2890. break;
  2891. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  2892. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  2893. if(sd->state.lr_flag != 2)
  2894. sd->magic_addele_script[type2] += val;
  2895. break;
  2896. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  2897. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  2898. if(sd->state.lr_flag != 2)
  2899. sd->magic_addrace[type2]+=val;
  2900. break;
  2901. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  2902. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  2903. if(sd->state.lr_flag != 2)
  2904. sd->magic_addclass[type2]+=val;
  2905. break;
  2906. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  2907. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  2908. if(sd->state.lr_flag != 2)
  2909. sd->magic_addsize[type2]+=val;
  2910. break;
  2911. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  2912. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  2913. if(sd->state.lr_flag != 2)
  2914. sd->magic_atk_ele[type2]+=val;
  2915. break;
  2916. case SP_ADD_DAMAGE_CLASS: // bonus2 bAddDamageClass,mid,x;
  2917. switch (sd->state.lr_flag) {
  2918. case 0: //Right hand
  2919. ARR_FIND(0, ARRAYLENGTH(sd->right_weapon.add_dmg), i, sd->right_weapon.add_dmg[i].rate == 0 || sd->right_weapon.add_dmg[i].class_ == type2);
  2920. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2921. {
  2922. ShowError("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->right_weapon.add_dmg));
  2923. break;
  2924. }
  2925. sd->right_weapon.add_dmg[i].class_ = type2;
  2926. sd->right_weapon.add_dmg[i].rate += val;
  2927. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2928. memmove(&sd->right_weapon.add_dmg[i], &sd->right_weapon.add_dmg[i+1], sizeof(sd->right_weapon.add_dmg) - (i+1)*sizeof(sd->right_weapon.add_dmg[0]));
  2929. break;
  2930. case 1: //Left hand
  2931. ARR_FIND(0, ARRAYLENGTH(sd->left_weapon.add_dmg), i, sd->left_weapon.add_dmg[i].rate == 0 || sd->left_weapon.add_dmg[i].class_ == type2);
  2932. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2933. {
  2934. ShowError("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->left_weapon.add_dmg));
  2935. break;
  2936. }
  2937. sd->left_weapon.add_dmg[i].class_ = type2;
  2938. sd->left_weapon.add_dmg[i].rate += val;
  2939. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2940. memmove(&sd->left_weapon.add_dmg[i], &sd->left_weapon.add_dmg[i+1], sizeof(sd->left_weapon.add_dmg) - (i+1)*sizeof(sd->left_weapon.add_dmg[0]));
  2941. break;
  2942. }
  2943. break;
  2944. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  2945. if(sd->state.lr_flag == 2)
  2946. break;
  2947. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2948. if (i == ARRAYLENGTH(sd->add_mdmg))
  2949. {
  2950. ShowError("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add Class magic dmg bonuses per character!\n", ARRAYLENGTH(sd->add_mdmg));
  2951. break;
  2952. }
  2953. sd->add_mdmg[i].class_ = type2;
  2954. sd->add_mdmg[i].rate += val;
  2955. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2956. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2957. break;
  2958. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  2959. if(sd->state.lr_flag == 2)
  2960. break;
  2961. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2962. if (i == ARRAYLENGTH(sd->add_def))
  2963. {
  2964. ShowError("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2965. break;
  2966. }
  2967. sd->add_def[i].class_ = type2;
  2968. sd->add_def[i].rate += val;
  2969. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2970. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2971. break;
  2972. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  2973. if(sd->state.lr_flag == 2)
  2974. break;
  2975. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2976. if (i == ARRAYLENGTH(sd->add_mdef))
  2977. {
  2978. ShowError("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2979. break;
  2980. }
  2981. sd->add_mdef[i].class_ = type2;
  2982. sd->add_mdef[i].rate += val;
  2983. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  2984. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  2985. break;
  2986. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  2987. if(!sd->state.lr_flag) {
  2988. sd->right_weapon.hp_drain_rate.rate += type2;
  2989. sd->right_weapon.hp_drain_rate.per += val;
  2990. }
  2991. else if(sd->state.lr_flag == 1) {
  2992. sd->left_weapon.hp_drain_rate.rate += type2;
  2993. sd->left_weapon.hp_drain_rate.per += val;
  2994. }
  2995. break;
  2996. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  2997. if(!sd->state.lr_flag) {
  2998. sd->right_weapon.sp_drain_rate.rate += type2;
  2999. sd->right_weapon.sp_drain_rate.per += val;
  3000. }
  3001. else if(sd->state.lr_flag == 1) {
  3002. sd->left_weapon.sp_drain_rate.rate += type2;
  3003. sd->left_weapon.sp_drain_rate.per += val;
  3004. }
  3005. break;
  3006. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3007. if(sd->state.lr_flag != 2) {
  3008. sd->bonus.sp_vanish_rate += type2;
  3009. sd->bonus.sp_vanish_per += val;
  3010. }
  3011. break;
  3012. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3013. if(sd->state.lr_flag != 2) {
  3014. sd->bonus.hp_vanish_rate += type2;
  3015. sd->bonus.hp_vanish_per += val;
  3016. }
  3017. break;
  3018. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3019. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3020. sd->bonus.get_zeny_rate = val;
  3021. sd->bonus.get_zeny_num = type2;
  3022. }
  3023. break;
  3024. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3025. if(sd->state.lr_flag != 2) {
  3026. sd->bonus.get_zeny_rate += val;
  3027. sd->bonus.get_zeny_num += type2;
  3028. }
  3029. break;
  3030. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3031. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3032. if(sd->state.lr_flag == 2)
  3033. break;
  3034. sd->weapon_coma_ele[type2] += val;
  3035. sd->special_state.bonus_coma = 1;
  3036. break;
  3037. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3038. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3039. if(sd->state.lr_flag == 2)
  3040. break;
  3041. sd->weapon_coma_race[type2] += val;
  3042. sd->special_state.bonus_coma = 1;
  3043. break;
  3044. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3045. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3046. if(sd->state.lr_flag == 2)
  3047. break;
  3048. sd->weapon_coma_class[type2] += val;
  3049. sd->special_state.bonus_coma = 1;
  3050. break;
  3051. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3052. if(sd->state.lr_flag != 2)
  3053. sd->weapon_atk[type2]+=val;
  3054. break;
  3055. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3056. if(sd->state.lr_flag != 2)
  3057. sd->weapon_damage_rate[type2]+=val;
  3058. break;
  3059. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3060. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3061. if(sd->state.lr_flag != 2)
  3062. sd->critaddrace[type2] += val*10;
  3063. break;
  3064. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3065. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3066. if(sd->state.lr_flag != 2)
  3067. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, val, 0, 0, 0);
  3068. break;
  3069. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3070. if(sd->state.lr_flag == 2)
  3071. break;
  3072. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  3073. if (i == ARRAYLENGTH(sd->skillatk))
  3074. { //Better mention this so the array length can be updated. [Skotlex]
  3075. ShowError("pc_bonus2: SP_SKILL_ATK: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillatk), type2, val);
  3076. break;
  3077. }
  3078. if (sd->skillatk[i].id == type2)
  3079. sd->skillatk[i].val += val;
  3080. else {
  3081. sd->skillatk[i].id = type2;
  3082. sd->skillatk[i].val = val;
  3083. }
  3084. break;
  3085. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3086. if(sd->state.lr_flag == 2)
  3087. break;
  3088. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  3089. if (i == ARRAYLENGTH(sd->skillheal))
  3090. { // Better mention this so the array length can be updated. [Skotlex]
  3091. ShowError("pc_bonus2: SP_SKILL_HEAL: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal), type2, val);
  3092. break;
  3093. }
  3094. if (sd->skillheal[i].id == type2)
  3095. sd->skillheal[i].val += val;
  3096. else {
  3097. sd->skillheal[i].id = type2;
  3098. sd->skillheal[i].val = val;
  3099. }
  3100. break;
  3101. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3102. if(sd->state.lr_flag == 2)
  3103. break;
  3104. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  3105. if (i == ARRAYLENGTH(sd->skillheal2))
  3106. { // Better mention this so the array length can be updated. [Skotlex]
  3107. ShowError("pc_bonus2: SP_SKILL_HEAL2: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal2), type2, val);
  3108. break;
  3109. }
  3110. if (sd->skillheal2[i].id == type2)
  3111. sd->skillheal2[i].val += val;
  3112. else {
  3113. sd->skillheal2[i].id = type2;
  3114. sd->skillheal2[i].val = val;
  3115. }
  3116. break;
  3117. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3118. if(sd->state.lr_flag == 2)
  3119. break;
  3120. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  3121. if (i == ARRAYLENGTH(sd->skillblown))
  3122. { //Better mention this so the array length can be updated. [Skotlex]
  3123. ShowError("pc_bonus2: SP_ADD_SKILL_BLOW: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillblown), type2, val);
  3124. break;
  3125. }
  3126. if(sd->skillblown[i].id == type2)
  3127. sd->skillblown[i].val += val;
  3128. else {
  3129. sd->skillblown[i].id = type2;
  3130. sd->skillblown[i].val = val;
  3131. }
  3132. break;
  3133. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3134. if(sd->state.lr_flag != 2) {
  3135. sd->hp_loss.value = type2;
  3136. sd->hp_loss.rate = val;
  3137. }
  3138. break;
  3139. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3140. if(sd->state.lr_flag != 2) {
  3141. sd->hp_regen.value = type2;
  3142. sd->hp_regen.rate = val;
  3143. }
  3144. break;
  3145. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3146. if (sd->state.lr_flag != 2) {
  3147. sd->percent_hp_regen.value = type2;
  3148. sd->percent_hp_regen.rate = val;
  3149. }
  3150. break;
  3151. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3152. if (sd->state.lr_flag != 2) {
  3153. sd->percent_sp_regen.value = type2;
  3154. sd->percent_sp_regen.rate = val;
  3155. }
  3156. break;
  3157. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3158. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3159. if(sd->state.lr_flag != 2)
  3160. sd->right_weapon.addrace2[type2] += val;
  3161. else
  3162. sd->left_weapon.addrace2[type2] += val;
  3163. break;
  3164. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3165. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3166. if(sd->state.lr_flag != 2)
  3167. sd->subsize[type2]+=val;
  3168. break;
  3169. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3170. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3171. if(sd->state.lr_flag != 2)
  3172. sd->subrace2[type2]+=val;
  3173. break;
  3174. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3175. if(sd->state.lr_flag == 2)
  3176. break;
  3177. if (!itemdb_exists(type2)) {
  3178. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3179. break;
  3180. }
  3181. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  3182. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  3183. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  3184. break;
  3185. }
  3186. sd->itemhealrate[i].nameid = type2;
  3187. sd->itemhealrate[i].rate += val;
  3188. break;
  3189. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3190. if (sd->state.lr_flag == 2)
  3191. break;
  3192. if (!type2 || !itemdb_group_exists(type2)) {
  3193. ShowError("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3194. break;
  3195. }
  3196. pc_itemgrouphealrate(sd, type2, val);
  3197. break;
  3198. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3199. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3200. if(sd->state.lr_flag != 2)
  3201. sd->expaddrace[type2]+=val;
  3202. break;
  3203. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3204. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3205. if(sd->state.lr_flag != 2)
  3206. sd->expaddclass[type2]+=val;
  3207. break;
  3208. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3209. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3210. if(sd->state.lr_flag != 2)
  3211. sd->sp_gain_race[type2]+=val;
  3212. break;
  3213. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3214. if (sd->state.lr_flag != 2)
  3215. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_ALL, RC_NONE_, val);
  3216. break;
  3217. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3218. if (sd->state.lr_flag != 2)
  3219. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_ALL, RC_NONE_, val);
  3220. break;
  3221. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3222. if(sd->state.lr_flag != 2) {
  3223. sd->sp_loss.value = type2;
  3224. sd->sp_loss.rate = val;
  3225. }
  3226. break;
  3227. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3228. if(sd->state.lr_flag != 2) {
  3229. sd->sp_regen.value = type2;
  3230. sd->sp_regen.rate = val;
  3231. }
  3232. break;
  3233. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3234. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3235. if(!sd->state.lr_flag) {
  3236. sd->right_weapon.hp_drain_race[type2] += val;
  3237. }
  3238. else if(sd->state.lr_flag == 1) {
  3239. sd->left_weapon.hp_drain_race[type2] += val;
  3240. }
  3241. break;
  3242. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3243. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3244. if(!sd->state.lr_flag) {
  3245. sd->right_weapon.sp_drain_race[type2] += val;
  3246. }
  3247. else if(sd->state.lr_flag == 1) {
  3248. sd->left_weapon.sp_drain_race[type2] += val;
  3249. }
  3250. break;
  3251. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3252. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3253. if(!sd->state.lr_flag) {
  3254. sd->right_weapon.hp_drain_class[type2] += val;
  3255. }
  3256. else if(sd->state.lr_flag == 1) {
  3257. sd->left_weapon.hp_drain_class[type2] += val;
  3258. }
  3259. break;
  3260. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3261. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3262. if(!sd->state.lr_flag) {
  3263. sd->right_weapon.sp_drain_class[type2] += val;
  3264. }
  3265. else if(sd->state.lr_flag == 1) {
  3266. sd->left_weapon.sp_drain_class[type2] += val;
  3267. }
  3268. break;
  3269. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3270. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3271. if(sd->state.lr_flag != 2)
  3272. sd->ignore_mdef_by_race[type2] += val;
  3273. break;
  3274. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3275. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3276. if(sd->state.lr_flag != 2)
  3277. sd->ignore_mdef_by_class[type2] += val;
  3278. break;
  3279. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3280. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3281. if(sd->state.lr_flag != 2)
  3282. sd->ignore_def_by_race[type2] += val;
  3283. break;
  3284. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3285. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3286. if (sd->state.lr_flag != 2)
  3287. sd->ignore_def_by_class[type2] += val;
  3288. break;
  3289. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3290. if(sd->state.lr_flag == 2)
  3291. break;
  3292. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  3293. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  3294. ShowError("pc_bonus2: SP_SKILL_USE_SP_RATE: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesprate), type2, val);
  3295. break;
  3296. }
  3297. if (sd->skillusesprate[i].id == type2)
  3298. sd->skillusesprate[i].val += val;
  3299. else {
  3300. sd->skillusesprate[i].id = type2;
  3301. sd->skillusesprate[i].val = val;
  3302. }
  3303. break;
  3304. case SP_SKILL_DELAY:
  3305. if(sd->state.lr_flag == 2)
  3306. break;
  3307. ARR_FIND(0, ARRAYLENGTH(sd->skilldelay), i, sd->skilldelay[i].id == 0 || sd->skilldelay[i].id == type2);
  3308. if (i == ARRAYLENGTH(sd->skilldelay)) {
  3309. ShowError("pc_bonus2: SP_SKILL_DELAY: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skilldelay), type2, val);
  3310. break;
  3311. }
  3312. if (sd->skilldelay[i].id == type2)
  3313. sd->skilldelay[i].val += val;
  3314. else {
  3315. sd->skilldelay[i].id = type2;
  3316. sd->skilldelay[i].val = val;
  3317. }
  3318. break;
  3319. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3320. if(sd->state.lr_flag == 2)
  3321. break;
  3322. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  3323. if (i == ARRAYLENGTH(sd->skillcooldown))
  3324. {
  3325. ShowError("pc_bonus2: SP_SKILL_COOLDOWN: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillcooldown), type2, val);
  3326. break;
  3327. }
  3328. if (sd->skillcooldown[i].id == type2)
  3329. sd->skillcooldown[i].val += val;
  3330. else {
  3331. sd->skillcooldown[i].id = type2;
  3332. sd->skillcooldown[i].val = val;
  3333. }
  3334. break;
  3335. #ifdef RENEWAL_CAST
  3336. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3337. if(sd->state.lr_flag == 2)
  3338. break;
  3339. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  3340. if (i == ARRAYLENGTH(sd->skillfixcast))
  3341. {
  3342. ShowError("pc_bonus2: SP_SKILL_FIXEDCAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillfixcast), type2, val);
  3343. break;
  3344. }
  3345. if (sd->skillfixcast[i].id == type2)
  3346. sd->skillfixcast[i].val += val;
  3347. else {
  3348. sd->skillfixcast[i].id = type2;
  3349. sd->skillfixcast[i].val = val;
  3350. }
  3351. break;
  3352. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3353. if(sd->state.lr_flag == 2)
  3354. break;
  3355. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  3356. if (i == ARRAYLENGTH(sd->skillvarcast))
  3357. {
  3358. ShowError("pc_bonus2: SP_SKILL_VARIABLECAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillvarcast), type2, val);
  3359. break;
  3360. }
  3361. if (sd->skillvarcast[i].id == type2)
  3362. sd->skillvarcast[i].val += val;
  3363. else {
  3364. sd->skillvarcast[i].id = type2;
  3365. sd->skillvarcast[i].val = val;
  3366. }
  3367. break;
  3368. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3369. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3370. if(sd->state.lr_flag == 2)
  3371. break;
  3372. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3373. if (i == ARRAYLENGTH(sd->skillcastrate))
  3374. {
  3375. ShowError("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",ARRAYLENGTH(sd->skillcastrate), type2, val);
  3376. break;
  3377. }
  3378. if(sd->skillcastrate[i].id == type2)
  3379. sd->skillcastrate[i].val -= val;
  3380. else {
  3381. sd->skillcastrate[i].id = type2;
  3382. sd->skillcastrate[i].val -= val;
  3383. }
  3384. break;
  3385. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3386. if(sd->state.lr_flag == 2)
  3387. break;
  3388. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  3389. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  3390. {
  3391. ShowError("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  3392. break;
  3393. }
  3394. if(sd->skillfixcastrate[i].id == type2)
  3395. sd->skillfixcastrate[i].val -= val;
  3396. else {
  3397. sd->skillfixcastrate[i].id = type2;
  3398. sd->skillfixcastrate[i].val -= val;
  3399. }
  3400. break;
  3401. #else
  3402. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3403. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3404. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3405. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3406. break;
  3407. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3408. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3409. if(sd->state.lr_flag == 2)
  3410. break;
  3411. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3412. if (i == ARRAYLENGTH(sd->skillcastrate))
  3413. { //Better mention this so the array length can be updated. [Skotlex]
  3414. ShowError("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",
  3415. (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", ARRAYLENGTH(sd->skillcastrate), type2, val);
  3416. break;
  3417. }
  3418. if(sd->skillcastrate[i].id == type2)
  3419. sd->skillcastrate[i].val += val;
  3420. else {
  3421. sd->skillcastrate[i].id = type2;
  3422. sd->skillcastrate[i].val = val;
  3423. }
  3424. break;
  3425. #endif
  3426. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3427. if(sd->state.lr_flag == 2)
  3428. break;
  3429. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  3430. if (i == ARRAYLENGTH(sd->skillusesp)) {
  3431. ShowError("pc_bonus2: SP_SKILL_USE_SP: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillusesp), type2, val);
  3432. break;
  3433. }
  3434. if (sd->skillusesp[i].id == type2)
  3435. sd->skillusesp[i].val += val;
  3436. else {
  3437. sd->skillusesp[i].id = type2;
  3438. sd->skillusesp[i].val = val;
  3439. }
  3440. break;
  3441. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3442. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == type2 || sd->subskill[i].id == 0);
  3443. if (i == ARRAYLENGTH(sd->subskill)) {
  3444. ShowError("pc_bonus2: SP_SUB_SKILL: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->subskill), type2, val);
  3445. break;
  3446. }
  3447. if (sd->subskill[i].id == type2)
  3448. sd->subskill[i].val += val;
  3449. else {
  3450. sd->subskill[i].id = type2;
  3451. sd->subskill[i].val = val;
  3452. }
  3453. break;
  3454. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3455. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3456. sd->subdefele[type2] += val;
  3457. break;
  3458. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3459. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3460. sd->coma_class[type2] += val;
  3461. sd->special_state.bonus_coma = 1;
  3462. break;
  3463. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3464. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3465. sd->coma_race[type2] += val;
  3466. sd->special_state.bonus_coma = 1;
  3467. break;
  3468. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3469. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3470. if(sd->state.lr_flag != 2)
  3471. sd->magic_addrace2[type2] += val;
  3472. break;
  3473. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3474. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3475. if (sd->state.lr_flag != 2)
  3476. sd->ignore_mdef_by_race2[type2] += val;
  3477. break;
  3478. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3479. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3480. if (sd->state.lr_flag != 2)
  3481. sd->dropaddrace[type2] += val;
  3482. break;
  3483. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3484. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3485. if (sd->state.lr_flag != 2)
  3486. sd->dropaddclass[type2] += val;
  3487. break;
  3488. default:
  3489. if (running_npc_stat_calc_event) {
  3490. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3491. }
  3492. else if (current_equip_combo_pos > 0) {
  3493. 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);
  3494. }
  3495. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3496. 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);
  3497. }
  3498. else {
  3499. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3500. }
  3501. break;
  3502. }
  3503. }
  3504. /**
  3505. * Gives item bonus to player for format: bonus3 bBonusName,type2,val;
  3506. * @param sd
  3507. * @param type Bonus type used by bBonusName
  3508. * @param type2
  3509. * @param val Value that usually for rate or fixed value
  3510. */
  3511. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3512. {
  3513. nullpo_retv(sd);
  3514. switch(type){
  3515. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3516. if(sd->state.lr_flag != 2)
  3517. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, type3, val);
  3518. break;
  3519. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3520. if(sd->state.lr_flag != 2)
  3521. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, -type3, val);
  3522. break;
  3523. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3524. if(sd->state.lr_flag != 2)
  3525. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, type3, RC_NONE_, val);
  3526. break;
  3527. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3528. if(sd->state.lr_flag != 2)
  3529. {
  3530. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3531. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3532. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  3533. target?-type2:type2, type3, val, 0, current_equip_card_id);
  3534. }
  3535. break;
  3536. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3537. if(sd->state.lr_flag != 2)
  3538. {
  3539. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3540. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3541. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  3542. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3543. }
  3544. break;
  3545. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3546. if (sd->state.lr_flag != 2)
  3547. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_NONE, type3, val);
  3548. break;
  3549. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3550. if (sd->state.lr_flag != 2)
  3551. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, type3, RC_NONE_, val);
  3552. break;
  3553. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3554. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3555. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3556. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3557. break;
  3558. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3559. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3560. if(sd->state.lr_flag != 2)
  3561. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, val, 0);
  3562. break;
  3563. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3564. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3565. if( sd->state.lr_flag != 2 )
  3566. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, val, type2, ATF_TARGET, 0);
  3567. break;
  3568. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3569. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3570. if (sd->state.lr_flag != 2)
  3571. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3572. break;
  3573. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3574. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3575. if (sd->state.lr_flag != 2)
  3576. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3577. break;
  3578. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3579. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3580. if(sd->state.lr_flag != 2) {
  3581. sd->sp_vanish_race[type2].rate += type3;
  3582. sd->sp_vanish_race[type2].per += val;
  3583. }
  3584. break;
  3585. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3586. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3587. if(sd->state.lr_flag != 2) {
  3588. sd->hp_vanish_race[type2].rate += type3;
  3589. sd->hp_vanish_race[type2].per += val;
  3590. }
  3591. break;
  3592. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3593. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3594. if (sd->state.lr_flag == 2)
  3595. break;
  3596. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3597. //if (type3 > sd->norecover_state_race[type2].rate) {
  3598. // sd->norecover_state_race[type2].rate = type3;
  3599. // sd->norecover_state_race[type2].tick = val;
  3600. // break;
  3601. //}
  3602. sd->norecover_state_race[type2].rate = type3;
  3603. sd->norecover_state_race[type2].tick = val;
  3604. break;
  3605. default:
  3606. if (running_npc_stat_calc_event) {
  3607. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3608. }
  3609. else if (current_equip_combo_pos > 0) {
  3610. 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);
  3611. }
  3612. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3613. 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);
  3614. }
  3615. else {
  3616. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3617. }
  3618. break;
  3619. }
  3620. }
  3621. /**
  3622. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,val;
  3623. * @param sd
  3624. * @param type Bonus type used by bBonusName
  3625. * @param type2
  3626. * @param type3
  3627. * @param val Value that usually for rate or fixed value
  3628. */
  3629. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3630. {
  3631. nullpo_retv(sd);
  3632. switch(type){
  3633. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3634. if(sd->state.lr_flag != 2)
  3635. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3636. break;
  3637. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3638. if(sd->state.lr_flag != 2)
  3639. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3640. break;
  3641. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3642. if(sd->state.lr_flag != 2)
  3643. {
  3644. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3645. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type3)&INF2_NO_TARGET_SELF));
  3646. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3647. }
  3648. break;
  3649. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3650. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3651. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3652. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3653. break;
  3654. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3655. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3656. if (sd->state.lr_flag != 2)
  3657. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, type4, val);
  3658. break;
  3659. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3660. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3661. if( sd->state.lr_flag != 2 )
  3662. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, val, 0);
  3663. break;
  3664. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3665. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3666. if(sd->state.lr_flag == 2)
  3667. break;
  3668. sd->def_set_race[type2].rate = type3;
  3669. sd->def_set_race[type2].tick = type4;
  3670. sd->def_set_race[type2].value = val;
  3671. break;
  3672. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3673. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3674. if(sd->state.lr_flag == 2)
  3675. break;
  3676. sd->mdef_set_race[type2].rate = type3;
  3677. sd->mdef_set_race[type2].tick = type4;
  3678. sd->mdef_set_race[type2].value = val;
  3679. break;
  3680. default:
  3681. if (running_npc_stat_calc_event) {
  3682. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3683. }
  3684. else if (current_equip_combo_pos > 0) {
  3685. 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);
  3686. }
  3687. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3688. 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);
  3689. }
  3690. else {
  3691. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3692. }
  3693. break;
  3694. }
  3695. }
  3696. /**
  3697. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3698. * @param sd
  3699. * @param type Bonus type used by bBonusName
  3700. * @param type2
  3701. * @param type3
  3702. * @param type4
  3703. * @param val Value that usually for rate or fixed value
  3704. */
  3705. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3706. {
  3707. nullpo_retv(sd);
  3708. switch(type){
  3709. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3710. if(sd->state.lr_flag != 2)
  3711. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3712. break;
  3713. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3714. if(sd->state.lr_flag != 2)
  3715. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3716. break;
  3717. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3718. if(sd->state.lr_flag != 2)
  3719. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3720. break;
  3721. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3722. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3723. if( sd->state.lr_flag != 2 )
  3724. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, type5, val);
  3725. break;
  3726. default:
  3727. if (running_npc_stat_calc_event) {
  3728. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3729. }
  3730. else if (current_equip_combo_pos > 0) {
  3731. 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);
  3732. }
  3733. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3734. 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);
  3735. }
  3736. else {
  3737. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3738. }
  3739. break;
  3740. }
  3741. }
  3742. /*==========================================
  3743. * Grants a player a given skill. Flag values are:
  3744. * 0 - Grant permanent skill to be bound to skill tree
  3745. * 1 - Grant an item skill (temporary)
  3746. * 2 - Like 1, except the level granted can stack with previously learned level.
  3747. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3748. *------------------------------------------*/
  3749. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3750. uint16 idx = 0;
  3751. nullpo_ret(sd);
  3752. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3753. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3754. return false;
  3755. }
  3756. if (level > MAX_SKILL_LEVEL) {
  3757. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3758. return false;
  3759. }
  3760. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3761. 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);
  3762. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3763. }
  3764. switch (type) {
  3765. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3766. sd->status.skill[idx].id = skill_id;
  3767. sd->status.skill[idx].lv = level;
  3768. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3769. if (level == 0) { //Remove skill.
  3770. sd->status.skill[idx].id = 0;
  3771. clif_deleteskill(sd,skill_id);
  3772. } else
  3773. clif_addskill(sd,skill_id);
  3774. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3775. status_calc_pc(sd, SCO_NONE);
  3776. break;
  3777. case ADDSKILL_TEMP: //Item bonus skill.
  3778. if (sd->status.skill[idx].id != 0) {
  3779. if (sd->status.skill[idx].lv >= level)
  3780. return true;
  3781. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3782. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3783. } else {
  3784. sd->status.skill[idx].id = skill_id;
  3785. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3786. }
  3787. sd->status.skill[idx].lv = level;
  3788. break;
  3789. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3790. if (sd->status.skill[idx].id != 0) {
  3791. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3792. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3793. } else {
  3794. sd->status.skill[idx].id = skill_id;
  3795. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3796. }
  3797. sd->status.skill[idx].lv += level;
  3798. break;
  3799. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3800. sd->status.skill[idx].id = skill_id;
  3801. sd->status.skill[idx].lv = level;
  3802. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3803. if (level == 0) { //Remove skill.
  3804. sd->status.skill[idx].id = 0;
  3805. clif_deleteskill(sd,skill_id);
  3806. } else
  3807. clif_addskill(sd,skill_id);
  3808. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3809. status_calc_pc(sd, SCO_NONE);
  3810. break;
  3811. default:
  3812. return false;
  3813. }
  3814. return true;
  3815. }
  3816. /*==========================================
  3817. * Append a card to an item ?
  3818. *------------------------------------------*/
  3819. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3820. {
  3821. int i;
  3822. unsigned short nameid;
  3823. nullpo_ret(sd);
  3824. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3825. return 0; //Invalid item index.
  3826. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3827. return 0; //Invalid card index.
  3828. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  3829. return 0; // target item missing
  3830. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  3831. return 0; // target card missing
  3832. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3833. return 0; // only weapons and armor are allowed
  3834. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3835. return 0; // must be a card
  3836. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  3837. return 0; // target must be identified
  3838. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  3839. return 0; // card slots reserved for other purposes
  3840. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3841. return 0; // card cannot be compounded on this item type
  3842. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3843. return 0; // attempted to place shield card on left-hand weapon.
  3844. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  3845. return 0; // item must be unequipped
  3846. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  3847. if( i == sd->inventory_data[idx_equip]->slot )
  3848. return 0; // no free slots
  3849. // remember the card id to insert
  3850. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  3851. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3852. {// failed
  3853. clif_insert_card(sd,idx_equip,idx_card,1);
  3854. }
  3855. else
  3856. {// success
  3857. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  3858. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  3859. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  3860. clif_insert_card(sd,idx_equip,idx_card,0);
  3861. }
  3862. return 0;
  3863. }
  3864. /**
  3865. * Returns the count of unidentified items with the option to identify too.
  3866. * @param sd: Player data
  3867. * @param identify_item: Whether or not to identify any unidentified items
  3868. * @return Unidentified items count
  3869. */
  3870. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  3871. {
  3872. int unidentified_count = 0;
  3873. for (int i = 0; i < MAX_INVENTORY; i++) {
  3874. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  3875. if (identify_item == true) {
  3876. sd->inventory.u.items_inventory[i].identify = 1;
  3877. clif_item_identified(sd,i,0);
  3878. }
  3879. unidentified_count++;
  3880. }
  3881. }
  3882. return unidentified_count;
  3883. }
  3884. //
  3885. // Items
  3886. //
  3887. /*==========================================
  3888. * Update buying value by skills
  3889. *------------------------------------------*/
  3890. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3891. {
  3892. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3893. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3894. rate1 = 5+skill*2-((skill==10)? 1:0);
  3895. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3896. rate2 = 5+skill*4;
  3897. if(rate1 < rate2) rate1 = rate2;
  3898. if(rate1)
  3899. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3900. if(val < 0) val = 0;
  3901. if(orig_value > 0 && val < 1) val = 1;
  3902. return val;
  3903. }
  3904. /*==========================================
  3905. * Update selling value by skills
  3906. *------------------------------------------*/
  3907. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3908. {
  3909. int skill,val = orig_value,rate = 0;
  3910. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3911. rate = 5+skill*2-((skill==10)? 1:0);
  3912. if(rate)
  3913. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3914. if(val < 0) val = 0;
  3915. if(orig_value > 0 && val < 1) val = 1;
  3916. return val;
  3917. }
  3918. /*==========================================
  3919. * Checking if we have enough place on inventory for new item
  3920. * Make sure to take 30k as limit (for client I guess)
  3921. * @param sd
  3922. * @param nameid
  3923. * @param amount
  3924. * @return e_chkitem_result
  3925. *------------------------------------------*/
  3926. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  3927. {
  3928. int i;
  3929. struct item_data* data;
  3930. nullpo_ret(sd);
  3931. if(amount > MAX_AMOUNT)
  3932. return CHKADDITEM_OVERAMOUNT;
  3933. data = itemdb_search(nameid);
  3934. if(!itemdb_isstackable2(data))
  3935. return CHKADDITEM_NEW;
  3936. if( data->stack.inventory && amount > data->stack.amount )
  3937. return CHKADDITEM_OVERAMOUNT;
  3938. for(i=0;i<MAX_INVENTORY;i++){
  3939. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3940. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  3941. 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 ) )
  3942. return CHKADDITEM_OVERAMOUNT;
  3943. return CHKADDITEM_EXIST;
  3944. }
  3945. }
  3946. return CHKADDITEM_NEW;
  3947. }
  3948. /*==========================================
  3949. * Return number of available place in inventory
  3950. * Each non stackable item will reduce place by 1
  3951. * @param sd
  3952. * @return Number of empty slots
  3953. *------------------------------------------*/
  3954. uint8 pc_inventoryblank(struct map_session_data *sd)
  3955. {
  3956. uint8 i, b;
  3957. nullpo_ret(sd);
  3958. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  3959. if(sd->inventory.u.items_inventory[i].nameid == 0)
  3960. b++;
  3961. }
  3962. return b;
  3963. }
  3964. /**
  3965. * Attempts to remove zeny from player
  3966. * @param sd: Player
  3967. * @param zeny: Zeny removed
  3968. * @param type: Log type
  3969. * @param tsd: (optional) From who to log (if null take sd)
  3970. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  3971. */
  3972. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3973. {
  3974. nullpo_retr(2,sd);
  3975. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3976. if( zeny < 0 )
  3977. {
  3978. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3979. return 1;
  3980. }
  3981. if( sd->status.zeny < zeny )
  3982. return 1; //Not enough.
  3983. sd->status.zeny -= zeny;
  3984. clif_updatestatus(sd,SP_ZENY);
  3985. if(!tsd) tsd = sd;
  3986. log_zeny(sd, type, tsd, -zeny);
  3987. if( zeny > 0 && sd->state.showzeny ) {
  3988. char output[255];
  3989. sprintf(output, "Removed %dz.", zeny);
  3990. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  3991. }
  3992. return 0;
  3993. }
  3994. /**
  3995. * Attempts to give zeny to player
  3996. * @param sd: Player
  3997. * @param type: Log type
  3998. * @param tsd: (optional) From who to log (if null take sd)
  3999. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4000. */
  4001. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4002. {
  4003. nullpo_retr(-1,sd);
  4004. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4005. if( zeny < 0 )
  4006. {
  4007. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4008. return 1;
  4009. }
  4010. if( zeny > MAX_ZENY - sd->status.zeny )
  4011. zeny = MAX_ZENY - sd->status.zeny;
  4012. sd->status.zeny += zeny;
  4013. clif_updatestatus(sd,SP_ZENY);
  4014. if(!tsd) tsd = sd;
  4015. log_zeny(sd, type, tsd, zeny);
  4016. if( zeny > 0 && sd->state.showzeny ) {
  4017. char output[255];
  4018. sprintf(output, "Gained %dz.", zeny);
  4019. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4020. }
  4021. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4022. return 0;
  4023. }
  4024. /**
  4025. * Attempts to remove Cash Points from player
  4026. * @param sd: Player
  4027. * @param price: Points player has to pay
  4028. * @param points: Points player has
  4029. * @param type: Log type
  4030. * @return -2: Paying negative points, -1: Not enough points, otherwise success (cash+points)
  4031. */
  4032. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4033. {
  4034. int cash;
  4035. nullpo_retr(-1,sd);
  4036. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4037. if( price < 0 || points < 0 )
  4038. {
  4039. ShowError("pc_paycash: Paying negative points (price=%d, points=%d, account_id=%d, char_id=%d).\n", price, points, sd->status.account_id, sd->status.char_id);
  4040. return -2;
  4041. }
  4042. if( points > price )
  4043. {
  4044. ShowWarning("pc_paycash: More kafra points provided than needed (price=%d, points=%d, account_id=%d, char_id=%d).\n", price, points, sd->status.account_id, sd->status.char_id);
  4045. points = price;
  4046. }
  4047. cash = price-points;
  4048. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4049. {
  4050. 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);
  4051. return -1;
  4052. }
  4053. if( cash ){
  4054. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4055. sd->cashPoints -= cash;
  4056. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4057. }
  4058. if( points ){
  4059. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4060. sd->kafraPoints -= points;
  4061. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4062. }
  4063. if( battle_config.cashshop_show_points )
  4064. {
  4065. char output[CHAT_SIZE_MAX];
  4066. 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.
  4067. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4068. }
  4069. return cash+points;
  4070. }
  4071. /**
  4072. * Attempts to give Cash Points to player
  4073. * @param sd: Player
  4074. * @param cash: Cash player gets
  4075. * @param points: Points player has
  4076. * @param type: Log type
  4077. * @return -2: Error, -1: Giving negative cash/points, otherwise success (cash or points)
  4078. */
  4079. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4080. {
  4081. char output[CHAT_SIZE_MAX];
  4082. nullpo_retr(-1,sd);
  4083. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4084. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4085. if( cash > 0 )
  4086. {
  4087. if( cash > MAX_ZENY-sd->cashPoints )
  4088. {
  4089. 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);
  4090. cash = MAX_ZENY-sd->cashPoints;
  4091. }
  4092. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4093. sd->cashPoints += cash;
  4094. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4095. if( battle_config.cashshop_show_points )
  4096. {
  4097. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4098. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4099. }
  4100. return cash;
  4101. }
  4102. else if( cash < 0 )
  4103. {
  4104. ShowError("pc_getcash: Obtaining negative cash points (cash=%d, account_id=%d, char_id=%d).\n", cash, sd->status.account_id, sd->status.char_id);
  4105. return -1;
  4106. }
  4107. if( points > 0 )
  4108. {
  4109. if( points > MAX_ZENY-sd->kafraPoints )
  4110. {
  4111. 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);
  4112. points = MAX_ZENY-sd->kafraPoints;
  4113. }
  4114. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4115. sd->kafraPoints += points;
  4116. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4117. if( battle_config.cashshop_show_points )
  4118. {
  4119. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4120. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4121. }
  4122. return points;
  4123. }
  4124. else if( points < 0 )
  4125. {
  4126. ShowError("pc_getcash: Obtaining negative kafra points (points=%d, account_id=%d, char_id=%d).\n", points, sd->status.account_id, sd->status.char_id);
  4127. return -1;
  4128. }
  4129. return -2; //shouldn't happen but just in case
  4130. }
  4131. /**
  4132. * Searching a specified itemid in inventory and return his stored index
  4133. * @param sd Player
  4134. * @param nameid Find this Item!
  4135. * @return Stored index in inventory, or -1 if not found.
  4136. **/
  4137. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4138. short i;
  4139. nullpo_retr(-1, sd);
  4140. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4141. return ( i < MAX_INVENTORY ) ? i : -1;
  4142. }
  4143. /** Attempt to add a new item to player inventory
  4144. * @param sd
  4145. * @param item_data
  4146. * @param amount
  4147. * @param log_type
  4148. * @return
  4149. * 0 = success
  4150. * 1 = invalid itemid not found or negative amount
  4151. * 2 = overweight
  4152. * 3 = ?
  4153. * 4 = no free place found
  4154. * 5 = max amount reached
  4155. * 6 = ?
  4156. * 7 = stack limitation
  4157. */
  4158. char pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4159. struct item_data *id;
  4160. int16 i;
  4161. unsigned int w;
  4162. nullpo_retr(1, sd);
  4163. nullpo_retr(1, item);
  4164. if( item->nameid == 0 || amount <= 0 )
  4165. return ADDITEM_INVALID;
  4166. if( amount > MAX_AMOUNT )
  4167. return ADDITEM_OVERAMOUNT;
  4168. id = itemdb_search(item->nameid);
  4169. if( id->stack.inventory && amount > id->stack.amount )
  4170. {// item stack limitation
  4171. return ADDITEM_STACKLIMIT;
  4172. }
  4173. w = id->weight*amount;
  4174. if(sd->weight + w > sd->max_weight)
  4175. return ADDITEM_OVERWEIGHT;
  4176. i = MAX_INVENTORY;
  4177. if (id->flag.guid && !item->unique_id)
  4178. item->unique_id = pc_generate_unique_id(sd);
  4179. // Stackable | Non Rental
  4180. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4181. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4182. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4183. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4184. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4185. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4186. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4187. 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 ) )
  4188. return ADDITEM_OVERAMOUNT;
  4189. sd->inventory.u.items_inventory[i].amount += amount;
  4190. clif_additem(sd,i,amount,0);
  4191. break;
  4192. }
  4193. }
  4194. }
  4195. if (i >= MAX_INVENTORY) {
  4196. i = pc_search_inventory(sd,0);
  4197. if( i < 0 )
  4198. return ADDITEM_OVERITEM;
  4199. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4200. // clear equip and favorite fields first, just in case
  4201. if( item->equip )
  4202. sd->inventory.u.items_inventory[i].equip = 0;
  4203. if( item->favorite )
  4204. sd->inventory.u.items_inventory[i].favorite = 0;
  4205. sd->inventory.u.items_inventory[i].amount = amount;
  4206. sd->inventory_data[i] = id;
  4207. sd->last_addeditem_index = i;
  4208. if (!itemdb_isstackable2(id) || id->flag.guid)
  4209. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4210. clif_additem(sd,i,amount,0);
  4211. }
  4212. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4213. sd->weight += w;
  4214. clif_updatestatus(sd,SP_WEIGHT);
  4215. //Auto-equip
  4216. if(id->flag.autoequip)
  4217. pc_equipitem(sd, i, id->equip);
  4218. /* rental item check */
  4219. if( item->expire_time ) {
  4220. if( time(NULL) > item->expire_time ) {
  4221. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4222. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4223. } else {
  4224. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4225. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4226. pc_inventory_rental_add(sd, seconds);
  4227. }
  4228. }
  4229. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4230. return ADDITEM_SUCCESS;
  4231. }
  4232. /*==========================================
  4233. * Remove an item at index n from inventory by amount.
  4234. * @param sd
  4235. * @param n Item index in inventory
  4236. * @param amount
  4237. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4238. * @param reason Delete reason
  4239. * @param log_type e_log_pick_type
  4240. * @return 1 - invalid itemid or negative amount; 0 - Success
  4241. *------------------------------------------*/
  4242. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4243. {
  4244. nullpo_retr(1, sd);
  4245. 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)
  4246. return 1;
  4247. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4248. sd->inventory.u.items_inventory[n].amount -= amount;
  4249. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4250. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4251. if(sd->inventory.u.items_inventory[n].equip)
  4252. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4253. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4254. sd->inventory_data[n] = NULL;
  4255. }
  4256. if(!(type&1))
  4257. clif_delitem(sd,n,amount,reason);
  4258. if(!(type&2))
  4259. clif_updatestatus(sd,SP_WEIGHT);
  4260. return 0;
  4261. }
  4262. /*==========================================
  4263. * Attempt to drop an item.
  4264. * @param sd
  4265. * @param n Item index in inventory
  4266. * @param amount Amount of item
  4267. * @return False = fail; True = success
  4268. *------------------------------------------*/
  4269. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4270. {
  4271. nullpo_retr(1, sd);
  4272. if(n < 0 || n >= MAX_INVENTORY)
  4273. return false;
  4274. if(amount <= 0)
  4275. return false;
  4276. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4277. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4278. sd->inventory.u.items_inventory[n].amount < amount ||
  4279. sd->state.trading || sd->state.vending ||
  4280. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4281. )
  4282. return false;
  4283. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4284. {
  4285. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4286. return false; //Can't drop items in nodrop mapflag maps.
  4287. }
  4288. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4289. {
  4290. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4291. return false;
  4292. }
  4293. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4294. return false;
  4295. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4296. clif_dropitem(sd, n, amount);
  4297. return true;
  4298. }
  4299. /*==========================================
  4300. * Attempt to pick up an item.
  4301. * @param sd
  4302. * @param fitem Item that will be picked
  4303. * @return False = fail; True = success
  4304. *------------------------------------------*/
  4305. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4306. {
  4307. int flag = 0;
  4308. unsigned int tick = gettick();
  4309. struct party_data *p = NULL;
  4310. nullpo_ret(sd);
  4311. nullpo_ret(fitem);
  4312. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4313. return false; // Distance is too far
  4314. if (sd->sc.cant.pickup)
  4315. return false;
  4316. if (sd->status.party_id)
  4317. p = party_search(sd->status.party_id);
  4318. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4319. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4320. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4321. if (!(p && p->party.item&1 &&
  4322. first_sd && first_sd->status.party_id == sd->status.party_id
  4323. ))
  4324. return false;
  4325. }
  4326. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4327. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4328. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4329. if (!(p && p->party.item&1 &&
  4330. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4331. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4332. ))
  4333. return false;
  4334. }
  4335. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4336. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4337. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4338. if(!(p && p->party.item&1 &&
  4339. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4340. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4341. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4342. ))
  4343. return false;
  4344. }
  4345. }
  4346. }
  4347. }
  4348. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4349. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4350. clif_additem(sd,0,0,flag);
  4351. return true;
  4352. }
  4353. //Display pickup animation.
  4354. pc_stop_attack(sd);
  4355. clif_takeitem(&sd->bl,&fitem->bl);
  4356. if (fitem->mob_id &&
  4357. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4358. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4359. )
  4360. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4361. map_clearflooritem(&fitem->bl);
  4362. return true;
  4363. }
  4364. /*==========================================
  4365. * Check if item is usable.
  4366. * Return:
  4367. * false = no
  4368. * true = yes
  4369. *------------------------------------------*/
  4370. bool pc_isUseitem(struct map_session_data *sd,int n)
  4371. {
  4372. struct item_data *item;
  4373. unsigned short nameid;
  4374. nullpo_ret(sd);
  4375. item = sd->inventory_data[n];
  4376. nameid = sd->inventory.u.items_inventory[n].nameid;
  4377. if( item == NULL )
  4378. return false;
  4379. //Not consumable item
  4380. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4381. return false;
  4382. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4383. return true;
  4384. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4385. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4386. return false;
  4387. //Prevent mass item usage. [Skotlex]
  4388. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4389. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4390. )
  4391. return false;
  4392. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4393. clif_msg(sd,ITEM_NOUSE_SITTING);
  4394. return false; // You cannot use this item while sitting.
  4395. }
  4396. if (sd->state.storage_flag && item->type != IT_CASH) {
  4397. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4398. return false; // You cannot use this item while storage is open.
  4399. }
  4400. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4401. return false;
  4402. switch( nameid ) {
  4403. case ITEMID_WING_OF_FLY:
  4404. case ITEMID_GIANT_FLY_WING:
  4405. case ITEMID_N_FLY_WING:
  4406. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4407. clif_skill_teleportmessage(sd,0);
  4408. return false;
  4409. }
  4410. if (nameid == ITEMID_GIANT_FLY_WING) {
  4411. struct party_data *pd = party_search(sd->status.party_id);
  4412. if (pd) {
  4413. int i;
  4414. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4415. if (i == MAX_PARTY) { // User is not party leader
  4416. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4417. break;
  4418. }
  4419. 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));
  4420. if (i == MAX_PARTY) { // No party members found on same map
  4421. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4422. break;
  4423. }
  4424. } else {
  4425. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4426. break;
  4427. }
  4428. }
  4429. // Fall through
  4430. case ITEMID_WING_OF_BUTTERFLY:
  4431. case ITEMID_N_BUTTERFLY_WING:
  4432. case ITEMID_DUN_TELE_SCROLL1:
  4433. case ITEMID_DUN_TELE_SCROLL2:
  4434. case ITEMID_DUN_TELE_SCROLL3:
  4435. case ITEMID_WOB_RUNE:
  4436. case ITEMID_WOB_SCHWALTZ:
  4437. case ITEMID_WOB_RACHEL:
  4438. case ITEMID_WOB_LOCAL:
  4439. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4440. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4441. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4442. return false;
  4443. }
  4444. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4445. return false;
  4446. break;
  4447. case ITEMID_MERCENARY_RED_POTION:
  4448. case ITEMID_MERCENARY_BLUE_POTION:
  4449. case ITEMID_M_CENTER_POTION:
  4450. case ITEMID_M_AWAKENING_POTION:
  4451. case ITEMID_M_BERSERK_POTION:
  4452. if( sd->md == NULL || sd->md->db == NULL )
  4453. return false;
  4454. if( sd->md->sc.data[SC_BERSERK] )
  4455. return false;
  4456. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4457. return false;
  4458. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4459. return false;
  4460. break;
  4461. case ITEMID_NEURALIZER:
  4462. if( !mapdata->flag[MF_RESET] )
  4463. return false;
  4464. break;
  4465. }
  4466. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4467. return false; // Mercenary Scrolls
  4468. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4469. if( pc_is90overweight(sd) ) {
  4470. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4471. return false;
  4472. }
  4473. if( !pc_inventoryblank(sd) ) {
  4474. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4475. return false;
  4476. }
  4477. }
  4478. //Gender check
  4479. if(item->sex != 2 && sd->status.sex != item->sex)
  4480. return false;
  4481. //Required level check
  4482. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4483. return false;
  4484. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4485. return false;
  4486. //Not equipable by class. [Skotlex]
  4487. if (!(
  4488. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4489. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4490. ))
  4491. return false;
  4492. if (sd->sc.count && (
  4493. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4494. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4495. sd->sc.data[SC_TRICKDEAD] ||
  4496. sd->sc.data[SC_HIDING] ||
  4497. sd->sc.data[SC__SHADOWFORM] ||
  4498. sd->sc.data[SC__INVISIBILITY] ||
  4499. sd->sc.data[SC__MANHOLE] ||
  4500. sd->sc.data[SC_DEEPSLEEP] ||
  4501. sd->sc.data[SC_CRYSTALIZE] ||
  4502. sd->sc.data[SC_KAGEHUMI] ||
  4503. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4504. sd->sc.data[SC_KINGS_GRACE] ||
  4505. sd->sc.data[SC_SUHIDE]))
  4506. return false;
  4507. if (!pc_isItemClass(sd,item))
  4508. return false;
  4509. //Dead Branch items
  4510. if( item->flag.dead_branch )
  4511. log_branch(sd);
  4512. return true;
  4513. }
  4514. /*==========================================
  4515. * Last checks to use an item.
  4516. * Return:
  4517. * 0 = fail
  4518. * 1 = success
  4519. *------------------------------------------*/
  4520. int pc_useitem(struct map_session_data *sd,int n)
  4521. {
  4522. unsigned int tick = gettick();
  4523. int amount;
  4524. unsigned short nameid;
  4525. struct script_code *script;
  4526. struct item item;
  4527. struct item_data *id;
  4528. nullpo_ret(sd);
  4529. if (sd->npc_id) {
  4530. if (sd->progressbar.npc_id) {
  4531. clif_progressbar_abort(sd);
  4532. return 0; // First item use attempt cancels the progress bar
  4533. }
  4534. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4535. #ifdef RENEWAL
  4536. clif_msg( sd, WORK_IN_PROGRESS );
  4537. #endif
  4538. return 0;
  4539. }
  4540. }
  4541. item = sd->inventory.u.items_inventory[n];
  4542. id = sd->inventory_data[n];
  4543. if (item.nameid == 0 || item.amount <= 0)
  4544. return 0;
  4545. if( !pc_isUseitem(sd,n) )
  4546. return 0;
  4547. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4548. nameid = id->nameid;
  4549. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4550. return 0;
  4551. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4552. if( id->flag.delay_consume ) {
  4553. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4554. return 0;
  4555. else if( pc_issit(sd) )
  4556. return 0;
  4557. }
  4558. //Since most delay-consume items involve using a "skill-type" target cursor,
  4559. //perform a skill-use check before going through. [Skotlex]
  4560. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4561. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  4562. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4563. return 0;
  4564. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4565. return 0;
  4566. /* on restricted maps the item is consumed but the effect is not used */
  4567. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4568. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4569. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4570. clif_useitemack(sd,n,item.amount-1,true);
  4571. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4572. }
  4573. return 0;/* regardless, effect is not run */
  4574. }
  4575. sd->itemid = item.nameid;
  4576. sd->itemindex = n;
  4577. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4578. sd->catch_target_class = PET_CATCH_FAIL;
  4579. amount = item.amount;
  4580. script = id->script;
  4581. //Check if the item is to be consumed immediately [Skotlex]
  4582. if (id->flag.delay_consume)
  4583. clif_useitemack(sd, n, amount, true);
  4584. else
  4585. {
  4586. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4587. {
  4588. clif_useitemack(sd, n, amount - 1, true);
  4589. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4590. }
  4591. else
  4592. clif_useitemack(sd, n, 0, false);
  4593. }
  4594. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4595. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4596. //Update item use time.
  4597. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4598. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4599. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4600. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4601. potion_flag = 0;
  4602. return 1;
  4603. }
  4604. /**
  4605. * Add item on cart for given index.
  4606. * @param sd
  4607. * @param item
  4608. * @param amount
  4609. * @param log_type
  4610. * @return 0 = success; 1 = fail; 2 = no slot
  4611. */
  4612. unsigned char pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4613. {
  4614. struct item_data *data;
  4615. int i,w;
  4616. nullpo_retr(1, sd);
  4617. nullpo_retr(1, item);
  4618. if(item->nameid == 0 || amount <= 0)
  4619. return 1;
  4620. data = itemdb_search(item->nameid);
  4621. if( data->stack.cart && amount > data->stack.amount )
  4622. {// item stack limitation
  4623. return 1;
  4624. }
  4625. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4626. { // Check item trade restrictions [Skotlex]
  4627. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4628. return 1;
  4629. }
  4630. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4631. return 1;
  4632. i = MAX_CART;
  4633. if( itemdb_isstackable2(data) && !item->expire_time )
  4634. {
  4635. for (i = 0; i < MAX_CART; i++) {
  4636. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4637. && sd->cart.u.items_cart[i].bound == item->bound
  4638. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4639. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4640. )
  4641. break;
  4642. }
  4643. }
  4644. if( i < MAX_CART )
  4645. {// item already in cart, stack it
  4646. 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 ) )
  4647. return 2; // no slot
  4648. sd->cart.u.items_cart[i].amount += amount;
  4649. clif_cart_additem(sd,i,amount,0);
  4650. }
  4651. else
  4652. {// item not stackable or not present, add it
  4653. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4654. if( i == MAX_CART )
  4655. return 2; // no slot
  4656. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4657. sd->cart.u.items_cart[i].id = 0;
  4658. sd->cart.u.items_cart[i].amount = amount;
  4659. sd->cart_num++;
  4660. clif_cart_additem(sd,i,amount,0);
  4661. }
  4662. sd->cart.u.items_cart[i].favorite = 0; // clear
  4663. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4664. sd->cart_weight += w;
  4665. clif_updatestatus(sd,SP_CARTINFO);
  4666. return 0;
  4667. }
  4668. /*==========================================
  4669. * Delete item on cart for given index.
  4670. *------------------------------------------*/
  4671. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4672. {
  4673. nullpo_retv(sd);
  4674. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4675. sd->cart.u.items_cart[n].amount < amount)
  4676. return;
  4677. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4678. sd->cart.u.items_cart[n].amount -= amount;
  4679. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4680. if(sd->cart.u.items_cart[n].amount <= 0) {
  4681. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4682. sd->cart_num--;
  4683. }
  4684. if(!type) {
  4685. clif_cart_delitem(sd,n,amount);
  4686. clif_updatestatus(sd,SP_CARTINFO);
  4687. }
  4688. }
  4689. /*==========================================
  4690. * Transfer item from inventory to cart.
  4691. *------------------------------------------*/
  4692. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4693. {
  4694. struct item *item_data;
  4695. char flag;
  4696. nullpo_retv(sd);
  4697. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4698. return;
  4699. item_data = &sd->inventory.u.items_inventory[idx];
  4700. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4701. return;
  4702. if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
  4703. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4704. else {
  4705. clif_dropitem(sd,idx,0);
  4706. clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  4707. }
  4708. }
  4709. /*==========================================
  4710. * Get number of item in cart.
  4711. * Return:
  4712. -1 = itemid not found or no amount found
  4713. x = remaining itemid on cart after get
  4714. *------------------------------------------*/
  4715. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4716. {
  4717. struct item* item_data;
  4718. nullpo_retr(-1, sd);
  4719. item_data = &sd->cart.u.items_cart[idx];
  4720. if( item_data->nameid == 0 || item_data->amount == 0 )
  4721. return -1;
  4722. return item_data->amount - amount;
  4723. }
  4724. /*==========================================
  4725. * Retrieve an item at index idx from cart.
  4726. *------------------------------------------*/
  4727. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4728. {
  4729. struct item *item_data;
  4730. unsigned char flag = 0;
  4731. nullpo_retv(sd);
  4732. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4733. return;
  4734. item_data=&sd->cart.u.items_cart[idx];
  4735. if(item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4736. return;
  4737. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4738. pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4739. else {
  4740. clif_dropitem(sd,idx,0);
  4741. clif_additem(sd,0,0,flag);
  4742. }
  4743. }
  4744. /*==========================================
  4745. * Bound Item Check
  4746. * Type:
  4747. * 1 Account Bound
  4748. * 2 Guild Bound
  4749. * 3 Party Bound
  4750. * 4 Character Bound
  4751. *------------------------------------------*/
  4752. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4753. {
  4754. int i = 0, j = 0;
  4755. for(i = 0; i < MAX_INVENTORY; i++) {
  4756. 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) {
  4757. idxlist[j] = i;
  4758. j++;
  4759. }
  4760. }
  4761. return j;
  4762. }
  4763. /*==========================================
  4764. * Display item stolen msg to player sd
  4765. *------------------------------------------*/
  4766. int pc_show_steal(struct block_list *bl,va_list ap)
  4767. {
  4768. struct map_session_data *sd;
  4769. int itemid;
  4770. struct item_data *item=NULL;
  4771. char output[100];
  4772. sd=va_arg(ap,struct map_session_data *);
  4773. itemid=va_arg(ap,int);
  4774. if((item=itemdb_exists(itemid))==NULL)
  4775. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4776. else
  4777. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4778. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4779. return 0;
  4780. }
  4781. /**
  4782. * Steal an item from bl (mob).
  4783. * @param sd: Player data
  4784. * @param bl: Object to steal from
  4785. * @param skill_lv: Level of skill used
  4786. * @return True on success or false otherwise
  4787. */
  4788. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4789. {
  4790. int i,itemid;
  4791. double rate;
  4792. unsigned char flag = 0;
  4793. struct status_data *sd_status, *md_status;
  4794. struct mob_data *md;
  4795. struct item tmp_item;
  4796. if(!sd || !bl || bl->type!=BL_MOB)
  4797. return false;
  4798. md = (TBL_MOB *)bl;
  4799. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4800. return false;
  4801. sd_status= status_get_status_data(&sd->bl);
  4802. md_status= status_get_status_data(bl);
  4803. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4804. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  4805. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4806. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4807. ) { //Can't steal from
  4808. md->state.steal_flag = UCHAR_MAX;
  4809. return false;
  4810. }
  4811. // base skill success chance (percentual)
  4812. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4813. rate += sd->bonus.add_steal_rate;
  4814. if( rate < 1
  4815. #ifdef RENEWAL
  4816. || rnd()%100 >= rate
  4817. #endif
  4818. )
  4819. return false;
  4820. // Try dropping one item, in the order from first to last possible slot.
  4821. // Droprate is affected by the skill success rate.
  4822. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4823. 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
  4824. #ifndef RENEWAL
  4825. * rate/100.
  4826. #endif
  4827. )
  4828. break;
  4829. if( i == MAX_STEAL_DROP )
  4830. return false;
  4831. itemid = md->db->dropitem[i].nameid;
  4832. memset(&tmp_item,0,sizeof(tmp_item));
  4833. tmp_item.nameid = itemid;
  4834. tmp_item.amount = 1;
  4835. tmp_item.identify = itemdb_isidentified(itemid);
  4836. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  4837. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4838. //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?
  4839. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4840. if(flag) { //Failed to steal due to overweight
  4841. clif_additem(sd,0,0,flag);
  4842. return false;
  4843. }
  4844. if(battle_config.show_steal_in_same_party)
  4845. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4846. //Logs items, Stolen from mobs [Lupus]
  4847. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4848. //A Rare Steal Global Announce by Lupus
  4849. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4850. struct item_data *i_data;
  4851. char message[128];
  4852. i_data = itemdb_search(itemid);
  4853. 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);
  4854. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4855. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4856. }
  4857. return true;
  4858. }
  4859. /*==========================================
  4860. * Stole zeny from bl (mob)
  4861. * return
  4862. * 0 = fail
  4863. * 1 = success
  4864. *------------------------------------------*/
  4865. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4866. {
  4867. int rate, target_lv;
  4868. struct mob_data *md;
  4869. if(!sd || !target || target->type != BL_MOB)
  4870. return 0;
  4871. md = (TBL_MOB*)target;
  4872. target_lv = status_get_lv(target);
  4873. 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)
  4874. return 0;
  4875. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  4876. if(rnd()%1000 < rate)
  4877. {
  4878. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  4879. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  4880. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4881. md->state.steal_coin_flag = 1;
  4882. return 1;
  4883. }
  4884. return 0;
  4885. }
  4886. /*==========================================
  4887. * Set's a player position.
  4888. * @param sd
  4889. * @param mapindex
  4890. * @param x
  4891. * @param y
  4892. * @param clrtype
  4893. * @return SETPOS_OK Success
  4894. * SETPOS_MAPINDEX Invalid map index
  4895. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  4896. * SETPOS_AUTOTRADE Player is in autotrade state
  4897. *------------------------------------------*/
  4898. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4899. {
  4900. nullpo_retr(SETPOS_OK,sd);
  4901. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4902. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4903. return SETPOS_MAPINDEX;
  4904. }
  4905. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  4906. return SETPOS_AUTOTRADE;
  4907. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  4908. pc_setstand(sd, true);
  4909. pc_setrestartvalue(sd,1);
  4910. }
  4911. int16 m = map_mapindex2mapid(mapindex);
  4912. struct map_data *mapdata = map_getmapdata(m);
  4913. sd->state.changemap = (sd->mapindex != mapindex);
  4914. sd->state.warping = 1;
  4915. sd->state.workinprogress = WIP_DISABLE_NONE;
  4916. if( sd->state.changemap ) { // Misc map-changing settings
  4917. unsigned short curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = mapdata->instance_id;
  4918. if (curr_map_instance_id != new_map_instance_id) {
  4919. if (curr_map_instance_id) // Update instance timer for the map on leave
  4920. instance_delusers(curr_map_instance_id);
  4921. if (new_map_instance_id) // Update instance timer for the map on enter
  4922. instance_addusers(new_map_instance_id);
  4923. }
  4924. sd->state.pmap = sd->bl.m;
  4925. if (sd->sc.count) { // Cancel some map related stuff.
  4926. if (sd->sc.data[SC_JAILED])
  4927. return SETPOS_MAPINDEX; //You may not get out!
  4928. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4929. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4930. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4931. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4932. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4933. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4934. if (sd->sc.data[SC_KNOWLEDGE]) {
  4935. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4936. if (sce->timer != INVALID_TIMER)
  4937. delete_timer(sce->timer, status_change_timer);
  4938. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4939. }
  4940. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4941. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4942. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4943. }
  4944. for(int i = 0; i < EQI_MAX; i++ ) {
  4945. if( sd->equip_index[i] >= 0 )
  4946. if( pc_isequip(sd,sd->equip_index[i]) )
  4947. pc_unequipitem(sd,sd->equip_index[i],2);
  4948. }
  4949. if (battle_config.clear_unit_onwarp&BL_PC)
  4950. skill_clear_unitgroup(&sd->bl);
  4951. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4952. guild_send_dot_remove(sd);
  4953. bg_send_dot_remove(sd);
  4954. if (sd->regen.state.gc)
  4955. sd->regen.state.gc = 0;
  4956. // make sure vending is allowed here
  4957. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  4958. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4959. vending_closevending(sd);
  4960. }
  4961. channel_pcquit(sd,4); //quit map chan
  4962. }
  4963. if( m < 0 )
  4964. {
  4965. uint32 ip;
  4966. uint16 port;
  4967. struct script_state *st;
  4968. //if can't find any map-servers, just abort setting position.
  4969. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4970. return SETPOS_NO_MAPSERVER;
  4971. if (sd->npc_id){
  4972. npc_event_dequeue(sd,false);
  4973. st = sd->st;
  4974. }else{
  4975. st = nullptr;
  4976. }
  4977. npc_script_event(sd, NPCE_LOGOUT);
  4978. //remove from map, THEN change x/y coordinates
  4979. unit_remove_map_pc(sd,clrtype);
  4980. sd->mapindex = mapindex;
  4981. sd->bl.x=x;
  4982. sd->bl.y=y;
  4983. pc_clean_skilltree(sd);
  4984. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  4985. chrif_changemapserver(sd, ip, (short)port);
  4986. //Free session data from this map server [Kevin]
  4987. unit_free_pc(sd);
  4988. if( st ){
  4989. // Has to be done here, because otherwise unit_free_pc will free the stack already
  4990. st->state = END;
  4991. }
  4992. return SETPOS_OK;
  4993. }
  4994. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  4995. {
  4996. 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);
  4997. x = y = 0; // make it random
  4998. }
  4999. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5000. int c=0;
  5001. do {
  5002. x = rnd()%(mapdata->xs-2)+1;
  5003. y = rnd()%(mapdata->ys-2)+1;
  5004. c++;
  5005. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5006. 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));
  5007. return SETPOS_OK; //preventing warp
  5008. //break; //allow warp anyway
  5009. }
  5010. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5011. }
  5012. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5013. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5014. vending_closevending(sd);
  5015. }
  5016. if(sd->bl.prev != NULL){
  5017. unit_remove_map_pc(sd,clrtype);
  5018. clif_changemap(sd,m,x,y); // [MouseJstr]
  5019. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5020. sd->state.rewarp = 1;
  5021. sd->mapindex = mapindex;
  5022. sd->bl.m = m;
  5023. sd->bl.x = sd->ud.to_x = x;
  5024. sd->bl.y = sd->ud.to_y = y;
  5025. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5026. { // Increased guild castle regen [Valaris]
  5027. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5028. if(gc && gc->guild_id == sd->status.guild_id)
  5029. sd->regen.state.gc = 1;
  5030. }
  5031. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5032. {
  5033. sd->pd->bl.m = m;
  5034. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5035. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5036. sd->pd->ud.dir = sd->ud.dir;
  5037. }
  5038. if( hom_is_active(sd->hd) )
  5039. {
  5040. sd->hd->bl.m = m;
  5041. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5042. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5043. sd->hd->ud.dir = sd->ud.dir;
  5044. }
  5045. if( sd->md )
  5046. {
  5047. sd->md->bl.m = m;
  5048. sd->md->bl.x = sd->md->ud.to_x = x;
  5049. sd->md->bl.y = sd->md->ud.to_y = y;
  5050. sd->md->ud.dir = sd->ud.dir;
  5051. }
  5052. if( sd->ed ) {
  5053. sd->ed->bl.m = m;
  5054. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5055. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5056. sd->ed->ud.dir = sd->ud.dir;
  5057. }
  5058. pc_cell_basilica(sd);
  5059. //check if we gonna be rewarped [lighta]
  5060. if(npc_check_areanpc(1,m,x,y,0)){
  5061. sd->count_rewarp++;
  5062. }
  5063. else
  5064. sd->count_rewarp = 0;
  5065. return SETPOS_OK;
  5066. }
  5067. /*==========================================
  5068. * Warp player sd to random location on current map.
  5069. * May fail if no walkable cell found (1000 attempts).
  5070. * Return:
  5071. * 0 = Success
  5072. * 1,2,3 = Fail
  5073. *------------------------------------------*/
  5074. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5075. {
  5076. int x,y,i=0;
  5077. nullpo_ret(sd);
  5078. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5079. if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
  5080. return 3;
  5081. do {
  5082. x = rnd()%(mapdata->xs-2)+1;
  5083. y = rnd()%(mapdata->ys-2)+1;
  5084. } 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);
  5085. if (i < 1000)
  5086. return pc_setpos(sd,mapdata->index,x,y,type);
  5087. return 3;
  5088. }
  5089. /*==========================================
  5090. * Records a memo point at sd's current position
  5091. * pos - entry to replace, (-1: shift oldest entry out)
  5092. *------------------------------------------*/
  5093. bool pc_memo(struct map_session_data* sd, int pos)
  5094. {
  5095. int skill;
  5096. nullpo_ret(sd);
  5097. // check mapflags
  5098. 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) ) {
  5099. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5100. return false;
  5101. }
  5102. // check inputs
  5103. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5104. return false; // invalid input
  5105. // check required skill level
  5106. skill = pc_checkskill(sd, AL_WARP);
  5107. if( skill < 1 ) {
  5108. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5109. return false;
  5110. }
  5111. if( skill < 2 || skill - 2 < pos ) {
  5112. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5113. return false;
  5114. }
  5115. if( pos == -1 )
  5116. {
  5117. uint8 i;
  5118. // prevent memo-ing the same map multiple times
  5119. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5120. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5121. pos = 0;
  5122. }
  5123. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5124. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5125. return false;
  5126. }
  5127. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5128. sd->status.memo_point[pos].x = sd->bl.x;
  5129. sd->status.memo_point[pos].y = sd->bl.y;
  5130. clif_skill_memomessage(sd, 0);
  5131. return true;
  5132. }
  5133. //
  5134. // Skills
  5135. //
  5136. /**
  5137. * Get the skill current cooldown for player.
  5138. * (get the db base cooldown for skill + player specific cooldown)
  5139. * @param sd : player pointer
  5140. * @param id : skill id
  5141. * @param lv : skill lv
  5142. * @return player skill cooldown
  5143. */
  5144. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5145. uint8 i;
  5146. uint16 idx = skill_get_index(skill_id);
  5147. int cooldown = 0, cooldownlen = ARRAYLENGTH(sd->skillcooldown);
  5148. if (!idx) return 0;
  5149. if (skill_db[idx]->cooldown[skill_lv - 1])
  5150. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  5151. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5152. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5153. ARR_FIND(0, cooldownlen, i, sd->skillcooldown[i].id == skill_id);
  5154. if (i < cooldownlen) {
  5155. cooldown += sd->skillcooldown[i].val;
  5156. cooldown = max(0,cooldown);
  5157. }
  5158. return cooldown;
  5159. }
  5160. /*==========================================
  5161. * Return player sd skill_lv learned for given skill
  5162. *------------------------------------------*/
  5163. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5164. {
  5165. uint16 idx = 0;
  5166. if (sd == NULL)
  5167. return 0;
  5168. if ((idx = skill_get_index(skill_id)) == 0) {
  5169. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5170. return 0;
  5171. }
  5172. if (SKILL_CHK_GUILD(skill_id) ) {
  5173. struct guild *g;
  5174. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5175. return guild_checkskill(g,skill_id);
  5176. return 0;
  5177. }
  5178. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5179. }
  5180. /**
  5181. * Check if we still have the correct weapon to continue the skill (actually status)
  5182. * If not ending it
  5183. * @param sd
  5184. * @return 0:error, 1:check done
  5185. */
  5186. static void pc_checkallowskill(struct map_session_data *sd)
  5187. {
  5188. const enum sc_type scw_list[] = {
  5189. SC_TWOHANDQUICKEN,
  5190. SC_ONEHAND,
  5191. SC_AURABLADE,
  5192. SC_PARRYING,
  5193. SC_SPEARQUICKEN,
  5194. SC_ADRENALINE,
  5195. SC_ADRENALINE2,
  5196. SC_DANCING,
  5197. SC_GATLINGFEVER,
  5198. };
  5199. uint8 i;
  5200. nullpo_retv(sd);
  5201. if(!sd->sc.count)
  5202. return;
  5203. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5204. { // Skills requiring specific weapon types
  5205. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5206. continue;
  5207. if(sd->sc.data[scw_list[i]] &&
  5208. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5209. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5210. }
  5211. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5212. // Spurt requires bare hands (feet, in fact xD)
  5213. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5214. if(sd->status.shield <= 0) { // Skills requiring a shield
  5215. const enum sc_type scs_list[] = {
  5216. SC_AUTOGUARD,
  5217. SC_DEFENDER,
  5218. SC_REFLECTSHIELD,
  5219. SC_REFLECTDAMAGE
  5220. };
  5221. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5222. if(sd->sc.data[scs_list[i]])
  5223. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5224. }
  5225. }
  5226. /*==========================================
  5227. * Return equipped index of item on player sd at pos
  5228. * Return
  5229. * -1 : Nothing equipped
  5230. * idx : (this index could be used in inventory to found item_data)
  5231. *------------------------------------------*/
  5232. short pc_checkequip(struct map_session_data *sd,int pos)
  5233. {
  5234. uint8 i;
  5235. nullpo_retr(-1, sd);
  5236. for(i=0;i<EQI_MAX;i++){
  5237. if(pos & equip_bitmask[i])
  5238. return sd->equip_index[i];
  5239. }
  5240. return -1;
  5241. }
  5242. /*==========================================
  5243. * Check if sd has nameid equipped somewhere
  5244. * @sd : the player session
  5245. * @nameid : id of the item to check
  5246. * @min : : see pc.hpp enum equip_index from ? to @max
  5247. * @max : see pc.hpp enum equip_index for @min to ?
  5248. * -return true,false
  5249. *------------------------------------------*/
  5250. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5251. {
  5252. int i;
  5253. for(i = min; i < max; i++) {
  5254. if(equip_bitmask[i]) {
  5255. int idx = sd->equip_index[i];
  5256. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5257. return true;
  5258. }
  5259. }
  5260. return false;
  5261. }
  5262. /*==========================================
  5263. * Convert's from the client's lame Job ID system
  5264. * to the map server's 'makes sense' system. [Skotlex]
  5265. *------------------------------------------*/
  5266. int pc_jobid2mapid(unsigned short b_class)
  5267. {
  5268. switch(b_class)
  5269. {
  5270. //Novice And 1-1 Jobs
  5271. case JOB_NOVICE: return MAPID_NOVICE;
  5272. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5273. case JOB_MAGE: return MAPID_MAGE;
  5274. case JOB_ARCHER: return MAPID_ARCHER;
  5275. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5276. case JOB_MERCHANT: return MAPID_MERCHANT;
  5277. case JOB_THIEF: return MAPID_THIEF;
  5278. case JOB_TAEKWON: return MAPID_TAEKWON;
  5279. case JOB_WEDDING: return MAPID_WEDDING;
  5280. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5281. case JOB_NINJA: return MAPID_NINJA;
  5282. case JOB_XMAS: return MAPID_XMAS;
  5283. case JOB_SUMMER: return MAPID_SUMMER;
  5284. case JOB_HANBOK: return MAPID_HANBOK;
  5285. case JOB_GANGSI: return MAPID_GANGSI;
  5286. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5287. case JOB_SUMMER2: return MAPID_SUMMER2;
  5288. //2-1 Jobs
  5289. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5290. case JOB_KNIGHT: return MAPID_KNIGHT;
  5291. case JOB_WIZARD: return MAPID_WIZARD;
  5292. case JOB_HUNTER: return MAPID_HUNTER;
  5293. case JOB_PRIEST: return MAPID_PRIEST;
  5294. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5295. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5296. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5297. case JOB_KAGEROU:
  5298. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5299. case JOB_REBELLION: return MAPID_REBELLION;
  5300. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5301. //2-2 Jobs
  5302. case JOB_CRUSADER: return MAPID_CRUSADER;
  5303. case JOB_SAGE: return MAPID_SAGE;
  5304. case JOB_BARD:
  5305. case JOB_DANCER: return MAPID_BARDDANCER;
  5306. case JOB_MONK: return MAPID_MONK;
  5307. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5308. case JOB_ROGUE: return MAPID_ROGUE;
  5309. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5310. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5311. //Trans Novice And Trans 1-1 Jobs
  5312. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5313. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5314. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5315. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5316. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5317. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5318. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5319. //Trans 2-1 Jobs
  5320. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5321. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5322. case JOB_SNIPER: return MAPID_SNIPER;
  5323. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5324. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5325. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5326. //Trans 2-2 Jobs
  5327. case JOB_PALADIN: return MAPID_PALADIN;
  5328. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5329. case JOB_CLOWN:
  5330. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5331. case JOB_CHAMPION: return MAPID_CHAMPION;
  5332. case JOB_CREATOR: return MAPID_CREATOR;
  5333. case JOB_STALKER: return MAPID_STALKER;
  5334. //Baby Novice And Baby 1-1 Jobs
  5335. case JOB_BABY: return MAPID_BABY;
  5336. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5337. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5338. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5339. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5340. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5341. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5342. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5343. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5344. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5345. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5346. //Baby 2-1 Jobs
  5347. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5348. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5349. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5350. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5351. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5352. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5353. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5354. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5355. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5356. case JOB_BABY_KAGEROU:
  5357. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5358. //Baby 2-2 Jobs
  5359. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5360. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5361. case JOB_BABY_BARD:
  5362. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5363. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5364. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5365. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5366. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5367. //3-1 Jobs
  5368. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5369. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5370. case JOB_WARLOCK: return MAPID_WARLOCK;
  5371. case JOB_RANGER: return MAPID_RANGER;
  5372. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5373. case JOB_MECHANIC: return MAPID_MECHANIC;
  5374. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5375. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5376. //3-2 Jobs
  5377. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5378. case JOB_SORCERER: return MAPID_SORCERER;
  5379. case JOB_MINSTREL:
  5380. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5381. case JOB_SURA: return MAPID_SURA;
  5382. case JOB_GENETIC: return MAPID_GENETIC;
  5383. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5384. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5385. //Trans 3-1 Jobs
  5386. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5387. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5388. case JOB_RANGER_T: return MAPID_RANGER_T;
  5389. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5390. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5391. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5392. //Trans 3-2 Jobs
  5393. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5394. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5395. case JOB_MINSTREL_T:
  5396. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5397. case JOB_SURA_T: return MAPID_SURA_T;
  5398. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5399. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5400. //Baby 3-1 Jobs
  5401. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5402. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5403. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5404. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5405. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5406. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5407. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5408. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5409. //Baby 3-2 Jobs
  5410. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5411. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5412. case JOB_BABY_MINSTREL:
  5413. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5414. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5415. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5416. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5417. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5418. //Doram Jobs
  5419. case JOB_SUMMONER: return MAPID_SUMMONER;
  5420. default:
  5421. return -1;
  5422. }
  5423. }
  5424. //Reverts the map-style class id to the client-style one.
  5425. int pc_mapid2jobid(unsigned short class_, int sex)
  5426. {
  5427. switch(class_) {
  5428. //Novice And 1-1 Jobs
  5429. case MAPID_NOVICE: return JOB_NOVICE;
  5430. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5431. case MAPID_MAGE: return JOB_MAGE;
  5432. case MAPID_ARCHER: return JOB_ARCHER;
  5433. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5434. case MAPID_MERCHANT: return JOB_MERCHANT;
  5435. case MAPID_THIEF: return JOB_THIEF;
  5436. case MAPID_TAEKWON: return JOB_TAEKWON;
  5437. case MAPID_WEDDING: return JOB_WEDDING;
  5438. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5439. case MAPID_NINJA: return JOB_NINJA;
  5440. case MAPID_XMAS: return JOB_XMAS;
  5441. case MAPID_SUMMER: return JOB_SUMMER;
  5442. case MAPID_HANBOK: return JOB_HANBOK;
  5443. case MAPID_GANGSI: return JOB_GANGSI;
  5444. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5445. case MAPID_SUMMER2: return JOB_SUMMER2;
  5446. //2-1 Jobs
  5447. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5448. case MAPID_KNIGHT: return JOB_KNIGHT;
  5449. case MAPID_WIZARD: return JOB_WIZARD;
  5450. case MAPID_HUNTER: return JOB_HUNTER;
  5451. case MAPID_PRIEST: return JOB_PRIEST;
  5452. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5453. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5454. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5455. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5456. case MAPID_REBELLION: return JOB_REBELLION;
  5457. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5458. //2-2 Jobs
  5459. case MAPID_CRUSADER: return JOB_CRUSADER;
  5460. case MAPID_SAGE: return JOB_SAGE;
  5461. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5462. case MAPID_MONK: return JOB_MONK;
  5463. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5464. case MAPID_ROGUE: return JOB_ROGUE;
  5465. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5466. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5467. //Trans Novice And Trans 2-1 Jobs
  5468. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5469. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5470. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5471. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5472. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5473. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5474. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5475. //Trans 2-1 Jobs
  5476. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5477. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5478. case MAPID_SNIPER: return JOB_SNIPER;
  5479. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5480. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5481. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5482. //Trans 2-2 Jobs
  5483. case MAPID_PALADIN: return JOB_PALADIN;
  5484. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5485. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5486. case MAPID_CHAMPION: return JOB_CHAMPION;
  5487. case MAPID_CREATOR: return JOB_CREATOR;
  5488. case MAPID_STALKER: return JOB_STALKER;
  5489. //Baby Novice And Baby 1-1 Jobs
  5490. case MAPID_BABY: return JOB_BABY;
  5491. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5492. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5493. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5494. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5495. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5496. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5497. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5498. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5499. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5500. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5501. //Baby 2-1 Jobs
  5502. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5503. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5504. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5505. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5506. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5507. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5508. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5509. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5510. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5511. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5512. //Baby 2-2 Jobs
  5513. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5514. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5515. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5516. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5517. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5518. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5519. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5520. //3-1 Jobs
  5521. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5522. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5523. case MAPID_WARLOCK: return JOB_WARLOCK;
  5524. case MAPID_RANGER: return JOB_RANGER;
  5525. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5526. case MAPID_MECHANIC: return JOB_MECHANIC;
  5527. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5528. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5529. //3-2 Jobs
  5530. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5531. case MAPID_SORCERER: return JOB_SORCERER;
  5532. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5533. case MAPID_SURA: return JOB_SURA;
  5534. case MAPID_GENETIC: return JOB_GENETIC;
  5535. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5536. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5537. //Trans 3-1 Jobs
  5538. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5539. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5540. case MAPID_RANGER_T: return JOB_RANGER_T;
  5541. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5542. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5543. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5544. //Trans 3-2 Jobs
  5545. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5546. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5547. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5548. case MAPID_SURA_T: return JOB_SURA_T;
  5549. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5550. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5551. //Baby 3-1 Jobs
  5552. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5553. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5554. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5555. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5556. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5557. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5558. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5559. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5560. //Baby 3-2 Jobs
  5561. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5562. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5563. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5564. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5565. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5566. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5567. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5568. //Doram Jobs
  5569. case MAPID_SUMMONER: return JOB_SUMMONER;
  5570. default:
  5571. return -1;
  5572. }
  5573. }
  5574. /*====================================================
  5575. * This function return the name of the job (by [Yor])
  5576. *----------------------------------------------------*/
  5577. const char* job_name(int class_)
  5578. {
  5579. switch (class_) {
  5580. case JOB_NOVICE:
  5581. case JOB_SWORDMAN:
  5582. case JOB_MAGE:
  5583. case JOB_ARCHER:
  5584. case JOB_ACOLYTE:
  5585. case JOB_MERCHANT:
  5586. case JOB_THIEF:
  5587. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5588. case JOB_KNIGHT:
  5589. case JOB_PRIEST:
  5590. case JOB_WIZARD:
  5591. case JOB_BLACKSMITH:
  5592. case JOB_HUNTER:
  5593. case JOB_ASSASSIN:
  5594. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5595. case JOB_KNIGHT2:
  5596. return msg_txt(NULL,557);
  5597. case JOB_CRUSADER:
  5598. case JOB_MONK:
  5599. case JOB_SAGE:
  5600. case JOB_ROGUE:
  5601. case JOB_ALCHEMIST:
  5602. case JOB_BARD:
  5603. case JOB_DANCER:
  5604. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5605. case JOB_CRUSADER2:
  5606. return msg_txt(NULL,563);
  5607. case JOB_WEDDING:
  5608. case JOB_SUPER_NOVICE:
  5609. case JOB_GUNSLINGER:
  5610. case JOB_NINJA:
  5611. case JOB_XMAS:
  5612. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5613. case JOB_SUMMER:
  5614. case JOB_SUMMER2:
  5615. return msg_txt(NULL,621);
  5616. case JOB_HANBOK:
  5617. return msg_txt(NULL,694);
  5618. case JOB_OKTOBERFEST:
  5619. return msg_txt(NULL,696);
  5620. case JOB_NOVICE_HIGH:
  5621. case JOB_SWORDMAN_HIGH:
  5622. case JOB_MAGE_HIGH:
  5623. case JOB_ARCHER_HIGH:
  5624. case JOB_ACOLYTE_HIGH:
  5625. case JOB_MERCHANT_HIGH:
  5626. case JOB_THIEF_HIGH:
  5627. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5628. case JOB_LORD_KNIGHT:
  5629. case JOB_HIGH_PRIEST:
  5630. case JOB_HIGH_WIZARD:
  5631. case JOB_WHITESMITH:
  5632. case JOB_SNIPER:
  5633. case JOB_ASSASSIN_CROSS:
  5634. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5635. case JOB_LORD_KNIGHT2:
  5636. return msg_txt(NULL,582);
  5637. case JOB_PALADIN:
  5638. case JOB_CHAMPION:
  5639. case JOB_PROFESSOR:
  5640. case JOB_STALKER:
  5641. case JOB_CREATOR:
  5642. case JOB_CLOWN:
  5643. case JOB_GYPSY:
  5644. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5645. case JOB_PALADIN2:
  5646. return msg_txt(NULL,588);
  5647. case JOB_BABY:
  5648. case JOB_BABY_SWORDMAN:
  5649. case JOB_BABY_MAGE:
  5650. case JOB_BABY_ARCHER:
  5651. case JOB_BABY_ACOLYTE:
  5652. case JOB_BABY_MERCHANT:
  5653. case JOB_BABY_THIEF:
  5654. return msg_txt(NULL,595 - JOB_BABY + class_);
  5655. case JOB_BABY_KNIGHT:
  5656. case JOB_BABY_PRIEST:
  5657. case JOB_BABY_WIZARD:
  5658. case JOB_BABY_BLACKSMITH:
  5659. case JOB_BABY_HUNTER:
  5660. case JOB_BABY_ASSASSIN:
  5661. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5662. case JOB_BABY_KNIGHT2:
  5663. return msg_txt(NULL,602);
  5664. case JOB_BABY_CRUSADER:
  5665. case JOB_BABY_MONK:
  5666. case JOB_BABY_SAGE:
  5667. case JOB_BABY_ROGUE:
  5668. case JOB_BABY_ALCHEMIST:
  5669. case JOB_BABY_BARD:
  5670. case JOB_BABY_DANCER:
  5671. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5672. case JOB_BABY_CRUSADER2:
  5673. return msg_txt(NULL,608);
  5674. case JOB_SUPER_BABY:
  5675. return msg_txt(NULL,615);
  5676. case JOB_TAEKWON:
  5677. return msg_txt(NULL,616);
  5678. case JOB_STAR_GLADIATOR:
  5679. case JOB_STAR_GLADIATOR2:
  5680. return msg_txt(NULL,617);
  5681. case JOB_SOUL_LINKER:
  5682. return msg_txt(NULL,618);
  5683. case JOB_GANGSI:
  5684. case JOB_DEATH_KNIGHT:
  5685. case JOB_DARK_COLLECTOR:
  5686. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5687. case JOB_RUNE_KNIGHT:
  5688. case JOB_WARLOCK:
  5689. case JOB_RANGER:
  5690. case JOB_ARCH_BISHOP:
  5691. case JOB_MECHANIC:
  5692. case JOB_GUILLOTINE_CROSS:
  5693. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5694. case JOB_RUNE_KNIGHT_T:
  5695. case JOB_WARLOCK_T:
  5696. case JOB_RANGER_T:
  5697. case JOB_ARCH_BISHOP_T:
  5698. case JOB_MECHANIC_T:
  5699. case JOB_GUILLOTINE_CROSS_T:
  5700. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5701. case JOB_ROYAL_GUARD:
  5702. case JOB_SORCERER:
  5703. case JOB_MINSTREL:
  5704. case JOB_WANDERER:
  5705. case JOB_SURA:
  5706. case JOB_GENETIC:
  5707. case JOB_SHADOW_CHASER:
  5708. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5709. case JOB_ROYAL_GUARD_T:
  5710. case JOB_SORCERER_T:
  5711. case JOB_MINSTREL_T:
  5712. case JOB_WANDERER_T:
  5713. case JOB_SURA_T:
  5714. case JOB_GENETIC_T:
  5715. case JOB_SHADOW_CHASER_T:
  5716. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5717. case JOB_RUNE_KNIGHT2:
  5718. case JOB_RUNE_KNIGHT_T2:
  5719. return msg_txt(NULL,625);
  5720. case JOB_ROYAL_GUARD2:
  5721. case JOB_ROYAL_GUARD_T2:
  5722. return msg_txt(NULL,631);
  5723. case JOB_RANGER2:
  5724. case JOB_RANGER_T2:
  5725. return msg_txt(NULL,627);
  5726. case JOB_MECHANIC2:
  5727. case JOB_MECHANIC_T2:
  5728. return msg_txt(NULL,629);
  5729. case JOB_BABY_RUNE:
  5730. case JOB_BABY_WARLOCK:
  5731. case JOB_BABY_RANGER:
  5732. case JOB_BABY_BISHOP:
  5733. case JOB_BABY_MECHANIC:
  5734. case JOB_BABY_CROSS:
  5735. case JOB_BABY_GUARD:
  5736. case JOB_BABY_SORCERER:
  5737. case JOB_BABY_MINSTREL:
  5738. case JOB_BABY_WANDERER:
  5739. case JOB_BABY_SURA:
  5740. case JOB_BABY_GENETIC:
  5741. case JOB_BABY_CHASER:
  5742. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5743. case JOB_BABY_RUNE2:
  5744. return msg_txt(NULL,638);
  5745. case JOB_BABY_GUARD2:
  5746. return msg_txt(NULL,644);
  5747. case JOB_BABY_RANGER2:
  5748. return msg_txt(NULL,640);
  5749. case JOB_BABY_MECHANIC2:
  5750. return msg_txt(NULL,642);
  5751. case JOB_SUPER_NOVICE_E:
  5752. case JOB_SUPER_BABY_E:
  5753. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5754. case JOB_KAGEROU:
  5755. case JOB_OBORO:
  5756. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5757. case JOB_REBELLION:
  5758. return msg_txt(NULL,695);
  5759. case JOB_SUMMONER:
  5760. return msg_txt(NULL,697);
  5761. case JOB_BABY_SUMMONER:
  5762. return msg_txt(NULL,698);
  5763. case JOB_BABY_NINJA:
  5764. return msg_txt(NULL,699);
  5765. case JOB_BABY_KAGEROU:
  5766. case JOB_BABY_OBORO:
  5767. case JOB_BABY_TAEKWON:
  5768. case JOB_BABY_STAR_GLADIATOR:
  5769. case JOB_BABY_SOUL_LINKER:
  5770. case JOB_BABY_GUNSLINGER:
  5771. case JOB_BABY_REBELLION:
  5772. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5773. case JOB_BABY_STAR_GLADIATOR2:
  5774. return msg_txt(NULL,756);
  5775. case JOB_STAR_EMPEROR:
  5776. case JOB_SOUL_REAPER:
  5777. case JOB_BABY_STAR_EMPEROR:
  5778. case JOB_BABY_SOUL_REAPER:
  5779. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  5780. case JOB_STAR_EMPEROR2:
  5781. return msg_txt(NULL,782);
  5782. case JOB_BABY_STAR_EMPEROR2:
  5783. return msg_txt(NULL,784);
  5784. default:
  5785. return msg_txt(NULL,655);
  5786. }
  5787. }
  5788. /*====================================================
  5789. * Timered function to make id follow a target.
  5790. * @id = bl.id (player only atm)
  5791. * target is define in sd->followtarget (bl.id)
  5792. * used by pc_follow
  5793. *----------------------------------------------------*/
  5794. TIMER_FUNC(pc_follow_timer){
  5795. struct map_session_data *sd;
  5796. struct block_list *tbl;
  5797. sd = map_id2sd(id);
  5798. nullpo_ret(sd);
  5799. if (sd->followtimer != tid){
  5800. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5801. sd->followtimer = INVALID_TIMER;
  5802. return 0;
  5803. }
  5804. sd->followtimer = INVALID_TIMER;
  5805. tbl = map_id2bl(sd->followtarget);
  5806. if (tbl == NULL || pc_isdead(sd))
  5807. {
  5808. pc_stop_following(sd);
  5809. return 0;
  5810. }
  5811. // either player or target is currently detached from map blocks (could be teleporting),
  5812. // but still connected to this map, so we'll just increment the timer and check back later
  5813. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5814. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5815. {
  5816. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5817. if (!check_distance_bl(&sd->bl, tbl, 5))
  5818. unit_walktobl(&sd->bl, tbl, 5, 0);
  5819. } else
  5820. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5821. }
  5822. sd->followtimer = add_timer(
  5823. tick + 1000, // increase time a bit to loosen up map's load
  5824. pc_follow_timer, sd->bl.id, 0);
  5825. return 0;
  5826. }
  5827. int pc_stop_following (struct map_session_data *sd)
  5828. {
  5829. nullpo_ret(sd);
  5830. if (sd->followtimer != INVALID_TIMER) {
  5831. delete_timer(sd->followtimer,pc_follow_timer);
  5832. sd->followtimer = INVALID_TIMER;
  5833. }
  5834. sd->followtarget = -1;
  5835. sd->ud.target_to = 0;
  5836. unit_stop_walking(&sd->bl, 1);
  5837. return 0;
  5838. }
  5839. int pc_follow(struct map_session_data *sd,int target_id)
  5840. {
  5841. struct block_list *bl = map_id2bl(target_id);
  5842. if (bl == NULL /*|| bl->type != BL_PC*/)
  5843. return 1;
  5844. if (sd->followtimer != INVALID_TIMER)
  5845. pc_stop_following(sd);
  5846. sd->followtarget = target_id;
  5847. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5848. return 0;
  5849. }
  5850. int pc_checkbaselevelup(struct map_session_data *sd) {
  5851. unsigned int next = pc_nextbaseexp(sd);
  5852. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5853. return 0;
  5854. do {
  5855. sd->status.base_exp -= next;
  5856. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5857. 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 )
  5858. sd->status.base_exp = next-1;
  5859. next = pc_gets_status_point(sd->status.base_level);
  5860. sd->status.base_level++;
  5861. sd->status.status_point += next;
  5862. if( pc_is_maxbaselv(sd) ){
  5863. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5864. break;
  5865. }
  5866. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5867. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5868. status_calc_pet(sd->pd,SCO_NONE);
  5869. clif_updatestatus(sd,SP_STATUSPOINT);
  5870. clif_updatestatus(sd,SP_BASELEVEL);
  5871. clif_updatestatus(sd,SP_BASEEXP);
  5872. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5873. status_calc_pc(sd,SCO_FORCE);
  5874. status_percent_heal(&sd->bl,100,100);
  5875. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5876. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5877. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5878. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5879. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5880. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5881. if (sd->state.snovice_dead_flag)
  5882. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5883. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5884. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5885. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5886. }
  5887. clif_misceffect(&sd->bl,0);
  5888. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5889. if(sd->status.party_id)
  5890. party_send_levelup(sd);
  5891. pc_baselevelchanged(sd);
  5892. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  5893. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  5894. return 1;
  5895. }
  5896. void pc_baselevelchanged(struct map_session_data *sd) {
  5897. uint8 i;
  5898. for( i = 0; i < EQI_MAX; i++ ) {
  5899. if( sd->equip_index[i] >= 0 ) {
  5900. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5901. pc_unequipitem(sd, sd->equip_index[i], 3);
  5902. }
  5903. }
  5904. pc_show_questinfo(sd);
  5905. }
  5906. int pc_checkjoblevelup(struct map_session_data *sd)
  5907. {
  5908. unsigned int next = pc_nextjobexp(sd);
  5909. nullpo_ret(sd);
  5910. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  5911. return 0;
  5912. do {
  5913. sd->status.job_exp -= next;
  5914. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5915. 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 )
  5916. sd->status.job_exp = next-1;
  5917. sd->status.job_level ++;
  5918. sd->status.skill_point ++;
  5919. if( pc_is_maxjoblv(sd) ){
  5920. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  5921. break;
  5922. }
  5923. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5924. clif_updatestatus(sd,SP_JOBLEVEL);
  5925. clif_updatestatus(sd,SP_JOBEXP);
  5926. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5927. clif_updatestatus(sd,SP_SKILLPOINT);
  5928. status_calc_pc(sd,SCO_FORCE);
  5929. clif_misceffect(&sd->bl,1);
  5930. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  5931. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5932. npc_script_event(sd, NPCE_JOBLVUP);
  5933. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  5934. return 1;
  5935. }
  5936. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  5937. * @param sd Player
  5938. * @param base_exp Base EXP before peronal bonuses
  5939. * @param job_exp Job EXP before peronal bonuses
  5940. * @param src Block list that affecting the exp calculation
  5941. */
  5942. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5943. {
  5944. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  5945. if (src) {
  5946. struct status_data *status = status_get_status_data(src);
  5947. if( sd->expaddrace[status->race] )
  5948. bonus += sd->expaddrace[status->race];
  5949. if( sd->expaddrace[RC_ALL] )
  5950. bonus += sd->expaddrace[RC_ALL];
  5951. if( sd->expaddclass[status->class_] )
  5952. bonus += sd->expaddclass[status->class_];
  5953. if( sd->expaddclass[CLASS_ALL] )
  5954. bonus += sd->expaddclass[CLASS_ALL];
  5955. if (battle_config.pk_mode &&
  5956. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5957. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5958. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  5959. vip_bonus_base = battle_config.vip_base_exp_increase;
  5960. vip_bonus_job = battle_config.vip_job_exp_increase;
  5961. }
  5962. }
  5963. // Give EXPBOOST for quests even if src is NULL.
  5964. if (sd->sc.data[SC_EXPBOOST]) {
  5965. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5966. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  5967. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  5968. }
  5969. if (*base_exp) {
  5970. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  5971. *base_exp = cap_value(exp, 1, UINT_MAX);
  5972. }
  5973. // Give JEXPBOOST for quests even if src is NULL.
  5974. if (sd->sc.data[SC_JEXPBOOST])
  5975. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5976. if (*job_exp) {
  5977. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  5978. *job_exp = cap_value(exp, 1, UINT_MAX);
  5979. }
  5980. return;
  5981. }
  5982. /**
  5983. * Show EXP gained by player in percentage by @showexp
  5984. * @param sd Player
  5985. * @param base_exp Base EXP gained/loss
  5986. * @param next_base_exp Base EXP needed for next base level
  5987. * @param job_exp Job EXP gained/loss
  5988. * @param next_job_exp Job EXP needed for next job level
  5989. * @param lost True:EXP penalty, lose EXP
  5990. **/
  5991. 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) {
  5992. char output[CHAT_SIZE_MAX];
  5993. nullpo_retv(sd);
  5994. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  5995. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  5996. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  5997. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  5998. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5999. }
  6000. /**
  6001. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6002. * @param sd Player
  6003. * @param src EXP source
  6004. * @param base_exp Base EXP gained
  6005. * @param base_exp Job EXP gained
  6006. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6007. * @return
  6008. **/
  6009. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  6010. {
  6011. unsigned int nextb = 0, nextj = 0;
  6012. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6013. nullpo_retv(sd);
  6014. if(sd->bl.prev == NULL || pc_isdead(sd))
  6015. return;
  6016. if (!(exp_flag&2)) {
  6017. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6018. return; // no exp on pvp maps
  6019. if (sd->status.guild_id>0)
  6020. base_exp -= guild_payexp(sd,base_exp);
  6021. }
  6022. flag = ((base_exp) ? 1 : 0) |
  6023. ((job_exp) ? 2 : 0) |
  6024. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6025. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6026. if (!(exp_flag&2))
  6027. pc_calcexp(sd, &base_exp, &job_exp, src);
  6028. nextb = pc_nextbaseexp(sd);
  6029. nextj = pc_nextjobexp(sd);
  6030. if (flag&4){
  6031. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6032. base_exp = 0;
  6033. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6034. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6035. }
  6036. if (flag&8){
  6037. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6038. job_exp = 0;
  6039. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6040. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6041. }
  6042. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6043. //Note that this value should never be greater than the original
  6044. //therefore no overflow checks are needed. [Skotlex]
  6045. if (nextb > 0) {
  6046. float nextbp = (float) base_exp / (float) nextb;
  6047. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6048. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6049. }
  6050. if (nextj > 0) {
  6051. float nextjp = (float) job_exp / (float) nextj;
  6052. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6053. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6054. }
  6055. }
  6056. // Give EXP for Base Level
  6057. if (base_exp) {
  6058. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  6059. sd->status.base_exp = UINT32_MAX;
  6060. else
  6061. sd->status.base_exp += base_exp;
  6062. if (!pc_checkbaselevelup(sd))
  6063. clif_updatestatus(sd,SP_BASEEXP);
  6064. }
  6065. // Give EXP for Job Level
  6066. if (job_exp) {
  6067. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  6068. sd->status.job_exp = UINT32_MAX;
  6069. else
  6070. sd->status.job_exp += job_exp;
  6071. if (!pc_checkjoblevelup(sd))
  6072. clif_updatestatus(sd,SP_JOBEXP);
  6073. }
  6074. if (flag&1)
  6075. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6076. if (flag&2)
  6077. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6078. if (sd->state.showexp && (base_exp || job_exp))
  6079. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6080. }
  6081. /**
  6082. * Lost Base/Job EXP from a player
  6083. * @param sd Player
  6084. * @param base_exp Base EXP lost
  6085. * @param job_exp Job EXP lost
  6086. **/
  6087. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  6088. nullpo_retv(sd);
  6089. if (base_exp) {
  6090. base_exp = u32min(sd->status.base_exp, base_exp);
  6091. sd->status.base_exp -= base_exp;
  6092. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6093. clif_updatestatus(sd, SP_BASEEXP);
  6094. }
  6095. if (job_exp) {
  6096. job_exp = u32min(sd->status.job_exp, job_exp);
  6097. sd->status.job_exp -= job_exp;
  6098. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6099. clif_updatestatus(sd, SP_JOBEXP);
  6100. }
  6101. if (sd->state.showexp && (base_exp || job_exp))
  6102. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6103. }
  6104. /**
  6105. * Returns max base level for this character's class.
  6106. * @param class_: Player's class
  6107. * @return Max Base Level
  6108. */
  6109. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6110. return job_info[pc_class2idx(class_)].max_level[0];
  6111. }
  6112. /**
  6113. * Returns max base level for this character.
  6114. * @param sd Player
  6115. * @return Max Base Level
  6116. **/
  6117. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6118. return pc_class_maxbaselv(sd->status.class_);
  6119. }
  6120. /**
  6121. * Returns max job level for this character's class.
  6122. * @param class_: Player's class
  6123. * @return Max Job Level
  6124. */
  6125. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6126. return job_info[pc_class2idx(class_)].max_level[1];
  6127. }
  6128. /**
  6129. * Returns max job level for this character.
  6130. * @param sd Player
  6131. * @return Max Job Level
  6132. **/
  6133. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6134. return pc_class_maxjoblv(sd->status.class_);
  6135. }
  6136. /**
  6137. * Check if player is reached max base level
  6138. * @param sd
  6139. * @return True if reached max level
  6140. **/
  6141. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6142. nullpo_retr(false, sd);
  6143. return (sd->status.base_level >= pc_maxbaselv(sd));
  6144. }
  6145. /**
  6146. * Check if player is reached max base level
  6147. * @param sd
  6148. * @return True if reached max level
  6149. **/
  6150. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6151. nullpo_retr(false, sd);
  6152. return (sd->status.job_level >= pc_maxjoblv(sd));
  6153. }
  6154. /**
  6155. * Base exp needed for player to level up.
  6156. * @param sd
  6157. * @return Base EXP needed for next base level
  6158. **/
  6159. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6160. nullpo_ret(sd);
  6161. if (sd->status.base_level == 0) // Is this something that possible?
  6162. return 0;
  6163. if (pc_is_maxbaselv(sd))
  6164. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6165. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6166. }
  6167. /**
  6168. * Job exp needed for player to level up.
  6169. * @param sd
  6170. * @return Job EXP needed for next job level
  6171. **/
  6172. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6173. nullpo_ret(sd);
  6174. if (sd->status.job_level == 0) // Is this something that possible?
  6175. return 0;
  6176. if (pc_is_maxjoblv(sd))
  6177. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6178. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6179. }
  6180. /// Returns the value of the specified stat.
  6181. static int pc_getstat(struct map_session_data* sd, int type)
  6182. {
  6183. nullpo_retr(-1, sd);
  6184. switch( type ) {
  6185. case SP_STR: return sd->status.str;
  6186. case SP_AGI: return sd->status.agi;
  6187. case SP_VIT: return sd->status.vit;
  6188. case SP_INT: return sd->status.int_;
  6189. case SP_DEX: return sd->status.dex;
  6190. case SP_LUK: return sd->status.luk;
  6191. default:
  6192. return -1;
  6193. }
  6194. }
  6195. /// Sets the specified stat to the specified value.
  6196. /// Returns the new value.
  6197. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6198. {
  6199. nullpo_retr(-1, sd);
  6200. switch( type ) {
  6201. case SP_STR: sd->status.str = val; break;
  6202. case SP_AGI: sd->status.agi = val; break;
  6203. case SP_VIT: sd->status.vit = val; break;
  6204. case SP_INT: sd->status.int_ = val; break;
  6205. case SP_DEX: sd->status.dex = val; break;
  6206. case SP_LUK: sd->status.luk = val; break;
  6207. default:
  6208. return -1;
  6209. }
  6210. return val;
  6211. }
  6212. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6213. int pc_gets_status_point(int level)
  6214. {
  6215. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6216. return (statp[level+1] - statp[level]);
  6217. else //Default increase
  6218. return ((level+15) / 5);
  6219. }
  6220. #ifdef RENEWAL_STAT
  6221. /// Renewal status point cost formula
  6222. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6223. #else
  6224. /// Pre-Renewal status point cost formula
  6225. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6226. #endif
  6227. /// Returns the number of stat points needed to change the specified stat by val.
  6228. /// If val is negative, returns the number of stat points that would be needed to
  6229. /// raise the specified stat from (current value - val) to current value.
  6230. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6231. {
  6232. int low, high, sp = 0, max = 0;
  6233. if ( val == 0 )
  6234. return 0;
  6235. low = pc_getstat(sd,type);
  6236. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6237. if ( low >= max && val > 0 )
  6238. return 0; // Official servers show '0' when max is reached
  6239. high = low + val;
  6240. if ( val < 0 )
  6241. SWAP(low, high);
  6242. for ( ; low < high; low++ )
  6243. sp += PC_STATUS_POINT_COST(low);
  6244. return sp;
  6245. }
  6246. /**
  6247. * Returns the value the specified stat can be increased by with the current
  6248. * amount of available status points for the current character's class.
  6249. *
  6250. * @param sd The target character.
  6251. * @param type Stat to verify.
  6252. * @return Maximum value the stat could grow by.
  6253. */
  6254. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6255. {
  6256. int base, final_val, status_points, max_param;
  6257. nullpo_ret(sd);
  6258. base = final_val = pc_getstat(sd, type);
  6259. status_points = sd->status.status_point;
  6260. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6261. while (final_val <= max_param && status_points >= 0) {
  6262. status_points -= PC_STATUS_POINT_COST(final_val);
  6263. final_val++;
  6264. }
  6265. final_val--;
  6266. return (final_val > base ? final_val-base : 0);
  6267. }
  6268. /**
  6269. * Raises a stat by the specified amount.
  6270. *
  6271. * Obeys max_parameter limits.
  6272. * Subtracts status points according to the cost of the increased stat points.
  6273. *
  6274. * @param sd The target character.
  6275. * @param type The stat to change (see enum _sp)
  6276. * @param increase The stat increase (strictly positive) amount.
  6277. * @retval true if the stat was increased by any amount.
  6278. * @retval false if there were no changes.
  6279. */
  6280. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6281. {
  6282. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6283. nullpo_ret(sd);
  6284. // check conditions
  6285. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6286. clif_statusupack(sd, type, 0, 0);
  6287. return false;
  6288. }
  6289. // check limits
  6290. current = pc_getstat(sd, type);
  6291. max_increase = pc_maxparameterincrease(sd, type);
  6292. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6293. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6294. clif_statusupack(sd, type, 0, 0);
  6295. return false;
  6296. }
  6297. // check status points
  6298. needed_points = pc_need_status_point(sd, type, increase);
  6299. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6300. clif_statusupack(sd, type, 0, 0);
  6301. return false;
  6302. }
  6303. // set new values
  6304. final_value = pc_setstat(sd, type, current + increase);
  6305. sd->status.status_point -= needed_points;
  6306. status_calc_pc(sd,SCO_NONE);
  6307. // update increase cost indicator
  6308. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6309. // update statpoint count
  6310. clif_updatestatus(sd, SP_STATUSPOINT);
  6311. // update stat value
  6312. clif_statusupack(sd, type, 1, final_value); // required
  6313. if( final_value > 255 )
  6314. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6315. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6316. return true;
  6317. }
  6318. /**
  6319. * Raises a stat by the specified amount.
  6320. *
  6321. * Obeys max_parameter limits.
  6322. * Does not subtract status points for the cost of the modified stat points.
  6323. *
  6324. * @param sd The target character.
  6325. * @param type The stat to change (see enum _sp)
  6326. * @param val The stat increase (or decrease) amount.
  6327. * @return the stat increase amount.
  6328. * @retval 0 if no changes were made.
  6329. */
  6330. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6331. {
  6332. int max, need;
  6333. nullpo_ret(sd);
  6334. if( type < SP_STR || type > SP_LUK )
  6335. {
  6336. clif_statusupack(sd,type,0,0);
  6337. return 0;
  6338. }
  6339. need = pc_need_status_point(sd,type,1);
  6340. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6341. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6342. status_calc_pc(sd,SCO_NONE);
  6343. // update increase cost indicator
  6344. if( need != pc_need_status_point(sd,type,1) )
  6345. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6346. // update stat value
  6347. clif_statusupack(sd,type,1,val); // required
  6348. if( val > 255 )
  6349. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6350. return val;
  6351. }
  6352. /*==========================================
  6353. * Update skill_lv for player sd
  6354. * Skill point allocation
  6355. *------------------------------------------*/
  6356. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6357. {
  6358. uint16 idx = skill_get_index(skill_id);
  6359. nullpo_retv(sd);
  6360. if (!idx) {
  6361. if (skill_id)
  6362. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6363. return;
  6364. }
  6365. // Level up guild skill
  6366. if (SKILL_CHK_GUILD(skill_id)) {
  6367. guild_skillup(sd, skill_id);
  6368. return;
  6369. }
  6370. // Level up homunculus skill
  6371. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6372. hom_skillup(sd->hd, skill_id);
  6373. return;
  6374. }
  6375. else {
  6376. if( sd->status.skill_point > 0 &&
  6377. sd->status.skill[idx].id &&
  6378. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6379. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6380. {
  6381. int lv, range, upgradable;
  6382. sd->status.skill[idx].lv++;
  6383. sd->status.skill_point--;
  6384. if( !skill_get_inf(skill_id) )
  6385. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6386. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6387. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6388. else
  6389. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6390. lv = sd->status.skill[idx].lv;
  6391. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6392. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6393. clif_skillup(sd,skill_id,lv,range,upgradable);
  6394. clif_updatestatus(sd,SP_SKILLPOINT);
  6395. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6396. clif_updatestatus(sd,SP_CARTINFO);
  6397. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6398. clif_skillinfoblock(sd);
  6399. }
  6400. //else
  6401. // 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);
  6402. }
  6403. }
  6404. /*==========================================
  6405. * /allskill
  6406. *------------------------------------------*/
  6407. int pc_allskillup(struct map_session_data *sd)
  6408. {
  6409. int i;
  6410. nullpo_ret(sd);
  6411. for (i = 0; i < MAX_SKILL; i++) {
  6412. 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) {
  6413. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6414. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6415. if (sd->status.skill[i].lv == 0)
  6416. sd->status.skill[i].id = 0;
  6417. }
  6418. }
  6419. if (!pc_grant_allskills(sd, true)) {
  6420. uint16 sk_id;
  6421. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6422. int inf2 = 0;
  6423. uint16 sk_idx = 0;
  6424. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6425. continue;
  6426. inf2 = skill_get_inf2(sk_id);
  6427. if (
  6428. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6429. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6430. sk_id == SG_DEVIL
  6431. )
  6432. continue; //Cannot be learned normally.
  6433. sd->status.skill[sk_idx].id = sk_id;
  6434. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6435. }
  6436. }
  6437. status_calc_pc(sd,SCO_NONE);
  6438. //Required because if you could level up all skills previously,
  6439. //the update will not be sent as only the lv variable changes.
  6440. clif_skillinfoblock(sd);
  6441. return 0;
  6442. }
  6443. /*==========================================
  6444. * /resetlvl
  6445. *------------------------------------------*/
  6446. int pc_resetlvl(struct map_session_data* sd,int type)
  6447. {
  6448. int i;
  6449. nullpo_ret(sd);
  6450. if (type != 3) //Also reset skills
  6451. pc_resetskill(sd, 0);
  6452. if(type == 1){
  6453. sd->status.skill_point=0;
  6454. sd->status.base_level=1;
  6455. sd->status.job_level=1;
  6456. sd->status.base_exp=0;
  6457. sd->status.job_exp=0;
  6458. if(sd->sc.option !=0)
  6459. sd->sc.option = 0;
  6460. sd->status.str=1;
  6461. sd->status.agi=1;
  6462. sd->status.vit=1;
  6463. sd->status.int_=1;
  6464. sd->status.dex=1;
  6465. sd->status.luk=1;
  6466. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6467. sd->status.status_point=100; // not 88 [celest]
  6468. // give platinum skills upon changing
  6469. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6470. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6471. }
  6472. }
  6473. if(type == 2){
  6474. sd->status.skill_point=0;
  6475. sd->status.base_level=1;
  6476. sd->status.job_level=1;
  6477. sd->status.base_exp=0;
  6478. sd->status.job_exp=0;
  6479. }
  6480. if(type == 3){
  6481. sd->status.base_level=1;
  6482. sd->status.base_exp=0;
  6483. }
  6484. if(type == 4){
  6485. sd->status.job_level=1;
  6486. sd->status.job_exp=0;
  6487. }
  6488. clif_updatestatus(sd,SP_STATUSPOINT);
  6489. clif_updatestatus(sd,SP_STR);
  6490. clif_updatestatus(sd,SP_AGI);
  6491. clif_updatestatus(sd,SP_VIT);
  6492. clif_updatestatus(sd,SP_INT);
  6493. clif_updatestatus(sd,SP_DEX);
  6494. clif_updatestatus(sd,SP_LUK);
  6495. clif_updatestatus(sd,SP_BASELEVEL);
  6496. clif_updatestatus(sd,SP_JOBLEVEL);
  6497. clif_updatestatus(sd,SP_STATUSPOINT);
  6498. clif_updatestatus(sd,SP_BASEEXP);
  6499. clif_updatestatus(sd,SP_JOBEXP);
  6500. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6501. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6502. clif_updatestatus(sd,SP_SKILLPOINT);
  6503. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6504. clif_updatestatus(sd,SP_UAGI);
  6505. clif_updatestatus(sd,SP_UVIT);
  6506. clif_updatestatus(sd,SP_UINT);
  6507. clif_updatestatus(sd,SP_UDEX);
  6508. clif_updatestatus(sd,SP_ULUK); // End Addition
  6509. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6510. if(sd->equip_index[i] >= 0)
  6511. if(pc_isequip(sd,sd->equip_index[i]))
  6512. pc_unequipitem(sd,sd->equip_index[i],2);
  6513. }
  6514. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6515. party_send_levelup(sd);
  6516. status_calc_pc(sd, SCO_FORCE);
  6517. clif_skillinfoblock(sd);
  6518. return 0;
  6519. }
  6520. /*==========================================
  6521. * /resetstate
  6522. *------------------------------------------*/
  6523. int pc_resetstate(struct map_session_data* sd)
  6524. {
  6525. nullpo_ret(sd);
  6526. if (battle_config.use_statpoint_table)
  6527. { // New statpoint table used here - Dexity
  6528. if (sd->status.base_level > MAX_LEVEL)
  6529. { //statp[] goes out of bounds, can't reset!
  6530. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6531. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6532. return 0;
  6533. }
  6534. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6535. }
  6536. else
  6537. {
  6538. int add=0;
  6539. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6540. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6541. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6542. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6543. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6544. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6545. sd->status.status_point+=add;
  6546. }
  6547. pc_setstat(sd, SP_STR, 1);
  6548. pc_setstat(sd, SP_AGI, 1);
  6549. pc_setstat(sd, SP_VIT, 1);
  6550. pc_setstat(sd, SP_INT, 1);
  6551. pc_setstat(sd, SP_DEX, 1);
  6552. pc_setstat(sd, SP_LUK, 1);
  6553. clif_updatestatus(sd,SP_STR);
  6554. clif_updatestatus(sd,SP_AGI);
  6555. clif_updatestatus(sd,SP_VIT);
  6556. clif_updatestatus(sd,SP_INT);
  6557. clif_updatestatus(sd,SP_DEX);
  6558. clif_updatestatus(sd,SP_LUK);
  6559. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6560. clif_updatestatus(sd,SP_UAGI);
  6561. clif_updatestatus(sd,SP_UVIT);
  6562. clif_updatestatus(sd,SP_UINT);
  6563. clif_updatestatus(sd,SP_UDEX);
  6564. clif_updatestatus(sd,SP_ULUK); // End Addition
  6565. clif_updatestatus(sd,SP_STATUSPOINT);
  6566. if( sd->mission_mobid ) { //bugreport:2200
  6567. sd->mission_mobid = 0;
  6568. sd->mission_count = 0;
  6569. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6570. }
  6571. status_calc_pc(sd, SCO_NONE);
  6572. return 1;
  6573. }
  6574. /*==========================================
  6575. * /resetskill
  6576. * if flag&1, perform block resync and status_calc call.
  6577. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6578. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6579. *------------------------------------------*/
  6580. int pc_resetskill(struct map_session_data* sd, int flag)
  6581. {
  6582. int i, skill_point=0;
  6583. nullpo_ret(sd);
  6584. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6585. return 0;
  6586. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6587. /**
  6588. * 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)
  6589. **/
  6590. if( pc_is_taekwon_ranker(sd) )
  6591. return 0;
  6592. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6593. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6594. i = sd->sc.option;
  6595. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6596. i &= ~OPTION_RIDING;
  6597. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6598. i &= ~OPTION_FALCON;
  6599. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6600. i &= ~OPTION_DRAGON;
  6601. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6602. i &= ~OPTION_WUG;
  6603. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6604. i &= ~OPTION_WUGRIDER;
  6605. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6606. i &= ~OPTION_MADOGEAR;
  6607. #ifndef NEW_CARTS
  6608. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6609. i &= ~OPTION_CART;
  6610. #else
  6611. if( sd->sc.data[SC_PUSH_CART] )
  6612. pc_setcart(sd, 0);
  6613. #endif
  6614. if( i != sd->sc.option )
  6615. pc_setoption(sd, i);
  6616. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6617. hom_vaporize(sd, HOM_ST_ACTIVE);
  6618. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6619. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6620. }
  6621. for( i = 1; i < MAX_SKILL; i++ )
  6622. {
  6623. uint8 lv = sd->status.skill[i].lv;
  6624. int inf2;
  6625. uint16 skill_id = skill_idx2id(i);
  6626. if (lv == 0 || skill_id == 0)
  6627. continue;
  6628. inf2 = skill_get_inf2(skill_id);
  6629. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6630. continue;
  6631. // Don't reset trick dead if not a novice/baby
  6632. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6633. {
  6634. sd->status.skill[i].lv = 0;
  6635. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6636. continue;
  6637. }
  6638. // do not reset basic skill
  6639. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6640. continue;
  6641. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6642. continue;
  6643. if( flag&4 && !skill_ischangesex(skill_id) )
  6644. continue;
  6645. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6646. { //Only handle quest skills in a special way when you can't learn them manually
  6647. if( battle_config.quest_skill_reset && !(flag&2) )
  6648. { //Wipe them
  6649. sd->status.skill[i].lv = 0;
  6650. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6651. }
  6652. continue;
  6653. }
  6654. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6655. skill_point += lv;
  6656. else
  6657. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6658. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6659. if( !(flag&2) )
  6660. {// reset
  6661. sd->status.skill[i].lv = 0;
  6662. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6663. }
  6664. }
  6665. if( flag&2 || !skill_point ) return skill_point;
  6666. sd->status.skill_point += skill_point;
  6667. if (flag&1) {
  6668. clif_updatestatus(sd,SP_SKILLPOINT);
  6669. clif_skillinfoblock(sd);
  6670. status_calc_pc(sd, SCO_FORCE);
  6671. }
  6672. return skill_point;
  6673. }
  6674. /*==========================================
  6675. * /resetfeel [Komurka]
  6676. *------------------------------------------*/
  6677. int pc_resetfeel(struct map_session_data* sd)
  6678. {
  6679. int i;
  6680. nullpo_ret(sd);
  6681. for (i=0; i<MAX_PC_FEELHATE; i++)
  6682. {
  6683. sd->feel_map[i].m = -1;
  6684. sd->feel_map[i].index = 0;
  6685. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6686. }
  6687. return 0;
  6688. }
  6689. int pc_resethate(struct map_session_data* sd)
  6690. {
  6691. int i;
  6692. nullpo_ret(sd);
  6693. for (i=0; i<3; i++)
  6694. {
  6695. sd->hate_mob[i] = -1;
  6696. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6697. }
  6698. return 0;
  6699. }
  6700. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6701. {
  6702. int i, bonus = 0;
  6703. nullpo_ret(sd);
  6704. skill_id = skill_dummy2skill_id(skill_id);
  6705. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  6706. if( i < ARRAYLENGTH(sd->skillatk) )
  6707. bonus = sd->skillatk[i].val;
  6708. return bonus;
  6709. }
  6710. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6711. {
  6712. int i, bonus = 0;
  6713. nullpo_ret(sd);
  6714. skill_id = skill_dummy2skill_id(skill_id);
  6715. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == skill_id);
  6716. if( i < ARRAYLENGTH(sd->subskill) )
  6717. bonus = sd->subskill[i].val;
  6718. return bonus;
  6719. }
  6720. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6721. int i, bonus = sd->bonus.add_heal_rate;
  6722. skill_id = skill_dummy2skill_id(skill_id);
  6723. if( bonus ) {
  6724. switch( skill_id ) {
  6725. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6726. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6727. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6728. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6729. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6730. }
  6731. }
  6732. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  6733. if( i < ARRAYLENGTH(sd->skillheal) )
  6734. bonus += sd->skillheal[i].val;
  6735. return bonus;
  6736. }
  6737. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6738. int i, bonus = sd->bonus.add_heal2_rate;
  6739. skill_id = skill_dummy2skill_id(skill_id);
  6740. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  6741. if( i < ARRAYLENGTH(sd->skillheal2) )
  6742. bonus += sd->skillheal2[i].val;
  6743. return bonus;
  6744. }
  6745. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6746. {
  6747. if( !pc_isdead(sd) )
  6748. return; // not applicable
  6749. if( sd->bg_id && bg_member_respawn(sd) )
  6750. return; // member revived by battleground
  6751. pc_setstand(sd, true);
  6752. pc_setrestartvalue(sd,3);
  6753. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6754. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6755. }
  6756. static TIMER_FUNC(pc_respawn_timer){
  6757. struct map_session_data *sd = map_id2sd(id);
  6758. if( sd != NULL )
  6759. {
  6760. sd->pvp_point=0;
  6761. pc_respawn(sd,CLR_OUTSIGHT);
  6762. }
  6763. return 0;
  6764. }
  6765. /*==========================================
  6766. * Invoked when a player has received damage
  6767. *------------------------------------------*/
  6768. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6769. {
  6770. if (sp) clif_updatestatus(sd,SP_SP);
  6771. if (hp) clif_updatestatus(sd,SP_HP);
  6772. else return;
  6773. if (!src)
  6774. return;
  6775. if( pc_issit(sd) ) {
  6776. pc_setstand(sd, true);
  6777. skill_sit(sd,0);
  6778. }
  6779. if (sd->progressbar.npc_id)
  6780. clif_progressbar_abort(sd);
  6781. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6782. pet_target_check(sd->pd,src,1);
  6783. if( sd->status.ele_id > 0 )
  6784. elemental_set_target(sd,src);
  6785. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  6786. sd->canlog_tick = gettick();
  6787. }
  6788. TIMER_FUNC(pc_close_npc_timer){
  6789. TBL_PC *sd = map_id2sd(id);
  6790. if(sd) pc_close_npc(sd,data);
  6791. return 0;
  6792. }
  6793. /**
  6794. * Method to properly close a NPC for player and clear anything related.
  6795. * @param sd: Player attached
  6796. * @param flag: Method of closure
  6797. * 1: Produce a close button and end the NPC
  6798. * 2: End the NPC (best for no dialog windows)
  6799. */
  6800. void pc_close_npc(struct map_session_data *sd,int flag)
  6801. {
  6802. nullpo_retv(sd);
  6803. if (sd->npc_id || sd->npc_shopid) {
  6804. if (sd->state.using_fake_npc) {
  6805. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6806. sd->state.using_fake_npc = 0;
  6807. }
  6808. if (sd->st) {
  6809. if(sd->st->state == RUN){ //wait ending code execution
  6810. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6811. return;
  6812. }
  6813. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6814. sd->st->mes_active = 0;
  6815. }
  6816. sd->state.menu_or_input = 0;
  6817. sd->npc_menu = 0;
  6818. sd->npc_shopid = 0;
  6819. #ifdef SECURE_NPCTIMEOUT
  6820. sd->npc_idle_timer = INVALID_TIMER;
  6821. #endif
  6822. if (sd->st) {
  6823. if (sd->st->state == CLOSE) {
  6824. clif_scriptclose(sd, sd->npc_id);
  6825. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  6826. sd->st->state = END; // Force to end now
  6827. }
  6828. if (sd->st->state == END) { // free attached scripts that are waiting
  6829. script_free_state(sd->st);
  6830. sd->st = NULL;
  6831. sd->npc_id = 0;
  6832. }
  6833. }
  6834. }
  6835. }
  6836. /*==========================================
  6837. * Invoked when a player has negative current hp
  6838. *------------------------------------------*/
  6839. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6840. {
  6841. int i=0,k=0;
  6842. unsigned int tick = gettick();
  6843. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6844. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6845. // Super Novices have no kill or die functions attached when saved by their angel
  6846. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6847. unsigned int exp = pc_nextbaseexp(sd);
  6848. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6849. sd->state.snovice_dead_flag = 1;
  6850. pc_setrestartvalue(sd,1);
  6851. status_percent_heal(&sd->bl, 100, 100);
  6852. clif_resurrection(&sd->bl, 1);
  6853. if(battle_config.pc_invincible_time)
  6854. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6855. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6856. if(mapdata_flag_gvg2(mapdata))
  6857. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6858. return 0;
  6859. }
  6860. }
  6861. for(k = 0; k < MAX_DEVOTION; k++) {
  6862. if (sd->devotion[k]){
  6863. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6864. if (devsd)
  6865. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6866. sd->devotion[k] = 0;
  6867. }
  6868. }
  6869. if(sd->shadowform_id) { //if we were target of shadowform
  6870. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6871. sd->shadowform_id = 0; //should be remove on status end anyway
  6872. }
  6873. if(sd->status.pet_id > 0 && sd->pd) {
  6874. struct pet_data *pd = sd->pd;
  6875. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  6876. pet_set_intimate(pd, pd->pet.intimate - pd->get_pet_db()->die);
  6877. if( pd->pet.intimate < 0 )
  6878. pd->pet.intimate = 0;
  6879. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  6880. }
  6881. if( sd->pd->target_id ) // Unlock all targets...
  6882. pet_unlocktarget(sd->pd);
  6883. }
  6884. 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)
  6885. hom_vaporize(sd, HOM_ST_ACTIVE);
  6886. if( sd->md )
  6887. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  6888. if( sd->ed )
  6889. elemental_delete(sd->ed);
  6890. // Leave duel if you die [LuzZza]
  6891. if(battle_config.duel_autoleave_when_die) {
  6892. if(sd->duel_group > 0)
  6893. duel_leave(sd->duel_group, sd);
  6894. if(sd->duel_invite > 0)
  6895. duel_reject(sd->duel_invite, sd);
  6896. }
  6897. pc_close_npc(sd,2); //close npc if we were using one
  6898. /* e.g. not killed thru pc_damage */
  6899. if( pc_issit(sd) ) {
  6900. clif_status_load(&sd->bl,EFST_SIT,0);
  6901. }
  6902. pc_setdead(sd);
  6903. clif_party_dead( sd );
  6904. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
  6905. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  6906. //Reset menu skills/item skills
  6907. if ((sd->skillitem) != 0)
  6908. sd->skillitem = sd->skillitemlv = 0;
  6909. if ((sd->menuskill_id) != 0)
  6910. sd->menuskill_id = sd->menuskill_val = 0;
  6911. //Reset ticks.
  6912. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  6913. if ( sd->spiritball !=0 )
  6914. pc_delspiritball(sd,sd->spiritball,0);
  6915. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  6916. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  6917. if (src)
  6918. switch (src->type) {
  6919. case BL_MOB:
  6920. {
  6921. struct mob_data *md=(struct mob_data *)src;
  6922. if(md->target_id==sd->bl.id)
  6923. mob_unlocktarget(md,tick);
  6924. if(battle_config.mobs_level_up && md->status.hp &&
  6925. (unsigned int)md->level < pc_maxbaselv(sd) &&
  6926. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  6927. ) { // monster level up [Valaris]
  6928. clif_misceffect(&md->bl,0);
  6929. md->level++;
  6930. status_calc_mob(md, SCO_NONE);
  6931. status_percent_heal(src,10,0);
  6932. if( battle_config.show_mob_info&4 )
  6933. {// update name with new level
  6934. clif_name_area(&md->bl);
  6935. }
  6936. }
  6937. src = battle_get_master(src); // Maybe Player Summon
  6938. }
  6939. break;
  6940. case BL_PET: //Pass on to master...
  6941. case BL_HOM:
  6942. case BL_MER:
  6943. src = battle_get_master(src);
  6944. break;
  6945. }
  6946. if (src && src->type == BL_PC) {
  6947. struct map_session_data *ssd = (struct map_session_data *)src;
  6948. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  6949. npc_script_event(ssd, NPCE_KILLPC);
  6950. if (battle_config.pk_mode&2) {
  6951. ssd->status.manner -= 5;
  6952. if(ssd->status.manner < 0)
  6953. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6954. #if 0
  6955. // PK/Karma system code (not enabled yet) [celest]
  6956. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6957. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6958. // karma going down = more 'good' / more honourable.
  6959. // The Karma System way...
  6960. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6961. sd->status.karma--;
  6962. ssd->status.karma--;
  6963. }
  6964. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6965. ssd->status.karma++;
  6966. // or the PK System way...
  6967. if (sd->status.karma > 0) // player killed is dishonourable?
  6968. ssd->status.karma--; // honour points earned
  6969. sd->status.karma++; // honour points lost
  6970. // To-do: Receive exp on certain occasions
  6971. #endif
  6972. }
  6973. }
  6974. if(battle_config.bone_drop==2
  6975. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  6976. {
  6977. struct item item_tmp;
  6978. memset(&item_tmp,0,sizeof(item_tmp));
  6979. item_tmp.nameid=ITEMID_SKULL_;
  6980. item_tmp.identify=1;
  6981. item_tmp.card[0]=CARD0_CREATE;
  6982. item_tmp.card[1]=0;
  6983. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6984. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6985. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6986. }
  6987. //Remove bonus_script when dead
  6988. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  6989. // changed penalty options, added death by player if pk_mode [Valaris]
  6990. if(battle_config.death_penalty_type
  6991. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6992. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  6993. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  6994. {
  6995. uint32 base_penalty = 0;
  6996. uint32 job_penalty = 0;
  6997. uint32 zeny_penalty = 0;
  6998. if (pc_isvip(sd)) { // EXP penalty for VIP
  6999. base_penalty = battle_config.vip_exp_penalty_base;
  7000. job_penalty = battle_config.vip_exp_penalty_job;
  7001. zeny_penalty = battle_config.vip_zeny_penalty;
  7002. } else {
  7003. base_penalty = battle_config.death_penalty_base;
  7004. job_penalty = battle_config.death_penalty_job;
  7005. zeny_penalty = battle_config.zeny_penalty;
  7006. }
  7007. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7008. switch (battle_config.death_penalty_type) {
  7009. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7010. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  7011. }
  7012. if (base_penalty){ //recheck after altering to speedup
  7013. if (battle_config.pk_mode && src && src->type==BL_PC)
  7014. base_penalty *= 2;
  7015. base_penalty = u32min(sd->status.base_exp, base_penalty);
  7016. }
  7017. }
  7018. else
  7019. base_penalty = 0;
  7020. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7021. switch (battle_config.death_penalty_type) {
  7022. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7023. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7024. }
  7025. if (job_penalty) {
  7026. if (battle_config.pk_mode && src && src->type==BL_PC)
  7027. job_penalty *= 2;
  7028. job_penalty = u32min(sd->status.job_exp, job_penalty);
  7029. }
  7030. }
  7031. else
  7032. job_penalty = 0;
  7033. if (base_penalty || job_penalty)
  7034. pc_lostexp(sd, base_penalty, job_penalty);
  7035. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  7036. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7037. if(zeny_penalty)
  7038. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7039. }
  7040. }
  7041. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7042. for (const auto &it : mapdata->drop_list) {
  7043. int id = it.drop_id, per = it.drop_per;
  7044. enum e_nightmare_drop_type type = it.drop_type;
  7045. if(id == 0)
  7046. continue;
  7047. if(id == -1){
  7048. int eq_num=0,eq_n[MAX_INVENTORY];
  7049. memset(eq_n,0,sizeof(eq_n));
  7050. for(i=0;i<MAX_INVENTORY;i++) {
  7051. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7052. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7053. || type&NMDT_ALL)
  7054. {
  7055. int l;
  7056. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7057. if( l < MAX_INVENTORY )
  7058. eq_n[l] = i;
  7059. eq_num++;
  7060. }
  7061. }
  7062. if(eq_num > 0){
  7063. int n = eq_n[rnd()%eq_num];
  7064. if(rnd()%10000 < per) {
  7065. if(sd->inventory.u.items_inventory[n].equip)
  7066. pc_unequipitem(sd,n,3);
  7067. pc_dropitem(sd,n,1);
  7068. }
  7069. }
  7070. }
  7071. else if(id > 0) {
  7072. for(i=0;i<MAX_INVENTORY;i++){
  7073. if(sd->inventory.u.items_inventory[i].nameid == id
  7074. && rnd()%10000 < per
  7075. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7076. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7077. || type&NMDT_ALL) ){
  7078. if(sd->inventory.u.items_inventory[i].equip)
  7079. pc_unequipitem(sd,i,3);
  7080. pc_dropitem(sd,i,1);
  7081. break;
  7082. }
  7083. }
  7084. }
  7085. }
  7086. }
  7087. // pvp
  7088. // disable certain pvp functions on pk_mode [Valaris]
  7089. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  7090. sd->pvp_point -= 5;
  7091. sd->pvp_lost++;
  7092. if( src && src->type == BL_PC ) {
  7093. struct map_session_data *ssd = (struct map_session_data *)src;
  7094. ssd->pvp_point++;
  7095. ssd->pvp_won++;
  7096. }
  7097. if( sd->pvp_point < 0 ) {
  7098. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7099. return 1|8;
  7100. }
  7101. }
  7102. //GvG
  7103. if( mapdata_flag_gvg2(mapdata) ) {
  7104. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7105. return 1|8;
  7106. }
  7107. else if( sd->bg_id ) {
  7108. struct battleground_data *bg = bg_team_search(sd->bg_id);
  7109. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  7110. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7111. return 1|8;
  7112. }
  7113. }
  7114. //Reset "can log out" tick.
  7115. if( battle_config.prevent_logout )
  7116. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7117. return 1;
  7118. }
  7119. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7120. if(hp) clif_updatestatus(sd,SP_HP);
  7121. if(sp) clif_updatestatus(sd,SP_SP);
  7122. pc_setstand(sd, true);
  7123. if(battle_config.pc_invincible_time > 0)
  7124. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7125. if( sd->state.gmaster_flag ) {
  7126. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7127. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7128. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7129. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7130. }
  7131. }
  7132. // script
  7133. //
  7134. /*==========================================
  7135. * script reading pc status registry
  7136. *------------------------------------------*/
  7137. int pc_readparam(struct map_session_data* sd,int type)
  7138. {
  7139. int val = 0;
  7140. nullpo_ret(sd);
  7141. switch(type) {
  7142. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7143. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7144. case SP_ZENY: val = sd->status.zeny; break;
  7145. case SP_BASELEVEL: val = sd->status.base_level; break;
  7146. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7147. case SP_CLASS: val = sd->status.class_; break;
  7148. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7149. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7150. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7151. case SP_SEX: val = sd->status.sex; break;
  7152. case SP_WEIGHT: val = sd->weight; break;
  7153. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7154. case SP_BASEEXP: val = sd->status.base_exp; break;
  7155. case SP_JOBEXP: val = sd->status.job_exp; break;
  7156. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7157. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7158. case SP_HP: val = sd->battle_status.hp; break;
  7159. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7160. case SP_SP: val = sd->battle_status.sp; break;
  7161. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7162. case SP_STR: val = sd->status.str; break;
  7163. case SP_AGI: val = sd->status.agi; break;
  7164. case SP_VIT: val = sd->status.vit; break;
  7165. case SP_INT: val = sd->status.int_; break;
  7166. case SP_DEX: val = sd->status.dex; break;
  7167. case SP_LUK: val = sd->status.luk; break;
  7168. case SP_KARMA: val = sd->status.karma; break;
  7169. case SP_MANNER: val = sd->status.manner; break;
  7170. case SP_FAME: val = sd->status.fame; break;
  7171. case SP_KILLERRID: val = sd->killerrid; break;
  7172. case SP_KILLEDRID: val = sd->killedrid; break;
  7173. case SP_KILLEDGID: val = sd->killedgid; break;
  7174. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7175. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7176. case SP_CHARRENAME: val = sd->status.rename; break;
  7177. case SP_CHARFONT: val = sd->status.font; break;
  7178. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7179. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7180. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7181. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7182. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7183. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7184. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7185. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7186. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7187. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7188. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  7189. case SP_DEF1: val = sd->battle_status.def; break;
  7190. case SP_DEF2: val = sd->battle_status.def2; break;
  7191. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7192. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7193. case SP_HIT: val = sd->battle_status.hit; break;
  7194. case SP_FLEE1: val = sd->battle_status.flee; break;
  7195. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7196. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7197. case SP_MAXHPRATE: val = sd->hprate; break;
  7198. case SP_MAXSPRATE: val = sd->sprate; break;
  7199. case SP_SPRATE: val = sd->dsprate; break;
  7200. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7201. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7202. case SP_ASPD_RATE:
  7203. #ifndef RENEWAL_ASPD
  7204. val = sd->battle_status.aspd_rate;
  7205. #else
  7206. val = sd->battle_status.aspd_rate2;
  7207. #endif
  7208. break;
  7209. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7210. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7211. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7212. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7213. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7214. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7215. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7216. case SP_MATK_RATE: val = sd->matk_rate; break;
  7217. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7218. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7219. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7220. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7221. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7222. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7223. case SP_HIT_RATE: val = sd->hit_rate; break;
  7224. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7225. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7226. case SP_DEF_RATE: val = sd->def_rate; break;
  7227. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7228. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7229. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7230. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7231. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7232. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7233. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7234. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7235. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7236. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7237. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7238. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7239. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7240. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7241. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7242. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7243. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7244. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7245. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7246. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7247. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7248. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7249. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7250. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7251. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7252. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7253. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7254. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7255. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7256. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7257. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7258. case SP_DELAYRATE: val = sd->delayrate; break;
  7259. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7260. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7261. case SP_UNSTRIPABLE:
  7262. case SP_UNSTRIPABLE_ARMOR:
  7263. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7264. break;
  7265. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7266. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7267. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7268. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7269. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7270. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7271. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7272. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7273. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7274. case SP_EMATK: val = sd->bonus.ematk; break;
  7275. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7276. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7277. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7278. case SP_CASTRATE:
  7279. case SP_VARCASTRATE:
  7280. #ifdef RENEWAL_CAST
  7281. val = sd->bonus.varcastrate; break;
  7282. #else
  7283. val = sd->castrate; break;
  7284. #endif
  7285. default:
  7286. ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
  7287. return -1;
  7288. }
  7289. return val;
  7290. }
  7291. /*==========================================
  7292. * script set pc status registry
  7293. *------------------------------------------*/
  7294. bool pc_setparam(struct map_session_data *sd,int type,int val)
  7295. {
  7296. nullpo_retr(false,sd);
  7297. switch(type){
  7298. case SP_BASELEVEL:
  7299. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  7300. val = pc_maxbaselv(sd);
  7301. if ((unsigned int)val > sd->status.base_level) {
  7302. int i = 0;
  7303. int stat=0;
  7304. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  7305. stat += pc_gets_status_point(sd->status.base_level + i);
  7306. sd->status.status_point += stat;
  7307. }
  7308. sd->status.base_level = (unsigned int)val;
  7309. sd->status.base_exp = 0;
  7310. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7311. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7312. clif_updatestatus(sd, SP_STATUSPOINT);
  7313. clif_updatestatus(sd, SP_BASEEXP);
  7314. status_calc_pc(sd, SCO_FORCE);
  7315. if(sd->status.party_id)
  7316. party_send_levelup(sd);
  7317. break;
  7318. case SP_JOBLEVEL:
  7319. if ((unsigned int)val >= sd->status.job_level) {
  7320. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7321. sd->status.skill_point += val - sd->status.job_level;
  7322. clif_updatestatus(sd, SP_SKILLPOINT);
  7323. }
  7324. sd->status.job_level = (unsigned int)val;
  7325. sd->status.job_exp = 0;
  7326. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7327. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7328. clif_updatestatus(sd, SP_JOBEXP);
  7329. status_calc_pc(sd, SCO_FORCE);
  7330. break;
  7331. case SP_SKILLPOINT:
  7332. sd->status.skill_point = val;
  7333. break;
  7334. case SP_STATUSPOINT:
  7335. sd->status.status_point = val;
  7336. break;
  7337. case SP_ZENY:
  7338. if( val < 0 )
  7339. return false;// can't set negative zeny
  7340. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7341. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7342. break;
  7343. case SP_BASEEXP:
  7344. {
  7345. val = cap_value(val, 0, INT_MAX);
  7346. if ((unsigned int)val < sd->status.base_exp) // Lost
  7347. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7348. else // Gained
  7349. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7350. }
  7351. return true;
  7352. case SP_JOBEXP:
  7353. {
  7354. val = cap_value(val, 0, INT_MAX);
  7355. if ((unsigned int)val < sd->status.job_exp) // Lost
  7356. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7357. else // Gained
  7358. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7359. }
  7360. return true;
  7361. case SP_SEX:
  7362. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7363. break;
  7364. case SP_WEIGHT:
  7365. sd->weight = val;
  7366. break;
  7367. case SP_MAXWEIGHT:
  7368. sd->max_weight = val;
  7369. break;
  7370. case SP_HP:
  7371. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7372. break;
  7373. case SP_MAXHP:
  7374. if (sd->status.base_level < 100)
  7375. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7376. else if (sd->status.base_level < 151)
  7377. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7378. else
  7379. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7380. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7381. {
  7382. sd->battle_status.hp = sd->battle_status.max_hp;
  7383. clif_updatestatus(sd, SP_HP);
  7384. }
  7385. break;
  7386. case SP_SP:
  7387. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7388. break;
  7389. case SP_MAXSP:
  7390. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7391. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7392. {
  7393. sd->battle_status.sp = sd->battle_status.max_sp;
  7394. clif_updatestatus(sd, SP_SP);
  7395. }
  7396. break;
  7397. case SP_STR:
  7398. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7399. break;
  7400. case SP_AGI:
  7401. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7402. break;
  7403. case SP_VIT:
  7404. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7405. break;
  7406. case SP_INT:
  7407. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7408. break;
  7409. case SP_DEX:
  7410. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7411. break;
  7412. case SP_LUK:
  7413. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7414. break;
  7415. case SP_KARMA:
  7416. sd->status.karma = val;
  7417. break;
  7418. case SP_MANNER:
  7419. sd->status.manner = val;
  7420. if( val < 0 )
  7421. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7422. else {
  7423. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7424. clif_manner_message(sd, 5);
  7425. }
  7426. return true; // status_change_start/status_change_end already sends packets warning the client
  7427. case SP_FAME:
  7428. sd->status.fame = val;
  7429. break;
  7430. case SP_KILLERRID:
  7431. sd->killerrid = val;
  7432. return true;
  7433. case SP_KILLEDRID:
  7434. sd->killedrid = val;
  7435. return true;
  7436. case SP_KILLEDGID:
  7437. sd->killedgid = val;
  7438. return true;
  7439. case SP_CHARMOVE:
  7440. sd->status.character_moves = val;
  7441. return true;
  7442. case SP_CHARRENAME:
  7443. sd->status.rename = val;
  7444. return true;
  7445. case SP_CHARFONT:
  7446. sd->status.font = val;
  7447. clif_font(sd);
  7448. return true;
  7449. case SP_BANK_VAULT:
  7450. if (val < 0)
  7451. return false;
  7452. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7453. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7454. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7455. return true;
  7456. case SP_ROULETTE_BRONZE:
  7457. sd->roulette_point.bronze = val;
  7458. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7459. return true;
  7460. case SP_ROULETTE_SILVER:
  7461. sd->roulette_point.silver = val;
  7462. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7463. return true;
  7464. case SP_ROULETTE_GOLD:
  7465. sd->roulette_point.gold = val;
  7466. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7467. return true;
  7468. case SP_CASHPOINTS:
  7469. if (val < 0)
  7470. return false;
  7471. if (!sd->state.connect_new)
  7472. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7473. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7474. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7475. return true;
  7476. case SP_KAFRAPOINTS:
  7477. if (val < 0)
  7478. return false;
  7479. if (!sd->state.connect_new)
  7480. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7481. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7482. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7483. return true;
  7484. case SP_PCDIECOUNTER:
  7485. if (val < 0)
  7486. return false;
  7487. if (sd->die_counter == val)
  7488. return true;
  7489. sd->die_counter = val;
  7490. if (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7491. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7492. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7493. return true;
  7494. case SP_COOKMASTERY:
  7495. if (val < 0)
  7496. return false;
  7497. if (sd->cook_mastery == val)
  7498. return true;
  7499. val = cap_value(val, 0, 1999);
  7500. sd->cook_mastery = val;
  7501. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7502. return true;
  7503. default:
  7504. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7505. return false;
  7506. }
  7507. clif_updatestatus(sd,type);
  7508. return true;
  7509. }
  7510. /*==========================================
  7511. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7512. *------------------------------------------*/
  7513. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7514. {
  7515. if (type&2) {
  7516. if (hp || type&4)
  7517. clif_heal(sd->fd,SP_HP,hp);
  7518. if (sp)
  7519. clif_heal(sd->fd,SP_SP,sp);
  7520. } else {
  7521. if(hp)
  7522. clif_updatestatus(sd,SP_HP);
  7523. if(sp)
  7524. clif_updatestatus(sd,SP_SP);
  7525. }
  7526. return;
  7527. }
  7528. /**
  7529. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7530. * @param sd: Player data
  7531. * @param itemid: Item ID
  7532. * @param hp: HP to heal
  7533. * @param sp: SP to heal
  7534. * @return Amount healed to an object
  7535. */
  7536. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7537. {
  7538. int bonus, tmp, penalty = 0;
  7539. if (hp) {
  7540. int i;
  7541. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7542. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7543. if (potion_flag == 2) {
  7544. bonus += 50;
  7545. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7546. bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
  7547. }
  7548. //All item bonuses.
  7549. bonus += sd->bonus.itemhealrate2;
  7550. //Item Group bonuses
  7551. bonus += pc_get_itemgroup_bonus(sd, itemid);
  7552. //Individual item bonuses.
  7553. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++) {
  7554. if (sd->itemhealrate[i].nameid == itemid) {
  7555. bonus += sd->itemhealrate[i].rate;
  7556. break;
  7557. }
  7558. }
  7559. // Recovery Potion
  7560. if (sd->sc.data[SC_INCHEALRATE])
  7561. bonus += sd->sc.data[SC_INCHEALRATE]->val1;
  7562. // 2014 Halloween Event : Pumpkin Bonus
  7563. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7564. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
  7565. tmp = hp * bonus / 100; // Overflow check
  7566. if (bonus != 100 && tmp > hp)
  7567. hp = tmp;
  7568. }
  7569. if (sp) {
  7570. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7571. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7572. if (potion_flag == 2)
  7573. bonus += 50;
  7574. tmp = sp * bonus / 100; // Overflow check
  7575. if (bonus != 100 && tmp > sp)
  7576. sp = tmp;
  7577. }
  7578. if (sd->sc.count) {
  7579. // Critical Wound and Death Hurt stack
  7580. if (sd->sc.data[SC_CRITICALWOUND])
  7581. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7582. if (sd->sc.data[SC_DEATHHURT])
  7583. penalty += 20;
  7584. if (sd->sc.data[SC_NORECOVER_STATE])
  7585. penalty = 100;
  7586. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7587. hp += hp / 2; // 1.5 times
  7588. sp -= sp / 2;
  7589. }
  7590. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7591. hp += hp / 10;
  7592. sp += sp / 10;
  7593. }
  7594. if (penalty > 0) {
  7595. hp -= hp * penalty / 100;
  7596. sp -= sp * penalty / 100;
  7597. }
  7598. #ifdef RENEWAL
  7599. if (sd->sc.data[SC_EXTREMITYFIST2])
  7600. sp = 0;
  7601. #endif
  7602. if (sd->sc.data[SC_BITESCAR])
  7603. hp = 0;
  7604. }
  7605. return status_heal(&sd->bl, hp, sp, 1);
  7606. }
  7607. /*==========================================
  7608. * HP/SP Recovery
  7609. * Heal player hp nad/or sp by rate
  7610. *------------------------------------------*/
  7611. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7612. {
  7613. nullpo_ret(sd);
  7614. if (hp > 100) hp = 100;
  7615. else if (hp <-100) hp = -100;
  7616. if (sp > 100) sp = 100;
  7617. else if (sp <-100) sp = -100;
  7618. if(hp >= 0 && sp >= 0) //Heal
  7619. return status_percent_heal(&sd->bl, hp, sp);
  7620. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7621. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7622. //Crossed signs
  7623. if(hp) {
  7624. if(hp > 0)
  7625. status_percent_heal(&sd->bl, hp, 0);
  7626. else
  7627. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7628. }
  7629. if(sp) {
  7630. if(sp > 0)
  7631. status_percent_heal(&sd->bl, 0, sp);
  7632. else
  7633. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7634. }
  7635. return 0;
  7636. }
  7637. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7638. {
  7639. struct mob_data *md;
  7640. int flag;
  7641. md = (struct mob_data *)bl;
  7642. nullpo_ret(md);
  7643. flag = va_arg(ap, int);
  7644. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7645. status_kill(&md->bl);
  7646. return 1;
  7647. }
  7648. /**
  7649. * Called when player changes job
  7650. * Rewrote to make it tidider [Celest]
  7651. * @param sd
  7652. * @param job JOB ID. See enum e_job
  7653. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7654. * @return True if success, false if failed
  7655. **/
  7656. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7657. {
  7658. int i, fame_flag = 0;
  7659. int b_class;
  7660. nullpo_retr(false,sd);
  7661. if (job < 0)
  7662. return false;
  7663. //Normalize job.
  7664. b_class = pc_jobid2mapid(job);
  7665. if (b_class == -1)
  7666. return false;
  7667. switch (upper) {
  7668. case 1:
  7669. b_class|= JOBL_UPPER;
  7670. break;
  7671. case 2:
  7672. b_class|= JOBL_BABY;
  7673. break;
  7674. }
  7675. //This will automatically adjust bard/dancer classes to the correct gender
  7676. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7677. job = pc_mapid2jobid(b_class, sd->status.sex);
  7678. if (job == -1)
  7679. return false;
  7680. if ((unsigned short)b_class == sd->class_)
  7681. return false; //Nothing to change.
  7682. // changing from 1st to 2nd job
  7683. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7684. sd->change_level_2nd = sd->status.job_level;
  7685. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  7686. }
  7687. // changing from 2nd to 3rd job
  7688. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7689. sd->change_level_3rd = sd->status.job_level;
  7690. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  7691. }
  7692. if(sd->cloneskill_idx > 0) {
  7693. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7694. sd->status.skill[sd->cloneskill_idx].id = 0;
  7695. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7696. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7697. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  7698. }
  7699. sd->cloneskill_idx = 0;
  7700. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7701. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7702. }
  7703. if(sd->reproduceskill_idx > 0) {
  7704. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7705. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7706. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7707. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7708. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  7709. }
  7710. sd->reproduceskill_idx = 0;
  7711. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7712. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7713. }
  7714. // Give or reduce transcendent status points
  7715. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7716. sd->status.status_point += battle_config.transcendent_status_points;
  7717. clif_updatestatus(sd,SP_STATUSPOINT);
  7718. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7719. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7720. // The player already used his bonus points, so we have to reset his status points
  7721. pc_resetstate(sd);
  7722. }
  7723. sd->status.status_point -= battle_config.transcendent_status_points;
  7724. clif_updatestatus(sd,SP_STATUSPOINT);
  7725. }
  7726. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7727. const int class_ = pc_class2idx(sd->status.class_);
  7728. uint16 skill_id;
  7729. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7730. //Remove status specific to your current tree skills.
  7731. enum sc_type sc = status_skill2sc(skill_id);
  7732. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7733. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7734. }
  7735. }
  7736. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7737. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7738. pc_resetfeel(sd);
  7739. }
  7740. // Reset body style to 0 before changing job to avoid
  7741. // errors since not every job has a alternate outfit.
  7742. sd->status.body = 0;
  7743. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7744. sd->status.class_ = job;
  7745. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7746. sd->class_ = (unsigned short)b_class;
  7747. sd->status.job_level=1;
  7748. sd->status.job_exp=0;
  7749. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7750. sd->status.base_level = pc_maxbaselv(sd);
  7751. sd->status.base_exp=0;
  7752. pc_resetstate(sd);
  7753. clif_updatestatus(sd,SP_STATUSPOINT);
  7754. clif_updatestatus(sd,SP_BASELEVEL);
  7755. clif_updatestatus(sd,SP_BASEEXP);
  7756. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7757. }
  7758. clif_updatestatus(sd,SP_JOBLEVEL);
  7759. clif_updatestatus(sd,SP_JOBEXP);
  7760. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7761. for(i=0;i<EQI_MAX;i++) {
  7762. if(sd->equip_index[i] >= 0)
  7763. if(pc_isequip(sd,sd->equip_index[i]))
  7764. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7765. }
  7766. //Change look, if disguised, you need to undisguise
  7767. //to correctly calculate new job sprite without
  7768. if (sd->disguise)
  7769. pc_disguise(sd, 0);
  7770. status_set_viewdata(&sd->bl, job);
  7771. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7772. #if PACKETVER >= 20151001
  7773. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7774. #endif
  7775. if(sd->vd.cloth_color)
  7776. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7777. /*
  7778. if(sd->vd.body_style)
  7779. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7780. */
  7781. //Update skill tree.
  7782. pc_calc_skilltree(sd);
  7783. clif_skillinfoblock(sd);
  7784. if (sd->ed)
  7785. elemental_delete(sd->ed);
  7786. if (sd->state.vending)
  7787. vending_closevending(sd);
  7788. if (sd->state.buyingstore)
  7789. buyingstore_close(sd);
  7790. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7791. //Remove peco/cart/falcon
  7792. i = sd->sc.option;
  7793. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7794. i&=~OPTION_RIDING;
  7795. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7796. i&=~OPTION_FALCON;
  7797. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7798. i&=~OPTION_DRAGON;
  7799. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7800. i&=~OPTION_WUGRIDER;
  7801. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7802. i&=~OPTION_WUG;
  7803. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7804. i&=~OPTION_MADOGEAR;
  7805. #ifndef NEW_CARTS
  7806. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7807. i&=~OPTION_CART;
  7808. #else
  7809. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7810. pc_setcart(sd, 0);
  7811. #endif
  7812. if(i != sd->sc.option)
  7813. pc_setoption(sd, i);
  7814. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7815. hom_vaporize(sd, HOM_ST_ACTIVE);
  7816. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7817. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7818. if(sd->status.manner < 0)
  7819. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7820. status_calc_pc(sd,SCO_FORCE);
  7821. pc_checkallowskill(sd);
  7822. pc_equiplookall(sd);
  7823. pc_show_questinfo(sd);
  7824. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  7825. if( sd->status.party_id ){
  7826. struct party_data* p;
  7827. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  7828. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  7829. if( i < MAX_PARTY ){
  7830. p->party.member[i].class_ = sd->status.class_;
  7831. clif_party_job_and_level(sd);
  7832. }
  7833. }
  7834. }
  7835. chrif_save(sd, CSAVE_NORMAL);
  7836. //if you were previously famous, not anymore.
  7837. if (fame_flag)
  7838. chrif_buildfamelist();
  7839. else if (sd->status.fame > 0) {
  7840. //It may be that now they are famous?
  7841. switch (sd->class_&MAPID_UPPERMASK) {
  7842. case MAPID_BLACKSMITH:
  7843. case MAPID_ALCHEMIST:
  7844. case MAPID_TAEKWON:
  7845. chrif_buildfamelist();
  7846. break;
  7847. }
  7848. }
  7849. return true;
  7850. }
  7851. /*==========================================
  7852. * Tell client player sd has change equipement
  7853. *------------------------------------------*/
  7854. void pc_equiplookall(struct map_session_data *sd)
  7855. {
  7856. nullpo_retv(sd);
  7857. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  7858. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7859. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7860. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7861. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7862. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  7863. }
  7864. /*==========================================
  7865. * Tell client player sd has change look (hair,equip...)
  7866. *------------------------------------------*/
  7867. void pc_changelook(struct map_session_data *sd,int type,int val) {
  7868. nullpo_retv(sd);
  7869. switch(type) {
  7870. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  7871. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  7872. if (sd->status.hair != val) {
  7873. sd->status.hair = val;
  7874. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7875. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7876. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  7877. }
  7878. break;
  7879. case LOOK_WEAPON:
  7880. sd->status.weapon = val;
  7881. break;
  7882. case LOOK_HEAD_BOTTOM:
  7883. sd->status.head_bottom = val;
  7884. sd->setlook_head_bottom = val;
  7885. break;
  7886. case LOOK_HEAD_TOP:
  7887. sd->status.head_top = val;
  7888. sd->setlook_head_top = val;
  7889. break;
  7890. case LOOK_HEAD_MID:
  7891. sd->status.head_mid = val;
  7892. sd->setlook_head_mid = val;
  7893. break;
  7894. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  7895. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  7896. if (sd->status.hair_color != val) {
  7897. sd->status.hair_color = val;
  7898. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7899. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7900. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  7901. }
  7902. break;
  7903. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  7904. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  7905. sd->status.clothes_color = val;
  7906. break;
  7907. case LOOK_SHIELD:
  7908. sd->status.shield = val;
  7909. break;
  7910. case LOOK_SHOES:
  7911. break;
  7912. case LOOK_ROBE:
  7913. sd->status.robe = val;
  7914. sd->setlook_robe = val;
  7915. break;
  7916. case LOOK_BODY2:
  7917. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  7918. sd->status.body = val;
  7919. break;
  7920. }
  7921. clif_changelook(&sd->bl, type, val);
  7922. }
  7923. /*==========================================
  7924. * Give an option (type) to player (sd) and display it to client
  7925. *------------------------------------------*/
  7926. void pc_setoption(struct map_session_data *sd,int type)
  7927. {
  7928. int p_type, new_look=0;
  7929. nullpo_retv(sd);
  7930. p_type = sd->sc.option;
  7931. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  7932. sd->sc.option=type;
  7933. clif_changeoption(&sd->bl);
  7934. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7935. { // Mounting
  7936. clif_status_load(&sd->bl,EFST_RIDING,1);
  7937. status_calc_pc(sd,SCO_NONE);
  7938. }
  7939. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7940. { // Dismount
  7941. clif_status_load(&sd->bl,EFST_RIDING,0);
  7942. status_calc_pc(sd,SCO_NONE);
  7943. }
  7944. #ifndef NEW_CARTS
  7945. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  7946. clif_cartlist(sd);
  7947. clif_updatestatus(sd, SP_CARTINFO);
  7948. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7949. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  7950. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  7951. clif_clearcart(sd->fd);
  7952. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7953. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  7954. }
  7955. #endif
  7956. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  7957. clif_status_load(&sd->bl,EFST_FALCON,1);
  7958. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  7959. clif_status_load(&sd->bl,EFST_FALCON,0);
  7960. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  7961. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  7962. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  7963. status_calc_pc(sd,SCO_NONE);
  7964. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  7965. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  7966. status_calc_pc(sd,SCO_NONE);
  7967. }
  7968. }
  7969. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  7970. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  7971. status_calc_pc(sd,SCO_NONE);
  7972. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  7973. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  7974. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  7975. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  7976. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  7977. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  7978. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  7979. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7980. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  7981. status_calc_pc(sd,SCO_NONE);
  7982. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  7983. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  7984. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  7985. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  7986. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  7987. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  7988. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  7989. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  7990. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7991. }
  7992. }
  7993. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  7994. new_look = JOB_STAR_GLADIATOR2;
  7995. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  7996. new_look = -1;
  7997. if (sd->disguise || !new_look)
  7998. return; //Disguises break sprite changes
  7999. if (new_look < 0) { //Restore normal look.
  8000. status_set_viewdata(&sd->bl, sd->status.class_);
  8001. new_look = sd->vd.class_;
  8002. }
  8003. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  8004. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  8005. if (sd->vd.cloth_color)
  8006. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8007. if( sd->vd.body_style )
  8008. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8009. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  8010. }
  8011. /**
  8012. * Give player a cart
  8013. * @param sd Player
  8014. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8015. **/
  8016. bool pc_setcart(struct map_session_data *sd,int type) {
  8017. #ifndef NEW_CARTS
  8018. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8019. int option;
  8020. #endif
  8021. nullpo_retr(false,sd);
  8022. if( type < 0 || type > MAX_CARTS )
  8023. return false;// Never trust the values sent by the client! [Skotlex]
  8024. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8025. return false;// Push cart is required
  8026. #ifdef NEW_CARTS
  8027. switch( type ) {
  8028. case 0:
  8029. if( !sd->sc.data[SC_PUSH_CART] )
  8030. return 0;
  8031. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8032. clif_clearcart(sd->fd);
  8033. break;
  8034. default:/* everything else is an allowed ID so we can move on */
  8035. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8036. clif_cartlist(sd);
  8037. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8038. }
  8039. clif_updatestatus(sd, SP_CARTINFO);
  8040. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8041. break;
  8042. }
  8043. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8044. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8045. #else
  8046. // Update option
  8047. option = sd->sc.option;
  8048. option &= ~OPTION_CART;// clear cart bits
  8049. option |= cart[type]; // set cart
  8050. pc_setoption(sd, option);
  8051. #endif
  8052. return true;
  8053. }
  8054. /*==========================================
  8055. * Give player a falcon
  8056. *------------------------------------------*/
  8057. void pc_setfalcon(struct map_session_data* sd, int flag)
  8058. {
  8059. if( flag ){
  8060. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8061. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8062. } else if( pc_isfalcon(sd) ){
  8063. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8064. }
  8065. }
  8066. /*==========================================
  8067. * Set player riding
  8068. *------------------------------------------*/
  8069. void pc_setriding(struct map_session_data* sd, int flag)
  8070. {
  8071. if( sd->sc.data[SC_ALL_RIDING] )
  8072. return;
  8073. if( flag ){
  8074. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8075. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8076. } else if( pc_isriding(sd) ){
  8077. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8078. }
  8079. }
  8080. /*==========================================
  8081. * Give player a mado
  8082. *------------------------------------------*/
  8083. void pc_setmadogear(struct map_session_data* sd, int flag)
  8084. {
  8085. if( flag ){
  8086. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8087. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8088. } else if( pc_ismadogear(sd) ){
  8089. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8090. }
  8091. }
  8092. /*==========================================
  8093. * Check if player can drop an item
  8094. *------------------------------------------*/
  8095. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8096. {
  8097. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  8098. return false;
  8099. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8100. return false;
  8101. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8102. }
  8103. /**
  8104. * Determines whether a player can attack based on status changes
  8105. * Why not use status_check_skilluse?
  8106. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8107. * Even ground-based attacks should be blocked by these statuses
  8108. * Called from unit_attack and unit_attack_timer_sub
  8109. * @retval true Can attack
  8110. **/
  8111. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8112. nullpo_retr(false, sd);
  8113. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8114. return false;
  8115. if( sd->sc.data[SC_BASILICA] ||
  8116. sd->sc.data[SC__SHADOWFORM] ||
  8117. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8118. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8119. sd->sc.data[SC_CRYSTALIZE] ||
  8120. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8121. sd->sc.data[SC_TRICKDEAD] ||
  8122. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8123. sd->sc.data[SC_BLADESTOP] ||
  8124. sd->sc.data[SC_DEEPSLEEP] ||
  8125. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8126. sd->sc.data[SC_KINGS_GRACE] )
  8127. return false;
  8128. return true;
  8129. }
  8130. /*==========================================
  8131. * Read '@type' variables (temporary numeric char reg)
  8132. *------------------------------------------*/
  8133. int pc_readreg(struct map_session_data* sd, int64 reg)
  8134. {
  8135. return i64db_iget(sd->regs.vars, reg);
  8136. }
  8137. /*==========================================
  8138. * Set '@type' variables (temporary numeric char reg)
  8139. *------------------------------------------*/
  8140. bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
  8141. {
  8142. unsigned int index = script_getvaridx(reg);
  8143. nullpo_retr(false, sd);
  8144. if( val ) {
  8145. i64db_iput(sd->regs.vars, reg, val);
  8146. if( index )
  8147. script_array_update(&sd->regs, reg, false);
  8148. } else {
  8149. i64db_remove(sd->regs.vars, reg);
  8150. if( index )
  8151. script_array_update(&sd->regs, reg, true);
  8152. }
  8153. return true;
  8154. }
  8155. /*==========================================
  8156. * Read '@type$' variables (temporary string char reg)
  8157. *------------------------------------------*/
  8158. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8159. {
  8160. struct script_reg_str *p = NULL;
  8161. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8162. return p ? p->value : NULL;
  8163. }
  8164. /*==========================================
  8165. * Set '@type$' variables (temporary string char reg)
  8166. *------------------------------------------*/
  8167. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8168. {
  8169. struct script_reg_str *p = NULL;
  8170. unsigned int index = script_getvaridx(reg);
  8171. DBData prev;
  8172. nullpo_retr(false, sd);
  8173. if( str[0] ) {
  8174. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8175. p->value = aStrdup(str);
  8176. p->flag.type = 1;
  8177. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8178. p = (struct script_reg_str *)db_data2ptr(&prev);
  8179. if( p->value )
  8180. aFree(p->value);
  8181. ers_free(str_reg_ers, p);
  8182. } else {
  8183. if( index )
  8184. script_array_update(&sd->regs, reg, false);
  8185. }
  8186. } else {
  8187. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8188. p = (struct script_reg_str *)db_data2ptr(&prev);
  8189. if( p->value )
  8190. aFree(p->value);
  8191. ers_free(str_reg_ers, p);
  8192. if( index )
  8193. script_array_update(&sd->regs, reg, true);
  8194. }
  8195. }
  8196. return true;
  8197. }
  8198. /**
  8199. * Serves the following variable types:
  8200. * - 'type' (permanent numeric char reg)
  8201. * - '#type' (permanent numeric account reg)
  8202. * - '##type' (permanent numeric account reg2)
  8203. **/
  8204. int pc_readregistry(struct map_session_data *sd, int64 reg)
  8205. {
  8206. struct script_reg_num *p = NULL;
  8207. if (!sd->vars_ok) {
  8208. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8209. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8210. //intif->request_registry(sd,type==3?4:type);
  8211. set_eof(sd->fd);
  8212. return 0;
  8213. }
  8214. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8215. return p ? p->value : 0;
  8216. }
  8217. /**
  8218. * Serves the following variable types:
  8219. * - 'type$' (permanent str char reg)
  8220. * - '#type$' (permanent str account reg)
  8221. * - '##type$' (permanent str account reg2)
  8222. **/
  8223. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8224. {
  8225. struct script_reg_str *p = NULL;
  8226. if (!sd->vars_ok) {
  8227. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8228. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8229. //intif->request_registry(sd,type==3?4:type);
  8230. set_eof(sd->fd);
  8231. return NULL;
  8232. }
  8233. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8234. return p ? p->value : NULL;
  8235. }
  8236. /**
  8237. * Serves the following variable types:
  8238. * - 'type' (permanent numeric char reg)
  8239. * - '#type' (permanent numeric account reg)
  8240. * - '##type' (permanent numeric account reg2)
  8241. **/
  8242. int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
  8243. {
  8244. struct script_reg_num *p = NULL;
  8245. const char *regname = get_str(script_getvarid(reg));
  8246. unsigned int index = script_getvaridx(reg);
  8247. if ( !reg_load && !sd->vars_ok ) {
  8248. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8249. return 0;
  8250. }
  8251. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8252. if( val ) {
  8253. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8254. script_array_update(&sd->regs, reg, false);
  8255. p->value = val;
  8256. } else {
  8257. p->value = 0;
  8258. if( index )
  8259. script_array_update(&sd->regs, reg, true);
  8260. }
  8261. if (!reg_load)
  8262. p->flag.update = 1;/* either way, it will require either delete or replace */
  8263. } else if( val ) {
  8264. DBData prev;
  8265. if( index )
  8266. script_array_update(&sd->regs, reg, false);
  8267. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8268. p->value = val;
  8269. if (!reg_load)
  8270. p->flag.update = 1;
  8271. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8272. p = (struct script_reg_num *)db_data2ptr(&prev);
  8273. ers_free(num_reg_ers, p);
  8274. }
  8275. }
  8276. if (!reg_load && p)
  8277. sd->vars_dirty = true;
  8278. return 1;
  8279. }
  8280. /**
  8281. * Serves the following variable types:
  8282. * - 'type$' (permanent str char reg)
  8283. * - '#type$' (permanent str account reg)
  8284. * - '##type$' (permanent str account reg2)
  8285. **/
  8286. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8287. {
  8288. struct script_reg_str *p = NULL;
  8289. const char *regname = get_str(script_getvarid(reg));
  8290. unsigned int index = script_getvaridx(reg);
  8291. size_t vlen = 0;
  8292. if (!reg_load && !sd->vars_ok) {
  8293. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8294. return 0;
  8295. }
  8296. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8297. {
  8298. 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);
  8299. return 0;
  8300. }
  8301. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8302. if( val[0] ) {
  8303. if( p->value )
  8304. aFree(p->value);
  8305. else if ( index ) // an entry that was deleted, so we reset
  8306. script_array_update(&sd->regs, reg, false);
  8307. p->value = aStrdup(val);
  8308. } else {
  8309. if (p->value)
  8310. aFree(p->value);
  8311. p->value = NULL;
  8312. if( index )
  8313. script_array_update(&sd->regs, reg, true);
  8314. }
  8315. if( !reg_load )
  8316. p->flag.update = 1; // either way, it will require either delete or replace
  8317. } else if( val[0] ) {
  8318. DBData prev;
  8319. if( index )
  8320. script_array_update(&sd->regs, reg, false);
  8321. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8322. p->value = aStrdup(val);
  8323. if( !reg_load )
  8324. p->flag.update = 1;
  8325. p->flag.type = 1;
  8326. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8327. p = (struct script_reg_str *)db_data2ptr(&prev);
  8328. if( p->value )
  8329. aFree(p->value);
  8330. ers_free(str_reg_ers, p);
  8331. }
  8332. }
  8333. if( !reg_load && p )
  8334. sd->vars_dirty = true;
  8335. return 1;
  8336. }
  8337. /**
  8338. * Set value of player variable
  8339. * @param sd Player
  8340. * @param reg Variable name
  8341. * @param value
  8342. * @return True if success, false if failed.
  8343. **/
  8344. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  8345. char prefix = reg[0];
  8346. nullpo_retr(false, sd);
  8347. if (reg[strlen(reg)-1] == '$') {
  8348. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8349. return false;
  8350. }
  8351. val = cap_value(val, INT_MIN, INT_MAX);
  8352. switch (prefix) {
  8353. case '.':
  8354. case '\'':
  8355. case '$':
  8356. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8357. return false;
  8358. case '@':
  8359. return pc_setreg(sd, add_str(reg), val);
  8360. case '#':
  8361. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) > 0 : pc_setaccountreg(sd, add_str(reg), val) > 0;
  8362. default:
  8363. return pc_setglobalreg(sd, add_str(reg), val) > 0;
  8364. }
  8365. return false;
  8366. }
  8367. /**
  8368. * Get value of player variable
  8369. * @param sd Player
  8370. * @param reg Variable name
  8371. * @return Variable value or 0 if failed.
  8372. **/
  8373. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  8374. char prefix = reg[0];
  8375. nullpo_ret(sd);
  8376. if (reg[strlen(reg)-1] == '$') {
  8377. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8378. return 0;
  8379. }
  8380. switch (prefix) {
  8381. case '.':
  8382. case '\'':
  8383. case '$':
  8384. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8385. return 0;
  8386. case '@':
  8387. return pc_readreg(sd, add_str(reg));
  8388. case '#':
  8389. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8390. default:
  8391. return pc_readglobalreg(sd, add_str(reg));
  8392. }
  8393. return 0;
  8394. }
  8395. /*==========================================
  8396. * Exec eventtimer for player sd (retrieved from map_session (id))
  8397. *------------------------------------------*/
  8398. static TIMER_FUNC(pc_eventtimer){
  8399. struct map_session_data *sd=map_id2sd(id);
  8400. char *p = (char *)data;
  8401. int i;
  8402. if(sd==NULL)
  8403. return 0;
  8404. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8405. if( i < MAX_EVENTTIMER )
  8406. {
  8407. sd->eventtimer[i] = INVALID_TIMER;
  8408. sd->eventcount--;
  8409. npc_event(sd,p,0);
  8410. }
  8411. else
  8412. ShowError("pc_eventtimer: no such event timer\n");
  8413. if (p) aFree(p);
  8414. return 0;
  8415. }
  8416. /*==========================================
  8417. * Add eventtimer for player sd ?
  8418. *------------------------------------------*/
  8419. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8420. {
  8421. int i;
  8422. nullpo_retr(false,sd);
  8423. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8424. if( i == MAX_EVENTTIMER )
  8425. return false;
  8426. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8427. sd->eventcount++;
  8428. return true;
  8429. }
  8430. /*==========================================
  8431. * Del eventtimer for player sd ?
  8432. *------------------------------------------*/
  8433. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8434. {
  8435. char* p = NULL;
  8436. int i;
  8437. nullpo_retr(false,sd);
  8438. if (sd->eventcount == 0)
  8439. return false;
  8440. // find the named event timer
  8441. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8442. sd->eventtimer[i] != INVALID_TIMER &&
  8443. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8444. strcmp(p, name) == 0
  8445. );
  8446. if( i == MAX_EVENTTIMER )
  8447. return false; // not found
  8448. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8449. sd->eventtimer[i] = INVALID_TIMER;
  8450. if(sd->eventcount > 0)
  8451. sd->eventcount--;
  8452. aFree(p);
  8453. return true;
  8454. }
  8455. /*==========================================
  8456. * Update eventtimer count for player sd
  8457. *------------------------------------------*/
  8458. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8459. {
  8460. int i;
  8461. nullpo_retv(sd);
  8462. for(i=0;i<MAX_EVENTTIMER;i++)
  8463. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8464. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8465. addtick_timer(sd->eventtimer[i],tick);
  8466. break;
  8467. }
  8468. }
  8469. /*==========================================
  8470. * Remove all eventtimer for player sd
  8471. *------------------------------------------*/
  8472. void pc_cleareventtimer(struct map_session_data *sd)
  8473. {
  8474. int i;
  8475. nullpo_retv(sd);
  8476. if (sd->eventcount == 0)
  8477. return;
  8478. for(i=0;i<MAX_EVENTTIMER;i++){
  8479. if( sd->eventtimer[i] != INVALID_TIMER ){
  8480. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8481. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8482. sd->eventtimer[i] = INVALID_TIMER;
  8483. if(sd->eventcount > 0) //avoid looping to max val
  8484. sd->eventcount--;
  8485. if (p) aFree(p);
  8486. }
  8487. }
  8488. }
  8489. /**
  8490. * Called when an item with combo is worn
  8491. * @param *sd
  8492. * @param *data struct item_data
  8493. * @return success numbers of succeed combo
  8494. */
  8495. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8496. uint16 i;
  8497. int success = 0;
  8498. for( i = 0; i < data->combos_count; i++ ) {
  8499. struct itemchk {
  8500. int idx;
  8501. unsigned short nameid;
  8502. short card[MAX_SLOTS];
  8503. } *combo_idx;
  8504. int idx, j;
  8505. int nb_itemCombo;
  8506. unsigned int pos = 0;
  8507. /* ensure this isn't a duplicate combo */
  8508. if( sd->combos.bonus != NULL ) {
  8509. int x;
  8510. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8511. /* found a match, skip this combo */
  8512. if( x < sd->combos.count )
  8513. continue;
  8514. }
  8515. nb_itemCombo = data->combos[i]->count;
  8516. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8517. continue;
  8518. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8519. for(j=0; j < nb_itemCombo; j++){
  8520. combo_idx[j].idx=-1;
  8521. combo_idx[j].nameid=-1;
  8522. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8523. }
  8524. for( j = 0; j < nb_itemCombo; j++ ) {
  8525. uint16 id = data->combos[i]->nameid[j], k;
  8526. bool found = false;
  8527. for( k = 0; k < EQI_MAX; k++ ) {
  8528. short index = sd->equip_index[k];
  8529. if( index < 0 )
  8530. continue;
  8531. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8532. continue;
  8533. if (!sd->inventory_data[index] )
  8534. continue;
  8535. if ( itemdb_type(id) != IT_CARD ) {
  8536. if ( sd->inventory_data[index]->nameid != id )
  8537. continue;
  8538. if(j>0){ //check if this item not already used
  8539. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8540. uint8 z;
  8541. for (z = 0; z < nb_itemCombo-1; z++)
  8542. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8543. do_continue=true;
  8544. if(do_continue)
  8545. continue;
  8546. }
  8547. combo_idx[j].nameid = id;
  8548. combo_idx[j].idx = index;
  8549. pos |= sd->inventory.u.items_inventory[index].equip;
  8550. found = true;
  8551. break;
  8552. } else { //Cards and enchants
  8553. uint16 z;
  8554. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8555. continue;
  8556. for (z = 0; z < MAX_SLOTS; z++) {
  8557. bool do_continue=false;
  8558. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8559. continue;
  8560. if(j>0){
  8561. int c1, c2;
  8562. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8563. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8564. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8565. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8566. do_continue = true;
  8567. break;
  8568. }
  8569. }
  8570. }
  8571. }
  8572. }
  8573. if(do_continue)
  8574. continue;
  8575. combo_idx[j].nameid = id;
  8576. combo_idx[j].idx = index;
  8577. combo_idx[j].card[z] = id;
  8578. pos |= sd->inventory.u.items_inventory[index].equip;
  8579. found = true;
  8580. break;
  8581. }
  8582. }
  8583. }
  8584. if( !found )
  8585. break;/* we haven't found all the ids for this combo, so we can return */
  8586. }
  8587. aFree(combo_idx);
  8588. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8589. if( j < nb_itemCombo )
  8590. continue;
  8591. /* we got here, means all items in the combo are matching */
  8592. idx = sd->combos.count;
  8593. if( sd->combos.bonus == NULL ) {
  8594. CREATE(sd->combos.bonus, struct script_code *, 1);
  8595. CREATE(sd->combos.id, unsigned short, 1);
  8596. CREATE(sd->combos.pos, unsigned int, 1);
  8597. sd->combos.count = 1;
  8598. } else {
  8599. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8600. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8601. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8602. }
  8603. /* we simply copy the pointer */
  8604. sd->combos.bonus[idx] = data->combos[i]->script;
  8605. /* save this combo's id */
  8606. sd->combos.id[idx] = data->combos[i]->id;
  8607. /* save pos of combo*/
  8608. sd->combos.pos[idx] = pos;
  8609. success++;
  8610. }
  8611. return success;
  8612. }
  8613. /**
  8614. * Called when an item with combo is removed
  8615. * @param *sd
  8616. * @param *data struct item_data
  8617. * @return retval numbers of removed combo
  8618. */
  8619. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8620. int i, retval = 0;
  8621. if( sd->combos.bonus == NULL )
  8622. return 0;/* nothing to do here, player has no combos */
  8623. for( i = 0; i < data->combos_count; i++ ) {
  8624. /* check if this combo exists in this user */
  8625. int x = 0, cursor = 0, j;
  8626. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8627. /* no match, skip this combo */
  8628. if(x >= sd->combos.count)
  8629. continue;
  8630. sd->combos.bonus[x] = NULL;
  8631. sd->combos.id[x] = 0;
  8632. sd->combos.pos[x] = 0;
  8633. retval++;
  8634. /* check if combo requirements still fit */
  8635. if( pc_checkcombo( sd, data ) )
  8636. continue;
  8637. /* move next value to empty slot */
  8638. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8639. if( sd->combos.bonus[j] == NULL )
  8640. continue;
  8641. if( cursor != j ) {
  8642. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8643. sd->combos.id[cursor] = sd->combos.id[j];
  8644. sd->combos.pos[cursor] = sd->combos.pos[j];
  8645. }
  8646. cursor++;
  8647. }
  8648. /* it's empty, we can clear all the memory */
  8649. if( (sd->combos.count = cursor) == 0 ) {
  8650. aFree(sd->combos.bonus);
  8651. aFree(sd->combos.id);
  8652. aFree(sd->combos.pos);
  8653. sd->combos.bonus = NULL;
  8654. sd->combos.id = NULL;
  8655. sd->combos.pos = NULL;
  8656. return retval; /* we also can return at this point for we have no more combos to check */
  8657. }
  8658. }
  8659. return retval;
  8660. }
  8661. /**
  8662. * Load combo data(s) of player
  8663. * @param *sd
  8664. * @return ret numbers of succeed combo
  8665. */
  8666. int pc_load_combo(struct map_session_data *sd) {
  8667. int i, ret = 0;
  8668. for( i = 0; i < EQI_MAX; i++ ) {
  8669. struct item_data *id = NULL;
  8670. short idx = sd->equip_index[i];
  8671. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8672. continue;
  8673. if( id->combos_count )
  8674. ret += pc_checkcombo(sd,id);
  8675. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8676. struct item_data *data;
  8677. int j;
  8678. for( j = 0; j < id->slot; j++ ) {
  8679. if (!sd->inventory.u.items_inventory[idx].card[j])
  8680. continue;
  8681. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8682. if( data->combos_count )
  8683. ret += pc_checkcombo(sd,data);
  8684. }
  8685. }
  8686. }
  8687. }
  8688. return ret;
  8689. }
  8690. /*==========================================
  8691. * Equip item on player sd at req_pos from inventory index n
  8692. * return: false - fail; true - success
  8693. *------------------------------------------*/
  8694. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos)
  8695. {
  8696. int i, pos, flag = 0, iflag;
  8697. struct item_data *id;
  8698. uint8 res = ITEM_EQUIP_ACK_OK;
  8699. nullpo_retr(false,sd);
  8700. if( n < 0 || n >= MAX_INVENTORY ) {
  8701. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8702. return false;
  8703. }
  8704. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8705. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8706. return false;
  8707. }
  8708. if (!(id = sd->inventory_data[n]))
  8709. return false;
  8710. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8711. if(battle_config.battle_log)
  8712. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  8713. if((res = pc_isequip(sd,n))) {
  8714. clif_equipitemack(sd,n,0,res); // fail
  8715. return false;
  8716. }
  8717. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  8718. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8719. return false;
  8720. }
  8721. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8722. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8723. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8724. return false;
  8725. }
  8726. if (id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  8727. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8728. clif_notify_bindOnEquip(sd,n);
  8729. }
  8730. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  8731. pos = req_pos&EQP_ACC;
  8732. if (pos == EQP_ACC) //User specified both slots..
  8733. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8734. }
  8735. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8736. pos = (req_pos&EQP_ARMS);
  8737. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8738. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8739. }
  8740. if(pos == EQP_SHADOW_ACC) { // Shadow System
  8741. pos = req_pos&EQP_SHADOW_ACC;
  8742. if (pos == EQP_SHADOW_ACC)
  8743. pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8744. }
  8745. if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  8746. pos = (req_pos&EQP_SHADOW_ARMS);
  8747. if( pos == EQP_SHADOW_ARMS )
  8748. pos = (sd->equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  8749. }
  8750. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8751. //Update skill-block range database when weapon range changes. [Skotlex]
  8752. i = sd->equip_index[EQI_HAND_R];
  8753. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8754. flag = 1;
  8755. else
  8756. flag = id->range != sd->inventory_data[i]->range;
  8757. }
  8758. for(i=0;i<EQI_MAX;i++) {
  8759. if(pos & equip_bitmask[i]) {
  8760. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8761. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  8762. sd->equip_index[i] = n;
  8763. }
  8764. }
  8765. if(pos==EQP_AMMO) {
  8766. clif_arrowequip(sd,n);
  8767. clif_arrow_fail(sd,3);
  8768. }
  8769. else
  8770. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8771. sd->inventory.u.items_inventory[n].equip = pos;
  8772. if(pos & EQP_HAND_R) {
  8773. if(id)
  8774. sd->weapontype1 = id->look;
  8775. else
  8776. sd->weapontype1 = 0;
  8777. pc_calcweapontype(sd);
  8778. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8779. }
  8780. if(pos & EQP_HAND_L) {
  8781. if(id) {
  8782. if(id->type == IT_WEAPON) {
  8783. sd->status.shield = 0;
  8784. sd->weapontype2 = id->look;
  8785. }
  8786. else
  8787. if(id->type == IT_ARMOR) {
  8788. sd->status.shield = id->look;
  8789. sd->weapontype2 = 0;
  8790. }
  8791. }
  8792. else
  8793. sd->status.shield = sd->weapontype2 = 0;
  8794. pc_calcweapontype(sd);
  8795. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8796. }
  8797. if(pos & EQP_SHOES)
  8798. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8799. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  8800. short idx = sd->equip_index[EQI_AMMO];
  8801. if (idx >= 0) {
  8802. switch (sd->inventory_data[idx]->look) {
  8803. case AMMO_ARROW:
  8804. if (id->look != W_BOW && id->look != W_MUSICAL && id->look != W_WHIP)
  8805. pc_unequipitem(sd, idx, 2 | 4);
  8806. break;
  8807. case AMMO_BULLET:
  8808. case AMMO_SHELL:
  8809. if (id->look != W_REVOLVER && id->look != W_RIFLE && id->look != W_GATLING && id->look != W_SHOTGUN
  8810. #ifdef RENEWAL
  8811. && id->look != W_GRENADE
  8812. #endif
  8813. )
  8814. pc_unequipitem(sd, idx, 2 | 4);
  8815. break;
  8816. #ifndef RENEWAL
  8817. case AMMO_GRENADE:
  8818. if (id->look != W_GRENADE)
  8819. pc_unequipitem(sd, idx, 2 | 4);
  8820. break;
  8821. #endif
  8822. }
  8823. }
  8824. }
  8825. pc_set_costume_view(sd);
  8826. pc_checkallowskill(sd); //Check if status changes should be halted.
  8827. iflag = sd->npc_item_flag;
  8828. /* check for combos (MUST be before status_calc_pc) */
  8829. if( id->combos_count )
  8830. pc_checkcombo(sd,id);
  8831. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8832. ; //No cards
  8833. else {
  8834. for( i = 0; i < MAX_SLOTS; i++ ) {
  8835. struct item_data *data;
  8836. if (!sd->inventory.u.items_inventory[n].card[i])
  8837. continue;
  8838. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8839. if( data->combos_count )
  8840. pc_checkcombo(sd,data);
  8841. }
  8842. }
  8843. }
  8844. status_calc_pc(sd,SCO_NONE);
  8845. if (flag) //Update skill data
  8846. clif_skillinfoblock(sd);
  8847. //OnEquip script [Skotlex]
  8848. if (id) {
  8849. //only run the script if item isn't restricted
  8850. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  8851. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8852. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8853. ; //No cards
  8854. else {
  8855. for( i = 0; i < MAX_SLOTS; i++ ) {
  8856. struct item_data *data;
  8857. if (!sd->inventory.u.items_inventory[n].card[i])
  8858. continue;
  8859. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8860. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  8861. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8862. }
  8863. }
  8864. }
  8865. }
  8866. sd->npc_item_flag = iflag;
  8867. return true;
  8868. }
  8869. /**
  8870. * Recalculate player status on unequip
  8871. * @param sd: Player data
  8872. * @param n: Item inventory index
  8873. * @param flag: Whether to recalculate a player's status or not
  8874. * @return True on success or false on failure
  8875. */
  8876. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  8877. int i, iflag;
  8878. bool status_calc = false;
  8879. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  8880. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  8881. sd->inventory.u.items_inventory[n].equip = 0;
  8882. pc_checkallowskill(sd);
  8883. iflag = sd->npc_item_flag;
  8884. /* check for combos (MUST be before status_calc_pc) */
  8885. if (sd->inventory_data[n]) {
  8886. if (sd->inventory_data[n]->combos_count) {
  8887. if (pc_removecombo(sd, sd->inventory_data[n]))
  8888. status_calc = true;
  8889. }
  8890. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8891. ; //No cards
  8892. else {
  8893. for (i = 0; i < MAX_SLOTS; i++) {
  8894. struct item_data *data;
  8895. if (!sd->inventory.u.items_inventory[n].card[i])
  8896. continue;
  8897. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  8898. if (data->combos_count) {
  8899. if (pc_removecombo(sd, data))
  8900. status_calc = true;
  8901. }
  8902. }
  8903. }
  8904. }
  8905. }
  8906. if (status_calc)
  8907. status_calc_pc(sd, SCO_NONE);
  8908. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  8909. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  8910. //OnUnEquip script [Skotlex]
  8911. if (sd->inventory_data[n]) {
  8912. if (sd->inventory_data[n]->unequip_script)
  8913. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  8914. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8915. ; //No cards
  8916. else {
  8917. for (i = 0; i < MAX_SLOTS; i++) {
  8918. struct item_data *data;
  8919. if (!sd->inventory.u.items_inventory[n].card[i])
  8920. continue;
  8921. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  8922. if (data->unequip_script)
  8923. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  8924. }
  8925. }
  8926. }
  8927. }
  8928. if (flag & 1)
  8929. status_calc_pc(sd, SCO_FORCE);
  8930. sd->npc_item_flag = iflag;
  8931. }
  8932. /**
  8933. * Called when attempting to unequip an item from a player
  8934. * @param sd: Player data
  8935. * @param n: Item inventory index
  8936. * @param flag: Type of unequip
  8937. * 0 - only unequip
  8938. * 1 - calculate status after unequipping
  8939. * 2 - force unequip
  8940. * 4 - unequip by switching equipment
  8941. * @return True on success or false on failure
  8942. */
  8943. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  8944. int i, pos;
  8945. nullpo_retr(false,sd);
  8946. if (n < 0 || n >= MAX_INVENTORY) {
  8947. clif_unequipitemack(sd,0,0,0);
  8948. return false;
  8949. }
  8950. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  8951. clif_unequipitemack(sd,n,0,0);
  8952. return false; //Nothing to unequip
  8953. }
  8954. // status change that makes player cannot unequip equipment
  8955. if (!(flag&2) && sd->sc.count &&
  8956. (sd->sc.data[SC_BERSERK] ||
  8957. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8958. sd->sc.data[SC__BLOODYLUST] ||
  8959. sd->sc.data[SC_KYOUGAKU] ||
  8960. (sd->sc.data[SC_PYROCLASTIC] &&
  8961. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  8962. {
  8963. clif_unequipitemack(sd,n,0,0);
  8964. return false;
  8965. }
  8966. if (battle_config.battle_log)
  8967. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  8968. for(i = 0; i < EQI_MAX; i++) {
  8969. if (pos & equip_bitmask[i])
  8970. sd->equip_index[i] = -1;
  8971. }
  8972. if(pos & EQP_HAND_R) {
  8973. sd->weapontype1 = 0;
  8974. sd->status.weapon = sd->weapontype2;
  8975. pc_calcweapontype(sd);
  8976. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8977. if( !battle_config.dancing_weaponswitch_fix )
  8978. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  8979. #ifdef RENEWAL
  8980. if (battle_config.switch_remove_edp&2) {
  8981. #else
  8982. if (battle_config.switch_remove_edp&1) {
  8983. #endif
  8984. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  8985. }
  8986. }
  8987. if(pos & EQP_HAND_L) {
  8988. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  8989. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  8990. sd->status.shield = sd->weapontype2 = 0;
  8991. pc_calcweapontype(sd);
  8992. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8993. }
  8994. if(pos & EQP_SHOES)
  8995. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8996. clif_unequipitemack(sd,n,pos,1);
  8997. pc_set_costume_view(sd);
  8998. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  8999. // On weapon change (right and left hand)
  9000. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  9001. if (battle_config.ammo_unequip && !(flag & 4)) {
  9002. switch (sd->inventory_data[n]->look) {
  9003. case W_BOW:
  9004. case W_MUSICAL:
  9005. case W_WHIP:
  9006. case W_REVOLVER:
  9007. case W_RIFLE:
  9008. case W_GATLING:
  9009. case W_SHOTGUN:
  9010. case W_GRENADE: {
  9011. short idx = sd->equip_index[EQI_AMMO];
  9012. if (idx >= 0) {
  9013. sd->equip_index[EQI_AMMO] = -1;
  9014. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  9015. pc_unequipitem_sub(sd, idx, 0);
  9016. }
  9017. }
  9018. break;
  9019. }
  9020. }
  9021. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  9022. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  9023. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  9024. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  9025. }
  9026. // On armor change
  9027. if (pos & EQP_ARMOR) {
  9028. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  9029. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  9030. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  9031. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  9032. }
  9033. // On ammo change
  9034. 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_))
  9035. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  9036. pc_unequipitem_sub(sd, n, flag);
  9037. return true;
  9038. }
  9039. /*==========================================
  9040. * Checking if player (sd) has an invalid item
  9041. * and is unequiped on map load (item_noequip)
  9042. *------------------------------------------*/
  9043. void pc_checkitem(struct map_session_data *sd) {
  9044. int i, calc_flag = 0;
  9045. struct item it;
  9046. nullpo_retv(sd);
  9047. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9048. return;
  9049. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9050. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9051. it = sd->inventory.u.items_inventory[i];
  9052. if( it.nameid == 0 )
  9053. continue;
  9054. if( !it.equip )
  9055. continue;
  9056. if( it.equip&~pc_equippoint(sd,i) ) {
  9057. pc_unequipitem(sd, i, 2);
  9058. calc_flag = 1;
  9059. continue;
  9060. }
  9061. 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) ) {
  9062. pc_unequipitem(sd, i, 2);
  9063. calc_flag = 1;
  9064. continue;
  9065. }
  9066. }
  9067. if( calc_flag && sd->state.active ) {
  9068. pc_checkallowskill(sd);
  9069. status_calc_pc(sd,SCO_NONE);
  9070. }
  9071. }
  9072. /*==========================================
  9073. * Checks for unavailable items and removes them.
  9074. * @param sd: Player data
  9075. * @param type Forced check:
  9076. * 1 - Inventory
  9077. * 2 - Cart
  9078. * 4 - Storage
  9079. *------------------------------------------*/
  9080. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9081. {
  9082. int i;
  9083. unsigned short nameid;
  9084. char output[256];
  9085. nullpo_retv(sd);
  9086. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9087. for(i = 0; i < MAX_INVENTORY; i++) {
  9088. nameid = sd->inventory.u.items_inventory[i].nameid;
  9089. if (!nameid)
  9090. continue;
  9091. if (!itemdb_available(nameid)) {
  9092. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  9093. clif_displaymessage(sd->fd, output);
  9094. 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);
  9095. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9096. continue;
  9097. }
  9098. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9099. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9100. }
  9101. }
  9102. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9103. for(i = 0; i < MAX_CART; i++) {
  9104. nameid = sd->cart.u.items_cart[i].nameid;
  9105. if (!nameid)
  9106. continue;
  9107. if (!itemdb_available(nameid)) {
  9108. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  9109. clif_displaymessage(sd->fd, output);
  9110. 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);
  9111. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9112. continue;
  9113. }
  9114. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9115. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9116. }
  9117. }
  9118. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9119. for(i = 0; i < sd->storage.max_amount; i++) {
  9120. nameid = sd->storage.u.items_storage[i].nameid;
  9121. if (!nameid)
  9122. continue;
  9123. if (!itemdb_available(nameid)) {
  9124. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  9125. clif_displaymessage(sd->fd, output);
  9126. 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);
  9127. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9128. continue;
  9129. }
  9130. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9131. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9132. }
  9133. }
  9134. }
  9135. /*==========================================
  9136. * Update PVP rank for sd1 in cmp to sd2
  9137. *------------------------------------------*/
  9138. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9139. {
  9140. struct map_session_data *sd1,*sd2;
  9141. sd1=(struct map_session_data *)bl;
  9142. sd2=va_arg(ap,struct map_session_data *);
  9143. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9144. {// cannot register pvp rank for hidden GMs
  9145. return 0;
  9146. }
  9147. if( sd1->pvp_point > sd2->pvp_point )
  9148. sd2->pvp_rank++;
  9149. return 0;
  9150. }
  9151. /*==========================================
  9152. * Calculate new rank beetween all present players (map_foreachinallarea)
  9153. * and display result
  9154. *------------------------------------------*/
  9155. int pc_calc_pvprank(struct map_session_data *sd)
  9156. {
  9157. int old = sd->pvp_rank;
  9158. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9159. sd->pvp_rank=1;
  9160. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9161. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9162. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9163. return sd->pvp_rank;
  9164. }
  9165. /*==========================================
  9166. * Calculate next sd ranking calculation from config
  9167. *------------------------------------------*/
  9168. TIMER_FUNC(pc_calc_pvprank_timer){
  9169. struct map_session_data *sd;
  9170. sd=map_id2sd(id);
  9171. if(sd==NULL)
  9172. return 0;
  9173. sd->pvp_timer = INVALID_TIMER;
  9174. if( pc_isinvisible(sd) )
  9175. {// do not calculate the pvp rank for a hidden GM
  9176. return 0;
  9177. }
  9178. if( pc_calc_pvprank(sd) > 0 )
  9179. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9180. return 0;
  9181. }
  9182. /*==========================================
  9183. * Checking if sd is married
  9184. * Return:
  9185. * partner_id = yes
  9186. * 0 = no
  9187. *------------------------------------------*/
  9188. int pc_ismarried(struct map_session_data *sd)
  9189. {
  9190. if(sd == NULL)
  9191. return -1;
  9192. if(sd->status.partner_id > 0)
  9193. return sd->status.partner_id;
  9194. else
  9195. return 0;
  9196. }
  9197. /*==========================================
  9198. * Marry player sd to player dstsd
  9199. * Return:
  9200. * false = fail
  9201. * true = success
  9202. *------------------------------------------*/
  9203. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9204. {
  9205. if(sd == NULL || dstsd == NULL ||
  9206. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9207. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9208. return false;
  9209. sd->status.partner_id = dstsd->status.char_id;
  9210. dstsd->status.partner_id = sd->status.char_id;
  9211. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9212. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9213. return true;
  9214. }
  9215. /*==========================================
  9216. * Divorce sd from its partner
  9217. * Return:
  9218. * false = fail
  9219. * true = success
  9220. *------------------------------------------*/
  9221. bool pc_divorce(struct map_session_data *sd)
  9222. {
  9223. struct map_session_data *p_sd;
  9224. int i;
  9225. if( sd == NULL || !pc_ismarried(sd) )
  9226. return false;
  9227. if( !sd->status.partner_id )
  9228. return false; // Char is not married
  9229. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9230. { // Lets char server do the divorce
  9231. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9232. return false; // No char server connected
  9233. return true;
  9234. }
  9235. // Both players online, lets do the divorce manually
  9236. sd->status.partner_id = 0;
  9237. p_sd->status.partner_id = 0;
  9238. for( i = 0; i < MAX_INVENTORY; i++ )
  9239. {
  9240. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9241. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9242. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9243. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9244. }
  9245. clif_divorced(sd, p_sd->status.name);
  9246. clif_divorced(p_sd, sd->status.name);
  9247. return true;
  9248. }
  9249. /**
  9250. * Get the partner map_session_data of a player
  9251. * @param sd : the husband|wife session
  9252. * @return partner session or NULL
  9253. */
  9254. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9255. if (!sd || !pc_ismarried(sd))
  9256. return NULL;
  9257. return map_charid2sd(sd->status.partner_id);
  9258. }
  9259. /**
  9260. * Get the father map_session_data of a player
  9261. * @param sd : the baby session
  9262. * @return father session or NULL
  9263. */
  9264. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9265. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9266. return NULL;
  9267. return map_charid2sd(sd->status.father);
  9268. }
  9269. /**
  9270. * Get the mother map_session_data of a player
  9271. * @param sd : the baby session
  9272. * @return mother session or NULL
  9273. */
  9274. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9275. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9276. return NULL;
  9277. return map_charid2sd(sd->status.mother);
  9278. }
  9279. /*==========================================
  9280. * Get sd children charid. (Need to be married)
  9281. *------------------------------------------*/
  9282. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9283. {
  9284. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9285. // charid2sd returns NULL if not found
  9286. return NULL;
  9287. return map_charid2sd(sd->status.child);
  9288. }
  9289. /*==========================================
  9290. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9291. *------------------------------------------*/
  9292. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  9293. {
  9294. int hp = 0, sp = 0;
  9295. if( pc_isdead(sd) )
  9296. return;
  9297. if (sd->hp_loss.value) {
  9298. sd->hp_loss.tick += diff_tick;
  9299. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9300. hp += sd->hp_loss.value;
  9301. sd->hp_loss.tick -= sd->hp_loss.rate;
  9302. }
  9303. if(hp >= sd->battle_status.hp)
  9304. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9305. }
  9306. if (sd->sp_loss.value) {
  9307. sd->sp_loss.tick += diff_tick;
  9308. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9309. sp += sd->sp_loss.value;
  9310. sd->sp_loss.tick -= sd->sp_loss.rate;
  9311. }
  9312. }
  9313. if (hp > 0 || sp > 0)
  9314. status_zap(&sd->bl, hp, sp);
  9315. }
  9316. //Character regen. Flag is used to know which types of regen can take place.
  9317. //&1: HP regen
  9318. //&2: SP regen
  9319. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  9320. {
  9321. int hp = 0, sp = 0;
  9322. if (sd->hp_regen.value) {
  9323. sd->hp_regen.tick += diff_tick;
  9324. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9325. hp += sd->hp_regen.value;
  9326. sd->hp_regen.tick -= sd->hp_regen.rate;
  9327. }
  9328. }
  9329. if (sd->sp_regen.value) {
  9330. sd->sp_regen.tick += diff_tick;
  9331. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9332. sp += sd->sp_regen.value;
  9333. sd->sp_regen.tick -= sd->sp_regen.rate;
  9334. }
  9335. }
  9336. if (sd->percent_hp_regen.value) {
  9337. sd->percent_hp_regen.tick += diff_tick;
  9338. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9339. hp += (sd->percent_hp_regen.value * sd->status.max_hp);
  9340. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9341. }
  9342. }
  9343. if (sd->percent_sp_regen.value) {
  9344. sd->percent_sp_regen.tick += diff_tick;
  9345. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9346. sp += (sd->percent_sp_regen.value * sd->status.max_sp);
  9347. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9348. }
  9349. }
  9350. if (hp > 0 || sp > 0)
  9351. status_heal(&sd->bl, hp, sp, 0);
  9352. }
  9353. /*==========================================
  9354. * Memo player sd savepoint. (map,x,y)
  9355. *------------------------------------------*/
  9356. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9357. {
  9358. nullpo_retv(sd);
  9359. sd->status.save_point.map = mapindex;
  9360. sd->status.save_point.x = x;
  9361. sd->status.save_point.y = y;
  9362. }
  9363. /*==========================================
  9364. * Save 1 player data at autosave interval
  9365. *------------------------------------------*/
  9366. static TIMER_FUNC(pc_autosave){
  9367. int interval;
  9368. struct s_mapiterator* iter;
  9369. struct map_session_data* sd;
  9370. static int last_save_id = 0, save_flag = 0;
  9371. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9372. save_flag = 0;
  9373. else
  9374. save_flag = 1; //Noone was saved, so save first found char.
  9375. iter = mapit_getallusers();
  9376. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9377. {
  9378. if(sd->bl.id == last_save_id && save_flag != 1) {
  9379. save_flag = 1;
  9380. continue;
  9381. }
  9382. if(save_flag != 1) //Not our turn to save yet.
  9383. continue;
  9384. //Save char.
  9385. last_save_id = sd->bl.id;
  9386. save_flag = 2;
  9387. if (pc_isvip(sd)) // Check if we're still VIP
  9388. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9389. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9390. break;
  9391. }
  9392. mapit_free(iter);
  9393. interval = autosave_interval/(map_usercount()+1);
  9394. if(interval < minsave_interval)
  9395. interval = minsave_interval;
  9396. add_timer(gettick()+interval,pc_autosave,0,0);
  9397. return 0;
  9398. }
  9399. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9400. {
  9401. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9402. { //Night/day state does not match.
  9403. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9404. sd->state.night = night_flag;
  9405. return 1;
  9406. }
  9407. return 0;
  9408. }
  9409. /*================================================
  9410. * timer to do the day [Yor]
  9411. * data: 0 = called by timer, 1 = gmcommand/script
  9412. *------------------------------------------------*/
  9413. TIMER_FUNC(map_day_timer){
  9414. char tmp_soutput[1024];
  9415. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9416. return 0;
  9417. if (!night_flag)
  9418. return 0; //Already day.
  9419. night_flag = 0; // 0=day, 1=night [Yor]
  9420. map_foreachpc(pc_daynight_timer_sub);
  9421. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9422. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9423. return 0;
  9424. }
  9425. /*================================================
  9426. * timer to do the night [Yor]
  9427. * data: 0 = called by timer, 1 = gmcommand/script
  9428. *------------------------------------------------*/
  9429. TIMER_FUNC(map_night_timer){
  9430. char tmp_soutput[1024];
  9431. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9432. return 0;
  9433. if (night_flag)
  9434. return 0; //Already nigth.
  9435. night_flag = 1; // 0=day, 1=night [Yor]
  9436. map_foreachpc(pc_daynight_timer_sub);
  9437. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9438. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9439. return 0;
  9440. }
  9441. /**
  9442. * Attempt to stand up a player
  9443. * @param sd
  9444. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9445. * @return True if success, Fals if failed
  9446. */
  9447. bool pc_setstand(struct map_session_data *sd, bool force){
  9448. nullpo_ret(sd);
  9449. // Cannot stand yet
  9450. // TODO: Move to SCS_NOSTAND [Cydh]
  9451. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9452. return false;
  9453. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9454. clif_status_load(&sd->bl,EFST_SIT,0);
  9455. clif_standing(&sd->bl); //Inform area PC is standing
  9456. //Reset sitting tick.
  9457. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9458. if( pc_isdead( sd ) ){
  9459. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9460. clif_party_dead( sd );
  9461. }else{
  9462. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9463. }
  9464. return true;
  9465. }
  9466. /**
  9467. * Calculate Overheat value
  9468. * @param sd: Player data
  9469. * @param heat: Amount of Heat to adjust
  9470. **/
  9471. void pc_overheat(struct map_session_data *sd, int16 heat) {
  9472. struct status_change_entry *sce = NULL;
  9473. int16 limit[] = { 150, 200, 280, 360, 450 };
  9474. uint16 skill_lv;
  9475. nullpo_retv(sd);
  9476. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  9477. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  9478. sce->val1 += heat;
  9479. sce->val1 = cap_value(sce->val1, 0, 1000);
  9480. if (sd->sc.data[SC_OVERHEAT])
  9481. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  9482. if (sce->val1 > limit[skill_lv])
  9483. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  9484. } else if (heat > 0)
  9485. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  9486. }
  9487. /**
  9488. * Check if player is autolooting given itemID.
  9489. */
  9490. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9491. {
  9492. uint8 i = 0;
  9493. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9494. return true;
  9495. if (!sd->state.autolooting)
  9496. return false;
  9497. if (sd->state.autolooting)
  9498. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9499. return (i != AUTOLOOTITEM_SIZE);
  9500. }
  9501. /**
  9502. * Checks if player can use @/#command
  9503. * @param sd Player map session data
  9504. * @param command Command name without @/# and params
  9505. * @param type is it atcommand or charcommand
  9506. */
  9507. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9508. {
  9509. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9510. }
  9511. /**
  9512. * Checks if commands used by a player should be logged
  9513. * according to their group setting.
  9514. * @param sd Player map session data
  9515. */
  9516. bool pc_should_log_commands(struct map_session_data *sd)
  9517. {
  9518. return pc_group_should_log_commands(pc_get_group_id(sd));
  9519. }
  9520. /**
  9521. * Spirit Charm expiration timer.
  9522. * @see TimerFunc
  9523. */
  9524. static TIMER_FUNC(pc_spiritcharm_timer){
  9525. struct map_session_data *sd;
  9526. int i;
  9527. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9528. return 1;
  9529. if (sd->spiritcharm <= 0) {
  9530. 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);
  9531. sd->spiritcharm = 0;
  9532. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9533. return 0;
  9534. }
  9535. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9536. if (i == sd->spiritcharm) {
  9537. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9538. return 0;
  9539. }
  9540. sd->spiritcharm--;
  9541. if (i != sd->spiritcharm)
  9542. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9543. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9544. if (sd->spiritcharm <= 0)
  9545. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9546. clif_spiritcharm(sd);
  9547. return 0;
  9548. }
  9549. /**
  9550. * Adds a spirit charm.
  9551. * @param sd: Target character
  9552. * @param interval: Duration
  9553. * @param max: Maximum amount of charms to add
  9554. * @param type: Charm type (@see spirit_charm_types)
  9555. */
  9556. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9557. {
  9558. int tid, i;
  9559. nullpo_retv(sd);
  9560. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9561. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9562. if (max > MAX_SPIRITCHARM)
  9563. max = MAX_SPIRITCHARM;
  9564. if (sd->spiritcharm < 0)
  9565. sd->spiritcharm = 0;
  9566. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9567. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9568. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9569. sd->spiritcharm--;
  9570. if (sd->spiritcharm != 0)
  9571. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9572. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9573. }
  9574. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9575. 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);
  9576. if (i != sd->spiritcharm)
  9577. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9578. sd->spiritcharm_timer[i] = tid;
  9579. sd->spiritcharm++;
  9580. sd->spiritcharm_type = type;
  9581. clif_spiritcharm(sd);
  9582. }
  9583. /**
  9584. * Removes one or more spirit charms.
  9585. * @param sd: The target character
  9586. * @param count: Amount of charms to remove
  9587. * @param type: Type of charm to remove
  9588. */
  9589. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9590. {
  9591. int i;
  9592. nullpo_retv(sd);
  9593. if (sd->spiritcharm_type != type)
  9594. return;
  9595. if (sd->spiritcharm <= 0) {
  9596. sd->spiritcharm = 0;
  9597. return;
  9598. }
  9599. if (count <= 0)
  9600. return;
  9601. if (count > sd->spiritcharm)
  9602. count = sd->spiritcharm;
  9603. sd->spiritcharm -= count;
  9604. if (count > MAX_SPIRITCHARM)
  9605. count = MAX_SPIRITCHARM;
  9606. for (i = 0; i < count; i++) {
  9607. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9608. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9609. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9610. }
  9611. }
  9612. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9613. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9614. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9615. }
  9616. if (sd->spiritcharm <= 0)
  9617. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9618. clif_spiritcharm(sd);
  9619. }
  9620. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9621. /**
  9622. * Renewal EXP/Item Drop rate modifier based on level penalty
  9623. * @param level_diff: Monster and Player level difference
  9624. * @param mob_class: Monster class
  9625. * @param mode: Monster mode
  9626. * @param type: 1 - EXP, 2 - Item Drop
  9627. * @return Penalty rate
  9628. */
  9629. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9630. {
  9631. int rate = 100;
  9632. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9633. return rate;
  9634. if (level_diff < 0)
  9635. level_diff = MAX_LEVEL + (~level_diff + 1);
  9636. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9637. return rate;
  9638. return 100; // Penalty not found, return default
  9639. }
  9640. #endif
  9641. int pc_split_str(char *str,char **val,int num)
  9642. {
  9643. int i;
  9644. for (i=0; i<num && str; i++){
  9645. val[i] = str;
  9646. str = strchr(str,',');
  9647. if (str && i<num-1) //Do not remove a trailing comma.
  9648. *str++=0;
  9649. }
  9650. return i;
  9651. }
  9652. int pc_split_atoi(char* str, int* val, char sep, int max)
  9653. {
  9654. int i,j;
  9655. for (i=0; i<max; i++) {
  9656. if (!str) break;
  9657. val[i] = atoi(str);
  9658. str = strchr(str,sep);
  9659. if (str)
  9660. *str++=0;
  9661. }
  9662. //Zero up the remaining.
  9663. for(j=i; j < max; j++)
  9664. val[j] = 0;
  9665. return i;
  9666. }
  9667. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9668. {
  9669. static int warning=0;
  9670. int i,j;
  9671. for (i=0; i<max; i++) {
  9672. double f;
  9673. if (!str) break;
  9674. f = atof(str);
  9675. if (f < 0)
  9676. val[i] = 0;
  9677. else if (f > UINT_MAX) {
  9678. val[i] = UINT_MAX;
  9679. if (!warning) {
  9680. warning = 1;
  9681. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9682. }
  9683. } else
  9684. val[i] = (unsigned int)f;
  9685. str = strchr(str,sep);
  9686. if (str)
  9687. *str++=0;
  9688. }
  9689. //Zero up the remaining.
  9690. for(j=i; j < max; j++)
  9691. val[j] = 0;
  9692. return i;
  9693. }
  9694. /*==========================================
  9695. * sub DB reading.
  9696. * Function used to read skill_tree.txt
  9697. *------------------------------------------*/
  9698. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  9699. {
  9700. uint32 baselv, joblv, baselv_max, joblv_max;
  9701. uint16 skill_id, skill_lv, skill_lv_max;
  9702. int idx, class_;
  9703. unsigned int i, offset, skill_idx;
  9704. class_ = atoi(fields[0]);
  9705. skill_id = (uint16)atoi(fields[1]);
  9706. skill_lv = (uint16)atoi(fields[2]);
  9707. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  9708. baselv = (uint32)atoi(fields[3]);
  9709. joblv = (uint32)atoi(fields[4]);
  9710. offset = 5;
  9711. }
  9712. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  9713. baselv = joblv = 0;
  9714. offset = 3;
  9715. }
  9716. else {
  9717. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  9718. return false;
  9719. }
  9720. if(!pcdb_checkid(class_))
  9721. {
  9722. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  9723. return false;
  9724. }
  9725. idx = pc_class2idx(class_);
  9726. if (!skill_get_index(skill_id)) {
  9727. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.", skill_id, class_);
  9728. return false;
  9729. }
  9730. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9731. 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);
  9732. skill_lv = skill_lv_max;
  9733. }
  9734. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  9735. 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);
  9736. baselv = baselv_max;
  9737. }
  9738. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  9739. 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);
  9740. joblv = joblv_max;
  9741. }
  9742. //This is to avoid adding two lines for the same skill. [Skotlex]
  9743. 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 );
  9744. if( skill_idx == MAX_SKILL_TREE )
  9745. {
  9746. 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_);
  9747. return false;
  9748. }
  9749. else if(skill_tree[idx][skill_idx].skill_id)
  9750. {
  9751. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  9752. }
  9753. skill_tree[idx][skill_idx].skill_id = skill_id;
  9754. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  9755. skill_tree[idx][skill_idx].baselv = baselv;
  9756. skill_tree[idx][skill_idx].joblv = joblv;
  9757. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  9758. {
  9759. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  9760. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  9761. if (skill_id == 0) {
  9762. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  9763. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  9764. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  9765. }
  9766. continue;
  9767. }
  9768. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  9769. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  9770. return false;
  9771. }
  9772. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9773. 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);
  9774. skill_lv = skill_lv_max;
  9775. }
  9776. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  9777. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  9778. }
  9779. return true;
  9780. }
  9781. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9782. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  9783. {
  9784. int type, class_, diff;
  9785. type = atoi(fields[0]); //1=experience, 2=item drop
  9786. class_ = atoi(fields[1]);
  9787. diff = atoi(fields[2]);
  9788. if( type != 1 && type != 2 ){
  9789. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  9790. return false;
  9791. }
  9792. if( !CHK_CLASS(class_) ){
  9793. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  9794. return false;
  9795. }
  9796. diff = min(diff, MAX_LEVEL);
  9797. if( diff < 0 )
  9798. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  9799. level_penalty[type][class_][diff] = atoi(fields[3]);
  9800. return true;
  9801. }
  9802. #endif
  9803. /** [Cydh]
  9804. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  9805. * @param level Base level of player
  9806. * @param class_ Job ID @see enum e_job
  9807. * @return base_hp
  9808. */
  9809. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  9810. double base_hp;
  9811. uint16 i, idx = pc_class2idx(class_);
  9812. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  9813. #ifndef RENEWAL
  9814. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  9815. #endif
  9816. for (i = 2; i <= level; i++)
  9817. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  9818. if (class_ == JOB_SUMMONER)
  9819. base_hp += floor((base_hp / 2) + 0.5);
  9820. return (unsigned int)base_hp;
  9821. }
  9822. /** [Playtester]
  9823. * Calculates base sp of player.
  9824. * @param level Base level of player
  9825. * @param class_ Job ID @see enum e_job
  9826. * @return base_sp
  9827. */
  9828. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  9829. double base_sp;
  9830. uint16 idx = pc_class2idx(class_);
  9831. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  9832. switch (class_) {
  9833. case JOB_NINJA:
  9834. if (level >= 10)
  9835. base_sp -= 22;
  9836. else
  9837. base_sp = 11 + 3*level;
  9838. break;
  9839. case JOB_GUNSLINGER:
  9840. if (level > 10)
  9841. base_sp -= 18;
  9842. else
  9843. base_sp = 9 + 3*level;
  9844. break;
  9845. case JOB_SUMMONER:
  9846. base_sp -= floor(base_sp / 2);
  9847. break;
  9848. }
  9849. return (unsigned int)base_sp;
  9850. }
  9851. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  9852. static bool pc_readdb_job1(char* fields[], int columns, int current){
  9853. int idx, class_;
  9854. unsigned int i;
  9855. class_ = atoi(fields[0]);
  9856. if (!pcdb_checkid(class_)) {
  9857. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  9858. return false;
  9859. }
  9860. idx = pc_class2idx(class_);
  9861. job_info[idx].max_weight_base = atoi(fields[1]);
  9862. job_info[idx].hp_factor = atoi(fields[2]);
  9863. job_info[idx].hp_multiplicator = atoi(fields[3]);
  9864. job_info[idx].sp_factor = atoi(fields[4]);
  9865. #ifdef RENEWAL_ASPD
  9866. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  9867. #else
  9868. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  9869. #endif
  9870. {
  9871. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  9872. }
  9873. return true;
  9874. }
  9875. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  9876. static bool pc_readdb_job2(char* fields[], int columns, int current)
  9877. {
  9878. int idx, class_, i;
  9879. class_ = atoi(fields[0]);
  9880. if(!pcdb_checkid(class_))
  9881. {
  9882. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  9883. return false;
  9884. }
  9885. idx = pc_class2idx(class_);
  9886. for(i = 1; i < columns; i++)
  9887. {
  9888. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  9889. }
  9890. return true;
  9891. }
  9892. //Reading job_exp.txt line
  9893. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  9894. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  9895. {
  9896. int idx, i, type;
  9897. int job_id,job_count,jobs[CLASS_COUNT];
  9898. unsigned int ui, maxlvl;
  9899. maxlvl = atoi(fields[0]);
  9900. if(maxlvl > MAX_LEVEL || maxlvl<1){
  9901. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  9902. return false;
  9903. }
  9904. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9905. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  9906. return false;
  9907. }
  9908. type = atoi(fields[2]);
  9909. if(type < 0 || type > 1){
  9910. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  9911. return false;
  9912. }
  9913. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  9914. if (job_count < 1)
  9915. return false;
  9916. job_id = jobs[0];
  9917. if(!pcdb_checkid(job_id)){
  9918. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  9919. return false;
  9920. }
  9921. idx = pc_class2idx(job_id);
  9922. job_info[idx].max_level[type] = maxlvl;
  9923. for(i=0; i<maxlvl; i++)
  9924. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  9925. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  9926. //The reasoning behind the -2 is this... if the max level is 5, then the array
  9927. //should look like this:
  9928. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  9929. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  9930. job_info[idx].max_level[type]--;
  9931. if (job_info[idx].max_level[type] < maxlvl) {
  9932. 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]);
  9933. ShowInfo("Filling the missing values with the last exp entry.\n");
  9934. //Fill the requested values with the last entry.
  9935. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  9936. for (; ui+2 < maxlvl; ui++)
  9937. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  9938. job_info[idx].max_level[type] = maxlvl;
  9939. }
  9940. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9941. for (i = 1; i < job_count; i++) {
  9942. job_id = jobs[i];
  9943. if (!pcdb_checkid(job_id)) {
  9944. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  9945. continue;
  9946. }
  9947. idx = pc_class2idx(job_id);
  9948. 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]));
  9949. job_info[idx].max_level[type] = maxlvl;
  9950. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9951. }
  9952. return true;
  9953. }
  9954. /**
  9955. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  9956. * startlvl,endlvl,class,type,values...
  9957. */
  9958. #ifdef HP_SP_TABLES
  9959. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  9960. {
  9961. int i, startlvl, endlvl;
  9962. int job_count,jobs[CLASS_COUNT];
  9963. short type;
  9964. startlvl = atoi(fields[0]);
  9965. if(startlvl<1){
  9966. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9967. return false;
  9968. }
  9969. endlvl = atoi(fields[1]);
  9970. if(endlvl<1 || endlvl<startlvl){
  9971. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  9972. return false;
  9973. }
  9974. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9975. 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);
  9976. return false;
  9977. }
  9978. type = atoi(fields[3]);
  9979. if(type < 0 || type > 1){
  9980. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  9981. return false;
  9982. }
  9983. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  9984. if (job_count < 1)
  9985. return false;
  9986. for (i = 0; i < job_count; i++) {
  9987. int idx, job_id = jobs[i], use_endlvl;
  9988. if (!pcdb_checkid(job_id)) {
  9989. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  9990. return false;
  9991. }
  9992. idx = pc_class2idx(job_id);
  9993. if (startlvl > job_info[idx].max_level[0]) {
  9994. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9995. return false;
  9996. }
  9997. //Just read until available max level for this job, don't use MAX_LEVEL!
  9998. use_endlvl = endlvl;
  9999. if (use_endlvl > job_info[idx].max_level[0])
  10000. use_endlvl = job_info[idx].max_level[0];
  10001. if(type == 0) { //hp type
  10002. uint16 j;
  10003. for(j = 0; j < use_endlvl; j++) {
  10004. if (atoi(fields[j+4])) {
  10005. uint16 lvl_idx = startlvl-1+j;
  10006. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  10007. //Tells if this HP is lower than previous level (but not for 99->100)
  10008. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  10009. 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",
  10010. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  10011. }
  10012. }
  10013. }
  10014. else { //sp type
  10015. uint16 j;
  10016. for(j = 0; j < use_endlvl; j++) {
  10017. if (atoi(fields[j+4])) {
  10018. uint16 lvl_idx = startlvl-1+j;
  10019. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  10020. //Tells if this SP is lower than previous level (but not for 99->100)
  10021. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  10022. 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",
  10023. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  10024. }
  10025. }
  10026. }
  10027. }
  10028. return true;
  10029. }
  10030. #endif
  10031. /** [Cydh]
  10032. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  10033. */
  10034. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  10035. {
  10036. int idx, class_;
  10037. uint16 str, agi, vit, int_, dex, luk;
  10038. script_get_constant(trim(fields[0]),&class_);
  10039. if ((idx = pc_class2idx(class_)) < 0) {
  10040. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  10041. return false;
  10042. }
  10043. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  10044. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  10045. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  10046. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  10047. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10048. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10049. job_info[idx].max_param.str = str;
  10050. job_info[idx].max_param.agi = agi;
  10051. job_info[idx].max_param.vit = vit;
  10052. job_info[idx].max_param.int_ = int_;
  10053. job_info[idx].max_param.dex = dex;
  10054. job_info[idx].max_param.luk = luk;
  10055. return true;
  10056. }
  10057. /**
  10058. * Read job_noenter_map.txt
  10059. **/
  10060. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10061. int idx, class_ = -1;
  10062. if (ISDIGIT(str[0][0])) {
  10063. class_ = atoi(str[0]);
  10064. } else {
  10065. if (!script_get_constant(str[0], &class_)) {
  10066. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10067. return false;
  10068. }
  10069. }
  10070. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10071. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  10072. return false;
  10073. }
  10074. job_info[idx].noenter_map.zone = atoi(str[1]);
  10075. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10076. return true;
  10077. }
  10078. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10079. int i=1;
  10080. char line[24000]; //FIXME this seem too big
  10081. FILE *fp;
  10082. sprintf(line, "%s/statpoint.txt", basedir);
  10083. fp=fopen(line,"r");
  10084. if(fp == NULL){
  10085. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10086. return max(last_s,i);
  10087. } else {
  10088. int entries=0;
  10089. while(fgets(line, sizeof(line), fp))
  10090. {
  10091. int stat;
  10092. trim(line);
  10093. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10094. continue;
  10095. if ((stat=strtoul(line,NULL,10))<0)
  10096. stat=0;
  10097. if (i > MAX_LEVEL)
  10098. break;
  10099. statp[i]=stat;
  10100. i++;
  10101. entries++;
  10102. }
  10103. fclose(fp);
  10104. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10105. }
  10106. return max(last_s,i);
  10107. }
  10108. /*==========================================
  10109. * pc DB reading.
  10110. * job_exp.txt - required experience values
  10111. * skill_tree.txt - skill tree for every class
  10112. * attr_fix.txt - elemental adjustment table
  10113. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10114. * job_db2.txt - job,stats bonuses/lvl
  10115. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10116. *------------------------------------------*/
  10117. void pc_readdb(void) {
  10118. int i, k, s = 1;
  10119. const char* dbsubpath[] = {
  10120. "",
  10121. "/" DBIMPORT,
  10122. //add other path here
  10123. };
  10124. //reset
  10125. memset(job_info,0,sizeof(job_info)); // job_info table
  10126. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10127. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10128. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10129. for( k=1; k < 3; k++ ){ // fill in the blanks
  10130. int j;
  10131. for( j = 0; j < CLASS_ALL; j++ ){
  10132. int tmp = 0;
  10133. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10134. if( i == MAX_LEVEL+1 )
  10135. tmp = level_penalty[k][j][0];// reset
  10136. if( level_penalty[k][j][i] > 0 )
  10137. tmp = level_penalty[k][j][i];
  10138. else
  10139. level_penalty[k][j][i] = tmp;
  10140. }
  10141. }
  10142. }
  10143. #endif
  10144. // reset then read statspoint
  10145. memset(statp,0,sizeof(statp));
  10146. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10147. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10148. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10149. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10150. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10151. if(i==0) {
  10152. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10153. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10154. }
  10155. else {
  10156. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10157. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10158. }
  10159. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10160. if (i == 0)
  10161. #ifdef RENEWAL_ASPD
  10162. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10163. #else
  10164. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10165. #endif
  10166. else
  10167. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, true);
  10168. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10169. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10170. #ifdef HP_SP_TABLES
  10171. 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!
  10172. #endif
  10173. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10174. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10175. aFree(dbsubpath1);
  10176. aFree(dbsubpath2);
  10177. }
  10178. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10179. memset(skill_tree, 0, sizeof(skill_tree));
  10180. 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);
  10181. 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);
  10182. // generate the remaining parts of the db if necessary
  10183. k = battle_config.use_statpoint_table; //save setting
  10184. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10185. statp[0] = 45; // seed value
  10186. for (; s <= MAX_LEVEL; s++)
  10187. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10188. battle_config.use_statpoint_table = k; //restore setting
  10189. //Checking if all class have their data
  10190. for (i = 0; i < JOB_MAX; i++) {
  10191. int idx;
  10192. uint16 j;
  10193. if (!pcdb_checkid(i))
  10194. continue;
  10195. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10196. continue; //Classes that do not need exp tables.
  10197. idx = pc_class2idx(i);
  10198. if (!job_info[idx].max_level[0])
  10199. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10200. if (!job_info[idx].max_level[1])
  10201. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10202. //Init and checking the empty value of Base HP/SP [Cydh]
  10203. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10204. if (job_info[idx].base_hp[j] == 0)
  10205. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10206. if (job_info[idx].base_sp[j] == 0)
  10207. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10208. }
  10209. }
  10210. }
  10211. // Read MOTD on startup. [Valaris]
  10212. int pc_read_motd(void)
  10213. {
  10214. FILE* fp;
  10215. // clear old MOTD
  10216. memset(motd_text, 0, sizeof(motd_text));
  10217. // read current MOTD
  10218. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10219. {
  10220. unsigned int entries = 0;
  10221. char buf[CHAT_SIZE_MAX];
  10222. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10223. {
  10224. unsigned int lines = 0;
  10225. size_t len;
  10226. lines++;
  10227. if( buf[0] == '/' && buf[1] == '/' )
  10228. continue;
  10229. len = strlen(buf);
  10230. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10231. len--;
  10232. if( len ) {
  10233. char * ptr;
  10234. buf[len] = 0;
  10235. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10236. 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);
  10237. }
  10238. else {// empty line
  10239. buf[0] = ' ';
  10240. buf[1] = 0;
  10241. }
  10242. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10243. entries++;
  10244. }
  10245. fclose(fp);
  10246. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10247. }
  10248. else
  10249. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10250. return 0;
  10251. }
  10252. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10253. int i,cursor = 0;
  10254. struct item_cd* cd = NULL;
  10255. if( load ) {
  10256. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10257. // no item cooldown is associated with this character
  10258. return;
  10259. }
  10260. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10261. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10262. sd->item_delay[cursor].tick = cd->tick[i];
  10263. sd->item_delay[cursor].nameid = cd->nameid[i];
  10264. cursor++;
  10265. }
  10266. }
  10267. idb_remove(itemcd_db,sd->status.char_id);
  10268. } else {
  10269. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10270. // create a new skill cooldown object for map storage
  10271. CREATE( cd, struct item_cd, 1 );
  10272. idb_put( itemcd_db, sd->status.char_id, cd );
  10273. }
  10274. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10275. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10276. cd->tick[cursor] = sd->item_delay[i].tick;
  10277. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10278. cursor++;
  10279. }
  10280. }
  10281. }
  10282. return;
  10283. }
  10284. /**
  10285. * Add item delay to player's item delay data
  10286. * @param sd Player
  10287. * @param id Item data
  10288. * @param tick Current tick
  10289. * @param n Item index in inventory
  10290. * @return 0: No delay, can consume item.
  10291. * 1: Has delay, cancel consumption.
  10292. **/
  10293. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10294. int i;
  10295. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10296. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10297. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10298. if( i < MAX_ITEMDELAYS ) {
  10299. if( sd->item_delay[i].nameid ) {// found
  10300. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10301. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10302. char e_msg[CHAT_SIZE_MAX];
  10303. if( e_tick > 99 )
  10304. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10305. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10306. else
  10307. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10308. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10309. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10310. return 1; // Delay has not expired yet
  10311. }
  10312. } else {// not yet used item (all slots are initially empty)
  10313. sd->item_delay[i].nameid = id->nameid;
  10314. }
  10315. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10316. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10317. } else {// should not happen
  10318. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10319. }
  10320. //clean up used delays so we can give room for more
  10321. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10322. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10323. sd->item_delay[i].tick = 0;
  10324. sd->item_delay[i].nameid = 0;
  10325. }
  10326. }
  10327. return 0;
  10328. }
  10329. /**
  10330. * Check if player has delay to reuse item
  10331. * @param sd Player
  10332. * @param id Item data
  10333. * @param tick Current tick
  10334. * @param n Item index in inventory
  10335. * @return 0: No delay, can consume item.
  10336. * 1: Has delay, cancel consumption.
  10337. **/
  10338. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10339. struct status_change *sc = NULL;
  10340. nullpo_retr(0, sd);
  10341. nullpo_retr(0, id);
  10342. // Do normal delay assignment
  10343. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10344. return pc_itemcd_add(sd, id, tick, n);
  10345. // Send reply of delay remains
  10346. if (sc->data[id->delay_sc]) {
  10347. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10348. clif_msg_value(sd, ITEM_REUSE_LIMIT, timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99);
  10349. return 1;
  10350. }
  10351. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10352. return 0;
  10353. }
  10354. /**
  10355. * Clear the dmglog data from player
  10356. * @param sd
  10357. * @param md
  10358. **/
  10359. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10360. {
  10361. uint8 i;
  10362. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10363. if (i < DAMAGELOG_SIZE) {
  10364. md->dmglog[i].id = 0;
  10365. md->dmglog[i].dmg = 0;
  10366. md->dmglog[i].flag = 0;
  10367. }
  10368. }
  10369. /**
  10370. * Add log to player's dmglog
  10371. * @param sd
  10372. * @param id Monster's GID
  10373. **/
  10374. void pc_damage_log_add(struct map_session_data *sd, int id)
  10375. {
  10376. uint8 i = 0;
  10377. if (!sd || !id)
  10378. return;
  10379. //Only store new data, don't need to renew the old one with same id
  10380. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10381. if (i < DAMAGELOG_SIZE_PC)
  10382. return;
  10383. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10384. if (sd->dmglog[i] == 0) {
  10385. sd->dmglog[i] = id;
  10386. return;
  10387. }
  10388. }
  10389. }
  10390. /**
  10391. * Clear dmglog data from player
  10392. * @param sd
  10393. * @param id Monster's id
  10394. **/
  10395. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10396. {
  10397. uint8 i;
  10398. struct mob_data *md = NULL;
  10399. if (!sd)
  10400. return;
  10401. if (!id) {
  10402. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10403. if( !sd->dmglog[i] ) //skip the empty value
  10404. continue;
  10405. if ((md = map_id2md(sd->dmglog[i])))
  10406. pc_clear_log_damage_sub(sd->status.char_id,md);
  10407. sd->dmglog[i] = 0;
  10408. }
  10409. }
  10410. else {
  10411. if ((md = map_id2md(id)))
  10412. pc_clear_log_damage_sub(sd->status.char_id,md);
  10413. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10414. if (i < DAMAGELOG_SIZE_PC)
  10415. sd->dmglog[i] = 0;
  10416. }
  10417. }
  10418. /**
  10419. * Status change data arrived from char-server
  10420. * @param sd: Player data
  10421. */
  10422. void pc_scdata_received(struct map_session_data *sd) {
  10423. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10424. clif_weight_limit( sd );
  10425. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10426. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10427. }
  10428. sd->state.pc_loaded = true;
  10429. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10430. sd->state.connect_new = 1;
  10431. clif_parse_LoadEndAck(sd->fd, sd);
  10432. }
  10433. if (pc_iscarton(sd)) {
  10434. sd->cart_weight_max = 0; // Force a client refesh
  10435. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10436. }
  10437. }
  10438. /**
  10439. * Check player account expiration time and rental item expirations
  10440. * @param sd: Player data
  10441. */
  10442. void pc_check_expiration(struct map_session_data *sd) {
  10443. #ifndef ENABLE_SC_SAVING
  10444. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10445. #endif
  10446. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10447. time_t exp_time = sd->expiration_time;
  10448. char tmpstr[1024];
  10449. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10450. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  10451. pc_expire_check(sd);
  10452. }
  10453. }
  10454. TIMER_FUNC(pc_expiration_timer){
  10455. struct map_session_data *sd = map_id2sd(id);
  10456. if( !sd ) return 0;
  10457. sd->expiration_tid = INVALID_TIMER;
  10458. if( sd->fd )
  10459. clif_authfail_fd(sd->fd,10);
  10460. map_quit(sd);
  10461. return 0;
  10462. }
  10463. TIMER_FUNC(pc_autotrade_timer){
  10464. struct map_session_data *sd = map_id2sd(id);
  10465. if (!sd)
  10466. return 0;
  10467. sd->autotrade_tid = INVALID_TIMER;
  10468. if (sd->state.autotrade&2)
  10469. vending_reopen(sd);
  10470. if (sd->state.autotrade&4)
  10471. buyingstore_reopen(sd);
  10472. if (!sd->vender_id && !sd->buyer_id) {
  10473. sd->state.autotrade = 0;
  10474. map_quit(sd);
  10475. }
  10476. return 0;
  10477. }
  10478. /* this timer exists only when a character with a expire timer > 24h is online */
  10479. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10480. TIMER_FUNC(pc_global_expiration_timer){
  10481. struct s_mapiterator* iter;
  10482. struct map_session_data* sd;
  10483. iter = mapit_getallusers();
  10484. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10485. if( sd->expiration_time )
  10486. pc_expire_check(sd);
  10487. mapit_free(iter);
  10488. return 0;
  10489. }
  10490. void pc_expire_check(struct map_session_data *sd) {
  10491. /* ongoing timer */
  10492. if( sd->expiration_tid != INVALID_TIMER )
  10493. return;
  10494. /* not within the next 24h, enable the global check */
  10495. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10496. /* global check not running, enable */
  10497. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10498. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10499. return;
  10500. }
  10501. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10502. }
  10503. /**
  10504. * Deposit some money to bank
  10505. * @param sd
  10506. * @param money Amount of money to deposit
  10507. **/
  10508. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10509. unsigned int limit_check = money + sd->bank_vault;
  10510. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10511. return BDA_OVERFLOW;
  10512. } else if ( money > sd->status.zeny ) {
  10513. return BDA_NO_MONEY;
  10514. }
  10515. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10516. return BDA_NO_MONEY;
  10517. sd->bank_vault += money;
  10518. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10519. if( save_settings&CHARSAVE_BANK )
  10520. chrif_save(sd, CSAVE_NORMAL);
  10521. return BDA_SUCCESS;
  10522. }
  10523. /**
  10524. * Withdraw money from bank
  10525. * @param sd
  10526. * @param money Amount of money that will be withdrawn
  10527. **/
  10528. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10529. unsigned int limit_check = money + sd->status.zeny;
  10530. if( money <= 0 ) {
  10531. return BWA_UNKNOWN_ERROR;
  10532. } else if ( money > sd->bank_vault ) {
  10533. return BWA_NO_MONEY;
  10534. } else if ( limit_check > MAX_ZENY ) {
  10535. /* no official response for this scenario exists. */
  10536. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10537. return BWA_UNKNOWN_ERROR;
  10538. }
  10539. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10540. return BWA_NO_MONEY;
  10541. sd->bank_vault -= money;
  10542. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10543. if( save_settings&CHARSAVE_BANK )
  10544. chrif_save(sd, CSAVE_NORMAL);
  10545. return BWA_SUCCESS;
  10546. }
  10547. /**
  10548. * Clear Crimson Marker data from caster
  10549. * @param sd: Player
  10550. **/
  10551. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10552. uint8 i;
  10553. if (!sd)
  10554. return;
  10555. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10556. struct block_list *bl = NULL;
  10557. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10558. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10559. sd->c_marker[i] = 0;
  10560. }
  10561. }
  10562. /**
  10563. * Show version to player
  10564. * @param sd: Player
  10565. **/
  10566. void pc_show_version(struct map_session_data *sd) {
  10567. const char* svn = get_svn_revision();
  10568. char buf[CHAT_SIZE_MAX];
  10569. if( svn[0] != UNKNOWN_VERSION )
  10570. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10571. else {
  10572. const char* git = get_git_hash();
  10573. if( git[0] != UNKNOWN_VERSION )
  10574. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10575. else
  10576. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10577. }
  10578. clif_displaymessage(sd->fd,buf);
  10579. }
  10580. /**
  10581. * Run bonus_script on player
  10582. * @param sd
  10583. * @author [Cydh]
  10584. **/
  10585. void pc_bonus_script(struct map_session_data *sd) {
  10586. int now = gettick();
  10587. struct linkdb_node *node = NULL, *next = NULL;
  10588. if (!sd || !(node = sd->bonus_script.head))
  10589. return;
  10590. while (node) {
  10591. struct s_bonus_script_entry *entry = NULL;
  10592. next = node->next;
  10593. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10594. // Only start timer for new bonus_script
  10595. if (entry->tid == INVALID_TIMER) {
  10596. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  10597. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10598. entry->tick += now;
  10599. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10600. }
  10601. if (entry->script)
  10602. run_script(entry->script, 0, sd->bl.id, 0);
  10603. else
  10604. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10605. }
  10606. node = next;
  10607. }
  10608. }
  10609. /**
  10610. * Add bonus_script to player
  10611. * @param sd Player
  10612. * @param script_str Script string
  10613. * @param dur Duration in ms
  10614. * @param icon EFST
  10615. * @param flag Flags @see enum e_bonus_script_flags
  10616. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10617. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10618. * @author [Cydh]
  10619. **/
  10620. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, uint32 dur, enum efst_types icon, uint16 flag, uint8 type) {
  10621. struct script_code *script = NULL;
  10622. struct linkdb_node *node = NULL;
  10623. struct s_bonus_script_entry *entry = NULL;
  10624. if (!sd)
  10625. return NULL;
  10626. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10627. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10628. return NULL;
  10629. }
  10630. // Duplication checks
  10631. if ((node = sd->bonus_script.head)) {
  10632. while (node) {
  10633. entry = (struct s_bonus_script_entry *)node->data;
  10634. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10635. int newdur = gettick() + dur;
  10636. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10637. settick_timer(entry->tid, newdur);
  10638. script_free_code(script);
  10639. return NULL;
  10640. }
  10641. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10642. break;
  10643. else { // No duplicate bonus
  10644. script_free_code(script);
  10645. return NULL;
  10646. }
  10647. }
  10648. node = node->next;
  10649. }
  10650. }
  10651. CREATE(entry, struct s_bonus_script_entry, 1);
  10652. entry->script_buf = StringBuf_Malloc();
  10653. StringBuf_AppendStr(entry->script_buf, script_str);
  10654. entry->tid = INVALID_TIMER;
  10655. entry->flag = flag;
  10656. entry->icon = icon;
  10657. entry->tick = dur; // Use duration first, on run change to expire time
  10658. entry->type = type;
  10659. entry->script = script;
  10660. sd->bonus_script.count++;
  10661. return entry;
  10662. }
  10663. /**
  10664. * Remove bonus_script data from player
  10665. * @param sd: Target player
  10666. * @param list: Bonus script entry from player
  10667. * @author [Cydh]
  10668. **/
  10669. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10670. if (entry->tid != INVALID_TIMER)
  10671. delete_timer(entry->tid, pc_bonus_script_timer);
  10672. if (entry->script)
  10673. script_free_code(entry->script);
  10674. if (entry->script_buf)
  10675. StringBuf_Free(entry->script_buf);
  10676. if (sd) {
  10677. if (entry->icon != EFST_BLANK)
  10678. clif_status_load(&sd->bl, entry->icon, 0);
  10679. if (sd->bonus_script.count > 0)
  10680. sd->bonus_script.count--;
  10681. }
  10682. aFree(entry);
  10683. }
  10684. /**
  10685. * Do final process if no entry left
  10686. * @param sd
  10687. **/
  10688. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  10689. if (sd->bonus_script.count == 0) {
  10690. if (sd->bonus_script.head && sd->bonus_script.head->data)
  10691. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  10692. linkdb_final(&sd->bonus_script.head);
  10693. }
  10694. }
  10695. /**
  10696. * Timer for bonus_script
  10697. * @param tid
  10698. * @param tick
  10699. * @param id
  10700. * @param data
  10701. * @author [Cydh]
  10702. **/
  10703. TIMER_FUNC(pc_bonus_script_timer){
  10704. struct map_session_data *sd;
  10705. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  10706. sd = map_id2sd(id);
  10707. if (!sd) {
  10708. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  10709. return 0;
  10710. }
  10711. if (tid == INVALID_TIMER)
  10712. return 0;
  10713. if (!sd->bonus_script.head || entry == NULL) {
  10714. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  10715. return 0;
  10716. }
  10717. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10718. pc_bonus_script_free_entry(sd, entry);
  10719. pc_bonus_script_check_final(sd);
  10720. status_calc_pc(sd,SCO_NONE);
  10721. return 0;
  10722. }
  10723. /**
  10724. * Check then clear all active timer(s) of bonus_script data from player based on reason
  10725. * @param sd: Target player
  10726. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  10727. * @author [Cydh]
  10728. **/
  10729. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  10730. struct linkdb_node *node = NULL;
  10731. uint16 count = 0;
  10732. if (!sd || !(node = sd->bonus_script.head))
  10733. return;
  10734. while (node) {
  10735. struct linkdb_node *next = node->next;
  10736. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  10737. if (entry && (
  10738. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  10739. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  10740. (flag&entry->flag) || // Matched flag
  10741. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  10742. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  10743. )
  10744. )
  10745. {
  10746. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10747. pc_bonus_script_free_entry(sd, entry);
  10748. count++;
  10749. }
  10750. node = next;
  10751. }
  10752. pc_bonus_script_check_final(sd);
  10753. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  10754. status_calc_pc(sd,SCO_NONE);
  10755. }
  10756. /** [Cydh]
  10757. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  10758. * @param sd: Target player
  10759. */
  10760. void pc_cell_basilica(struct map_session_data *sd) {
  10761. nullpo_retv(sd);
  10762. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  10763. if (sd->sc.data[SC_BASILICA])
  10764. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  10765. }
  10766. else if (!sd->sc.data[SC_BASILICA])
  10767. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,-1);
  10768. }
  10769. /** [Cydh]
  10770. * Get maximum specified parameter for specified class
  10771. * @param class_: sd->class
  10772. * @param sex: sd->status.sex
  10773. * @param flag: parameter will be checked
  10774. * @return max_param
  10775. */
  10776. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  10777. int idx = -1, class_ = sd->class_;
  10778. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  10779. short max_param = 0;
  10780. switch (param) {
  10781. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  10782. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  10783. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  10784. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  10785. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  10786. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  10787. }
  10788. if (max_param > 0)
  10789. return max_param;
  10790. }
  10791. return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
  10792. ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  10793. ((class_&JOBL_THIRD) ? ((class_&JOBL_UPPER) ? battle_config.max_third_trans_parameter : ((class_&JOBL_BABY) ? battle_config.max_baby_third_parameter : battle_config.max_third_parameter)) :
  10794. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  10795. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  10796. }
  10797. /**
  10798. * Get max ASPD for player based on Class
  10799. * @param sd Player
  10800. * @return ASPD
  10801. */
  10802. short pc_maxaspd(struct map_session_data *sd) {
  10803. nullpo_ret(sd);
  10804. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  10805. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  10806. battle_config.max_aspd ));
  10807. }
  10808. /**
  10809. * Calculates total item-group related bonuses for the given item
  10810. * @param sd Player
  10811. * @param nameid Item ID
  10812. * @return Heal rate
  10813. **/
  10814. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  10815. short bonus = 0;
  10816. uint8 i;
  10817. if (!sd->itemgrouphealrate_count)
  10818. return bonus;
  10819. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10820. uint16 group_id = sd->itemgrouphealrate[i]->group_id, j;
  10821. struct s_item_group_db *group = NULL;
  10822. if (!group_id || !(group = itemdb_group_exists(group_id)))
  10823. continue;
  10824. for (j = 0; j < group->random[0].data_qty; j++) {
  10825. if (group->random[0].data[j].nameid == nameid) {
  10826. bonus += sd->itemgrouphealrate[i]->rate;
  10827. break;
  10828. }
  10829. }
  10830. }
  10831. return bonus;
  10832. }
  10833. /**
  10834. * Calculates total item-group related bonuses for the given item group
  10835. * @param sd Player
  10836. * @param group_id Item Group ID
  10837. * @return Heal rate
  10838. **/
  10839. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  10840. short bonus = 0;
  10841. uint8 i;
  10842. if (!sd->itemgrouphealrate_count)
  10843. return bonus;
  10844. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10845. if (sd->itemgrouphealrate[i]->group_id == group_id)
  10846. return sd->itemgrouphealrate[i]->rate;
  10847. }
  10848. return bonus;
  10849. }
  10850. /**
  10851. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  10852. * @param eqi Item EQI of enum equip_index
  10853. * @param *equip_index Player's equip_index[]
  10854. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  10855. * @return True if item in same inventory index, False if doesn't
  10856. */
  10857. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  10858. if (index < 0 || index >= MAX_INVENTORY)
  10859. return true;
  10860. // Dual weapon checks
  10861. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  10862. return true;
  10863. // Headgear with Mid & Low location
  10864. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  10865. return true;
  10866. // Headgear with Top & Mid or Low location
  10867. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  10868. return true;
  10869. // Headgear with Mid & Low location
  10870. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  10871. return true;
  10872. // Headgear with Top & Mid or Low location
  10873. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  10874. return true;
  10875. return false;
  10876. }
  10877. /**
  10878. * Generate Unique item ID for player
  10879. * @param sd : Player
  10880. * @return A generated Unique item ID
  10881. */
  10882. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  10883. nullpo_ret(sd);
  10884. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  10885. }
  10886. /**
  10887. * Validating skill from player after logged on
  10888. * @param sd
  10889. **/
  10890. void pc_validate_skill(struct map_session_data *sd) {
  10891. if (sd) {
  10892. uint16 i = 0, count = 0;
  10893. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  10894. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  10895. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  10896. for (i = 0; i < MAX_SKILL; i++) {
  10897. uint16 idx = 0;
  10898. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  10899. continue;
  10900. if ((idx = skill_get_index(tmp_skills[i].id))) {
  10901. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  10902. count++;
  10903. }
  10904. else
  10905. 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);
  10906. }
  10907. }
  10908. }
  10909. /**
  10910. * Toggle to remember if the questinfo is displayed yet or not.
  10911. * @param qi_display Display flag
  10912. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  10913. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  10914. **/
  10915. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct questinfo *qi, bool show) {
  10916. if (show) {
  10917. // Check if need to be displayed
  10918. if ((*qi_display) != 1) {
  10919. (*qi_display) = 1;
  10920. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  10921. }
  10922. }
  10923. else {
  10924. // Check if need to be hide
  10925. if ((*qi_display) != 0) {
  10926. (*qi_display) = 0;
  10927. #if PACKETVER >= 20120410
  10928. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  10929. #else
  10930. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  10931. #endif
  10932. }
  10933. }
  10934. }
  10935. /**
  10936. * Show available NPC Quest / Event Icon Check [Kisuka]
  10937. * @param sd Player
  10938. **/
  10939. void pc_show_questinfo(struct map_session_data *sd) {
  10940. #if PACKETVER >= 20090218
  10941. struct questinfo *qi = NULL;
  10942. unsigned short i;
  10943. uint8 j;
  10944. int8 mystate = 0;
  10945. bool failed = false;
  10946. nullpo_retv(sd);
  10947. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10948. return;
  10949. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10950. if (!mapdata->qi_count || !mapdata->qi_data)
  10951. return;
  10952. if (mapdata->qi_count != sd->qi_count)
  10953. return; // init was not called yet
  10954. for(i = 0; i < mapdata->qi_count; i++) {
  10955. qi = &mapdata->qi_data[i];
  10956. if (!qi)
  10957. continue;
  10958. if (quest_check(sd, qi->quest_id, HAVEQUEST) != -1) { // Check if quest is not started
  10959. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10960. continue;
  10961. }
  10962. // Level range checks
  10963. if (sd->status.base_level < qi->min_level || sd->status.base_level > qi->max_level) {
  10964. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10965. continue;
  10966. }
  10967. // Quest requirements
  10968. if (qi->req_count) {
  10969. failed = false;
  10970. for (j = 0; j < qi->req_count; j++) {
  10971. mystate = quest_check(sd, qi->req[j].quest_id, HAVEQUEST);
  10972. mystate = mystate + (mystate < 1);
  10973. if (mystate != qi->req[j].state) {
  10974. failed = true;
  10975. break;
  10976. }
  10977. }
  10978. if (failed) {
  10979. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10980. continue;
  10981. }
  10982. }
  10983. // Job requirements
  10984. if (qi->jobid_count) {
  10985. failed = true;
  10986. for (j = 0; j < qi->jobid_count; j++) {
  10987. if (pc_mapid2jobid(sd->class_,sd->status.sex) == qi->jobid[j]) {
  10988. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10989. failed = false;
  10990. break;
  10991. }
  10992. }
  10993. if (!failed)
  10994. continue;
  10995. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10996. }
  10997. else {
  10998. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10999. }
  11000. }
  11001. #endif
  11002. }
  11003. /**
  11004. * Reinit the questinfo for player when changing map
  11005. * @param sd Player
  11006. **/
  11007. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  11008. #if PACKETVER >= 20090218
  11009. nullpo_retv(sd);
  11010. if (sd->qi_display) {
  11011. aFree(sd->qi_display);
  11012. sd->qi_display = NULL;
  11013. }
  11014. sd->qi_count = 0;
  11015. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11016. return;
  11017. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11018. if (!mapdata->qi_count || !mapdata->qi_data)
  11019. return;
  11020. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_count));
  11021. #endif
  11022. }
  11023. /**
  11024. * Check if a job is allowed to enter the map
  11025. * @param jobid Job ID see enum e_job or sd->status.class_
  11026. * @param m ID -an index- for direct indexing map[] array
  11027. * @return 1 if job is allowed, 0 otherwise
  11028. **/
  11029. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  11030. uint16 idx = 0;
  11031. // Map is other map server.
  11032. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  11033. if (m < 0)
  11034. return true;
  11035. struct map_data *mapdata = map_getmapdata(m);
  11036. if (!mapdata->cell)
  11037. return false;
  11038. if (!pcdb_checkid(jobid))
  11039. return false;
  11040. idx = pc_class2idx(jobid);
  11041. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  11042. return true;
  11043. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  11044. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  11045. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  11046. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  11047. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  11048. (job_info[idx].noenter_map.zone&(8*mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  11049. )
  11050. return false;
  11051. return true;
  11052. }
  11053. /**
  11054. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11055. * @param sd
  11056. **/
  11057. void pc_set_costume_view(struct map_session_data *sd) {
  11058. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11059. struct item_data *id = NULL;
  11060. nullpo_retv(sd);
  11061. head_low = sd->status.head_bottom;
  11062. head_mid = sd->status.head_mid;
  11063. head_top = sd->status.head_top;
  11064. robe = sd->status.robe;
  11065. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11066. //Added check to prevent sending the same look on multiple slots ->
  11067. //causes client to redraw item on top of itself. (suggested by Lupus)
  11068. // Normal headgear checks
  11069. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11070. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11071. sd->status.head_bottom = id->look;
  11072. else
  11073. sd->status.head_bottom = 0;
  11074. }
  11075. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11076. if (!(id->equip&(EQP_HEAD_TOP)))
  11077. sd->status.head_mid = id->look;
  11078. else
  11079. sd->status.head_mid = 0;
  11080. }
  11081. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11082. sd->status.head_top = id->look;
  11083. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11084. sd->status.robe = id->look;
  11085. // Costumes check
  11086. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11087. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11088. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11089. sd->status.head_bottom = id->look;
  11090. else
  11091. sd->status.head_bottom = 0;
  11092. }
  11093. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11094. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11095. sd->status.head_mid = id->look;
  11096. else
  11097. sd->status.head_mid = 0;
  11098. }
  11099. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11100. sd->status.head_top = id->look;
  11101. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11102. sd->status.robe = id->look;
  11103. }
  11104. if (sd->setlook_head_bottom)
  11105. sd->status.head_bottom = sd->setlook_head_bottom;
  11106. if (sd->setlook_head_mid)
  11107. sd->status.head_mid = sd->setlook_head_mid;
  11108. if (sd->setlook_head_top)
  11109. sd->status.head_top = sd->setlook_head_top;
  11110. if (sd->setlook_robe)
  11111. sd->status.robe = sd->setlook_robe;
  11112. if (head_low != sd->status.head_bottom)
  11113. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11114. if (head_mid != sd->status.head_mid)
  11115. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11116. if (head_top != sd->status.head_top)
  11117. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11118. if (robe != sd->status.robe)
  11119. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11120. }
  11121. struct s_attendance_period* pc_attendance_period(){
  11122. uint32 date = date_get(DT_YYYYMMDD);
  11123. for( struct s_attendance_period& period : attendance_periods ){
  11124. if( period.start <= date && period.end >= date ){
  11125. return &period;
  11126. }
  11127. }
  11128. return nullptr;
  11129. }
  11130. bool pc_attendance_enabled(){
  11131. // Check if the attendance feature is disabled
  11132. if( !battle_config.feature_attendance ){
  11133. return false;
  11134. }
  11135. // Check if there is a running attendance period
  11136. return pc_attendance_period() != nullptr;
  11137. }
  11138. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11139. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11140. }
  11141. int32 pc_attendance_counter( struct map_session_data* sd ){
  11142. struct s_attendance_period* period = pc_attendance_period();
  11143. // No running attendance period
  11144. if( period == nullptr ){
  11145. return 0;
  11146. }
  11147. // Get the counter for the current period
  11148. int counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
  11149. // Check if we have a remaining counter from a previous period
  11150. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11151. // Reset the counter to zero
  11152. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11153. return 0;
  11154. }
  11155. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11156. }
  11157. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11158. // If the user's group does not have the permission
  11159. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11160. return;
  11161. }
  11162. // Check if the attendance feature is disabled
  11163. if( !pc_attendance_enabled() ){
  11164. return;
  11165. }
  11166. // Check if the user already got his reward today
  11167. if( pc_attendance_rewarded_today( sd ) ){
  11168. return;
  11169. }
  11170. int32 attendance_counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
  11171. attendance_counter += 1;
  11172. struct s_attendance_period* period = pc_attendance_period();
  11173. if( period == nullptr ){
  11174. return;
  11175. }
  11176. if( period->rewards.size() < attendance_counter ){
  11177. return;
  11178. }
  11179. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11180. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11181. if( save_settings&CHARSAVE_ATTENDANCE )
  11182. chrif_save(sd, CSAVE_NORMAL);
  11183. struct s_attendance_reward& reward = period->rewards.at( attendance_counter - 1 );
  11184. struct mail_message msg;
  11185. memset( &msg, 0, sizeof( struct mail_message ) );
  11186. msg.dest_id = sd->status.char_id;
  11187. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11188. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11189. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11190. msg.item[0].nameid = reward.item_id;
  11191. msg.item[0].amount = reward.amount;
  11192. msg.item[0].identify = 1;
  11193. msg.status = MAIL_NEW;
  11194. msg.type = MAIL_INBOX_NORMAL;
  11195. msg.timestamp = time(NULL);
  11196. intif_Mail_send(0, &msg);
  11197. clif_attendence_response( sd, attendance_counter );
  11198. }
  11199. void pc_attendance_load( std::string path ){
  11200. YAML::Node root;
  11201. try{
  11202. root = YAML::LoadFile( path );
  11203. }catch( ... ){
  11204. ShowError( "pc_attendance_load: Failed to read attendance configuration file \"%s\".\n", path.c_str() );
  11205. return;
  11206. }
  11207. if( root["Attendance"] ){
  11208. YAML::Node attendance = root["Attendance"];
  11209. for( const auto &periodNode : attendance ){
  11210. if( !periodNode["Start"].IsDefined() ){
  11211. ShowError( "pc_attendance_load: Missing \"Start\" for period in line %d.\n", periodNode.Mark().line );
  11212. continue;
  11213. }
  11214. YAML::Node startNode = periodNode["Start"];
  11215. if( !periodNode["End"].IsDefined() ){
  11216. ShowError( "pc_attendance_load: Missing \"End\" for period in line %d.\n", periodNode.Mark().line );
  11217. continue;
  11218. }
  11219. YAML::Node endNode = periodNode["End"];
  11220. if( !periodNode["Rewards"].IsDefined() ){
  11221. ShowError( "pc_attendance_load: Missing \"Rewards\" for period in line %d.\n", periodNode.Mark().line );
  11222. continue;
  11223. }
  11224. YAML::Node rewardsNode = periodNode["Rewards"];
  11225. uint32 start = startNode.as<uint32>();
  11226. uint32 end = endNode.as<uint32>();
  11227. // If the period is outdated already, we do not even bother parsing
  11228. if( end < date_get( DT_YYYYMMDD ) ){
  11229. continue;
  11230. }
  11231. // Collision detection
  11232. bool collision = false;
  11233. for( struct s_attendance_period& period : attendance_periods ){
  11234. // Check if start is inside another period
  11235. if( period.start <= start && start <= period.end ){
  11236. ShowError( "pc_attendance_load: period start %u intersects with period %u-%u.\n", start, period.start, period.end );
  11237. collision = true;
  11238. break;
  11239. }
  11240. // Check if end is inside another period
  11241. if( period.start <= end && end <= period.end ){
  11242. ShowError( "pc_attendance_load: period end %u intersects with period %u-%u.\n", start, period.start, period.end );
  11243. collision = true;
  11244. break;
  11245. }
  11246. }
  11247. if( collision ){
  11248. continue;
  11249. }
  11250. struct s_attendance_period period;
  11251. period.start = start;
  11252. period.end = end;
  11253. for( const auto& rewardNode : rewardsNode ){
  11254. if( !rewardNode["Day"].IsDefined() ){
  11255. ShowError( "pc_attendance_load: No day defined for node in line %d.\n", rewardNode.Mark().line );
  11256. continue;
  11257. }
  11258. uint32 day = rewardNode["Day"].as<uint32>();
  11259. if( !rewardNode["ItemId"].IsDefined() ){
  11260. ShowError( "pc_attendance_load: No reward defined for day %d.\n", day );
  11261. continue;
  11262. }
  11263. YAML::Node itemNode = rewardNode["ItemId"];
  11264. uint16 item_id = itemNode.as<uint16>();
  11265. if( item_id == 0 || !itemdb_exists( item_id ) ){
  11266. ShowError( "pc_attendance_load: Unknown item ID %hu for day %d.\n", item_id, day );
  11267. continue;
  11268. }
  11269. uint16 amount;
  11270. if( rewardNode["Amount"] ){
  11271. amount = rewardNode["Amount"].as<uint16>();
  11272. if( amount == 0 ){
  11273. ShowError( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day );
  11274. amount = 1;
  11275. }else if( amount > MAX_AMOUNT ){
  11276. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day, MAX_AMOUNT );
  11277. amount = MAX_AMOUNT;
  11278. }
  11279. }else{
  11280. amount = 1;
  11281. }
  11282. struct s_attendance_reward* reward = &period.rewards[day - 1];
  11283. reward->item_id = item_id;
  11284. reward->amount = amount;
  11285. }
  11286. bool missing_day = false;
  11287. for( int day = 0; day < period.rewards.size(); day++ ){
  11288. if( !util::map_exists( period.rewards, day ) ){
  11289. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  11290. missing_day = true;
  11291. break;
  11292. }
  11293. }
  11294. if( missing_day ){
  11295. continue;
  11296. }
  11297. attendance_periods.push_back( period );
  11298. }
  11299. }
  11300. }
  11301. void pc_read_attendance(){
  11302. char path[1024];
  11303. sprintf( path, "%s/%sattendance.yml", db_path, DBPATH );
  11304. pc_attendance_load( path );
  11305. sprintf( path, "%s/%s/attendance.yml", db_path, DBIMPORT );
  11306. pc_attendance_load( path );
  11307. }
  11308. /*==========================================
  11309. * pc Init/Terminate
  11310. *------------------------------------------*/
  11311. void do_final_pc(void) {
  11312. db_destroy(itemcd_db);
  11313. do_final_pc_groups();
  11314. ers_destroy(pc_sc_display_ers);
  11315. ers_destroy(pc_itemgrouphealrate_ers);
  11316. ers_destroy(num_reg_ers);
  11317. ers_destroy(str_reg_ers);
  11318. attendance_periods.clear();
  11319. }
  11320. void do_init_pc(void) {
  11321. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11322. pc_readdb();
  11323. pc_read_motd(); // Read MOTD [Valaris]
  11324. pc_read_attendance();
  11325. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11326. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11327. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11328. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11329. add_timer_func_list(pc_autosave, "pc_autosave");
  11330. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11331. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11332. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11333. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11334. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11335. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11336. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11337. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11338. // 0=day, 1=night [Yor]
  11339. night_flag = battle_config.night_at_start ? 1 : 0;
  11340. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11341. int day_duration = battle_config.day_duration;
  11342. int night_duration = battle_config.night_duration;
  11343. // add night/day timer [Yor]
  11344. add_timer_func_list(map_day_timer, "map_day_timer");
  11345. add_timer_func_list(map_night_timer, "map_night_timer");
  11346. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11347. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11348. }
  11349. do_init_pc_groups();
  11350. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11351. pc_itemgrouphealrate_ers = ers_new(sizeof(struct s_pc_itemgrouphealrate), "pc.cpp:pc_itemgrouphealrate_ers", ERS_OPT_NONE);
  11352. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11353. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11354. ers_chunk_size(pc_sc_display_ers, 150);
  11355. ers_chunk_size(num_reg_ers, 300);
  11356. ers_chunk_size(str_reg_ers, 50);
  11357. }