pc.cpp 392 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584
  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 <math.h>
  5. #include <stdlib.h>
  6. #include "../common/cbasetypes.hpp"
  7. #include "../common/core.hpp" // get_svn_revision()
  8. #include "../common/ers.hpp" // ers_destroy
  9. #include "../common/malloc.hpp"
  10. #include "../common/mmo.hpp" //NAME_LENGTH
  11. #include "../common/nullpo.hpp"
  12. #include "../common/random.hpp"
  13. #include "../common/showmsg.hpp"
  14. #include "../common/socket.hpp" // session[]
  15. #include "../common/strlib.hpp" // safestrncpy()
  16. #include "../common/timer.hpp"
  17. #include "../common/utils.hpp"
  18. #include "achievement.hpp"
  19. #include "atcommand.hpp" // get_atcommand_level()
  20. #include "battle.hpp" // battle_config
  21. #include "battleground.hpp"
  22. #include "buyingstore.hpp" // struct s_buyingstore
  23. #include "channel.hpp"
  24. #include "chat.hpp"
  25. #include "chrif.hpp"
  26. #include "clan.hpp"
  27. #include "clif.hpp"
  28. #include "date.hpp" // is_day_of_*()
  29. #include "duel.hpp"
  30. #include "elemental.hpp"
  31. #include "guild.hpp"
  32. #include "homunculus.hpp"
  33. #include "instance.hpp"
  34. #include "intif.hpp"
  35. #include "itemdb.hpp" // MAX_ITEMGROUP
  36. #include "log.hpp"
  37. #include "map.hpp"
  38. #include "mercenary.hpp"
  39. #include "mob.hpp"
  40. #include "npc.hpp"
  41. #include "party.hpp" // party_search()
  42. #include "pc_groups.hpp"
  43. #include "pet.hpp" // pet_unlocktarget()
  44. #include "quest.hpp"
  45. #include "script.hpp" // struct script_reg, struct script_regstr
  46. #include "searchstore.hpp" // struct s_search_store_info
  47. #include "status.hpp" // OPTION_*, struct weapon_atk
  48. #include "storage.hpp"
  49. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  50. #include "vending.hpp" // struct s_vending
  51. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  52. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  53. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  54. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  55. static unsigned int statp[MAX_LEVEL+1];
  56. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  57. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  58. #endif
  59. // h-files are for declarations, not for implementations... [Shinomori]
  60. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  61. // timer for night.day implementation
  62. int day_timer_tid = INVALID_TIMER;
  63. int night_timer_tid = INVALID_TIMER;
  64. struct eri *pc_sc_display_ers = NULL;
  65. struct eri *pc_itemgrouphealrate_ers = NULL;
  66. struct eri *num_reg_ers;
  67. struct eri *str_reg_ers;
  68. int pc_expiration_tid = INVALID_TIMER;
  69. struct fame_list smith_fame_list[MAX_FAME_LIST];
  70. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  71. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  72. struct s_job_info job_info[CLASS_COUNT];
  73. #define MOTD_LINE_SIZE 128
  74. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  75. bool reg_load;
  76. /**
  77. * Translation table from athena equip index to aegis bitmask
  78. */
  79. unsigned int equip_bitmask[EQI_MAX] = {
  80. EQP_ACC_L, // EQI_ACC_L
  81. EQP_ACC_R, // EQI_ACC_R
  82. EQP_SHOES, // EQI_SHOES
  83. EQP_GARMENT, // EQI_GARMENT
  84. EQP_HEAD_LOW, // EQI_HEAD_LOW
  85. EQP_HEAD_MID, // EQI_HEAD_MID
  86. EQP_HEAD_TOP, // EQI_HEAD_TOP
  87. EQP_ARMOR, // EQI_ARMOR
  88. EQP_HAND_L, // EQI_HAND_L
  89. EQP_HAND_R, // EQI_HAND_R
  90. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  91. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  92. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  93. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  94. EQP_AMMO, // EQI_AMMO
  95. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  96. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  97. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  98. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  99. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  100. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  101. };
  102. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  103. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  104. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  105. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  106. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  107. };
  108. void pc_set_reg_load( bool val ){
  109. reg_load = val;
  110. }
  111. /**
  112. * Item Cool Down Delay Saving
  113. * Struct item_cd is not a member of struct map_session_data
  114. * to keep cooldowns in memory between player log-ins.
  115. * All cooldowns are reset when server is restarted.
  116. **/
  117. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  118. struct item_cd {
  119. unsigned int tick[MAX_ITEMDELAYS]; //tick
  120. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  121. };
  122. /**
  123. * Converts a class to its array index for CLASS_COUNT defined arrays.
  124. * Note that it does not do a validity check for speed purposes, where parsing
  125. * player input make sure to use a pcdb_checkid first!
  126. * @param class_ Job ID see enum e_job
  127. * @return Class Index
  128. */
  129. int pc_class2idx(int class_) {
  130. if (class_ >= JOB_NOVICE_HIGH)
  131. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  132. return class_;
  133. }
  134. /**
  135. * Get player's group ID
  136. * @param sd
  137. * @return Group ID
  138. */
  139. int pc_get_group_id(struct map_session_data *sd) {
  140. return sd->group_id;
  141. }
  142. /** Get player's group Level
  143. * @param sd
  144. * @return Group Level
  145. */
  146. int pc_get_group_level(struct map_session_data *sd) {
  147. return sd->group_level;
  148. }
  149. static int pc_invincible_timer(int tid, unsigned int tick, int id, intptr_t data)
  150. {
  151. struct map_session_data *sd;
  152. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  153. return 1;
  154. if(sd->invincible_timer != tid){
  155. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  156. return 0;
  157. }
  158. sd->invincible_timer = INVALID_TIMER;
  159. skill_unit_move(&sd->bl,tick,1);
  160. return 0;
  161. }
  162. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  163. nullpo_retv(sd);
  164. if( sd->invincible_timer != INVALID_TIMER )
  165. delete_timer(sd->invincible_timer,pc_invincible_timer);
  166. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  167. }
  168. void pc_delinvincibletimer(struct map_session_data* sd)
  169. {
  170. nullpo_retv(sd);
  171. if( sd->invincible_timer != INVALID_TIMER )
  172. {
  173. delete_timer(sd->invincible_timer,pc_invincible_timer);
  174. sd->invincible_timer = INVALID_TIMER;
  175. skill_unit_move(&sd->bl,gettick(),1);
  176. }
  177. }
  178. static int pc_spiritball_timer(int tid, unsigned int tick, int id, intptr_t data)
  179. {
  180. struct map_session_data *sd;
  181. int i;
  182. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  183. return 1;
  184. if( sd->spiritball <= 0 )
  185. {
  186. 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);
  187. sd->spiritball = 0;
  188. return 0;
  189. }
  190. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  191. if( i == sd->spiritball )
  192. {
  193. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  194. return 0;
  195. }
  196. sd->spiritball--;
  197. if( i != sd->spiritball )
  198. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  199. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  200. clif_spiritball(&sd->bl);
  201. return 0;
  202. }
  203. /**
  204. * Adds a spiritball to player for 'interval' ms
  205. * @param sd
  206. * @param interval
  207. * @param max
  208. */
  209. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  210. {
  211. int tid;
  212. uint8 i;
  213. nullpo_retv(sd);
  214. if(max > MAX_SPIRITBALL)
  215. max = MAX_SPIRITBALL;
  216. if(sd->spiritball < 0)
  217. sd->spiritball = 0;
  218. if( sd->spiritball && sd->spiritball >= max )
  219. {
  220. if(sd->spirit_timer[0] != INVALID_TIMER)
  221. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  222. sd->spiritball--;
  223. if( sd->spiritball != 0 )
  224. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  225. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  226. }
  227. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  228. 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);
  229. if( i != sd->spiritball )
  230. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  231. sd->spirit_timer[i] = tid;
  232. sd->spiritball++;
  233. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  234. clif_millenniumshield(&sd->bl,sd->spiritball);
  235. else
  236. clif_spiritball(&sd->bl);
  237. }
  238. /**
  239. * Removes number of spiritball from player
  240. * @param sd
  241. * @param count
  242. * @param type 1 = doesn't give client effect
  243. */
  244. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  245. {
  246. uint8 i;
  247. nullpo_retv(sd);
  248. if(sd->spiritball <= 0) {
  249. sd->spiritball = 0;
  250. return;
  251. }
  252. if(count == 0)
  253. return;
  254. if(count > sd->spiritball)
  255. count = sd->spiritball;
  256. sd->spiritball -= count;
  257. if(count > MAX_SPIRITBALL)
  258. count = MAX_SPIRITBALL;
  259. for(i=0;i<count;i++) {
  260. if(sd->spirit_timer[i] != INVALID_TIMER) {
  261. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  262. sd->spirit_timer[i] = INVALID_TIMER;
  263. }
  264. }
  265. for(i=count;i<MAX_SPIRITBALL;i++) {
  266. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  267. sd->spirit_timer[i] = INVALID_TIMER;
  268. }
  269. if(!type) {
  270. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  271. clif_millenniumshield(&sd->bl,sd->spiritball);
  272. else
  273. clif_spiritball(&sd->bl);
  274. }
  275. }
  276. static int pc_check_banding( struct block_list *bl, va_list ap ) {
  277. int *c, *b_sd;
  278. struct block_list *src;
  279. struct map_session_data *tsd;
  280. struct status_change *sc;
  281. nullpo_ret(bl);
  282. nullpo_ret(tsd = (struct map_session_data*)bl);
  283. nullpo_ret(src = va_arg(ap,struct block_list *));
  284. c = va_arg(ap,int *);
  285. b_sd = va_arg(ap, int *);
  286. if(pc_isdead(tsd))
  287. return 0;
  288. sc = status_get_sc(bl);
  289. if( sc && sc->data[SC_BANDING] )
  290. {
  291. b_sd[(*c)++] = tsd->bl.id;
  292. return 1;
  293. }
  294. return 0;
  295. }
  296. int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
  297. int c;
  298. int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
  299. int i, j, hp, extra_hp = 0, tmp_qty = 0;
  300. int range = skill_get_splash(LG_BANDING,skill_lv);
  301. nullpo_ret(sd);
  302. c = 0;
  303. memset(b_sd, 0, sizeof(b_sd));
  304. i = party_foreachsamemap(pc_check_banding,sd,range,&sd->bl,&c,&b_sd);
  305. if( c < 1 ) //just recalc status no need to recalc hp
  306. { // No more Royal Guards in Banding found.
  307. struct status_change *sc;
  308. if( (sc = status_get_sc(&sd->bl)) != NULL && sc->data[SC_BANDING] )
  309. {
  310. sc->data[SC_BANDING]->val2 = 0; // Reset the counter
  311. status_calc_bl(&sd->bl, status_sc2scb_flag(SC_BANDING));
  312. }
  313. return 0;
  314. }
  315. //Add yourself
  316. hp = status_get_hp(&sd->bl);
  317. i++;
  318. // Get total HP of all Royal Guards in party.
  319. for( j = 0; j < i; j++ ){
  320. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  321. if( bsd != NULL )
  322. hp += status_get_hp(&bsd->bl);
  323. }
  324. // Set average HP.
  325. hp = hp / i;
  326. // If a Royal Guard have full HP, give more HP to others that haven't full HP.
  327. for( j = 0; j < i; j++ )
  328. {
  329. int tmp_hp=0;
  330. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  331. if( bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0 ){
  332. extra_hp += tmp_hp;
  333. tmp_qty++;
  334. }
  335. }
  336. if( extra_hp > 0 && tmp_qty > 0 )
  337. hp += extra_hp / tmp_qty;
  338. for( j = 0; j < i; j++ ){
  339. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  340. if( bsd != NULL )
  341. {
  342. struct status_change *sc;
  343. status_set_hp(&bsd->bl,hp,0); // Set hp
  344. if( (sc = status_get_sc(&bsd->bl)) != NULL && sc->data[SC_BANDING] )
  345. {
  346. sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
  347. status_calc_bl(&bsd->bl, status_sc2scb_flag(SC_BANDING)); // Set atk and def.
  348. }
  349. }
  350. }
  351. return c;
  352. }
  353. /**
  354. * Increases a player's fame points and displays a notice to him
  355. * @param sd Player
  356. * @param count Fame point
  357. */
  358. void pc_addfame(struct map_session_data *sd,int count)
  359. {
  360. enum e_rank ranktype;
  361. nullpo_retv(sd);
  362. sd->status.fame += count;
  363. if(sd->status.fame > MAX_FAME)
  364. sd->status.fame = MAX_FAME;
  365. switch(sd->class_&MAPID_UPPERMASK){
  366. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  367. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  368. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  369. default:
  370. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  371. return;
  372. }
  373. clif_update_rankingpoint(sd,ranktype,count);
  374. chrif_updatefamelist(sd);
  375. }
  376. /**
  377. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  378. * @param sd
  379. * @param job Job use enum e_mapid
  380. * @return Rank
  381. */
  382. unsigned char pc_famerank(uint32 char_id, int job)
  383. {
  384. uint8 i;
  385. switch(job){
  386. case MAPID_BLACKSMITH: // Blacksmith
  387. for(i = 0; i < MAX_FAME_LIST; i++){
  388. if(smith_fame_list[i].id == char_id)
  389. return i + 1;
  390. }
  391. break;
  392. case MAPID_ALCHEMIST: // Alchemist
  393. for(i = 0; i < MAX_FAME_LIST; i++){
  394. if(chemist_fame_list[i].id == char_id)
  395. return i + 1;
  396. }
  397. break;
  398. case MAPID_TAEKWON: // Taekwon
  399. for(i = 0; i < MAX_FAME_LIST; i++){
  400. if(taekwon_fame_list[i].id == char_id)
  401. return i + 1;
  402. }
  403. break;
  404. }
  405. return 0;
  406. }
  407. /**
  408. * Restart player's HP & SP value
  409. * @param sd
  410. * @param type Restart type: 1 - Normal Resurection
  411. */
  412. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  413. struct status_data *status, *b_status;
  414. nullpo_retv(sd);
  415. b_status = &sd->base_status;
  416. status = &sd->battle_status;
  417. if (type&1) { //Normal resurrection
  418. status->hp = 1; //Otherwise status_heal may fail if dead.
  419. status_heal(&sd->bl, b_status->hp, 0, 1);
  420. if( status->sp < b_status->sp )
  421. status_set_sp(&sd->bl, b_status->sp, 1);
  422. } else { //Just for saving on the char-server (with values as if respawned)
  423. sd->status.hp = b_status->hp;
  424. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  425. }
  426. }
  427. /*==========================================
  428. Rental System
  429. *------------------------------------------*/
  430. /**
  431. * Ends a rental and removes the item/effect
  432. * @param tid: Tick ID
  433. * @param tick: Timer
  434. * @param id: Timer ID
  435. * @param data: Data
  436. * @return false - failure, true - success
  437. */
  438. int pc_inventory_rental_end(int tid, unsigned int tick, int id, intptr_t data)
  439. {
  440. struct map_session_data *sd = map_id2sd(id);
  441. if( sd == NULL )
  442. return 0;
  443. if( tid != sd->rental_timer ) {
  444. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  445. return 0;
  446. }
  447. pc_inventory_rentals(sd);
  448. return 1;
  449. }
  450. /**
  451. * Removes the rental timer from the player
  452. * @param sd: Player data
  453. */
  454. void pc_inventory_rental_clear(struct map_session_data *sd)
  455. {
  456. if( sd->rental_timer != INVALID_TIMER ) {
  457. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  458. sd->rental_timer = INVALID_TIMER;
  459. }
  460. }
  461. /**
  462. * Check for items in the player's inventory that are rental type
  463. * @param sd: Player data
  464. */
  465. void pc_inventory_rentals(struct map_session_data *sd)
  466. {
  467. int i, c = 0;
  468. unsigned int next_tick = UINT_MAX;
  469. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  470. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  471. continue; // Nothing here
  472. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  473. continue;
  474. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  475. if (sd->inventory_data[i]->unequip_script)
  476. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  477. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  478. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  479. } else {
  480. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  481. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  482. next_tick = umin(expire_tick * 1000U, next_tick);
  483. c++;
  484. }
  485. }
  486. 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
  487. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  488. else
  489. sd->rental_timer = INVALID_TIMER;
  490. }
  491. /**
  492. * Add a rental item to the player and adjusts the rental timer appropriately
  493. * @param sd: Player data
  494. * @param seconds: Rental time
  495. */
  496. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  497. {
  498. unsigned int tick = seconds * 1000;
  499. if( sd == NULL )
  500. return;
  501. if( sd->rental_timer != INVALID_TIMER ) {
  502. const struct TimerData * td;
  503. td = get_timer(sd->rental_timer);
  504. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  505. pc_inventory_rental_clear(sd);
  506. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  507. }
  508. } else
  509. sd->rental_timer = add_timer(gettick() + umin(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  510. }
  511. /**
  512. * Check if the player can sell the current item
  513. * @param sd: map_session_data of the player
  514. * @param item: struct of the checking item
  515. * @return bool 'true' is sellable, 'false' otherwise
  516. */
  517. bool pc_can_sell_item(struct map_session_data *sd, struct item *item) {
  518. struct npc_data *nd;
  519. if (sd == NULL || item == NULL)
  520. return false;
  521. nd = map_id2nd(sd->npc_shopid);
  522. if (battle_config.hide_fav_sell && item->favorite)
  523. return false; //Cannot sell favs (optional config)
  524. if (item->expire_time)
  525. return false; // Cannot Sell Rental Items
  526. if (nd && nd->subtype == NPCTYPE_ITEMSHOP) {
  527. struct item_data *itd;
  528. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND)
  529. return true; // NPCTYPE_ITEMSHOP and bound item config is sellable
  530. if ((itd = itemdb_search(item->nameid)) && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  531. return true; // NPCTYPE_ITEMSHOP and sell restricted item config is sellable
  532. }
  533. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  534. return false;
  535. if (item->bound && !pc_can_give_bounded_items(sd))
  536. return false; // Don't allow sale of bound items
  537. return true;
  538. }
  539. /**
  540. * Determines if player can give / drop / trade / vend items
  541. */
  542. bool pc_can_give_items(struct map_session_data *sd)
  543. {
  544. return pc_has_permission(sd, PC_PERM_TRADE);
  545. }
  546. /**
  547. * Determines if player can give / drop / trade / vend bounded items
  548. */
  549. bool pc_can_give_bounded_items(struct map_session_data *sd)
  550. {
  551. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  552. }
  553. /*==========================================
  554. * Prepares character for saving.
  555. * @param sd
  556. *------------------------------------------*/
  557. void pc_makesavestatus(struct map_session_data *sd) {
  558. nullpo_retv(sd);
  559. if(!battle_config.save_clothcolor)
  560. sd->status.clothes_color = 0;
  561. // Since this is currently not officially released,
  562. // its best to have a forced option to not save body styles.
  563. if(!battle_config.save_body_style)
  564. sd->status.body = 0;
  565. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  566. //status change load/saving. [Skotlex]
  567. #ifdef NEW_CARTS
  568. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  569. #else
  570. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  571. #endif
  572. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  573. if(pc_isdead(sd)){
  574. pc_setrestartvalue(sd, 0);
  575. } else {
  576. sd->status.hp = sd->battle_status.hp;
  577. sd->status.sp = sd->battle_status.sp;
  578. }
  579. sd->status.last_point.map = sd->mapindex;
  580. sd->status.last_point.x = sd->bl.x;
  581. sd->status.last_point.y = sd->bl.y;
  582. return;
  583. }
  584. if(pc_isdead(sd)) {
  585. pc_setrestartvalue(sd, 0);
  586. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  587. } else {
  588. sd->status.hp = sd->battle_status.hp;
  589. sd->status.sp = sd->battle_status.sp;
  590. sd->status.last_point.map = sd->mapindex;
  591. sd->status.last_point.x = sd->bl.x;
  592. sd->status.last_point.y = sd->bl.y;
  593. }
  594. if(map[sd->bl.m].flag.nosave) {
  595. struct map_data *m=&map[sd->bl.m];
  596. if(m->save.map)
  597. memcpy(&sd->status.last_point,&m->save,sizeof(sd->status.last_point));
  598. else
  599. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  600. }
  601. }
  602. /*==========================================
  603. * Off init ? Connection?
  604. *------------------------------------------*/
  605. 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) {
  606. nullpo_retv(sd);
  607. sd->bl.id = account_id;
  608. sd->status.account_id = account_id;
  609. sd->status.char_id = char_id;
  610. sd->status.sex = sex;
  611. sd->login_id1 = login_id1;
  612. sd->login_id2 = 0; // at this point, we can not know the value :(
  613. sd->client_tick = client_tick;
  614. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  615. sd->bl.type = BL_PC;
  616. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  617. sd->canlog_tick = gettick();
  618. //Required to prevent homunculus copuing a base speed of 0.
  619. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  620. }
  621. /**
  622. * Get equip point for an equip
  623. * @param sd
  624. * @param id
  625. */
  626. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  627. int ep = 0;
  628. nullpo_ret(sd);
  629. nullpo_ret(id);
  630. if (!itemdb_isequip2(id))
  631. return 0; //Not equippable by players.
  632. ep = id->equip;
  633. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  634. 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]
  635. if (ep == EQP_WEAPON)
  636. return EQP_ARMS;
  637. if (ep == EQP_SHADOW_WEAPON)
  638. return EQP_SHADOW_ARMS;
  639. }
  640. }
  641. return ep;
  642. }
  643. /**
  644. * Get equip point for an equip
  645. * @param sd
  646. * @param n Equip index in inventory
  647. */
  648. int pc_equippoint(struct map_session_data *sd,int n){
  649. nullpo_ret(sd);
  650. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  651. }
  652. /**
  653. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  654. * @param sd : player session
  655. * @return 0 sucess, 1:invalid sd
  656. */
  657. void pc_setinventorydata(struct map_session_data *sd)
  658. {
  659. uint8 i;
  660. nullpo_retv(sd);
  661. for(i = 0; i < MAX_INVENTORY; i++) {
  662. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  663. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  664. }
  665. }
  666. /**
  667. * 'Calculates' weapon type
  668. * @param sd : Player
  669. */
  670. void pc_calcweapontype(struct map_session_data *sd)
  671. {
  672. nullpo_retv(sd);
  673. // single-hand
  674. if(sd->weapontype2 == W_FIST) {
  675. sd->status.weapon = sd->weapontype1;
  676. return;
  677. }
  678. if(sd->weapontype1 == W_FIST) {
  679. sd->status.weapon = sd->weapontype2;
  680. return;
  681. }
  682. // dual-wield
  683. sd->status.weapon = 0;
  684. switch (sd->weapontype1){
  685. case W_DAGGER:
  686. switch (sd->weapontype2) {
  687. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  688. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  689. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  690. }
  691. break;
  692. case W_1HSWORD:
  693. switch (sd->weapontype2) {
  694. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  695. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  696. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  697. }
  698. break;
  699. case W_1HAXE:
  700. switch (sd->weapontype2) {
  701. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  702. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  703. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  704. }
  705. }
  706. // unknown, default to right hand type
  707. if (!sd->status.weapon)
  708. sd->status.weapon = sd->weapontype1;
  709. }
  710. /**
  711. * Set equip index
  712. * @param sd : Player
  713. */
  714. void pc_setequipindex(struct map_session_data *sd)
  715. {
  716. uint16 i;
  717. nullpo_retv(sd);
  718. for (i = 0; i < EQI_MAX; i++)
  719. sd->equip_index[i] = -1;
  720. for (i = 0; i < MAX_INVENTORY; i++) {
  721. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  722. continue;
  723. if (sd->inventory.u.items_inventory[i].equip) {
  724. uint8 j;
  725. for (j = 0; j < EQI_MAX; j++)
  726. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  727. sd->equip_index[j] = i;
  728. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  729. if (sd->inventory_data[i])
  730. sd->weapontype1 = sd->inventory_data[i]->look;
  731. else
  732. sd->weapontype1 = 0;
  733. }
  734. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  735. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  736. sd->weapontype2 = sd->inventory_data[i]->look;
  737. else
  738. sd->weapontype2 = 0;
  739. }
  740. }
  741. }
  742. pc_calcweapontype(sd);
  743. }
  744. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  745. //{
  746. // int i;
  747. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  748. // struct item_data *data;
  749. //
  750. // //Crafted/made/hatched items.
  751. // if (itemdb_isspecial(item->card[0]))
  752. // return 1;
  753. //
  754. // /* scan for enchant armor gems */
  755. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  756. // s = MAX_SLOTS - 1;
  757. //
  758. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  759. // return( i < s ) ? 0 : 1;
  760. //}
  761. /**
  762. * Check if an item is equiped by player
  763. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  764. * @param sd : player session
  765. * @param nameid : itemid
  766. * @return 1:yes, 0:no
  767. */
  768. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  769. {
  770. uint8 i;
  771. for( i = 0; i < EQI_MAX; i++ )
  772. {
  773. short index = sd->equip_index[i], j;
  774. if( index < 0 )
  775. continue;
  776. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  777. continue;
  778. if( !sd->inventory_data[index] )
  779. continue;
  780. if( sd->inventory_data[index]->nameid == nameid )
  781. return true;
  782. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  783. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  784. return true;
  785. }
  786. }
  787. return false;
  788. }
  789. /**
  790. * Check adoption rules
  791. * @param p1_sd: Player 1
  792. * @param p2_sd: Player 2
  793. * @param b_sd: Player that will be adopted
  794. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  795. * ADOPT_ALREADY_ADOPTED - Already adopted
  796. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  797. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  798. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  799. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  800. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  801. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  802. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  803. */
  804. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  805. {
  806. if( !p1_sd || !p2_sd || !b_sd )
  807. return ADOPT_CHARACTER_NOT_FOUND;
  808. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  809. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  810. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  811. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  812. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  813. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  814. if( p2_sd->status.party_id != p1_sd->status.party_id )
  815. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  816. // Parents need to have their ring equipped
  817. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  818. return ADOPT_EQUIP_RINGS;
  819. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  820. return ADOPT_EQUIP_RINGS;
  821. // Already adopted a baby
  822. if( p1_sd->status.child || p2_sd->status.child ) {
  823. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  824. return ADOPT_MORE_CHILDREN;
  825. }
  826. // Parents need at least lvl 70 to adopt
  827. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  828. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  829. return ADOPT_LEVEL_70;
  830. }
  831. if( b_sd->status.partner_id ) {
  832. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  833. return ADOPT_MARRIED;
  834. }
  835. 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 ) )
  836. return ADOPT_NOT_NOVICE;
  837. return ADOPT_ALLOWED;
  838. }
  839. /*==========================================
  840. * Adoption Process
  841. *------------------------------------------*/
  842. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  843. {
  844. int job, joblevel;
  845. unsigned int jobexp;
  846. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  847. return false;
  848. // Preserve current job levels and progress
  849. joblevel = b_sd->status.job_level;
  850. jobexp = b_sd->status.job_exp;
  851. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  852. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  853. { // Success, proceed to configure parents and baby skills
  854. p1_sd->status.child = b_sd->status.char_id;
  855. p2_sd->status.child = b_sd->status.char_id;
  856. b_sd->status.father = p1_sd->status.char_id;
  857. b_sd->status.mother = p2_sd->status.char_id;
  858. // Restore progress
  859. b_sd->status.job_level = joblevel;
  860. clif_updatestatus(b_sd, SP_JOBLEVEL);
  861. b_sd->status.job_exp = jobexp;
  862. clif_updatestatus(b_sd, SP_JOBEXP);
  863. // Baby Skills
  864. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  865. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  866. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  867. // Parents Skills
  868. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  869. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  870. chrif_save(p1_sd, CSAVE_NORMAL);
  871. chrif_save(p2_sd, CSAVE_NORMAL);
  872. chrif_save(b_sd, CSAVE_NORMAL);
  873. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  874. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  875. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  876. return true;
  877. }
  878. return false; // Job Change Fail
  879. }
  880. /*==========================================
  881. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  882. Returns:
  883. false : Cannot use/equip
  884. true : Can use/equip
  885. * Credits:
  886. [Inkfish] for first idea
  887. [Haru] for third-classes extension
  888. [Cydh] finishing :D
  889. *------------------------------------------*/
  890. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  891. while (1) {
  892. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  893. break;
  894. #ifndef RENEWAL
  895. //allow third classes to use trans. class items
  896. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  897. break;
  898. //third-baby classes can use same item too
  899. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  900. break;
  901. //don't need to decide specific rules for third-classes?
  902. //items for third classes can be used for all third classes
  903. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  904. break;
  905. #else
  906. //trans. classes (exl. third-trans.)
  907. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  908. break;
  909. //baby classes (exl. third-baby)
  910. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  911. break;
  912. //third classes (exl. third-trans. and baby-third)
  913. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  914. break;
  915. //trans-third classes
  916. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  917. break;
  918. //third-baby classes
  919. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  920. break;
  921. #endif
  922. return false;
  923. }
  924. return true;
  925. }
  926. /*=================================================
  927. * Checks if the player can equip the item at index n in inventory.
  928. * @param sd
  929. * @param n Item index in inventory
  930. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  931. *------------------------------------------------*/
  932. uint8 pc_isequip(struct map_session_data *sd,int n)
  933. {
  934. struct item_data *item;
  935. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  936. item = sd->inventory_data[n];
  937. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  938. return ITEM_EQUIP_ACK_OK;
  939. if(item == NULL)
  940. return ITEM_EQUIP_ACK_FAIL;
  941. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  942. return ITEM_EQUIP_ACK_FAILLEVEL;
  943. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  944. return ITEM_EQUIP_ACK_FAILLEVEL;
  945. if(item->sex != 2 && sd->status.sex != item->sex)
  946. return ITEM_EQUIP_ACK_FAIL;
  947. //fail to equip if item is restricted
  948. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  949. return ITEM_EQUIP_ACK_FAIL;
  950. if (sd->sc.count) {
  951. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  952. return ITEM_EQUIP_ACK_FAIL;
  953. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  954. return ITEM_EQUIP_ACK_FAIL;
  955. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  956. return ITEM_EQUIP_ACK_FAIL;
  957. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  958. return ITEM_EQUIP_ACK_FAIL;
  959. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  960. return ITEM_EQUIP_ACK_FAIL;
  961. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  962. return ITEM_EQUIP_ACK_FAIL;
  963. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  964. return ITEM_EQUIP_ACK_FAIL;
  965. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  966. //Spirit of Super Novice equip bonuses. [Skotlex]
  967. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  968. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  969. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  970. switch(item->look) { //In weapons, the look determines type of weapon.
  971. case W_DAGGER: //All level 4 - Daggers
  972. case W_1HSWORD: //All level 4 - 1H Swords
  973. case W_1HAXE: //All level 4 - 1H Axes
  974. case W_MACE: //All level 4 - 1H Maces
  975. case W_STAFF: //All level 4 - 1H Staves
  976. case W_2HSTAFF: //All level 4 - 2H Staves
  977. return ITEM_EQUIP_ACK_OK;
  978. }
  979. }
  980. }
  981. //Not equipable by class. [Skotlex]
  982. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  983. return ITEM_EQUIP_ACK_FAIL;
  984. if (!pc_isItemClass(sd, item))
  985. return ITEM_EQUIP_ACK_FAIL;
  986. return ITEM_EQUIP_ACK_OK;
  987. }
  988. /*==========================================
  989. * No problem with the session id
  990. * set the status that has been sent from char server
  991. *------------------------------------------*/
  992. 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)
  993. {
  994. int i;
  995. unsigned long tick = gettick();
  996. uint32 ip = session[sd->fd]->client_addr;
  997. sd->login_id2 = login_id2;
  998. sd->group_id = group_id;
  999. /* load user permissions */
  1000. pc_group_pc_load(sd);
  1001. memcpy(&sd->status, st, sizeof(*st));
  1002. if (st->sex != sd->status.sex) {
  1003. clif_authfail_fd(sd->fd, 0);
  1004. return false;
  1005. }
  1006. //Set the map-server used job id. [Skotlex]
  1007. i = pc_jobid2mapid(sd->status.class_);
  1008. if (i == -1) { //Invalid class?
  1009. 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);
  1010. sd->status.class_ = JOB_NOVICE;
  1011. sd->class_ = MAPID_NOVICE;
  1012. } else
  1013. sd->class_ = i;
  1014. // Checks and fixes to character status data, that are required
  1015. // in case of configuration change or stuff, which cannot be
  1016. // checked on char-server.
  1017. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1018. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1019. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1020. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1021. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1022. sd->state.connect_new = 1;
  1023. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1024. sd->invincible_timer = INVALID_TIMER;
  1025. sd->npc_timer_id = INVALID_TIMER;
  1026. sd->pvp_timer = INVALID_TIMER;
  1027. sd->expiration_tid = INVALID_TIMER;
  1028. sd->autotrade_tid = INVALID_TIMER;
  1029. #ifdef SECURE_NPCTIMEOUT
  1030. // Initialize to defaults/expected
  1031. sd->npc_idle_timer = INVALID_TIMER;
  1032. sd->npc_idle_tick = tick;
  1033. sd->npc_idle_type = NPCT_INPUT;
  1034. sd->state.ignoretimeout = false;
  1035. #endif
  1036. sd->canuseitem_tick = tick;
  1037. sd->canusecashfood_tick = tick;
  1038. sd->canequip_tick = tick;
  1039. sd->cantalk_tick = tick;
  1040. sd->canskill_tick = tick;
  1041. sd->cansendmail_tick = tick;
  1042. sd->idletime = last_tick;
  1043. for(i = 0; i < MAX_SPIRITBALL; i++)
  1044. sd->spirit_timer[i] = INVALID_TIMER;
  1045. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  1046. sd->autobonus[i].active = INVALID_TIMER;
  1047. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  1048. sd->autobonus2[i].active = INVALID_TIMER;
  1049. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  1050. sd->autobonus3[i].active = INVALID_TIMER;
  1051. if (battle_config.item_auto_get)
  1052. sd->state.autoloot = 10000;
  1053. if (battle_config.disp_experience)
  1054. sd->state.showexp = 1;
  1055. if (battle_config.disp_zeny)
  1056. sd->state.showzeny = 1;
  1057. #ifdef VIP_ENABLE
  1058. if (!battle_config.vip_disp_rate)
  1059. sd->vip.disableshowrate = 1;
  1060. #endif
  1061. if (!(battle_config.display_skill_fail&2))
  1062. sd->state.showdelay = 1;
  1063. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1064. memset(&sd->cart, 0, sizeof(struct s_storage));
  1065. memset(&sd->storage, 0, sizeof(struct s_storage));
  1066. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1067. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1068. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1069. sd->status.option &= ~OPTION_INVISIBLE;
  1070. }
  1071. status_change_init(&sd->bl);
  1072. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1073. unit_dataset(&sd->bl);
  1074. sd->guild_x = -1;
  1075. sd->guild_y = -1;
  1076. sd->delayed_damage = 0;
  1077. // Event Timers
  1078. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1079. sd->eventtimer[i] = INVALID_TIMER;
  1080. // Rental Timer
  1081. sd->rental_timer = INVALID_TIMER;
  1082. for( i = 0; i < 3; i++ )
  1083. sd->hate_mob[i] = -1;
  1084. sd->quest_log = NULL;
  1085. sd->num_quests = 0;
  1086. sd->avail_quests = 0;
  1087. sd->save_quest = false;
  1088. sd->count_rewarp = 0;
  1089. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1090. sd->regs.arrays = NULL;
  1091. sd->vars_dirty = false;
  1092. sd->vars_ok = false;
  1093. sd->vars_received = 0x0;
  1094. sd->qi_display = NULL;
  1095. sd->qi_count = 0;
  1096. //warp player
  1097. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1098. 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);
  1099. // try warping to a default map instead (church graveyard)
  1100. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1101. // if we fail again
  1102. clif_authfail_fd(sd->fd, 0);
  1103. return false;
  1104. }
  1105. }
  1106. clif_authok(sd);
  1107. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1108. sd->die_counter=-1;
  1109. //display login notice
  1110. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1111. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1112. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1113. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1114. sd->status.name, sd->status.account_id, sd->status.char_id,
  1115. CONVIP(ip), sd->group_id);
  1116. // Send friends list
  1117. clif_friendslist_send(sd);
  1118. if( !changing_mapservers ) {
  1119. if (battle_config.display_version == 1)
  1120. pc_show_version(sd);
  1121. // Message of the Day [Valaris]
  1122. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1123. if (battle_config.motd_type)
  1124. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1125. else
  1126. clif_displaymessage(sd->fd, motd_text[i]);
  1127. }
  1128. if (expiration_time != 0)
  1129. sd->expiration_time = expiration_time;
  1130. /**
  1131. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1132. **/
  1133. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1134. }
  1135. pc_validate_skill(sd);
  1136. /* [Ind] */
  1137. sd->sc_display = NULL;
  1138. sd->sc_display_count = 0;
  1139. // Player has not yet received the CashShop list
  1140. sd->status.cashshop_sent = false;
  1141. sd->last_addeditem_index = -1;
  1142. sd->bonus_script.head = NULL;
  1143. sd->bonus_script.count = 0;
  1144. #if PACKETVER >= 20150513
  1145. sd->hatEffectIDs = NULL;
  1146. sd->hatEffectCount = 0;
  1147. #endif
  1148. sd->catch_target_class = PET_CATCH_FAIL;
  1149. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1150. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1151. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1152. clif_updatestatus(sd, SP_BASEEXP);
  1153. }
  1154. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1155. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1156. clif_updatestatus(sd, SP_JOBEXP);
  1157. }
  1158. // Request all registries (auth is considered completed whence they arrive)
  1159. intif_request_registry(sd,7);
  1160. return true;
  1161. }
  1162. /*==========================================
  1163. * Closes a connection because it failed to be authenticated from the char server.
  1164. *------------------------------------------*/
  1165. void pc_authfail(struct map_session_data *sd)
  1166. {
  1167. clif_authfail_fd(sd->fd, 0);
  1168. return;
  1169. }
  1170. /**
  1171. * Player register a bl as hatred
  1172. * @param sd : player session
  1173. * @param pos : hate position [0;2]
  1174. * @param bl : target bl
  1175. * @return false:failed, true:success
  1176. */
  1177. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1178. {
  1179. int class_;
  1180. if (!sd || !bl || pos < 0 || pos > 2)
  1181. return false;
  1182. if (sd->hate_mob[pos] != -1)
  1183. { //Can't change hate targets.
  1184. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1185. return false;
  1186. }
  1187. class_ = status_get_class(bl);
  1188. if (!pcdb_checkid(class_)) {
  1189. unsigned int max_hp = status_get_max_hp(bl);
  1190. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1191. return false;
  1192. if (pos != status_get_size(bl))
  1193. return false; //Wrong size
  1194. }
  1195. sd->hate_mob[pos] = class_;
  1196. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1197. clif_hate_info(sd, pos, class_, 1);
  1198. return true;
  1199. }
  1200. /*==========================================
  1201. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1202. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1203. * See intif_parse_StorageReceived() for item operations [lighta]
  1204. *------------------------------------------*/
  1205. void pc_reg_received(struct map_session_data *sd)
  1206. {
  1207. uint8 i;
  1208. sd->vars_ok = true;
  1209. sd->change_level_2nd = pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR));
  1210. sd->change_level_3rd = pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR));
  1211. sd->die_counter = pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR));
  1212. sd->langtype = pc_readaccountreg(sd, add_str(LANGTYPE_VAR));
  1213. if (msg_checklangtype(sd->langtype,true) < 0)
  1214. sd->langtype = 0; //invalid langtype reset to default
  1215. // Cash shop
  1216. sd->cashPoints = pc_readaccountreg(sd, add_str(CASHPOINT_VAR));
  1217. sd->kafraPoints = pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR));
  1218. // Cooking Exp
  1219. sd->cook_mastery = pc_readglobalreg(sd, add_str(COOKMASTERY_VAR));
  1220. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1221. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1222. sd->mission_mobid = pc_readglobalreg(sd, add_str(TKMISSIONID_VAR));
  1223. sd->mission_count = pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR));
  1224. }
  1225. if (battle_config.feature_banking)
  1226. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1227. if (battle_config.feature_roulette) {
  1228. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1229. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1230. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1231. }
  1232. sd->roulette.prizeIdx = -1;
  1233. //SG map and mob read [Komurka]
  1234. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1235. uint16 j;
  1236. if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
  1237. sd->feel_map[i].index = j;
  1238. sd->feel_map[i].m = map_mapindex2mapid(j);
  1239. } else {
  1240. sd->feel_map[i].index = 0;
  1241. sd->feel_map[i].m = -1;
  1242. }
  1243. sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
  1244. }
  1245. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1246. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
  1247. sd->cloneskill_idx = skill_get_index(skid);
  1248. if (sd->cloneskill_idx > 0) {
  1249. sd->status.skill[sd->cloneskill_idx].id = skid;
  1250. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
  1251. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1252. sd->status.skill[sd->cloneskill_idx].lv = i;
  1253. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1254. }
  1255. }
  1256. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1257. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
  1258. sd->reproduceskill_idx = skill_get_index(skid);
  1259. if (sd->reproduceskill_idx > 0) {
  1260. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1261. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
  1262. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1263. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1264. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1265. }
  1266. }
  1267. //Weird... maybe registries were reloaded?
  1268. if (sd->state.active)
  1269. return;
  1270. sd->state.active = 1;
  1271. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1272. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1273. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1274. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1275. if (sd->status.party_id)
  1276. party_member_joined(sd);
  1277. if (sd->status.guild_id)
  1278. guild_member_joined(sd);
  1279. if( sd->status.clan_id )
  1280. clan_member_joined(sd);
  1281. // pet
  1282. if (sd->status.pet_id > 0)
  1283. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1284. // Homunculus [albator]
  1285. if( sd->status.hom_id > 0 )
  1286. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1287. if( sd->status.mer_id > 0 )
  1288. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1289. if( sd->status.ele_id > 0 )
  1290. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1291. map_addiddb(&sd->bl);
  1292. map_delnickdb(sd->status.char_id, sd->status.name);
  1293. if (!chrif_auth_finished(sd))
  1294. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1295. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1296. chrif_bsdata_request(sd->status.char_id);
  1297. #ifdef VIP_ENABLE
  1298. sd->vip.time = 0;
  1299. sd->vip.enabled = 0;
  1300. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1301. #endif
  1302. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1303. intif_request_questlog(sd);
  1304. if (battle_config.feature_achievement) {
  1305. sd->achievement_data.total_score = 0;
  1306. sd->achievement_data.level = 0;
  1307. sd->achievement_data.save = false;
  1308. sd->achievement_data.count = 0;
  1309. sd->achievement_data.incompleteCount = 0;
  1310. sd->achievement_data.achievements = NULL;
  1311. intif_request_achievements(sd->status.char_id);
  1312. }
  1313. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1314. sd->state.connect_new = 1;
  1315. clif_parse_LoadEndAck(sd->fd, sd);
  1316. }
  1317. if( pc_isinvisible(sd) ) {
  1318. sd->vd.class_ = JT_INVISIBLE;
  1319. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1320. // decrement the number of pvp players on the map
  1321. map[sd->bl.m].users_pvp--;
  1322. if( map[sd->bl.m].flag.pvp && !map[sd->bl.m].flag.pvp_nocalcrank && sd->pvp_timer != INVALID_TIMER ){
  1323. // unregister the player for ranking
  1324. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1325. sd->pvp_timer = INVALID_TIMER;
  1326. }
  1327. clif_changeoption( &sd->bl );
  1328. }
  1329. channel_autojoin(sd);
  1330. }
  1331. static int pc_calc_skillpoint(struct map_session_data* sd)
  1332. {
  1333. uint16 i, skill_point = 0;
  1334. nullpo_ret(sd);
  1335. for(i = 1; i < MAX_SKILL; i++) {
  1336. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1337. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1338. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1339. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1340. )
  1341. {
  1342. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1343. skill_point += sd->status.skill[i].lv;
  1344. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1345. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1346. }
  1347. }
  1348. }
  1349. return skill_point;
  1350. }
  1351. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1352. uint16 i = 0;
  1353. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1354. return false;
  1355. /**
  1356. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1357. * skill tree and since they have no icons they'll give resource errors
  1358. * Get ALL skills except npc/guild ones. [Skotlex]
  1359. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1360. **/
  1361. for( i = 0; i < MAX_SKILL; i++ ) {
  1362. uint16 skill_id = skill_idx2id(i);
  1363. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1364. continue;
  1365. switch (skill_id) {
  1366. case SM_SELFPROVOKE:
  1367. case AB_DUPLELIGHT_MELEE:
  1368. case AB_DUPLELIGHT_MAGIC:
  1369. case WL_CHAINLIGHTNING_ATK:
  1370. case WL_TETRAVORTEX_FIRE:
  1371. case WL_TETRAVORTEX_WATER:
  1372. case WL_TETRAVORTEX_WIND:
  1373. case WL_TETRAVORTEX_GROUND:
  1374. case WL_SUMMON_ATK_FIRE:
  1375. case WL_SUMMON_ATK_WIND:
  1376. case WL_SUMMON_ATK_WATER:
  1377. case WL_SUMMON_ATK_GROUND:
  1378. case LG_OVERBRAND_BRANDISH:
  1379. case LG_OVERBRAND_PLUSATK:
  1380. case WM_SEVERE_RAINSTORM_MELEE:
  1381. case RL_R_TRIP_PLUSATK:
  1382. case SG_DEVIL:
  1383. case MO_TRIPLEATTACK:
  1384. case RG_SNATCHER:
  1385. continue;
  1386. default:
  1387. {
  1388. uint8 lv = (uint8)skill_get_max(skill_id);
  1389. if (lv > 0) {
  1390. sd->status.skill[i].id = skill_id;
  1391. if (addlv)
  1392. sd->status.skill[i].lv = lv;
  1393. }
  1394. }
  1395. break;
  1396. }
  1397. }
  1398. return true;
  1399. }
  1400. /*==========================================
  1401. * Calculation of skill level.
  1402. * @param sd
  1403. *------------------------------------------*/
  1404. void pc_calc_skilltree(struct map_session_data *sd)
  1405. {
  1406. int i, flag;
  1407. int c = 0;
  1408. nullpo_retv(sd);
  1409. i = pc_calc_skilltree_normalize_job(sd);
  1410. c = pc_mapid2jobid(i, sd->status.sex);
  1411. if( c == -1 )
  1412. { //Unable to normalize job??
  1413. 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);
  1414. return;
  1415. }
  1416. c = pc_class2idx(c);
  1417. for( i = 0; i < MAX_SKILL; i++ ) {
  1418. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1419. sd->status.skill[i].id = 0; //First clear skills.
  1420. /* permanent skills that must be re-checked */
  1421. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1422. uint16 sk_id = skill_idx2id(i);
  1423. if (!sk_id) {
  1424. sd->status.skill[i].id = 0;
  1425. sd->status.skill[i].lv = 0;
  1426. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1427. continue;
  1428. }
  1429. switch (sk_id) {
  1430. case NV_TRICKDEAD:
  1431. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1432. sd->status.skill[i].id = 0;
  1433. sd->status.skill[i].lv = 0;
  1434. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1435. }
  1436. break;
  1437. }
  1438. }
  1439. }
  1440. for( i = 0; i < MAX_SKILL; i++ ) {
  1441. uint16 skill_id = 0;
  1442. // Restore original level of skills after deleting earned skills.
  1443. 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 ) {
  1444. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1445. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1446. }
  1447. //Enable Bard/Dancer spirit linked skills.
  1448. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1449. continue;
  1450. if( &sd->sc && sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1451. //Link Dancer skills to bard.
  1452. if( sd->status.sex ) {
  1453. if( sd->status.skill[i-8].lv < 10 )
  1454. continue;
  1455. sd->status.skill[i].id = skill_id;
  1456. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1457. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1458. }
  1459. //Link Bard skills to dancer.
  1460. else {
  1461. if( sd->status.skill[i].lv < 10 )
  1462. continue;
  1463. sd->status.skill[i-8].id = skill_id - 8;
  1464. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1465. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1466. }
  1467. }
  1468. }
  1469. // Removes Taekwon Ranker skill bonus
  1470. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1471. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1472. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1473. uint16 sk_id = skill_tree[c_][i].skill_id;
  1474. uint16 sk_idx = 0;
  1475. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1476. continue;
  1477. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1478. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1479. continue;
  1480. sd->status.skill[sk_idx].id = 0;
  1481. }
  1482. }
  1483. }
  1484. // Grant all skills
  1485. pc_grant_allskills(sd, false);
  1486. do {
  1487. uint16 skid = 0;
  1488. flag = 0;
  1489. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++) {
  1490. bool fail = false;
  1491. uint16 sk_idx = skill_get_index(skid);
  1492. if (sd->status.skill[sk_idx].id)
  1493. continue; //Skill already known.
  1494. if (!battle_config.skillfree) {
  1495. uint8 j;
  1496. // Checking required skills
  1497. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1498. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1499. uint16 sk_need_idx = 0;
  1500. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1501. short sk_need = sk_need_id;
  1502. 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)
  1503. sk_need = 0; //Not learned.
  1504. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1505. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1506. else
  1507. sk_need = pc_checkskill(sd,sk_need_id);
  1508. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1509. fail = true;
  1510. break;
  1511. }
  1512. }
  1513. }
  1514. if (sd->status.base_level < skill_tree[c][i].baselv) { //We need to get the actual class in this case
  1515. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1516. class_ = pc_class2idx(class_);
  1517. if (class_ == c || (class_ != c && sd->status.base_level < skill_tree[class_][i].baselv))
  1518. fail = true; // base level requirement wasn't satisfied
  1519. }
  1520. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1521. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1522. class_ = pc_class2idx(class_);
  1523. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1524. fail = true; // job level requirement wasn't satisfied
  1525. }
  1526. }
  1527. if (!fail) {
  1528. int inf2 = skill_get_inf2(skid);
  1529. if (!sd->status.skill[sk_idx].lv && (
  1530. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1531. inf2&INF2_WEDDING_SKILL ||
  1532. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1533. ))
  1534. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1535. sd->status.skill[sk_idx].id = skid;
  1536. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1537. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1538. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1539. }
  1540. flag = 1; // skill list has changed, perform another pass
  1541. }
  1542. }
  1543. } while(flag);
  1544. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1545. unsigned short skid = 0;
  1546. /* Taekwon Ranker Bonus Skill Tree
  1547. ============================================
  1548. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1549. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1550. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1551. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1552. uint16 sk_idx = 0;
  1553. if (!(sk_idx = skill_get_index(skid)))
  1554. continue;
  1555. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1556. continue; //Do not include Quest/Wedding skills.
  1557. if( sd->status.skill[sk_idx].id == 0 ) {
  1558. sd->status.skill[sk_idx].id = skid;
  1559. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1560. } else if( skid != NV_BASIC )
  1561. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1562. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1563. }
  1564. }
  1565. }
  1566. //Checks if you can learn a new skill after having leveled up a skill.
  1567. static void pc_check_skilltree(struct map_session_data *sd)
  1568. {
  1569. int i, flag = 0;
  1570. int c = 0;
  1571. if (battle_config.skillfree)
  1572. return; //Function serves no purpose if this is set
  1573. i = pc_calc_skilltree_normalize_job(sd);
  1574. c = pc_mapid2jobid(i, sd->status.sex);
  1575. if (c == -1) { //Unable to normalize job??
  1576. 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);
  1577. return;
  1578. }
  1579. c = pc_class2idx(c);
  1580. do {
  1581. uint16 skid = 0;
  1582. flag = 0;
  1583. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1584. uint16 sk_idx = skill_get_index(skid);
  1585. bool fail = false;
  1586. uint8 j = 0;
  1587. if (sd->status.skill[sk_idx].id) //Already learned
  1588. continue;
  1589. // Checking required skills
  1590. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1591. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1592. uint16 sk_need_idx = 0;
  1593. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1594. short sk_need = sk_need_id;
  1595. 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)
  1596. sk_need = 0; //Not learned.
  1597. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1598. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1599. else
  1600. sk_need = pc_checkskill(sd,sk_need_id);
  1601. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1602. fail = true;
  1603. break;
  1604. }
  1605. }
  1606. }
  1607. if( fail )
  1608. continue;
  1609. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1610. continue;
  1611. j = skill_get_inf2(skid);
  1612. if( !sd->status.skill[sk_idx].lv && (
  1613. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1614. j&INF2_WEDDING_SKILL ||
  1615. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1616. ) )
  1617. continue; //Cannot be learned via normal means.
  1618. sd->status.skill[sk_idx].id = skid;
  1619. flag = 1;
  1620. }
  1621. } while(flag);
  1622. }
  1623. // Make sure all the skills are in the correct condition
  1624. // before persisting to the backend.. [MouseJstr]
  1625. void pc_clean_skilltree(struct map_session_data *sd)
  1626. {
  1627. uint16 i;
  1628. for (i = 0; i < MAX_SKILL; i++){
  1629. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1630. sd->status.skill[i].id = 0;
  1631. sd->status.skill[i].lv = 0;
  1632. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1633. }
  1634. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1635. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1636. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1637. }
  1638. }
  1639. }
  1640. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1641. {
  1642. int skill_point, novice_skills;
  1643. int c = sd->class_;
  1644. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1645. return c;
  1646. skill_point = pc_calc_skillpoint(sd);
  1647. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1648. // limit 1st class and above to novice job levels
  1649. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1650. {
  1651. c = MAPID_NOVICE;
  1652. }
  1653. // limit 2nd class and above to first class job levels (super novices are exempt)
  1654. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1655. {
  1656. // regenerate change_level_2nd
  1657. if (!sd->change_level_2nd)
  1658. {
  1659. if (sd->class_&JOBL_THIRD)
  1660. {
  1661. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1662. if (!sd->change_level_3rd)
  1663. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1664. else
  1665. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1666. - (sd->status.job_level - 1)
  1667. - (sd->change_level_3rd - 1)
  1668. - novice_skills;
  1669. }
  1670. else
  1671. {
  1672. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1673. - (sd->status.job_level - 1)
  1674. - novice_skills;
  1675. }
  1676. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1677. }
  1678. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1679. {
  1680. c &= MAPID_BASEMASK;
  1681. }
  1682. // limit 3rd class to 2nd class/trans job levels
  1683. else if(sd->class_&JOBL_THIRD)
  1684. {
  1685. // regenerate change_level_3rd
  1686. if (!sd->change_level_3rd)
  1687. {
  1688. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1689. - (sd->status.job_level - 1)
  1690. - (sd->change_level_2nd - 1)
  1691. - novice_skills;
  1692. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1693. }
  1694. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1695. c &= MAPID_UPPERMASK;
  1696. }
  1697. }
  1698. // restore non-limiting flags
  1699. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1700. return c;
  1701. }
  1702. /*==========================================
  1703. * Updates the weight status
  1704. *------------------------------------------
  1705. * 1: overweight 50%
  1706. * 2: overweight 90%
  1707. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1708. */
  1709. void pc_updateweightstatus(struct map_session_data *sd)
  1710. {
  1711. int old_overweight;
  1712. int new_overweight;
  1713. nullpo_retv(sd);
  1714. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1715. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  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. int pc_endautobonus(int tid, unsigned int tick, int id, intptr_t data)
  2066. {
  2067. struct map_session_data *sd = map_id2sd(id);
  2068. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  2069. nullpo_ret(sd);
  2070. nullpo_ret(autobonus);
  2071. autobonus->active = INVALID_TIMER;
  2072. sd->state.autobonus &= ~autobonus->pos;
  2073. status_calc_pc(sd,SCO_FORCE);
  2074. return 0;
  2075. }
  2076. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2077. {
  2078. uint8 i;
  2079. struct weapon_data* wd;
  2080. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2081. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  2082. if (i == MAX_PC_BONUS)
  2083. {
  2084. ShowWarning("pc_bonus_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2085. return;
  2086. }
  2087. if (!(flag&BF_RANGEMASK))
  2088. flag |= BF_SHORT|BF_LONG;
  2089. if (!(flag&BF_WEAPONMASK))
  2090. flag |= BF_WEAPON;
  2091. if (!(flag&BF_SKILLMASK))
  2092. {
  2093. if (flag&(BF_MAGIC|BF_MISC))
  2094. flag |= BF_SKILL;
  2095. if (flag&BF_WEAPON)
  2096. flag |= BF_NORMAL|BF_SKILL;
  2097. }
  2098. wd->addele2[i].ele = ele;
  2099. wd->addele2[i].rate = rate;
  2100. wd->addele2[i].flag = flag;
  2101. }
  2102. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2103. {
  2104. uint8 i;
  2105. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  2106. if (i == MAX_PC_BONUS)
  2107. {
  2108. ShowWarning("pc_bonus_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2109. return;
  2110. }
  2111. if (!(flag&BF_RANGEMASK))
  2112. flag |= BF_SHORT|BF_LONG;
  2113. if (!(flag&BF_WEAPONMASK))
  2114. flag |= BF_WEAPON;
  2115. if (!(flag&BF_SKILLMASK))
  2116. {
  2117. if (flag&(BF_MAGIC|BF_MISC))
  2118. flag |= BF_SKILL;
  2119. if (flag&BF_WEAPON)
  2120. flag |= BF_NORMAL|BF_SKILL;
  2121. }
  2122. sd->subele2[i].ele = ele;
  2123. sd->subele2[i].rate = rate;
  2124. sd->subele2[i].flag = flag;
  2125. }
  2126. /** Add item group heal rate bonus to player
  2127. * @param sd Player
  2128. * @param group_id Item Group ID
  2129. * @param rate
  2130. * @author Cydh
  2131. */
  2132. void pc_itemgrouphealrate(struct map_session_data *sd, uint16 group_id, short rate) {
  2133. struct s_pc_itemgrouphealrate *entry;
  2134. uint8 i;
  2135. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  2136. if (sd->itemgrouphealrate[i]->group_id == group_id)
  2137. break;
  2138. }
  2139. if (i != sd->itemgrouphealrate_count) {
  2140. sd->itemgrouphealrate[i]->rate += rate;
  2141. return;
  2142. }
  2143. if (i >= UINT8_MAX) {
  2144. ShowError("pc_itemgrouphealrate_add: Reached max (%d) possible bonuses for this player %d\n", UINT8_MAX);
  2145. return;
  2146. }
  2147. entry = ers_alloc(pc_itemgrouphealrate_ers, struct s_pc_itemgrouphealrate);
  2148. entry->group_id = group_id;
  2149. entry->rate = rate;
  2150. RECREATE(sd->itemgrouphealrate, struct s_pc_itemgrouphealrate *, sd->itemgrouphealrate_count+1);
  2151. sd->itemgrouphealrate[sd->itemgrouphealrate_count++] = entry;
  2152. }
  2153. /** Clear item group heal rate from player
  2154. * @param sd Player
  2155. * @author Cydh
  2156. */
  2157. void pc_itemgrouphealrate_clear(struct map_session_data *sd) {
  2158. if (!sd || !sd->itemgrouphealrate_count)
  2159. return;
  2160. else {
  2161. uint8 i;
  2162. for( i = 0; i < sd->itemgrouphealrate_count; i++ )
  2163. ers_free(pc_itemgrouphealrate_ers, sd->itemgrouphealrate[i]);
  2164. sd->itemgrouphealrate_count = 0;
  2165. aFree(sd->itemgrouphealrate);
  2166. sd->itemgrouphealrate = NULL;
  2167. }
  2168. }
  2169. /*==========================================
  2170. * Add a bonus(type) to player sd
  2171. * format: bonus bBonusName,val;
  2172. * @param sd
  2173. * @param type Bonus type used by bBonusName
  2174. * @param val Value that usually for rate or fixed value
  2175. *------------------------------------------*/
  2176. void pc_bonus(struct map_session_data *sd,int type,int val)
  2177. {
  2178. struct status_data *status;
  2179. int bonus;
  2180. nullpo_retv(sd);
  2181. status = &sd->base_status;
  2182. switch(type){
  2183. case SP_STR:
  2184. case SP_AGI:
  2185. case SP_VIT:
  2186. case SP_INT:
  2187. case SP_DEX:
  2188. case SP_LUK:
  2189. if(sd->state.lr_flag != 2)
  2190. sd->param_bonus[type-SP_STR]+=val;
  2191. break;
  2192. case SP_ATK1:
  2193. if(!sd->state.lr_flag) {
  2194. bonus = status->rhw.atk + val;
  2195. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2196. }
  2197. else if(sd->state.lr_flag == 1) {
  2198. bonus = status->lhw.atk + val;
  2199. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2200. }
  2201. break;
  2202. case SP_ATK2:
  2203. if(!sd->state.lr_flag) {
  2204. bonus = status->rhw.atk2 + val;
  2205. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2206. }
  2207. else if(sd->state.lr_flag == 1) {
  2208. bonus = status->lhw.atk2 + val;
  2209. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2210. }
  2211. break;
  2212. case SP_BASE_ATK:
  2213. if(sd->state.lr_flag != 2) {
  2214. #ifdef RENEWAL
  2215. bonus = sd->bonus.eatk + val;
  2216. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2217. #else
  2218. bonus = status->batk + val;
  2219. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2220. #endif
  2221. }
  2222. break;
  2223. case SP_DEF1:
  2224. if(sd->state.lr_flag != 2) {
  2225. bonus = status->def + val;
  2226. #ifdef RENEWAL
  2227. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2228. #else
  2229. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2230. #endif
  2231. }
  2232. break;
  2233. case SP_DEF2:
  2234. if(sd->state.lr_flag != 2) {
  2235. bonus = status->def2 + val;
  2236. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2237. }
  2238. break;
  2239. case SP_MDEF1:
  2240. if(sd->state.lr_flag != 2) {
  2241. bonus = status->mdef + val;
  2242. #ifdef RENEWAL
  2243. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2244. #else
  2245. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2246. #endif
  2247. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2248. sd->bonus.shieldmdef += bonus;
  2249. }
  2250. }
  2251. break;
  2252. case SP_MDEF2:
  2253. if(sd->state.lr_flag != 2) {
  2254. bonus = status->mdef2 + val;
  2255. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2256. }
  2257. break;
  2258. case SP_HIT:
  2259. if(sd->state.lr_flag != 2) {
  2260. bonus = status->hit + val;
  2261. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2262. } else
  2263. sd->bonus.arrow_hit+=val;
  2264. break;
  2265. case SP_FLEE1:
  2266. if(sd->state.lr_flag != 2) {
  2267. bonus = status->flee + val;
  2268. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2269. }
  2270. break;
  2271. case SP_FLEE2:
  2272. if(sd->state.lr_flag != 2) {
  2273. bonus = status->flee2 + val*10;
  2274. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2275. }
  2276. break;
  2277. case SP_CRITICAL:
  2278. if(sd->state.lr_flag != 2) {
  2279. bonus = status->cri + val*10;
  2280. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2281. } else
  2282. sd->bonus.arrow_cri += val*10;
  2283. break;
  2284. case SP_ATKELE:
  2285. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2286. switch (sd->state.lr_flag)
  2287. {
  2288. case 2:
  2289. switch (sd->status.weapon) {
  2290. case W_BOW:
  2291. case W_REVOLVER:
  2292. case W_RIFLE:
  2293. case W_GATLING:
  2294. case W_SHOTGUN:
  2295. case W_GRENADE:
  2296. //Become weapon element.
  2297. status->rhw.ele=val;
  2298. break;
  2299. default: //Become arrow element.
  2300. sd->bonus.arrow_ele=val;
  2301. break;
  2302. }
  2303. break;
  2304. case 1:
  2305. status->lhw.ele=val;
  2306. break;
  2307. default:
  2308. status->rhw.ele=val;
  2309. break;
  2310. }
  2311. break;
  2312. case SP_DEFELE:
  2313. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2314. if(sd->state.lr_flag != 2)
  2315. status->def_ele=val;
  2316. break;
  2317. case SP_MAXHP:
  2318. if(sd->state.lr_flag == 2)
  2319. break;
  2320. sd->bonus.hp += val;
  2321. break;
  2322. case SP_MAXSP:
  2323. if(sd->state.lr_flag == 2)
  2324. break;
  2325. sd->bonus.sp += val;
  2326. break;
  2327. case SP_MAXHPRATE:
  2328. if(sd->state.lr_flag != 2)
  2329. sd->hprate+=val;
  2330. break;
  2331. case SP_MAXSPRATE:
  2332. if(sd->state.lr_flag != 2)
  2333. sd->sprate+=val;
  2334. break;
  2335. case SP_SPRATE:
  2336. if(sd->state.lr_flag != 2)
  2337. sd->dsprate+=val;
  2338. break;
  2339. case SP_ATTACKRANGE:
  2340. switch (sd->state.lr_flag) {
  2341. case 2:
  2342. switch (sd->status.weapon) {
  2343. case W_BOW:
  2344. case W_REVOLVER:
  2345. case W_RIFLE:
  2346. case W_GATLING:
  2347. case W_SHOTGUN:
  2348. case W_GRENADE:
  2349. status->rhw.range += val;
  2350. }
  2351. break;
  2352. case 1:
  2353. status->lhw.range += val;
  2354. break;
  2355. default:
  2356. status->rhw.range += val;
  2357. break;
  2358. }
  2359. break;
  2360. case SP_SPEED_RATE: //Non stackable increase
  2361. if(sd->state.lr_flag != 2)
  2362. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2363. break;
  2364. case SP_SPEED_ADDRATE: //Stackable increase
  2365. if(sd->state.lr_flag != 2)
  2366. sd->bonus.speed_add_rate -= val;
  2367. break;
  2368. case SP_ASPD: //Raw increase
  2369. if(sd->state.lr_flag != 2)
  2370. sd->bonus.aspd_add -= 10*val;
  2371. break;
  2372. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2373. if(sd->state.lr_flag != 2)
  2374. #ifndef RENEWAL_ASPD
  2375. status->aspd_rate -= 10*val;
  2376. #else
  2377. status->aspd_rate2 += val;
  2378. #endif
  2379. break;
  2380. case SP_HP_RECOV_RATE:
  2381. if(sd->state.lr_flag != 2)
  2382. sd->hprecov_rate += val;
  2383. break;
  2384. case SP_SP_RECOV_RATE:
  2385. if(sd->state.lr_flag != 2)
  2386. sd->sprecov_rate += val;
  2387. break;
  2388. case SP_CRITICAL_DEF:
  2389. if(sd->state.lr_flag != 2)
  2390. sd->bonus.critical_def += val;
  2391. break;
  2392. case SP_NEAR_ATK_DEF:
  2393. if(sd->state.lr_flag != 2)
  2394. sd->bonus.near_attack_def_rate += val;
  2395. break;
  2396. case SP_LONG_ATK_DEF:
  2397. if(sd->state.lr_flag != 2)
  2398. sd->bonus.long_attack_def_rate += val;
  2399. break;
  2400. case SP_DOUBLE_RATE:
  2401. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2402. sd->bonus.double_rate = val;
  2403. break;
  2404. case SP_DOUBLE_ADD_RATE:
  2405. if(sd->state.lr_flag == 0)
  2406. sd->bonus.double_add_rate += val;
  2407. break;
  2408. case SP_MATK_RATE:
  2409. if(sd->state.lr_flag != 2)
  2410. sd->matk_rate += val;
  2411. break;
  2412. case SP_IGNORE_DEF_ELE:
  2413. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2414. if(!sd->state.lr_flag)
  2415. sd->right_weapon.ignore_def_ele |= 1<<val;
  2416. else if(sd->state.lr_flag == 1)
  2417. sd->left_weapon.ignore_def_ele |= 1<<val;
  2418. break;
  2419. case SP_IGNORE_DEF_RACE:
  2420. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2421. if(!sd->state.lr_flag)
  2422. sd->right_weapon.ignore_def_race |= 1<<val;
  2423. else if(sd->state.lr_flag == 1)
  2424. sd->left_weapon.ignore_def_race |= 1<<val;
  2425. break;
  2426. case SP_IGNORE_DEF_CLASS:
  2427. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2428. if(!sd->state.lr_flag)
  2429. sd->right_weapon.ignore_def_class |= 1<<val;
  2430. else if(sd->state.lr_flag == 1)
  2431. sd->left_weapon.ignore_def_class |= 1<<val;
  2432. break;
  2433. case SP_ATK_RATE:
  2434. if(sd->state.lr_flag != 2)
  2435. sd->bonus.atk_rate += val;
  2436. break;
  2437. case SP_MAGIC_ATK_DEF:
  2438. if(sd->state.lr_flag != 2)
  2439. sd->bonus.magic_def_rate += val;
  2440. break;
  2441. case SP_MISC_ATK_DEF:
  2442. if(sd->state.lr_flag != 2)
  2443. sd->bonus.misc_def_rate += val;
  2444. break;
  2445. case SP_IGNORE_MDEF_ELE:
  2446. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2447. if(sd->state.lr_flag != 2)
  2448. sd->bonus.ignore_mdef_ele |= 1<<val;
  2449. break;
  2450. case SP_IGNORE_MDEF_RACE:
  2451. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2452. if(sd->state.lr_flag != 2)
  2453. sd->bonus.ignore_mdef_race |= 1<<val;
  2454. break;
  2455. case SP_PERFECT_HIT_RATE:
  2456. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2457. sd->bonus.perfect_hit = val;
  2458. break;
  2459. case SP_PERFECT_HIT_ADD_RATE:
  2460. if(sd->state.lr_flag != 2)
  2461. sd->bonus.perfect_hit_add += val;
  2462. break;
  2463. case SP_CRITICAL_RATE:
  2464. if(sd->state.lr_flag != 2)
  2465. sd->critical_rate+=val;
  2466. break;
  2467. case SP_DEF_RATIO_ATK_ELE:
  2468. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2469. if(!sd->state.lr_flag)
  2470. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2471. else if(sd->state.lr_flag == 1)
  2472. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2473. break;
  2474. case SP_DEF_RATIO_ATK_RACE:
  2475. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2476. if(!sd->state.lr_flag)
  2477. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2478. else if(sd->state.lr_flag == 1)
  2479. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2480. break;
  2481. case SP_DEF_RATIO_ATK_CLASS:
  2482. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2483. if(!sd->state.lr_flag)
  2484. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2485. else if(sd->state.lr_flag == 1)
  2486. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2487. break;
  2488. case SP_HIT_RATE:
  2489. if(sd->state.lr_flag != 2)
  2490. sd->hit_rate += val;
  2491. break;
  2492. case SP_FLEE_RATE:
  2493. if(sd->state.lr_flag != 2)
  2494. sd->flee_rate += val;
  2495. break;
  2496. case SP_FLEE2_RATE:
  2497. if(sd->state.lr_flag != 2)
  2498. sd->flee2_rate += val;
  2499. break;
  2500. case SP_DEF_RATE:
  2501. if(sd->state.lr_flag != 2)
  2502. sd->def_rate += val;
  2503. break;
  2504. case SP_DEF2_RATE:
  2505. if(sd->state.lr_flag != 2)
  2506. sd->def2_rate += val;
  2507. break;
  2508. case SP_MDEF_RATE:
  2509. if(sd->state.lr_flag != 2)
  2510. sd->mdef_rate += val;
  2511. break;
  2512. case SP_MDEF2_RATE:
  2513. if(sd->state.lr_flag != 2)
  2514. sd->mdef2_rate += val;
  2515. break;
  2516. case SP_RESTART_FULL_RECOVER:
  2517. if(sd->state.lr_flag != 2)
  2518. sd->special_state.restart_full_recover = 1;
  2519. break;
  2520. case SP_NO_CASTCANCEL:
  2521. if(sd->state.lr_flag != 2)
  2522. sd->special_state.no_castcancel = 1;
  2523. break;
  2524. case SP_NO_CASTCANCEL2:
  2525. if(sd->state.lr_flag != 2)
  2526. sd->special_state.no_castcancel2 = 1;
  2527. break;
  2528. case SP_NO_SIZEFIX:
  2529. if(sd->state.lr_flag != 2)
  2530. sd->special_state.no_sizefix = 1;
  2531. break;
  2532. case SP_NO_MAGIC_DAMAGE:
  2533. if(sd->state.lr_flag == 2)
  2534. break;
  2535. val+= sd->special_state.no_magic_damage;
  2536. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2537. break;
  2538. case SP_NO_WEAPON_DAMAGE:
  2539. if(sd->state.lr_flag == 2)
  2540. break;
  2541. val+= sd->special_state.no_weapon_damage;
  2542. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2543. break;
  2544. case SP_NO_MISC_DAMAGE:
  2545. if(sd->state.lr_flag == 2)
  2546. break;
  2547. val+= sd->special_state.no_misc_damage;
  2548. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2549. break;
  2550. case SP_NO_GEMSTONE:
  2551. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2552. sd->special_state.no_gemstone = 1;
  2553. break;
  2554. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2555. if(sd->state.lr_flag != 2) {
  2556. sd->special_state.intravision = 1;
  2557. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2558. }
  2559. break;
  2560. case SP_NO_KNOCKBACK:
  2561. if(sd->state.lr_flag != 2)
  2562. sd->special_state.no_knockback = 1;
  2563. break;
  2564. case SP_SPLASH_RANGE:
  2565. if(sd->bonus.splash_range < val)
  2566. sd->bonus.splash_range = val;
  2567. break;
  2568. case SP_SPLASH_ADD_RANGE:
  2569. sd->bonus.splash_add_range += val;
  2570. break;
  2571. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2572. if(sd->state.lr_flag != 2)
  2573. sd->bonus.short_weapon_damage_return += val;
  2574. break;
  2575. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2576. if(sd->state.lr_flag != 2)
  2577. sd->bonus.long_weapon_damage_return += val;
  2578. break;
  2579. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2580. if(sd->state.lr_flag != 2)
  2581. sd->bonus.magic_damage_return += val;
  2582. break;
  2583. case SP_ALL_STATS: // [Valaris]
  2584. if(sd->state.lr_flag!=2) {
  2585. sd->param_bonus[SP_STR-SP_STR]+=val;
  2586. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2587. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2588. sd->param_bonus[SP_INT-SP_STR]+=val;
  2589. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2590. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2591. }
  2592. break;
  2593. case SP_AGI_VIT: // [Valaris]
  2594. if(sd->state.lr_flag!=2) {
  2595. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2596. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2597. }
  2598. break;
  2599. case SP_AGI_DEX_STR: // [Valaris]
  2600. if(sd->state.lr_flag!=2) {
  2601. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2602. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2603. sd->param_bonus[SP_STR-SP_STR]+=val;
  2604. }
  2605. break;
  2606. case SP_PERFECT_HIDE: // [Valaris]
  2607. if(sd->state.lr_flag!=2)
  2608. sd->special_state.perfect_hiding=1;
  2609. break;
  2610. case SP_UNBREAKABLE:
  2611. if(sd->state.lr_flag!=2)
  2612. sd->bonus.unbreakable += val;
  2613. break;
  2614. case SP_UNBREAKABLE_WEAPON:
  2615. if(sd->state.lr_flag != 2)
  2616. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2617. break;
  2618. case SP_UNBREAKABLE_ARMOR:
  2619. if(sd->state.lr_flag != 2)
  2620. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2621. break;
  2622. case SP_UNBREAKABLE_HELM:
  2623. if(sd->state.lr_flag != 2)
  2624. sd->bonus.unbreakable_equip |= EQP_HELM;
  2625. break;
  2626. case SP_UNBREAKABLE_SHIELD:
  2627. if(sd->state.lr_flag != 2)
  2628. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2629. break;
  2630. case SP_UNBREAKABLE_GARMENT:
  2631. if(sd->state.lr_flag != 2)
  2632. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2633. break;
  2634. case SP_UNBREAKABLE_SHOES:
  2635. if(sd->state.lr_flag != 2)
  2636. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2637. break;
  2638. case SP_CLASSCHANGE: // [Valaris]
  2639. if(sd->state.lr_flag !=2)
  2640. sd->bonus.classchange=val;
  2641. break;
  2642. case SP_LONG_ATK_RATE:
  2643. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2644. sd->bonus.long_attack_atk_rate+=val;
  2645. break;
  2646. case SP_BREAK_WEAPON_RATE:
  2647. if(sd->state.lr_flag != 2)
  2648. sd->bonus.break_weapon_rate+=val;
  2649. break;
  2650. case SP_BREAK_ARMOR_RATE:
  2651. if(sd->state.lr_flag != 2)
  2652. sd->bonus.break_armor_rate+=val;
  2653. break;
  2654. case SP_ADD_STEAL_RATE:
  2655. if(sd->state.lr_flag != 2)
  2656. sd->bonus.add_steal_rate+=val;
  2657. break;
  2658. case SP_DELAYRATE:
  2659. if(sd->state.lr_flag != 2)
  2660. sd->delayrate+=val;
  2661. break;
  2662. case SP_CRIT_ATK_RATE:
  2663. if(sd->state.lr_flag != 2)
  2664. sd->bonus.crit_atk_rate += val;
  2665. break;
  2666. case SP_NO_REGEN:
  2667. if(sd->state.lr_flag != 2)
  2668. sd->regen.state.block|=val;
  2669. break;
  2670. case SP_UNSTRIPABLE_WEAPON:
  2671. if(sd->state.lr_flag != 2)
  2672. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2673. break;
  2674. case SP_UNSTRIPABLE:
  2675. case SP_UNSTRIPABLE_ARMOR:
  2676. if(sd->state.lr_flag != 2)
  2677. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2678. break;
  2679. case SP_UNSTRIPABLE_HELM:
  2680. if(sd->state.lr_flag != 2)
  2681. sd->bonus.unstripable_equip |= EQP_HELM;
  2682. break;
  2683. case SP_UNSTRIPABLE_SHIELD:
  2684. if(sd->state.lr_flag != 2)
  2685. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2686. break;
  2687. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2688. if(!sd->state.lr_flag) {
  2689. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2690. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2691. } else if(sd->state.lr_flag == 1) {
  2692. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2693. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2694. }
  2695. break;
  2696. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2697. if(!sd->state.lr_flag) {
  2698. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2699. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2700. } else if(sd->state.lr_flag == 1) {
  2701. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2702. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2703. }
  2704. break;
  2705. case SP_SP_GAIN_VALUE:
  2706. if(!sd->state.lr_flag)
  2707. sd->bonus.sp_gain_value += val;
  2708. break;
  2709. case SP_HP_GAIN_VALUE:
  2710. if(!sd->state.lr_flag)
  2711. sd->bonus.hp_gain_value += val;
  2712. break;
  2713. case SP_MAGIC_SP_GAIN_VALUE:
  2714. if(!sd->state.lr_flag)
  2715. sd->bonus.magic_sp_gain_value += val;
  2716. break;
  2717. case SP_MAGIC_HP_GAIN_VALUE:
  2718. if(!sd->state.lr_flag)
  2719. sd->bonus.magic_hp_gain_value += val;
  2720. break;
  2721. case SP_ADD_HEAL_RATE:
  2722. if(sd->state.lr_flag != 2)
  2723. sd->bonus.add_heal_rate += val;
  2724. break;
  2725. case SP_ADD_HEAL2_RATE:
  2726. if(sd->state.lr_flag != 2)
  2727. sd->bonus.add_heal2_rate += val;
  2728. break;
  2729. case SP_ADD_ITEM_HEAL_RATE:
  2730. if(sd->state.lr_flag != 2)
  2731. sd->bonus.itemhealrate2 += val;
  2732. break;
  2733. case SP_EMATK:
  2734. if(sd->state.lr_flag != 2)
  2735. sd->bonus.ematk += val;
  2736. break;
  2737. #ifdef RENEWAL_CAST
  2738. case SP_FIXCASTRATE:
  2739. if(sd->state.lr_flag != 2)
  2740. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2741. break;
  2742. case SP_ADD_FIXEDCAST:
  2743. if(sd->state.lr_flag != 2)
  2744. sd->bonus.add_fixcast += val;
  2745. break;
  2746. case SP_CASTRATE:
  2747. case SP_VARCASTRATE:
  2748. if(sd->state.lr_flag != 2)
  2749. sd->bonus.varcastrate -= val;
  2750. break;
  2751. case SP_ADD_VARIABLECAST:
  2752. if(sd->state.lr_flag != 2)
  2753. sd->bonus.add_varcast += val;
  2754. break;
  2755. #else
  2756. case SP_ADD_FIXEDCAST:
  2757. case SP_FIXCASTRATE:
  2758. case SP_ADD_VARIABLECAST:
  2759. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2760. break;
  2761. case SP_VARCASTRATE:
  2762. case SP_CASTRATE:
  2763. if(sd->state.lr_flag != 2)
  2764. sd->castrate += val;
  2765. break;
  2766. #endif
  2767. case SP_ADDMAXWEIGHT:
  2768. if (sd->state.lr_flag != 2)
  2769. sd->add_max_weight += val;
  2770. break;
  2771. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  2772. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  2773. break;
  2774. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  2775. if (sd->state.lr_flag != 2)
  2776. sd->bonus.critical_rangeatk += val*10;
  2777. else
  2778. sd->bonus.arrow_cri += val*10;
  2779. break;
  2780. case SP_WEAPON_ATK_RATE:
  2781. if (sd->state.lr_flag != 2)
  2782. sd->bonus.weapon_atk_rate += val;
  2783. break;
  2784. case SP_WEAPON_MATK_RATE:
  2785. if (sd->state.lr_flag != 2)
  2786. sd->bonus.weapon_matk_rate += val;
  2787. break;
  2788. case SP_NO_MADO_FUEL:
  2789. if (sd->state.lr_flag != 2)
  2790. sd->special_state.no_mado_fuel = 1;
  2791. break;
  2792. default:
  2793. if (running_npc_stat_calc_event) {
  2794. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  2795. }
  2796. else if (current_equip_combo_pos > 0) {
  2797. 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);
  2798. }
  2799. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  2800. 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);
  2801. }
  2802. else {
  2803. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  2804. }
  2805. break;
  2806. }
  2807. }
  2808. /*==========================================
  2809. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2810. * format: bonus2 bBonusName,type2,val;
  2811. * @param sd
  2812. * @param type Bonus type used by bBonusName
  2813. * @param type2
  2814. * @param val Value that usually for rate or fixed value
  2815. *------------------------------------------*/
  2816. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2817. {
  2818. int i;
  2819. nullpo_retv(sd);
  2820. switch(type){
  2821. case SP_ADDELE: // bonus2 bAddEle,e,x;
  2822. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  2823. if(!sd->state.lr_flag)
  2824. sd->right_weapon.addele[type2]+=val;
  2825. else if(sd->state.lr_flag == 1)
  2826. sd->left_weapon.addele[type2]+=val;
  2827. else if(sd->state.lr_flag == 2)
  2828. sd->arrow_addele[type2]+=val;
  2829. break;
  2830. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  2831. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  2832. if(!sd->state.lr_flag)
  2833. sd->right_weapon.addrace[type2]+=val;
  2834. else if(sd->state.lr_flag == 1)
  2835. sd->left_weapon.addrace[type2]+=val;
  2836. else if(sd->state.lr_flag == 2)
  2837. sd->arrow_addrace[type2]+=val;
  2838. break;
  2839. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  2840. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  2841. if(!sd->state.lr_flag)
  2842. sd->right_weapon.addclass[type2]+=val;
  2843. else if(sd->state.lr_flag == 1)
  2844. sd->left_weapon.addclass[type2]+=val;
  2845. else if(sd->state.lr_flag == 2)
  2846. sd->arrow_addclass[type2]+=val;
  2847. break;
  2848. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  2849. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  2850. if(!sd->state.lr_flag)
  2851. sd->right_weapon.addsize[type2]+=val;
  2852. else if(sd->state.lr_flag == 1)
  2853. sd->left_weapon.addsize[type2]+=val;
  2854. else if(sd->state.lr_flag == 2)
  2855. sd->arrow_addsize[type2]+=val;
  2856. break;
  2857. case SP_SUBELE: // bonus2 bSubEle,e,x;
  2858. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  2859. if(sd->state.lr_flag != 2)
  2860. sd->subele_script[type2] += val;
  2861. break;
  2862. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  2863. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  2864. if(sd->state.lr_flag != 2)
  2865. sd->subrace[type2]+=val;
  2866. break;
  2867. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  2868. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  2869. if(sd->state.lr_flag != 2)
  2870. sd->subclass[type2]+=val;
  2871. break;
  2872. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  2873. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  2874. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2875. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  2876. break;
  2877. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  2878. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  2879. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2880. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  2881. break;
  2882. case SP_RESEFF: // bonus2 bResEff,eff,n;
  2883. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2884. ShowError("pc_bonus2: SP_RESEFF: %d is invalid effect.\n", type2);
  2885. break;
  2886. }
  2887. if(sd->state.lr_flag == 2)
  2888. break;
  2889. i = sd->reseff[type2]+val;
  2890. sd->reseff[type2]= cap_value(i, -10000, 10000);
  2891. break;
  2892. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  2893. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  2894. if(sd->state.lr_flag != 2)
  2895. sd->magic_addele_script[type2] += val;
  2896. break;
  2897. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  2898. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  2899. if(sd->state.lr_flag != 2)
  2900. sd->magic_addrace[type2]+=val;
  2901. break;
  2902. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  2903. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  2904. if(sd->state.lr_flag != 2)
  2905. sd->magic_addclass[type2]+=val;
  2906. break;
  2907. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  2908. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  2909. if(sd->state.lr_flag != 2)
  2910. sd->magic_addsize[type2]+=val;
  2911. break;
  2912. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  2913. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  2914. if(sd->state.lr_flag != 2)
  2915. sd->magic_atk_ele[type2]+=val;
  2916. break;
  2917. case SP_ADD_DAMAGE_CLASS: // bonus2 bAddDamageClass,mid,x;
  2918. switch (sd->state.lr_flag) {
  2919. case 0: //Right hand
  2920. 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);
  2921. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2922. {
  2923. 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));
  2924. break;
  2925. }
  2926. sd->right_weapon.add_dmg[i].class_ = type2;
  2927. sd->right_weapon.add_dmg[i].rate += val;
  2928. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2929. 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]));
  2930. break;
  2931. case 1: //Left hand
  2932. 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);
  2933. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2934. {
  2935. 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));
  2936. break;
  2937. }
  2938. sd->left_weapon.add_dmg[i].class_ = type2;
  2939. sd->left_weapon.add_dmg[i].rate += val;
  2940. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2941. 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]));
  2942. break;
  2943. }
  2944. break;
  2945. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  2946. if(sd->state.lr_flag == 2)
  2947. break;
  2948. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2949. if (i == ARRAYLENGTH(sd->add_mdmg))
  2950. {
  2951. 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));
  2952. break;
  2953. }
  2954. sd->add_mdmg[i].class_ = type2;
  2955. sd->add_mdmg[i].rate += val;
  2956. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2957. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2958. break;
  2959. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  2960. if(sd->state.lr_flag == 2)
  2961. break;
  2962. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2963. if (i == ARRAYLENGTH(sd->add_def))
  2964. {
  2965. ShowError("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2966. break;
  2967. }
  2968. sd->add_def[i].class_ = type2;
  2969. sd->add_def[i].rate += val;
  2970. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2971. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2972. break;
  2973. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  2974. if(sd->state.lr_flag == 2)
  2975. break;
  2976. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2977. if (i == ARRAYLENGTH(sd->add_mdef))
  2978. {
  2979. ShowError("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2980. break;
  2981. }
  2982. sd->add_mdef[i].class_ = type2;
  2983. sd->add_mdef[i].rate += val;
  2984. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  2985. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  2986. break;
  2987. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  2988. if(!sd->state.lr_flag) {
  2989. sd->right_weapon.hp_drain_rate.rate += type2;
  2990. sd->right_weapon.hp_drain_rate.per += val;
  2991. }
  2992. else if(sd->state.lr_flag == 1) {
  2993. sd->left_weapon.hp_drain_rate.rate += type2;
  2994. sd->left_weapon.hp_drain_rate.per += val;
  2995. }
  2996. break;
  2997. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  2998. if(!sd->state.lr_flag) {
  2999. sd->right_weapon.sp_drain_rate.rate += type2;
  3000. sd->right_weapon.sp_drain_rate.per += val;
  3001. }
  3002. else if(sd->state.lr_flag == 1) {
  3003. sd->left_weapon.sp_drain_rate.rate += type2;
  3004. sd->left_weapon.sp_drain_rate.per += val;
  3005. }
  3006. break;
  3007. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3008. if(sd->state.lr_flag != 2) {
  3009. sd->bonus.sp_vanish_rate += type2;
  3010. sd->bonus.sp_vanish_per += val;
  3011. }
  3012. break;
  3013. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3014. if(sd->state.lr_flag != 2) {
  3015. sd->bonus.hp_vanish_rate += type2;
  3016. sd->bonus.hp_vanish_per += val;
  3017. }
  3018. break;
  3019. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3020. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3021. sd->bonus.get_zeny_rate = val;
  3022. sd->bonus.get_zeny_num = type2;
  3023. }
  3024. break;
  3025. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3026. if(sd->state.lr_flag != 2) {
  3027. sd->bonus.get_zeny_rate += val;
  3028. sd->bonus.get_zeny_num += type2;
  3029. }
  3030. break;
  3031. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3032. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3033. if(sd->state.lr_flag == 2)
  3034. break;
  3035. sd->weapon_coma_ele[type2] += val;
  3036. sd->special_state.bonus_coma = 1;
  3037. break;
  3038. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3039. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3040. if(sd->state.lr_flag == 2)
  3041. break;
  3042. sd->weapon_coma_race[type2] += val;
  3043. sd->special_state.bonus_coma = 1;
  3044. break;
  3045. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3046. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3047. if(sd->state.lr_flag == 2)
  3048. break;
  3049. sd->weapon_coma_class[type2] += val;
  3050. sd->special_state.bonus_coma = 1;
  3051. break;
  3052. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3053. if(sd->state.lr_flag != 2)
  3054. sd->weapon_atk[type2]+=val;
  3055. break;
  3056. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3057. if(sd->state.lr_flag != 2)
  3058. sd->weapon_damage_rate[type2]+=val;
  3059. break;
  3060. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3061. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3062. if(sd->state.lr_flag != 2)
  3063. sd->critaddrace[type2] += val*10;
  3064. break;
  3065. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3066. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3067. if(sd->state.lr_flag != 2)
  3068. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, val, 0, 0, 0);
  3069. break;
  3070. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3071. if(sd->state.lr_flag == 2)
  3072. break;
  3073. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  3074. if (i == ARRAYLENGTH(sd->skillatk))
  3075. { //Better mention this so the array length can be updated. [Skotlex]
  3076. 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);
  3077. break;
  3078. }
  3079. if (sd->skillatk[i].id == type2)
  3080. sd->skillatk[i].val += val;
  3081. else {
  3082. sd->skillatk[i].id = type2;
  3083. sd->skillatk[i].val = val;
  3084. }
  3085. break;
  3086. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3087. if(sd->state.lr_flag == 2)
  3088. break;
  3089. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  3090. if (i == ARRAYLENGTH(sd->skillheal))
  3091. { // Better mention this so the array length can be updated. [Skotlex]
  3092. 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);
  3093. break;
  3094. }
  3095. if (sd->skillheal[i].id == type2)
  3096. sd->skillheal[i].val += val;
  3097. else {
  3098. sd->skillheal[i].id = type2;
  3099. sd->skillheal[i].val = val;
  3100. }
  3101. break;
  3102. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3103. if(sd->state.lr_flag == 2)
  3104. break;
  3105. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  3106. if (i == ARRAYLENGTH(sd->skillheal2))
  3107. { // Better mention this so the array length can be updated. [Skotlex]
  3108. 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);
  3109. break;
  3110. }
  3111. if (sd->skillheal2[i].id == type2)
  3112. sd->skillheal2[i].val += val;
  3113. else {
  3114. sd->skillheal2[i].id = type2;
  3115. sd->skillheal2[i].val = val;
  3116. }
  3117. break;
  3118. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3119. if(sd->state.lr_flag == 2)
  3120. break;
  3121. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  3122. if (i == ARRAYLENGTH(sd->skillblown))
  3123. { //Better mention this so the array length can be updated. [Skotlex]
  3124. 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);
  3125. break;
  3126. }
  3127. if(sd->skillblown[i].id == type2)
  3128. sd->skillblown[i].val += val;
  3129. else {
  3130. sd->skillblown[i].id = type2;
  3131. sd->skillblown[i].val = val;
  3132. }
  3133. break;
  3134. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3135. if(sd->state.lr_flag != 2) {
  3136. sd->hp_loss.value = type2;
  3137. sd->hp_loss.rate = val;
  3138. }
  3139. break;
  3140. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3141. if(sd->state.lr_flag != 2) {
  3142. sd->hp_regen.value = type2;
  3143. sd->hp_regen.rate = val;
  3144. }
  3145. break;
  3146. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3147. if (sd->state.lr_flag != 2) {
  3148. sd->percent_hp_regen.value = type2;
  3149. sd->percent_hp_regen.rate = val;
  3150. }
  3151. break;
  3152. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3153. if (sd->state.lr_flag != 2) {
  3154. sd->percent_sp_regen.value = type2;
  3155. sd->percent_sp_regen.rate = val;
  3156. }
  3157. break;
  3158. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3159. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3160. if(sd->state.lr_flag != 2)
  3161. sd->right_weapon.addrace2[type2] += val;
  3162. else
  3163. sd->left_weapon.addrace2[type2] += val;
  3164. break;
  3165. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3166. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3167. if(sd->state.lr_flag != 2)
  3168. sd->subsize[type2]+=val;
  3169. break;
  3170. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3171. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3172. if(sd->state.lr_flag != 2)
  3173. sd->subrace2[type2]+=val;
  3174. break;
  3175. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3176. if(sd->state.lr_flag == 2)
  3177. break;
  3178. if (!itemdb_exists(type2)) {
  3179. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3180. break;
  3181. }
  3182. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  3183. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  3184. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  3185. break;
  3186. }
  3187. sd->itemhealrate[i].nameid = type2;
  3188. sd->itemhealrate[i].rate += val;
  3189. break;
  3190. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3191. if (sd->state.lr_flag == 2)
  3192. break;
  3193. if (!type2 || !itemdb_group_exists(type2)) {
  3194. ShowError("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3195. break;
  3196. }
  3197. pc_itemgrouphealrate(sd, type2, val);
  3198. break;
  3199. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3200. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3201. if(sd->state.lr_flag != 2)
  3202. sd->expaddrace[type2]+=val;
  3203. break;
  3204. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3205. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3206. if(sd->state.lr_flag != 2)
  3207. sd->expaddclass[type2]+=val;
  3208. break;
  3209. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3210. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3211. if(sd->state.lr_flag != 2)
  3212. sd->sp_gain_race[type2]+=val;
  3213. break;
  3214. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3215. if (sd->state.lr_flag != 2)
  3216. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_ALL, RC_NONE_, val);
  3217. break;
  3218. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3219. if (sd->state.lr_flag != 2)
  3220. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_ALL, RC_NONE_, val);
  3221. break;
  3222. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3223. if(sd->state.lr_flag != 2) {
  3224. sd->sp_loss.value = type2;
  3225. sd->sp_loss.rate = val;
  3226. }
  3227. break;
  3228. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3229. if(sd->state.lr_flag != 2) {
  3230. sd->sp_regen.value = type2;
  3231. sd->sp_regen.rate = val;
  3232. }
  3233. break;
  3234. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3235. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3236. if(!sd->state.lr_flag) {
  3237. sd->right_weapon.hp_drain_race[type2] += val;
  3238. }
  3239. else if(sd->state.lr_flag == 1) {
  3240. sd->left_weapon.hp_drain_race[type2] += val;
  3241. }
  3242. break;
  3243. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3244. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3245. if(!sd->state.lr_flag) {
  3246. sd->right_weapon.sp_drain_race[type2] += val;
  3247. }
  3248. else if(sd->state.lr_flag == 1) {
  3249. sd->left_weapon.sp_drain_race[type2] += val;
  3250. }
  3251. break;
  3252. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3253. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3254. if(!sd->state.lr_flag) {
  3255. sd->right_weapon.hp_drain_class[type2] += val;
  3256. }
  3257. else if(sd->state.lr_flag == 1) {
  3258. sd->left_weapon.hp_drain_class[type2] += val;
  3259. }
  3260. break;
  3261. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3262. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3263. if(!sd->state.lr_flag) {
  3264. sd->right_weapon.sp_drain_class[type2] += val;
  3265. }
  3266. else if(sd->state.lr_flag == 1) {
  3267. sd->left_weapon.sp_drain_class[type2] += val;
  3268. }
  3269. break;
  3270. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3271. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3272. if(sd->state.lr_flag != 2)
  3273. sd->ignore_mdef_by_race[type2] += val;
  3274. break;
  3275. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3276. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3277. if(sd->state.lr_flag != 2)
  3278. sd->ignore_mdef_by_class[type2] += val;
  3279. break;
  3280. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3281. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3282. if(sd->state.lr_flag != 2)
  3283. sd->ignore_def_by_race[type2] += val;
  3284. break;
  3285. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3286. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3287. if (sd->state.lr_flag != 2)
  3288. sd->ignore_def_by_class[type2] += val;
  3289. break;
  3290. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3291. if(sd->state.lr_flag == 2)
  3292. break;
  3293. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  3294. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  3295. 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);
  3296. break;
  3297. }
  3298. if (sd->skillusesprate[i].id == type2)
  3299. sd->skillusesprate[i].val += val;
  3300. else {
  3301. sd->skillusesprate[i].id = type2;
  3302. sd->skillusesprate[i].val = val;
  3303. }
  3304. break;
  3305. case SP_SKILL_DELAY:
  3306. if(sd->state.lr_flag == 2)
  3307. break;
  3308. ARR_FIND(0, ARRAYLENGTH(sd->skilldelay), i, sd->skilldelay[i].id == 0 || sd->skilldelay[i].id == type2);
  3309. if (i == ARRAYLENGTH(sd->skilldelay)) {
  3310. 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);
  3311. break;
  3312. }
  3313. if (sd->skilldelay[i].id == type2)
  3314. sd->skilldelay[i].val += val;
  3315. else {
  3316. sd->skilldelay[i].id = type2;
  3317. sd->skilldelay[i].val = val;
  3318. }
  3319. break;
  3320. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3321. if(sd->state.lr_flag == 2)
  3322. break;
  3323. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  3324. if (i == ARRAYLENGTH(sd->skillcooldown))
  3325. {
  3326. 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);
  3327. break;
  3328. }
  3329. if (sd->skillcooldown[i].id == type2)
  3330. sd->skillcooldown[i].val += val;
  3331. else {
  3332. sd->skillcooldown[i].id = type2;
  3333. sd->skillcooldown[i].val = val;
  3334. }
  3335. break;
  3336. #ifdef RENEWAL_CAST
  3337. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3338. if(sd->state.lr_flag == 2)
  3339. break;
  3340. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  3341. if (i == ARRAYLENGTH(sd->skillfixcast))
  3342. {
  3343. 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);
  3344. break;
  3345. }
  3346. if (sd->skillfixcast[i].id == type2)
  3347. sd->skillfixcast[i].val += val;
  3348. else {
  3349. sd->skillfixcast[i].id = type2;
  3350. sd->skillfixcast[i].val = val;
  3351. }
  3352. break;
  3353. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3354. if(sd->state.lr_flag == 2)
  3355. break;
  3356. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  3357. if (i == ARRAYLENGTH(sd->skillvarcast))
  3358. {
  3359. 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);
  3360. break;
  3361. }
  3362. if (sd->skillvarcast[i].id == type2)
  3363. sd->skillvarcast[i].val += val;
  3364. else {
  3365. sd->skillvarcast[i].id = type2;
  3366. sd->skillvarcast[i].val = val;
  3367. }
  3368. break;
  3369. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3370. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3371. if(sd->state.lr_flag == 2)
  3372. break;
  3373. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3374. if (i == ARRAYLENGTH(sd->skillcastrate))
  3375. {
  3376. ShowError("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",ARRAYLENGTH(sd->skillcastrate), type2, val);
  3377. break;
  3378. }
  3379. if(sd->skillcastrate[i].id == type2)
  3380. sd->skillcastrate[i].val -= val;
  3381. else {
  3382. sd->skillcastrate[i].id = type2;
  3383. sd->skillcastrate[i].val -= val;
  3384. }
  3385. break;
  3386. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3387. if(sd->state.lr_flag == 2)
  3388. break;
  3389. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  3390. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  3391. {
  3392. ShowError("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  3393. break;
  3394. }
  3395. if(sd->skillfixcastrate[i].id == type2)
  3396. sd->skillfixcastrate[i].val -= val;
  3397. else {
  3398. sd->skillfixcastrate[i].id = type2;
  3399. sd->skillfixcastrate[i].val -= val;
  3400. }
  3401. break;
  3402. #else
  3403. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3404. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3405. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3406. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3407. break;
  3408. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3409. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3410. if(sd->state.lr_flag == 2)
  3411. break;
  3412. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3413. if (i == ARRAYLENGTH(sd->skillcastrate))
  3414. { //Better mention this so the array length can be updated. [Skotlex]
  3415. ShowError("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",
  3416. (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", ARRAYLENGTH(sd->skillcastrate), type2, val);
  3417. break;
  3418. }
  3419. if(sd->skillcastrate[i].id == type2)
  3420. sd->skillcastrate[i].val += val;
  3421. else {
  3422. sd->skillcastrate[i].id = type2;
  3423. sd->skillcastrate[i].val = val;
  3424. }
  3425. break;
  3426. #endif
  3427. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3428. if(sd->state.lr_flag == 2)
  3429. break;
  3430. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  3431. if (i == ARRAYLENGTH(sd->skillusesp)) {
  3432. 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);
  3433. break;
  3434. }
  3435. if (sd->skillusesp[i].id == type2)
  3436. sd->skillusesp[i].val += val;
  3437. else {
  3438. sd->skillusesp[i].id = type2;
  3439. sd->skillusesp[i].val = val;
  3440. }
  3441. break;
  3442. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3443. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == type2 || sd->subskill[i].id == 0);
  3444. if (i == ARRAYLENGTH(sd->subskill)) {
  3445. 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);
  3446. break;
  3447. }
  3448. if (sd->subskill[i].id == type2)
  3449. sd->subskill[i].val += val;
  3450. else {
  3451. sd->subskill[i].id = type2;
  3452. sd->subskill[i].val = val;
  3453. }
  3454. break;
  3455. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3456. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3457. sd->subdefele[type2] += val;
  3458. break;
  3459. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3460. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3461. sd->coma_class[type2] += val;
  3462. sd->special_state.bonus_coma = 1;
  3463. break;
  3464. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3465. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3466. sd->coma_race[type2] += val;
  3467. sd->special_state.bonus_coma = 1;
  3468. break;
  3469. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3470. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3471. if(sd->state.lr_flag != 2)
  3472. sd->magic_addrace2[type2] += val;
  3473. break;
  3474. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3475. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3476. if (sd->state.lr_flag != 2)
  3477. sd->ignore_mdef_by_race2[type2] += val;
  3478. break;
  3479. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3480. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3481. if (sd->state.lr_flag != 2)
  3482. sd->dropaddrace[type2] += val;
  3483. break;
  3484. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3485. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3486. if (sd->state.lr_flag != 2)
  3487. sd->dropaddclass[type2] += val;
  3488. break;
  3489. default:
  3490. if (running_npc_stat_calc_event) {
  3491. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3492. }
  3493. else if (current_equip_combo_pos > 0) {
  3494. 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);
  3495. }
  3496. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3497. 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);
  3498. }
  3499. else {
  3500. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3501. }
  3502. break;
  3503. }
  3504. }
  3505. /**
  3506. * Gives item bonus to player for format: bonus3 bBonusName,type2,val;
  3507. * @param sd
  3508. * @param type Bonus type used by bBonusName
  3509. * @param type2
  3510. * @param val Value that usually for rate or fixed value
  3511. */
  3512. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3513. {
  3514. nullpo_retv(sd);
  3515. switch(type){
  3516. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3517. if(sd->state.lr_flag != 2)
  3518. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, type3, val);
  3519. break;
  3520. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3521. if(sd->state.lr_flag != 2)
  3522. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, -type3, val);
  3523. break;
  3524. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3525. if(sd->state.lr_flag != 2)
  3526. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, type3, RC_NONE_, val);
  3527. break;
  3528. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3529. if(sd->state.lr_flag != 2)
  3530. {
  3531. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3532. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3533. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  3534. target?-type2:type2, type3, val, 0, current_equip_card_id);
  3535. }
  3536. break;
  3537. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3538. if(sd->state.lr_flag != 2)
  3539. {
  3540. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3541. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3542. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  3543. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3544. }
  3545. break;
  3546. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3547. if (sd->state.lr_flag != 2)
  3548. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_NONE, type3, val);
  3549. break;
  3550. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3551. if (sd->state.lr_flag != 2)
  3552. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, type3, RC_NONE_, val);
  3553. break;
  3554. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3555. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3556. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3557. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3558. break;
  3559. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3560. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3561. if(sd->state.lr_flag != 2)
  3562. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, val, 0);
  3563. break;
  3564. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3565. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3566. if( sd->state.lr_flag != 2 )
  3567. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, val, type2, ATF_TARGET, 0);
  3568. break;
  3569. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3570. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3571. if (sd->state.lr_flag != 2)
  3572. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3573. break;
  3574. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3575. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3576. if (sd->state.lr_flag != 2)
  3577. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3578. break;
  3579. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3580. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3581. if(sd->state.lr_flag != 2) {
  3582. sd->sp_vanish_race[type2].rate += type3;
  3583. sd->sp_vanish_race[type2].per += val;
  3584. }
  3585. break;
  3586. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3587. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3588. if(sd->state.lr_flag != 2) {
  3589. sd->hp_vanish_race[type2].rate += type3;
  3590. sd->hp_vanish_race[type2].per += val;
  3591. }
  3592. break;
  3593. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3594. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3595. if (sd->state.lr_flag == 2)
  3596. break;
  3597. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3598. //if (type3 > sd->norecover_state_race[type2].rate) {
  3599. // sd->norecover_state_race[type2].rate = type3;
  3600. // sd->norecover_state_race[type2].tick = val;
  3601. // break;
  3602. //}
  3603. sd->norecover_state_race[type2].rate = type3;
  3604. sd->norecover_state_race[type2].tick = val;
  3605. break;
  3606. default:
  3607. if (running_npc_stat_calc_event) {
  3608. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3609. }
  3610. else if (current_equip_combo_pos > 0) {
  3611. 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);
  3612. }
  3613. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3614. 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);
  3615. }
  3616. else {
  3617. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3618. }
  3619. break;
  3620. }
  3621. }
  3622. /**
  3623. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,val;
  3624. * @param sd
  3625. * @param type Bonus type used by bBonusName
  3626. * @param type2
  3627. * @param type3
  3628. * @param val Value that usually for rate or fixed value
  3629. */
  3630. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3631. {
  3632. nullpo_retv(sd);
  3633. switch(type){
  3634. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3635. if(sd->state.lr_flag != 2)
  3636. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3637. break;
  3638. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3639. if(sd->state.lr_flag != 2)
  3640. 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);
  3641. break;
  3642. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3643. if(sd->state.lr_flag != 2)
  3644. {
  3645. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3646. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type3)&INF2_NO_TARGET_SELF));
  3647. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3648. }
  3649. break;
  3650. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3651. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3652. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3653. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3654. break;
  3655. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3656. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3657. if (sd->state.lr_flag != 2)
  3658. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, type4, val);
  3659. break;
  3660. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3661. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3662. if( sd->state.lr_flag != 2 )
  3663. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, val, 0);
  3664. break;
  3665. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3666. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3667. if(sd->state.lr_flag == 2)
  3668. break;
  3669. sd->def_set_race[type2].rate = type3;
  3670. sd->def_set_race[type2].tick = type4;
  3671. sd->def_set_race[type2].value = val;
  3672. break;
  3673. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3674. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3675. if(sd->state.lr_flag == 2)
  3676. break;
  3677. sd->mdef_set_race[type2].rate = type3;
  3678. sd->mdef_set_race[type2].tick = type4;
  3679. sd->mdef_set_race[type2].value = val;
  3680. break;
  3681. default:
  3682. if (running_npc_stat_calc_event) {
  3683. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3684. }
  3685. else if (current_equip_combo_pos > 0) {
  3686. 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);
  3687. }
  3688. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3689. 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);
  3690. }
  3691. else {
  3692. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3693. }
  3694. break;
  3695. }
  3696. }
  3697. /**
  3698. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3699. * @param sd
  3700. * @param type Bonus type used by bBonusName
  3701. * @param type2
  3702. * @param type3
  3703. * @param type4
  3704. * @param val Value that usually for rate or fixed value
  3705. */
  3706. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3707. {
  3708. nullpo_retv(sd);
  3709. switch(type){
  3710. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3711. if(sd->state.lr_flag != 2)
  3712. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3713. break;
  3714. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3715. if(sd->state.lr_flag != 2)
  3716. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3717. break;
  3718. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3719. if(sd->state.lr_flag != 2)
  3720. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3721. break;
  3722. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3723. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3724. if( sd->state.lr_flag != 2 )
  3725. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, type5, val);
  3726. break;
  3727. default:
  3728. if (running_npc_stat_calc_event) {
  3729. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3730. }
  3731. else if (current_equip_combo_pos > 0) {
  3732. 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);
  3733. }
  3734. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3735. 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);
  3736. }
  3737. else {
  3738. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3739. }
  3740. break;
  3741. }
  3742. }
  3743. /*==========================================
  3744. * Grants a player a given skill. Flag values are:
  3745. * 0 - Grant permanent skill to be bound to skill tree
  3746. * 1 - Grant an item skill (temporary)
  3747. * 2 - Like 1, except the level granted can stack with previously learned level.
  3748. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3749. *------------------------------------------*/
  3750. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3751. uint16 idx = 0;
  3752. nullpo_ret(sd);
  3753. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3754. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3755. return false;
  3756. }
  3757. if (level > MAX_SKILL_LEVEL) {
  3758. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3759. return false;
  3760. }
  3761. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3762. 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);
  3763. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3764. }
  3765. switch (type) {
  3766. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3767. sd->status.skill[idx].id = skill_id;
  3768. sd->status.skill[idx].lv = level;
  3769. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3770. if (level == 0) { //Remove skill.
  3771. sd->status.skill[idx].id = 0;
  3772. clif_deleteskill(sd,skill_id);
  3773. } else
  3774. clif_addskill(sd,skill_id);
  3775. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3776. status_calc_pc(sd, SCO_NONE);
  3777. break;
  3778. case ADDSKILL_TEMP: //Item bonus skill.
  3779. if (sd->status.skill[idx].id != 0) {
  3780. if (sd->status.skill[idx].lv >= level)
  3781. return true;
  3782. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3783. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3784. } else {
  3785. sd->status.skill[idx].id = skill_id;
  3786. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3787. }
  3788. sd->status.skill[idx].lv = level;
  3789. break;
  3790. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3791. if (sd->status.skill[idx].id != 0) {
  3792. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3793. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3794. } else {
  3795. sd->status.skill[idx].id = skill_id;
  3796. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3797. }
  3798. sd->status.skill[idx].lv += level;
  3799. break;
  3800. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3801. sd->status.skill[idx].id = skill_id;
  3802. sd->status.skill[idx].lv = level;
  3803. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3804. if (level == 0) { //Remove skill.
  3805. sd->status.skill[idx].id = 0;
  3806. clif_deleteskill(sd,skill_id);
  3807. } else
  3808. clif_addskill(sd,skill_id);
  3809. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3810. status_calc_pc(sd, SCO_NONE);
  3811. break;
  3812. default:
  3813. return false;
  3814. }
  3815. return true;
  3816. }
  3817. /*==========================================
  3818. * Append a card to an item ?
  3819. *------------------------------------------*/
  3820. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3821. {
  3822. int i;
  3823. unsigned short nameid;
  3824. nullpo_ret(sd);
  3825. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3826. return 0; //Invalid item index.
  3827. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3828. return 0; //Invalid card index.
  3829. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  3830. return 0; // target item missing
  3831. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  3832. return 0; // target card missing
  3833. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3834. return 0; // only weapons and armor are allowed
  3835. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3836. return 0; // must be a card
  3837. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  3838. return 0; // target must be identified
  3839. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  3840. return 0; // card slots reserved for other purposes
  3841. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3842. return 0; // card cannot be compounded on this item type
  3843. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3844. return 0; // attempted to place shield card on left-hand weapon.
  3845. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  3846. return 0; // item must be unequipped
  3847. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  3848. if( i == sd->inventory_data[idx_equip]->slot )
  3849. return 0; // no free slots
  3850. // remember the card id to insert
  3851. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  3852. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3853. {// failed
  3854. clif_insert_card(sd,idx_equip,idx_card,1);
  3855. }
  3856. else
  3857. {// success
  3858. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  3859. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  3860. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  3861. clif_insert_card(sd,idx_equip,idx_card,0);
  3862. }
  3863. return 0;
  3864. }
  3865. //
  3866. // Items
  3867. //
  3868. /*==========================================
  3869. * Update buying value by skills
  3870. *------------------------------------------*/
  3871. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3872. {
  3873. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3874. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3875. rate1 = 5+skill*2-((skill==10)? 1:0);
  3876. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3877. rate2 = 5+skill*4;
  3878. if(rate1 < rate2) rate1 = rate2;
  3879. if(rate1)
  3880. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3881. if(val < 0) val = 0;
  3882. if(orig_value > 0 && val < 1) val = 1;
  3883. return val;
  3884. }
  3885. /*==========================================
  3886. * Update selling value by skills
  3887. *------------------------------------------*/
  3888. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3889. {
  3890. int skill,val = orig_value,rate = 0;
  3891. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3892. rate = 5+skill*2-((skill==10)? 1:0);
  3893. if(rate)
  3894. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3895. if(val < 0) val = 0;
  3896. if(orig_value > 0 && val < 1) val = 1;
  3897. return val;
  3898. }
  3899. /*==========================================
  3900. * Checking if we have enough place on inventory for new item
  3901. * Make sure to take 30k as limit (for client I guess)
  3902. * @param sd
  3903. * @param nameid
  3904. * @param amount
  3905. * @return e_chkitem_result
  3906. *------------------------------------------*/
  3907. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  3908. {
  3909. int i;
  3910. struct item_data* data;
  3911. nullpo_ret(sd);
  3912. if(amount > MAX_AMOUNT)
  3913. return CHKADDITEM_OVERAMOUNT;
  3914. data = itemdb_search(nameid);
  3915. if(!itemdb_isstackable2(data))
  3916. return CHKADDITEM_NEW;
  3917. if( data->stack.inventory && amount > data->stack.amount )
  3918. return CHKADDITEM_OVERAMOUNT;
  3919. for(i=0;i<MAX_INVENTORY;i++){
  3920. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3921. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  3922. 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 ) )
  3923. return CHKADDITEM_OVERAMOUNT;
  3924. return CHKADDITEM_EXIST;
  3925. }
  3926. }
  3927. return CHKADDITEM_NEW;
  3928. }
  3929. /*==========================================
  3930. * Return number of available place in inventory
  3931. * Each non stackable item will reduce place by 1
  3932. * @param sd
  3933. * @return Number of empty slots
  3934. *------------------------------------------*/
  3935. uint8 pc_inventoryblank(struct map_session_data *sd)
  3936. {
  3937. uint8 i, b;
  3938. nullpo_ret(sd);
  3939. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  3940. if(sd->inventory.u.items_inventory[i].nameid == 0)
  3941. b++;
  3942. }
  3943. return b;
  3944. }
  3945. /**
  3946. * Attempts to remove zeny from player
  3947. * @param sd: Player
  3948. * @param zeny: Zeny removed
  3949. * @param type: Log type
  3950. * @param tsd: (optional) From who to log (if null take sd)
  3951. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  3952. */
  3953. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3954. {
  3955. nullpo_retr(2,sd);
  3956. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3957. if( zeny < 0 )
  3958. {
  3959. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3960. return 1;
  3961. }
  3962. if( sd->status.zeny < zeny )
  3963. return 1; //Not enough.
  3964. sd->status.zeny -= zeny;
  3965. clif_updatestatus(sd,SP_ZENY);
  3966. if(!tsd) tsd = sd;
  3967. log_zeny(sd, type, tsd, -zeny);
  3968. if( zeny > 0 && sd->state.showzeny ) {
  3969. char output[255];
  3970. sprintf(output, "Removed %dz.", zeny);
  3971. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  3972. }
  3973. return 0;
  3974. }
  3975. /**
  3976. * Attempts to give zeny to player
  3977. * @param sd: Player
  3978. * @param type: Log type
  3979. * @param tsd: (optional) From who to log (if null take sd)
  3980. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  3981. */
  3982. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3983. {
  3984. nullpo_retr(-1,sd);
  3985. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3986. if( zeny < 0 )
  3987. {
  3988. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3989. return 1;
  3990. }
  3991. if( zeny > MAX_ZENY - sd->status.zeny )
  3992. zeny = MAX_ZENY - sd->status.zeny;
  3993. sd->status.zeny += zeny;
  3994. clif_updatestatus(sd,SP_ZENY);
  3995. if(!tsd) tsd = sd;
  3996. log_zeny(sd, type, tsd, zeny);
  3997. if( zeny > 0 && sd->state.showzeny ) {
  3998. char output[255];
  3999. sprintf(output, "Gained %dz.", zeny);
  4000. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4001. }
  4002. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4003. return 0;
  4004. }
  4005. /**
  4006. * Attempts to remove Cash Points from player
  4007. * @param sd: Player
  4008. * @param price: Points player has to pay
  4009. * @param points: Points player has
  4010. * @param type: Log type
  4011. * @return -2: Paying negative points, -1: Not enough points, otherwise success (cash+points)
  4012. */
  4013. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4014. {
  4015. int cash;
  4016. nullpo_retr(-1,sd);
  4017. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4018. if( price < 0 || points < 0 )
  4019. {
  4020. 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);
  4021. return -2;
  4022. }
  4023. if( points > price )
  4024. {
  4025. 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);
  4026. points = price;
  4027. }
  4028. cash = price-points;
  4029. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4030. {
  4031. 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);
  4032. return -1;
  4033. }
  4034. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints-cash);
  4035. if( cash ){
  4036. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4037. }
  4038. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints-points);
  4039. if( points ){
  4040. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4041. }
  4042. if( battle_config.cashshop_show_points )
  4043. {
  4044. char output[CHAT_SIZE_MAX];
  4045. 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.
  4046. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4047. }
  4048. return cash+points;
  4049. }
  4050. /**
  4051. * Attempts to give Cash Points to player
  4052. * @param sd: Player
  4053. * @param cash: Cash player gets
  4054. * @param points: Points player has
  4055. * @param type: Log type
  4056. * @return -2: Error, -1: Giving negative cash/points, otherwise success (cash or points)
  4057. */
  4058. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4059. {
  4060. char output[CHAT_SIZE_MAX];
  4061. nullpo_retr(-1,sd);
  4062. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4063. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4064. if( cash > 0 )
  4065. {
  4066. if( cash > MAX_ZENY-sd->cashPoints )
  4067. {
  4068. 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);
  4069. cash = MAX_ZENY-sd->cashPoints;
  4070. }
  4071. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4072. if( cash ){
  4073. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4074. }
  4075. if( battle_config.cashshop_show_points )
  4076. {
  4077. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4078. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4079. }
  4080. return cash;
  4081. }
  4082. else if( cash < 0 )
  4083. {
  4084. 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);
  4085. return -1;
  4086. }
  4087. if( points > 0 )
  4088. {
  4089. if( points > MAX_ZENY-sd->kafraPoints )
  4090. {
  4091. 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);
  4092. points = MAX_ZENY-sd->kafraPoints;
  4093. }
  4094. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4095. if( points ){
  4096. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4097. }
  4098. if( battle_config.cashshop_show_points )
  4099. {
  4100. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4101. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4102. }
  4103. return points;
  4104. }
  4105. else if( points < 0 )
  4106. {
  4107. 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);
  4108. return -1;
  4109. }
  4110. return -2; //shouldn't happen but just in case
  4111. }
  4112. /**
  4113. * Searching a specified itemid in inventory and return his stored index
  4114. * @param sd Player
  4115. * @param nameid Find this Item!
  4116. * @return Stored index in inventory, or -1 if not found.
  4117. **/
  4118. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4119. short i;
  4120. nullpo_retr(-1, sd);
  4121. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4122. return ( i < MAX_INVENTORY ) ? i : -1;
  4123. }
  4124. /** Attempt to add a new item to player inventory
  4125. * @param sd
  4126. * @param item_data
  4127. * @param amount
  4128. * @param log_type
  4129. * @return
  4130. * 0 = success
  4131. * 1 = invalid itemid not found or negative amount
  4132. * 2 = overweight
  4133. * 3 = ?
  4134. * 4 = no free place found
  4135. * 5 = max amount reached
  4136. * 6 = ?
  4137. * 7 = stack limitation
  4138. */
  4139. char pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4140. struct item_data *id;
  4141. int16 i;
  4142. unsigned int w;
  4143. nullpo_retr(1, sd);
  4144. nullpo_retr(1, item);
  4145. if( item->nameid == 0 || amount <= 0 )
  4146. return ADDITEM_INVALID;
  4147. if( amount > MAX_AMOUNT )
  4148. return ADDITEM_OVERAMOUNT;
  4149. id = itemdb_search(item->nameid);
  4150. if( id->stack.inventory && amount > id->stack.amount )
  4151. {// item stack limitation
  4152. return ADDITEM_STACKLIMIT;
  4153. }
  4154. w = id->weight*amount;
  4155. if(sd->weight + w > sd->max_weight)
  4156. return ADDITEM_OVERWEIGHT;
  4157. i = MAX_INVENTORY;
  4158. if (id->flag.guid && !item->unique_id)
  4159. item->unique_id = pc_generate_unique_id(sd);
  4160. // Stackable | Non Rental
  4161. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4162. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4163. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4164. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4165. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4166. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4167. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4168. 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 ) )
  4169. return ADDITEM_OVERAMOUNT;
  4170. sd->inventory.u.items_inventory[i].amount += amount;
  4171. clif_additem(sd,i,amount,0);
  4172. break;
  4173. }
  4174. }
  4175. }
  4176. if (i >= MAX_INVENTORY) {
  4177. i = pc_search_inventory(sd,0);
  4178. if( i < 0 )
  4179. return ADDITEM_OVERITEM;
  4180. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4181. // clear equip and favorite fields first, just in case
  4182. if( item->equip )
  4183. sd->inventory.u.items_inventory[i].equip = 0;
  4184. if( item->favorite )
  4185. sd->inventory.u.items_inventory[i].favorite = 0;
  4186. sd->inventory.u.items_inventory[i].amount = amount;
  4187. sd->inventory_data[i] = id;
  4188. sd->last_addeditem_index = i;
  4189. if (!itemdb_isstackable2(id) || id->flag.guid)
  4190. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4191. clif_additem(sd,i,amount,0);
  4192. }
  4193. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4194. sd->weight += w;
  4195. clif_updatestatus(sd,SP_WEIGHT);
  4196. //Auto-equip
  4197. if(id->flag.autoequip)
  4198. pc_equipitem(sd, i, id->equip);
  4199. /* rental item check */
  4200. if( item->expire_time ) {
  4201. if( time(NULL) > item->expire_time ) {
  4202. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4203. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4204. } else {
  4205. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4206. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4207. pc_inventory_rental_add(sd, seconds);
  4208. }
  4209. }
  4210. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4211. return ADDITEM_SUCCESS;
  4212. }
  4213. /*==========================================
  4214. * Remove an item at index n from inventory by amount.
  4215. * @param sd
  4216. * @param n Item index in inventory
  4217. * @param amount
  4218. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4219. * @param reason Delete reason
  4220. * @param log_type e_log_pick_type
  4221. * @return 1 - invalid itemid or negative amount; 0 - Success
  4222. *------------------------------------------*/
  4223. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4224. {
  4225. nullpo_retr(1, sd);
  4226. 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)
  4227. return 1;
  4228. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4229. sd->inventory.u.items_inventory[n].amount -= amount;
  4230. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4231. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4232. if(sd->inventory.u.items_inventory[n].equip)
  4233. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4234. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4235. sd->inventory_data[n] = NULL;
  4236. }
  4237. if(!(type&1))
  4238. clif_delitem(sd,n,amount,reason);
  4239. if(!(type&2))
  4240. clif_updatestatus(sd,SP_WEIGHT);
  4241. return 0;
  4242. }
  4243. /*==========================================
  4244. * Attempt to drop an item.
  4245. * @param sd
  4246. * @param n Item index in inventory
  4247. * @param amount Amount of item
  4248. * @return False = fail; True = success
  4249. *------------------------------------------*/
  4250. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4251. {
  4252. nullpo_retr(1, sd);
  4253. if(n < 0 || n >= MAX_INVENTORY)
  4254. return false;
  4255. if(amount <= 0)
  4256. return false;
  4257. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4258. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4259. sd->inventory.u.items_inventory[n].amount < amount ||
  4260. sd->state.trading || sd->state.vending ||
  4261. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4262. )
  4263. return false;
  4264. if( map[sd->bl.m].flag.nodrop )
  4265. {
  4266. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4267. return false; //Can't drop items in nodrop mapflag maps.
  4268. }
  4269. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4270. {
  4271. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4272. return false;
  4273. }
  4274. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4275. return false;
  4276. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4277. clif_dropitem(sd, n, amount);
  4278. return true;
  4279. }
  4280. /*==========================================
  4281. * Attempt to pick up an item.
  4282. * @param sd
  4283. * @param fitem Item that will be picked
  4284. * @return False = fail; True = success
  4285. *------------------------------------------*/
  4286. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4287. {
  4288. int flag = 0;
  4289. unsigned int tick = gettick();
  4290. struct party_data *p = NULL;
  4291. nullpo_ret(sd);
  4292. nullpo_ret(fitem);
  4293. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4294. return false; // Distance is too far
  4295. if (sd->sc.cant.pickup)
  4296. return false;
  4297. if (sd->status.party_id)
  4298. p = party_search(sd->status.party_id);
  4299. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4300. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4301. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4302. if (!(p && p->party.item&1 &&
  4303. first_sd && first_sd->status.party_id == sd->status.party_id
  4304. ))
  4305. return false;
  4306. }
  4307. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4308. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4309. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4310. if (!(p && p->party.item&1 &&
  4311. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4312. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4313. ))
  4314. return false;
  4315. }
  4316. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4317. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4318. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4319. if(!(p && p->party.item&1 &&
  4320. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4321. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4322. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4323. ))
  4324. return false;
  4325. }
  4326. }
  4327. }
  4328. }
  4329. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4330. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4331. clif_additem(sd,0,0,flag);
  4332. return true;
  4333. }
  4334. //Display pickup animation.
  4335. pc_stop_attack(sd);
  4336. clif_takeitem(&sd->bl,&fitem->bl);
  4337. if (fitem->mob_id &&
  4338. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4339. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4340. )
  4341. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4342. map_clearflooritem(&fitem->bl);
  4343. return true;
  4344. }
  4345. /*==========================================
  4346. * Check if item is usable.
  4347. * Return:
  4348. * false = no
  4349. * true = yes
  4350. *------------------------------------------*/
  4351. bool pc_isUseitem(struct map_session_data *sd,int n)
  4352. {
  4353. struct item_data *item;
  4354. unsigned short nameid;
  4355. nullpo_ret(sd);
  4356. item = sd->inventory_data[n];
  4357. nameid = sd->inventory.u.items_inventory[n].nameid;
  4358. if( item == NULL )
  4359. return false;
  4360. //Not consumable item
  4361. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4362. return false;
  4363. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4364. return true;
  4365. if(map[sd->bl.m].flag.noitemconsumption) //consumable but mapflag prevent it
  4366. return false;
  4367. //Prevent mass item usage. [Skotlex]
  4368. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4369. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4370. )
  4371. return false;
  4372. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4373. clif_msg(sd,ITEM_NOUSE_SITTING);
  4374. return false; // You cannot use this item while sitting.
  4375. }
  4376. if (sd->state.storage_flag && item->type != IT_CASH) {
  4377. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4378. return false; // You cannot use this item while storage is open.
  4379. }
  4380. if (item->flag.dead_branch && (map[sd->bl.m].flag.nobranch || map_flag_gvg2(sd->bl.m)))
  4381. return false;
  4382. switch( nameid ) {
  4383. case ITEMID_WING_OF_FLY:
  4384. case ITEMID_GIANT_FLY_WING:
  4385. case ITEMID_N_FLY_WING:
  4386. if( map[sd->bl.m].flag.noteleport || map_flag_gvg2(sd->bl.m) ) {
  4387. clif_skill_teleportmessage(sd,0);
  4388. return false;
  4389. }
  4390. if (nameid == ITEMID_GIANT_FLY_WING) {
  4391. struct party_data *pd = party_search(sd->status.party_id);
  4392. if (pd) {
  4393. int i;
  4394. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4395. if (i == MAX_PARTY) { // User is not party leader
  4396. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4397. break;
  4398. }
  4399. 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));
  4400. if (i == MAX_PARTY) { // No party members found on same map
  4401. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4402. break;
  4403. }
  4404. } else {
  4405. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4406. break;
  4407. }
  4408. }
  4409. // Fall through
  4410. case ITEMID_WING_OF_BUTTERFLY:
  4411. case ITEMID_N_BUTTERFLY_WING:
  4412. case ITEMID_DUN_TELE_SCROLL1:
  4413. case ITEMID_DUN_TELE_SCROLL2:
  4414. case ITEMID_DUN_TELE_SCROLL3:
  4415. case ITEMID_WOB_RUNE:
  4416. case ITEMID_WOB_SCHWALTZ:
  4417. case ITEMID_WOB_RACHEL:
  4418. case ITEMID_WOB_LOCAL:
  4419. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4420. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4421. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4422. return false;
  4423. }
  4424. if( map[sd->bl.m].flag.noreturn && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4425. return false;
  4426. break;
  4427. case ITEMID_MERCENARY_RED_POTION:
  4428. case ITEMID_MERCENARY_BLUE_POTION:
  4429. case ITEMID_M_CENTER_POTION:
  4430. case ITEMID_M_AWAKENING_POTION:
  4431. case ITEMID_M_BERSERK_POTION:
  4432. if( sd->md == NULL || sd->md->db == NULL )
  4433. return false;
  4434. if( sd->md->sc.data[SC_BERSERK] )
  4435. return false;
  4436. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4437. return false;
  4438. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4439. return false;
  4440. break;
  4441. case ITEMID_NEURALIZER:
  4442. if( !map[sd->bl.m].flag.reset )
  4443. return false;
  4444. break;
  4445. }
  4446. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4447. return false; // Mercenary Scrolls
  4448. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4449. if( pc_is90overweight(sd) ) {
  4450. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4451. return false;
  4452. }
  4453. if( !pc_inventoryblank(sd) ) {
  4454. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4455. return false;
  4456. }
  4457. }
  4458. //Gender check
  4459. if(item->sex != 2 && sd->status.sex != item->sex)
  4460. return false;
  4461. //Required level check
  4462. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4463. return false;
  4464. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4465. return false;
  4466. //Not equipable by class. [Skotlex]
  4467. if (!(
  4468. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4469. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4470. ))
  4471. return false;
  4472. if (sd->sc.count && (
  4473. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4474. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4475. sd->sc.data[SC_TRICKDEAD] ||
  4476. sd->sc.data[SC_HIDING] ||
  4477. sd->sc.data[SC__SHADOWFORM] ||
  4478. sd->sc.data[SC__INVISIBILITY] ||
  4479. sd->sc.data[SC__MANHOLE] ||
  4480. sd->sc.data[SC_DEEPSLEEP] ||
  4481. sd->sc.data[SC_CRYSTALIZE] ||
  4482. sd->sc.data[SC_KAGEHUMI] ||
  4483. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4484. sd->sc.data[SC_KINGS_GRACE] ||
  4485. sd->sc.data[SC_SUHIDE]))
  4486. return false;
  4487. if (!pc_isItemClass(sd,item))
  4488. return false;
  4489. //Dead Branch items
  4490. if( item->flag.dead_branch )
  4491. log_branch(sd);
  4492. return true;
  4493. }
  4494. /*==========================================
  4495. * Last checks to use an item.
  4496. * Return:
  4497. * 0 = fail
  4498. * 1 = success
  4499. *------------------------------------------*/
  4500. int pc_useitem(struct map_session_data *sd,int n)
  4501. {
  4502. unsigned int tick = gettick();
  4503. int amount;
  4504. unsigned short nameid;
  4505. struct script_code *script;
  4506. struct item item;
  4507. struct item_data *id;
  4508. nullpo_ret(sd);
  4509. if (sd->npc_id) {
  4510. if (sd->progressbar.npc_id) {
  4511. clif_progressbar_abort(sd);
  4512. return 0; // First item use attempt cancels the progress bar
  4513. }
  4514. #ifdef RENEWAL
  4515. if (pc_hasprogress(sd, WIP_DISABLE_SKILLITEM)) {
  4516. clif_msg(sd, WORK_IN_PROGRESS);
  4517. return 0;
  4518. }
  4519. #else
  4520. if (!sd->npc_item_flag)
  4521. return 0;
  4522. #endif
  4523. }
  4524. item = sd->inventory.u.items_inventory[n];
  4525. id = sd->inventory_data[n];
  4526. if (item.nameid == 0 || item.amount <= 0)
  4527. return 0;
  4528. if( !pc_isUseitem(sd,n) )
  4529. return 0;
  4530. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4531. nameid = id->nameid;
  4532. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4533. return 0;
  4534. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4535. if( id->flag.delay_consume ) {
  4536. if( nameid != ITEMID_REINS_OF_MOUNT && &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  4537. return 0;
  4538. else if( pc_issit(sd) )
  4539. return 0;
  4540. }
  4541. //Since most delay-consume items involve using a "skill-type" target cursor,
  4542. //perform a skill-use check before going through. [Skotlex]
  4543. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4544. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  4545. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4546. return 0;
  4547. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4548. return 0;
  4549. /* on restricted maps the item is consumed but the effect is not used */
  4550. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4551. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4552. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4553. clif_useitemack(sd,n,item.amount-1,true);
  4554. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4555. }
  4556. return 0;/* regardless, effect is not run */
  4557. }
  4558. sd->itemid = item.nameid;
  4559. sd->itemindex = n;
  4560. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4561. sd->catch_target_class = PET_CATCH_FAIL;
  4562. amount = item.amount;
  4563. script = id->script;
  4564. //Check if the item is to be consumed immediately [Skotlex]
  4565. if (id->flag.delay_consume)
  4566. clif_useitemack(sd, n, amount, true);
  4567. else
  4568. {
  4569. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4570. {
  4571. clif_useitemack(sd, n, amount - 1, true);
  4572. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4573. }
  4574. else
  4575. clif_useitemack(sd, n, 0, false);
  4576. }
  4577. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4578. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4579. //Update item use time.
  4580. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4581. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4582. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4583. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4584. potion_flag = 0;
  4585. return 1;
  4586. }
  4587. /**
  4588. * Add item on cart for given index.
  4589. * @param sd
  4590. * @param item
  4591. * @param amount
  4592. * @param log_type
  4593. * @return 0 = success; 1 = fail; 2 = no slot
  4594. */
  4595. unsigned char pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4596. {
  4597. struct item_data *data;
  4598. int i,w;
  4599. nullpo_retr(1, sd);
  4600. nullpo_retr(1, item);
  4601. if(item->nameid == 0 || amount <= 0)
  4602. return 1;
  4603. data = itemdb_search(item->nameid);
  4604. if( data->stack.cart && amount > data->stack.amount )
  4605. {// item stack limitation
  4606. return 1;
  4607. }
  4608. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4609. { // Check item trade restrictions [Skotlex]
  4610. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4611. return 1;
  4612. }
  4613. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4614. return 1;
  4615. i = MAX_CART;
  4616. if( itemdb_isstackable2(data) && !item->expire_time )
  4617. {
  4618. for (i = 0; i < MAX_CART; i++) {
  4619. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4620. && sd->cart.u.items_cart[i].bound == item->bound
  4621. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4622. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4623. )
  4624. break;
  4625. }
  4626. }
  4627. if( i < MAX_CART )
  4628. {// item already in cart, stack it
  4629. 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 ) )
  4630. return 2; // no slot
  4631. sd->cart.u.items_cart[i].amount += amount;
  4632. clif_cart_additem(sd,i,amount,0);
  4633. }
  4634. else
  4635. {// item not stackable or not present, add it
  4636. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4637. if( i == MAX_CART )
  4638. return 2; // no slot
  4639. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4640. sd->cart.u.items_cart[i].id = 0;
  4641. sd->cart.u.items_cart[i].amount = amount;
  4642. sd->cart_num++;
  4643. clif_cart_additem(sd,i,amount,0);
  4644. }
  4645. sd->cart.u.items_cart[i].favorite = 0; // clear
  4646. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4647. sd->cart_weight += w;
  4648. clif_updatestatus(sd,SP_CARTINFO);
  4649. return 0;
  4650. }
  4651. /*==========================================
  4652. * Delete item on cart for given index.
  4653. *------------------------------------------*/
  4654. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4655. {
  4656. nullpo_retv(sd);
  4657. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4658. sd->cart.u.items_cart[n].amount < amount)
  4659. return;
  4660. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4661. sd->cart.u.items_cart[n].amount -= amount;
  4662. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4663. if(sd->cart.u.items_cart[n].amount <= 0) {
  4664. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4665. sd->cart_num--;
  4666. }
  4667. if(!type) {
  4668. clif_cart_delitem(sd,n,amount);
  4669. clif_updatestatus(sd,SP_CARTINFO);
  4670. }
  4671. }
  4672. /*==========================================
  4673. * Transfer item from inventory to cart.
  4674. *------------------------------------------*/
  4675. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4676. {
  4677. struct item *item_data;
  4678. char flag;
  4679. nullpo_retv(sd);
  4680. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4681. return;
  4682. item_data = &sd->inventory.u.items_inventory[idx];
  4683. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4684. return;
  4685. if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
  4686. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4687. else {
  4688. clif_dropitem(sd,idx,0);
  4689. clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  4690. }
  4691. }
  4692. /*==========================================
  4693. * Get number of item in cart.
  4694. * Return:
  4695. -1 = itemid not found or no amount found
  4696. x = remaining itemid on cart after get
  4697. *------------------------------------------*/
  4698. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4699. {
  4700. struct item* item_data;
  4701. nullpo_retr(-1, sd);
  4702. item_data = &sd->cart.u.items_cart[idx];
  4703. if( item_data->nameid == 0 || item_data->amount == 0 )
  4704. return -1;
  4705. return item_data->amount - amount;
  4706. }
  4707. /*==========================================
  4708. * Retrieve an item at index idx from cart.
  4709. *------------------------------------------*/
  4710. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4711. {
  4712. struct item *item_data;
  4713. unsigned char flag = 0;
  4714. nullpo_retv(sd);
  4715. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4716. return;
  4717. item_data=&sd->cart.u.items_cart[idx];
  4718. if(item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4719. return;
  4720. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4721. pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4722. else {
  4723. clif_dropitem(sd,idx,0);
  4724. clif_additem(sd,0,0,flag);
  4725. }
  4726. }
  4727. /*==========================================
  4728. * Bound Item Check
  4729. * Type:
  4730. * 1 Account Bound
  4731. * 2 Guild Bound
  4732. * 3 Party Bound
  4733. * 4 Character Bound
  4734. *------------------------------------------*/
  4735. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4736. {
  4737. int i = 0, j = 0;
  4738. for(i = 0; i < MAX_INVENTORY; i++) {
  4739. 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) {
  4740. idxlist[j] = i;
  4741. j++;
  4742. }
  4743. }
  4744. return j;
  4745. }
  4746. /*==========================================
  4747. * Display item stolen msg to player sd
  4748. *------------------------------------------*/
  4749. int pc_show_steal(struct block_list *bl,va_list ap)
  4750. {
  4751. struct map_session_data *sd;
  4752. int itemid;
  4753. struct item_data *item=NULL;
  4754. char output[100];
  4755. sd=va_arg(ap,struct map_session_data *);
  4756. itemid=va_arg(ap,int);
  4757. if((item=itemdb_exists(itemid))==NULL)
  4758. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4759. else
  4760. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4761. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4762. return 0;
  4763. }
  4764. /*==========================================
  4765. * Steal an item from bl (mob).
  4766. * Return:
  4767. * 0 = fail
  4768. * 1 = succes
  4769. *------------------------------------------*/
  4770. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4771. {
  4772. int i,itemid;
  4773. double rate;
  4774. unsigned char flag = 0;
  4775. struct status_data *sd_status, *md_status;
  4776. struct mob_data *md;
  4777. struct item tmp_item;
  4778. if(!sd || !bl || bl->type!=BL_MOB)
  4779. return 0;
  4780. md = (TBL_MOB *)bl;
  4781. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4782. return 0;
  4783. sd_status= status_get_status_data(&sd->bl);
  4784. md_status= status_get_status_data(bl);
  4785. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4786. map[bl->m].flag.nomobloot || // check noloot map flag [Lorky]
  4787. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4788. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4789. ) { //Can't steal from
  4790. md->state.steal_flag = UCHAR_MAX;
  4791. return 0;
  4792. }
  4793. // base skill success chance (percentual)
  4794. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4795. rate += sd->bonus.add_steal_rate;
  4796. if( rate < 1 )
  4797. return 0;
  4798. // Try dropping one item, in the order from first to last possible slot.
  4799. // Droprate is affected by the skill success rate.
  4800. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4801. 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 * rate/100. )
  4802. break;
  4803. if( i == MAX_STEAL_DROP )
  4804. return 0;
  4805. itemid = md->db->dropitem[i].nameid;
  4806. memset(&tmp_item,0,sizeof(tmp_item));
  4807. tmp_item.nameid = itemid;
  4808. tmp_item.amount = 1;
  4809. tmp_item.identify = itemdb_isidentified(itemid);
  4810. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  4811. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4812. //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?
  4813. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4814. if(flag) { //Failed to steal due to overweight
  4815. clif_additem(sd,0,0,flag);
  4816. return 0;
  4817. }
  4818. if(battle_config.show_steal_in_same_party)
  4819. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4820. //Logs items, Stolen from mobs [Lupus]
  4821. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4822. //A Rare Steal Global Announce by Lupus
  4823. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4824. struct item_data *i_data;
  4825. char message[128];
  4826. i_data = itemdb_search(itemid);
  4827. sprintf (message, msg_txt(sd,542), (sd->status.name != NULL)?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
  4828. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4829. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4830. }
  4831. return 1;
  4832. }
  4833. /*==========================================
  4834. * Stole zeny from bl (mob)
  4835. * return
  4836. * 0 = fail
  4837. * 1 = success
  4838. *------------------------------------------*/
  4839. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4840. {
  4841. int rate, target_lv;
  4842. struct mob_data *md;
  4843. if(!sd || !target || target->type != BL_MOB)
  4844. return 0;
  4845. md = (TBL_MOB*)target;
  4846. target_lv = status_get_lv(target);
  4847. 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)
  4848. return 0;
  4849. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  4850. if(rnd()%1000 < rate)
  4851. {
  4852. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  4853. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  4854. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4855. md->state.steal_coin_flag = 1;
  4856. return 1;
  4857. }
  4858. return 0;
  4859. }
  4860. /*==========================================
  4861. * Set's a player position.
  4862. * @param sd
  4863. * @param mapindex
  4864. * @param x
  4865. * @param y
  4866. * @param clrtype
  4867. * @return SETPOS_OK Success
  4868. * SETPOS_MAPINDEX Invalid map index
  4869. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  4870. * SETPOS_AUTOTRADE Player is in autotrade state
  4871. *------------------------------------------*/
  4872. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4873. {
  4874. int16 m;
  4875. nullpo_retr(SETPOS_OK,sd);
  4876. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4877. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4878. return SETPOS_MAPINDEX;
  4879. }
  4880. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  4881. return SETPOS_AUTOTRADE;
  4882. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  4883. pc_setstand(sd, true);
  4884. pc_setrestartvalue(sd,1);
  4885. }
  4886. m = map_mapindex2mapid(mapindex);
  4887. sd->state.changemap = (sd->mapindex != mapindex);
  4888. sd->state.warping = 1;
  4889. sd->state.workinprogress = WIP_DISABLE_NONE;
  4890. if( sd->state.changemap ) { // Misc map-changing settings
  4891. int i;
  4892. if(map[sd->bl.m].instance_id && !map[m].instance_id) {
  4893. bool instance_found = false;
  4894. struct party_data *p = NULL;
  4895. struct guild *g = NULL;
  4896. if (sd->instance_id) {
  4897. instance_delusers(sd->instance_id);
  4898. instance_found = true;
  4899. }
  4900. if (!instance_found && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id) {
  4901. instance_delusers(p->instance_id);
  4902. instance_found = true;
  4903. }
  4904. if (!instance_found && sd->status.guild_id && (g = guild_search(sd->status.guild_id)) != NULL && g->instance_id)
  4905. instance_delusers(g->instance_id);
  4906. }
  4907. sd->state.pmap = sd->bl.m;
  4908. if (sd->sc.count) { // Cancel some map related stuff.
  4909. if (sd->sc.data[SC_JAILED])
  4910. return SETPOS_MAPINDEX; //You may not get out!
  4911. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4912. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4913. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4914. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4915. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4916. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4917. if (sd->sc.data[SC_KNOWLEDGE]) {
  4918. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4919. if (sce->timer != INVALID_TIMER)
  4920. delete_timer(sce->timer, status_change_timer);
  4921. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4922. }
  4923. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4924. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4925. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4926. }
  4927. for( i = 0; i < EQI_MAX; i++ ) {
  4928. if( sd->equip_index[i] >= 0 )
  4929. if( pc_isequip(sd,sd->equip_index[i]) )
  4930. pc_unequipitem(sd,sd->equip_index[i],2);
  4931. }
  4932. if (battle_config.clear_unit_onwarp&BL_PC)
  4933. skill_clear_unitgroup(&sd->bl);
  4934. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4935. guild_send_dot_remove(sd);
  4936. bg_send_dot_remove(sd);
  4937. if (sd->regen.state.gc)
  4938. sd->regen.state.gc = 0;
  4939. // make sure vending is allowed here
  4940. if (sd->state.vending && map[m].flag.novending) {
  4941. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4942. vending_closevending(sd);
  4943. }
  4944. channel_pcquit(sd,4); //quit map chan
  4945. }
  4946. if( m < 0 )
  4947. {
  4948. uint32 ip;
  4949. uint16 port;
  4950. struct script_state *st;
  4951. //if can't find any map-servers, just abort setting position.
  4952. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4953. return SETPOS_NO_MAPSERVER;
  4954. if (sd->npc_id){
  4955. npc_event_dequeue(sd,false);
  4956. st = sd->st;
  4957. }else{
  4958. st = nullptr;
  4959. }
  4960. npc_script_event(sd, NPCE_LOGOUT);
  4961. //remove from map, THEN change x/y coordinates
  4962. unit_remove_map_pc(sd,clrtype);
  4963. sd->mapindex = mapindex;
  4964. sd->bl.x=x;
  4965. sd->bl.y=y;
  4966. pc_clean_skilltree(sd);
  4967. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  4968. chrif_changemapserver(sd, ip, (short)port);
  4969. //Free session data from this map server [Kevin]
  4970. unit_free_pc(sd);
  4971. if( st ){
  4972. // Has to be done here, because otherwise unit_free_pc will free the stack already
  4973. st->state = END;
  4974. }
  4975. return SETPOS_OK;
  4976. }
  4977. if( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  4978. {
  4979. 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);
  4980. x = y = 0; // make it random
  4981. }
  4982. if( x == 0 && y == 0 ) { // pick a random walkable cell
  4983. int c=0;
  4984. do {
  4985. x = rnd()%(map[m].xs-2)+1;
  4986. y = rnd()%(map[m].ys-2)+1;
  4987. c++;
  4988. if(c > (map[m].xs * map[m].ys)*3){ //force out
  4989. 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));
  4990. return SETPOS_OK; //preventing warp
  4991. //break; //allow warp anyway
  4992. }
  4993. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  4994. }
  4995. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  4996. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  4997. vending_closevending(sd);
  4998. }
  4999. if(sd->bl.prev != NULL){
  5000. unit_remove_map_pc(sd,clrtype);
  5001. clif_changemap(sd,m,x,y); // [MouseJstr]
  5002. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5003. sd->state.rewarp = 1;
  5004. sd->mapindex = mapindex;
  5005. sd->bl.m = m;
  5006. sd->bl.x = sd->ud.to_x = x;
  5007. sd->bl.y = sd->ud.to_y = y;
  5008. if( sd->status.guild_id > 0 && map[m].flag.gvg_castle )
  5009. { // Increased guild castle regen [Valaris]
  5010. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5011. if(gc && gc->guild_id == sd->status.guild_id)
  5012. sd->regen.state.gc = 1;
  5013. }
  5014. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5015. {
  5016. sd->pd->bl.m = m;
  5017. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5018. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5019. sd->pd->ud.dir = sd->ud.dir;
  5020. }
  5021. if( hom_is_active(sd->hd) )
  5022. {
  5023. sd->hd->bl.m = m;
  5024. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5025. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5026. sd->hd->ud.dir = sd->ud.dir;
  5027. }
  5028. if( sd->md )
  5029. {
  5030. sd->md->bl.m = m;
  5031. sd->md->bl.x = sd->md->ud.to_x = x;
  5032. sd->md->bl.y = sd->md->ud.to_y = y;
  5033. sd->md->ud.dir = sd->ud.dir;
  5034. }
  5035. if( sd->ed ) {
  5036. sd->ed->bl.m = m;
  5037. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5038. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5039. sd->ed->ud.dir = sd->ud.dir;
  5040. }
  5041. pc_cell_basilica(sd);
  5042. //check if we gonna be rewarped [lighta]
  5043. if(npc_check_areanpc(1,m,x,y,0)){
  5044. sd->count_rewarp++;
  5045. }
  5046. else
  5047. sd->count_rewarp = 0;
  5048. return SETPOS_OK;
  5049. }
  5050. /*==========================================
  5051. * Warp player sd to random location on current map.
  5052. * May fail if no walkable cell found (1000 attempts).
  5053. * Return:
  5054. * 0 = Success
  5055. * 1,2,3 = Fail
  5056. *------------------------------------------*/
  5057. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5058. {
  5059. int x,y,i=0;
  5060. int16 m;
  5061. nullpo_ret(sd);
  5062. m=sd->bl.m;
  5063. if (map[sd->bl.m].flag.noteleport) //Teleport forbidden
  5064. return 3;
  5065. do {
  5066. x = rnd()%(map[m].xs-2)+1;
  5067. y = rnd()%(map[m].ys-2)+1;
  5068. } while((map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1))) && (i++) < 1000);
  5069. if (i < 1000)
  5070. return pc_setpos(sd,map[sd->bl.m].index,x,y,type);
  5071. return 3;
  5072. }
  5073. /*==========================================
  5074. * Records a memo point at sd's current position
  5075. * pos - entry to replace, (-1: shift oldest entry out)
  5076. *------------------------------------------*/
  5077. bool pc_memo(struct map_session_data* sd, int pos)
  5078. {
  5079. int skill;
  5080. nullpo_ret(sd);
  5081. // check mapflags
  5082. if( sd->bl.m >= 0 && (map[sd->bl.m].flag.nomemo || map[sd->bl.m].flag.nowarpto) && !pc_has_permission(sd, PC_PERM_WARP_ANYWHERE) ) {
  5083. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5084. return false;
  5085. }
  5086. // check inputs
  5087. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5088. return false; // invalid input
  5089. // check required skill level
  5090. skill = pc_checkskill(sd, AL_WARP);
  5091. if( skill < 1 ) {
  5092. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5093. return false;
  5094. }
  5095. if( skill < 2 || skill - 2 < pos ) {
  5096. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5097. return false;
  5098. }
  5099. if( pos == -1 )
  5100. {
  5101. uint8 i;
  5102. // prevent memo-ing the same map multiple times
  5103. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5104. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5105. pos = 0;
  5106. }
  5107. if( map[sd->bl.m].instance_id ) {
  5108. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5109. return false;
  5110. }
  5111. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5112. sd->status.memo_point[pos].x = sd->bl.x;
  5113. sd->status.memo_point[pos].y = sd->bl.y;
  5114. clif_skill_memomessage(sd, 0);
  5115. return true;
  5116. }
  5117. //
  5118. // Skills
  5119. //
  5120. /**
  5121. * Get the skill current cooldown for player.
  5122. * (get the db base cooldown for skill + player specific cooldown)
  5123. * @param sd : player pointer
  5124. * @param id : skill id
  5125. * @param lv : skill lv
  5126. * @return player skill cooldown
  5127. */
  5128. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5129. uint8 i;
  5130. uint16 idx = skill_get_index(skill_id);
  5131. int cooldown = 0, cooldownlen = ARRAYLENGTH(sd->skillcooldown);
  5132. if (!idx) return 0;
  5133. if (skill_db[idx]->cooldown[skill_lv - 1])
  5134. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  5135. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5136. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5137. ARR_FIND(0, cooldownlen, i, sd->skillcooldown[i].id == skill_id);
  5138. if (i < cooldownlen) {
  5139. cooldown += sd->skillcooldown[i].val;
  5140. cooldown = max(0,cooldown);
  5141. }
  5142. return cooldown;
  5143. }
  5144. /*==========================================
  5145. * Return player sd skill_lv learned for given skill
  5146. *------------------------------------------*/
  5147. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5148. {
  5149. uint16 idx = 0;
  5150. if (sd == NULL)
  5151. return 0;
  5152. if ((idx = skill_get_index(skill_id)) == 0) {
  5153. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5154. return 0;
  5155. }
  5156. if (SKILL_CHK_GUILD(skill_id) ) {
  5157. struct guild *g;
  5158. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5159. return guild_checkskill(g,skill_id);
  5160. return 0;
  5161. }
  5162. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5163. }
  5164. /**
  5165. * Check if we still have the correct weapon to continue the skill (actually status)
  5166. * If not ending it
  5167. * @param sd
  5168. * @return 0:error, 1:check done
  5169. */
  5170. static void pc_checkallowskill(struct map_session_data *sd)
  5171. {
  5172. const enum sc_type scw_list[] = {
  5173. SC_TWOHANDQUICKEN,
  5174. SC_ONEHAND,
  5175. SC_AURABLADE,
  5176. SC_PARRYING,
  5177. SC_SPEARQUICKEN,
  5178. SC_ADRENALINE,
  5179. SC_ADRENALINE2,
  5180. SC_DANCING,
  5181. SC_GATLINGFEVER,
  5182. };
  5183. uint8 i;
  5184. nullpo_retv(sd);
  5185. if(!sd->sc.count)
  5186. return;
  5187. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5188. { // Skills requiring specific weapon types
  5189. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5190. continue;
  5191. if(sd->sc.data[scw_list[i]] &&
  5192. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5193. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5194. }
  5195. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5196. // Spurt requires bare hands (feet, in fact xD)
  5197. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5198. if(sd->status.shield <= 0) { // Skills requiring a shield
  5199. const enum sc_type scs_list[] = {
  5200. SC_AUTOGUARD,
  5201. SC_DEFENDER,
  5202. SC_REFLECTSHIELD,
  5203. SC_REFLECTDAMAGE
  5204. };
  5205. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5206. if(sd->sc.data[scs_list[i]])
  5207. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5208. }
  5209. }
  5210. /*==========================================
  5211. * Return equipped index of item on player sd at pos
  5212. * Return
  5213. * -1 : Nothing equipped
  5214. * idx : (this index could be used in inventory to found item_data)
  5215. *------------------------------------------*/
  5216. short pc_checkequip(struct map_session_data *sd,int pos)
  5217. {
  5218. uint8 i;
  5219. nullpo_retr(-1, sd);
  5220. for(i=0;i<EQI_MAX;i++){
  5221. if(pos & equip_bitmask[i])
  5222. return sd->equip_index[i];
  5223. }
  5224. return -1;
  5225. }
  5226. /*==========================================
  5227. * Check if sd has nameid equipped somewhere
  5228. * @sd : the player session
  5229. * @nameid : id of the item to check
  5230. * @min : : see pc.hpp enum equip_index from ? to @max
  5231. * @max : see pc.hpp enum equip_index for @min to ?
  5232. * -return true,false
  5233. *------------------------------------------*/
  5234. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5235. {
  5236. int i;
  5237. for(i = min; i < max; i++) {
  5238. if(equip_bitmask[i]) {
  5239. int idx = sd->equip_index[i];
  5240. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5241. return true;
  5242. }
  5243. }
  5244. return false;
  5245. }
  5246. /*==========================================
  5247. * Convert's from the client's lame Job ID system
  5248. * to the map server's 'makes sense' system. [Skotlex]
  5249. *------------------------------------------*/
  5250. int pc_jobid2mapid(unsigned short b_class)
  5251. {
  5252. switch(b_class)
  5253. {
  5254. //Novice And 1-1 Jobs
  5255. case JOB_NOVICE: return MAPID_NOVICE;
  5256. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5257. case JOB_MAGE: return MAPID_MAGE;
  5258. case JOB_ARCHER: return MAPID_ARCHER;
  5259. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5260. case JOB_MERCHANT: return MAPID_MERCHANT;
  5261. case JOB_THIEF: return MAPID_THIEF;
  5262. case JOB_TAEKWON: return MAPID_TAEKWON;
  5263. case JOB_WEDDING: return MAPID_WEDDING;
  5264. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5265. case JOB_NINJA: return MAPID_NINJA;
  5266. case JOB_XMAS: return MAPID_XMAS;
  5267. case JOB_SUMMER: return MAPID_SUMMER;
  5268. case JOB_HANBOK: return MAPID_HANBOK;
  5269. case JOB_GANGSI: return MAPID_GANGSI;
  5270. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5271. //2-1 Jobs
  5272. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5273. case JOB_KNIGHT: return MAPID_KNIGHT;
  5274. case JOB_WIZARD: return MAPID_WIZARD;
  5275. case JOB_HUNTER: return MAPID_HUNTER;
  5276. case JOB_PRIEST: return MAPID_PRIEST;
  5277. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5278. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5279. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5280. case JOB_KAGEROU:
  5281. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5282. case JOB_REBELLION: return MAPID_REBELLION;
  5283. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5284. //2-2 Jobs
  5285. case JOB_CRUSADER: return MAPID_CRUSADER;
  5286. case JOB_SAGE: return MAPID_SAGE;
  5287. case JOB_BARD:
  5288. case JOB_DANCER: return MAPID_BARDDANCER;
  5289. case JOB_MONK: return MAPID_MONK;
  5290. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5291. case JOB_ROGUE: return MAPID_ROGUE;
  5292. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5293. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5294. //Trans Novice And Trans 1-1 Jobs
  5295. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5296. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5297. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5298. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5299. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5300. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5301. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5302. //Trans 2-1 Jobs
  5303. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5304. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5305. case JOB_SNIPER: return MAPID_SNIPER;
  5306. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5307. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5308. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5309. //Trans 2-2 Jobs
  5310. case JOB_PALADIN: return MAPID_PALADIN;
  5311. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5312. case JOB_CLOWN:
  5313. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5314. case JOB_CHAMPION: return MAPID_CHAMPION;
  5315. case JOB_CREATOR: return MAPID_CREATOR;
  5316. case JOB_STALKER: return MAPID_STALKER;
  5317. //Baby Novice And Baby 1-1 Jobs
  5318. case JOB_BABY: return MAPID_BABY;
  5319. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5320. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5321. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5322. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5323. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5324. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5325. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5326. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5327. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5328. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5329. //Baby 2-1 Jobs
  5330. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5331. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5332. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5333. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5334. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5335. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5336. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5337. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5338. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5339. case JOB_BABY_KAGEROU:
  5340. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5341. //Baby 2-2 Jobs
  5342. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5343. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5344. case JOB_BABY_BARD:
  5345. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5346. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5347. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5348. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5349. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5350. //3-1 Jobs
  5351. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5352. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5353. case JOB_WARLOCK: return MAPID_WARLOCK;
  5354. case JOB_RANGER: return MAPID_RANGER;
  5355. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5356. case JOB_MECHANIC: return MAPID_MECHANIC;
  5357. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5358. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5359. //3-2 Jobs
  5360. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5361. case JOB_SORCERER: return MAPID_SORCERER;
  5362. case JOB_MINSTREL:
  5363. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5364. case JOB_SURA: return MAPID_SURA;
  5365. case JOB_GENETIC: return MAPID_GENETIC;
  5366. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5367. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5368. //Trans 3-1 Jobs
  5369. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5370. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5371. case JOB_RANGER_T: return MAPID_RANGER_T;
  5372. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5373. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5374. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5375. //Trans 3-2 Jobs
  5376. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5377. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5378. case JOB_MINSTREL_T:
  5379. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5380. case JOB_SURA_T: return MAPID_SURA_T;
  5381. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5382. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5383. //Baby 3-1 Jobs
  5384. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5385. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5386. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5387. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5388. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5389. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5390. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5391. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5392. //Baby 3-2 Jobs
  5393. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5394. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5395. case JOB_BABY_MINSTREL:
  5396. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5397. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5398. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5399. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5400. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5401. //Doram Jobs
  5402. case JOB_SUMMONER: return MAPID_SUMMONER;
  5403. default:
  5404. return -1;
  5405. }
  5406. }
  5407. //Reverts the map-style class id to the client-style one.
  5408. int pc_mapid2jobid(unsigned short class_, int sex)
  5409. {
  5410. switch(class_) {
  5411. //Novice And 1-1 Jobs
  5412. case MAPID_NOVICE: return JOB_NOVICE;
  5413. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5414. case MAPID_MAGE: return JOB_MAGE;
  5415. case MAPID_ARCHER: return JOB_ARCHER;
  5416. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5417. case MAPID_MERCHANT: return JOB_MERCHANT;
  5418. case MAPID_THIEF: return JOB_THIEF;
  5419. case MAPID_TAEKWON: return JOB_TAEKWON;
  5420. case MAPID_WEDDING: return JOB_WEDDING;
  5421. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5422. case MAPID_NINJA: return JOB_NINJA;
  5423. case MAPID_XMAS: return JOB_XMAS;
  5424. case MAPID_SUMMER: return JOB_SUMMER;
  5425. case MAPID_HANBOK: return JOB_HANBOK;
  5426. case MAPID_GANGSI: return JOB_GANGSI;
  5427. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5428. //2-1 Jobs
  5429. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5430. case MAPID_KNIGHT: return JOB_KNIGHT;
  5431. case MAPID_WIZARD: return JOB_WIZARD;
  5432. case MAPID_HUNTER: return JOB_HUNTER;
  5433. case MAPID_PRIEST: return JOB_PRIEST;
  5434. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5435. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5436. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5437. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5438. case MAPID_REBELLION: return JOB_REBELLION;
  5439. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5440. //2-2 Jobs
  5441. case MAPID_CRUSADER: return JOB_CRUSADER;
  5442. case MAPID_SAGE: return JOB_SAGE;
  5443. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5444. case MAPID_MONK: return JOB_MONK;
  5445. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5446. case MAPID_ROGUE: return JOB_ROGUE;
  5447. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5448. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5449. //Trans Novice And Trans 2-1 Jobs
  5450. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5451. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5452. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5453. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5454. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5455. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5456. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5457. //Trans 2-1 Jobs
  5458. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5459. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5460. case MAPID_SNIPER: return JOB_SNIPER;
  5461. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5462. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5463. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5464. //Trans 2-2 Jobs
  5465. case MAPID_PALADIN: return JOB_PALADIN;
  5466. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5467. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5468. case MAPID_CHAMPION: return JOB_CHAMPION;
  5469. case MAPID_CREATOR: return JOB_CREATOR;
  5470. case MAPID_STALKER: return JOB_STALKER;
  5471. //Baby Novice And Baby 1-1 Jobs
  5472. case MAPID_BABY: return JOB_BABY;
  5473. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5474. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5475. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5476. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5477. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5478. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5479. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5480. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5481. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5482. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5483. //Baby 2-1 Jobs
  5484. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5485. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5486. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5487. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5488. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5489. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5490. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5491. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5492. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5493. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5494. //Baby 2-2 Jobs
  5495. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5496. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5497. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5498. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5499. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5500. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5501. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5502. //3-1 Jobs
  5503. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5504. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5505. case MAPID_WARLOCK: return JOB_WARLOCK;
  5506. case MAPID_RANGER: return JOB_RANGER;
  5507. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5508. case MAPID_MECHANIC: return JOB_MECHANIC;
  5509. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5510. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5511. //3-2 Jobs
  5512. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5513. case MAPID_SORCERER: return JOB_SORCERER;
  5514. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5515. case MAPID_SURA: return JOB_SURA;
  5516. case MAPID_GENETIC: return JOB_GENETIC;
  5517. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5518. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5519. //Trans 3-1 Jobs
  5520. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5521. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5522. case MAPID_RANGER_T: return JOB_RANGER_T;
  5523. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5524. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5525. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5526. //Trans 3-2 Jobs
  5527. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5528. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5529. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5530. case MAPID_SURA_T: return JOB_SURA_T;
  5531. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5532. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5533. //Baby 3-1 Jobs
  5534. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5535. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5536. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5537. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5538. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5539. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5540. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5541. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5542. //Baby 3-2 Jobs
  5543. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5544. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5545. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5546. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5547. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5548. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5549. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5550. //Doram Jobs
  5551. case MAPID_SUMMONER: return JOB_SUMMONER;
  5552. default:
  5553. return -1;
  5554. }
  5555. }
  5556. /*====================================================
  5557. * This function return the name of the job (by [Yor])
  5558. *----------------------------------------------------*/
  5559. const char* job_name(int class_)
  5560. {
  5561. switch (class_) {
  5562. case JOB_NOVICE:
  5563. case JOB_SWORDMAN:
  5564. case JOB_MAGE:
  5565. case JOB_ARCHER:
  5566. case JOB_ACOLYTE:
  5567. case JOB_MERCHANT:
  5568. case JOB_THIEF:
  5569. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5570. case JOB_KNIGHT:
  5571. case JOB_PRIEST:
  5572. case JOB_WIZARD:
  5573. case JOB_BLACKSMITH:
  5574. case JOB_HUNTER:
  5575. case JOB_ASSASSIN:
  5576. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5577. case JOB_KNIGHT2:
  5578. return msg_txt(NULL,557);
  5579. case JOB_CRUSADER:
  5580. case JOB_MONK:
  5581. case JOB_SAGE:
  5582. case JOB_ROGUE:
  5583. case JOB_ALCHEMIST:
  5584. case JOB_BARD:
  5585. case JOB_DANCER:
  5586. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5587. case JOB_CRUSADER2:
  5588. return msg_txt(NULL,563);
  5589. case JOB_WEDDING:
  5590. case JOB_SUPER_NOVICE:
  5591. case JOB_GUNSLINGER:
  5592. case JOB_NINJA:
  5593. case JOB_XMAS:
  5594. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5595. case JOB_SUMMER:
  5596. return msg_txt(NULL,621);
  5597. case JOB_HANBOK:
  5598. return msg_txt(NULL,694);
  5599. case JOB_OKTOBERFEST:
  5600. return msg_txt(NULL,696);
  5601. case JOB_NOVICE_HIGH:
  5602. case JOB_SWORDMAN_HIGH:
  5603. case JOB_MAGE_HIGH:
  5604. case JOB_ARCHER_HIGH:
  5605. case JOB_ACOLYTE_HIGH:
  5606. case JOB_MERCHANT_HIGH:
  5607. case JOB_THIEF_HIGH:
  5608. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5609. case JOB_LORD_KNIGHT:
  5610. case JOB_HIGH_PRIEST:
  5611. case JOB_HIGH_WIZARD:
  5612. case JOB_WHITESMITH:
  5613. case JOB_SNIPER:
  5614. case JOB_ASSASSIN_CROSS:
  5615. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5616. case JOB_LORD_KNIGHT2:
  5617. return msg_txt(NULL,582);
  5618. case JOB_PALADIN:
  5619. case JOB_CHAMPION:
  5620. case JOB_PROFESSOR:
  5621. case JOB_STALKER:
  5622. case JOB_CREATOR:
  5623. case JOB_CLOWN:
  5624. case JOB_GYPSY:
  5625. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5626. case JOB_PALADIN2:
  5627. return msg_txt(NULL,588);
  5628. case JOB_BABY:
  5629. case JOB_BABY_SWORDMAN:
  5630. case JOB_BABY_MAGE:
  5631. case JOB_BABY_ARCHER:
  5632. case JOB_BABY_ACOLYTE:
  5633. case JOB_BABY_MERCHANT:
  5634. case JOB_BABY_THIEF:
  5635. return msg_txt(NULL,595 - JOB_BABY + class_);
  5636. case JOB_BABY_KNIGHT:
  5637. case JOB_BABY_PRIEST:
  5638. case JOB_BABY_WIZARD:
  5639. case JOB_BABY_BLACKSMITH:
  5640. case JOB_BABY_HUNTER:
  5641. case JOB_BABY_ASSASSIN:
  5642. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5643. case JOB_BABY_KNIGHT2:
  5644. return msg_txt(NULL,602);
  5645. case JOB_BABY_CRUSADER:
  5646. case JOB_BABY_MONK:
  5647. case JOB_BABY_SAGE:
  5648. case JOB_BABY_ROGUE:
  5649. case JOB_BABY_ALCHEMIST:
  5650. case JOB_BABY_BARD:
  5651. case JOB_BABY_DANCER:
  5652. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5653. case JOB_BABY_CRUSADER2:
  5654. return msg_txt(NULL,608);
  5655. case JOB_SUPER_BABY:
  5656. return msg_txt(NULL,615);
  5657. case JOB_TAEKWON:
  5658. return msg_txt(NULL,616);
  5659. case JOB_STAR_GLADIATOR:
  5660. case JOB_STAR_GLADIATOR2:
  5661. return msg_txt(NULL,617);
  5662. case JOB_SOUL_LINKER:
  5663. return msg_txt(NULL,618);
  5664. case JOB_GANGSI:
  5665. case JOB_DEATH_KNIGHT:
  5666. case JOB_DARK_COLLECTOR:
  5667. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5668. case JOB_RUNE_KNIGHT:
  5669. case JOB_WARLOCK:
  5670. case JOB_RANGER:
  5671. case JOB_ARCH_BISHOP:
  5672. case JOB_MECHANIC:
  5673. case JOB_GUILLOTINE_CROSS:
  5674. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5675. case JOB_RUNE_KNIGHT_T:
  5676. case JOB_WARLOCK_T:
  5677. case JOB_RANGER_T:
  5678. case JOB_ARCH_BISHOP_T:
  5679. case JOB_MECHANIC_T:
  5680. case JOB_GUILLOTINE_CROSS_T:
  5681. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5682. case JOB_ROYAL_GUARD:
  5683. case JOB_SORCERER:
  5684. case JOB_MINSTREL:
  5685. case JOB_WANDERER:
  5686. case JOB_SURA:
  5687. case JOB_GENETIC:
  5688. case JOB_SHADOW_CHASER:
  5689. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5690. case JOB_ROYAL_GUARD_T:
  5691. case JOB_SORCERER_T:
  5692. case JOB_MINSTREL_T:
  5693. case JOB_WANDERER_T:
  5694. case JOB_SURA_T:
  5695. case JOB_GENETIC_T:
  5696. case JOB_SHADOW_CHASER_T:
  5697. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5698. case JOB_RUNE_KNIGHT2:
  5699. case JOB_RUNE_KNIGHT_T2:
  5700. return msg_txt(NULL,625);
  5701. case JOB_ROYAL_GUARD2:
  5702. case JOB_ROYAL_GUARD_T2:
  5703. return msg_txt(NULL,631);
  5704. case JOB_RANGER2:
  5705. case JOB_RANGER_T2:
  5706. return msg_txt(NULL,627);
  5707. case JOB_MECHANIC2:
  5708. case JOB_MECHANIC_T2:
  5709. return msg_txt(NULL,629);
  5710. case JOB_BABY_RUNE:
  5711. case JOB_BABY_WARLOCK:
  5712. case JOB_BABY_RANGER:
  5713. case JOB_BABY_BISHOP:
  5714. case JOB_BABY_MECHANIC:
  5715. case JOB_BABY_CROSS:
  5716. case JOB_BABY_GUARD:
  5717. case JOB_BABY_SORCERER:
  5718. case JOB_BABY_MINSTREL:
  5719. case JOB_BABY_WANDERER:
  5720. case JOB_BABY_SURA:
  5721. case JOB_BABY_GENETIC:
  5722. case JOB_BABY_CHASER:
  5723. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5724. case JOB_BABY_RUNE2:
  5725. return msg_txt(NULL,638);
  5726. case JOB_BABY_GUARD2:
  5727. return msg_txt(NULL,644);
  5728. case JOB_BABY_RANGER2:
  5729. return msg_txt(NULL,640);
  5730. case JOB_BABY_MECHANIC2:
  5731. return msg_txt(NULL,642);
  5732. case JOB_SUPER_NOVICE_E:
  5733. case JOB_SUPER_BABY_E:
  5734. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5735. case JOB_KAGEROU:
  5736. case JOB_OBORO:
  5737. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5738. case JOB_REBELLION:
  5739. return msg_txt(NULL,695);
  5740. case JOB_SUMMONER:
  5741. return msg_txt(NULL,697);
  5742. case JOB_BABY_SUMMONER:
  5743. return msg_txt(NULL,698);
  5744. case JOB_BABY_NINJA:
  5745. return msg_txt(NULL,699);
  5746. case JOB_BABY_KAGEROU:
  5747. case JOB_BABY_OBORO:
  5748. case JOB_BABY_TAEKWON:
  5749. case JOB_BABY_STAR_GLADIATOR:
  5750. case JOB_BABY_SOUL_LINKER:
  5751. case JOB_BABY_GUNSLINGER:
  5752. case JOB_BABY_REBELLION:
  5753. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5754. case JOB_BABY_STAR_GLADIATOR2:
  5755. return msg_txt(NULL,756);
  5756. case JOB_STAR_EMPEROR:
  5757. case JOB_SOUL_REAPER:
  5758. case JOB_BABY_STAR_EMPEROR:
  5759. case JOB_BABY_SOUL_REAPER:
  5760. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  5761. case JOB_STAR_EMPEROR2:
  5762. return msg_txt(NULL,782);
  5763. case JOB_BABY_STAR_EMPEROR2:
  5764. return msg_txt(NULL,784);
  5765. default:
  5766. return msg_txt(NULL,655);
  5767. }
  5768. }
  5769. /*====================================================
  5770. * Timered function to make id follow a target.
  5771. * @id = bl.id (player only atm)
  5772. * target is define in sd->followtarget (bl.id)
  5773. * used by pc_follow
  5774. *----------------------------------------------------*/
  5775. int pc_follow_timer(int tid, unsigned int tick, int id, intptr_t data)
  5776. {
  5777. struct map_session_data *sd;
  5778. struct block_list *tbl;
  5779. sd = map_id2sd(id);
  5780. nullpo_ret(sd);
  5781. if (sd->followtimer != tid){
  5782. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5783. sd->followtimer = INVALID_TIMER;
  5784. return 0;
  5785. }
  5786. sd->followtimer = INVALID_TIMER;
  5787. tbl = map_id2bl(sd->followtarget);
  5788. if (tbl == NULL || pc_isdead(sd))
  5789. {
  5790. pc_stop_following(sd);
  5791. return 0;
  5792. }
  5793. // either player or target is currently detached from map blocks (could be teleporting),
  5794. // but still connected to this map, so we'll just increment the timer and check back later
  5795. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5796. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5797. {
  5798. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5799. if (!check_distance_bl(&sd->bl, tbl, 5))
  5800. unit_walktobl(&sd->bl, tbl, 5, 0);
  5801. } else
  5802. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5803. }
  5804. sd->followtimer = add_timer(
  5805. tick + 1000, // increase time a bit to loosen up map's load
  5806. pc_follow_timer, sd->bl.id, 0);
  5807. return 0;
  5808. }
  5809. int pc_stop_following (struct map_session_data *sd)
  5810. {
  5811. nullpo_ret(sd);
  5812. if (sd->followtimer != INVALID_TIMER) {
  5813. delete_timer(sd->followtimer,pc_follow_timer);
  5814. sd->followtimer = INVALID_TIMER;
  5815. }
  5816. sd->followtarget = -1;
  5817. sd->ud.target_to = 0;
  5818. unit_stop_walking(&sd->bl, 1);
  5819. return 0;
  5820. }
  5821. int pc_follow(struct map_session_data *sd,int target_id)
  5822. {
  5823. struct block_list *bl = map_id2bl(target_id);
  5824. if (bl == NULL /*|| bl->type != BL_PC*/)
  5825. return 1;
  5826. if (sd->followtimer != INVALID_TIMER)
  5827. pc_stop_following(sd);
  5828. sd->followtarget = target_id;
  5829. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5830. return 0;
  5831. }
  5832. int pc_checkbaselevelup(struct map_session_data *sd) {
  5833. unsigned int next = pc_nextbaseexp(sd);
  5834. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5835. return 0;
  5836. do {
  5837. sd->status.base_exp -= next;
  5838. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5839. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  5840. sd->status.base_exp = next-1;
  5841. next = pc_gets_status_point(sd->status.base_level);
  5842. sd->status.base_level++;
  5843. sd->status.status_point += next;
  5844. if( pc_is_maxbaselv(sd) ){
  5845. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5846. break;
  5847. }
  5848. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5849. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5850. status_calc_pet(sd->pd,SCO_NONE);
  5851. clif_updatestatus(sd,SP_STATUSPOINT);
  5852. clif_updatestatus(sd,SP_BASELEVEL);
  5853. clif_updatestatus(sd,SP_BASEEXP);
  5854. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5855. status_calc_pc(sd,SCO_FORCE);
  5856. status_percent_heal(&sd->bl,100,100);
  5857. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5858. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5859. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5860. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5861. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5862. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5863. if (sd->state.snovice_dead_flag)
  5864. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5865. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5866. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5867. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5868. }
  5869. clif_misceffect(&sd->bl,0);
  5870. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5871. if(sd->status.party_id)
  5872. party_send_levelup(sd);
  5873. pc_baselevelchanged(sd);
  5874. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  5875. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  5876. return 1;
  5877. }
  5878. void pc_baselevelchanged(struct map_session_data *sd) {
  5879. uint8 i;
  5880. for( i = 0; i < EQI_MAX; i++ ) {
  5881. if( sd->equip_index[i] >= 0 ) {
  5882. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5883. pc_unequipitem(sd, sd->equip_index[i], 3);
  5884. }
  5885. }
  5886. pc_show_questinfo(sd);
  5887. }
  5888. int pc_checkjoblevelup(struct map_session_data *sd)
  5889. {
  5890. unsigned int next = pc_nextjobexp(sd);
  5891. nullpo_ret(sd);
  5892. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  5893. return 0;
  5894. do {
  5895. sd->status.job_exp -= next;
  5896. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5897. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  5898. sd->status.job_exp = next-1;
  5899. sd->status.job_level ++;
  5900. sd->status.skill_point ++;
  5901. if( pc_is_maxjoblv(sd) ){
  5902. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  5903. break;
  5904. }
  5905. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5906. clif_updatestatus(sd,SP_JOBLEVEL);
  5907. clif_updatestatus(sd,SP_JOBEXP);
  5908. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5909. clif_updatestatus(sd,SP_SKILLPOINT);
  5910. status_calc_pc(sd,SCO_FORCE);
  5911. clif_misceffect(&sd->bl,1);
  5912. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  5913. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5914. npc_script_event(sd, NPCE_JOBLVUP);
  5915. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  5916. return 1;
  5917. }
  5918. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  5919. * @param sd Player
  5920. * @param base_exp Base EXP before peronal bonuses
  5921. * @param job_exp Job EXP before peronal bonuses
  5922. * @param src Block list that affecting the exp calculation
  5923. */
  5924. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5925. {
  5926. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  5927. if (src) {
  5928. struct status_data *status = status_get_status_data(src);
  5929. if( sd->expaddrace[status->race] )
  5930. bonus += sd->expaddrace[status->race];
  5931. if( sd->expaddrace[RC_ALL] )
  5932. bonus += sd->expaddrace[RC_ALL];
  5933. if( sd->expaddclass[status->class_] )
  5934. bonus += sd->expaddclass[status->class_];
  5935. if( sd->expaddclass[CLASS_ALL] )
  5936. bonus += sd->expaddclass[CLASS_ALL];
  5937. if (battle_config.pk_mode &&
  5938. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5939. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5940. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  5941. vip_bonus_base = battle_config.vip_base_exp_increase;
  5942. vip_bonus_job = battle_config.vip_job_exp_increase;
  5943. }
  5944. }
  5945. // Give EXPBOOST for quests even if src is NULL.
  5946. if (&sd->sc && sd->sc.data[SC_EXPBOOST]) {
  5947. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5948. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  5949. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  5950. }
  5951. if (*base_exp) {
  5952. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  5953. *base_exp = cap_value(exp, 1, UINT_MAX);
  5954. }
  5955. // Give JEXPBOOST for quests even if src is NULL.
  5956. if (&sd->sc && sd->sc.data[SC_JEXPBOOST])
  5957. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5958. if (*job_exp) {
  5959. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  5960. *job_exp = cap_value(exp, 1, UINT_MAX);
  5961. }
  5962. return;
  5963. }
  5964. /**
  5965. * Show EXP gained by player in percentage by @showexp
  5966. * @param sd Player
  5967. * @param base_exp Base EXP gained/loss
  5968. * @param next_base_exp Base EXP needed for next base level
  5969. * @param job_exp Job EXP gained/loss
  5970. * @param next_job_exp Job EXP needed for next job level
  5971. * @param lost True:EXP penalty, lose EXP
  5972. **/
  5973. 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) {
  5974. char output[CHAT_SIZE_MAX];
  5975. nullpo_retv(sd);
  5976. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  5977. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  5978. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  5979. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  5980. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5981. }
  5982. /**
  5983. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  5984. * @param sd Player
  5985. * @param src EXP source
  5986. * @param base_exp Base EXP gained
  5987. * @param base_exp Job EXP gained
  5988. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  5989. * @return
  5990. **/
  5991. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  5992. {
  5993. unsigned int nextb = 0, nextj = 0;
  5994. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  5995. nullpo_retv(sd);
  5996. if(sd->bl.prev == NULL || pc_isdead(sd))
  5997. return;
  5998. if (!(exp_flag&2)) {
  5999. if (!battle_config.pvp_exp && map[sd->bl.m].flag.pvp) // [MouseJstr]
  6000. return; // no exp on pvp maps
  6001. if (sd->status.guild_id>0)
  6002. base_exp -= guild_payexp(sd,base_exp);
  6003. }
  6004. flag = ((base_exp) ? 1 : 0) |
  6005. ((job_exp) ? 2 : 0) |
  6006. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6007. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6008. if (!(exp_flag&2))
  6009. pc_calcexp(sd, &base_exp, &job_exp, src);
  6010. nextb = pc_nextbaseexp(sd);
  6011. nextj = pc_nextjobexp(sd);
  6012. if (flag&4){
  6013. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6014. base_exp = 0;
  6015. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6016. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6017. }
  6018. if (flag&8){
  6019. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6020. job_exp = 0;
  6021. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6022. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6023. }
  6024. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6025. //Note that this value should never be greater than the original
  6026. //therefore no overflow checks are needed. [Skotlex]
  6027. if (nextb > 0) {
  6028. float nextbp = (float) base_exp / (float) nextb;
  6029. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6030. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6031. }
  6032. if (nextj > 0) {
  6033. float nextjp = (float) job_exp / (float) nextj;
  6034. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6035. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6036. }
  6037. }
  6038. // Give EXP for Base Level
  6039. if (base_exp) {
  6040. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  6041. sd->status.base_exp = UINT32_MAX;
  6042. else
  6043. sd->status.base_exp += base_exp;
  6044. if (!pc_checkbaselevelup(sd))
  6045. clif_updatestatus(sd,SP_BASEEXP);
  6046. }
  6047. // Give EXP for Job Level
  6048. if (job_exp) {
  6049. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  6050. sd->status.job_exp = UINT32_MAX;
  6051. else
  6052. sd->status.job_exp += job_exp;
  6053. if (!pc_checkjoblevelup(sd))
  6054. clif_updatestatus(sd,SP_JOBEXP);
  6055. }
  6056. if (flag&1)
  6057. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6058. if (flag&2)
  6059. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6060. if (sd->state.showexp && (base_exp || job_exp))
  6061. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6062. }
  6063. /**
  6064. * Lost Base/Job EXP from a player
  6065. * @param sd Player
  6066. * @param base_exp Base EXP lost
  6067. * @param job_exp Job EXP lost
  6068. **/
  6069. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  6070. nullpo_retv(sd);
  6071. if (base_exp) {
  6072. base_exp = u32min(sd->status.base_exp, base_exp);
  6073. sd->status.base_exp -= base_exp;
  6074. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6075. clif_updatestatus(sd, SP_BASEEXP);
  6076. }
  6077. if (job_exp) {
  6078. job_exp = u32min(sd->status.job_exp, job_exp);
  6079. sd->status.job_exp -= job_exp;
  6080. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6081. clif_updatestatus(sd, SP_JOBEXP);
  6082. }
  6083. if (sd->state.showexp && (base_exp || job_exp))
  6084. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6085. }
  6086. /**
  6087. * Returns max base level for this character's class.
  6088. * @param class_: Player's class
  6089. * @return Max Base Level
  6090. */
  6091. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6092. return job_info[pc_class2idx(class_)].max_level[0];
  6093. }
  6094. /**
  6095. * Returns max base level for this character.
  6096. * @param sd Player
  6097. * @return Max Base Level
  6098. **/
  6099. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6100. return pc_class_maxbaselv(sd->status.class_);
  6101. }
  6102. /**
  6103. * Returns max job level for this character's class.
  6104. * @param class_: Player's class
  6105. * @return Max Job Level
  6106. */
  6107. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6108. return job_info[pc_class2idx(class_)].max_level[1];
  6109. }
  6110. /**
  6111. * Returns max job level for this character.
  6112. * @param sd Player
  6113. * @return Max Job Level
  6114. **/
  6115. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6116. return pc_class_maxjoblv(sd->status.class_);
  6117. }
  6118. /**
  6119. * Check if player is reached max base level
  6120. * @param sd
  6121. * @return True if reached max level
  6122. **/
  6123. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6124. nullpo_retr(false, sd);
  6125. return (sd->status.base_level >= pc_maxbaselv(sd));
  6126. }
  6127. /**
  6128. * Check if player is reached max base level
  6129. * @param sd
  6130. * @return True if reached max level
  6131. **/
  6132. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6133. nullpo_retr(false, sd);
  6134. return (sd->status.job_level >= pc_maxjoblv(sd));
  6135. }
  6136. /**
  6137. * Base exp needed for player to level up.
  6138. * @param sd
  6139. * @return Base EXP needed for next base level
  6140. **/
  6141. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6142. nullpo_ret(sd);
  6143. if (sd->status.base_level == 0) // Is this something that possible?
  6144. return 0;
  6145. if (pc_is_maxbaselv(sd))
  6146. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6147. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6148. }
  6149. /**
  6150. * Job exp needed for player to level up.
  6151. * @param sd
  6152. * @return Job EXP needed for next job level
  6153. **/
  6154. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6155. nullpo_ret(sd);
  6156. if (sd->status.job_level == 0) // Is this something that possible?
  6157. return 0;
  6158. if (pc_is_maxjoblv(sd))
  6159. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6160. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6161. }
  6162. /// Returns the value of the specified stat.
  6163. static int pc_getstat(struct map_session_data* sd, int type)
  6164. {
  6165. nullpo_retr(-1, sd);
  6166. switch( type ) {
  6167. case SP_STR: return sd->status.str;
  6168. case SP_AGI: return sd->status.agi;
  6169. case SP_VIT: return sd->status.vit;
  6170. case SP_INT: return sd->status.int_;
  6171. case SP_DEX: return sd->status.dex;
  6172. case SP_LUK: return sd->status.luk;
  6173. default:
  6174. return -1;
  6175. }
  6176. }
  6177. /// Sets the specified stat to the specified value.
  6178. /// Returns the new value.
  6179. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6180. {
  6181. nullpo_retr(-1, sd);
  6182. switch( type ) {
  6183. case SP_STR: sd->status.str = val; break;
  6184. case SP_AGI: sd->status.agi = val; break;
  6185. case SP_VIT: sd->status.vit = val; break;
  6186. case SP_INT: sd->status.int_ = val; break;
  6187. case SP_DEX: sd->status.dex = val; break;
  6188. case SP_LUK: sd->status.luk = val; break;
  6189. default:
  6190. return -1;
  6191. }
  6192. return val;
  6193. }
  6194. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6195. int pc_gets_status_point(int level)
  6196. {
  6197. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6198. return (statp[level+1] - statp[level]);
  6199. else //Default increase
  6200. return ((level+15) / 5);
  6201. }
  6202. #ifdef RENEWAL_STAT
  6203. /// Renewal status point cost formula
  6204. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6205. #else
  6206. /// Pre-Renewal status point cost formula
  6207. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6208. #endif
  6209. /// Returns the number of stat points needed to change the specified stat by val.
  6210. /// If val is negative, returns the number of stat points that would be needed to
  6211. /// raise the specified stat from (current value - val) to current value.
  6212. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6213. {
  6214. int low, high, sp = 0, max = 0;
  6215. if ( val == 0 )
  6216. return 0;
  6217. low = pc_getstat(sd,type);
  6218. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6219. if ( low >= max && val > 0 )
  6220. return 0; // Official servers show '0' when max is reached
  6221. high = low + val;
  6222. if ( val < 0 )
  6223. SWAP(low, high);
  6224. for ( ; low < high; low++ )
  6225. sp += PC_STATUS_POINT_COST(low);
  6226. return sp;
  6227. }
  6228. /**
  6229. * Returns the value the specified stat can be increased by with the current
  6230. * amount of available status points for the current character's class.
  6231. *
  6232. * @param sd The target character.
  6233. * @param type Stat to verify.
  6234. * @return Maximum value the stat could grow by.
  6235. */
  6236. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6237. {
  6238. int base, final_val, status_points, max_param;
  6239. nullpo_ret(sd);
  6240. base = final_val = pc_getstat(sd, type);
  6241. status_points = sd->status.status_point;
  6242. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6243. while (final_val <= max_param && status_points >= 0) {
  6244. status_points -= PC_STATUS_POINT_COST(final_val);
  6245. final_val++;
  6246. }
  6247. final_val--;
  6248. return (final_val > base ? final_val-base : 0);
  6249. }
  6250. /**
  6251. * Raises a stat by the specified amount.
  6252. *
  6253. * Obeys max_parameter limits.
  6254. * Subtracts status points according to the cost of the increased stat points.
  6255. *
  6256. * @param sd The target character.
  6257. * @param type The stat to change (see enum _sp)
  6258. * @param increase The stat increase (strictly positive) amount.
  6259. * @retval true if the stat was increased by any amount.
  6260. * @retval false if there were no changes.
  6261. */
  6262. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6263. {
  6264. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6265. nullpo_ret(sd);
  6266. // check conditions
  6267. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6268. clif_statusupack(sd, type, 0, 0);
  6269. return false;
  6270. }
  6271. // check limits
  6272. current = pc_getstat(sd, type);
  6273. max_increase = pc_maxparameterincrease(sd, type);
  6274. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6275. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6276. clif_statusupack(sd, type, 0, 0);
  6277. return false;
  6278. }
  6279. // check status points
  6280. needed_points = pc_need_status_point(sd, type, increase);
  6281. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6282. clif_statusupack(sd, type, 0, 0);
  6283. return false;
  6284. }
  6285. // set new values
  6286. final_value = pc_setstat(sd, type, current + increase);
  6287. sd->status.status_point -= needed_points;
  6288. status_calc_pc(sd,SCO_NONE);
  6289. // update increase cost indicator
  6290. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6291. // update statpoint count
  6292. clif_updatestatus(sd, SP_STATUSPOINT);
  6293. // update stat value
  6294. clif_statusupack(sd, type, 1, final_value); // required
  6295. if( final_value > 255 )
  6296. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6297. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6298. return true;
  6299. }
  6300. /**
  6301. * Raises a stat by the specified amount.
  6302. *
  6303. * Obeys max_parameter limits.
  6304. * Does not subtract status points for the cost of the modified stat points.
  6305. *
  6306. * @param sd The target character.
  6307. * @param type The stat to change (see enum _sp)
  6308. * @param val The stat increase (or decrease) amount.
  6309. * @return the stat increase amount.
  6310. * @retval 0 if no changes were made.
  6311. */
  6312. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6313. {
  6314. int max, need;
  6315. nullpo_ret(sd);
  6316. if( type < SP_STR || type > SP_LUK )
  6317. {
  6318. clif_statusupack(sd,type,0,0);
  6319. return 0;
  6320. }
  6321. need = pc_need_status_point(sd,type,1);
  6322. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6323. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6324. status_calc_pc(sd,SCO_NONE);
  6325. // update increase cost indicator
  6326. if( need != pc_need_status_point(sd,type,1) )
  6327. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6328. // update stat value
  6329. clif_statusupack(sd,type,1,val); // required
  6330. if( val > 255 )
  6331. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6332. return val;
  6333. }
  6334. /*==========================================
  6335. * Update skill_lv for player sd
  6336. * Skill point allocation
  6337. *------------------------------------------*/
  6338. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6339. {
  6340. uint16 idx = skill_get_index(skill_id);
  6341. nullpo_retv(sd);
  6342. if (!idx) {
  6343. if (skill_id)
  6344. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6345. return;
  6346. }
  6347. // Level up guild skill
  6348. if (SKILL_CHK_GUILD(skill_id)) {
  6349. guild_skillup(sd, skill_id);
  6350. return;
  6351. }
  6352. // Level up homunculus skill
  6353. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6354. hom_skillup(sd->hd, skill_id);
  6355. return;
  6356. }
  6357. else {
  6358. if( sd->status.skill_point > 0 &&
  6359. sd->status.skill[idx].id &&
  6360. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6361. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6362. {
  6363. int lv, range, upgradable;
  6364. sd->status.skill[idx].lv++;
  6365. sd->status.skill_point--;
  6366. if( !skill_get_inf(skill_id) )
  6367. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6368. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6369. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6370. else
  6371. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6372. lv = sd->status.skill[idx].lv;
  6373. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6374. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6375. clif_skillup(sd,skill_id,lv,range,upgradable);
  6376. clif_updatestatus(sd,SP_SKILLPOINT);
  6377. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6378. clif_updatestatus(sd,SP_CARTINFO);
  6379. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6380. clif_skillinfoblock(sd);
  6381. }
  6382. //else
  6383. // 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);
  6384. }
  6385. }
  6386. /*==========================================
  6387. * /allskill
  6388. *------------------------------------------*/
  6389. int pc_allskillup(struct map_session_data *sd)
  6390. {
  6391. int i;
  6392. nullpo_ret(sd);
  6393. for (i = 0; i < MAX_SKILL; i++) {
  6394. 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) {
  6395. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6396. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6397. if (sd->status.skill[i].lv == 0)
  6398. sd->status.skill[i].id = 0;
  6399. }
  6400. }
  6401. if (!pc_grant_allskills(sd, true)) {
  6402. uint16 sk_id;
  6403. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6404. int inf2 = 0;
  6405. uint16 sk_idx = 0;
  6406. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6407. continue;
  6408. inf2 = skill_get_inf2(sk_id);
  6409. if (
  6410. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6411. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6412. sk_id == SG_DEVIL
  6413. )
  6414. continue; //Cannot be learned normally.
  6415. sd->status.skill[sk_idx].id = sk_id;
  6416. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6417. }
  6418. }
  6419. status_calc_pc(sd,SCO_NONE);
  6420. //Required because if you could level up all skills previously,
  6421. //the update will not be sent as only the lv variable changes.
  6422. clif_skillinfoblock(sd);
  6423. return 0;
  6424. }
  6425. /*==========================================
  6426. * /resetlvl
  6427. *------------------------------------------*/
  6428. int pc_resetlvl(struct map_session_data* sd,int type)
  6429. {
  6430. int i;
  6431. nullpo_ret(sd);
  6432. if (type != 3) //Also reset skills
  6433. pc_resetskill(sd, 0);
  6434. if(type == 1){
  6435. sd->status.skill_point=0;
  6436. sd->status.base_level=1;
  6437. sd->status.job_level=1;
  6438. sd->status.base_exp=0;
  6439. sd->status.job_exp=0;
  6440. if(sd->sc.option !=0)
  6441. sd->sc.option = 0;
  6442. sd->status.str=1;
  6443. sd->status.agi=1;
  6444. sd->status.vit=1;
  6445. sd->status.int_=1;
  6446. sd->status.dex=1;
  6447. sd->status.luk=1;
  6448. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6449. sd->status.status_point=100; // not 88 [celest]
  6450. // give platinum skills upon changing
  6451. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6452. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6453. }
  6454. }
  6455. if(type == 2){
  6456. sd->status.skill_point=0;
  6457. sd->status.base_level=1;
  6458. sd->status.job_level=1;
  6459. sd->status.base_exp=0;
  6460. sd->status.job_exp=0;
  6461. }
  6462. if(type == 3){
  6463. sd->status.base_level=1;
  6464. sd->status.base_exp=0;
  6465. }
  6466. if(type == 4){
  6467. sd->status.job_level=1;
  6468. sd->status.job_exp=0;
  6469. }
  6470. clif_updatestatus(sd,SP_STATUSPOINT);
  6471. clif_updatestatus(sd,SP_STR);
  6472. clif_updatestatus(sd,SP_AGI);
  6473. clif_updatestatus(sd,SP_VIT);
  6474. clif_updatestatus(sd,SP_INT);
  6475. clif_updatestatus(sd,SP_DEX);
  6476. clif_updatestatus(sd,SP_LUK);
  6477. clif_updatestatus(sd,SP_BASELEVEL);
  6478. clif_updatestatus(sd,SP_JOBLEVEL);
  6479. clif_updatestatus(sd,SP_STATUSPOINT);
  6480. clif_updatestatus(sd,SP_BASEEXP);
  6481. clif_updatestatus(sd,SP_JOBEXP);
  6482. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6483. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6484. clif_updatestatus(sd,SP_SKILLPOINT);
  6485. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6486. clif_updatestatus(sd,SP_UAGI);
  6487. clif_updatestatus(sd,SP_UVIT);
  6488. clif_updatestatus(sd,SP_UINT);
  6489. clif_updatestatus(sd,SP_UDEX);
  6490. clif_updatestatus(sd,SP_ULUK); // End Addition
  6491. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6492. if(sd->equip_index[i] >= 0)
  6493. if(pc_isequip(sd,sd->equip_index[i]))
  6494. pc_unequipitem(sd,sd->equip_index[i],2);
  6495. }
  6496. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6497. party_send_levelup(sd);
  6498. status_calc_pc(sd, SCO_FORCE);
  6499. clif_skillinfoblock(sd);
  6500. return 0;
  6501. }
  6502. /*==========================================
  6503. * /resetstate
  6504. *------------------------------------------*/
  6505. int pc_resetstate(struct map_session_data* sd)
  6506. {
  6507. nullpo_ret(sd);
  6508. if (battle_config.use_statpoint_table)
  6509. { // New statpoint table used here - Dexity
  6510. if (sd->status.base_level > MAX_LEVEL)
  6511. { //statp[] goes out of bounds, can't reset!
  6512. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6513. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6514. return 0;
  6515. }
  6516. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6517. }
  6518. else
  6519. {
  6520. int add=0;
  6521. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6522. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6523. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6524. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6525. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6526. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6527. sd->status.status_point+=add;
  6528. }
  6529. pc_setstat(sd, SP_STR, 1);
  6530. pc_setstat(sd, SP_AGI, 1);
  6531. pc_setstat(sd, SP_VIT, 1);
  6532. pc_setstat(sd, SP_INT, 1);
  6533. pc_setstat(sd, SP_DEX, 1);
  6534. pc_setstat(sd, SP_LUK, 1);
  6535. clif_updatestatus(sd,SP_STR);
  6536. clif_updatestatus(sd,SP_AGI);
  6537. clif_updatestatus(sd,SP_VIT);
  6538. clif_updatestatus(sd,SP_INT);
  6539. clif_updatestatus(sd,SP_DEX);
  6540. clif_updatestatus(sd,SP_LUK);
  6541. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6542. clif_updatestatus(sd,SP_UAGI);
  6543. clif_updatestatus(sd,SP_UVIT);
  6544. clif_updatestatus(sd,SP_UINT);
  6545. clif_updatestatus(sd,SP_UDEX);
  6546. clif_updatestatus(sd,SP_ULUK); // End Addition
  6547. clif_updatestatus(sd,SP_STATUSPOINT);
  6548. if( sd->mission_mobid ) { //bugreport:2200
  6549. sd->mission_mobid = 0;
  6550. sd->mission_count = 0;
  6551. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6552. }
  6553. status_calc_pc(sd, SCO_NONE);
  6554. return 1;
  6555. }
  6556. /*==========================================
  6557. * /resetskill
  6558. * if flag&1, perform block resync and status_calc call.
  6559. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6560. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6561. *------------------------------------------*/
  6562. int pc_resetskill(struct map_session_data* sd, int flag)
  6563. {
  6564. int i, skill_point=0;
  6565. nullpo_ret(sd);
  6566. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6567. return 0;
  6568. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6569. /**
  6570. * 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)
  6571. **/
  6572. if( pc_is_taekwon_ranker(sd) )
  6573. return 0;
  6574. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6575. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6576. i = sd->sc.option;
  6577. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6578. i &= ~OPTION_RIDING;
  6579. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6580. i &= ~OPTION_FALCON;
  6581. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6582. i &= ~OPTION_DRAGON;
  6583. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6584. i &= ~OPTION_WUG;
  6585. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6586. i &= ~OPTION_WUGRIDER;
  6587. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6588. i &= ~OPTION_MADOGEAR;
  6589. #ifndef NEW_CARTS
  6590. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6591. i &= ~OPTION_CART;
  6592. #else
  6593. if( sd->sc.data[SC_PUSH_CART] )
  6594. pc_setcart(sd, 0);
  6595. #endif
  6596. if( i != sd->sc.option )
  6597. pc_setoption(sd, i);
  6598. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6599. hom_vaporize(sd, HOM_ST_ACTIVE);
  6600. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6601. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6602. }
  6603. for( i = 1; i < MAX_SKILL; i++ )
  6604. {
  6605. uint8 lv = sd->status.skill[i].lv;
  6606. int inf2;
  6607. uint16 skill_id = skill_idx2id(i);
  6608. if (lv == 0 || skill_id == 0)
  6609. continue;
  6610. inf2 = skill_get_inf2(skill_id);
  6611. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6612. continue;
  6613. // Don't reset trick dead if not a novice/baby
  6614. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6615. {
  6616. sd->status.skill[i].lv = 0;
  6617. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6618. continue;
  6619. }
  6620. // do not reset basic skill
  6621. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6622. continue;
  6623. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6624. continue;
  6625. if( flag&4 && !skill_ischangesex(skill_id) )
  6626. continue;
  6627. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6628. { //Only handle quest skills in a special way when you can't learn them manually
  6629. if( battle_config.quest_skill_reset && !(flag&2) )
  6630. { //Wipe them
  6631. sd->status.skill[i].lv = 0;
  6632. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6633. }
  6634. continue;
  6635. }
  6636. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6637. skill_point += lv;
  6638. else
  6639. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6640. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6641. if( !(flag&2) )
  6642. {// reset
  6643. sd->status.skill[i].lv = 0;
  6644. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6645. }
  6646. }
  6647. if( flag&2 || !skill_point ) return skill_point;
  6648. sd->status.skill_point += skill_point;
  6649. if (flag&1) {
  6650. clif_updatestatus(sd,SP_SKILLPOINT);
  6651. clif_skillinfoblock(sd);
  6652. status_calc_pc(sd, SCO_FORCE);
  6653. }
  6654. return skill_point;
  6655. }
  6656. /*==========================================
  6657. * /resetfeel [Komurka]
  6658. *------------------------------------------*/
  6659. int pc_resetfeel(struct map_session_data* sd)
  6660. {
  6661. int i;
  6662. nullpo_ret(sd);
  6663. for (i=0; i<MAX_PC_FEELHATE; i++)
  6664. {
  6665. sd->feel_map[i].m = -1;
  6666. sd->feel_map[i].index = 0;
  6667. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6668. }
  6669. return 0;
  6670. }
  6671. int pc_resethate(struct map_session_data* sd)
  6672. {
  6673. int i;
  6674. nullpo_ret(sd);
  6675. for (i=0; i<3; i++)
  6676. {
  6677. sd->hate_mob[i] = -1;
  6678. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6679. }
  6680. return 0;
  6681. }
  6682. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6683. {
  6684. int i, bonus = 0;
  6685. nullpo_ret(sd);
  6686. skill_id = skill_dummy2skill_id(skill_id);
  6687. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  6688. if( i < ARRAYLENGTH(sd->skillatk) )
  6689. bonus = sd->skillatk[i].val;
  6690. return bonus;
  6691. }
  6692. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6693. {
  6694. int i, bonus = 0;
  6695. nullpo_ret(sd);
  6696. skill_id = skill_dummy2skill_id(skill_id);
  6697. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == skill_id);
  6698. if( i < ARRAYLENGTH(sd->subskill) )
  6699. bonus = sd->subskill[i].val;
  6700. return bonus;
  6701. }
  6702. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6703. int i, bonus = sd->bonus.add_heal_rate;
  6704. skill_id = skill_dummy2skill_id(skill_id);
  6705. if( bonus ) {
  6706. switch( skill_id ) {
  6707. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6708. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6709. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6710. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6711. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6712. }
  6713. }
  6714. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  6715. if( i < ARRAYLENGTH(sd->skillheal) )
  6716. bonus += sd->skillheal[i].val;
  6717. return bonus;
  6718. }
  6719. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6720. int i, bonus = sd->bonus.add_heal2_rate;
  6721. skill_id = skill_dummy2skill_id(skill_id);
  6722. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  6723. if( i < ARRAYLENGTH(sd->skillheal2) )
  6724. bonus += sd->skillheal2[i].val;
  6725. return bonus;
  6726. }
  6727. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6728. {
  6729. if( !pc_isdead(sd) )
  6730. return; // not applicable
  6731. if( sd->bg_id && bg_member_respawn(sd) )
  6732. return; // member revived by battleground
  6733. pc_setstand(sd, true);
  6734. pc_setrestartvalue(sd,3);
  6735. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6736. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6737. }
  6738. static int pc_respawn_timer(int tid, unsigned int tick, int id, intptr_t data)
  6739. {
  6740. struct map_session_data *sd = map_id2sd(id);
  6741. if( sd != NULL )
  6742. {
  6743. sd->pvp_point=0;
  6744. pc_respawn(sd,CLR_OUTSIGHT);
  6745. }
  6746. return 0;
  6747. }
  6748. /*==========================================
  6749. * Invoked when a player has received damage
  6750. *------------------------------------------*/
  6751. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6752. {
  6753. if (sp) clif_updatestatus(sd,SP_SP);
  6754. if (hp) clif_updatestatus(sd,SP_HP);
  6755. else return;
  6756. if (!src)
  6757. return;
  6758. if( pc_issit(sd) ) {
  6759. pc_setstand(sd, true);
  6760. skill_sit(sd,0);
  6761. }
  6762. if (sd->progressbar.npc_id)
  6763. clif_progressbar_abort(sd);
  6764. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6765. pet_target_check(sd->pd,src,1);
  6766. if( sd->status.ele_id > 0 )
  6767. elemental_set_target(sd,src);
  6768. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  6769. sd->canlog_tick = gettick();
  6770. }
  6771. int pc_close_npc_timer(int tid, unsigned int tick, int id, intptr_t data)
  6772. {
  6773. TBL_PC *sd = map_id2sd(id);
  6774. if(sd) pc_close_npc(sd,data);
  6775. return 0;
  6776. }
  6777. /*
  6778. * Method to properly close npc for player and clear anything related
  6779. * @flag == 1 : produce close button
  6780. * @flag == 2 : directly close it
  6781. */
  6782. void pc_close_npc(struct map_session_data *sd,int flag)
  6783. {
  6784. nullpo_retv(sd);
  6785. if (sd->npc_id || sd->npc_shopid) {
  6786. if (sd->state.using_fake_npc) {
  6787. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6788. sd->state.using_fake_npc = 0;
  6789. }
  6790. if (sd->st) {
  6791. if(sd->st->state == RUN){ //wait ending code execution
  6792. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6793. return;
  6794. }
  6795. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6796. sd->st->mes_active = 0;
  6797. }
  6798. sd->state.menu_or_input = 0;
  6799. sd->npc_menu = 0;
  6800. sd->npc_shopid = 0;
  6801. #ifdef SECURE_NPCTIMEOUT
  6802. sd->npc_idle_timer = INVALID_TIMER;
  6803. #endif
  6804. clif_scriptclose(sd,sd->npc_id);
  6805. clif_scriptclear(sd,sd->npc_id); // [Ind/Hercules]
  6806. if(sd->st && sd->st->state == END ) {// free attached scripts that are waiting
  6807. script_free_state(sd->st);
  6808. sd->st = NULL;
  6809. sd->npc_id = 0;
  6810. }
  6811. }
  6812. }
  6813. /*==========================================
  6814. * Invoked when a player has negative current hp
  6815. *------------------------------------------*/
  6816. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6817. {
  6818. int i=0,k=0;
  6819. unsigned int tick = gettick();
  6820. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6821. // Super Novices have no kill or die functions attached when saved by their angel
  6822. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6823. unsigned int exp = pc_nextbaseexp(sd);
  6824. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6825. sd->state.snovice_dead_flag = 1;
  6826. pc_setrestartvalue(sd,1);
  6827. status_percent_heal(&sd->bl, 100, 100);
  6828. clif_resurrection(&sd->bl, 1);
  6829. if(battle_config.pc_invincible_time)
  6830. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6831. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6832. if(map_flag_gvg2(sd->bl.m))
  6833. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6834. return 0;
  6835. }
  6836. }
  6837. for(k = 0; k < MAX_DEVOTION; k++) {
  6838. if (sd->devotion[k]){
  6839. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6840. if (devsd)
  6841. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6842. sd->devotion[k] = 0;
  6843. }
  6844. }
  6845. if(sd->shadowform_id) { //if we were target of shadowform
  6846. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6847. sd->shadowform_id = 0; //should be remove on status end anyway
  6848. }
  6849. if(sd->status.pet_id > 0 && sd->pd) {
  6850. struct pet_data *pd = sd->pd;
  6851. if( !map[sd->bl.m].flag.noexppenalty ) {
  6852. pet_set_intimate(pd, pd->pet.intimate - pd->get_pet_db()->die);
  6853. if( pd->pet.intimate < 0 )
  6854. pd->pet.intimate = 0;
  6855. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  6856. }
  6857. if( sd->pd->target_id ) // Unlock all targets...
  6858. pet_unlocktarget(sd->pd);
  6859. }
  6860. 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)
  6861. hom_vaporize(sd, HOM_ST_ACTIVE);
  6862. if( sd->md )
  6863. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  6864. if( sd->ed )
  6865. elemental_delete(sd->ed);
  6866. // Leave duel if you die [LuzZza]
  6867. if(battle_config.duel_autoleave_when_die) {
  6868. if(sd->duel_group > 0)
  6869. duel_leave(sd->duel_group, sd);
  6870. if(sd->duel_invite > 0)
  6871. duel_reject(sd->duel_invite, sd);
  6872. }
  6873. pc_close_npc(sd,2); //close npc if we were using one
  6874. /* e.g. not killed thru pc_damage */
  6875. if( pc_issit(sd) ) {
  6876. clif_status_load(&sd->bl,EFST_SIT,0);
  6877. }
  6878. pc_setdead(sd);
  6879. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
  6880. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  6881. //Reset menu skills/item skills
  6882. if ((sd->skillitem) != 0)
  6883. sd->skillitem = sd->skillitemlv = 0;
  6884. if ((sd->menuskill_id) != 0)
  6885. sd->menuskill_id = sd->menuskill_val = 0;
  6886. //Reset ticks.
  6887. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  6888. if ( sd->spiritball !=0 )
  6889. pc_delspiritball(sd,sd->spiritball,0);
  6890. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  6891. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  6892. if (src)
  6893. switch (src->type) {
  6894. case BL_MOB:
  6895. {
  6896. struct mob_data *md=(struct mob_data *)src;
  6897. if(md->target_id==sd->bl.id)
  6898. mob_unlocktarget(md,tick);
  6899. if(battle_config.mobs_level_up && md->status.hp &&
  6900. (unsigned int)md->level < pc_maxbaselv(sd) &&
  6901. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  6902. ) { // monster level up [Valaris]
  6903. clif_misceffect(&md->bl,0);
  6904. md->level++;
  6905. status_calc_mob(md, SCO_NONE);
  6906. status_percent_heal(src,10,0);
  6907. if( battle_config.show_mob_info&4 )
  6908. {// update name with new level
  6909. clif_name_area(&md->bl);
  6910. }
  6911. }
  6912. src = battle_get_master(src); // Maybe Player Summon
  6913. }
  6914. break;
  6915. case BL_PET: //Pass on to master...
  6916. case BL_HOM:
  6917. case BL_MER:
  6918. src = battle_get_master(src);
  6919. break;
  6920. }
  6921. if (src && src->type == BL_PC) {
  6922. struct map_session_data *ssd = (struct map_session_data *)src;
  6923. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  6924. npc_script_event(ssd, NPCE_KILLPC);
  6925. if (battle_config.pk_mode&2) {
  6926. ssd->status.manner -= 5;
  6927. if(ssd->status.manner < 0)
  6928. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6929. #if 0
  6930. // PK/Karma system code (not enabled yet) [celest]
  6931. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6932. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6933. // karma going down = more 'good' / more honourable.
  6934. // The Karma System way...
  6935. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6936. sd->status.karma--;
  6937. ssd->status.karma--;
  6938. }
  6939. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6940. ssd->status.karma++;
  6941. // or the PK System way...
  6942. if (sd->status.karma > 0) // player killed is dishonourable?
  6943. ssd->status.karma--; // honour points earned
  6944. sd->status.karma++; // honour points lost
  6945. // To-do: Receive exp on certain occasions
  6946. #endif
  6947. }
  6948. }
  6949. if(battle_config.bone_drop==2
  6950. || (battle_config.bone_drop==1 && map[sd->bl.m].flag.pvp))
  6951. {
  6952. struct item item_tmp;
  6953. memset(&item_tmp,0,sizeof(item_tmp));
  6954. item_tmp.nameid=ITEMID_SKULL_;
  6955. item_tmp.identify=1;
  6956. item_tmp.card[0]=CARD0_CREATE;
  6957. item_tmp.card[1]=0;
  6958. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6959. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6960. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6961. }
  6962. //Remove bonus_script when dead
  6963. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  6964. // changed penalty options, added death by player if pk_mode [Valaris]
  6965. if(battle_config.death_penalty_type
  6966. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6967. && !map[sd->bl.m].flag.noexppenalty && !map_flag_gvg2(sd->bl.m)
  6968. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE])
  6969. {
  6970. uint32 base_penalty = 0;
  6971. uint32 job_penalty = 0;
  6972. uint32 zeny_penalty = 0;
  6973. if (pc_isvip(sd)) { // EXP penalty for VIP
  6974. base_penalty = battle_config.vip_exp_penalty_base;
  6975. job_penalty = battle_config.vip_exp_penalty_job;
  6976. zeny_penalty = battle_config.vip_zeny_penalty;
  6977. } else {
  6978. base_penalty = battle_config.death_penalty_base;
  6979. job_penalty = battle_config.death_penalty_job;
  6980. zeny_penalty = battle_config.zeny_penalty;
  6981. }
  6982. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  6983. switch (battle_config.death_penalty_type) {
  6984. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  6985. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  6986. }
  6987. if (base_penalty){ //recheck after altering to speedup
  6988. if (battle_config.pk_mode && src && src->type==BL_PC)
  6989. base_penalty *= 2;
  6990. base_penalty = u32min(sd->status.base_exp, base_penalty);
  6991. }
  6992. }
  6993. else
  6994. base_penalty = 0;
  6995. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  6996. switch (battle_config.death_penalty_type) {
  6997. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  6998. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  6999. }
  7000. if (job_penalty) {
  7001. if (battle_config.pk_mode && src && src->type==BL_PC)
  7002. job_penalty *= 2;
  7003. job_penalty = u32min(sd->status.job_exp, job_penalty);
  7004. }
  7005. }
  7006. else
  7007. job_penalty = 0;
  7008. if (base_penalty || job_penalty)
  7009. pc_lostexp(sd, base_penalty, job_penalty);
  7010. if( zeny_penalty > 0 && !map[sd->bl.m].flag.nozenypenalty) {
  7011. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7012. if(zeny_penalty)
  7013. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7014. }
  7015. }
  7016. if(map[sd->bl.m].flag.pvp_nightmaredrop) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7017. int j;
  7018. for(j=0;j<MAX_DROP_PER_MAP;j++){
  7019. int id = map[sd->bl.m].drop_list[j].drop_id;
  7020. int type = map[sd->bl.m].drop_list[j].drop_type;
  7021. int per = map[sd->bl.m].drop_list[j].drop_per;
  7022. if(id == 0)
  7023. continue;
  7024. if(id == -1){
  7025. int eq_num=0,eq_n[MAX_INVENTORY];
  7026. memset(eq_n,0,sizeof(eq_n));
  7027. for(i=0;i<MAX_INVENTORY;i++) {
  7028. if( (type == 1 && !sd->inventory.u.items_inventory[i].equip)
  7029. || (type == 2 && sd->inventory.u.items_inventory[i].equip)
  7030. || type == 3)
  7031. {
  7032. int l;
  7033. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7034. if( l < MAX_INVENTORY )
  7035. eq_n[l] = i;
  7036. eq_num++;
  7037. }
  7038. }
  7039. if(eq_num > 0){
  7040. int n = eq_n[rnd()%eq_num];
  7041. if(rnd()%10000 < per) {
  7042. if(sd->inventory.u.items_inventory[n].equip)
  7043. pc_unequipitem(sd,n,3);
  7044. pc_dropitem(sd,n,1);
  7045. }
  7046. }
  7047. }
  7048. else if(id > 0) {
  7049. for(i=0;i<MAX_INVENTORY;i++){
  7050. if(sd->inventory.u.items_inventory[i].nameid == id
  7051. && rnd()%10000 < per
  7052. && ((type == 1 && !sd->inventory.u.items_inventory[i].equip)
  7053. || (type == 2 && sd->inventory.u.items_inventory[i].equip)
  7054. || type == 3) ){
  7055. if(sd->inventory.u.items_inventory[i].equip)
  7056. pc_unequipitem(sd,i,3);
  7057. pc_dropitem(sd,i,1);
  7058. break;
  7059. }
  7060. }
  7061. }
  7062. }
  7063. }
  7064. // pvp
  7065. // disable certain pvp functions on pk_mode [Valaris]
  7066. if( map[sd->bl.m].flag.pvp && !battle_config.pk_mode && !map[sd->bl.m].flag.pvp_nocalcrank ) {
  7067. sd->pvp_point -= 5;
  7068. sd->pvp_lost++;
  7069. if( src && src->type == BL_PC ) {
  7070. struct map_session_data *ssd = (struct map_session_data *)src;
  7071. ssd->pvp_point++;
  7072. ssd->pvp_won++;
  7073. }
  7074. if( sd->pvp_point < 0 ) {
  7075. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7076. return 1|8;
  7077. }
  7078. }
  7079. //GvG
  7080. if( map_flag_gvg2(sd->bl.m) ) {
  7081. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7082. return 1|8;
  7083. }
  7084. else if( sd->bg_id ) {
  7085. struct battleground_data *bg = bg_team_search(sd->bg_id);
  7086. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  7087. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7088. return 1|8;
  7089. }
  7090. }
  7091. //Reset "can log out" tick.
  7092. if( battle_config.prevent_logout )
  7093. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7094. return 1;
  7095. }
  7096. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7097. if(hp) clif_updatestatus(sd,SP_HP);
  7098. if(sp) clif_updatestatus(sd,SP_SP);
  7099. pc_setstand(sd, true);
  7100. if(battle_config.pc_invincible_time > 0)
  7101. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7102. if( sd->state.gmaster_flag ) {
  7103. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7104. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7105. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7106. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7107. }
  7108. }
  7109. // script
  7110. //
  7111. /*==========================================
  7112. * script reading pc status registry
  7113. *------------------------------------------*/
  7114. int pc_readparam(struct map_session_data* sd,int type)
  7115. {
  7116. int val = 0;
  7117. nullpo_ret(sd);
  7118. switch(type) {
  7119. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7120. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7121. case SP_ZENY: val = sd->status.zeny; break;
  7122. case SP_BASELEVEL: val = sd->status.base_level; break;
  7123. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7124. case SP_CLASS: val = sd->status.class_; break;
  7125. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7126. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7127. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7128. case SP_SEX: val = sd->status.sex; break;
  7129. case SP_WEIGHT: val = sd->weight; break;
  7130. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7131. case SP_BASEEXP: val = sd->status.base_exp; break;
  7132. case SP_JOBEXP: val = sd->status.job_exp; break;
  7133. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7134. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7135. case SP_HP: val = sd->battle_status.hp; break;
  7136. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7137. case SP_SP: val = sd->battle_status.sp; break;
  7138. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7139. case SP_STR: val = sd->status.str; break;
  7140. case SP_AGI: val = sd->status.agi; break;
  7141. case SP_VIT: val = sd->status.vit; break;
  7142. case SP_INT: val = sd->status.int_; break;
  7143. case SP_DEX: val = sd->status.dex; break;
  7144. case SP_LUK: val = sd->status.luk; break;
  7145. case SP_KARMA: val = sd->status.karma; break;
  7146. case SP_MANNER: val = sd->status.manner; break;
  7147. case SP_FAME: val = sd->status.fame; break;
  7148. case SP_KILLERRID: val = sd->killerrid; break;
  7149. case SP_KILLEDRID: val = sd->killedrid; break;
  7150. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7151. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7152. case SP_CHARRENAME: val = sd->status.rename; break;
  7153. case SP_CHARFONT: val = sd->status.font; break;
  7154. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7155. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7156. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7157. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7158. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7159. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7160. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  7161. case SP_DEF1: val = sd->battle_status.def; break;
  7162. case SP_DEF2: val = sd->battle_status.def2; break;
  7163. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7164. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7165. case SP_HIT: val = sd->battle_status.hit; break;
  7166. case SP_FLEE1: val = sd->battle_status.flee; break;
  7167. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7168. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7169. case SP_MAXHPRATE: val = sd->hprate; break;
  7170. case SP_MAXSPRATE: val = sd->sprate; break;
  7171. case SP_SPRATE: val = sd->dsprate; break;
  7172. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7173. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7174. case SP_ASPD_RATE:
  7175. #ifndef RENEWAL_ASPD
  7176. val = sd->battle_status.aspd_rate;
  7177. #else
  7178. val = sd->battle_status.aspd_rate2;
  7179. #endif
  7180. break;
  7181. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7182. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7183. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7184. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7185. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7186. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7187. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7188. case SP_MATK_RATE: val = sd->matk_rate; break;
  7189. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7190. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7191. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7192. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7193. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7194. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7195. case SP_HIT_RATE: val = sd->hit_rate; break;
  7196. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7197. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7198. case SP_DEF_RATE: val = sd->def_rate; break;
  7199. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7200. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7201. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7202. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7203. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7204. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7205. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7206. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7207. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7208. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7209. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7210. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7211. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7212. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7213. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7214. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7215. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7216. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7217. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7218. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7219. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7220. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7221. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7222. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7223. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7224. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7225. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7226. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7227. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7228. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7229. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7230. case SP_DELAYRATE: val = sd->delayrate; break;
  7231. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7232. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7233. case SP_UNSTRIPABLE:
  7234. case SP_UNSTRIPABLE_ARMOR:
  7235. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7236. break;
  7237. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7238. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7239. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7240. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7241. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7242. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7243. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7244. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7245. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7246. case SP_EMATK: val = sd->bonus.ematk; break;
  7247. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7248. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7249. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7250. case SP_CASTRATE:
  7251. case SP_VARCASTRATE:
  7252. #ifdef RENEWAL_CAST
  7253. val = sd->bonus.varcastrate; break;
  7254. #else
  7255. val = sd->castrate; break;
  7256. #endif
  7257. default:
  7258. ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
  7259. return -1;
  7260. }
  7261. return val;
  7262. }
  7263. /*==========================================
  7264. * script set pc status registry
  7265. *------------------------------------------*/
  7266. bool pc_setparam(struct map_session_data *sd,int type,int val)
  7267. {
  7268. nullpo_retr(false,sd);
  7269. switch(type){
  7270. case SP_BASELEVEL:
  7271. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  7272. val = pc_maxbaselv(sd);
  7273. if ((unsigned int)val > sd->status.base_level) {
  7274. int i = 0;
  7275. int stat=0;
  7276. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  7277. stat += pc_gets_status_point(sd->status.base_level + i);
  7278. sd->status.status_point += stat;
  7279. }
  7280. sd->status.base_level = (unsigned int)val;
  7281. sd->status.base_exp = 0;
  7282. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7283. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7284. clif_updatestatus(sd, SP_STATUSPOINT);
  7285. clif_updatestatus(sd, SP_BASEEXP);
  7286. status_calc_pc(sd, SCO_FORCE);
  7287. if(sd->status.party_id)
  7288. party_send_levelup(sd);
  7289. break;
  7290. case SP_JOBLEVEL:
  7291. if ((unsigned int)val >= sd->status.job_level) {
  7292. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7293. sd->status.skill_point += val - sd->status.job_level;
  7294. clif_updatestatus(sd, SP_SKILLPOINT);
  7295. }
  7296. sd->status.job_level = (unsigned int)val;
  7297. sd->status.job_exp = 0;
  7298. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7299. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7300. clif_updatestatus(sd, SP_JOBEXP);
  7301. status_calc_pc(sd, SCO_FORCE);
  7302. break;
  7303. case SP_SKILLPOINT:
  7304. sd->status.skill_point = val;
  7305. break;
  7306. case SP_STATUSPOINT:
  7307. sd->status.status_point = val;
  7308. break;
  7309. case SP_ZENY:
  7310. if( val < 0 )
  7311. return false;// can't set negative zeny
  7312. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7313. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7314. break;
  7315. case SP_BASEEXP:
  7316. {
  7317. val = cap_value(val, 0, INT_MAX);
  7318. if ((unsigned int)val < sd->status.base_exp) // Lost
  7319. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7320. else // Gained
  7321. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7322. }
  7323. return true;
  7324. case SP_JOBEXP:
  7325. {
  7326. val = cap_value(val, 0, INT_MAX);
  7327. if ((unsigned int)val < sd->status.job_exp) // Lost
  7328. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7329. else // Gained
  7330. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7331. }
  7332. return true;
  7333. case SP_SEX:
  7334. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7335. break;
  7336. case SP_WEIGHT:
  7337. sd->weight = val;
  7338. break;
  7339. case SP_MAXWEIGHT:
  7340. sd->max_weight = val;
  7341. break;
  7342. case SP_HP:
  7343. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7344. break;
  7345. case SP_MAXHP:
  7346. if (sd->status.base_level < 100)
  7347. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7348. else if (sd->status.base_level < 151)
  7349. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7350. else
  7351. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7352. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7353. {
  7354. sd->battle_status.hp = sd->battle_status.max_hp;
  7355. clif_updatestatus(sd, SP_HP);
  7356. }
  7357. break;
  7358. case SP_SP:
  7359. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7360. break;
  7361. case SP_MAXSP:
  7362. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7363. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7364. {
  7365. sd->battle_status.sp = sd->battle_status.max_sp;
  7366. clif_updatestatus(sd, SP_SP);
  7367. }
  7368. break;
  7369. case SP_STR:
  7370. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7371. break;
  7372. case SP_AGI:
  7373. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7374. break;
  7375. case SP_VIT:
  7376. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7377. break;
  7378. case SP_INT:
  7379. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7380. break;
  7381. case SP_DEX:
  7382. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7383. break;
  7384. case SP_LUK:
  7385. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7386. break;
  7387. case SP_KARMA:
  7388. sd->status.karma = val;
  7389. break;
  7390. case SP_MANNER:
  7391. sd->status.manner = val;
  7392. if( val < 0 )
  7393. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7394. else {
  7395. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7396. clif_manner_message(sd, 5);
  7397. }
  7398. return true; // status_change_start/status_change_end already sends packets warning the client
  7399. case SP_FAME:
  7400. sd->status.fame = val;
  7401. break;
  7402. case SP_KILLERRID:
  7403. sd->killerrid = val;
  7404. return true;
  7405. case SP_KILLEDRID:
  7406. sd->killedrid = val;
  7407. return true;
  7408. case SP_CHARMOVE:
  7409. sd->status.character_moves = val;
  7410. return true;
  7411. case SP_CHARRENAME:
  7412. sd->status.rename = val;
  7413. return true;
  7414. case SP_CHARFONT:
  7415. sd->status.font = val;
  7416. clif_font(sd);
  7417. return true;
  7418. case SP_BANK_VAULT:
  7419. if (val < 0)
  7420. return false;
  7421. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7422. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7423. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7424. return true;
  7425. case SP_ROULETTE_BRONZE:
  7426. sd->roulette_point.bronze = val;
  7427. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7428. return true;
  7429. case SP_ROULETTE_SILVER:
  7430. sd->roulette_point.silver = val;
  7431. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7432. return true;
  7433. case SP_ROULETTE_GOLD:
  7434. sd->roulette_point.gold = val;
  7435. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7436. return true;
  7437. default:
  7438. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7439. return false;
  7440. }
  7441. clif_updatestatus(sd,type);
  7442. return true;
  7443. }
  7444. /*==========================================
  7445. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7446. *------------------------------------------*/
  7447. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7448. {
  7449. if (type&2) {
  7450. if (hp || type&4)
  7451. clif_heal(sd->fd,SP_HP,hp);
  7452. if (sp)
  7453. clif_heal(sd->fd,SP_SP,sp);
  7454. } else {
  7455. if(hp)
  7456. clif_updatestatus(sd,SP_HP);
  7457. if(sp)
  7458. clif_updatestatus(sd,SP_SP);
  7459. }
  7460. return;
  7461. }
  7462. /**
  7463. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7464. * @param sd: Player data
  7465. * @param itemid: Item ID
  7466. * @param hp: HP to heal
  7467. * @param sp: SP to heal
  7468. * @return Amount healed to an object
  7469. */
  7470. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7471. {
  7472. int bonus, tmp, penalty = 0;
  7473. if (hp) {
  7474. int i;
  7475. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7476. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7477. if (potion_flag == 2) {
  7478. bonus += 50;
  7479. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7480. bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
  7481. }
  7482. //All item bonuses.
  7483. bonus += sd->bonus.itemhealrate2;
  7484. //Item Group bonuses
  7485. bonus += pc_get_itemgroup_bonus(sd, itemid);
  7486. //Individual item bonuses.
  7487. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++) {
  7488. if (sd->itemhealrate[i].nameid == itemid) {
  7489. bonus += sd->itemhealrate[i].rate;
  7490. break;
  7491. }
  7492. }
  7493. // Recovery Potion
  7494. if (sd->sc.data[SC_INCHEALRATE])
  7495. bonus += sd->sc.data[SC_INCHEALRATE]->val1;
  7496. // 2014 Halloween Event : Pumpkin Bonus
  7497. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7498. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
  7499. tmp = hp * bonus / 100; // Overflow check
  7500. if (bonus != 100 && tmp > hp)
  7501. hp = tmp;
  7502. }
  7503. if (sp) {
  7504. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7505. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7506. if (potion_flag == 2)
  7507. bonus += 50;
  7508. tmp = sp * bonus / 100; // Overflow check
  7509. if (bonus != 100 && tmp > sp)
  7510. sp = tmp;
  7511. }
  7512. if (sd->sc.count) {
  7513. // Critical Wound and Death Hurt stack
  7514. if (sd->sc.data[SC_CRITICALWOUND])
  7515. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7516. if (sd->sc.data[SC_DEATHHURT])
  7517. penalty += 20;
  7518. if (sd->sc.data[SC_NORECOVER_STATE])
  7519. penalty = 100;
  7520. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7521. hp += hp / 2; // 1.5 times
  7522. sp -= sp / 2;
  7523. }
  7524. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7525. hp += hp / 10;
  7526. sp += sp / 10;
  7527. }
  7528. if (penalty > 0) {
  7529. hp -= hp * penalty / 100;
  7530. sp -= sp * penalty / 100;
  7531. }
  7532. #ifdef RENEWAL
  7533. if (sd->sc.data[SC_EXTREMITYFIST2])
  7534. sp = 0;
  7535. #endif
  7536. if (sd->sc.data[SC_BITESCAR])
  7537. hp = 0;
  7538. }
  7539. return status_heal(&sd->bl, hp, sp, 1);
  7540. }
  7541. /*==========================================
  7542. * HP/SP Recovery
  7543. * Heal player hp nad/or sp by rate
  7544. *------------------------------------------*/
  7545. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7546. {
  7547. nullpo_ret(sd);
  7548. if (hp > 100) hp = 100;
  7549. else if (hp <-100) hp = -100;
  7550. if (sp > 100) sp = 100;
  7551. else if (sp <-100) sp = -100;
  7552. if(hp >= 0 && sp >= 0) //Heal
  7553. return status_percent_heal(&sd->bl, hp, sp);
  7554. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7555. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7556. //Crossed signs
  7557. if(hp) {
  7558. if(hp > 0)
  7559. status_percent_heal(&sd->bl, hp, 0);
  7560. else
  7561. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7562. }
  7563. if(sp) {
  7564. if(sp > 0)
  7565. status_percent_heal(&sd->bl, 0, sp);
  7566. else
  7567. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7568. }
  7569. return 0;
  7570. }
  7571. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7572. {
  7573. struct mob_data *md;
  7574. int flag;
  7575. md = (struct mob_data *)bl;
  7576. nullpo_ret(md);
  7577. flag = va_arg(ap, int);
  7578. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7579. status_kill(&md->bl);
  7580. return 1;
  7581. }
  7582. /**
  7583. * Called when player changes job
  7584. * Rewrote to make it tidider [Celest]
  7585. * @param sd
  7586. * @param job JOB ID. See enum e_job
  7587. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7588. * @return True if success, false if failed
  7589. **/
  7590. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7591. {
  7592. int i, fame_flag = 0;
  7593. int b_class;
  7594. nullpo_retr(false,sd);
  7595. if (job < 0)
  7596. return false;
  7597. //Normalize job.
  7598. b_class = pc_jobid2mapid(job);
  7599. if (b_class == -1)
  7600. return false;
  7601. switch (upper) {
  7602. case 1:
  7603. b_class|= JOBL_UPPER;
  7604. break;
  7605. case 2:
  7606. b_class|= JOBL_BABY;
  7607. break;
  7608. }
  7609. //This will automatically adjust bard/dancer classes to the correct gender
  7610. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7611. job = pc_mapid2jobid(b_class, sd->status.sex);
  7612. if (job == -1)
  7613. return false;
  7614. if ((unsigned short)b_class == sd->class_)
  7615. return false; //Nothing to change.
  7616. // changing from 1st to 2nd job
  7617. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7618. sd->change_level_2nd = sd->status.job_level;
  7619. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  7620. }
  7621. // changing from 2nd to 3rd job
  7622. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7623. sd->change_level_3rd = sd->status.job_level;
  7624. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  7625. }
  7626. if(sd->cloneskill_idx > 0) {
  7627. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7628. sd->status.skill[sd->cloneskill_idx].id = 0;
  7629. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7630. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7631. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  7632. }
  7633. sd->cloneskill_idx = 0;
  7634. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7635. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7636. }
  7637. if(sd->reproduceskill_idx > 0) {
  7638. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7639. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7640. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7641. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7642. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  7643. }
  7644. sd->reproduceskill_idx = 0;
  7645. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7646. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7647. }
  7648. // Give or reduce transcendent status points
  7649. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7650. sd->status.status_point += battle_config.transcendent_status_points;
  7651. clif_updatestatus(sd,SP_STATUSPOINT);
  7652. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7653. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7654. // The player already used his bonus points, so we have to reset his status points
  7655. pc_resetstate(sd);
  7656. }
  7657. sd->status.status_point -= battle_config.transcendent_status_points;
  7658. clif_updatestatus(sd,SP_STATUSPOINT);
  7659. }
  7660. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7661. const int class_ = pc_class2idx(sd->status.class_);
  7662. uint16 skill_id;
  7663. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7664. //Remove status specific to your current tree skills.
  7665. enum sc_type sc = status_skill2sc(skill_id);
  7666. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7667. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7668. }
  7669. }
  7670. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7671. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7672. pc_resetfeel(sd);
  7673. }
  7674. // Reset body style to 0 before changing job to avoid
  7675. // errors since not every job has a alternate outfit.
  7676. sd->status.body = 0;
  7677. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7678. sd->status.class_ = job;
  7679. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7680. sd->class_ = (unsigned short)b_class;
  7681. sd->status.job_level=1;
  7682. sd->status.job_exp=0;
  7683. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7684. sd->status.base_level = pc_maxbaselv(sd);
  7685. sd->status.base_exp=0;
  7686. pc_resetstate(sd);
  7687. clif_updatestatus(sd,SP_STATUSPOINT);
  7688. clif_updatestatus(sd,SP_BASELEVEL);
  7689. clif_updatestatus(sd,SP_BASEEXP);
  7690. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7691. }
  7692. clif_updatestatus(sd,SP_JOBLEVEL);
  7693. clif_updatestatus(sd,SP_JOBEXP);
  7694. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7695. for(i=0;i<EQI_MAX;i++) {
  7696. if(sd->equip_index[i] >= 0)
  7697. if(pc_isequip(sd,sd->equip_index[i]))
  7698. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7699. }
  7700. //Change look, if disguised, you need to undisguise
  7701. //to correctly calculate new job sprite without
  7702. if (sd->disguise)
  7703. pc_disguise(sd, 0);
  7704. status_set_viewdata(&sd->bl, job);
  7705. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7706. #if PACKETVER >= 20151001
  7707. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7708. #endif
  7709. if(sd->vd.cloth_color)
  7710. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7711. /*
  7712. if(sd->vd.body_style)
  7713. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7714. */
  7715. //Update skill tree.
  7716. pc_calc_skilltree(sd);
  7717. clif_skillinfoblock(sd);
  7718. if (sd->ed)
  7719. elemental_delete(sd->ed);
  7720. if (sd->state.vending)
  7721. vending_closevending(sd);
  7722. if (sd->state.buyingstore)
  7723. buyingstore_close(sd);
  7724. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7725. //Remove peco/cart/falcon
  7726. i = sd->sc.option;
  7727. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7728. i&=~OPTION_RIDING;
  7729. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7730. i&=~OPTION_FALCON;
  7731. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7732. i&=~OPTION_DRAGON;
  7733. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7734. i&=~OPTION_WUGRIDER;
  7735. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7736. i&=~OPTION_WUG;
  7737. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7738. i&=~OPTION_MADOGEAR;
  7739. #ifndef NEW_CARTS
  7740. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7741. i&=~OPTION_CART;
  7742. #else
  7743. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7744. pc_setcart(sd, 0);
  7745. #endif
  7746. if(i != sd->sc.option)
  7747. pc_setoption(sd, i);
  7748. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7749. hom_vaporize(sd, HOM_ST_ACTIVE);
  7750. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7751. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7752. if(sd->status.manner < 0)
  7753. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7754. status_calc_pc(sd,SCO_FORCE);
  7755. pc_checkallowskill(sd);
  7756. pc_equiplookall(sd);
  7757. pc_show_questinfo(sd);
  7758. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  7759. if( sd->status.party_id ){
  7760. struct party_data* p;
  7761. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  7762. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  7763. if( i < MAX_PARTY ){
  7764. p->party.member[i].class_ = sd->status.class_;
  7765. clif_party_job_and_level(sd);
  7766. }
  7767. }
  7768. }
  7769. chrif_save(sd, CSAVE_NORMAL);
  7770. //if you were previously famous, not anymore.
  7771. if (fame_flag)
  7772. chrif_buildfamelist();
  7773. else if (sd->status.fame > 0) {
  7774. //It may be that now they are famous?
  7775. switch (sd->class_&MAPID_UPPERMASK) {
  7776. case MAPID_BLACKSMITH:
  7777. case MAPID_ALCHEMIST:
  7778. case MAPID_TAEKWON:
  7779. chrif_buildfamelist();
  7780. break;
  7781. }
  7782. }
  7783. return true;
  7784. }
  7785. /*==========================================
  7786. * Tell client player sd has change equipement
  7787. *------------------------------------------*/
  7788. void pc_equiplookall(struct map_session_data *sd)
  7789. {
  7790. nullpo_retv(sd);
  7791. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  7792. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7793. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7794. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7795. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7796. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  7797. }
  7798. /*==========================================
  7799. * Tell client player sd has change look (hair,equip...)
  7800. *------------------------------------------*/
  7801. void pc_changelook(struct map_session_data *sd,int type,int val) {
  7802. nullpo_retv(sd);
  7803. switch(type) {
  7804. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  7805. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  7806. if (sd->status.hair != val) {
  7807. sd->status.hair = val;
  7808. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7809. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7810. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  7811. }
  7812. break;
  7813. case LOOK_WEAPON:
  7814. sd->status.weapon = val;
  7815. break;
  7816. case LOOK_HEAD_BOTTOM:
  7817. sd->status.head_bottom = val;
  7818. sd->setlook_head_bottom = val;
  7819. break;
  7820. case LOOK_HEAD_TOP:
  7821. sd->status.head_top = val;
  7822. sd->setlook_head_top = val;
  7823. break;
  7824. case LOOK_HEAD_MID:
  7825. sd->status.head_mid = val;
  7826. sd->setlook_head_mid = val;
  7827. break;
  7828. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  7829. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  7830. if (sd->status.hair_color != val) {
  7831. sd->status.hair_color = val;
  7832. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7833. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7834. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  7835. }
  7836. break;
  7837. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  7838. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  7839. sd->status.clothes_color = val;
  7840. break;
  7841. case LOOK_SHIELD:
  7842. sd->status.shield = val;
  7843. break;
  7844. case LOOK_SHOES:
  7845. break;
  7846. case LOOK_ROBE:
  7847. sd->status.robe = val;
  7848. sd->setlook_robe = val;
  7849. break;
  7850. case LOOK_BODY2:
  7851. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  7852. sd->status.body = val;
  7853. break;
  7854. }
  7855. clif_changelook(&sd->bl, type, val);
  7856. }
  7857. /*==========================================
  7858. * Give an option (type) to player (sd) and display it to client
  7859. *------------------------------------------*/
  7860. void pc_setoption(struct map_session_data *sd,int type)
  7861. {
  7862. int p_type, new_look=0;
  7863. nullpo_retv(sd);
  7864. p_type = sd->sc.option;
  7865. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  7866. sd->sc.option=type;
  7867. clif_changeoption(&sd->bl);
  7868. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7869. { // Mounting
  7870. clif_status_load(&sd->bl,EFST_RIDING,1);
  7871. status_calc_pc(sd,SCO_NONE);
  7872. }
  7873. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7874. { // Dismount
  7875. clif_status_load(&sd->bl,EFST_RIDING,0);
  7876. status_calc_pc(sd,SCO_NONE);
  7877. }
  7878. #ifndef NEW_CARTS
  7879. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  7880. clif_cartlist(sd);
  7881. clif_updatestatus(sd, SP_CARTINFO);
  7882. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7883. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  7884. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  7885. clif_clearcart(sd->fd);
  7886. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7887. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  7888. }
  7889. #endif
  7890. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  7891. clif_status_load(&sd->bl,EFST_FALCON,1);
  7892. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  7893. clif_status_load(&sd->bl,EFST_FALCON,0);
  7894. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  7895. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  7896. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  7897. status_calc_pc(sd,SCO_NONE);
  7898. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  7899. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  7900. status_calc_pc(sd,SCO_NONE);
  7901. }
  7902. }
  7903. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  7904. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  7905. status_calc_pc(sd,SCO_NONE);
  7906. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  7907. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  7908. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  7909. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  7910. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  7911. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  7912. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  7913. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7914. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  7915. status_calc_pc(sd,SCO_NONE);
  7916. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  7917. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  7918. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  7919. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  7920. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  7921. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  7922. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  7923. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  7924. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7925. }
  7926. }
  7927. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  7928. new_look = JOB_STAR_GLADIATOR2;
  7929. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  7930. new_look = -1;
  7931. if (sd->disguise || !new_look)
  7932. return; //Disguises break sprite changes
  7933. if (new_look < 0) { //Restore normal look.
  7934. status_set_viewdata(&sd->bl, sd->status.class_);
  7935. new_look = sd->vd.class_;
  7936. }
  7937. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  7938. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  7939. if (sd->vd.cloth_color)
  7940. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7941. if( sd->vd.body_style )
  7942. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7943. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  7944. }
  7945. /**
  7946. * Give player a cart
  7947. * @param sd Player
  7948. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  7949. **/
  7950. bool pc_setcart(struct map_session_data *sd,int type) {
  7951. #ifndef NEW_CARTS
  7952. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  7953. int option;
  7954. #endif
  7955. nullpo_retr(false,sd);
  7956. if( type < 0 || type > MAX_CARTS )
  7957. return false;// Never trust the values sent by the client! [Skotlex]
  7958. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  7959. return false;// Push cart is required
  7960. #ifdef NEW_CARTS
  7961. switch( type ) {
  7962. case 0:
  7963. if( !sd->sc.data[SC_PUSH_CART] )
  7964. return 0;
  7965. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  7966. clif_clearcart(sd->fd);
  7967. break;
  7968. default:/* everything else is an allowed ID so we can move on */
  7969. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  7970. clif_cartlist(sd);
  7971. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  7972. }
  7973. clif_updatestatus(sd, SP_CARTINFO);
  7974. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  7975. break;
  7976. }
  7977. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7978. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  7979. #else
  7980. // Update option
  7981. option = sd->sc.option;
  7982. option &= ~OPTION_CART;// clear cart bits
  7983. option |= cart[type]; // set cart
  7984. pc_setoption(sd, option);
  7985. #endif
  7986. return true;
  7987. }
  7988. /*==========================================
  7989. * Give player a falcon
  7990. *------------------------------------------*/
  7991. void pc_setfalcon(struct map_session_data* sd, int flag)
  7992. {
  7993. if( flag ){
  7994. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  7995. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  7996. } else if( pc_isfalcon(sd) ){
  7997. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  7998. }
  7999. }
  8000. /*==========================================
  8001. * Set player riding
  8002. *------------------------------------------*/
  8003. void pc_setriding(struct map_session_data* sd, int flag)
  8004. {
  8005. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  8006. return;
  8007. if( flag ){
  8008. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8009. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8010. } else if( pc_isriding(sd) ){
  8011. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8012. }
  8013. }
  8014. /*==========================================
  8015. * Give player a mado
  8016. *------------------------------------------*/
  8017. void pc_setmadogear(struct map_session_data* sd, int flag)
  8018. {
  8019. if( flag ){
  8020. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8021. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8022. } else if( pc_ismadogear(sd) ){
  8023. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8024. }
  8025. }
  8026. /*==========================================
  8027. * Check if player can drop an item
  8028. *------------------------------------------*/
  8029. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8030. {
  8031. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  8032. return false;
  8033. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8034. return false;
  8035. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8036. }
  8037. /**
  8038. * Determines whether a player can attack based on status changes
  8039. * Why not use status_check_skilluse?
  8040. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8041. * Even ground-based attacks should be blocked by these statuses
  8042. * Called from unit_attack and unit_attack_timer_sub
  8043. * @retval true Can attack
  8044. **/
  8045. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8046. nullpo_retr(false, sd);
  8047. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8048. return false;
  8049. if( sd->sc.data[SC_BASILICA] ||
  8050. sd->sc.data[SC__SHADOWFORM] ||
  8051. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8052. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8053. sd->sc.data[SC_CRYSTALIZE] ||
  8054. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8055. sd->sc.data[SC_TRICKDEAD] ||
  8056. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8057. sd->sc.data[SC_BLADESTOP] ||
  8058. sd->sc.data[SC_DEEPSLEEP] ||
  8059. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8060. sd->sc.data[SC_KINGS_GRACE] )
  8061. return false;
  8062. return true;
  8063. }
  8064. /*==========================================
  8065. * Read '@type' variables (temporary numeric char reg)
  8066. *------------------------------------------*/
  8067. int pc_readreg(struct map_session_data* sd, int64 reg)
  8068. {
  8069. return i64db_iget(sd->regs.vars, reg);
  8070. }
  8071. /*==========================================
  8072. * Set '@type' variables (temporary numeric char reg)
  8073. *------------------------------------------*/
  8074. bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
  8075. {
  8076. unsigned int index = script_getvaridx(reg);
  8077. nullpo_retr(false, sd);
  8078. if( val ) {
  8079. i64db_iput(sd->regs.vars, reg, val);
  8080. if( index )
  8081. script_array_update(&sd->regs, reg, false);
  8082. } else {
  8083. i64db_remove(sd->regs.vars, reg);
  8084. if( index )
  8085. script_array_update(&sd->regs, reg, true);
  8086. }
  8087. return true;
  8088. }
  8089. /*==========================================
  8090. * Read '@type$' variables (temporary string char reg)
  8091. *------------------------------------------*/
  8092. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8093. {
  8094. struct script_reg_str *p = NULL;
  8095. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8096. return p ? p->value : NULL;
  8097. }
  8098. /*==========================================
  8099. * Set '@type$' variables (temporary string char reg)
  8100. *------------------------------------------*/
  8101. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8102. {
  8103. struct script_reg_str *p = NULL;
  8104. unsigned int index = script_getvaridx(reg);
  8105. DBData prev;
  8106. nullpo_retr(false, sd);
  8107. if( str[0] ) {
  8108. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8109. p->value = aStrdup(str);
  8110. p->flag.type = 1;
  8111. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8112. p = (struct script_reg_str *)db_data2ptr(&prev);
  8113. if( p->value )
  8114. aFree(p->value);
  8115. ers_free(str_reg_ers, p);
  8116. } else {
  8117. if( index )
  8118. script_array_update(&sd->regs, reg, false);
  8119. }
  8120. } else {
  8121. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8122. p = (struct script_reg_str *)db_data2ptr(&prev);
  8123. if( p->value )
  8124. aFree(p->value);
  8125. ers_free(str_reg_ers, p);
  8126. if( index )
  8127. script_array_update(&sd->regs, reg, true);
  8128. }
  8129. }
  8130. return true;
  8131. }
  8132. /**
  8133. * Serves the following variable types:
  8134. * - 'type' (permanent numeric char reg)
  8135. * - '#type' (permanent numeric account reg)
  8136. * - '##type' (permanent numeric account reg2)
  8137. **/
  8138. int pc_readregistry(struct map_session_data *sd, int64 reg)
  8139. {
  8140. struct script_reg_num *p = NULL;
  8141. if (!sd->vars_ok) {
  8142. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8143. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8144. //intif->request_registry(sd,type==3?4:type);
  8145. set_eof(sd->fd);
  8146. return 0;
  8147. }
  8148. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8149. return p ? p->value : 0;
  8150. }
  8151. /**
  8152. * Serves the following variable types:
  8153. * - 'type$' (permanent str char reg)
  8154. * - '#type$' (permanent str account reg)
  8155. * - '##type$' (permanent str account reg2)
  8156. **/
  8157. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8158. {
  8159. struct script_reg_str *p = NULL;
  8160. if (!sd->vars_ok) {
  8161. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8162. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8163. //intif->request_registry(sd,type==3?4:type);
  8164. set_eof(sd->fd);
  8165. return NULL;
  8166. }
  8167. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8168. return p ? p->value : NULL;
  8169. }
  8170. /**
  8171. * Serves the following variable types:
  8172. * - 'type' (permanent numeric char reg)
  8173. * - '#type' (permanent numeric account reg)
  8174. * - '##type' (permanent numeric account reg2)
  8175. **/
  8176. int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
  8177. {
  8178. struct script_reg_num *p = NULL;
  8179. const char *regname = get_str(script_getvarid(reg));
  8180. unsigned int index = script_getvaridx(reg);
  8181. // These should be stored elsewhere e.g. char ones in char table, the cash ones in account_data table!
  8182. switch( regname[0] ) {
  8183. default: //Char reg
  8184. if( !strcmp(regname,PCDIECOUNTER_VAR) && sd->die_counter != val ) {
  8185. int i = (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE);
  8186. sd->die_counter = val;
  8187. if( i )
  8188. status_calc_pc(sd,SCO_NONE); // Lost the bonus.
  8189. } else if( !strcmp(regname,COOKMASTERY_VAR) && sd->cook_mastery != val ) {
  8190. val = cap_value(val, 0, 1999);
  8191. sd->cook_mastery = val;
  8192. }
  8193. break;
  8194. case '#':
  8195. if( !strcmp(regname,CASHPOINT_VAR) && sd->cashPoints != val ) {
  8196. val = cap_value(val, 0, MAX_ZENY);
  8197. sd->cashPoints = val;
  8198. } else if( !strcmp(regname,KAFRAPOINT_VAR) && sd->kafraPoints != val ) {
  8199. val = cap_value(val, 0, MAX_ZENY);
  8200. sd->kafraPoints = val;
  8201. }
  8202. break;
  8203. }
  8204. if ( !reg_load && !sd->vars_ok ) {
  8205. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8206. return 0;
  8207. }
  8208. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8209. if( val ) {
  8210. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8211. script_array_update(&sd->regs, reg, false);
  8212. p->value = val;
  8213. } else {
  8214. p->value = 0;
  8215. if( index )
  8216. script_array_update(&sd->regs, reg, true);
  8217. }
  8218. if (!reg_load)
  8219. p->flag.update = 1;/* either way, it will require either delete or replace */
  8220. } else if( val ) {
  8221. DBData prev;
  8222. if( index )
  8223. script_array_update(&sd->regs, reg, false);
  8224. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8225. p->value = val;
  8226. if (!reg_load)
  8227. p->flag.update = 1;
  8228. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8229. p = (struct script_reg_num *)db_data2ptr(&prev);
  8230. ers_free(num_reg_ers, p);
  8231. }
  8232. }
  8233. if (!reg_load && p)
  8234. sd->vars_dirty = true;
  8235. return 1;
  8236. }
  8237. /**
  8238. * Serves the following variable types:
  8239. * - 'type$' (permanent str char reg)
  8240. * - '#type$' (permanent str account reg)
  8241. * - '##type$' (permanent str account reg2)
  8242. **/
  8243. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8244. {
  8245. struct script_reg_str *p = NULL;
  8246. const char *regname = get_str(script_getvarid(reg));
  8247. unsigned int index = script_getvaridx(reg);
  8248. size_t vlen = 0;
  8249. if (!reg_load && !sd->vars_ok) {
  8250. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8251. return 0;
  8252. }
  8253. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8254. {
  8255. 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);
  8256. return 0;
  8257. }
  8258. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8259. if( val[0] ) {
  8260. if( p->value )
  8261. aFree(p->value);
  8262. else if ( index ) // an entry that was deleted, so we reset
  8263. script_array_update(&sd->regs, reg, false);
  8264. p->value = aStrdup(val);
  8265. } else {
  8266. if (p->value)
  8267. aFree(p->value);
  8268. p->value = NULL;
  8269. if( index )
  8270. script_array_update(&sd->regs, reg, true);
  8271. }
  8272. if( !reg_load )
  8273. p->flag.update = 1; // either way, it will require either delete or replace
  8274. } else if( val[0] ) {
  8275. DBData prev;
  8276. if( index )
  8277. script_array_update(&sd->regs, reg, false);
  8278. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8279. p->value = aStrdup(val);
  8280. if( !reg_load )
  8281. p->flag.update = 1;
  8282. p->flag.type = 1;
  8283. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8284. p = (struct script_reg_str *)db_data2ptr(&prev);
  8285. if( p->value )
  8286. aFree(p->value);
  8287. ers_free(str_reg_ers, p);
  8288. }
  8289. }
  8290. if( !reg_load && p )
  8291. sd->vars_dirty = true;
  8292. return 1;
  8293. }
  8294. /**
  8295. * Set value of player variable
  8296. * @param sd Player
  8297. * @param reg Variable name
  8298. * @param value
  8299. * @return True if success, false if failed.
  8300. **/
  8301. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  8302. char prefix = reg[0];
  8303. nullpo_retr(false, sd);
  8304. if (reg[strlen(reg)-1] == '$') {
  8305. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8306. return false;
  8307. }
  8308. val = cap_value(val, INT_MIN, INT_MAX);
  8309. switch (prefix) {
  8310. case '.':
  8311. case '\'':
  8312. case '$':
  8313. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8314. return false;
  8315. case '@':
  8316. return pc_setreg(sd, add_str(reg), val);
  8317. case '#':
  8318. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) > 0 : pc_setaccountreg(sd, add_str(reg), val) > 0;
  8319. default:
  8320. return pc_setglobalreg(sd, add_str(reg), val) > 0;
  8321. }
  8322. return false;
  8323. }
  8324. /**
  8325. * Get value of player variable
  8326. * @param sd Player
  8327. * @param reg Variable name
  8328. * @return Variable value or 0 if failed.
  8329. **/
  8330. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  8331. char prefix = reg[0];
  8332. nullpo_ret(sd);
  8333. if (reg[strlen(reg)-1] == '$') {
  8334. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8335. return 0;
  8336. }
  8337. switch (prefix) {
  8338. case '.':
  8339. case '\'':
  8340. case '$':
  8341. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8342. return 0;
  8343. case '@':
  8344. return pc_readreg(sd, add_str(reg));
  8345. case '#':
  8346. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8347. default:
  8348. return pc_readglobalreg(sd, add_str(reg));
  8349. }
  8350. return 0;
  8351. }
  8352. /*==========================================
  8353. * Exec eventtimer for player sd (retrieved from map_session (id))
  8354. *------------------------------------------*/
  8355. static int pc_eventtimer(int tid, unsigned int tick, int id, intptr_t data)
  8356. {
  8357. struct map_session_data *sd=map_id2sd(id);
  8358. char *p = (char *)data;
  8359. int i;
  8360. if(sd==NULL)
  8361. return 0;
  8362. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8363. if( i < MAX_EVENTTIMER )
  8364. {
  8365. sd->eventtimer[i] = INVALID_TIMER;
  8366. sd->eventcount--;
  8367. npc_event(sd,p,0);
  8368. }
  8369. else
  8370. ShowError("pc_eventtimer: no such event timer\n");
  8371. if (p) aFree(p);
  8372. return 0;
  8373. }
  8374. /*==========================================
  8375. * Add eventtimer for player sd ?
  8376. *------------------------------------------*/
  8377. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8378. {
  8379. int i;
  8380. nullpo_retr(false,sd);
  8381. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8382. if( i == MAX_EVENTTIMER )
  8383. return false;
  8384. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8385. sd->eventcount++;
  8386. return true;
  8387. }
  8388. /*==========================================
  8389. * Del eventtimer for player sd ?
  8390. *------------------------------------------*/
  8391. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8392. {
  8393. char* p = NULL;
  8394. int i;
  8395. nullpo_retr(false,sd);
  8396. if (sd->eventcount == 0)
  8397. return false;
  8398. // find the named event timer
  8399. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8400. sd->eventtimer[i] != INVALID_TIMER &&
  8401. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8402. strcmp(p, name) == 0
  8403. );
  8404. if( i == MAX_EVENTTIMER )
  8405. return false; // not found
  8406. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8407. sd->eventtimer[i] = INVALID_TIMER;
  8408. if(sd->eventcount > 0)
  8409. sd->eventcount--;
  8410. aFree(p);
  8411. return true;
  8412. }
  8413. /*==========================================
  8414. * Update eventtimer count for player sd
  8415. *------------------------------------------*/
  8416. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8417. {
  8418. int i;
  8419. nullpo_retv(sd);
  8420. for(i=0;i<MAX_EVENTTIMER;i++)
  8421. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8422. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8423. addtick_timer(sd->eventtimer[i],tick);
  8424. break;
  8425. }
  8426. }
  8427. /*==========================================
  8428. * Remove all eventtimer for player sd
  8429. *------------------------------------------*/
  8430. void pc_cleareventtimer(struct map_session_data *sd)
  8431. {
  8432. int i;
  8433. nullpo_retv(sd);
  8434. if (sd->eventcount == 0)
  8435. return;
  8436. for(i=0;i<MAX_EVENTTIMER;i++){
  8437. if( sd->eventtimer[i] != INVALID_TIMER ){
  8438. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8439. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8440. sd->eventtimer[i] = INVALID_TIMER;
  8441. if(sd->eventcount > 0) //avoid looping to max val
  8442. sd->eventcount--;
  8443. if (p) aFree(p);
  8444. }
  8445. }
  8446. }
  8447. /**
  8448. * Called when an item with combo is worn
  8449. * @param *sd
  8450. * @param *data struct item_data
  8451. * @return success numbers of succeed combo
  8452. */
  8453. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8454. uint16 i;
  8455. int success = 0;
  8456. for( i = 0; i < data->combos_count; i++ ) {
  8457. struct itemchk {
  8458. int idx;
  8459. unsigned short nameid;
  8460. short card[MAX_SLOTS];
  8461. } *combo_idx;
  8462. int idx, j;
  8463. int nb_itemCombo;
  8464. unsigned int pos = 0;
  8465. /* ensure this isn't a duplicate combo */
  8466. if( sd->combos.bonus != NULL ) {
  8467. int x;
  8468. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8469. /* found a match, skip this combo */
  8470. if( x < sd->combos.count )
  8471. continue;
  8472. }
  8473. nb_itemCombo = data->combos[i]->count;
  8474. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8475. continue;
  8476. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8477. for(j=0; j < nb_itemCombo; j++){
  8478. combo_idx[j].idx=-1;
  8479. combo_idx[j].nameid=-1;
  8480. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8481. }
  8482. for( j = 0; j < nb_itemCombo; j++ ) {
  8483. uint16 id = data->combos[i]->nameid[j], k;
  8484. bool found = false;
  8485. for( k = 0; k < EQI_MAX; k++ ) {
  8486. short index = sd->equip_index[k];
  8487. if( index < 0 )
  8488. continue;
  8489. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8490. continue;
  8491. if (!sd->inventory_data[index] )
  8492. continue;
  8493. if ( itemdb_type(id) != IT_CARD ) {
  8494. if ( sd->inventory_data[index]->nameid != id )
  8495. continue;
  8496. if(j>0){ //check if this item not already used
  8497. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8498. uint8 z;
  8499. for (z = 0; z < nb_itemCombo-1; z++)
  8500. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8501. do_continue=true;
  8502. if(do_continue)
  8503. continue;
  8504. }
  8505. combo_idx[j].nameid = id;
  8506. combo_idx[j].idx = index;
  8507. pos |= sd->inventory.u.items_inventory[index].equip;
  8508. found = true;
  8509. break;
  8510. } else { //Cards and enchants
  8511. uint16 z;
  8512. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8513. continue;
  8514. for (z = 0; z < MAX_SLOTS; z++) {
  8515. bool do_continue=false;
  8516. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8517. continue;
  8518. if(j>0){
  8519. int c1, c2;
  8520. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8521. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8522. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8523. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8524. do_continue = true;
  8525. break;
  8526. }
  8527. }
  8528. }
  8529. }
  8530. }
  8531. if(do_continue)
  8532. continue;
  8533. combo_idx[j].nameid = id;
  8534. combo_idx[j].idx = index;
  8535. combo_idx[j].card[z] = id;
  8536. pos |= sd->inventory.u.items_inventory[index].equip;
  8537. found = true;
  8538. break;
  8539. }
  8540. }
  8541. }
  8542. if( !found )
  8543. break;/* we haven't found all the ids for this combo, so we can return */
  8544. }
  8545. aFree(combo_idx);
  8546. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8547. if( j < nb_itemCombo )
  8548. continue;
  8549. /* we got here, means all items in the combo are matching */
  8550. idx = sd->combos.count;
  8551. if( sd->combos.bonus == NULL ) {
  8552. CREATE(sd->combos.bonus, struct script_code *, 1);
  8553. CREATE(sd->combos.id, unsigned short, 1);
  8554. CREATE(sd->combos.pos, unsigned int, 1);
  8555. sd->combos.count = 1;
  8556. } else {
  8557. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8558. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8559. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8560. }
  8561. /* we simply copy the pointer */
  8562. sd->combos.bonus[idx] = data->combos[i]->script;
  8563. /* save this combo's id */
  8564. sd->combos.id[idx] = data->combos[i]->id;
  8565. /* save pos of combo*/
  8566. sd->combos.pos[idx] = pos;
  8567. success++;
  8568. }
  8569. return success;
  8570. }
  8571. /**
  8572. * Called when an item with combo is removed
  8573. * @param *sd
  8574. * @param *data struct item_data
  8575. * @return retval numbers of removed combo
  8576. */
  8577. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8578. int i, retval = 0;
  8579. if( sd->combos.bonus == NULL )
  8580. return 0;/* nothing to do here, player has no combos */
  8581. for( i = 0; i < data->combos_count; i++ ) {
  8582. /* check if this combo exists in this user */
  8583. int x = 0, cursor = 0, j;
  8584. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8585. /* no match, skip this combo */
  8586. if(x >= sd->combos.count)
  8587. continue;
  8588. sd->combos.bonus[x] = NULL;
  8589. sd->combos.id[x] = 0;
  8590. sd->combos.pos[x] = 0;
  8591. retval++;
  8592. /* check if combo requirements still fit */
  8593. if( pc_checkcombo( sd, data ) )
  8594. continue;
  8595. /* move next value to empty slot */
  8596. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8597. if( sd->combos.bonus[j] == NULL )
  8598. continue;
  8599. if( cursor != j ) {
  8600. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8601. sd->combos.id[cursor] = sd->combos.id[j];
  8602. sd->combos.pos[cursor] = sd->combos.pos[j];
  8603. }
  8604. cursor++;
  8605. }
  8606. /* it's empty, we can clear all the memory */
  8607. if( (sd->combos.count = cursor) == 0 ) {
  8608. aFree(sd->combos.bonus);
  8609. aFree(sd->combos.id);
  8610. aFree(sd->combos.pos);
  8611. sd->combos.bonus = NULL;
  8612. sd->combos.id = NULL;
  8613. sd->combos.pos = NULL;
  8614. return retval; /* we also can return at this point for we have no more combos to check */
  8615. }
  8616. }
  8617. return retval;
  8618. }
  8619. /**
  8620. * Load combo data(s) of player
  8621. * @param *sd
  8622. * @return ret numbers of succeed combo
  8623. */
  8624. int pc_load_combo(struct map_session_data *sd) {
  8625. int i, ret = 0;
  8626. for( i = 0; i < EQI_MAX; i++ ) {
  8627. struct item_data *id = NULL;
  8628. short idx = sd->equip_index[i];
  8629. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8630. continue;
  8631. if( id->combos_count )
  8632. ret += pc_checkcombo(sd,id);
  8633. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8634. struct item_data *data;
  8635. int j;
  8636. for( j = 0; j < id->slot; j++ ) {
  8637. if (!sd->inventory.u.items_inventory[idx].card[j])
  8638. continue;
  8639. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8640. if( data->combos_count )
  8641. ret += pc_checkcombo(sd,data);
  8642. }
  8643. }
  8644. }
  8645. }
  8646. return ret;
  8647. }
  8648. /*==========================================
  8649. * Equip item on player sd at req_pos from inventory index n
  8650. * return: false - fail; true - success
  8651. *------------------------------------------*/
  8652. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos)
  8653. {
  8654. int i, pos, flag = 0, iflag;
  8655. struct item_data *id;
  8656. uint8 res = ITEM_EQUIP_ACK_OK;
  8657. nullpo_retr(false,sd);
  8658. if( n < 0 || n >= MAX_INVENTORY ) {
  8659. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8660. return false;
  8661. }
  8662. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8663. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8664. return false;
  8665. }
  8666. if (!(id = sd->inventory_data[n]))
  8667. return false;
  8668. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8669. if(battle_config.battle_log)
  8670. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  8671. if((res = pc_isequip(sd,n))) {
  8672. clif_equipitemack(sd,n,0,res); // fail
  8673. return false;
  8674. }
  8675. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  8676. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8677. return false;
  8678. }
  8679. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8680. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8681. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8682. return false;
  8683. }
  8684. if ((sd->class_&MAPID_BASEMASK) == MAPID_GUNSLINGER) {
  8685. /** Failing condition:
  8686. * 1. Always failed to equip ammo if no weapon equipped yet
  8687. * 2. Grenade only can be equipped if weapon is Grenade Launcher
  8688. * 3. Bullet cannot be equipped if the weapon is Grenade Launcher
  8689. * (4. The rest is relying on item job/class restriction).
  8690. **/
  8691. if (id->type == IT_AMMO) {
  8692. int w_idx = sd->equip_index[EQI_HAND_R];
  8693. enum weapon_type w_type = (w_idx != -1) ? (enum weapon_type)sd->inventory_data[w_idx]->look : W_FIST;
  8694. if (w_idx == -1 ||
  8695. (id->look == A_GRENADE && w_type != W_GRENADE) ||
  8696. (id->look != A_GRENADE && w_type == W_GRENADE))
  8697. {
  8698. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8699. return false;
  8700. }
  8701. }
  8702. else if (id->type == IT_WEAPON && id->look >= W_REVOLVER && id->look <= W_GRENADE) {
  8703. int a_idx = sd->equip_index[EQI_AMMO];
  8704. if (a_idx != -1) {
  8705. enum ammo_type a_type = (enum ammo_type)sd->inventory_data[a_idx]->look;
  8706. if ((a_type == A_GRENADE && id->look != W_GRENADE) ||
  8707. (a_type != A_GRENADE && id->look == W_GRENADE))
  8708. {
  8709. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8710. return false;
  8711. }
  8712. }
  8713. }
  8714. }
  8715. if (id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  8716. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8717. clif_notify_bindOnEquip(sd,n);
  8718. }
  8719. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  8720. pos = req_pos&EQP_ACC;
  8721. if (pos == EQP_ACC) //User specified both slots..
  8722. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8723. }
  8724. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8725. pos = (req_pos&EQP_ARMS);
  8726. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8727. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8728. }
  8729. if(pos == EQP_SHADOW_ACC) { // Shadow System
  8730. pos = req_pos&EQP_SHADOW_ACC;
  8731. if (pos == EQP_SHADOW_ACC)
  8732. pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8733. }
  8734. if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  8735. pos = (req_pos&EQP_SHADOW_ARMS);
  8736. if( pos == EQP_SHADOW_ARMS )
  8737. pos = (sd->equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  8738. }
  8739. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8740. //Update skill-block range database when weapon range changes. [Skotlex]
  8741. i = sd->equip_index[EQI_HAND_R];
  8742. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8743. flag = 1;
  8744. else
  8745. flag = id->range != sd->inventory_data[i]->range;
  8746. }
  8747. for(i=0;i<EQI_MAX;i++) {
  8748. if(pos & equip_bitmask[i]) {
  8749. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8750. pc_unequipitem(sd,sd->equip_index[i],2);
  8751. sd->equip_index[i] = n;
  8752. }
  8753. }
  8754. if(pos==EQP_AMMO) {
  8755. clif_arrowequip(sd,n);
  8756. clif_arrow_fail(sd,3);
  8757. }
  8758. else
  8759. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8760. sd->inventory.u.items_inventory[n].equip = pos;
  8761. if(pos & EQP_HAND_R) {
  8762. if(id)
  8763. sd->weapontype1 = id->look;
  8764. else
  8765. sd->weapontype1 = 0;
  8766. pc_calcweapontype(sd);
  8767. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8768. }
  8769. if(pos & EQP_HAND_L) {
  8770. if(id) {
  8771. if(id->type == IT_WEAPON) {
  8772. sd->status.shield = 0;
  8773. sd->weapontype2 = id->look;
  8774. }
  8775. else
  8776. if(id->type == IT_ARMOR) {
  8777. sd->status.shield = id->look;
  8778. sd->weapontype2 = 0;
  8779. }
  8780. }
  8781. else
  8782. sd->status.shield = sd->weapontype2 = 0;
  8783. pc_calcweapontype(sd);
  8784. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8785. }
  8786. if(pos & EQP_SHOES)
  8787. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8788. pc_set_costume_view(sd);
  8789. pc_checkallowskill(sd); //Check if status changes should be halted.
  8790. iflag = sd->npc_item_flag;
  8791. /* check for combos (MUST be before status_calc_pc) */
  8792. if( id->combos_count )
  8793. pc_checkcombo(sd,id);
  8794. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8795. ; //No cards
  8796. else {
  8797. for( i = 0; i < MAX_SLOTS; i++ ) {
  8798. struct item_data *data;
  8799. if (!sd->inventory.u.items_inventory[n].card[i])
  8800. continue;
  8801. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8802. if( data->combos_count )
  8803. pc_checkcombo(sd,data);
  8804. }
  8805. }
  8806. }
  8807. status_calc_pc(sd,SCO_NONE);
  8808. if (flag) //Update skill data
  8809. clif_skillinfoblock(sd);
  8810. //OnEquip script [Skotlex]
  8811. if (id) {
  8812. //only run the script if item isn't restricted
  8813. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  8814. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8815. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8816. ; //No cards
  8817. else {
  8818. for( i = 0; i < MAX_SLOTS; i++ ) {
  8819. struct item_data *data;
  8820. if (!sd->inventory.u.items_inventory[n].card[i])
  8821. continue;
  8822. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8823. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  8824. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8825. }
  8826. }
  8827. }
  8828. }
  8829. sd->npc_item_flag = iflag;
  8830. return true;
  8831. }
  8832. /*==========================================
  8833. * Called when attemting to unequip an item from player
  8834. * type:
  8835. * 0 - only unequip
  8836. * 1 - calculate status after unequipping
  8837. * 2 - force unequip
  8838. * return: false - fail; true - success
  8839. *------------------------------------------*/
  8840. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  8841. int i, iflag;
  8842. bool status_calc = false;
  8843. nullpo_retr(false,sd);
  8844. if (n < 0 || n >= MAX_INVENTORY) {
  8845. clif_unequipitemack(sd,0,0,0);
  8846. return false;
  8847. }
  8848. if (!sd->inventory.u.items_inventory[n].equip) {
  8849. clif_unequipitemack(sd,n,0,0);
  8850. return false; //Nothing to unequip
  8851. }
  8852. // status change that makes player cannot unequip equipment
  8853. if (!(flag&2) && sd->sc.count &&
  8854. (sd->sc.data[SC_BERSERK] ||
  8855. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8856. sd->sc.data[SC__BLOODYLUST] ||
  8857. sd->sc.data[SC_KYOUGAKU] ||
  8858. (sd->sc.data[SC_PYROCLASTIC] &&
  8859. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  8860. {
  8861. clif_unequipitemack(sd,n,0,0);
  8862. return false;
  8863. }
  8864. if (battle_config.battle_log)
  8865. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->inventory.u.items_inventory[n].equip);
  8866. if (!sd->inventory.u.items_inventory[n].equip) { //Nothing to unequip
  8867. clif_unequipitemack(sd, n, 0, 0);
  8868. return false;
  8869. }
  8870. for(i = 0; i < EQI_MAX; i++) {
  8871. if (sd->inventory.u.items_inventory[n].equip & equip_bitmask[i])
  8872. sd->equip_index[i] = -1;
  8873. }
  8874. if(sd->inventory.u.items_inventory[n].equip & EQP_HAND_R) {
  8875. sd->weapontype1 = 0;
  8876. sd->status.weapon = sd->weapontype2;
  8877. pc_calcweapontype(sd);
  8878. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8879. if( !battle_config.dancing_weaponswitch_fix )
  8880. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  8881. }
  8882. if(sd->inventory.u.items_inventory[n].equip & EQP_HAND_L) {
  8883. sd->status.shield = sd->weapontype2 = 0;
  8884. pc_calcweapontype(sd);
  8885. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8886. }
  8887. if(sd->inventory.u.items_inventory[n].equip & EQP_SHOES)
  8888. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8889. clif_unequipitemack(sd,n,sd->inventory.u.items_inventory[n].equip,1);
  8890. pc_set_costume_view(sd);
  8891. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  8892. // On weapon change (right and left hand)
  8893. if ((sd->inventory.u.items_inventory[n].equip & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  8894. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  8895. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  8896. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  8897. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  8898. }
  8899. // On armor change
  8900. if (sd->inventory.u.items_inventory[n].equip & EQP_ARMOR) {
  8901. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  8902. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  8903. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  8904. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  8905. }
  8906. // On ammo change
  8907. 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_))
  8908. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8909. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  8910. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  8911. sd->inventory.u.items_inventory[n].equip = 0;
  8912. iflag = sd->npc_item_flag;
  8913. /* check for combos (MUST be before status_calc_pc) */
  8914. if ( sd->inventory_data[n] ) {
  8915. if( sd->inventory_data[n]->combos_count ) {
  8916. if( pc_removecombo(sd,sd->inventory_data[n]) )
  8917. status_calc = true;
  8918. }
  8919. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8920. ; //No cards
  8921. else {
  8922. for( i = 0; i < MAX_SLOTS; i++ ) {
  8923. struct item_data *data;
  8924. if (!sd->inventory.u.items_inventory[n].card[i])
  8925. continue;
  8926. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8927. if( data->combos_count ) {
  8928. if( pc_removecombo(sd,data) )
  8929. status_calc = true;
  8930. }
  8931. }
  8932. }
  8933. }
  8934. }
  8935. if(flag&1 || status_calc) {
  8936. pc_checkallowskill(sd);
  8937. status_calc_pc(sd,SCO_NONE);
  8938. }
  8939. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  8940. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  8941. //OnUnEquip script [Skotlex]
  8942. if (sd->inventory_data[n]) {
  8943. if (sd->inventory_data[n]->unequip_script)
  8944. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8945. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8946. ; //No cards
  8947. else {
  8948. for( i = 0; i < MAX_SLOTS; i++ ) {
  8949. struct item_data *data;
  8950. if (!sd->inventory.u.items_inventory[n].card[i])
  8951. continue;
  8952. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8953. if( data->unequip_script )
  8954. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8955. }
  8956. }
  8957. }
  8958. }
  8959. sd->npc_item_flag = iflag;
  8960. return true;
  8961. }
  8962. /*==========================================
  8963. * Checking if player (sd) has an invalid item
  8964. * and is unequiped on map load (item_noequip)
  8965. *------------------------------------------*/
  8966. void pc_checkitem(struct map_session_data *sd) {
  8967. int i, calc_flag = 0;
  8968. struct item it;
  8969. nullpo_retv(sd);
  8970. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  8971. return;
  8972. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  8973. for( i = 0; i < MAX_INVENTORY; i++ ) {
  8974. it = sd->inventory.u.items_inventory[i];
  8975. if( it.nameid == 0 )
  8976. continue;
  8977. if( !it.equip )
  8978. continue;
  8979. if( it.equip&~pc_equippoint(sd,i) ) {
  8980. pc_unequipitem(sd, i, 2);
  8981. calc_flag = 1;
  8982. continue;
  8983. }
  8984. 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) ) {
  8985. pc_unequipitem(sd, i, 2);
  8986. calc_flag = 1;
  8987. continue;
  8988. }
  8989. }
  8990. if( calc_flag && sd->state.active ) {
  8991. pc_checkallowskill(sd);
  8992. status_calc_pc(sd,SCO_NONE);
  8993. }
  8994. }
  8995. /*==========================================
  8996. * Checks for unavailable items and removes them.
  8997. * @param sd: Player data
  8998. * @param type Forced check:
  8999. * 1 - Inventory
  9000. * 2 - Cart
  9001. * 4 - Storage
  9002. *------------------------------------------*/
  9003. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9004. {
  9005. int i;
  9006. unsigned short nameid;
  9007. char output[256];
  9008. nullpo_retv(sd);
  9009. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9010. for(i = 0; i < MAX_INVENTORY; i++) {
  9011. nameid = sd->inventory.u.items_inventory[i].nameid;
  9012. if (!nameid)
  9013. continue;
  9014. if (!itemdb_available(nameid)) {
  9015. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  9016. clif_displaymessage(sd->fd, output);
  9017. 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);
  9018. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9019. continue;
  9020. }
  9021. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9022. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9023. }
  9024. }
  9025. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9026. for(i = 0; i < MAX_CART; i++) {
  9027. nameid = sd->cart.u.items_cart[i].nameid;
  9028. if (!nameid)
  9029. continue;
  9030. if (!itemdb_available(nameid)) {
  9031. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  9032. clif_displaymessage(sd->fd, output);
  9033. 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);
  9034. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9035. continue;
  9036. }
  9037. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9038. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9039. }
  9040. }
  9041. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9042. for(i = 0; i < sd->storage.max_amount; i++) {
  9043. nameid = sd->storage.u.items_storage[i].nameid;
  9044. if (!nameid)
  9045. continue;
  9046. if (!itemdb_available(nameid)) {
  9047. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  9048. clif_displaymessage(sd->fd, output);
  9049. 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);
  9050. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9051. continue;
  9052. }
  9053. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9054. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9055. }
  9056. }
  9057. }
  9058. /*==========================================
  9059. * Update PVP rank for sd1 in cmp to sd2
  9060. *------------------------------------------*/
  9061. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9062. {
  9063. struct map_session_data *sd1,*sd2;
  9064. sd1=(struct map_session_data *)bl;
  9065. sd2=va_arg(ap,struct map_session_data *);
  9066. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9067. {// cannot register pvp rank for hidden GMs
  9068. return 0;
  9069. }
  9070. if( sd1->pvp_point > sd2->pvp_point )
  9071. sd2->pvp_rank++;
  9072. return 0;
  9073. }
  9074. /*==========================================
  9075. * Calculate new rank beetween all present players (map_foreachinallarea)
  9076. * and display result
  9077. *------------------------------------------*/
  9078. int pc_calc_pvprank(struct map_session_data *sd)
  9079. {
  9080. int old = sd->pvp_rank;
  9081. struct map_data *m = &map[sd->bl.m];
  9082. sd->pvp_rank=1;
  9083. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9084. if(old!=sd->pvp_rank || sd->pvp_lastusers!=m->users_pvp)
  9085. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=m->users_pvp,0);
  9086. return sd->pvp_rank;
  9087. }
  9088. /*==========================================
  9089. * Calculate next sd ranking calculation from config
  9090. *------------------------------------------*/
  9091. int pc_calc_pvprank_timer(int tid, unsigned int tick, int id, intptr_t data)
  9092. {
  9093. struct map_session_data *sd;
  9094. sd=map_id2sd(id);
  9095. if(sd==NULL)
  9096. return 0;
  9097. sd->pvp_timer = INVALID_TIMER;
  9098. if( pc_isinvisible(sd) )
  9099. {// do not calculate the pvp rank for a hidden GM
  9100. return 0;
  9101. }
  9102. if( pc_calc_pvprank(sd) > 0 )
  9103. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9104. return 0;
  9105. }
  9106. /*==========================================
  9107. * Checking if sd is married
  9108. * Return:
  9109. * partner_id = yes
  9110. * 0 = no
  9111. *------------------------------------------*/
  9112. int pc_ismarried(struct map_session_data *sd)
  9113. {
  9114. if(sd == NULL)
  9115. return -1;
  9116. if(sd->status.partner_id > 0)
  9117. return sd->status.partner_id;
  9118. else
  9119. return 0;
  9120. }
  9121. /*==========================================
  9122. * Marry player sd to player dstsd
  9123. * Return:
  9124. * false = fail
  9125. * true = success
  9126. *------------------------------------------*/
  9127. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9128. {
  9129. if(sd == NULL || dstsd == NULL ||
  9130. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9131. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9132. return false;
  9133. sd->status.partner_id = dstsd->status.char_id;
  9134. dstsd->status.partner_id = sd->status.char_id;
  9135. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9136. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9137. return true;
  9138. }
  9139. /*==========================================
  9140. * Divorce sd from its partner
  9141. * Return:
  9142. * false = fail
  9143. * true = success
  9144. *------------------------------------------*/
  9145. bool pc_divorce(struct map_session_data *sd)
  9146. {
  9147. struct map_session_data *p_sd;
  9148. int i;
  9149. if( sd == NULL || !pc_ismarried(sd) )
  9150. return false;
  9151. if( !sd->status.partner_id )
  9152. return false; // Char is not married
  9153. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9154. { // Lets char server do the divorce
  9155. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9156. return false; // No char server connected
  9157. return true;
  9158. }
  9159. // Both players online, lets do the divorce manually
  9160. sd->status.partner_id = 0;
  9161. p_sd->status.partner_id = 0;
  9162. for( i = 0; i < MAX_INVENTORY; i++ )
  9163. {
  9164. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9165. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9166. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9167. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9168. }
  9169. clif_divorced(sd, p_sd->status.name);
  9170. clif_divorced(p_sd, sd->status.name);
  9171. return true;
  9172. }
  9173. /**
  9174. * Get the partner map_session_data of a player
  9175. * @param sd : the husband|wife session
  9176. * @return partner session or NULL
  9177. */
  9178. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9179. if (!sd || !pc_ismarried(sd))
  9180. return NULL;
  9181. return map_charid2sd(sd->status.partner_id);
  9182. }
  9183. /**
  9184. * Get the father map_session_data of a player
  9185. * @param sd : the baby session
  9186. * @return father session or NULL
  9187. */
  9188. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9189. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9190. return NULL;
  9191. return map_charid2sd(sd->status.father);
  9192. }
  9193. /**
  9194. * Get the mother map_session_data of a player
  9195. * @param sd : the baby session
  9196. * @return mother session or NULL
  9197. */
  9198. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9199. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9200. return NULL;
  9201. return map_charid2sd(sd->status.mother);
  9202. }
  9203. /*==========================================
  9204. * Get sd children charid. (Need to be married)
  9205. *------------------------------------------*/
  9206. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9207. {
  9208. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9209. // charid2sd returns NULL if not found
  9210. return NULL;
  9211. return map_charid2sd(sd->status.child);
  9212. }
  9213. /*==========================================
  9214. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9215. *------------------------------------------*/
  9216. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  9217. {
  9218. int hp = 0, sp = 0;
  9219. if( pc_isdead(sd) )
  9220. return;
  9221. if (sd->hp_loss.value) {
  9222. sd->hp_loss.tick += diff_tick;
  9223. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9224. hp += sd->hp_loss.value;
  9225. sd->hp_loss.tick -= sd->hp_loss.rate;
  9226. }
  9227. if(hp >= sd->battle_status.hp)
  9228. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9229. }
  9230. if (sd->sp_loss.value) {
  9231. sd->sp_loss.tick += diff_tick;
  9232. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9233. sp += sd->sp_loss.value;
  9234. sd->sp_loss.tick -= sd->sp_loss.rate;
  9235. }
  9236. }
  9237. if (hp > 0 || sp > 0)
  9238. status_zap(&sd->bl, hp, sp);
  9239. }
  9240. //Character regen. Flag is used to know which types of regen can take place.
  9241. //&1: HP regen
  9242. //&2: SP regen
  9243. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  9244. {
  9245. int hp = 0, sp = 0;
  9246. if (sd->hp_regen.value) {
  9247. sd->hp_regen.tick += diff_tick;
  9248. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9249. hp += sd->hp_regen.value;
  9250. sd->hp_regen.tick -= sd->hp_regen.rate;
  9251. }
  9252. }
  9253. if (sd->sp_regen.value) {
  9254. sd->sp_regen.tick += diff_tick;
  9255. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9256. sp += sd->sp_regen.value;
  9257. sd->sp_regen.tick -= sd->sp_regen.rate;
  9258. }
  9259. }
  9260. if (sd->percent_hp_regen.value) {
  9261. sd->percent_hp_regen.tick += diff_tick;
  9262. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9263. hp += (sd->percent_hp_regen.value * sd->status.max_hp);
  9264. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9265. }
  9266. }
  9267. if (sd->percent_sp_regen.value) {
  9268. sd->percent_sp_regen.tick += diff_tick;
  9269. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9270. sp += (sd->percent_sp_regen.value * sd->status.max_sp);
  9271. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9272. }
  9273. }
  9274. if (hp > 0 || sp > 0)
  9275. status_heal(&sd->bl, hp, sp, 0);
  9276. }
  9277. /*==========================================
  9278. * Memo player sd savepoint. (map,x,y)
  9279. *------------------------------------------*/
  9280. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9281. {
  9282. nullpo_retv(sd);
  9283. sd->status.save_point.map = mapindex;
  9284. sd->status.save_point.x = x;
  9285. sd->status.save_point.y = y;
  9286. }
  9287. /*==========================================
  9288. * Save 1 player data at autosave interval
  9289. *------------------------------------------*/
  9290. static int pc_autosave(int tid, unsigned int tick, int id, intptr_t data)
  9291. {
  9292. int interval;
  9293. struct s_mapiterator* iter;
  9294. struct map_session_data* sd;
  9295. static int last_save_id = 0, save_flag = 0;
  9296. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9297. save_flag = 0;
  9298. else
  9299. save_flag = 1; //Noone was saved, so save first found char.
  9300. iter = mapit_getallusers();
  9301. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9302. {
  9303. if(sd->bl.id == last_save_id && save_flag != 1) {
  9304. save_flag = 1;
  9305. continue;
  9306. }
  9307. if(save_flag != 1) //Not our turn to save yet.
  9308. continue;
  9309. //Save char.
  9310. last_save_id = sd->bl.id;
  9311. save_flag = 2;
  9312. if (pc_isvip(sd)) // Check if we're still VIP
  9313. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9314. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9315. break;
  9316. }
  9317. mapit_free(iter);
  9318. interval = autosave_interval/(map_usercount()+1);
  9319. if(interval < minsave_interval)
  9320. interval = minsave_interval;
  9321. add_timer(gettick()+interval,pc_autosave,0,0);
  9322. return 0;
  9323. }
  9324. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9325. {
  9326. if (sd->state.night != night_flag && map[sd->bl.m].flag.nightenabled)
  9327. { //Night/day state does not match.
  9328. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9329. sd->state.night = night_flag;
  9330. return 1;
  9331. }
  9332. return 0;
  9333. }
  9334. /*================================================
  9335. * timer to do the day [Yor]
  9336. * data: 0 = called by timer, 1 = gmcommand/script
  9337. *------------------------------------------------*/
  9338. int map_day_timer(int tid, unsigned int tick, int id, intptr_t data)
  9339. {
  9340. char tmp_soutput[1024];
  9341. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9342. return 0;
  9343. if (!night_flag)
  9344. return 0; //Already day.
  9345. night_flag = 0; // 0=day, 1=night [Yor]
  9346. map_foreachpc(pc_daynight_timer_sub);
  9347. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9348. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9349. return 0;
  9350. }
  9351. /*================================================
  9352. * timer to do the night [Yor]
  9353. * data: 0 = called by timer, 1 = gmcommand/script
  9354. *------------------------------------------------*/
  9355. int map_night_timer(int tid, unsigned int tick, int id, intptr_t data)
  9356. {
  9357. char tmp_soutput[1024];
  9358. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9359. return 0;
  9360. if (night_flag)
  9361. return 0; //Already nigth.
  9362. night_flag = 1; // 0=day, 1=night [Yor]
  9363. map_foreachpc(pc_daynight_timer_sub);
  9364. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9365. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9366. return 0;
  9367. }
  9368. /**
  9369. * Attempt to stand up a player
  9370. * @param sd
  9371. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9372. * @return True if success, Fals if failed
  9373. */
  9374. bool pc_setstand(struct map_session_data *sd, bool force){
  9375. nullpo_ret(sd);
  9376. // Cannot stand yet
  9377. // TODO: Move to SCS_NOSTAND [Cydh]
  9378. if (!force && &sd->sc && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9379. return false;
  9380. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9381. clif_status_load(&sd->bl,EFST_SIT,0);
  9382. clif_standing(&sd->bl); //Inform area PC is standing
  9383. //Reset sitting tick.
  9384. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9385. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9386. return true;
  9387. }
  9388. /**
  9389. * Calculate Overheat value
  9390. * @param sd: Player data
  9391. * @param heat: Amount of Heat to adjust
  9392. **/
  9393. void pc_overheat(struct map_session_data *sd, int16 heat) {
  9394. struct status_change_entry *sce = NULL;
  9395. int16 limit[] = { 150, 200, 280, 360, 450 };
  9396. uint16 skill_lv;
  9397. nullpo_retv(sd);
  9398. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  9399. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  9400. sce->val1 += heat;
  9401. sce->val1 = cap_value(sce->val1, 0, 1000);
  9402. if (sd->sc.data[SC_OVERHEAT])
  9403. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  9404. if (sce->val1 > limit[skill_lv])
  9405. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  9406. } else if (heat > 0)
  9407. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  9408. }
  9409. /**
  9410. * Check if player is autolooting given itemID.
  9411. */
  9412. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9413. {
  9414. uint8 i = 0;
  9415. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9416. return true;
  9417. if (!sd->state.autolooting)
  9418. return false;
  9419. if (sd->state.autolooting)
  9420. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9421. return (i != AUTOLOOTITEM_SIZE);
  9422. }
  9423. /**
  9424. * Checks if player can use @/#command
  9425. * @param sd Player map session data
  9426. * @param command Command name without @/# and params
  9427. * @param type is it atcommand or charcommand
  9428. */
  9429. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9430. {
  9431. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9432. }
  9433. /**
  9434. * Checks if commands used by a player should be logged
  9435. * according to their group setting.
  9436. * @param sd Player map session data
  9437. */
  9438. bool pc_should_log_commands(struct map_session_data *sd)
  9439. {
  9440. return pc_group_should_log_commands(pc_get_group_id(sd));
  9441. }
  9442. /**
  9443. * Spirit Charm expiration timer.
  9444. * @see TimerFunc
  9445. */
  9446. static int pc_spiritcharm_timer(int tid, unsigned int tick, int id, intptr_t data)
  9447. {
  9448. struct map_session_data *sd;
  9449. int i;
  9450. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9451. return 1;
  9452. if (sd->spiritcharm <= 0) {
  9453. 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);
  9454. sd->spiritcharm = 0;
  9455. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9456. return 0;
  9457. }
  9458. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9459. if (i == sd->spiritcharm) {
  9460. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9461. return 0;
  9462. }
  9463. sd->spiritcharm--;
  9464. if (i != sd->spiritcharm)
  9465. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9466. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9467. if (sd->spiritcharm <= 0)
  9468. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9469. clif_spiritcharm(sd);
  9470. return 0;
  9471. }
  9472. /**
  9473. * Adds a spirit charm.
  9474. * @param sd: Target character
  9475. * @param interval: Duration
  9476. * @param max: Maximum amount of charms to add
  9477. * @param type: Charm type (@see spirit_charm_types)
  9478. */
  9479. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9480. {
  9481. int tid, i;
  9482. nullpo_retv(sd);
  9483. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9484. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9485. if (max > MAX_SPIRITCHARM)
  9486. max = MAX_SPIRITCHARM;
  9487. if (sd->spiritcharm < 0)
  9488. sd->spiritcharm = 0;
  9489. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9490. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9491. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9492. sd->spiritcharm--;
  9493. if (sd->spiritcharm != 0)
  9494. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9495. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9496. }
  9497. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9498. 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);
  9499. if (i != sd->spiritcharm)
  9500. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9501. sd->spiritcharm_timer[i] = tid;
  9502. sd->spiritcharm++;
  9503. sd->spiritcharm_type = type;
  9504. clif_spiritcharm(sd);
  9505. }
  9506. /**
  9507. * Removes one or more spirit charms.
  9508. * @param sd: The target character
  9509. * @param count: Amount of charms to remove
  9510. * @param type: Type of charm to remove
  9511. */
  9512. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9513. {
  9514. int i;
  9515. nullpo_retv(sd);
  9516. if (sd->spiritcharm_type != type)
  9517. return;
  9518. if (sd->spiritcharm <= 0) {
  9519. sd->spiritcharm = 0;
  9520. return;
  9521. }
  9522. if (count <= 0)
  9523. return;
  9524. if (count > sd->spiritcharm)
  9525. count = sd->spiritcharm;
  9526. sd->spiritcharm -= count;
  9527. if (count > MAX_SPIRITCHARM)
  9528. count = MAX_SPIRITCHARM;
  9529. for (i = 0; i < count; i++) {
  9530. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9531. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9532. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9533. }
  9534. }
  9535. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9536. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9537. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9538. }
  9539. if (sd->spiritcharm <= 0)
  9540. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9541. clif_spiritcharm(sd);
  9542. }
  9543. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9544. /**
  9545. * Renewal EXP/Item Drop rate modifier based on level penalty
  9546. * @param level_diff: Monster and Player level difference
  9547. * @param mob_class: Monster class
  9548. * @param mode: Monster mode
  9549. * @param type: 1 - EXP, 2 - Item Drop
  9550. * @return Penalty rate
  9551. */
  9552. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9553. {
  9554. int rate = 100;
  9555. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9556. return rate;
  9557. if (level_diff < 0)
  9558. level_diff = MAX_LEVEL + (~level_diff + 1);
  9559. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9560. return rate;
  9561. return 100; // Penalty not found, return default
  9562. }
  9563. #endif
  9564. int pc_split_str(char *str,char **val,int num)
  9565. {
  9566. int i;
  9567. for (i=0; i<num && str; i++){
  9568. val[i] = str;
  9569. str = strchr(str,',');
  9570. if (str && i<num-1) //Do not remove a trailing comma.
  9571. *str++=0;
  9572. }
  9573. return i;
  9574. }
  9575. int pc_split_atoi(char* str, int* val, char sep, int max)
  9576. {
  9577. int i,j;
  9578. for (i=0; i<max; i++) {
  9579. if (!str) break;
  9580. val[i] = atoi(str);
  9581. str = strchr(str,sep);
  9582. if (str)
  9583. *str++=0;
  9584. }
  9585. //Zero up the remaining.
  9586. for(j=i; j < max; j++)
  9587. val[j] = 0;
  9588. return i;
  9589. }
  9590. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9591. {
  9592. static int warning=0;
  9593. int i,j;
  9594. for (i=0; i<max; i++) {
  9595. double f;
  9596. if (!str) break;
  9597. f = atof(str);
  9598. if (f < 0)
  9599. val[i] = 0;
  9600. else if (f > UINT_MAX) {
  9601. val[i] = UINT_MAX;
  9602. if (!warning) {
  9603. warning = 1;
  9604. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9605. }
  9606. } else
  9607. val[i] = (unsigned int)f;
  9608. str = strchr(str,sep);
  9609. if (str)
  9610. *str++=0;
  9611. }
  9612. //Zero up the remaining.
  9613. for(j=i; j < max; j++)
  9614. val[j] = 0;
  9615. return i;
  9616. }
  9617. /*==========================================
  9618. * sub DB reading.
  9619. * Function used to read skill_tree.txt
  9620. *------------------------------------------*/
  9621. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  9622. {
  9623. uint32 baselv, joblv, baselv_max, joblv_max;
  9624. uint16 skill_id, skill_lv, skill_lv_max;
  9625. int idx, class_;
  9626. unsigned int i, offset, skill_idx;
  9627. class_ = atoi(fields[0]);
  9628. skill_id = (uint16)atoi(fields[1]);
  9629. skill_lv = (uint16)atoi(fields[2]);
  9630. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  9631. baselv = (uint32)atoi(fields[3]);
  9632. joblv = (uint32)atoi(fields[4]);
  9633. offset = 5;
  9634. }
  9635. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  9636. baselv = joblv = 0;
  9637. offset = 3;
  9638. }
  9639. else {
  9640. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  9641. return false;
  9642. }
  9643. if(!pcdb_checkid(class_))
  9644. {
  9645. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  9646. return false;
  9647. }
  9648. idx = pc_class2idx(class_);
  9649. if (!skill_get_index(skill_id)) {
  9650. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.", skill_id, class_);
  9651. return false;
  9652. }
  9653. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9654. 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);
  9655. skill_lv = skill_lv_max;
  9656. }
  9657. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  9658. 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);
  9659. baselv = baselv_max;
  9660. }
  9661. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  9662. 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);
  9663. joblv = joblv_max;
  9664. }
  9665. //This is to avoid adding two lines for the same skill. [Skotlex]
  9666. 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 );
  9667. if( skill_idx == MAX_SKILL_TREE )
  9668. {
  9669. 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_);
  9670. return false;
  9671. }
  9672. else if(skill_tree[idx][skill_idx].skill_id)
  9673. {
  9674. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  9675. }
  9676. skill_tree[idx][skill_idx].skill_id = skill_id;
  9677. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  9678. skill_tree[idx][skill_idx].baselv = baselv;
  9679. skill_tree[idx][skill_idx].joblv = joblv;
  9680. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  9681. {
  9682. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  9683. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  9684. if (skill_id == 0) {
  9685. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  9686. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  9687. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  9688. }
  9689. continue;
  9690. }
  9691. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  9692. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  9693. return false;
  9694. }
  9695. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9696. 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);
  9697. skill_lv = skill_lv_max;
  9698. }
  9699. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  9700. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  9701. }
  9702. return true;
  9703. }
  9704. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9705. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  9706. {
  9707. int type, class_, diff;
  9708. type = atoi(fields[0]); //1=experience, 2=item drop
  9709. class_ = atoi(fields[1]);
  9710. diff = atoi(fields[2]);
  9711. if( type != 1 && type != 2 ){
  9712. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  9713. return false;
  9714. }
  9715. if( !CHK_CLASS(class_) ){
  9716. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  9717. return false;
  9718. }
  9719. diff = min(diff, MAX_LEVEL);
  9720. if( diff < 0 )
  9721. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  9722. level_penalty[type][class_][diff] = atoi(fields[3]);
  9723. return true;
  9724. }
  9725. #endif
  9726. /** [Cydh]
  9727. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  9728. * @param level Base level of player
  9729. * @param class_ Job ID @see enum e_job
  9730. * @return base_hp
  9731. */
  9732. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  9733. double base_hp;
  9734. uint16 i, idx = pc_class2idx(class_);
  9735. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  9736. #ifndef RENEWAL
  9737. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  9738. #endif
  9739. for (i = 2; i <= level; i++)
  9740. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  9741. if (class_ == JOB_SUMMONER)
  9742. base_hp += floor((base_hp / 2) + 0.5);
  9743. return (unsigned int)base_hp;
  9744. }
  9745. /** [Playtester]
  9746. * Calculates base sp of player.
  9747. * @param level Base level of player
  9748. * @param class_ Job ID @see enum e_job
  9749. * @return base_sp
  9750. */
  9751. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  9752. double base_sp;
  9753. uint16 idx = pc_class2idx(class_);
  9754. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  9755. switch (class_) {
  9756. case JOB_NINJA:
  9757. if (level >= 10)
  9758. base_sp -= 22;
  9759. else
  9760. base_sp = 11 + 3*level;
  9761. break;
  9762. case JOB_GUNSLINGER:
  9763. if (level > 10)
  9764. base_sp -= 18;
  9765. else
  9766. base_sp = 9 + 3*level;
  9767. break;
  9768. case JOB_SUMMONER:
  9769. base_sp -= floor(base_sp / 2);
  9770. break;
  9771. }
  9772. return (unsigned int)base_sp;
  9773. }
  9774. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  9775. static bool pc_readdb_job1(char* fields[], int columns, int current){
  9776. int idx, class_;
  9777. unsigned int i;
  9778. class_ = atoi(fields[0]);
  9779. if (!pcdb_checkid(class_)) {
  9780. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  9781. return false;
  9782. }
  9783. idx = pc_class2idx(class_);
  9784. job_info[idx].max_weight_base = atoi(fields[1]);
  9785. job_info[idx].hp_factor = atoi(fields[2]);
  9786. job_info[idx].hp_multiplicator = atoi(fields[3]);
  9787. job_info[idx].sp_factor = atoi(fields[4]);
  9788. #ifdef RENEWAL_ASPD
  9789. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  9790. #else
  9791. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  9792. #endif
  9793. {
  9794. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  9795. }
  9796. return true;
  9797. }
  9798. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  9799. static bool pc_readdb_job2(char* fields[], int columns, int current)
  9800. {
  9801. int idx, class_, i;
  9802. class_ = atoi(fields[0]);
  9803. if(!pcdb_checkid(class_))
  9804. {
  9805. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  9806. return false;
  9807. }
  9808. idx = pc_class2idx(class_);
  9809. for(i = 1; i < columns; i++)
  9810. {
  9811. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  9812. }
  9813. return true;
  9814. }
  9815. //Reading job_exp.txt line
  9816. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  9817. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  9818. {
  9819. int idx, i, type;
  9820. int job_id,job_count,jobs[CLASS_COUNT];
  9821. unsigned int ui, maxlvl;
  9822. maxlvl = atoi(fields[0]);
  9823. if(maxlvl > MAX_LEVEL || maxlvl<1){
  9824. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  9825. return false;
  9826. }
  9827. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9828. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  9829. return false;
  9830. }
  9831. type = atoi(fields[2]);
  9832. if(type < 0 || type > 1){
  9833. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  9834. return false;
  9835. }
  9836. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  9837. if (job_count < 1)
  9838. return false;
  9839. job_id = jobs[0];
  9840. if(!pcdb_checkid(job_id)){
  9841. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  9842. return false;
  9843. }
  9844. idx = pc_class2idx(job_id);
  9845. job_info[idx].max_level[type] = maxlvl;
  9846. for(i=0; i<maxlvl; i++)
  9847. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  9848. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  9849. //The reasoning behind the -2 is this... if the max level is 5, then the array
  9850. //should look like this:
  9851. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  9852. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  9853. job_info[idx].max_level[type]--;
  9854. if (job_info[idx].max_level[type] < maxlvl) {
  9855. 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]);
  9856. ShowInfo("Filling the missing values with the last exp entry.\n");
  9857. //Fill the requested values with the last entry.
  9858. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  9859. for (; ui+2 < maxlvl; ui++)
  9860. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  9861. job_info[idx].max_level[type] = maxlvl;
  9862. }
  9863. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9864. for (i = 1; i < job_count; i++) {
  9865. job_id = jobs[i];
  9866. if (!pcdb_checkid(job_id)) {
  9867. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  9868. continue;
  9869. }
  9870. idx = pc_class2idx(job_id);
  9871. 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]));
  9872. job_info[idx].max_level[type] = maxlvl;
  9873. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9874. }
  9875. return true;
  9876. }
  9877. /**
  9878. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  9879. * startlvl,endlvl,class,type,values...
  9880. */
  9881. #ifdef HP_SP_TABLES
  9882. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  9883. {
  9884. int i, startlvl, endlvl;
  9885. int job_count,jobs[CLASS_COUNT];
  9886. short type;
  9887. startlvl = atoi(fields[0]);
  9888. if(startlvl<1){
  9889. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9890. return false;
  9891. }
  9892. endlvl = atoi(fields[1]);
  9893. if(endlvl<1 || endlvl<startlvl){
  9894. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  9895. return false;
  9896. }
  9897. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9898. 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);
  9899. return false;
  9900. }
  9901. type = atoi(fields[3]);
  9902. if(type < 0 || type > 1){
  9903. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  9904. return false;
  9905. }
  9906. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  9907. if (job_count < 1)
  9908. return false;
  9909. for (i = 0; i < job_count; i++) {
  9910. int idx, job_id = jobs[i], use_endlvl;
  9911. if (!pcdb_checkid(job_id)) {
  9912. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  9913. return false;
  9914. }
  9915. idx = pc_class2idx(job_id);
  9916. if (startlvl > job_info[idx].max_level[0]) {
  9917. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9918. return false;
  9919. }
  9920. //Just read until available max level for this job, don't use MAX_LEVEL!
  9921. use_endlvl = endlvl;
  9922. if (use_endlvl > job_info[idx].max_level[0])
  9923. use_endlvl = job_info[idx].max_level[0];
  9924. if(type == 0) { //hp type
  9925. uint16 j;
  9926. for(j = 0; j < use_endlvl; j++) {
  9927. if (atoi(fields[j+4])) {
  9928. uint16 lvl_idx = startlvl-1+j;
  9929. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  9930. //Tells if this HP is lower than previous level (but not for 99->100)
  9931. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  9932. 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",
  9933. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  9934. }
  9935. }
  9936. }
  9937. else { //sp type
  9938. uint16 j;
  9939. for(j = 0; j < use_endlvl; j++) {
  9940. if (atoi(fields[j+4])) {
  9941. uint16 lvl_idx = startlvl-1+j;
  9942. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  9943. //Tells if this SP is lower than previous level (but not for 99->100)
  9944. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  9945. 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",
  9946. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  9947. }
  9948. }
  9949. }
  9950. }
  9951. return true;
  9952. }
  9953. #endif
  9954. /** [Cydh]
  9955. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  9956. */
  9957. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  9958. {
  9959. int idx, class_;
  9960. uint16 str, agi, vit, int_, dex, luk;
  9961. script_get_constant(trim(fields[0]),&class_);
  9962. if ((idx = pc_class2idx(class_)) < 0) {
  9963. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  9964. return false;
  9965. }
  9966. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  9967. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  9968. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  9969. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  9970. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  9971. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  9972. job_info[idx].max_param.str = str;
  9973. job_info[idx].max_param.agi = agi;
  9974. job_info[idx].max_param.vit = vit;
  9975. job_info[idx].max_param.int_ = int_;
  9976. job_info[idx].max_param.dex = dex;
  9977. job_info[idx].max_param.luk = luk;
  9978. return true;
  9979. }
  9980. /**
  9981. * Read job_noenter_map.txt
  9982. **/
  9983. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  9984. int idx, class_ = -1;
  9985. if (ISDIGIT(str[0][0])) {
  9986. class_ = atoi(str[0]);
  9987. } else {
  9988. if (!script_get_constant(str[0], &class_)) {
  9989. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  9990. return false;
  9991. }
  9992. }
  9993. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  9994. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", str[0]);
  9995. return false;
  9996. }
  9997. job_info[idx].noenter_map.zone = atoi(str[1]);
  9998. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  9999. return true;
  10000. }
  10001. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10002. int i=1;
  10003. char line[24000]; //FIXME this seem too big
  10004. FILE *fp;
  10005. sprintf(line, "%s/statpoint.txt", basedir);
  10006. fp=fopen(line,"r");
  10007. if(fp == NULL){
  10008. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10009. return max(last_s,i);
  10010. } else {
  10011. int entries=0;
  10012. while(fgets(line, sizeof(line), fp))
  10013. {
  10014. int stat;
  10015. trim(line);
  10016. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10017. continue;
  10018. if ((stat=strtoul(line,NULL,10))<0)
  10019. stat=0;
  10020. if (i > MAX_LEVEL)
  10021. break;
  10022. statp[i]=stat;
  10023. i++;
  10024. entries++;
  10025. }
  10026. fclose(fp);
  10027. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10028. }
  10029. return max(last_s,i);
  10030. }
  10031. /*==========================================
  10032. * pc DB reading.
  10033. * job_exp.txt - required experience values
  10034. * skill_tree.txt - skill tree for every class
  10035. * attr_fix.txt - elemental adjustment table
  10036. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10037. * job_db2.txt - job,stats bonuses/lvl
  10038. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10039. *------------------------------------------*/
  10040. void pc_readdb(void) {
  10041. int i, k, s = 1;
  10042. const char* dbsubpath[] = {
  10043. "",
  10044. "/" DBIMPORT,
  10045. //add other path here
  10046. };
  10047. //reset
  10048. memset(job_info,0,sizeof(job_info)); // job_info table
  10049. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10050. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10051. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10052. for( k=1; k < 3; k++ ){ // fill in the blanks
  10053. int j;
  10054. for( j = 0; j < CLASS_ALL; j++ ){
  10055. int tmp = 0;
  10056. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10057. if( i == MAX_LEVEL+1 )
  10058. tmp = level_penalty[k][j][0];// reset
  10059. if( level_penalty[k][j][i] > 0 )
  10060. tmp = level_penalty[k][j][i];
  10061. else
  10062. level_penalty[k][j][i] = tmp;
  10063. }
  10064. }
  10065. }
  10066. #endif
  10067. // reset then read statspoint
  10068. memset(statp,0,sizeof(statp));
  10069. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10070. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10071. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10072. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10073. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10074. if(i==0) {
  10075. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10076. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10077. }
  10078. else {
  10079. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10080. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10081. }
  10082. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10083. if (i == 0)
  10084. #ifdef RENEWAL_ASPD
  10085. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i > 0);
  10086. #else
  10087. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i > 0);
  10088. #endif
  10089. else
  10090. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i > 0);
  10091. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10092. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10093. #ifdef HP_SP_TABLES
  10094. 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!
  10095. #endif
  10096. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10097. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10098. aFree(dbsubpath1);
  10099. aFree(dbsubpath2);
  10100. }
  10101. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10102. memset(skill_tree, 0, sizeof(skill_tree));
  10103. 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);
  10104. 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);
  10105. // generate the remaining parts of the db if necessary
  10106. k = battle_config.use_statpoint_table; //save setting
  10107. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10108. statp[0] = 45; // seed value
  10109. for (; s <= MAX_LEVEL; s++)
  10110. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10111. battle_config.use_statpoint_table = k; //restore setting
  10112. //Checking if all class have their data
  10113. for (i = 0; i < JOB_MAX; i++) {
  10114. int idx;
  10115. uint16 j;
  10116. if (!pcdb_checkid(i))
  10117. continue;
  10118. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST)
  10119. continue; //Classes that do not need exp tables.
  10120. idx = pc_class2idx(i);
  10121. if (!job_info[idx].max_level[0])
  10122. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10123. if (!job_info[idx].max_level[1])
  10124. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10125. //Init and checking the empty value of Base HP/SP [Cydh]
  10126. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10127. if (job_info[idx].base_hp[j] == 0)
  10128. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10129. if (job_info[idx].base_sp[j] == 0)
  10130. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10131. }
  10132. }
  10133. }
  10134. // Read MOTD on startup. [Valaris]
  10135. int pc_read_motd(void)
  10136. {
  10137. FILE* fp;
  10138. // clear old MOTD
  10139. memset(motd_text, 0, sizeof(motd_text));
  10140. // read current MOTD
  10141. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10142. {
  10143. unsigned int entries = 0;
  10144. char buf[CHAT_SIZE_MAX];
  10145. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10146. {
  10147. unsigned int lines = 0;
  10148. size_t len;
  10149. lines++;
  10150. if( buf[0] == '/' && buf[1] == '/' )
  10151. continue;
  10152. len = strlen(buf);
  10153. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10154. len--;
  10155. if( len ) {
  10156. char * ptr;
  10157. buf[len] = 0;
  10158. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10159. 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);
  10160. }
  10161. else {// empty line
  10162. buf[0] = ' ';
  10163. buf[1] = 0;
  10164. }
  10165. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10166. entries++;
  10167. }
  10168. fclose(fp);
  10169. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10170. }
  10171. else
  10172. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10173. return 0;
  10174. }
  10175. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10176. int i,cursor = 0;
  10177. struct item_cd* cd = NULL;
  10178. if( load ) {
  10179. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10180. // no item cooldown is associated with this character
  10181. return;
  10182. }
  10183. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10184. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10185. sd->item_delay[cursor].tick = cd->tick[i];
  10186. sd->item_delay[cursor].nameid = cd->nameid[i];
  10187. cursor++;
  10188. }
  10189. }
  10190. idb_remove(itemcd_db,sd->status.char_id);
  10191. } else {
  10192. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10193. // create a new skill cooldown object for map storage
  10194. CREATE( cd, struct item_cd, 1 );
  10195. idb_put( itemcd_db, sd->status.char_id, cd );
  10196. }
  10197. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10198. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10199. cd->tick[cursor] = sd->item_delay[i].tick;
  10200. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10201. cursor++;
  10202. }
  10203. }
  10204. }
  10205. return;
  10206. }
  10207. /**
  10208. * Add item delay to player's item delay data
  10209. * @param sd Player
  10210. * @param id Item data
  10211. * @param tick Current tick
  10212. * @param n Item index in inventory
  10213. * @return 0: No delay, can consume item.
  10214. * 1: Has delay, cancel consumption.
  10215. **/
  10216. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10217. int i;
  10218. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10219. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10220. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10221. if( i < MAX_ITEMDELAYS ) {
  10222. if( sd->item_delay[i].nameid ) {// found
  10223. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10224. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10225. char e_msg[CHAT_SIZE_MAX];
  10226. if( e_tick > 99 )
  10227. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10228. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10229. else
  10230. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10231. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10232. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10233. return 1; // Delay has not expired yet
  10234. }
  10235. } else {// not yet used item (all slots are initially empty)
  10236. sd->item_delay[i].nameid = id->nameid;
  10237. }
  10238. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10239. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10240. } else {// should not happen
  10241. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10242. }
  10243. //clean up used delays so we can give room for more
  10244. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10245. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10246. sd->item_delay[i].tick = 0;
  10247. sd->item_delay[i].nameid = 0;
  10248. }
  10249. }
  10250. return 0;
  10251. }
  10252. /**
  10253. * Check if player has delay to reuse item
  10254. * @param sd Player
  10255. * @param id Item data
  10256. * @param tick Current tick
  10257. * @param n Item index in inventory
  10258. * @return 0: No delay, can consume item.
  10259. * 1: Has delay, cancel consumption.
  10260. **/
  10261. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10262. struct status_change *sc = NULL;
  10263. nullpo_retr(0, sd);
  10264. nullpo_retr(0, id);
  10265. // Do normal delay assignment
  10266. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10267. return pc_itemcd_add(sd, id, tick, n);
  10268. // Send reply of delay remains
  10269. if (sc->data[id->delay_sc]) {
  10270. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10271. clif_msg_value(sd, ITEM_REUSE_LIMIT, timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99);
  10272. return 1;
  10273. }
  10274. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10275. return 0;
  10276. }
  10277. /**
  10278. * Clear the dmglog data from player
  10279. * @param sd
  10280. * @param md
  10281. **/
  10282. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10283. {
  10284. uint8 i;
  10285. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10286. if (i < DAMAGELOG_SIZE) {
  10287. md->dmglog[i].id = 0;
  10288. md->dmglog[i].dmg = 0;
  10289. md->dmglog[i].flag = 0;
  10290. }
  10291. }
  10292. /**
  10293. * Add log to player's dmglog
  10294. * @param sd
  10295. * @param id Monster's GID
  10296. **/
  10297. void pc_damage_log_add(struct map_session_data *sd, int id)
  10298. {
  10299. uint8 i = 0;
  10300. if (!sd || !id)
  10301. return;
  10302. //Only store new data, don't need to renew the old one with same id
  10303. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10304. if (i < DAMAGELOG_SIZE_PC)
  10305. return;
  10306. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10307. if (sd->dmglog[i] == 0) {
  10308. sd->dmglog[i] = id;
  10309. return;
  10310. }
  10311. }
  10312. }
  10313. /**
  10314. * Clear dmglog data from player
  10315. * @param sd
  10316. * @param id Monster's id
  10317. **/
  10318. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10319. {
  10320. uint8 i;
  10321. struct mob_data *md = NULL;
  10322. if (!sd)
  10323. return;
  10324. if (!id) {
  10325. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10326. if( !sd->dmglog[i] ) //skip the empty value
  10327. continue;
  10328. if ((md = map_id2md(sd->dmglog[i])))
  10329. pc_clear_log_damage_sub(sd->status.char_id,md);
  10330. sd->dmglog[i] = 0;
  10331. }
  10332. }
  10333. else {
  10334. if ((md = map_id2md(id)))
  10335. pc_clear_log_damage_sub(sd->status.char_id,md);
  10336. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10337. if (i < DAMAGELOG_SIZE_PC)
  10338. sd->dmglog[i] = 0;
  10339. }
  10340. }
  10341. /**
  10342. * Status change data arrived from char-server
  10343. * @param sd: Player data
  10344. */
  10345. void pc_scdata_received(struct map_session_data *sd) {
  10346. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10347. sd->state.pc_loaded = true;
  10348. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10349. sd->state.connect_new = 1;
  10350. clif_parse_LoadEndAck(sd->fd, sd);
  10351. }
  10352. if (pc_iscarton(sd)) {
  10353. sd->cart_weight_max = 0; // Force a client refesh
  10354. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10355. }
  10356. }
  10357. /**
  10358. * Check player account expiration time and rental item expirations
  10359. * @param sd: Player data
  10360. */
  10361. void pc_check_expiration(struct map_session_data *sd) {
  10362. #ifndef ENABLE_SC_SAVING
  10363. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10364. #endif
  10365. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10366. time_t exp_time = sd->expiration_time;
  10367. char tmpstr[1024];
  10368. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10369. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  10370. pc_expire_check(sd);
  10371. }
  10372. }
  10373. int pc_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10374. struct map_session_data *sd = map_id2sd(id);
  10375. if( !sd ) return 0;
  10376. sd->expiration_tid = INVALID_TIMER;
  10377. if( sd->fd )
  10378. clif_authfail_fd(sd->fd,10);
  10379. map_quit(sd);
  10380. return 0;
  10381. }
  10382. int pc_autotrade_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10383. struct map_session_data *sd = map_id2sd(id);
  10384. if (!sd)
  10385. return 0;
  10386. sd->autotrade_tid = INVALID_TIMER;
  10387. if (sd->state.autotrade&2)
  10388. vending_reopen(sd);
  10389. if (sd->state.autotrade&4)
  10390. buyingstore_reopen(sd);
  10391. if (!sd->vender_id && !sd->buyer_id) {
  10392. sd->state.autotrade = 0;
  10393. map_quit(sd);
  10394. }
  10395. return 0;
  10396. }
  10397. /* this timer exists only when a character with a expire timer > 24h is online */
  10398. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10399. int pc_global_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10400. struct s_mapiterator* iter;
  10401. struct map_session_data* sd;
  10402. iter = mapit_getallusers();
  10403. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10404. if( sd->expiration_time )
  10405. pc_expire_check(sd);
  10406. mapit_free(iter);
  10407. return 0;
  10408. }
  10409. void pc_expire_check(struct map_session_data *sd) {
  10410. /* ongoing timer */
  10411. if( sd->expiration_tid != INVALID_TIMER )
  10412. return;
  10413. /* not within the next 24h, enable the global check */
  10414. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10415. /* global check not running, enable */
  10416. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10417. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10418. return;
  10419. }
  10420. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10421. }
  10422. /**
  10423. * Deposit some money to bank
  10424. * @param sd
  10425. * @param money Amount of money to deposit
  10426. **/
  10427. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10428. unsigned int limit_check = money + sd->bank_vault;
  10429. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10430. return BDA_OVERFLOW;
  10431. } else if ( money > sd->status.zeny ) {
  10432. return BDA_NO_MONEY;
  10433. }
  10434. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10435. return BDA_NO_MONEY;
  10436. sd->bank_vault += money;
  10437. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10438. if( save_settings&CHARSAVE_BANK )
  10439. chrif_save(sd, CSAVE_NORMAL);
  10440. return BDA_SUCCESS;
  10441. }
  10442. /**
  10443. * Withdraw money from bank
  10444. * @param sd
  10445. * @param money Amount of money that will be withdrawn
  10446. **/
  10447. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10448. unsigned int limit_check = money + sd->status.zeny;
  10449. if( money <= 0 ) {
  10450. return BWA_UNKNOWN_ERROR;
  10451. } else if ( money > sd->bank_vault ) {
  10452. return BWA_NO_MONEY;
  10453. } else if ( limit_check > MAX_ZENY ) {
  10454. /* no official response for this scenario exists. */
  10455. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10456. return BWA_UNKNOWN_ERROR;
  10457. }
  10458. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10459. return BWA_NO_MONEY;
  10460. sd->bank_vault -= money;
  10461. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10462. if( save_settings&CHARSAVE_BANK )
  10463. chrif_save(sd, CSAVE_NORMAL);
  10464. return BWA_SUCCESS;
  10465. }
  10466. /**
  10467. * Clear Crimson Marker data from caster
  10468. * @param sd: Player
  10469. **/
  10470. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10471. uint8 i;
  10472. if (!sd)
  10473. return;
  10474. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10475. struct block_list *bl = NULL;
  10476. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10477. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10478. sd->c_marker[i] = 0;
  10479. }
  10480. }
  10481. /**
  10482. * Show version to player
  10483. * @param sd: Player
  10484. **/
  10485. void pc_show_version(struct map_session_data *sd) {
  10486. const char* svn = get_svn_revision();
  10487. char buf[CHAT_SIZE_MAX];
  10488. if( svn[0] != UNKNOWN_VERSION )
  10489. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10490. else {
  10491. const char* git = get_git_hash();
  10492. if( git[0] != UNKNOWN_VERSION )
  10493. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10494. else
  10495. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10496. }
  10497. clif_displaymessage(sd->fd,buf);
  10498. }
  10499. /**
  10500. * Run bonus_script on player
  10501. * @param sd
  10502. * @author [Cydh]
  10503. **/
  10504. void pc_bonus_script(struct map_session_data *sd) {
  10505. int now = gettick();
  10506. struct linkdb_node *node = NULL, *next = NULL;
  10507. if (!sd || !(node = sd->bonus_script.head))
  10508. return;
  10509. while (node) {
  10510. struct s_bonus_script_entry *entry = NULL;
  10511. next = node->next;
  10512. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10513. // Only start timer for new bonus_script
  10514. if (entry->tid == INVALID_TIMER) {
  10515. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  10516. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10517. entry->tick += now;
  10518. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10519. }
  10520. if (entry->script)
  10521. run_script(entry->script, 0, sd->bl.id, 0);
  10522. else
  10523. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10524. }
  10525. node = next;
  10526. }
  10527. }
  10528. /**
  10529. * Add bonus_script to player
  10530. * @param sd Player
  10531. * @param script_str Script string
  10532. * @param dur Duration in ms
  10533. * @param icon EFST
  10534. * @param flag Flags @see enum e_bonus_script_flags
  10535. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10536. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10537. * @author [Cydh]
  10538. **/
  10539. 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) {
  10540. struct script_code *script = NULL;
  10541. struct linkdb_node *node = NULL;
  10542. struct s_bonus_script_entry *entry = NULL;
  10543. if (!sd)
  10544. return NULL;
  10545. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10546. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10547. return NULL;
  10548. }
  10549. // Duplication checks
  10550. if ((node = sd->bonus_script.head)) {
  10551. while (node) {
  10552. entry = (struct s_bonus_script_entry *)node->data;
  10553. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10554. int newdur = gettick() + dur;
  10555. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10556. settick_timer(entry->tid, newdur);
  10557. script_free_code(script);
  10558. return NULL;
  10559. }
  10560. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10561. break;
  10562. else { // No duplicate bonus
  10563. script_free_code(script);
  10564. return NULL;
  10565. }
  10566. }
  10567. node = node->next;
  10568. }
  10569. }
  10570. CREATE(entry, struct s_bonus_script_entry, 1);
  10571. entry->script_buf = StringBuf_Malloc();
  10572. StringBuf_AppendStr(entry->script_buf, script_str);
  10573. entry->tid = INVALID_TIMER;
  10574. entry->flag = flag;
  10575. entry->icon = icon;
  10576. entry->tick = dur; // Use duration first, on run change to expire time
  10577. entry->type = type;
  10578. entry->script = script;
  10579. sd->bonus_script.count++;
  10580. return entry;
  10581. }
  10582. /**
  10583. * Remove bonus_script data from player
  10584. * @param sd: Target player
  10585. * @param list: Bonus script entry from player
  10586. * @author [Cydh]
  10587. **/
  10588. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10589. if (entry->tid != INVALID_TIMER)
  10590. delete_timer(entry->tid, pc_bonus_script_timer);
  10591. if (entry->script)
  10592. script_free_code(entry->script);
  10593. if (entry->script_buf)
  10594. StringBuf_Free(entry->script_buf);
  10595. if (sd) {
  10596. if (entry->icon != EFST_BLANK)
  10597. clif_status_load(&sd->bl, entry->icon, 0);
  10598. if (sd->bonus_script.count > 0)
  10599. sd->bonus_script.count--;
  10600. }
  10601. aFree(entry);
  10602. }
  10603. /**
  10604. * Do final process if no entry left
  10605. * @param sd
  10606. **/
  10607. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  10608. if (sd->bonus_script.count == 0) {
  10609. if (sd->bonus_script.head && sd->bonus_script.head->data)
  10610. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  10611. linkdb_final(&sd->bonus_script.head);
  10612. }
  10613. }
  10614. /**
  10615. * Timer for bonus_script
  10616. * @param tid
  10617. * @param tick
  10618. * @param id
  10619. * @param data
  10620. * @author [Cydh]
  10621. **/
  10622. int pc_bonus_script_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10623. struct map_session_data *sd;
  10624. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  10625. sd = map_id2sd(id);
  10626. if (!sd) {
  10627. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  10628. return 0;
  10629. }
  10630. if (tid == INVALID_TIMER)
  10631. return 0;
  10632. if (!sd->bonus_script.head || entry == NULL) {
  10633. ShowError("pc_bonus_script_timer: Invalid entry pointer 0x%08X!\n", entry);
  10634. return 0;
  10635. }
  10636. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10637. pc_bonus_script_free_entry(sd, entry);
  10638. pc_bonus_script_check_final(sd);
  10639. status_calc_pc(sd,SCO_NONE);
  10640. return 0;
  10641. }
  10642. /**
  10643. * Check then clear all active timer(s) of bonus_script data from player based on reason
  10644. * @param sd: Target player
  10645. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  10646. * @author [Cydh]
  10647. **/
  10648. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  10649. struct linkdb_node *node = NULL;
  10650. uint16 count = 0;
  10651. if (!sd || !(node = sd->bonus_script.head))
  10652. return;
  10653. while (node) {
  10654. struct linkdb_node *next = node->next;
  10655. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  10656. if (entry && (
  10657. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  10658. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  10659. (flag&entry->flag) || // Matched flag
  10660. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  10661. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  10662. )
  10663. )
  10664. {
  10665. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10666. pc_bonus_script_free_entry(sd, entry);
  10667. count++;
  10668. }
  10669. node = next;
  10670. }
  10671. pc_bonus_script_check_final(sd);
  10672. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  10673. status_calc_pc(sd,SCO_NONE);
  10674. }
  10675. /** [Cydh]
  10676. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  10677. * @param sd: Target player
  10678. */
  10679. void pc_cell_basilica(struct map_session_data *sd) {
  10680. nullpo_retv(sd);
  10681. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  10682. if (&sd->sc && sd->sc.data[SC_BASILICA])
  10683. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  10684. }
  10685. else if (!(&sd->sc) || !sd->sc.data[SC_BASILICA])
  10686. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,-1);
  10687. }
  10688. /** [Cydh]
  10689. * Get maximum specified parameter for specified class
  10690. * @param class_: sd->class
  10691. * @param sex: sd->status.sex
  10692. * @param flag: parameter will be checked
  10693. * @return max_param
  10694. */
  10695. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  10696. int idx = -1, class_ = sd->class_;
  10697. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  10698. short max_param = 0;
  10699. switch (param) {
  10700. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  10701. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  10702. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  10703. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  10704. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  10705. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  10706. }
  10707. if (max_param > 0)
  10708. return max_param;
  10709. }
  10710. return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
  10711. ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  10712. ((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)) :
  10713. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  10714. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  10715. }
  10716. /**
  10717. * Get max ASPD for player based on Class
  10718. * @param sd Player
  10719. * @return ASPD
  10720. */
  10721. short pc_maxaspd(struct map_session_data *sd) {
  10722. nullpo_ret(sd);
  10723. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  10724. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  10725. battle_config.max_aspd ));
  10726. }
  10727. /**
  10728. * Calculates total item-group related bonuses for the given item
  10729. * @param sd Player
  10730. * @param nameid Item ID
  10731. * @return Heal rate
  10732. **/
  10733. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  10734. short bonus = 0;
  10735. uint8 i;
  10736. if (!sd->itemgrouphealrate_count)
  10737. return bonus;
  10738. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10739. uint16 group_id = sd->itemgrouphealrate[i]->group_id, j;
  10740. struct s_item_group_db *group = NULL;
  10741. if (!group_id || !(group = itemdb_group_exists(group_id)))
  10742. continue;
  10743. for (j = 0; j < group->random[0].data_qty; j++) {
  10744. if (group->random[0].data[j].nameid == nameid) {
  10745. bonus += sd->itemgrouphealrate[i]->rate;
  10746. break;
  10747. }
  10748. }
  10749. }
  10750. return bonus;
  10751. }
  10752. /**
  10753. * Calculates total item-group related bonuses for the given item group
  10754. * @param sd Player
  10755. * @param group_id Item Group ID
  10756. * @return Heal rate
  10757. **/
  10758. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  10759. short bonus = 0;
  10760. uint8 i;
  10761. if (!sd->itemgrouphealrate_count)
  10762. return bonus;
  10763. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10764. if (sd->itemgrouphealrate[i]->group_id == group_id)
  10765. return sd->itemgrouphealrate[i]->rate;
  10766. }
  10767. return bonus;
  10768. }
  10769. /**
  10770. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  10771. * @param eqi Item EQI of enum equip_index
  10772. * @param *equip_index Player's equip_index[]
  10773. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  10774. * @return True if item in same inventory index, False if doesn't
  10775. */
  10776. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  10777. if (index < 0 || index >= MAX_INVENTORY)
  10778. return true;
  10779. // Dual weapon checks
  10780. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  10781. return true;
  10782. // Headgear with Mid & Low location
  10783. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  10784. return true;
  10785. // Headgear with Top & Mid or Low location
  10786. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  10787. return true;
  10788. // Headgear with Mid & Low location
  10789. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  10790. return true;
  10791. // Headgear with Top & Mid or Low location
  10792. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  10793. return true;
  10794. return false;
  10795. }
  10796. /**
  10797. * Generate Unique item ID for player
  10798. * @param sd : Player
  10799. * @return A generated Unique item ID
  10800. */
  10801. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  10802. nullpo_ret(sd);
  10803. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  10804. }
  10805. /**
  10806. * Validating skill from player after logged on
  10807. * @param sd
  10808. **/
  10809. void pc_validate_skill(struct map_session_data *sd) {
  10810. if (sd) {
  10811. uint16 i = 0, count = 0;
  10812. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  10813. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  10814. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  10815. for (i = 0; i < MAX_SKILL; i++) {
  10816. uint16 idx = 0;
  10817. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  10818. continue;
  10819. if ((idx = skill_get_index(tmp_skills[i].id))) {
  10820. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  10821. count++;
  10822. }
  10823. else
  10824. 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);
  10825. }
  10826. }
  10827. }
  10828. /**
  10829. * Toggle to remember if the questinfo is displayed yet or not.
  10830. * @param qi_display Display flag
  10831. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  10832. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  10833. **/
  10834. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct questinfo *qi, bool show) {
  10835. if (show) {
  10836. // Check if need to be displayed
  10837. if ((*qi_display) != 1) {
  10838. (*qi_display) = 1;
  10839. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  10840. }
  10841. }
  10842. else {
  10843. // Check if need to be hide
  10844. if ((*qi_display) != 0) {
  10845. (*qi_display) = 0;
  10846. #if PACKETVER >= 20120410
  10847. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  10848. #else
  10849. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  10850. #endif
  10851. }
  10852. }
  10853. }
  10854. /**
  10855. * Show available NPC Quest / Event Icon Check [Kisuka]
  10856. * @param sd Player
  10857. **/
  10858. void pc_show_questinfo(struct map_session_data *sd) {
  10859. #if PACKETVER >= 20090218
  10860. struct questinfo *qi = NULL;
  10861. unsigned short i;
  10862. uint8 j;
  10863. int8 mystate = 0;
  10864. bool failed = false;
  10865. nullpo_retv(sd);
  10866. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10867. return;
  10868. if (!map[sd->bl.m].qi_count || !map[sd->bl.m].qi_data)
  10869. return;
  10870. if (map[sd->bl.m].qi_count != sd->qi_count)
  10871. return; // init was not called yet
  10872. for(i = 0; i < map[sd->bl.m].qi_count; i++) {
  10873. qi = &map[sd->bl.m].qi_data[i];
  10874. if (!qi)
  10875. continue;
  10876. if (quest_check(sd, qi->quest_id, HAVEQUEST) != -1) { // Check if quest is not started
  10877. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10878. continue;
  10879. }
  10880. // Level range checks
  10881. if (sd->status.base_level < qi->min_level || sd->status.base_level > qi->max_level) {
  10882. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10883. continue;
  10884. }
  10885. // Quest requirements
  10886. if (qi->req_count) {
  10887. failed = false;
  10888. for (j = 0; j < qi->req_count; j++) {
  10889. mystate = quest_check(sd, qi->req[j].quest_id, HAVEQUEST);
  10890. mystate = mystate + (mystate < 1);
  10891. if (mystate != qi->req[j].state) {
  10892. failed = true;
  10893. break;
  10894. }
  10895. }
  10896. if (failed) {
  10897. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10898. continue;
  10899. }
  10900. }
  10901. // Job requirements
  10902. if (qi->jobid_count) {
  10903. failed = true;
  10904. for (j = 0; j < qi->jobid_count; j++) {
  10905. if (pc_mapid2jobid(sd->class_,sd->status.sex) == qi->jobid[j]) {
  10906. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10907. failed = false;
  10908. break;
  10909. }
  10910. }
  10911. if (!failed)
  10912. continue;
  10913. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10914. }
  10915. else {
  10916. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10917. }
  10918. }
  10919. #endif
  10920. }
  10921. /**
  10922. * Reinit the questinfo for player when changing map
  10923. * @param sd Player
  10924. **/
  10925. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  10926. #if PACKETVER >= 20090218
  10927. nullpo_retv(sd);
  10928. if (sd->qi_display) {
  10929. aFree(sd->qi_display);
  10930. sd->qi_display = NULL;
  10931. }
  10932. sd->qi_count = 0;
  10933. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10934. return;
  10935. if (!map[sd->bl.m].qi_count || !map[sd->bl.m].qi_data)
  10936. return;
  10937. CREATE(sd->qi_display, bool, (sd->qi_count = map[sd->bl.m].qi_count));
  10938. #endif
  10939. }
  10940. /**
  10941. * Check if a job is allowed to enter the map
  10942. * @param jobid Job ID see enum e_job or sd->status.class_
  10943. * @param m ID -an index- for direct indexing map[] array
  10944. * @return 1 if job is allowed, 0 otherwise
  10945. **/
  10946. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  10947. uint16 idx = 0;
  10948. // Map is other map server.
  10949. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  10950. if (m < 0)
  10951. return true;
  10952. if (m >= MAX_MAP_PER_SERVER || !map[m].cell)
  10953. return false;
  10954. if (!pcdb_checkid(jobid))
  10955. return false;
  10956. idx = pc_class2idx(jobid);
  10957. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  10958. return true;
  10959. if ((!map_flag_vs2(m) && job_info[idx].noenter_map.zone&1) || // Normal
  10960. (map[m].flag.pvp && job_info[idx].noenter_map.zone&2) || // PVP
  10961. (map_flag_gvg2_no_te(m) && job_info[idx].noenter_map.zone&4) || // GVG
  10962. (map[m].flag.battleground && job_info[idx].noenter_map.zone&8) || // Battleground
  10963. (map_flag_gvg2_te(m) && job_info[idx].noenter_map.zone&16) || // WOE:TE
  10964. (map[m].flag.restricted && job_info[idx].noenter_map.zone&(8*map[m].zone)) // Zone restriction
  10965. )
  10966. return false;
  10967. return true;
  10968. }
  10969. /**
  10970. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  10971. * @param sd
  10972. **/
  10973. void pc_set_costume_view(struct map_session_data *sd) {
  10974. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  10975. struct item_data *id = NULL;
  10976. nullpo_retv(sd);
  10977. head_low = sd->status.head_bottom;
  10978. head_mid = sd->status.head_mid;
  10979. head_top = sd->status.head_top;
  10980. robe = sd->status.robe;
  10981. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  10982. //Added check to prevent sending the same look on multiple slots ->
  10983. //causes client to redraw item on top of itself. (suggested by Lupus)
  10984. // Normal headgear checks
  10985. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  10986. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  10987. sd->status.head_bottom = id->look;
  10988. else
  10989. sd->status.head_bottom = 0;
  10990. }
  10991. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  10992. if (!(id->equip&(EQP_HEAD_TOP)))
  10993. sd->status.head_mid = id->look;
  10994. else
  10995. sd->status.head_mid = 0;
  10996. }
  10997. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  10998. sd->status.head_top = id->look;
  10999. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11000. sd->status.robe = id->look;
  11001. // Costumes check
  11002. if (!map[sd->bl.m].flag.nocostume) {
  11003. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11004. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11005. sd->status.head_bottom = id->look;
  11006. else
  11007. sd->status.head_bottom = 0;
  11008. }
  11009. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11010. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11011. sd->status.head_mid = id->look;
  11012. else
  11013. sd->status.head_mid = 0;
  11014. }
  11015. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11016. sd->status.head_top = id->look;
  11017. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11018. sd->status.robe = id->look;
  11019. }
  11020. if (sd->setlook_head_bottom)
  11021. sd->status.head_bottom = sd->setlook_head_bottom;
  11022. if (sd->setlook_head_mid)
  11023. sd->status.head_mid = sd->setlook_head_mid;
  11024. if (sd->setlook_head_top)
  11025. sd->status.head_top = sd->setlook_head_top;
  11026. if (sd->setlook_robe)
  11027. sd->status.robe = sd->setlook_robe;
  11028. if (head_low != sd->status.head_bottom)
  11029. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11030. if (head_mid != sd->status.head_mid)
  11031. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11032. if (head_top != sd->status.head_top)
  11033. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11034. if (robe != sd->status.robe)
  11035. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11036. }
  11037. /*==========================================
  11038. * pc Init/Terminate
  11039. *------------------------------------------*/
  11040. void do_final_pc(void) {
  11041. db_destroy(itemcd_db);
  11042. do_final_pc_groups();
  11043. ers_destroy(pc_sc_display_ers);
  11044. ers_destroy(pc_itemgrouphealrate_ers);
  11045. ers_destroy(num_reg_ers);
  11046. ers_destroy(str_reg_ers);
  11047. }
  11048. void do_init_pc(void) {
  11049. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11050. pc_readdb();
  11051. pc_read_motd(); // Read MOTD [Valaris]
  11052. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11053. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11054. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11055. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11056. add_timer_func_list(pc_autosave, "pc_autosave");
  11057. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11058. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11059. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11060. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11061. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11062. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11063. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11064. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11065. // 0=day, 1=night [Yor]
  11066. night_flag = battle_config.night_at_start ? 1 : 0;
  11067. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11068. int day_duration = battle_config.day_duration;
  11069. int night_duration = battle_config.night_duration;
  11070. // add night/day timer [Yor]
  11071. add_timer_func_list(map_day_timer, "map_day_timer");
  11072. add_timer_func_list(map_night_timer, "map_night_timer");
  11073. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11074. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11075. }
  11076. do_init_pc_groups();
  11077. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11078. pc_itemgrouphealrate_ers = ers_new(sizeof(struct s_pc_itemgrouphealrate), "pc.cpp:pc_itemgrouphealrate_ers", ERS_OPT_NONE);
  11079. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11080. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11081. ers_chunk_size(pc_sc_display_ers, 150);
  11082. ers_chunk_size(num_reg_ers, 300);
  11083. ers_chunk_size(str_reg_ers, 50);
  11084. }