pc.c 355 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/core.h" // get_svn_revision()
  5. #include "../common/malloc.h"
  6. #include "../common/nullpo.h"
  7. #include "../common/random.h"
  8. #include "../common/showmsg.h"
  9. #include "../common/socket.h" // session[]
  10. #include "../common/strlib.h" // safestrncpy()
  11. #include "../common/timer.h"
  12. #include "../common/utils.h"
  13. #include "../common/mmo.h" //NAME_LENGTH
  14. #include "atcommand.h" // get_atcommand_level()
  15. #include "map.h"
  16. #include "battle.h" // battle_config
  17. #include "battleground.h"
  18. #include "channel.h"
  19. #include "chat.h"
  20. #include "chrif.h"
  21. #include "date.h" // is_day_of_*()
  22. #include "duel.h"
  23. #include "intif.h"
  24. #include "homunculus.h"
  25. #include "instance.h"
  26. #include "mercenary.h"
  27. #include "elemental.h"
  28. #include "pet.h" // pet_unlocktarget()
  29. #include "party.h" // party_search()
  30. #include "storage.h"
  31. #include <stdlib.h>
  32. #include <math.h>
  33. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  34. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  35. static unsigned int statp[MAX_LEVEL+1];
  36. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  37. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  38. #endif
  39. // h-files are for declarations, not for implementations... [Shinomori]
  40. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  41. // timer for night.day implementation
  42. int day_timer_tid = INVALID_TIMER;
  43. int night_timer_tid = INVALID_TIMER;
  44. struct eri *pc_sc_display_ers = NULL;
  45. struct eri *pc_itemgrouphealrate_ers = NULL;
  46. int pc_expiration_tid = INVALID_TIMER;
  47. struct fame_list smith_fame_list[MAX_FAME_LIST];
  48. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  49. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  50. static unsigned int equip_pos[EQI_MAX]={EQP_ACC_L,EQP_ACC_R,EQP_SHOES,EQP_GARMENT,EQP_HEAD_LOW,EQP_HEAD_MID,EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_COSTUME_HEAD_TOP,EQP_COSTUME_HEAD_MID,EQP_COSTUME_HEAD_LOW,EQP_COSTUME_GARMENT,EQP_AMMO,EQP_SHADOW_ARMOR,EQP_SHADOW_WEAPON,EQP_SHADOW_SHIELD,EQP_SHADOW_SHOES,EQP_SHADOW_ACC_R,EQP_SHADOW_ACC_L};
  51. #define MOTD_LINE_SIZE 128
  52. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  53. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  54. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  55. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  56. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  57. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  58. };
  59. /**
  60. * Item Cool Down Delay Saving
  61. * Struct item_cd is not a member of struct map_session_data
  62. * to keep cooldowns in memory between player log-ins.
  63. * All cooldowns are reset when server is restarted.
  64. **/
  65. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  66. struct item_cd {
  67. unsigned int tick[MAX_ITEMDELAYS]; //tick
  68. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  69. };
  70. /**
  71. * Converts a class to its array index for CLASS_COUNT defined arrays.
  72. * Note that it does not do a validity check for speed purposes, where parsing
  73. * player input make sure to use a pcdb_checkid first!
  74. * @param class_
  75. * @return Class Index
  76. */
  77. int pc_class2idx(int class_) {
  78. if (class_ >= JOB_NOVICE_HIGH)
  79. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  80. return class_;
  81. }
  82. /**
  83. * Get player's group ID
  84. * @param sd
  85. * @return Group ID
  86. */
  87. inline int pc_get_group_id(struct map_session_data *sd) {
  88. return sd->group_id;
  89. }
  90. /** Get player's group Level
  91. * @param sd
  92. * @return Group Level
  93. */
  94. inline int pc_get_group_level(struct map_session_data *sd) {
  95. return sd->group_level;
  96. }
  97. static int pc_invincible_timer(int tid, unsigned int tick, int id, intptr_t data)
  98. {
  99. struct map_session_data *sd;
  100. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  101. return 1;
  102. if(sd->invincible_timer != tid){
  103. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  104. return 0;
  105. }
  106. sd->invincible_timer = INVALID_TIMER;
  107. skill_unit_move(&sd->bl,tick,1);
  108. return 0;
  109. }
  110. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  111. nullpo_retv(sd);
  112. if( sd->invincible_timer != INVALID_TIMER )
  113. delete_timer(sd->invincible_timer,pc_invincible_timer);
  114. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  115. }
  116. void pc_delinvincibletimer(struct map_session_data* sd)
  117. {
  118. nullpo_retv(sd);
  119. if( sd->invincible_timer != INVALID_TIMER )
  120. {
  121. delete_timer(sd->invincible_timer,pc_invincible_timer);
  122. sd->invincible_timer = INVALID_TIMER;
  123. skill_unit_move(&sd->bl,gettick(),1);
  124. }
  125. }
  126. static int pc_spiritball_timer(int tid, unsigned int tick, int id, intptr_t data)
  127. {
  128. struct map_session_data *sd;
  129. int i;
  130. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  131. return 1;
  132. if( sd->spiritball <= 0 )
  133. {
  134. 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);
  135. sd->spiritball = 0;
  136. return 0;
  137. }
  138. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  139. if( i == sd->spiritball )
  140. {
  141. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  142. return 0;
  143. }
  144. sd->spiritball--;
  145. if( i != sd->spiritball )
  146. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  147. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  148. clif_spiritball(&sd->bl);
  149. return 0;
  150. }
  151. /**
  152. * Adds a spiritball to player for 'interval' ms
  153. * @param sd
  154. * @param interval
  155. * @param max
  156. */
  157. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  158. {
  159. int tid;
  160. uint8 i;
  161. nullpo_retv(sd);
  162. if(max > MAX_SPIRITBALL)
  163. max = MAX_SPIRITBALL;
  164. if(sd->spiritball < 0)
  165. sd->spiritball = 0;
  166. if( sd->spiritball && sd->spiritball >= max )
  167. {
  168. if(sd->spirit_timer[0] != INVALID_TIMER)
  169. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  170. sd->spiritball--;
  171. if( sd->spiritball != 0 )
  172. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  173. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  174. }
  175. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  176. 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);
  177. if( i != sd->spiritball )
  178. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  179. sd->spirit_timer[i] = tid;
  180. sd->spiritball++;
  181. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  182. clif_millenniumshield(&sd->bl,sd->spiritball);
  183. else
  184. clif_spiritball(&sd->bl);
  185. }
  186. /**
  187. * Removes number of spiritball from player
  188. * @param sd
  189. * @param count
  190. * @param type 1 = doesn't give client effect
  191. */
  192. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  193. {
  194. uint8 i;
  195. nullpo_retv(sd);
  196. if(sd->spiritball <= 0) {
  197. sd->spiritball = 0;
  198. return;
  199. }
  200. if(count == 0)
  201. return;
  202. if(count > sd->spiritball)
  203. count = sd->spiritball;
  204. sd->spiritball -= count;
  205. if(count > MAX_SPIRITBALL)
  206. count = MAX_SPIRITBALL;
  207. for(i=0;i<count;i++) {
  208. if(sd->spirit_timer[i] != INVALID_TIMER) {
  209. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  210. sd->spirit_timer[i] = INVALID_TIMER;
  211. }
  212. }
  213. for(i=count;i<MAX_SPIRITBALL;i++) {
  214. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  215. sd->spirit_timer[i] = INVALID_TIMER;
  216. }
  217. if(!type) {
  218. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  219. clif_millenniumshield(&sd->bl,sd->spiritball);
  220. else
  221. clif_spiritball(&sd->bl);
  222. }
  223. }
  224. static int pc_check_banding( struct block_list *bl, va_list ap ) {
  225. int *c, *b_sd;
  226. struct block_list *src;
  227. struct map_session_data *tsd;
  228. struct status_change *sc;
  229. nullpo_ret(bl);
  230. nullpo_ret(tsd = (struct map_session_data*)bl);
  231. nullpo_ret(src = va_arg(ap,struct block_list *));
  232. c = va_arg(ap,int *);
  233. b_sd = va_arg(ap, int *);
  234. if(pc_isdead(tsd))
  235. return 0;
  236. sc = status_get_sc(bl);
  237. if( bl == src )
  238. return 0;
  239. if( sc && sc->data[SC_BANDING] )
  240. {
  241. b_sd[(*c)++] = tsd->bl.id;
  242. return 1;
  243. }
  244. return 0;
  245. }
  246. int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
  247. int c;
  248. int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
  249. int i, j, hp, extra_hp = 0, tmp_qty = 0;
  250. int range = skill_get_splash(LG_BANDING,skill_lv);
  251. nullpo_ret(sd);
  252. c = 0;
  253. memset(b_sd, 0, sizeof(b_sd));
  254. i = party_foreachsamemap(pc_check_banding,sd,range,&sd->bl,&c,&b_sd);
  255. if( c < 1 ) //just recalc status no need to recalc hp
  256. { // No more Royal Guards in Banding found.
  257. struct status_change *sc;
  258. if( (sc = status_get_sc(&sd->bl)) != NULL && sc->data[SC_BANDING] )
  259. {
  260. sc->data[SC_BANDING]->val2 = 0; // Reset the counter
  261. status_calc_bl(&sd->bl, status_sc2scb_flag(SC_BANDING));
  262. }
  263. return 0;
  264. }
  265. //Add yourself
  266. hp = status_get_hp(&sd->bl);
  267. i++;
  268. // Get total HP of all Royal Guards in party.
  269. for( j = 0; j < i; j++ ){
  270. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  271. if( bsd != NULL )
  272. hp += status_get_hp(&bsd->bl);
  273. }
  274. // Set average HP.
  275. hp = hp / i;
  276. // If a Royal Guard have full HP, give more HP to others that haven't full HP.
  277. for( j = 0; j < i; j++ )
  278. {
  279. int tmp_hp=0;
  280. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  281. if( bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0 ){
  282. extra_hp += tmp_hp;
  283. tmp_qty++;
  284. }
  285. }
  286. if( extra_hp > 0 && tmp_qty > 0 )
  287. hp += extra_hp / tmp_qty;
  288. for( j = 0; j < i; j++ ){
  289. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  290. if( bsd != NULL )
  291. {
  292. struct status_change *sc;
  293. status_set_hp(&bsd->bl,hp,0); // Set hp
  294. if( (sc = status_get_sc(&bsd->bl)) != NULL && sc->data[SC_BANDING] )
  295. {
  296. sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
  297. status_calc_bl(&bsd->bl, status_sc2scb_flag(SC_BANDING)); // Set atk and def.
  298. }
  299. }
  300. }
  301. return c;
  302. }
  303. /**
  304. * Increases a player's fame points and displays a notice to him
  305. * @param sd Player
  306. * @param count Fame point
  307. */
  308. void pc_addfame(struct map_session_data *sd,int count)
  309. {
  310. int ranktype=-1;
  311. nullpo_retv(sd);
  312. sd->status.fame += count;
  313. if(sd->status.fame > MAX_FAME)
  314. sd->status.fame = MAX_FAME;
  315. switch(sd->class_&MAPID_UPPERMASK){
  316. case MAPID_BLACKSMITH: ranktype = 0; break;
  317. case MAPID_ALCHEMIST: ranktype = 1; break;
  318. case MAPID_TAEKWON: ranktype = 2; break;
  319. }
  320. clif_update_rankingpoint(sd,ranktype,count);
  321. chrif_updatefamelist(sd);
  322. }
  323. /**
  324. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  325. * @param sd
  326. * @param job Job use enum e_mapid
  327. * @return Rank
  328. */
  329. unsigned char pc_famerank(uint32 char_id, int job)
  330. {
  331. uint8 i;
  332. switch(job){
  333. case MAPID_BLACKSMITH: // Blacksmith
  334. for(i = 0; i < MAX_FAME_LIST; i++){
  335. if(smith_fame_list[i].id == char_id)
  336. return i + 1;
  337. }
  338. break;
  339. case MAPID_ALCHEMIST: // Alchemist
  340. for(i = 0; i < MAX_FAME_LIST; i++){
  341. if(chemist_fame_list[i].id == char_id)
  342. return i + 1;
  343. }
  344. break;
  345. case MAPID_TAEKWON: // Taekwon
  346. for(i = 0; i < MAX_FAME_LIST; i++){
  347. if(taekwon_fame_list[i].id == char_id)
  348. return i + 1;
  349. }
  350. break;
  351. }
  352. return 0;
  353. }
  354. /**
  355. * Restart player's HP & SP value
  356. * @param sd
  357. * @param type Restart type: 1 - Normal Resurection
  358. */
  359. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  360. struct status_data *status, *b_status;
  361. nullpo_retv(sd);
  362. b_status = &sd->base_status;
  363. status = &sd->battle_status;
  364. if (type&1) { //Normal resurrection
  365. status->hp = 1; //Otherwise status_heal may fail if dead.
  366. status_heal(&sd->bl, b_status->hp, 0, 1);
  367. if( status->sp < b_status->sp )
  368. status_set_sp(&sd->bl, b_status->sp, 1);
  369. } else { //Just for saving on the char-server (with values as if respawned)
  370. sd->status.hp = b_status->hp;
  371. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  372. }
  373. }
  374. /*==========================================
  375. Rental System
  376. *------------------------------------------*/
  377. /** Ends rental */
  378. static int pc_inventory_rental_end(int tid, unsigned int tick, int id, intptr_t data)
  379. {
  380. struct map_session_data *sd = map_id2sd(id);
  381. if( sd == NULL )
  382. return 0;
  383. if( tid != sd->rental_timer ) {
  384. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  385. return 0;
  386. }
  387. pc_inventory_rentals(sd);
  388. return 1;
  389. }
  390. void pc_inventory_rental_clear(struct map_session_data *sd)
  391. {
  392. if( sd->rental_timer != INVALID_TIMER ) {
  393. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  394. sd->rental_timer = INVALID_TIMER;
  395. }
  396. }
  397. /** Assumes I is valid (from default areas where it is called, it is) */
  398. void pc_rental_expire(struct map_session_data *sd, int i)
  399. {
  400. unsigned short nameid = sd->status.inventory[i].nameid;
  401. /* Soon to be dropped, we got plans to integrate it with item db */
  402. switch( nameid ) {
  403. case ITEMID_REINS_OF_MOUNT:
  404. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  405. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER);
  406. break;
  407. case ITEMID_LOVE_ANGEL:
  408. if( sd->status.font == 1 ) {
  409. sd->status.font = 0;
  410. clif_font(sd);
  411. }
  412. break;
  413. case ITEMID_SQUIRREL:
  414. if( sd->status.font == 2 ) {
  415. sd->status.font = 0;
  416. clif_font(sd);
  417. }
  418. break;
  419. case ITEMID_GOGO:
  420. if( sd->status.font == 3 ) {
  421. sd->status.font = 0;
  422. clif_font(sd);
  423. }
  424. break;
  425. case ITEMID_PICTURE_DIARY:
  426. if( sd->status.font == 4 ) {
  427. sd->status.font = 0;
  428. clif_font(sd);
  429. }
  430. break;
  431. case ITEMID_MINI_HEART:
  432. if( sd->status.font == 5 ) {
  433. sd->status.font = 0;
  434. clif_font(sd);
  435. }
  436. break;
  437. case ITEMID_NEWCOMER:
  438. if( sd->status.font == 6 ) {
  439. sd->status.font = 0;
  440. clif_font(sd);
  441. }
  442. break;
  443. case ITEMID_KID:
  444. if( sd->status.font == 7 ) {
  445. sd->status.font = 0;
  446. clif_font(sd);
  447. }
  448. break;
  449. case ITEMID_MAGIC_CASTLE:
  450. if( sd->status.font == 8 ) {
  451. sd->status.font = 0;
  452. clif_font(sd);
  453. }
  454. break;
  455. case ITEMID_BULGING_HEAD:
  456. if( sd->status.font == 9 ) {
  457. sd->status.font = 0;
  458. clif_font(sd);
  459. }
  460. break;
  461. }
  462. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  463. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  464. }
  465. void pc_inventory_rentals(struct map_session_data *sd)
  466. {
  467. int i, c = 0;
  468. unsigned int expire_tick, next_tick = UINT_MAX;
  469. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  470. if( sd->status.inventory[i].nameid == 0 )
  471. continue; // Nothing here
  472. if( sd->status.inventory[i].expire_time == 0 )
  473. continue;
  474. if( sd->status.inventory[i].expire_time <= time(NULL) )
  475. pc_rental_expire(sd, i);
  476. else {
  477. expire_tick = (unsigned int)(sd->status.inventory[i].expire_time - time(NULL)) * 1000;
  478. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, (int)(expire_tick / 1000));
  479. next_tick = min(expire_tick, next_tick);
  480. c++;
  481. }
  482. }
  483. 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
  484. sd->rental_timer = add_timer(gettick() + min(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  485. else
  486. sd->rental_timer = INVALID_TIMER;
  487. }
  488. void pc_inventory_rental_add(struct map_session_data *sd, int seconds)
  489. {
  490. int tick = seconds * 1000;
  491. if( sd == NULL )
  492. return;
  493. if( sd->rental_timer != INVALID_TIMER ) {
  494. const struct TimerData * td;
  495. td = get_timer(sd->rental_timer);
  496. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  497. pc_inventory_rental_clear(sd);
  498. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  499. }
  500. } else
  501. sd->rental_timer = add_timer(gettick() + min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  502. }
  503. /**
  504. * Determines if player can give / drop / trade / vend items
  505. */
  506. bool pc_can_give_items(struct map_session_data *sd)
  507. {
  508. return pc_has_permission(sd, PC_PERM_TRADE);
  509. }
  510. /**
  511. * Determines if player can give / drop / trade / vend bounded items
  512. */
  513. bool pc_can_give_bounded_items(struct map_session_data *sd)
  514. {
  515. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  516. }
  517. /*==========================================
  518. * Prepares character for saving.
  519. * @param sd
  520. *------------------------------------------*/
  521. void pc_makesavestatus(struct map_session_data *sd)
  522. {
  523. nullpo_retv(sd);
  524. if(!battle_config.save_clothcolor)
  525. sd->status.clothes_color=0;
  526. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  527. //status change load/saving. [Skotlex]
  528. #ifdef NEW_CARTS
  529. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  530. #else
  531. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  532. #endif
  533. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  534. if(pc_isdead(sd)){
  535. pc_setrestartvalue(sd,0);
  536. } else {
  537. sd->status.hp = sd->battle_status.hp;
  538. sd->status.sp = sd->battle_status.sp;
  539. }
  540. sd->status.last_point.map = sd->mapindex;
  541. sd->status.last_point.x = sd->bl.x;
  542. sd->status.last_point.y = sd->bl.y;
  543. return;
  544. }
  545. if(pc_isdead(sd)){
  546. pc_setrestartvalue(sd,0);
  547. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  548. } else {
  549. sd->status.hp = sd->battle_status.hp;
  550. sd->status.sp = sd->battle_status.sp;
  551. sd->status.last_point.map = sd->mapindex;
  552. sd->status.last_point.x = sd->bl.x;
  553. sd->status.last_point.y = sd->bl.y;
  554. }
  555. if(map[sd->bl.m].flag.nosave){
  556. struct map_data *m=&map[sd->bl.m];
  557. if(m->save.map)
  558. memcpy(&sd->status.last_point,&m->save,sizeof(sd->status.last_point));
  559. else
  560. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  561. }
  562. }
  563. /*==========================================
  564. * Off init ? Connection?
  565. *------------------------------------------*/
  566. 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)
  567. {
  568. nullpo_retv(sd);
  569. sd->bl.id = account_id;
  570. sd->status.account_id = account_id;
  571. sd->status.char_id = char_id;
  572. sd->status.sex = sex;
  573. sd->login_id1 = login_id1;
  574. sd->login_id2 = 0; // at this point, we can not know the value :(
  575. sd->client_tick = client_tick;
  576. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  577. sd->bl.type = BL_PC;
  578. sd->canlog_tick = gettick();
  579. //Required to prevent homunculus copuing a base speed of 0.
  580. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  581. }
  582. /**
  583. * Get equip point for an equip
  584. * @param sd
  585. * @param n Equip index in inventory
  586. */
  587. int pc_equippoint(struct map_session_data *sd,int n){
  588. int ep = 0;
  589. nullpo_ret(sd);
  590. if(!sd->inventory_data[n])
  591. return 0;
  592. if (!itemdb_isequip2(sd->inventory_data[n]))
  593. return 0; //Not equippable by players.
  594. ep = sd->inventory_data[n]->equip;
  595. if(sd->inventory_data[n]->look == W_DAGGER ||
  596. sd->inventory_data[n]->look == W_1HSWORD ||
  597. sd->inventory_data[n]->look == W_1HAXE) {
  598. if(ep == EQP_HAND_R && (pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN ||
  599. (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO))//Kagerou and Oboro can dual wield daggers. [Rytech]
  600. return EQP_ARMS;
  601. }
  602. return ep;
  603. }
  604. /**
  605. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  606. * @param sd : player session
  607. * @return 0 sucess, 1:invalid sd
  608. */
  609. void pc_setinventorydata(struct map_session_data *sd)
  610. {
  611. uint8 i;
  612. nullpo_retv(sd);
  613. for(i = 0; i < MAX_INVENTORY; i++) {
  614. unsigned short id = sd->status.inventory[i].nameid;
  615. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  616. }
  617. }
  618. /**
  619. * 'Calculates' weapon type
  620. * @param sd : player
  621. */
  622. void pc_calcweapontype(struct map_session_data *sd)
  623. {
  624. nullpo_retv(sd);
  625. // single-hand
  626. if(sd->weapontype2 == W_FIST) {
  627. sd->status.weapon = sd->weapontype1;
  628. return;
  629. }
  630. if(sd->weapontype1 == W_FIST) {
  631. sd->status.weapon = sd->weapontype2;
  632. return;
  633. }
  634. // dual-wield
  635. sd->status.weapon = 0;
  636. switch (sd->weapontype1){
  637. case W_DAGGER:
  638. switch (sd->weapontype2) {
  639. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  640. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  641. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  642. }
  643. break;
  644. case W_1HSWORD:
  645. switch (sd->weapontype2) {
  646. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  647. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  648. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  649. }
  650. break;
  651. case W_1HAXE:
  652. switch (sd->weapontype2) {
  653. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  654. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  655. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  656. }
  657. }
  658. // unknown, default to right hand type
  659. if (!sd->status.weapon)
  660. sd->status.weapon = sd->weapontype1;
  661. }
  662. /**
  663. * Set equip index
  664. * @param sd : Player
  665. */
  666. void pc_setequipindex(struct map_session_data *sd)
  667. {
  668. uint16 i;
  669. nullpo_retv(sd);
  670. for (i = 0; i < EQI_MAX; i++)
  671. sd->equip_index[i] = -1;
  672. for (i = 0; i < MAX_INVENTORY; i++) {
  673. if (sd->status.inventory[i].nameid <= 0)
  674. continue;
  675. if (sd->status.inventory[i].equip) {
  676. uint8 j;
  677. for (j = 0; j < EQI_MAX; j++)
  678. if (sd->status.inventory[i].equip & equip_pos[j])
  679. sd->equip_index[j] = i;
  680. if (sd->status.inventory[i].equip & EQP_HAND_R) {
  681. if (sd->inventory_data[i])
  682. sd->weapontype1 = sd->inventory_data[i]->look;
  683. else
  684. sd->weapontype1 = 0;
  685. }
  686. if( sd->status.inventory[i].equip & EQP_HAND_L ) {
  687. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  688. sd->weapontype2 = sd->inventory_data[i]->look;
  689. else
  690. sd->weapontype2 = 0;
  691. }
  692. }
  693. }
  694. pc_calcweapontype(sd);
  695. }
  696. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  697. //{
  698. // int i;
  699. // struct item *item = &sd->status.inventory[eqindex];
  700. // struct item_data *data;
  701. //
  702. // //Crafted/made/hatched items.
  703. // if (itemdb_isspecial(item->card[0]))
  704. // return 1;
  705. //
  706. // /* scan for enchant armor gems */
  707. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  708. // s = MAX_SLOTS - 1;
  709. //
  710. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  711. // return( i < s ) ? 0 : 1;
  712. //}
  713. /**
  714. * Check if an item is equiped by player
  715. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  716. * @param sd : player session
  717. * @param nameid : itemid
  718. * @return 1:yes, 0:no
  719. */
  720. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  721. {
  722. uint8 i;
  723. for( i = 0; i < EQI_MAX; i++ )
  724. {
  725. short index = sd->equip_index[i], j;
  726. if( index < 0 )
  727. continue;
  728. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  729. continue;
  730. if( !sd->inventory_data[index] )
  731. continue;
  732. if( sd->inventory_data[index]->nameid == nameid )
  733. return true;
  734. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  735. if( sd->status.inventory[index].card[j] == nameid )
  736. return true;
  737. }
  738. }
  739. return false;
  740. }
  741. /** Check adopt rule
  742. * @param p1_sd Player 1
  743. * @param p2_sd Player 2
  744. * @param b_sd Player that will be adopted
  745. * @return True - if can be adopted, False otherwise
  746. */
  747. bool pc_can_Adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd )
  748. {
  749. if( !p1_sd || !p2_sd || !b_sd )
  750. return false;
  751. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  752. return false; // already adopted baby / in adopt request
  753. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  754. return false; // You need to be married and in party with baby to adopt
  755. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  756. return false; // Not married, wrong married
  757. if( p2_sd->status.party_id != p1_sd->status.party_id )
  758. return false; // Both parents need to be in the same party
  759. // Parents need to have their ring equipped
  760. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  761. return false;
  762. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  763. return false;
  764. // Already adopted a baby
  765. if( p1_sd->status.child || p2_sd->status.child ) {
  766. clif_Adopt_reply(p1_sd, 0);
  767. return false;
  768. }
  769. // Parents need at least lvl 70 to adopt
  770. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  771. clif_Adopt_reply(p1_sd, 1);
  772. return false;
  773. }
  774. if( b_sd->status.partner_id ) {
  775. clif_Adopt_reply(p1_sd, 2);
  776. return false;
  777. }
  778. 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 ) )
  779. return false;
  780. return true;
  781. }
  782. /*==========================================
  783. * Adoption Process
  784. *------------------------------------------*/
  785. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  786. {
  787. int job, joblevel;
  788. unsigned int jobexp;
  789. if( !pc_can_Adopt(p1_sd, p2_sd, b_sd) )
  790. return false;
  791. // Preserve current job levels and progress
  792. joblevel = b_sd->status.job_level;
  793. jobexp = b_sd->status.job_exp;
  794. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  795. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  796. { // Success, proceed to configure parents and baby skills
  797. p1_sd->status.child = b_sd->status.char_id;
  798. p2_sd->status.child = b_sd->status.char_id;
  799. b_sd->status.father = p1_sd->status.char_id;
  800. b_sd->status.mother = p2_sd->status.char_id;
  801. // Restore progress
  802. b_sd->status.job_level = joblevel;
  803. clif_updatestatus(b_sd, SP_JOBLEVEL);
  804. b_sd->status.job_exp = jobexp;
  805. clif_updatestatus(b_sd, SP_JOBEXP);
  806. // Baby Skills
  807. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  808. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  809. // Parents Skills
  810. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  811. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  812. return true;
  813. }
  814. return false; // Job Change Fail
  815. }
  816. /*==========================================
  817. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  818. Returns:
  819. false : Cannot use/equip
  820. true : Can use/equip
  821. * Credits:
  822. [Inkfish] for first idea
  823. [Haru] for third-classes extension
  824. [Cydh] finishing :D
  825. *------------------------------------------*/
  826. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  827. while (1) {
  828. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  829. break;
  830. #ifndef RENEWAL
  831. //allow third classes to use trans. class items
  832. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  833. break;
  834. //third-baby classes can use same item too
  835. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  836. break;
  837. //don't need to decide specific rules for third-classes?
  838. //items for third classes can be used for all third classes
  839. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  840. break;
  841. #else
  842. //trans. classes (exl. third-trans.)
  843. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  844. break;
  845. //baby classes (exl. third-baby)
  846. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  847. break;
  848. //third classes (exl. third-trans. and baby-third)
  849. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  850. break;
  851. //trans-third classes
  852. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  853. break;
  854. //third-baby classes
  855. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  856. break;
  857. #endif
  858. return false;
  859. }
  860. return true;
  861. }
  862. /*=================================================
  863. * Checks if the player can equip the item at index n in inventory.
  864. * @param sd
  865. * @param n Item index in inventory
  866. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  867. *------------------------------------------------*/
  868. uint8 pc_isequip(struct map_session_data *sd,int n)
  869. {
  870. struct item_data *item;
  871. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  872. item = sd->inventory_data[n];
  873. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  874. return ITEM_EQUIP_ACK_OK;
  875. if(item == NULL)
  876. return ITEM_EQUIP_ACK_FAIL;
  877. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  878. return ITEM_EQUIP_ACK_FAILLEVEL;
  879. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  880. return ITEM_EQUIP_ACK_FAILLEVEL;
  881. if(item->sex != 2 && sd->status.sex != item->sex)
  882. return ITEM_EQUIP_ACK_FAIL;
  883. if (sd->sc.count) {
  884. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  885. return ITEM_EQUIP_ACK_FAIL;
  886. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  887. return ITEM_EQUIP_ACK_FAIL;
  888. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  889. return ITEM_EQUIP_ACK_FAIL;
  890. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  891. return ITEM_EQUIP_ACK_FAIL;
  892. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  893. return ITEM_EQUIP_ACK_FAIL;
  894. if(item->equip && sd->sc.data[SC_KYOUGAKU])
  895. return ITEM_EQUIP_ACK_FAIL;
  896. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  897. //Spirit of Super Novice equip bonuses. [Skotlex]
  898. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  899. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  900. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  901. switch(item->look) { //In weapons, the look determines type of weapon.
  902. case W_DAGGER: //All level 4 - Daggers
  903. case W_1HSWORD: //All level 4 - 1H Swords
  904. case W_1HAXE: //All level 4 - 1H Axes
  905. case W_MACE: //All level 4 - 1H Maces
  906. case W_STAFF: //All level 4 - 1H Staves
  907. case W_2HSTAFF: //All level 4 - 2H Staves
  908. return ITEM_EQUIP_ACK_OK;
  909. }
  910. }
  911. }
  912. //fail to equip if item is restricted
  913. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  914. return ITEM_EQUIP_ACK_FAIL;
  915. //Not equipable by class. [Skotlex]
  916. if (!(1<<(sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1)?1:((sd->class_&JOBL_2_2)?2:0)]))
  917. return ITEM_EQUIP_ACK_FAIL;
  918. if (!pc_isItemClass(sd,item))
  919. return ITEM_EQUIP_ACK_FAIL;
  920. return ITEM_EQUIP_ACK_OK;
  921. }
  922. /*==========================================
  923. * No problem with the session id
  924. * set the status that has been sent from char server
  925. *------------------------------------------*/
  926. 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)
  927. {
  928. int i;
  929. #ifdef BOUND_ITEMS
  930. int j;
  931. int idxlist[MAX_INVENTORY];
  932. #endif
  933. unsigned long tick = gettick();
  934. uint32 ip = session[sd->fd]->client_addr;
  935. sd->login_id2 = login_id2;
  936. sd->group_id = group_id;
  937. /* load user permissions */
  938. pc_group_pc_load(sd);
  939. memcpy(&sd->status, st, sizeof(*st));
  940. if (st->sex != sd->status.sex) {
  941. clif_authfail_fd(sd->fd, 0);
  942. return false;
  943. }
  944. //Set the map-server used job id. [Skotlex]
  945. i = pc_jobid2mapid(sd->status.class_);
  946. if (i == -1) { //Invalid class?
  947. 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);
  948. sd->status.class_ = JOB_NOVICE;
  949. sd->class_ = MAPID_NOVICE;
  950. } else
  951. sd->class_ = i;
  952. // Checks and fixes to character status data, that are required
  953. // in case of configuration change or stuff, which cannot be
  954. // checked on char-server.
  955. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  956. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  957. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  958. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  959. if(!sd->status.hp) pc_setdead(sd);
  960. sd->state.connect_new = 1;
  961. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  962. sd->invincible_timer = INVALID_TIMER;
  963. sd->npc_timer_id = INVALID_TIMER;
  964. sd->pvp_timer = INVALID_TIMER;
  965. sd->expiration_tid = INVALID_TIMER;
  966. sd->autotrade_tid = INVALID_TIMER;
  967. #ifdef SECURE_NPCTIMEOUT
  968. // Initialize to defaults/expected
  969. sd->npc_idle_timer = INVALID_TIMER;
  970. sd->npc_idle_tick = tick;
  971. sd->npc_idle_type = NPCT_INPUT;
  972. #endif
  973. sd->canuseitem_tick = tick;
  974. sd->canusecashfood_tick = tick;
  975. sd->canequip_tick = tick;
  976. sd->cantalk_tick = tick;
  977. sd->canskill_tick = tick;
  978. sd->cansendmail_tick = tick;
  979. sd->idletime = last_tick;
  980. for(i = 0; i < MAX_SPIRITBALL; i++)
  981. sd->spirit_timer[i] = INVALID_TIMER;
  982. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  983. sd->autobonus[i].active = INVALID_TIMER;
  984. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  985. sd->autobonus2[i].active = INVALID_TIMER;
  986. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  987. sd->autobonus3[i].active = INVALID_TIMER;
  988. if (battle_config.item_auto_get)
  989. sd->state.autoloot = 10000;
  990. if (battle_config.disp_experience)
  991. sd->state.showexp = 1;
  992. if (battle_config.disp_zeny)
  993. sd->state.showzeny = 1;
  994. #ifdef VIP_ENABLE
  995. if (!battle_config.vip_disp_rate)
  996. sd->disableshowrate = 1;
  997. #endif
  998. if (!(battle_config.display_skill_fail&2))
  999. sd->state.showdelay = 1;
  1000. pc_setinventorydata(sd);
  1001. pc_setequipindex(sd);
  1002. if( sd->status.option&OPTION_INVISIBLE && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1003. sd->status.option &= ~OPTION_INVISIBLE;
  1004. }
  1005. status_change_init(&sd->bl);
  1006. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1007. //Set here because we need the inventory data for weapon sprite parsing.
  1008. status_set_viewdata(&sd->bl, sd->status.class_);
  1009. unit_dataset(&sd->bl);
  1010. sd->guild_x = -1;
  1011. sd->guild_y = -1;
  1012. sd->delayed_damage = 0;
  1013. // Event Timers
  1014. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1015. sd->eventtimer[i] = INVALID_TIMER;
  1016. // Rental Timer
  1017. sd->rental_timer = INVALID_TIMER;
  1018. for( i = 0; i < 3; i++ )
  1019. sd->hate_mob[i] = -1;
  1020. sd->quest_log = NULL;
  1021. sd->num_quests = 0;
  1022. sd->avail_quests = 0;
  1023. sd->save_quest = false;
  1024. sd->count_rewarp = 0;
  1025. //warp player
  1026. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != 0) {
  1027. 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);
  1028. // try warping to a default map instead (church graveyard)
  1029. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != 0) {
  1030. // if we fail again
  1031. clif_authfail_fd(sd->fd, 0);
  1032. return false;
  1033. }
  1034. }
  1035. clif_authok(sd);
  1036. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1037. sd->die_counter=-1;
  1038. //display login notice
  1039. ShowInfo("'"CL_WHITE"%s"CL_RESET"' logged in."
  1040. " (AID/CID: '"CL_WHITE"%d/%d"CL_RESET"',"
  1041. " Packet Ver: '"CL_WHITE"%d"CL_RESET"', IP: '"CL_WHITE"%d.%d.%d.%d"CL_RESET"',"
  1042. " Group '"CL_WHITE"%d"CL_RESET"').\n",
  1043. sd->status.name, sd->status.account_id, sd->status.char_id,
  1044. sd->packet_ver, CONVIP(ip), sd->group_id);
  1045. // Send friends list
  1046. clif_friendslist_send(sd);
  1047. if( !changing_mapservers ) {
  1048. if (battle_config.display_version == 1)
  1049. pc_show_version(sd);
  1050. // Message of the Day [Valaris]
  1051. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1052. if (battle_config.motd_type)
  1053. clif_disp_onlyself(sd,motd_text[i],strlen(motd_text[i]));
  1054. else
  1055. clif_displaymessage(sd->fd, motd_text[i]);
  1056. }
  1057. if (expiration_time != 0)
  1058. sd->expiration_time = expiration_time;
  1059. /**
  1060. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1061. **/
  1062. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1063. }
  1064. /**
  1065. * Check if player have any item cooldowns on
  1066. **/
  1067. pc_itemcd_do(sd,true);
  1068. pc_validate_skill(sd);
  1069. #ifdef BOUND_ITEMS
  1070. // Party bound item check
  1071. if(sd->status.party_id == 0 && (j = pc_bound_chk(sd,3,idxlist))) { // Party was deleted while character offline
  1072. for(i=0;i<j;i++)
  1073. pc_delitem(sd,idxlist[i],sd->status.inventory[idxlist[i]].amount,0,1,LOG_TYPE_OTHER);
  1074. }
  1075. #endif
  1076. /* [Ind] */
  1077. sd->sc_display = NULL;
  1078. sd->sc_display_count = 0;
  1079. // Player has not yet received the CashShop list
  1080. sd->status.cashshop_sent = false;
  1081. sd->last_addeditem_index = -1;
  1082. sd->bonus_script.head = NULL;
  1083. sd->bonus_script.count = 0;
  1084. // Request all registries (auth is considered completed whence they arrive)
  1085. intif_request_registry(sd,7);
  1086. return true;
  1087. }
  1088. /*==========================================
  1089. * Closes a connection because it failed to be authenticated from the char server.
  1090. *------------------------------------------*/
  1091. void pc_authfail(struct map_session_data *sd)
  1092. {
  1093. clif_authfail_fd(sd->fd, 0);
  1094. return;
  1095. }
  1096. /**
  1097. * Player register a bl as hatred
  1098. * @param sd : player session
  1099. * @param pos : hate position [0;2]
  1100. * @param bl : target bl
  1101. * @return false:failed, true:success
  1102. */
  1103. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1104. {
  1105. int class_;
  1106. if (!sd || !bl || pos < 0 || pos > 2)
  1107. return false;
  1108. if (sd->hate_mob[pos] != -1)
  1109. { //Can't change hate targets.
  1110. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1111. return false;
  1112. }
  1113. class_ = status_get_class(bl);
  1114. if (!pcdb_checkid(class_)) {
  1115. unsigned int max_hp = status_get_max_hp(bl);
  1116. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1117. return false;
  1118. if (pos != status_get_size(bl))
  1119. return false; //Wrong size
  1120. }
  1121. sd->hate_mob[pos] = class_;
  1122. pc_setglobalreg(sd,sg_info[pos].hate_var,class_+1);
  1123. clif_hate_info(sd, pos, class_, 1);
  1124. return true;
  1125. }
  1126. /*==========================================
  1127. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1128. *------------------------------------------*/
  1129. void pc_reg_received(struct map_session_data *sd)
  1130. {
  1131. uint8 i;
  1132. sd->change_level_2nd = pc_readglobalreg(sd,"jobchange_level");
  1133. sd->change_level_3rd = pc_readglobalreg(sd,"jobchange_level_3rd");
  1134. sd->die_counter = pc_readglobalreg(sd,"PC_DIE_COUNTER");
  1135. sd->langtype = pc_readaccountreg(sd,"#langtype");
  1136. if(msg_checklangtype(sd->langtype,true)<0) sd->langtype=0; //invalid langtype reset to default
  1137. // Cash shop
  1138. sd->cashPoints = pc_readaccountreg(sd,"#CASHPOINTS");
  1139. sd->kafraPoints = pc_readaccountreg(sd,"#KAFRAPOINTS");
  1140. // Cooking Exp
  1141. sd->cook_mastery = pc_readglobalreg(sd,"COOK_MASTERY");
  1142. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1143. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1144. sd->mission_mobid = pc_readglobalreg(sd,"TK_MISSION_ID");
  1145. sd->mission_count = pc_readglobalreg(sd,"TK_MISSION_COUNT");
  1146. }
  1147. if (battle_config.feature_banking)
  1148. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1149. if (battle_config.feature_roulette) {
  1150. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1151. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1152. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1153. }
  1154. sd->roulette.prizeIdx = -1;
  1155. //SG map and mob read [Komurka]
  1156. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1157. uint16 j;
  1158. if ((j = pc_readglobalreg(sd,sg_info[i].feel_var))!=0) {
  1159. sd->feel_map[i].index = j;
  1160. sd->feel_map[i].m = map_mapindex2mapid(j);
  1161. } else {
  1162. sd->feel_map[i].index = 0;
  1163. sd->feel_map[i].m = -1;
  1164. }
  1165. sd->hate_mob[i] = pc_readglobalreg(sd,sg_info[i].hate_var)-1;
  1166. }
  1167. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1168. sd->cloneskill_idx = skill_get_index(pc_readglobalreg(sd,SKILL_VAR_PLAGIARISM));
  1169. if (sd->cloneskill_idx > 0) {
  1170. sd->status.skill[sd->cloneskill_idx].id = pc_readglobalreg(sd,SKILL_VAR_PLAGIARISM);
  1171. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd,SKILL_VAR_PLAGIARISM_LV);
  1172. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1173. sd->status.skill[sd->cloneskill_idx].lv = i;
  1174. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1175. }
  1176. }
  1177. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1178. sd->reproduceskill_idx = skill_get_index(pc_readglobalreg(sd,SKILL_VAR_REPRODUCE));
  1179. if (sd->reproduceskill_idx > 0) {
  1180. sd->status.skill[sd->reproduceskill_idx].id = pc_readglobalreg(sd,SKILL_VAR_REPRODUCE);
  1181. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd,SKILL_VAR_REPRODUCE_LV);
  1182. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1183. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1184. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1185. }
  1186. }
  1187. //Weird... maybe registries were reloaded?
  1188. if (sd->state.active)
  1189. return;
  1190. sd->state.active = 1;
  1191. if (sd->status.party_id)
  1192. party_member_joined(sd);
  1193. if (sd->status.guild_id)
  1194. guild_member_joined(sd);
  1195. // pet
  1196. if (sd->status.pet_id > 0)
  1197. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1198. // Homunculus [albator]
  1199. if( sd->status.hom_id > 0 )
  1200. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1201. if( sd->status.mer_id > 0 )
  1202. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1203. if( sd->status.ele_id > 0 )
  1204. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1205. map_addiddb(&sd->bl);
  1206. map_delnickdb(sd->status.char_id, sd->status.name);
  1207. if (!chrif_auth_finished(sd))
  1208. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1209. pc_check_available_item(sd); // Check for invalid(ated) items.
  1210. pc_load_combo(sd);
  1211. status_calc_pc(sd, (enum e_status_calc_opt)(SCO_FIRST|SCO_FORCE));
  1212. chrif_scdata_request(sd->status.account_id, sd->status.char_id);
  1213. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1214. chrif_bsdata_request(sd->status.char_id);
  1215. sd->storage_size = MIN_STORAGE; //default to min
  1216. #ifdef VIP_ENABLE
  1217. sd->vip.time = 0;
  1218. sd->vip.enabled = 0;
  1219. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0); // request VIP informations
  1220. #endif
  1221. intif_Mail_requestinbox(sd->status.char_id, 0); // MAIL SYSTEM - Request Mail Inbox
  1222. intif_request_questlog(sd);
  1223. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1224. sd->state.connect_new = 1;
  1225. clif_parse_LoadEndAck(sd->fd, sd);
  1226. }
  1227. if( sd->sc.option&OPTION_INVISIBLE ) {
  1228. sd->vd.class_ = INVISIBLE_CLASS;
  1229. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1230. // decrement the number of pvp players on the map
  1231. map[sd->bl.m].users_pvp--;
  1232. if( map[sd->bl.m].flag.pvp && !map[sd->bl.m].flag.pvp_nocalcrank && sd->pvp_timer != INVALID_TIMER ){
  1233. // unregister the player for ranking
  1234. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1235. sd->pvp_timer = INVALID_TIMER;
  1236. }
  1237. clif_changeoption( &sd->bl );
  1238. }
  1239. pc_check_expiration(sd);
  1240. if( sd->state.autotrade ) {
  1241. clif_parse_LoadEndAck(sd->fd, sd);
  1242. sd->autotrade_tid = add_timer(gettick() + battle_config.feature_autotrade_open_delay, pc_autotrade_timer, sd->bl.id, 0);
  1243. }
  1244. }
  1245. static int pc_calc_skillpoint(struct map_session_data* sd)
  1246. {
  1247. uint16 i, skill_point = 0;
  1248. nullpo_ret(sd);
  1249. for(i = 1; i < MAX_SKILL; i++) {
  1250. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1251. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1252. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1253. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1254. )
  1255. {
  1256. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1257. skill_point += sd->status.skill[i].lv;
  1258. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1259. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1260. }
  1261. }
  1262. }
  1263. return skill_point;
  1264. }
  1265. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1266. uint16 i = 0;
  1267. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1268. return false;
  1269. /**
  1270. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1271. * skill tree and since they have no icons they'll give resource errors
  1272. * Get ALL skills except npc/guild ones. [Skotlex]
  1273. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1274. **/
  1275. for( i = 0; i < MAX_SKILL; i++ ) {
  1276. uint16 skill_id = skill_idx2id(i);
  1277. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1278. continue;
  1279. switch (skill_id) {
  1280. case SM_SELFPROVOKE:
  1281. case AB_DUPLELIGHT_MELEE:
  1282. case AB_DUPLELIGHT_MAGIC:
  1283. case WL_CHAINLIGHTNING_ATK:
  1284. case WL_TETRAVORTEX_FIRE:
  1285. case WL_TETRAVORTEX_WATER:
  1286. case WL_TETRAVORTEX_WIND:
  1287. case WL_TETRAVORTEX_GROUND:
  1288. case WL_SUMMON_ATK_FIRE:
  1289. case WL_SUMMON_ATK_WIND:
  1290. case WL_SUMMON_ATK_WATER:
  1291. case WL_SUMMON_ATK_GROUND:
  1292. case LG_OVERBRAND_BRANDISH:
  1293. case LG_OVERBRAND_PLUSATK:
  1294. case WM_SEVERE_RAINSTORM_MELEE:
  1295. case RL_R_TRIP_PLUSATK:
  1296. case SG_DEVIL:
  1297. case MO_TRIPLEATTACK:
  1298. case RG_SNATCHER:
  1299. continue;
  1300. default:
  1301. {
  1302. uint8 lv = (uint8)skill_get_max(skill_id);
  1303. if (lv > 0) {
  1304. sd->status.skill[i].id = skill_id;
  1305. if (addlv)
  1306. sd->status.skill[i].lv = lv;
  1307. }
  1308. }
  1309. break;
  1310. }
  1311. }
  1312. return true;
  1313. }
  1314. /*==========================================
  1315. * Calculation of skill level.
  1316. * @param sd
  1317. *------------------------------------------*/
  1318. void pc_calc_skilltree(struct map_session_data *sd)
  1319. {
  1320. int i, flag;
  1321. int c = 0;
  1322. nullpo_retv(sd);
  1323. i = pc_calc_skilltree_normalize_job(sd);
  1324. c = pc_mapid2jobid(i, sd->status.sex);
  1325. if( c == -1 )
  1326. { //Unable to normalize job??
  1327. 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);
  1328. return;
  1329. }
  1330. c = pc_class2idx(c);
  1331. for( i = 0; i < MAX_SKILL; i++ ) {
  1332. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1333. sd->status.skill[i].id = 0; //First clear skills.
  1334. /* permanent skills that must be re-checked */
  1335. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1336. uint16 sk_id = skill_idx2id(i);
  1337. if (!sk_id) {
  1338. sd->status.skill[i].id = 0;
  1339. sd->status.skill[i].lv = 0;
  1340. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1341. continue;
  1342. }
  1343. switch (sk_id) {
  1344. case NV_TRICKDEAD:
  1345. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1346. sd->status.skill[i].id = 0;
  1347. sd->status.skill[i].lv = 0;
  1348. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1349. }
  1350. break;
  1351. }
  1352. }
  1353. }
  1354. for( i = 0; i < MAX_SKILL; i++ ) {
  1355. uint16 skill_id = 0;
  1356. // Restore original level of skills after deleting earned skills.
  1357. 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 ) {
  1358. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1359. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1360. }
  1361. //Enable Bard/Dancer spirit linked skills.
  1362. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1363. continue;
  1364. if( &sd->sc && sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1365. //Link Dancer skills to bard.
  1366. if( sd->status.sex ) {
  1367. if( sd->status.skill[i-8].lv < 10 )
  1368. continue;
  1369. sd->status.skill[i].id = skill_id;
  1370. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1371. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1372. }
  1373. //Link Bard skills to dancer.
  1374. else {
  1375. if( sd->status.skill[i].lv < 10 )
  1376. continue;
  1377. sd->status.skill[i-8].id = skill_id - 8;
  1378. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1379. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1380. }
  1381. }
  1382. }
  1383. // Removes Taekwon Ranker skill bonus
  1384. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1385. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1386. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1387. uint16 sk_id = skill_tree[c_][i].id;
  1388. uint16 sk_idx = 0;
  1389. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].id)))
  1390. continue;
  1391. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1392. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1393. continue;
  1394. sd->status.skill[sk_idx].id = 0;
  1395. }
  1396. }
  1397. }
  1398. // Grant all skills
  1399. pc_grant_allskills(sd, false);
  1400. do {
  1401. uint16 skid = 0;
  1402. flag = 0;
  1403. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].id) > 0; i++) {
  1404. bool fail = false;
  1405. uint16 sk_idx = skill_get_index(skid);
  1406. if (sd->status.skill[sk_idx].id)
  1407. continue; //Skill already known.
  1408. if (!battle_config.skillfree) {
  1409. uint8 j;
  1410. // Checking required skills
  1411. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1412. uint16 sk_need_id = skill_tree[c][i].need[j].id;
  1413. uint16 sk_need_idx = 0;
  1414. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1415. short sk_need = sk_need_id;
  1416. 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)
  1417. sk_need = 0; //Not learned.
  1418. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1419. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1420. else
  1421. sk_need = pc_checkskill(sd,sk_need_id);
  1422. if (sk_need < skill_tree[c][i].need[j].lv) {
  1423. fail = true;
  1424. break;
  1425. }
  1426. }
  1427. }
  1428. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1429. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1430. class_ = pc_class2idx(class_);
  1431. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1432. fail = true; // job level requirement wasn't satisfied
  1433. }
  1434. }
  1435. if (!fail) {
  1436. int inf2 = skill_get_inf2(skid);
  1437. if (!sd->status.skill[sk_idx].lv && (
  1438. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1439. inf2&INF2_WEDDING_SKILL ||
  1440. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1441. ))
  1442. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1443. sd->status.skill[sk_idx].id = skid;
  1444. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1445. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1446. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1447. }
  1448. flag = 1; // skill list has changed, perform another pass
  1449. }
  1450. }
  1451. } while(flag);
  1452. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1453. unsigned short skid = 0;
  1454. /* Taekwon Ranker Bonus Skill Tree
  1455. ============================================
  1456. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1457. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1458. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1459. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].id) > 0; i++ ) {
  1460. uint16 sk_idx = 0;
  1461. if (!(sk_idx = skill_get_index(skid)))
  1462. continue;
  1463. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1464. continue; //Do not include Quest/Wedding skills.
  1465. if( sd->status.skill[sk_idx].id == 0 ) {
  1466. sd->status.skill[sk_idx].id = skid;
  1467. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1468. } else if( skid != NV_BASIC )
  1469. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1470. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1471. }
  1472. }
  1473. }
  1474. //Checks if you can learn a new skill after having leveled up a skill.
  1475. static void pc_check_skilltree(struct map_session_data *sd)
  1476. {
  1477. int i, flag = 0;
  1478. int c = 0;
  1479. if (battle_config.skillfree)
  1480. return; //Function serves no purpose if this is set
  1481. i = pc_calc_skilltree_normalize_job(sd);
  1482. c = pc_mapid2jobid(i, sd->status.sex);
  1483. if (c == -1) { //Unable to normalize job??
  1484. 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);
  1485. return;
  1486. }
  1487. c = pc_class2idx(c);
  1488. do {
  1489. uint16 skid = 0;
  1490. flag = 0;
  1491. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].id) > 0; i++ ) {
  1492. uint16 sk_idx = skill_get_index(skid);
  1493. bool fail = false;
  1494. uint8 j = 0;
  1495. if (sd->status.skill[sk_idx].id) //Already learned
  1496. continue;
  1497. // Checking required skills
  1498. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1499. uint16 sk_need_id = skill_tree[c][i].need[j].id;
  1500. uint16 sk_need_idx = 0;
  1501. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1502. short sk_need = sk_need_id;
  1503. 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)
  1504. sk_need = 0; //Not learned.
  1505. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1506. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1507. else
  1508. sk_need = pc_checkskill(sd,sk_need_id);
  1509. if (sk_need < skill_tree[c][i].need[j].lv) {
  1510. fail = true;
  1511. break;
  1512. }
  1513. }
  1514. }
  1515. if( fail )
  1516. continue;
  1517. if( sd->status.job_level < skill_tree[c][i].joblv )
  1518. continue;
  1519. j = skill_get_inf2(skid);
  1520. if( !sd->status.skill[sk_idx].lv && (
  1521. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1522. j&INF2_WEDDING_SKILL ||
  1523. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1524. ) )
  1525. continue; //Cannot be learned via normal means.
  1526. sd->status.skill[sk_idx].id = skid;
  1527. flag = 1;
  1528. }
  1529. } while(flag);
  1530. }
  1531. // Make sure all the skills are in the correct condition
  1532. // before persisting to the backend.. [MouseJstr]
  1533. void pc_clean_skilltree(struct map_session_data *sd)
  1534. {
  1535. uint16 i;
  1536. for (i = 0; i < MAX_SKILL; i++){
  1537. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1538. sd->status.skill[i].id = 0;
  1539. sd->status.skill[i].lv = 0;
  1540. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1541. }
  1542. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1543. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1544. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1545. }
  1546. }
  1547. }
  1548. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1549. {
  1550. int skill_point, novice_skills;
  1551. int c = sd->class_;
  1552. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1553. return c;
  1554. skill_point = pc_calc_skillpoint(sd);
  1555. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1556. // limit 1st class and above to novice job levels
  1557. if(skill_point < novice_skills)
  1558. {
  1559. c = MAPID_NOVICE;
  1560. }
  1561. // limit 2nd class and above to first class job levels (super novices are exempt)
  1562. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1563. {
  1564. // regenerate change_level_2nd
  1565. if (!sd->change_level_2nd)
  1566. {
  1567. if (sd->class_&JOBL_THIRD)
  1568. {
  1569. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1570. if (!sd->change_level_3rd)
  1571. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1572. else
  1573. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1574. - (sd->status.job_level - 1)
  1575. - (sd->change_level_3rd - 1)
  1576. - novice_skills;
  1577. }
  1578. else
  1579. {
  1580. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1581. - (sd->status.job_level - 1)
  1582. - novice_skills;
  1583. }
  1584. pc_setglobalreg (sd, "jobchange_level", sd->change_level_2nd);
  1585. }
  1586. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1587. {
  1588. c &= MAPID_BASEMASK;
  1589. }
  1590. // limit 3rd class to 2nd class/trans job levels
  1591. else if(sd->class_&JOBL_THIRD)
  1592. {
  1593. // regenerate change_level_3rd
  1594. if (!sd->change_level_3rd)
  1595. {
  1596. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1597. - (sd->status.job_level - 1)
  1598. - (sd->change_level_2nd - 1)
  1599. - novice_skills;
  1600. pc_setglobalreg (sd, "jobchange_level_3rd", sd->change_level_3rd);
  1601. }
  1602. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1603. c &= MAPID_UPPERMASK;
  1604. }
  1605. }
  1606. // restore non-limiting flags
  1607. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1608. return c;
  1609. }
  1610. /*==========================================
  1611. * Updates the weight status
  1612. *------------------------------------------
  1613. * 1: overweight 50%
  1614. * 2: overweight 90%
  1615. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1616. */
  1617. void pc_updateweightstatus(struct map_session_data *sd)
  1618. {
  1619. int old_overweight;
  1620. int new_overweight;
  1621. nullpo_retv(sd);
  1622. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1623. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1624. if( old_overweight == new_overweight )
  1625. return; // no change
  1626. // stop old status change
  1627. if( old_overweight == 1 )
  1628. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1629. else if( old_overweight == 2 )
  1630. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1631. // start new status change
  1632. if( new_overweight == 1 )
  1633. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1634. else if( new_overweight == 2 )
  1635. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1636. // update overweight status
  1637. sd->regen.state.overweight = new_overweight;
  1638. }
  1639. int pc_disguise(struct map_session_data *sd, int class_)
  1640. {
  1641. if (!class_ && !sd->disguise)
  1642. return 0;
  1643. if (class_ && sd->disguise == class_)
  1644. return 0;
  1645. if(sd->sc.option&OPTION_INVISIBLE)
  1646. { //Character is invisible. Stealth class-change. [Skotlex]
  1647. sd->disguise = class_; //viewdata is set on uncloaking.
  1648. return 2;
  1649. }
  1650. if (sd->bl.prev != NULL) {
  1651. pc_stop_walking(sd, 0);
  1652. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1653. }
  1654. if (!class_) {
  1655. sd->disguise = 0;
  1656. class_ = sd->status.class_;
  1657. } else
  1658. sd->disguise=class_;
  1659. status_set_viewdata(&sd->bl, class_);
  1660. clif_changeoption(&sd->bl);
  1661. if (sd->bl.prev != NULL) {
  1662. clif_spawn(&sd->bl);
  1663. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1664. { //It seems the cart info is lost on undisguise.
  1665. clif_cartlist(sd);
  1666. clif_updatestatus(sd,SP_CARTINFO);
  1667. }
  1668. if (sd->chatID) {
  1669. struct chat_data* cd;
  1670. nullpo_retr(1, sd);
  1671. cd = (struct chat_data*)map_id2bl(sd->chatID);
  1672. if( cd != NULL || (struct block_list*)sd == cd->owner )
  1673. clif_dispchat(cd,0);
  1674. }
  1675. }
  1676. return 1;
  1677. }
  1678. /// Show error message
  1679. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1680. /// Check for valid Element, break & show error message if invalid Element
  1681. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1682. /// Check for valid Race, break & show error message if invalid Race
  1683. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1684. /// Check for valid Race2, break & show error message if invalid Race2
  1685. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1686. /// Check for valid Class, break & show error message if invalid Class
  1687. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1688. /// Check for valid Size, break & show error message if invalid Size
  1689. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1690. static void pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, unsigned short card_id)
  1691. {
  1692. uint8 i;
  1693. if( !rate )
  1694. return;
  1695. for( i = 0; i < max && spell[i].id; i++ )
  1696. {
  1697. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1698. {
  1699. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1700. return;
  1701. rate += spell[i].rate;
  1702. break;
  1703. }
  1704. }
  1705. if (i == max) {
  1706. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", max);
  1707. return;
  1708. }
  1709. spell[i].id = id;
  1710. spell[i].lv = lv;
  1711. spell[i].rate = rate;
  1712. //Auto-update flag value.
  1713. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1714. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1715. if (!(flag&BF_SKILLMASK)) {
  1716. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1717. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1718. }
  1719. spell[i].flag|= flag;
  1720. spell[i].card_id = card_id;
  1721. }
  1722. 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)
  1723. {
  1724. uint8 i;
  1725. if( !rate )
  1726. return;
  1727. for( i = 0; i < max && spell[i].id; i++ )
  1728. {
  1729. ; // each autospell works independently
  1730. }
  1731. if( i == max )
  1732. {
  1733. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", max);
  1734. return;
  1735. }
  1736. spell[i].flag = src_skill;
  1737. spell[i].id = id;
  1738. spell[i].lv = lv;
  1739. spell[i].rate = rate;
  1740. spell[i].card_id = card_id;
  1741. return;
  1742. }
  1743. static void pc_bonus_addeff(struct s_addeffect* effect, int max, enum sc_type id, short rate, short arrow_rate, unsigned char flag)
  1744. {
  1745. uint16 i;
  1746. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1747. flag|=ATF_SHORT|ATF_LONG; //Default range: both
  1748. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1749. flag|=ATF_TARGET; //Default target: enemy.
  1750. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1751. flag|=ATF_WEAPON; //Default type: weapon.
  1752. for (i = 0; i < max && effect[i].flag; i++) {
  1753. if (effect[i].id == id && effect[i].flag == flag)
  1754. {
  1755. effect[i].rate += rate;
  1756. effect[i].arrow_rate += arrow_rate;
  1757. return;
  1758. }
  1759. }
  1760. if (i == max) {
  1761. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", max);
  1762. return;
  1763. }
  1764. effect[i].id = id;
  1765. effect[i].rate = rate;
  1766. effect[i].arrow_rate = arrow_rate;
  1767. effect[i].flag = flag;
  1768. }
  1769. static void pc_bonus_addeff_onskill(struct s_addeffectonskill* effect, int max, enum sc_type id, short rate, short skill, unsigned char target)
  1770. {
  1771. uint8 i;
  1772. for( i = 0; i < max && effect[i].skill; i++ )
  1773. {
  1774. if( effect[i].id == id && effect[i].skill == skill && effect[i].target == target )
  1775. {
  1776. effect[i].rate += rate;
  1777. return;
  1778. }
  1779. }
  1780. if( i == max ) {
  1781. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects on skill per character!\n", max);
  1782. return;
  1783. }
  1784. effect[i].id = id;
  1785. effect[i].rate = rate;
  1786. effect[i].skill = skill;
  1787. effect[i].target = target;
  1788. }
  1789. /** Adjust/add drop rate modifier for player
  1790. * @param drop: Player's sd->add_drop (struct s_add_drop)
  1791. * @param max: Max bonus can be received
  1792. * @param nameid: item id that will be dropped
  1793. * @param group: group id
  1794. * @param class_: target class
  1795. * @param race: target race. if < 0, means monster_id
  1796. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  1797. */
  1798. 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)
  1799. {
  1800. uint8 i;
  1801. if (!nameid && !group) {
  1802. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  1803. return;
  1804. }
  1805. if (nameid && !itemdb_exists(nameid)) {
  1806. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  1807. return;
  1808. }
  1809. if (group && !itemdb_group_exists(group)) {
  1810. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  1811. return;
  1812. }
  1813. //Apply config rate adjustment settings.
  1814. if (rate >= 0) { //Absolute drop.
  1815. if (battle_config.item_rate_adddrop != 100)
  1816. rate = rate*battle_config.item_rate_adddrop/100;
  1817. if (rate < battle_config.item_drop_adddrop_min)
  1818. rate = battle_config.item_drop_adddrop_min;
  1819. else if (rate > battle_config.item_drop_adddrop_max)
  1820. rate = battle_config.item_drop_adddrop_max;
  1821. } else { //Relative drop, max/min limits are applied at drop time.
  1822. if (battle_config.item_rate_adddrop != 100)
  1823. rate = rate*battle_config.item_rate_adddrop/100;
  1824. if (rate > -1)
  1825. rate = -1;
  1826. }
  1827. //Find match entry, and adjust the rate only
  1828. for (i = 0; i < max; i++) {
  1829. if (!&drop[i] || (!drop[i].nameid && !drop[i].group))
  1830. continue;
  1831. if (drop[i].nameid == nameid &&
  1832. drop[i].group == group &&
  1833. drop[i].race == race &&
  1834. drop[i].class_ == class_
  1835. )
  1836. {
  1837. //Adjust the rate if it has same classification
  1838. if ((rate < 0 && drop[i].rate < 0) ||
  1839. (rate > 0 && drop[i].rate > 0))
  1840. {
  1841. drop[i].rate += rate;
  1842. return;
  1843. }
  1844. }
  1845. }
  1846. ARR_FIND(0,max,i,!&drop[i] || (drop[i].nameid == 0 && drop[i].group == 0));
  1847. if (i >= max) {
  1848. 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);
  1849. return;
  1850. }
  1851. drop[i].nameid = nameid;
  1852. drop[i].group = group;
  1853. drop[i].race = race;
  1854. drop[i].class_ = class_;
  1855. drop[i].rate = rate;
  1856. }
  1857. 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)
  1858. {
  1859. int i;
  1860. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1861. if( i == max )
  1862. {
  1863. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1864. return false;
  1865. }
  1866. if( !onskill )
  1867. {
  1868. if( !(flag&BF_RANGEMASK) )
  1869. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1870. if( !(flag&BF_WEAPONMASK) )
  1871. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1872. if( !(flag&BF_SKILLMASK) )
  1873. {
  1874. if( flag&(BF_MAGIC|BF_MISC) )
  1875. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1876. if( flag&BF_WEAPON )
  1877. flag|=BF_NORMAL|BF_SKILL;
  1878. }
  1879. }
  1880. bonus[i].rate = rate;
  1881. bonus[i].duration = dur;
  1882. bonus[i].active = INVALID_TIMER;
  1883. bonus[i].atk_type = flag;
  1884. bonus[i].pos = pos;
  1885. bonus[i].bonus_script = aStrdup(script);
  1886. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  1887. return true;
  1888. }
  1889. void pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  1890. {
  1891. int i;
  1892. if (!sd)
  1893. return;
  1894. for( i = 0; i < max; i++ )
  1895. {
  1896. if( autobonus[i].active != INVALID_TIMER )
  1897. {
  1898. if( restore && (sd->state.autobonus&autobonus[i].pos) == autobonus[i].pos)
  1899. {
  1900. if( autobonus[i].bonus_script )
  1901. {
  1902. int j;
  1903. unsigned int equip_pos_idx = 0;
  1904. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  1905. for(j = 0; j < EQI_MAX; j++) {
  1906. if(sd->equip_index[j] >= 0)
  1907. equip_pos_idx |= sd->status.inventory[sd->equip_index[j]].equip;
  1908. }
  1909. if((equip_pos_idx&autobonus[i].pos) == autobonus[i].pos)
  1910. script_run_autobonus(autobonus[i].bonus_script,sd,autobonus[i].pos);
  1911. }
  1912. continue;
  1913. }
  1914. else
  1915. { // Logout / Unequipped an item with an activated bonus
  1916. delete_timer(autobonus[i].active,pc_endautobonus);
  1917. autobonus[i].active = INVALID_TIMER;
  1918. }
  1919. }
  1920. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  1921. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  1922. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  1923. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  1924. autobonus[i].active = INVALID_TIMER;
  1925. }
  1926. }
  1927. void pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  1928. {
  1929. if (!sd || !autobonus)
  1930. return;
  1931. if( autobonus->other_script )
  1932. {
  1933. int j;
  1934. unsigned int equip_pos_idx = 0;
  1935. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  1936. for(j = 0; j < EQI_MAX; j++) {
  1937. if(sd->equip_index[j] >= 0)
  1938. equip_pos_idx |= sd->status.inventory[sd->equip_index[j]].equip;
  1939. }
  1940. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  1941. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  1942. }
  1943. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  1944. sd->state.autobonus |= autobonus->pos;
  1945. status_calc_pc(sd,SCO_FORCE);
  1946. }
  1947. int pc_endautobonus(int tid, unsigned int tick, int id, intptr_t data)
  1948. {
  1949. struct map_session_data *sd = map_id2sd(id);
  1950. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  1951. nullpo_ret(sd);
  1952. nullpo_ret(autobonus);
  1953. autobonus->active = INVALID_TIMER;
  1954. sd->state.autobonus &= ~autobonus->pos;
  1955. status_calc_pc(sd,SCO_FORCE);
  1956. return 0;
  1957. }
  1958. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  1959. {
  1960. uint8 i;
  1961. struct weapon_data* wd;
  1962. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  1963. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  1964. if (i == MAX_PC_BONUS)
  1965. {
  1966. ShowWarning("pc_bonus_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  1967. return;
  1968. }
  1969. if (!(flag&BF_RANGEMASK))
  1970. flag |= BF_SHORT|BF_LONG;
  1971. if (!(flag&BF_WEAPONMASK))
  1972. flag |= BF_WEAPON;
  1973. if (!(flag&BF_SKILLMASK))
  1974. {
  1975. if (flag&(BF_MAGIC|BF_MISC))
  1976. flag |= BF_SKILL;
  1977. if (flag&BF_WEAPON)
  1978. flag |= BF_NORMAL|BF_SKILL;
  1979. }
  1980. wd->addele2[i].ele = ele;
  1981. wd->addele2[i].rate = rate;
  1982. wd->addele2[i].flag = flag;
  1983. }
  1984. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  1985. {
  1986. uint8 i;
  1987. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  1988. if (i == MAX_PC_BONUS)
  1989. {
  1990. ShowWarning("pc_bonus_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  1991. return;
  1992. }
  1993. if (!(flag&BF_RANGEMASK))
  1994. flag |= BF_SHORT|BF_LONG;
  1995. if (!(flag&BF_WEAPONMASK))
  1996. flag |= BF_WEAPON;
  1997. if (!(flag&BF_SKILLMASK))
  1998. {
  1999. if (flag&(BF_MAGIC|BF_MISC))
  2000. flag |= BF_SKILL;
  2001. if (flag&BF_WEAPON)
  2002. flag |= BF_NORMAL|BF_SKILL;
  2003. }
  2004. sd->subele2[i].ele = ele;
  2005. sd->subele2[i].rate = rate;
  2006. sd->subele2[i].flag = flag;
  2007. }
  2008. /** Add item group heal rate bonus to player
  2009. * @param sd Player
  2010. * @param group_id Item Group ID
  2011. * @param rate
  2012. * @author Cydh
  2013. */
  2014. void pc_itemgrouphealrate(struct map_session_data *sd, uint16 group_id, short rate) {
  2015. struct s_pc_itemgrouphealrate *entry;
  2016. uint8 i;
  2017. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  2018. if (sd->itemgrouphealrate[i]->group_id == group_id)
  2019. break;
  2020. }
  2021. if (i != sd->itemgrouphealrate_count) {
  2022. sd->itemgrouphealrate[i]->rate += rate;
  2023. return;
  2024. }
  2025. if (i >= UINT8_MAX) {
  2026. ShowError("pc_itemgrouphealrate_add: Reached max (%d) possible bonuses for this player %d\n", UINT8_MAX);
  2027. return;
  2028. }
  2029. entry = ers_alloc(pc_itemgrouphealrate_ers, struct s_pc_itemgrouphealrate);
  2030. entry->group_id = group_id;
  2031. entry->rate = rate;
  2032. RECREATE(sd->itemgrouphealrate, struct s_pc_itemgrouphealrate *, sd->itemgrouphealrate_count+1);
  2033. sd->itemgrouphealrate[sd->itemgrouphealrate_count++] = entry;
  2034. }
  2035. /** Clear item group heal rate from player
  2036. * @param sd Player
  2037. * @author Cydh
  2038. */
  2039. void pc_itemgrouphealrate_clear(struct map_session_data *sd) {
  2040. if (!sd || !sd->itemgrouphealrate_count)
  2041. return;
  2042. else {
  2043. uint8 i;
  2044. for( i = 0; i < sd->itemgrouphealrate_count; i++ )
  2045. ers_free(pc_itemgrouphealrate_ers, sd->itemgrouphealrate[i]);
  2046. sd->itemgrouphealrate_count = 0;
  2047. aFree(sd->itemgrouphealrate);
  2048. sd->itemgrouphealrate = NULL;
  2049. }
  2050. }
  2051. /*==========================================
  2052. * Add a bonus(type) to player sd
  2053. * format: bonus bBonusName,val;
  2054. * @param sd
  2055. * @param type Bonus type used by bBonusName
  2056. * @param val Value that usually for rate or fixed value
  2057. *------------------------------------------*/
  2058. void pc_bonus(struct map_session_data *sd,int type,int val)
  2059. {
  2060. struct status_data *status;
  2061. int bonus;
  2062. nullpo_retv(sd);
  2063. status = &sd->base_status;
  2064. switch(type){
  2065. case SP_STR:
  2066. case SP_AGI:
  2067. case SP_VIT:
  2068. case SP_INT:
  2069. case SP_DEX:
  2070. case SP_LUK:
  2071. if(sd->state.lr_flag != 2)
  2072. sd->param_bonus[type-SP_STR]+=val;
  2073. break;
  2074. case SP_ATK1:
  2075. if(!sd->state.lr_flag) {
  2076. bonus = status->rhw.atk + val;
  2077. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2078. }
  2079. else if(sd->state.lr_flag == 1) {
  2080. bonus = status->lhw.atk + val;
  2081. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2082. }
  2083. break;
  2084. case SP_ATK2:
  2085. if(!sd->state.lr_flag) {
  2086. bonus = status->rhw.atk2 + val;
  2087. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2088. }
  2089. else if(sd->state.lr_flag == 1) {
  2090. bonus = status->lhw.atk2 + val;
  2091. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2092. }
  2093. break;
  2094. case SP_BASE_ATK:
  2095. if(sd->state.lr_flag != 2) {
  2096. #ifdef RENEWAL
  2097. sd->bonus.eatk += val;
  2098. #else
  2099. bonus = status->batk + val;
  2100. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2101. #endif
  2102. }
  2103. break;
  2104. case SP_DEF1:
  2105. if(sd->state.lr_flag != 2) {
  2106. bonus = status->def + val;
  2107. #ifdef RENEWAL
  2108. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2109. #else
  2110. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2111. #endif
  2112. }
  2113. break;
  2114. case SP_DEF2:
  2115. if(sd->state.lr_flag != 2) {
  2116. bonus = status->def2 + val;
  2117. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2118. }
  2119. break;
  2120. case SP_MDEF1:
  2121. if(sd->state.lr_flag != 2) {
  2122. bonus = status->mdef + val;
  2123. #ifdef RENEWAL
  2124. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2125. #else
  2126. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2127. #endif
  2128. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2129. sd->bonus.shieldmdef += bonus;
  2130. }
  2131. }
  2132. break;
  2133. case SP_MDEF2:
  2134. if(sd->state.lr_flag != 2) {
  2135. bonus = status->mdef2 + val;
  2136. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2137. }
  2138. break;
  2139. case SP_HIT:
  2140. if(sd->state.lr_flag != 2) {
  2141. bonus = status->hit + val;
  2142. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2143. } else
  2144. sd->bonus.arrow_hit+=val;
  2145. break;
  2146. case SP_FLEE1:
  2147. if(sd->state.lr_flag != 2) {
  2148. bonus = status->flee + val;
  2149. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2150. }
  2151. break;
  2152. case SP_FLEE2:
  2153. if(sd->state.lr_flag != 2) {
  2154. bonus = status->flee2 + val*10;
  2155. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2156. }
  2157. break;
  2158. case SP_CRITICAL:
  2159. if(sd->state.lr_flag != 2) {
  2160. bonus = status->cri + val*10;
  2161. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2162. } else
  2163. sd->bonus.arrow_cri += val*10;
  2164. break;
  2165. case SP_ATKELE:
  2166. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2167. switch (sd->state.lr_flag)
  2168. {
  2169. case 2:
  2170. switch (sd->status.weapon) {
  2171. case W_BOW:
  2172. case W_REVOLVER:
  2173. case W_RIFLE:
  2174. case W_GATLING:
  2175. case W_SHOTGUN:
  2176. case W_GRENADE:
  2177. //Become weapon element.
  2178. status->rhw.ele=val;
  2179. break;
  2180. default: //Become arrow element.
  2181. sd->bonus.arrow_ele=val;
  2182. break;
  2183. }
  2184. break;
  2185. case 1:
  2186. status->lhw.ele=val;
  2187. break;
  2188. default:
  2189. status->rhw.ele=val;
  2190. break;
  2191. }
  2192. break;
  2193. case SP_DEFELE:
  2194. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2195. if(sd->state.lr_flag != 2)
  2196. status->def_ele=val;
  2197. break;
  2198. case SP_MAXHP:
  2199. if(sd->state.lr_flag == 2)
  2200. break;
  2201. sd->bonus.hp += val;
  2202. break;
  2203. case SP_MAXSP:
  2204. if(sd->state.lr_flag == 2)
  2205. break;
  2206. sd->bonus.sp += val;
  2207. break;
  2208. case SP_MAXHPRATE:
  2209. if(sd->state.lr_flag != 2)
  2210. sd->hprate+=val;
  2211. break;
  2212. case SP_MAXSPRATE:
  2213. if(sd->state.lr_flag != 2)
  2214. sd->sprate+=val;
  2215. break;
  2216. case SP_SPRATE:
  2217. if(sd->state.lr_flag != 2)
  2218. sd->dsprate+=val;
  2219. break;
  2220. case SP_ATTACKRANGE:
  2221. switch (sd->state.lr_flag) {
  2222. case 2:
  2223. switch (sd->status.weapon) {
  2224. case W_BOW:
  2225. case W_REVOLVER:
  2226. case W_RIFLE:
  2227. case W_GATLING:
  2228. case W_SHOTGUN:
  2229. case W_GRENADE:
  2230. status->rhw.range += val;
  2231. }
  2232. break;
  2233. case 1:
  2234. status->lhw.range += val;
  2235. break;
  2236. default:
  2237. status->rhw.range += val;
  2238. break;
  2239. }
  2240. break;
  2241. case SP_SPEED_RATE: //Non stackable increase
  2242. if(sd->state.lr_flag != 2)
  2243. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2244. break;
  2245. case SP_SPEED_ADDRATE: //Stackable increase
  2246. if(sd->state.lr_flag != 2)
  2247. sd->bonus.speed_add_rate -= val;
  2248. break;
  2249. case SP_ASPD: //Raw increase
  2250. if(sd->state.lr_flag != 2)
  2251. sd->bonus.aspd_add -= 10*val;
  2252. break;
  2253. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2254. if(sd->state.lr_flag != 2)
  2255. #ifndef RENEWAL_ASPD
  2256. status->aspd_rate -= 10*val;
  2257. #else
  2258. status->aspd_rate2 += val;
  2259. #endif
  2260. break;
  2261. case SP_HP_RECOV_RATE:
  2262. if(sd->state.lr_flag != 2)
  2263. sd->hprecov_rate += val;
  2264. break;
  2265. case SP_SP_RECOV_RATE:
  2266. if(sd->state.lr_flag != 2)
  2267. sd->sprecov_rate += val;
  2268. break;
  2269. case SP_CRITICAL_DEF:
  2270. if(sd->state.lr_flag != 2)
  2271. sd->bonus.critical_def += val;
  2272. break;
  2273. case SP_NEAR_ATK_DEF:
  2274. if(sd->state.lr_flag != 2)
  2275. sd->bonus.near_attack_def_rate += val;
  2276. break;
  2277. case SP_LONG_ATK_DEF:
  2278. if(sd->state.lr_flag != 2)
  2279. sd->bonus.long_attack_def_rate += val;
  2280. break;
  2281. case SP_DOUBLE_RATE:
  2282. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2283. sd->bonus.double_rate = val;
  2284. break;
  2285. case SP_DOUBLE_ADD_RATE:
  2286. if(sd->state.lr_flag == 0)
  2287. sd->bonus.double_add_rate += val;
  2288. break;
  2289. case SP_MATK_RATE:
  2290. if(sd->state.lr_flag != 2)
  2291. sd->matk_rate += val;
  2292. break;
  2293. case SP_IGNORE_DEF_ELE:
  2294. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2295. if(!sd->state.lr_flag)
  2296. sd->right_weapon.ignore_def_ele |= 1<<val;
  2297. else if(sd->state.lr_flag == 1)
  2298. sd->left_weapon.ignore_def_ele |= 1<<val;
  2299. break;
  2300. case SP_IGNORE_DEF_RACE:
  2301. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2302. if(!sd->state.lr_flag)
  2303. sd->right_weapon.ignore_def_race |= 1<<val;
  2304. else if(sd->state.lr_flag == 1)
  2305. sd->left_weapon.ignore_def_race |= 1<<val;
  2306. break;
  2307. case SP_IGNORE_DEF_CLASS:
  2308. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2309. if(!sd->state.lr_flag)
  2310. sd->right_weapon.ignore_def_class |= 1<<val;
  2311. else if(sd->state.lr_flag == 1)
  2312. sd->left_weapon.ignore_def_class |= 1<<val;
  2313. break;
  2314. case SP_ATK_RATE:
  2315. if(sd->state.lr_flag != 2)
  2316. sd->bonus.atk_rate += val;
  2317. break;
  2318. case SP_MAGIC_ATK_DEF:
  2319. if(sd->state.lr_flag != 2)
  2320. sd->bonus.magic_def_rate += val;
  2321. break;
  2322. case SP_MISC_ATK_DEF:
  2323. if(sd->state.lr_flag != 2)
  2324. sd->bonus.misc_def_rate += val;
  2325. break;
  2326. case SP_IGNORE_MDEF_ELE:
  2327. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2328. if(sd->state.lr_flag != 2)
  2329. sd->bonus.ignore_mdef_ele |= 1<<val;
  2330. break;
  2331. case SP_IGNORE_MDEF_RACE:
  2332. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2333. if(sd->state.lr_flag != 2)
  2334. sd->bonus.ignore_mdef_race |= 1<<val;
  2335. break;
  2336. case SP_PERFECT_HIT_RATE:
  2337. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2338. sd->bonus.perfect_hit = val;
  2339. break;
  2340. case SP_PERFECT_HIT_ADD_RATE:
  2341. if(sd->state.lr_flag != 2)
  2342. sd->bonus.perfect_hit_add += val;
  2343. break;
  2344. case SP_CRITICAL_RATE:
  2345. if(sd->state.lr_flag != 2)
  2346. sd->critical_rate+=val;
  2347. break;
  2348. case SP_DEF_RATIO_ATK_ELE:
  2349. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2350. if(!sd->state.lr_flag)
  2351. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2352. else if(sd->state.lr_flag == 1)
  2353. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2354. break;
  2355. case SP_DEF_RATIO_ATK_RACE:
  2356. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2357. if(!sd->state.lr_flag)
  2358. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2359. else if(sd->state.lr_flag == 1)
  2360. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2361. break;
  2362. case SP_DEF_RATIO_ATK_CLASS:
  2363. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2364. if(!sd->state.lr_flag)
  2365. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2366. else if(sd->state.lr_flag == 1)
  2367. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2368. break;
  2369. case SP_HIT_RATE:
  2370. if(sd->state.lr_flag != 2)
  2371. sd->hit_rate += val;
  2372. break;
  2373. case SP_FLEE_RATE:
  2374. if(sd->state.lr_flag != 2)
  2375. sd->flee_rate += val;
  2376. break;
  2377. case SP_FLEE2_RATE:
  2378. if(sd->state.lr_flag != 2)
  2379. sd->flee2_rate += val;
  2380. break;
  2381. case SP_DEF_RATE:
  2382. if(sd->state.lr_flag != 2)
  2383. sd->def_rate += val;
  2384. break;
  2385. case SP_DEF2_RATE:
  2386. if(sd->state.lr_flag != 2)
  2387. sd->def2_rate += val;
  2388. break;
  2389. case SP_MDEF_RATE:
  2390. if(sd->state.lr_flag != 2)
  2391. sd->mdef_rate += val;
  2392. break;
  2393. case SP_MDEF2_RATE:
  2394. if(sd->state.lr_flag != 2)
  2395. sd->mdef2_rate += val;
  2396. break;
  2397. case SP_RESTART_FULL_RECOVER:
  2398. if(sd->state.lr_flag != 2)
  2399. sd->special_state.restart_full_recover = 1;
  2400. break;
  2401. case SP_NO_CASTCANCEL:
  2402. if(sd->state.lr_flag != 2)
  2403. sd->special_state.no_castcancel = 1;
  2404. break;
  2405. case SP_NO_CASTCANCEL2:
  2406. if(sd->state.lr_flag != 2)
  2407. sd->special_state.no_castcancel2 = 1;
  2408. break;
  2409. case SP_NO_SIZEFIX:
  2410. if(sd->state.lr_flag != 2)
  2411. sd->special_state.no_sizefix = 1;
  2412. break;
  2413. case SP_NO_MAGIC_DAMAGE:
  2414. if(sd->state.lr_flag == 2)
  2415. break;
  2416. val+= sd->special_state.no_magic_damage;
  2417. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2418. break;
  2419. case SP_NO_WEAPON_DAMAGE:
  2420. if(sd->state.lr_flag == 2)
  2421. break;
  2422. val+= sd->special_state.no_weapon_damage;
  2423. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2424. break;
  2425. case SP_NO_MISC_DAMAGE:
  2426. if(sd->state.lr_flag == 2)
  2427. break;
  2428. val+= sd->special_state.no_misc_damage;
  2429. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2430. break;
  2431. case SP_NO_GEMSTONE:
  2432. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2433. sd->special_state.no_gemstone = 1;
  2434. break;
  2435. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2436. if(sd->state.lr_flag != 2) {
  2437. sd->special_state.intravision = 1;
  2438. clif_status_load(&sd->bl, SI_INTRAVISION, 1);
  2439. }
  2440. break;
  2441. case SP_NO_KNOCKBACK:
  2442. if(sd->state.lr_flag != 2)
  2443. sd->special_state.no_knockback = 1;
  2444. break;
  2445. case SP_SPLASH_RANGE:
  2446. if(sd->bonus.splash_range < val)
  2447. sd->bonus.splash_range = val;
  2448. break;
  2449. case SP_SPLASH_ADD_RANGE:
  2450. sd->bonus.splash_add_range += val;
  2451. break;
  2452. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2453. if(sd->state.lr_flag != 2)
  2454. sd->bonus.short_weapon_damage_return += val;
  2455. break;
  2456. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2457. if(sd->state.lr_flag != 2)
  2458. sd->bonus.long_weapon_damage_return += val;
  2459. break;
  2460. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2461. if(sd->state.lr_flag != 2)
  2462. sd->bonus.magic_damage_return += val;
  2463. break;
  2464. case SP_ALL_STATS: // [Valaris]
  2465. if(sd->state.lr_flag!=2) {
  2466. sd->param_bonus[SP_STR-SP_STR]+=val;
  2467. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2468. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2469. sd->param_bonus[SP_INT-SP_STR]+=val;
  2470. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2471. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2472. }
  2473. break;
  2474. case SP_AGI_VIT: // [Valaris]
  2475. if(sd->state.lr_flag!=2) {
  2476. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2477. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2478. }
  2479. break;
  2480. case SP_AGI_DEX_STR: // [Valaris]
  2481. if(sd->state.lr_flag!=2) {
  2482. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2483. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2484. sd->param_bonus[SP_STR-SP_STR]+=val;
  2485. }
  2486. break;
  2487. case SP_PERFECT_HIDE: // [Valaris]
  2488. if(sd->state.lr_flag!=2)
  2489. sd->special_state.perfect_hiding=1;
  2490. break;
  2491. case SP_UNBREAKABLE:
  2492. if(sd->state.lr_flag!=2)
  2493. sd->bonus.unbreakable += val;
  2494. break;
  2495. case SP_UNBREAKABLE_WEAPON:
  2496. if(sd->state.lr_flag != 2)
  2497. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2498. break;
  2499. case SP_UNBREAKABLE_ARMOR:
  2500. if(sd->state.lr_flag != 2)
  2501. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2502. break;
  2503. case SP_UNBREAKABLE_HELM:
  2504. if(sd->state.lr_flag != 2)
  2505. sd->bonus.unbreakable_equip |= EQP_HELM;
  2506. break;
  2507. case SP_UNBREAKABLE_SHIELD:
  2508. if(sd->state.lr_flag != 2)
  2509. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2510. break;
  2511. case SP_UNBREAKABLE_GARMENT:
  2512. if(sd->state.lr_flag != 2)
  2513. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2514. break;
  2515. case SP_UNBREAKABLE_SHOES:
  2516. if(sd->state.lr_flag != 2)
  2517. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2518. break;
  2519. case SP_CLASSCHANGE: // [Valaris]
  2520. if(sd->state.lr_flag !=2)
  2521. sd->bonus.classchange=val;
  2522. break;
  2523. case SP_LONG_ATK_RATE:
  2524. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2525. sd->bonus.long_attack_atk_rate+=val;
  2526. break;
  2527. case SP_BREAK_WEAPON_RATE:
  2528. if(sd->state.lr_flag != 2)
  2529. sd->bonus.break_weapon_rate+=val;
  2530. break;
  2531. case SP_BREAK_ARMOR_RATE:
  2532. if(sd->state.lr_flag != 2)
  2533. sd->bonus.break_armor_rate+=val;
  2534. break;
  2535. case SP_ADD_STEAL_RATE:
  2536. if(sd->state.lr_flag != 2)
  2537. sd->bonus.add_steal_rate+=val;
  2538. break;
  2539. case SP_DELAYRATE:
  2540. if(sd->state.lr_flag != 2)
  2541. sd->delayrate+=val;
  2542. break;
  2543. case SP_CRIT_ATK_RATE:
  2544. if(sd->state.lr_flag != 2)
  2545. sd->bonus.crit_atk_rate += val;
  2546. break;
  2547. case SP_NO_REGEN:
  2548. if(sd->state.lr_flag != 2)
  2549. sd->regen.state.block|=val;
  2550. break;
  2551. case SP_UNSTRIPABLE_WEAPON:
  2552. if(sd->state.lr_flag != 2)
  2553. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2554. break;
  2555. case SP_UNSTRIPABLE:
  2556. case SP_UNSTRIPABLE_ARMOR:
  2557. if(sd->state.lr_flag != 2)
  2558. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2559. break;
  2560. case SP_UNSTRIPABLE_HELM:
  2561. if(sd->state.lr_flag != 2)
  2562. sd->bonus.unstripable_equip |= EQP_HELM;
  2563. break;
  2564. case SP_UNSTRIPABLE_SHIELD:
  2565. if(sd->state.lr_flag != 2)
  2566. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2567. break;
  2568. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2569. if(!sd->state.lr_flag) {
  2570. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2571. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2572. } else if(sd->state.lr_flag == 1) {
  2573. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2574. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2575. }
  2576. break;
  2577. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2578. if(!sd->state.lr_flag) {
  2579. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2580. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2581. } else if(sd->state.lr_flag == 1) {
  2582. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2583. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2584. }
  2585. break;
  2586. case SP_SP_GAIN_VALUE:
  2587. if(!sd->state.lr_flag)
  2588. sd->bonus.sp_gain_value += val;
  2589. break;
  2590. case SP_HP_GAIN_VALUE:
  2591. if(!sd->state.lr_flag)
  2592. sd->bonus.hp_gain_value += val;
  2593. break;
  2594. case SP_MAGIC_SP_GAIN_VALUE:
  2595. if(!sd->state.lr_flag)
  2596. sd->bonus.magic_sp_gain_value += val;
  2597. break;
  2598. case SP_MAGIC_HP_GAIN_VALUE:
  2599. if(!sd->state.lr_flag)
  2600. sd->bonus.magic_hp_gain_value += val;
  2601. break;
  2602. case SP_ADD_HEAL_RATE:
  2603. if(sd->state.lr_flag != 2)
  2604. sd->bonus.add_heal_rate += val;
  2605. break;
  2606. case SP_ADD_HEAL2_RATE:
  2607. if(sd->state.lr_flag != 2)
  2608. sd->bonus.add_heal2_rate += val;
  2609. break;
  2610. case SP_ADD_ITEM_HEAL_RATE:
  2611. if(sd->state.lr_flag != 2)
  2612. sd->bonus.itemhealrate2 += val;
  2613. break;
  2614. case SP_EMATK:
  2615. if(sd->state.lr_flag != 2)
  2616. sd->bonus.ematk += val;
  2617. break;
  2618. #ifdef RENEWAL_CAST
  2619. case SP_FIXCASTRATE:
  2620. if(sd->state.lr_flag != 2)
  2621. FIXEDCASTRATE(sd->bonus.fixcastrate,val);
  2622. break;
  2623. case SP_ADD_FIXEDCAST:
  2624. if(sd->state.lr_flag != 2)
  2625. sd->bonus.add_fixcast += val;
  2626. break;
  2627. case SP_CASTRATE:
  2628. case SP_VARCASTRATE:
  2629. if(sd->state.lr_flag != 2)
  2630. sd->bonus.varcastrate += val;
  2631. break;
  2632. case SP_ADD_VARIABLECAST:
  2633. if(sd->state.lr_flag != 2)
  2634. sd->bonus.add_varcast += val;
  2635. break;
  2636. #else
  2637. case SP_ADD_FIXEDCAST:
  2638. case SP_FIXCASTRATE:
  2639. case SP_ADD_VARIABLECAST:
  2640. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2641. break;
  2642. case SP_VARCASTRATE:
  2643. case SP_CASTRATE:
  2644. if(sd->state.lr_flag != 2)
  2645. sd->castrate += val;
  2646. break;
  2647. #endif
  2648. case SP_ADDMAXWEIGHT:
  2649. if (sd->state.lr_flag != 2)
  2650. sd->max_weight += val;
  2651. break;
  2652. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  2653. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  2654. break;
  2655. default:
  2656. ShowWarning("pc_bonus: unknown type %d %d !\n",type,val);
  2657. break;
  2658. }
  2659. }
  2660. /*==========================================
  2661. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2662. * format: bonus2 bBonusName,type2,val;
  2663. * @param sd
  2664. * @param type Bonus type used by bBonusName
  2665. * @param type2
  2666. * @param val Value that usually for rate or fixed value
  2667. *------------------------------------------*/
  2668. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2669. {
  2670. int i;
  2671. nullpo_retv(sd);
  2672. switch(type){
  2673. case SP_ADDELE: // bonus2 bAddEle,e,x;
  2674. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  2675. if(!sd->state.lr_flag)
  2676. sd->right_weapon.addele[type2]+=val;
  2677. else if(sd->state.lr_flag == 1)
  2678. sd->left_weapon.addele[type2]+=val;
  2679. else if(sd->state.lr_flag == 2)
  2680. sd->arrow_addele[type2]+=val;
  2681. break;
  2682. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  2683. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  2684. if(!sd->state.lr_flag)
  2685. sd->right_weapon.addrace[type2]+=val;
  2686. else if(sd->state.lr_flag == 1)
  2687. sd->left_weapon.addrace[type2]+=val;
  2688. else if(sd->state.lr_flag == 2)
  2689. sd->arrow_addrace[type2]+=val;
  2690. break;
  2691. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  2692. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  2693. if(!sd->state.lr_flag)
  2694. sd->right_weapon.addclass[type2]+=val;
  2695. else if(sd->state.lr_flag == 1)
  2696. sd->left_weapon.addclass[type2]+=val;
  2697. else if(sd->state.lr_flag == 2)
  2698. sd->arrow_addclass[type2]+=val;
  2699. break;
  2700. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  2701. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  2702. if(!sd->state.lr_flag)
  2703. sd->right_weapon.addsize[type2]+=val;
  2704. else if(sd->state.lr_flag == 1)
  2705. sd->left_weapon.addsize[type2]+=val;
  2706. else if(sd->state.lr_flag == 2)
  2707. sd->arrow_addsize[type2]+=val;
  2708. break;
  2709. case SP_SUBELE: // bonus2 bSubEle,e,x;
  2710. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  2711. if(sd->state.lr_flag != 2)
  2712. sd->subele[type2]+=val;
  2713. break;
  2714. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  2715. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  2716. if(sd->state.lr_flag != 2)
  2717. sd->subrace[type2]+=val;
  2718. break;
  2719. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  2720. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  2721. if(sd->state.lr_flag != 2)
  2722. sd->subclass[type2]+=val;
  2723. break;
  2724. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  2725. if (type2 <= SC_NONE || type2 >= SC_MAX) {
  2726. ShowError("pc_bonus2: SP_ADDEFF: %d invalid effect.\n", type2);
  2727. break;
  2728. }
  2729. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2730. sd->state.lr_flag!=2?val:0, sd->state.lr_flag==2?val:0, 0);
  2731. break;
  2732. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  2733. if (type2 <= SC_NONE || type2 >= SC_MAX) {
  2734. ShowError("pc_bonus2: SP_ADDEFF2: %d is invalid effect.\n", type2);
  2735. break;
  2736. }
  2737. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2738. sd->state.lr_flag!=2?val:0, sd->state.lr_flag==2?val:0, ATF_SELF);
  2739. break;
  2740. case SP_RESEFF: // bonus2 bResEff,eff,n;
  2741. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2742. ShowError("pc_bonus2: SP_RESEFF: %d is invalid effect.\n", type2);
  2743. break;
  2744. }
  2745. if(sd->state.lr_flag == 2)
  2746. break;
  2747. i = sd->reseff[type2]+val;
  2748. sd->reseff[type2]= cap_value(i, -10000, 10000);
  2749. break;
  2750. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  2751. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  2752. if(sd->state.lr_flag != 2)
  2753. sd->magic_addele[type2]+=val;
  2754. break;
  2755. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  2756. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  2757. if(sd->state.lr_flag != 2)
  2758. sd->magic_addrace[type2]+=val;
  2759. break;
  2760. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  2761. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  2762. if(sd->state.lr_flag != 2)
  2763. sd->magic_addclass[type2]+=val;
  2764. break;
  2765. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  2766. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  2767. if(sd->state.lr_flag != 2)
  2768. sd->magic_addsize[type2]+=val;
  2769. break;
  2770. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  2771. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  2772. if(sd->state.lr_flag != 2)
  2773. sd->magic_atk_ele[type2]+=val;
  2774. break;
  2775. case SP_ADD_DAMAGE_CLASS: // bonus2 bAddDamageClass,mid,x;
  2776. switch (sd->state.lr_flag) {
  2777. case 0: //Right hand
  2778. 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);
  2779. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2780. {
  2781. 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));
  2782. break;
  2783. }
  2784. sd->right_weapon.add_dmg[i].class_ = type2;
  2785. sd->right_weapon.add_dmg[i].rate += val;
  2786. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2787. 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]));
  2788. break;
  2789. case 1: //Left hand
  2790. 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);
  2791. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2792. {
  2793. 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));
  2794. break;
  2795. }
  2796. sd->left_weapon.add_dmg[i].class_ = type2;
  2797. sd->left_weapon.add_dmg[i].rate += val;
  2798. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2799. 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]));
  2800. break;
  2801. }
  2802. break;
  2803. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  2804. if(sd->state.lr_flag == 2)
  2805. break;
  2806. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2807. if (i == ARRAYLENGTH(sd->add_mdmg))
  2808. {
  2809. 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));
  2810. break;
  2811. }
  2812. sd->add_mdmg[i].class_ = type2;
  2813. sd->add_mdmg[i].rate += val;
  2814. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2815. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2816. break;
  2817. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  2818. if(sd->state.lr_flag == 2)
  2819. break;
  2820. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2821. if (i == ARRAYLENGTH(sd->add_def))
  2822. {
  2823. ShowError("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2824. break;
  2825. }
  2826. sd->add_def[i].class_ = type2;
  2827. sd->add_def[i].rate += val;
  2828. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2829. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2830. break;
  2831. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  2832. if(sd->state.lr_flag == 2)
  2833. break;
  2834. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2835. if (i == ARRAYLENGTH(sd->add_mdef))
  2836. {
  2837. ShowError("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2838. break;
  2839. }
  2840. sd->add_mdef[i].class_ = type2;
  2841. sd->add_mdef[i].rate += val;
  2842. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  2843. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  2844. break;
  2845. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  2846. if(!sd->state.lr_flag) {
  2847. sd->right_weapon.hp_drain_rate.rate += type2;
  2848. sd->right_weapon.hp_drain_rate.per += val;
  2849. }
  2850. else if(sd->state.lr_flag == 1) {
  2851. sd->left_weapon.hp_drain_rate.rate += type2;
  2852. sd->left_weapon.hp_drain_rate.per += val;
  2853. }
  2854. break;
  2855. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  2856. if(!sd->state.lr_flag) {
  2857. sd->right_weapon.sp_drain_rate.rate += type2;
  2858. sd->right_weapon.sp_drain_rate.per += val;
  2859. }
  2860. else if(sd->state.lr_flag == 1) {
  2861. sd->left_weapon.sp_drain_rate.rate += type2;
  2862. sd->left_weapon.sp_drain_rate.per += val;
  2863. }
  2864. break;
  2865. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  2866. if(sd->state.lr_flag != 2) {
  2867. sd->bonus.sp_vanish_rate += type2;
  2868. sd->bonus.sp_vanish_per += val;
  2869. }
  2870. break;
  2871. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  2872. if(sd->state.lr_flag != 2) {
  2873. sd->bonus.hp_vanish_rate += type2;
  2874. sd->bonus.hp_vanish_per += val;
  2875. }
  2876. break;
  2877. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  2878. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  2879. sd->bonus.get_zeny_rate = val;
  2880. sd->bonus.get_zeny_num = type2;
  2881. }
  2882. break;
  2883. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  2884. if(sd->state.lr_flag != 2) {
  2885. sd->bonus.get_zeny_rate += val;
  2886. sd->bonus.get_zeny_num += type2;
  2887. }
  2888. break;
  2889. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  2890. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  2891. if(sd->state.lr_flag == 2)
  2892. break;
  2893. sd->weapon_coma_ele[type2] += val;
  2894. sd->special_state.bonus_coma = 1;
  2895. break;
  2896. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  2897. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  2898. if(sd->state.lr_flag == 2)
  2899. break;
  2900. sd->weapon_coma_race[type2] += val;
  2901. sd->special_state.bonus_coma = 1;
  2902. break;
  2903. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  2904. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  2905. if(sd->state.lr_flag == 2)
  2906. break;
  2907. sd->weapon_coma_class[type2] += val;
  2908. sd->special_state.bonus_coma = 1;
  2909. break;
  2910. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  2911. if(sd->state.lr_flag != 2)
  2912. sd->weapon_atk[type2]+=val;
  2913. break;
  2914. case SP_WEAPON_ATK_RATE: // bonus2 bWeaponAtkRate,w,n;
  2915. if(sd->state.lr_flag != 2)
  2916. sd->weapon_atk_rate[type2]+=val;
  2917. break;
  2918. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  2919. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  2920. if(sd->state.lr_flag != 2)
  2921. sd->critaddrace[type2] += val*10;
  2922. break;
  2923. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  2924. if (type2 <= SC_NONE || type2 >= SC_MAX) {
  2925. ShowError("pc_bonus2: SP_ADDEFF_WHENHIT: %d is invalid effect.\n", type2);
  2926. break;
  2927. }
  2928. if(sd->state.lr_flag != 2)
  2929. pc_bonus_addeff(sd->addeff2, ARRAYLENGTH(sd->addeff2), (sc_type)type2, val, 0, 0);
  2930. break;
  2931. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  2932. if(sd->state.lr_flag == 2)
  2933. break;
  2934. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  2935. if (i == ARRAYLENGTH(sd->skillatk))
  2936. { //Better mention this so the array length can be updated. [Skotlex]
  2937. 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);
  2938. break;
  2939. }
  2940. if (sd->skillatk[i].id == type2)
  2941. sd->skillatk[i].val += val;
  2942. else {
  2943. sd->skillatk[i].id = type2;
  2944. sd->skillatk[i].val = val;
  2945. }
  2946. break;
  2947. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  2948. if(sd->state.lr_flag == 2)
  2949. break;
  2950. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  2951. if (i == ARRAYLENGTH(sd->skillheal))
  2952. { // Better mention this so the array length can be updated. [Skotlex]
  2953. 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);
  2954. break;
  2955. }
  2956. if (sd->skillheal[i].id == type2)
  2957. sd->skillheal[i].val += val;
  2958. else {
  2959. sd->skillheal[i].id = type2;
  2960. sd->skillheal[i].val = val;
  2961. }
  2962. break;
  2963. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  2964. if(sd->state.lr_flag == 2)
  2965. break;
  2966. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  2967. if (i == ARRAYLENGTH(sd->skillheal2))
  2968. { // Better mention this so the array length can be updated. [Skotlex]
  2969. 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);
  2970. break;
  2971. }
  2972. if (sd->skillheal2[i].id == type2)
  2973. sd->skillheal2[i].val += val;
  2974. else {
  2975. sd->skillheal2[i].id = type2;
  2976. sd->skillheal2[i].val = val;
  2977. }
  2978. break;
  2979. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  2980. if(sd->state.lr_flag == 2)
  2981. break;
  2982. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  2983. if (i == ARRAYLENGTH(sd->skillblown))
  2984. { //Better mention this so the array length can be updated. [Skotlex]
  2985. 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);
  2986. break;
  2987. }
  2988. if(sd->skillblown[i].id == type2)
  2989. sd->skillblown[i].val += val;
  2990. else {
  2991. sd->skillblown[i].id = type2;
  2992. sd->skillblown[i].val = val;
  2993. }
  2994. break;
  2995. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  2996. if(sd->state.lr_flag != 2) {
  2997. sd->hp_loss.value = type2;
  2998. sd->hp_loss.rate = val;
  2999. }
  3000. break;
  3001. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3002. if(sd->state.lr_flag != 2) {
  3003. sd->hp_regen.value = type2;
  3004. sd->hp_regen.rate = val;
  3005. }
  3006. break;
  3007. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3008. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3009. if(sd->state.lr_flag != 2)
  3010. sd->right_weapon.addrace2[type2] += val;
  3011. else
  3012. sd->left_weapon.addrace2[type2] += val;
  3013. break;
  3014. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3015. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3016. if(sd->state.lr_flag != 2)
  3017. sd->subsize[type2]+=val;
  3018. break;
  3019. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3020. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3021. if(sd->state.lr_flag != 2)
  3022. sd->subrace2[type2]+=val;
  3023. break;
  3024. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3025. if(sd->state.lr_flag == 2)
  3026. break;
  3027. if (!itemdb_exists(type2)) {
  3028. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3029. break;
  3030. }
  3031. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  3032. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  3033. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  3034. break;
  3035. }
  3036. sd->itemhealrate[i].nameid = type2;
  3037. sd->itemhealrate[i].rate += val;
  3038. break;
  3039. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3040. if (sd->state.lr_flag == 2)
  3041. break;
  3042. if (!type2 || !itemdb_group_exists(type2)) {
  3043. ShowError("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3044. break;
  3045. }
  3046. pc_itemgrouphealrate(sd, type2, val);
  3047. break;
  3048. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3049. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3050. if(sd->state.lr_flag != 2)
  3051. sd->expaddrace[type2]+=val;
  3052. break;
  3053. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3054. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3055. if(sd->state.lr_flag != 2)
  3056. sd->expaddclass[type2]+=val;
  3057. break;
  3058. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3059. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3060. if(sd->state.lr_flag != 2)
  3061. sd->sp_gain_race[type2]+=val;
  3062. break;
  3063. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3064. if (sd->state.lr_flag != 2)
  3065. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_ALL, RC_NONE_, val);
  3066. break;
  3067. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3068. if (sd->state.lr_flag != 2)
  3069. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_ALL, RC_NONE_, val);
  3070. break;
  3071. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3072. if(sd->state.lr_flag != 2) {
  3073. sd->sp_loss.value = type2;
  3074. sd->sp_loss.rate = val;
  3075. }
  3076. break;
  3077. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3078. if(sd->state.lr_flag != 2) {
  3079. sd->sp_regen.value = type2;
  3080. sd->sp_regen.rate = val;
  3081. }
  3082. break;
  3083. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3084. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3085. if(!sd->state.lr_flag) {
  3086. sd->right_weapon.hp_drain_race[type2] += val;
  3087. }
  3088. else if(sd->state.lr_flag == 1) {
  3089. sd->left_weapon.hp_drain_race[type2] += val;
  3090. }
  3091. break;
  3092. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3093. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3094. if(!sd->state.lr_flag) {
  3095. sd->right_weapon.sp_drain_race[type2] += val;
  3096. }
  3097. else if(sd->state.lr_flag == 1) {
  3098. sd->left_weapon.sp_drain_race[type2] += val;
  3099. }
  3100. break;
  3101. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3102. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3103. if(!sd->state.lr_flag) {
  3104. sd->right_weapon.hp_drain_class[type2] += val;
  3105. }
  3106. else if(sd->state.lr_flag == 1) {
  3107. sd->left_weapon.hp_drain_class[type2] += val;
  3108. }
  3109. break;
  3110. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3111. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3112. if(!sd->state.lr_flag) {
  3113. sd->right_weapon.sp_drain_class[type2] += val;
  3114. }
  3115. else if(sd->state.lr_flag == 1) {
  3116. sd->left_weapon.sp_drain_class[type2] += val;
  3117. }
  3118. break;
  3119. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3120. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3121. if(sd->state.lr_flag != 2)
  3122. sd->ignore_mdef_by_race[type2] += val;
  3123. break;
  3124. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3125. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3126. if(sd->state.lr_flag != 2)
  3127. sd->ignore_mdef_by_class[type2] += val;
  3128. break;
  3129. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3130. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3131. if(sd->state.lr_flag != 2)
  3132. sd->ignore_def_by_race[type2] += val;
  3133. break;
  3134. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3135. if(sd->state.lr_flag == 2)
  3136. break;
  3137. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  3138. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  3139. 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);
  3140. break;
  3141. }
  3142. if (sd->skillusesprate[i].id == type2)
  3143. sd->skillusesprate[i].val += val;
  3144. else {
  3145. sd->skillusesprate[i].id = type2;
  3146. sd->skillusesprate[i].val = val;
  3147. }
  3148. break;
  3149. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3150. if(sd->state.lr_flag == 2)
  3151. break;
  3152. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  3153. if (i == ARRAYLENGTH(sd->skillcooldown))
  3154. {
  3155. 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);
  3156. break;
  3157. }
  3158. if (sd->skillcooldown[i].id == type2)
  3159. sd->skillcooldown[i].val += val;
  3160. else {
  3161. sd->skillcooldown[i].id = type2;
  3162. sd->skillcooldown[i].val = val;
  3163. }
  3164. break;
  3165. #ifdef RENEWAL_CAST
  3166. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3167. if(sd->state.lr_flag == 2)
  3168. break;
  3169. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  3170. if (i == ARRAYLENGTH(sd->skillfixcast))
  3171. {
  3172. 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);
  3173. break;
  3174. }
  3175. if (sd->skillfixcast[i].id == type2)
  3176. sd->skillfixcast[i].val += val;
  3177. else {
  3178. sd->skillfixcast[i].id = type2;
  3179. sd->skillfixcast[i].val = val;
  3180. }
  3181. break;
  3182. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3183. if(sd->state.lr_flag == 2)
  3184. break;
  3185. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  3186. if (i == ARRAYLENGTH(sd->skillvarcast))
  3187. {
  3188. 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);
  3189. break;
  3190. }
  3191. if (sd->skillvarcast[i].id == type2)
  3192. sd->skillvarcast[i].val += val;
  3193. else {
  3194. sd->skillvarcast[i].id = type2;
  3195. sd->skillvarcast[i].val = val;
  3196. }
  3197. break;
  3198. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3199. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3200. if(sd->state.lr_flag == 2)
  3201. break;
  3202. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3203. if (i == ARRAYLENGTH(sd->skillcastrate))
  3204. {
  3205. ShowError("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",ARRAYLENGTH(sd->skillcastrate), type2, val);
  3206. break;
  3207. }
  3208. if(sd->skillcastrate[i].id == type2)
  3209. sd->skillcastrate[i].val += val;
  3210. else {
  3211. sd->skillcastrate[i].id = type2;
  3212. sd->skillcastrate[i].val += val;
  3213. }
  3214. break;
  3215. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3216. if(sd->state.lr_flag == 2)
  3217. break;
  3218. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  3219. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  3220. {
  3221. ShowError("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  3222. break;
  3223. }
  3224. if(sd->skillfixcastrate[i].id == type2)
  3225. FIXEDCASTRATE(sd->skillfixcastrate[i].val,val);
  3226. else {
  3227. sd->skillfixcastrate[i].id = type2;
  3228. sd->skillfixcastrate[i].val = val;
  3229. }
  3230. break;
  3231. #else
  3232. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3233. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3234. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3235. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3236. break;
  3237. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3238. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3239. if(sd->state.lr_flag == 2)
  3240. break;
  3241. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3242. if (i == ARRAYLENGTH(sd->skillcastrate))
  3243. { //Better mention this so the array length can be updated. [Skotlex]
  3244. ShowError("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",
  3245. (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", ARRAYLENGTH(sd->skillcastrate), type2, val);
  3246. break;
  3247. }
  3248. if(sd->skillcastrate[i].id == type2)
  3249. sd->skillcastrate[i].val += val;
  3250. else {
  3251. sd->skillcastrate[i].id = type2;
  3252. sd->skillcastrate[i].val = val;
  3253. }
  3254. break;
  3255. #endif
  3256. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3257. if(sd->state.lr_flag == 2)
  3258. break;
  3259. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  3260. if (i == ARRAYLENGTH(sd->skillusesp)) {
  3261. 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);
  3262. break;
  3263. }
  3264. if (sd->skillusesp[i].id == type2)
  3265. sd->skillusesp[i].val += val;
  3266. else {
  3267. sd->skillusesp[i].id = type2;
  3268. sd->skillusesp[i].val = val;
  3269. }
  3270. break;
  3271. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3272. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == type2 || sd->subskill[i].id == 0);
  3273. if (i == ARRAYLENGTH(sd->subskill)) {
  3274. 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);
  3275. break;
  3276. }
  3277. if (sd->subskill[i].id == type2)
  3278. sd->subskill[i].val += val;
  3279. else {
  3280. sd->subskill[i].id = type2;
  3281. sd->subskill[i].val = val;
  3282. }
  3283. break;
  3284. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3285. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3286. sd->subdefele[type2] += val;
  3287. break;
  3288. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3289. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3290. sd->coma_class[type2] += val;
  3291. sd->special_state.bonus_coma = 1;
  3292. break;
  3293. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3294. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3295. sd->coma_race[type2] += val;
  3296. sd->special_state.bonus_coma = 1;
  3297. break;
  3298. default:
  3299. ShowWarning("pc_bonus2: unknown type %d %d %d!\n",type,type2,val);
  3300. break;
  3301. }
  3302. }
  3303. /**
  3304. * Gives item bonus to player for format: bonus3 bBonusName,type2,val;
  3305. * @param sd
  3306. * @param type Bonus type used by bBonusName
  3307. * @param type2
  3308. * @param val Value that usually for rate or fixed value
  3309. */
  3310. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3311. {
  3312. nullpo_retv(sd);
  3313. switch(type){
  3314. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3315. if(sd->state.lr_flag != 2)
  3316. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, type3, val);
  3317. break;
  3318. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3319. if(sd->state.lr_flag != 2)
  3320. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, -type3, val);
  3321. break;
  3322. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3323. if(sd->state.lr_flag != 2)
  3324. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, type3, RC_NONE_, val);
  3325. break;
  3326. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3327. if(sd->state.lr_flag != 2)
  3328. {
  3329. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3330. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3331. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  3332. target?-type2:type2, type3, val, 0, current_equip_card_id);
  3333. }
  3334. break;
  3335. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3336. if(sd->state.lr_flag != 2)
  3337. {
  3338. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3339. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3340. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  3341. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3342. }
  3343. break;
  3344. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3345. if (sd->state.lr_flag != 2)
  3346. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_NONE, type3, val);
  3347. break;
  3348. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3349. if (sd->state.lr_flag != 2)
  3350. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, type3, RC_NONE_, val);
  3351. break;
  3352. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3353. if (type2 <= SC_NONE || type2 >= SC_MAX) {
  3354. ShowError("pc_bonus3: SP_ADDEFF: %d is not supported.\n", type2);
  3355. break;
  3356. }
  3357. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3358. sd->state.lr_flag!=2?type3:0, sd->state.lr_flag==2?type3:0, val);
  3359. break;
  3360. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3361. if (type2 <= SC_NONE || type2 >= SC_MAX) {
  3362. ShowError("pc_bonus3: SP_ADDEFF_WHENHIT: %d is not supported.\n", type2);
  3363. break;
  3364. }
  3365. if(sd->state.lr_flag != 2)
  3366. pc_bonus_addeff(sd->addeff2, ARRAYLENGTH(sd->addeff2), (sc_type)type2, type3, 0, val);
  3367. break;
  3368. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3369. if (type3 <= SC_NONE || type3 >= SC_MAX) {
  3370. ShowError("pc_bonus3: SP_ADDEFF_ONSKILL: %d is not supported.\n", type3);
  3371. break;
  3372. }
  3373. if( sd->state.lr_flag != 2 )
  3374. pc_bonus_addeff_onskill(sd->addeff3, ARRAYLENGTH(sd->addeff3), (sc_type)type3, val, type2, ATF_TARGET);
  3375. break;
  3376. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3377. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3378. if (sd->state.lr_flag != 2)
  3379. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3380. break;
  3381. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3382. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3383. if (sd->state.lr_flag != 2)
  3384. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3385. break;
  3386. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3387. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3388. if(sd->state.lr_flag != 2) {
  3389. sd->sp_vanish_race[type2].rate += type3;
  3390. sd->sp_vanish_race[type2].per += val;
  3391. }
  3392. break;
  3393. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3394. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3395. if(sd->state.lr_flag != 2) {
  3396. sd->hp_vanish_race[type2].rate += type3;
  3397. sd->hp_vanish_race[type2].per += val;
  3398. }
  3399. break;
  3400. default:
  3401. ShowWarning("pc_bonus3: unknown type %d %d %d %d!\n",type,type2,type3,val);
  3402. break;
  3403. }
  3404. }
  3405. /**
  3406. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,val;
  3407. * @param sd
  3408. * @param type Bonus type used by bBonusName
  3409. * @param type2
  3410. * @param type3
  3411. * @param val Value that usually for rate or fixed value
  3412. */
  3413. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3414. {
  3415. nullpo_retv(sd);
  3416. switch(type){
  3417. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3418. if(sd->state.lr_flag != 2)
  3419. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3420. break;
  3421. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3422. if(sd->state.lr_flag != 2)
  3423. 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);
  3424. break;
  3425. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3426. if(sd->state.lr_flag != 2)
  3427. {
  3428. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3429. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3430. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3431. }
  3432. break;
  3433. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3434. if (type2 <= SC_NONE || type2 >= SC_MAX) {
  3435. ShowError("pc_bonus4: SP_ADDEFF_ONSKILL: %d is not supported.\n", type2);
  3436. break;
  3437. }
  3438. if( sd->state.lr_flag != 2 )
  3439. pc_bonus_addeff_onskill(sd->addeff3, ARRAYLENGTH(sd->addeff3), (sc_type)type3, type4, type2, val);
  3440. break;
  3441. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3442. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3443. if(sd->state.lr_flag == 2)
  3444. break;
  3445. sd->def_set_race[type2].rate = type3;
  3446. sd->def_set_race[type2].tick = type4;
  3447. sd->def_set_race[type2].value = val;
  3448. break;
  3449. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3450. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3451. if(sd->state.lr_flag == 2)
  3452. break;
  3453. sd->mdef_set_race[type2].rate = type3;
  3454. sd->mdef_set_race[type2].tick = type4;
  3455. sd->mdef_set_race[type2].value = val;
  3456. break;
  3457. default:
  3458. ShowWarning("pc_bonus4: unknown type %d %d %d %d %d!\n",type,type2,type3,type4,val);
  3459. break;
  3460. }
  3461. }
  3462. /**
  3463. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3464. * @param sd
  3465. * @param type Bonus type used by bBonusName
  3466. * @param type2
  3467. * @param type3
  3468. * @param type4
  3469. * @param val Value that usually for rate or fixed value
  3470. */
  3471. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3472. {
  3473. nullpo_retv(sd);
  3474. switch(type){
  3475. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3476. if(sd->state.lr_flag != 2)
  3477. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3478. break;
  3479. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3480. if(sd->state.lr_flag != 2)
  3481. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3482. break;
  3483. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3484. if(sd->state.lr_flag != 2)
  3485. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3486. break;
  3487. default:
  3488. ShowWarning("pc_bonus5: unknown type %d %d %d %d %d %d!\n",type,type2,type3,type4,type5,val);
  3489. break;
  3490. }
  3491. }
  3492. /*==========================================
  3493. * Grants a player a given skill. Flag values are:
  3494. * 0 - Grant permanent skill to be bound to skill tree
  3495. * 1 - Grant an item skill (temporary)
  3496. * 2 - Like 1, except the level granted can stack with previously learned level.
  3497. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3498. *------------------------------------------*/
  3499. bool pc_skill(TBL_PC* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3500. uint16 idx = 0;
  3501. nullpo_ret(sd);
  3502. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3503. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3504. return false;
  3505. }
  3506. if (level > MAX_SKILL_LEVEL) {
  3507. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3508. return false;
  3509. }
  3510. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3511. 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);
  3512. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3513. }
  3514. switch (type) {
  3515. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3516. sd->status.skill[idx].id = skill_id;
  3517. sd->status.skill[idx].lv = level;
  3518. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3519. if (level == 0) { //Remove skill.
  3520. sd->status.skill[idx].id = 0;
  3521. clif_deleteskill(sd,skill_id);
  3522. } else
  3523. clif_addskill(sd,skill_id);
  3524. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3525. status_calc_pc(sd, SCO_NONE);
  3526. break;
  3527. case ADDSKILL_TEMP: //Item bonus skill.
  3528. if (sd->status.skill[idx].id != 0) {
  3529. if (sd->status.skill[idx].lv >= level)
  3530. return true;
  3531. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3532. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3533. } else {
  3534. sd->status.skill[idx].id = skill_id;
  3535. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3536. }
  3537. sd->status.skill[idx].lv = level;
  3538. break;
  3539. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3540. if (sd->status.skill[idx].id != 0) {
  3541. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3542. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3543. } else {
  3544. sd->status.skill[idx].id = skill_id;
  3545. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3546. }
  3547. sd->status.skill[idx].lv += level;
  3548. break;
  3549. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3550. sd->status.skill[idx].id = skill_id;
  3551. sd->status.skill[idx].lv = level;
  3552. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3553. if (level == 0) { //Remove skill.
  3554. sd->status.skill[idx].id = 0;
  3555. clif_deleteskill(sd,skill_id);
  3556. } else
  3557. clif_addskill(sd,skill_id);
  3558. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3559. status_calc_pc(sd, SCO_NONE);
  3560. break;
  3561. default:
  3562. return false;
  3563. }
  3564. return true;
  3565. }
  3566. /*==========================================
  3567. * Append a card to an item ?
  3568. *------------------------------------------*/
  3569. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3570. {
  3571. int i;
  3572. unsigned short nameid;
  3573. nullpo_ret(sd);
  3574. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3575. return 0; //Invalid item index.
  3576. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3577. return 0; //Invalid card index.
  3578. if( sd->status.inventory[idx_equip].nameid <= 0 || sd->status.inventory[idx_equip].amount < 1 )
  3579. return 0; // target item missing
  3580. if( sd->status.inventory[idx_card].nameid <= 0 || sd->status.inventory[idx_card].amount < 1 )
  3581. return 0; // target card missing
  3582. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3583. return 0; // only weapons and armor are allowed
  3584. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3585. return 0; // must be a card
  3586. if( sd->status.inventory[idx_equip].identify == 0 )
  3587. return 0; // target must be identified
  3588. if( itemdb_isspecial(sd->status.inventory[idx_equip].card[0]) )
  3589. return 0; // card slots reserved for other purposes
  3590. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3591. return 0; // card cannot be compounded on this item type
  3592. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3593. return 0; // attempted to place shield card on left-hand weapon.
  3594. if( sd->status.inventory[idx_equip].equip != 0 )
  3595. return 0; // item must be unequipped
  3596. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->status.inventory[idx_equip].card[i] == 0 );
  3597. if( i == sd->inventory_data[idx_equip]->slot )
  3598. return 0; // no free slots
  3599. // remember the card id to insert
  3600. nameid = sd->status.inventory[idx_card].nameid;
  3601. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3602. {// failed
  3603. clif_insert_card(sd,idx_equip,idx_card,1);
  3604. }
  3605. else
  3606. {// success
  3607. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->status.inventory[idx_equip]);
  3608. sd->status.inventory[idx_equip].card[i] = nameid;
  3609. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->status.inventory[idx_equip]);
  3610. clif_insert_card(sd,idx_equip,idx_card,0);
  3611. }
  3612. return 0;
  3613. }
  3614. //
  3615. // Items
  3616. //
  3617. /*==========================================
  3618. * Update buying value by skills
  3619. *------------------------------------------*/
  3620. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3621. {
  3622. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3623. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3624. rate1 = 5+skill*2-((skill==10)? 1:0);
  3625. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3626. rate2 = 5+skill*4;
  3627. if(rate1 < rate2) rate1 = rate2;
  3628. if(rate1)
  3629. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3630. if(val < 0) val = 0;
  3631. if(orig_value > 0 && val < 1) val = 1;
  3632. return val;
  3633. }
  3634. /*==========================================
  3635. * Update selling value by skills
  3636. *------------------------------------------*/
  3637. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3638. {
  3639. int skill,val = orig_value,rate = 0;
  3640. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3641. rate = 5+skill*2-((skill==10)? 1:0);
  3642. if(rate)
  3643. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3644. if(val < 0) val = 0;
  3645. if(orig_value > 0 && val < 1) val = 1;
  3646. return val;
  3647. }
  3648. /*==========================================
  3649. * Checking if we have enough place on inventory for new item
  3650. * Make sure to take 30k as limit (for client I guess)
  3651. * @param sd
  3652. * @param nameid
  3653. * @param amount
  3654. * @return e_chkitem_result
  3655. *------------------------------------------*/
  3656. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  3657. {
  3658. int i;
  3659. struct item_data* data;
  3660. nullpo_ret(sd);
  3661. if(amount > MAX_AMOUNT)
  3662. return CHKADDITEM_OVERAMOUNT;
  3663. data = itemdb_search(nameid);
  3664. if(!itemdb_isstackable2(data))
  3665. return CHKADDITEM_NEW;
  3666. if( data->stack.inventory && amount > data->stack.amount )
  3667. return CHKADDITEM_OVERAMOUNT;
  3668. for(i=0;i<MAX_INVENTORY;i++){
  3669. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3670. if(sd->status.inventory[i].nameid==nameid){
  3671. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
  3672. return CHKADDITEM_OVERAMOUNT;
  3673. return CHKADDITEM_EXIST;
  3674. }
  3675. }
  3676. return CHKADDITEM_NEW;
  3677. }
  3678. /*==========================================
  3679. * Return number of available place in inventory
  3680. * Each non stackable item will reduce place by 1
  3681. * @param sd
  3682. * @return Number of empty slots
  3683. *------------------------------------------*/
  3684. uint8 pc_inventoryblank(struct map_session_data *sd)
  3685. {
  3686. uint8 i, b;
  3687. nullpo_ret(sd);
  3688. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  3689. if(sd->status.inventory[i].nameid==0)
  3690. b++;
  3691. }
  3692. return b;
  3693. }
  3694. /*==========================================
  3695. * attempts to remove zeny from player (sd)
  3696. * @param sd
  3697. * @param zeny Zeny removed
  3698. * @param type e_log_pick_type
  3699. * @param tsd (just for log?)
  3700. * @return 0 - Success, 1 - Failed
  3701. *------------------------------------------*/
  3702. char pc_payzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3703. {
  3704. nullpo_retr(2,sd);
  3705. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3706. if( zeny < 0 )
  3707. {
  3708. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3709. return 1;
  3710. }
  3711. if( sd->status.zeny < zeny )
  3712. return 1; //Not enough.
  3713. sd->status.zeny -= zeny;
  3714. clif_updatestatus(sd,SP_ZENY);
  3715. if(!tsd) tsd = sd;
  3716. log_zeny(sd, type, tsd, -zeny);
  3717. if( zeny > 0 && sd->state.showzeny ) {
  3718. char output[255];
  3719. sprintf(output, "Removed %dz.", zeny);
  3720. clif_disp_onlyself(sd,output,strlen(output));
  3721. }
  3722. return 0;
  3723. }
  3724. /** Makes player pays by using cash points
  3725. * @param sd Player who pays
  3726. * @param price How many point player has to pay
  3727. * @param points
  3728. * @param type e_log_pick_type
  3729. * @return -2: Paying negative points, -1: Not enough points, otherwise is succes (cash+points)
  3730. */
  3731. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type ){
  3732. int cash;
  3733. nullpo_retr(-1,sd);
  3734. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3735. if( price < 0 || points < 0 )
  3736. {
  3737. 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);
  3738. return -2;
  3739. }
  3740. if( points > price )
  3741. {
  3742. 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);
  3743. points = price;
  3744. }
  3745. cash = price-points;
  3746. if( sd->cashPoints < cash || sd->kafraPoints < points )
  3747. {
  3748. 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);
  3749. return -1;
  3750. }
  3751. pc_setaccountreg(sd, "#CASHPOINTS", sd->cashPoints-cash);
  3752. if( cash ){
  3753. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  3754. }
  3755. pc_setaccountreg(sd, "#KAFRAPOINTS", sd->kafraPoints-points);
  3756. if( points ){
  3757. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  3758. }
  3759. if( battle_config.cashshop_show_points )
  3760. {
  3761. char output[128];
  3762. sprintf(output, msg_txt(sd,504), points, cash, sd->kafraPoints, sd->cashPoints);
  3763. clif_disp_onlyself(sd, output, strlen(output));
  3764. }
  3765. return cash+points;
  3766. }
  3767. int pc_getcash( struct map_session_data *sd, int cash, int points, e_log_pick_type type ){
  3768. char output[128];
  3769. nullpo_retr(-1,sd);
  3770. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3771. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3772. if( cash > 0 )
  3773. {
  3774. if( cash > MAX_ZENY-sd->cashPoints )
  3775. {
  3776. 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);
  3777. cash = MAX_ZENY-sd->cashPoints;
  3778. }
  3779. pc_setaccountreg(sd, "#CASHPOINTS", sd->cashPoints+cash);
  3780. if( cash ){
  3781. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  3782. }
  3783. if( battle_config.cashshop_show_points )
  3784. {
  3785. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  3786. clif_disp_onlyself(sd, output, strlen(output));
  3787. }
  3788. return cash;
  3789. }
  3790. else if( cash < 0 )
  3791. {
  3792. 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);
  3793. return -1;
  3794. }
  3795. if( points > 0 )
  3796. {
  3797. if( points > MAX_ZENY-sd->kafraPoints )
  3798. {
  3799. 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);
  3800. points = MAX_ZENY-sd->kafraPoints;
  3801. }
  3802. pc_setaccountreg(sd, "#KAFRAPOINTS", sd->kafraPoints+points);
  3803. if( points ){
  3804. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  3805. }
  3806. if( battle_config.cashshop_show_points )
  3807. {
  3808. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  3809. clif_disp_onlyself(sd, output, strlen(output));
  3810. }
  3811. return points;
  3812. }
  3813. else if( points < 0 )
  3814. {
  3815. 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);
  3816. return -1;
  3817. }
  3818. return -2; //shouldn't happen but jsut in case
  3819. }
  3820. /*==========================================
  3821. * Attempts to give zeny to player (sd)
  3822. * tsd (optional) from who for log (if null take sd)
  3823. *------------------------------------------*/
  3824. char pc_getzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3825. {
  3826. nullpo_retr(-1,sd);
  3827. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3828. if( zeny < 0 )
  3829. {
  3830. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3831. return 1;
  3832. }
  3833. if( zeny > MAX_ZENY - sd->status.zeny )
  3834. zeny = MAX_ZENY - sd->status.zeny;
  3835. sd->status.zeny += zeny;
  3836. clif_updatestatus(sd,SP_ZENY);
  3837. if(!tsd) tsd = sd;
  3838. log_zeny(sd, type, tsd, zeny);
  3839. if( zeny > 0 && sd->state.showzeny ) {
  3840. char output[255];
  3841. sprintf(output, "Gained %dz.", zeny);
  3842. clif_disp_onlyself(sd,output,strlen(output));
  3843. }
  3844. return 0;
  3845. }
  3846. /**
  3847. * Searching a specified itemid in inventory and return his stored index
  3848. * @param sd Player
  3849. * @param nameid Find this Item!
  3850. * @return Stored index in inventory, or -1 if not found.
  3851. **/
  3852. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  3853. short i;
  3854. nullpo_retr(-1, sd);
  3855. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid && (sd->status.inventory[i].amount > 0 || nameid == 0) );
  3856. return ( i < MAX_INVENTORY ) ? i : -1;
  3857. }
  3858. /** Attempt to add a new item to player inventory
  3859. * @param sd
  3860. * @param item_data
  3861. * @param amount
  3862. * @param log_type
  3863. * @return
  3864. * 0 = success
  3865. * 1 = invalid itemid not found or negative amount
  3866. * 2 = overweight
  3867. * 3 = ?
  3868. * 4 = no free place found
  3869. * 5 = max amount reached
  3870. * 6 = ?
  3871. * 7 = stack limitation
  3872. */
  3873. char pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  3874. struct item_data *id;
  3875. int16 i;
  3876. unsigned int w;
  3877. nullpo_retr(1, sd);
  3878. nullpo_retr(1, item);
  3879. if( item->nameid == 0 || amount <= 0 )
  3880. return ADDITEM_INVALID;
  3881. if( amount > MAX_AMOUNT )
  3882. return ADDITEM_OVERAMOUNT;
  3883. id = itemdb_search(item->nameid);
  3884. if( id->stack.inventory && amount > id->stack.amount )
  3885. {// item stack limitation
  3886. return ADDITEM_STACKLIMIT;
  3887. }
  3888. w = id->weight*amount;
  3889. if(sd->weight + w > sd->max_weight)
  3890. return ADDITEM_OVERWEIGHT;
  3891. i = MAX_INVENTORY;
  3892. #ifdef ENABLE_ITEM_GUID
  3893. if (id->flag.guid && !item->unique_id)
  3894. item->unique_id = pc_generate_unique_id(sd);
  3895. #endif
  3896. // Stackable | Non Rental
  3897. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  3898. for( i = 0; i < MAX_INVENTORY; i++ ) {
  3899. if( sd->status.inventory[i].nameid == item->nameid &&
  3900. sd->status.inventory[i].bound == item->bound &&
  3901. sd->status.inventory[i].expire_time == 0 &&
  3902. #ifdef ENABLE_ITEM_GUID
  3903. sd->status.inventory[i].unique_id == item->unique_id &&
  3904. #endif
  3905. memcmp(&sd->status.inventory[i].card, &item->card, sizeof(item->card)) == 0
  3906. )
  3907. {
  3908. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( id->stack.inventory && amount > id->stack.amount - sd->status.inventory[i].amount ) )
  3909. return ADDITEM_OVERAMOUNT;
  3910. sd->status.inventory[i].amount += amount;
  3911. clif_additem(sd,i,amount,0);
  3912. break;
  3913. }
  3914. }
  3915. }
  3916. if( i >= MAX_INVENTORY )
  3917. {
  3918. i = pc_search_inventory(sd,0);
  3919. if( i < 0 )
  3920. return ADDITEM_OVERITEM;
  3921. memcpy(&sd->status.inventory[i], item, sizeof(sd->status.inventory[0]));
  3922. // clear equip and favorite fields first, just in case
  3923. if( item->equip )
  3924. sd->status.inventory[i].equip = 0;
  3925. if( item->favorite )
  3926. sd->status.inventory[i].favorite = 0;
  3927. sd->status.inventory[i].amount = amount;
  3928. sd->inventory_data[i] = id;
  3929. sd->last_addeditem_index = i;
  3930. clif_additem(sd,i,amount,0);
  3931. }
  3932. if( !itemdb_isstackable2(id) && !item->unique_id )
  3933. item->unique_id = pc_generate_unique_id(sd);
  3934. log_pick_pc(sd, log_type, amount, &sd->status.inventory[i]);
  3935. sd->weight += w;
  3936. clif_updatestatus(sd,SP_WEIGHT);
  3937. //Auto-equip
  3938. if(id->flag.autoequip)
  3939. pc_equipitem(sd, i, id->equip);
  3940. /* rental item check */
  3941. if( item->expire_time ) {
  3942. if( time(NULL) > item->expire_time ) {
  3943. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  3944. pc_delitem(sd, i, sd->status.inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  3945. } else {
  3946. int seconds = (int)( item->expire_time - time(NULL) );
  3947. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, seconds);
  3948. pc_inventory_rental_add(sd, seconds);
  3949. }
  3950. }
  3951. return ADDITEM_SUCCESS;
  3952. }
  3953. /*==========================================
  3954. * Remove an item at index n from inventory by amount.
  3955. * @param sd
  3956. * @param n Item index in inventory
  3957. * @param amount
  3958. * @param type &1: Don't notify deletion; &2 Don't notify weight change
  3959. * @param reason Delete reason
  3960. * @param log_type e_log_pick_type
  3961. * @return 1 - invalid itemid or negative amount; 0 - Success
  3962. *------------------------------------------*/
  3963. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  3964. {
  3965. nullpo_retr(1, sd);
  3966. if(n < 0 || sd->status.inventory[n].nameid==0 || amount <= 0 || sd->status.inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  3967. return 1;
  3968. log_pick_pc(sd, log_type, -amount, &sd->status.inventory[n]);
  3969. sd->status.inventory[n].amount -= amount;
  3970. sd->weight -= sd->inventory_data[n]->weight*amount ;
  3971. if( sd->status.inventory[n].amount <= 0 ){
  3972. if(sd->status.inventory[n].equip)
  3973. pc_unequipitem(sd,n,3);
  3974. memset(&sd->status.inventory[n],0,sizeof(sd->status.inventory[0]));
  3975. sd->inventory_data[n] = NULL;
  3976. }
  3977. if(!(type&1))
  3978. clif_delitem(sd,n,amount,reason);
  3979. if(!(type&2))
  3980. clif_updatestatus(sd,SP_WEIGHT);
  3981. return 0;
  3982. }
  3983. /*==========================================
  3984. * Attempt to drop an item.
  3985. * @param sd
  3986. * @param n Item index in inventory
  3987. * @param amount Amount of item
  3988. * @return False = fail; True = success
  3989. *------------------------------------------*/
  3990. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  3991. {
  3992. nullpo_retr(1, sd);
  3993. if(n < 0 || n >= MAX_INVENTORY)
  3994. return false;
  3995. if(amount <= 0)
  3996. return false;
  3997. if(sd->status.inventory[n].nameid <= 0 ||
  3998. sd->status.inventory[n].amount <= 0 ||
  3999. sd->status.inventory[n].amount < amount ||
  4000. sd->state.trading || sd->state.vending ||
  4001. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4002. )
  4003. return false;
  4004. if( map[sd->bl.m].flag.nodrop )
  4005. {
  4006. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4007. return false; //Can't drop items in nodrop mapflag maps.
  4008. }
  4009. if( !pc_candrop(sd,&sd->status.inventory[n]) )
  4010. {
  4011. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4012. return false;
  4013. }
  4014. if (!map_addflooritem(&sd->status.inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2))
  4015. return false;
  4016. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4017. clif_dropitem(sd, n, amount);
  4018. return true;
  4019. }
  4020. /*==========================================
  4021. * Attempt to pick up an item.
  4022. * @param sd
  4023. * @param fitem Item that will be picked
  4024. * @return False = fail; True = success
  4025. *------------------------------------------*/
  4026. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4027. {
  4028. int flag = 0;
  4029. unsigned int tick = gettick();
  4030. struct party_data *p = NULL;
  4031. nullpo_ret(sd);
  4032. nullpo_ret(fitem);
  4033. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4034. return false; // Distance is too far
  4035. if (sd->sc.cant.pickup)
  4036. return false;
  4037. if (sd->status.party_id)
  4038. p = party_search(sd->status.party_id);
  4039. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4040. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4041. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4042. if (!(p && p->party.item&1 &&
  4043. first_sd && first_sd->status.party_id == sd->status.party_id
  4044. ))
  4045. return false;
  4046. }
  4047. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4048. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4049. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4050. if (!(p && p->party.item&1 &&
  4051. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4052. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4053. ))
  4054. return false;
  4055. }
  4056. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4057. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4058. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4059. if(!(p && p->party.item&1 &&
  4060. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4061. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4062. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4063. ))
  4064. return false;
  4065. }
  4066. }
  4067. }
  4068. }
  4069. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4070. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4071. clif_additem(sd,0,0,flag);
  4072. return true;
  4073. }
  4074. //Display pickup animation.
  4075. pc_stop_attack(sd);
  4076. clif_takeitem(&sd->bl,&fitem->bl);
  4077. map_clearflooritem(&fitem->bl);
  4078. return true;
  4079. }
  4080. /*==========================================
  4081. * Check if item is usable.
  4082. * Return:
  4083. * 0 = no
  4084. * 1 = yes
  4085. *------------------------------------------*/
  4086. bool pc_isUseitem(struct map_session_data *sd,int n)
  4087. {
  4088. struct item_data *item;
  4089. unsigned short nameid;
  4090. nullpo_ret(sd);
  4091. item = sd->inventory_data[n];
  4092. nameid = sd->status.inventory[n].nameid;
  4093. if( item == NULL )
  4094. return false;
  4095. //Not consumable item
  4096. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4097. return false;
  4098. if( !item->script ) //if it has no script, you can't really consume it!
  4099. return false;
  4100. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4101. return true;
  4102. if(map[sd->bl.m].flag.noitemconsumption) //consumable but mapflag prevent it
  4103. return false;
  4104. //Prevent mass item usage. [Skotlex]
  4105. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4106. (itemdb_iscashfood(nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4107. )
  4108. return false;
  4109. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4110. clif_msgtable(sd->fd,ITEM_NOUSE_SITTING);
  4111. return false; // You cannot use this item while sitting.
  4112. }
  4113. if (sd->state.storage_flag && item->type != IT_CASH) {
  4114. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd,388));
  4115. return false; // You cannot use this item while storage is open.
  4116. }
  4117. if (item->flag.dead_branch && (map[sd->bl.m].flag.nobranch || map_flag_gvg(sd->bl.m)))
  4118. return false;
  4119. switch( nameid ) {
  4120. case ITEMID_ANODYNE:
  4121. if( map_flag_gvg(sd->bl.m) )
  4122. return false;
  4123. case ITEMID_ALOEBERA:
  4124. if( pc_issit(sd) )
  4125. return false;
  4126. break;
  4127. case ITEMID_WING_OF_FLY:
  4128. case ITEMID_GIANT_FLY_WING:
  4129. if( map[sd->bl.m].flag.noteleport || map_flag_gvg(sd->bl.m) ) {
  4130. clif_skill_teleportmessage(sd,0);
  4131. return false;
  4132. }
  4133. case ITEMID_WING_OF_BUTTERFLY:
  4134. case ITEMID_DUN_TELE_SCROLL1:
  4135. case ITEMID_DUN_TELE_SCROLL2:
  4136. case ITEMID_DUN_TELE_SCROLL3:
  4137. case ITEMID_WOB_RUNE:
  4138. case ITEMID_WOB_SCHWALTZ:
  4139. case ITEMID_WOB_RACHEL:
  4140. case ITEMID_WOB_LOCAL:
  4141. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4142. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4143. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4144. return false;
  4145. }
  4146. if( nameid != 601 && nameid != 12212 && map[sd->bl.m].flag.noreturn )
  4147. return false;
  4148. break;
  4149. case ITEMID_BUBBLE_GUM:
  4150. case ITEMID_COMP_BUBBLE_GUM:
  4151. if( sd->sc.data[SC_ITEMBOOST] )
  4152. return false;
  4153. break;
  4154. case ITEMID_BATTLE_MANUAL:
  4155. case ITEMID_COMP_BATTLE_MANUAL:
  4156. case ITEMID_THICK_BATTLE_MANUAL:
  4157. case ITEMID_NOBLE_NAMEPLATE:
  4158. case ITEMID_BATTLE_MANUAL25:
  4159. case ITEMID_BATTLE_MANUAL100:
  4160. case ITEMID_BATTLE_MANUAL300:
  4161. if( sd->sc.data[SC_EXPBOOST] )
  4162. return false;
  4163. break;
  4164. case ITEMID_JOB_MANUAL50:
  4165. if( sd->sc.data[SC_JEXPBOOST] )
  4166. return false;
  4167. break;
  4168. case ITEMID_MERCENARY_RED_POTION:
  4169. case ITEMID_MERCENARY_BLUE_POTION:
  4170. case ITEMID_M_CENTER_POTION:
  4171. case ITEMID_M_AWAKENING_POTION:
  4172. case ITEMID_M_BERSERK_POTION:
  4173. if( sd->md == NULL || sd->md->db == NULL )
  4174. return false;
  4175. if( sd->md->sc.data[SC_BERSERK] )
  4176. return false;
  4177. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4178. return false;
  4179. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4180. return false;
  4181. break;
  4182. case ITEMID_NEURALIZER:
  4183. if( !map[sd->bl.m].flag.reset )
  4184. return false;
  4185. break;
  4186. }
  4187. if( nameid >= ITEMID_BOW_MERCENARY_SCROLL1 && nameid <= ITEMID_SPEARMERCENARY_SCROLL10 && sd->md != NULL )
  4188. return false; // Mercenary Scrolls
  4189. /**
  4190. * Only Rune Knights may use runes
  4191. **/
  4192. if( itemdb_is_rune(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  4193. return false;
  4194. /**
  4195. * Only GCross may use poisons
  4196. **/
  4197. else if( itemdb_is_poison(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_GUILLOTINE_CROSS )
  4198. return false;
  4199. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4200. if( pc_is90overweight(sd) ) {
  4201. clif_msgtable(sd->fd, ITEM_CANT_OBTAIN_WEIGHT);
  4202. return false;
  4203. }
  4204. if( !pc_inventoryblank(sd) ) {
  4205. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 732)); //Item cannot be open when inventory is full
  4206. return false;
  4207. }
  4208. }
  4209. //Gender check
  4210. if(item->sex != 2 && sd->status.sex != item->sex)
  4211. return false;
  4212. //Required level check
  4213. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4214. return false;
  4215. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4216. return false;
  4217. //Not equipable by class. [Skotlex]
  4218. if (!(
  4219. (1<<(sd->class_&MAPID_BASEMASK)) &
  4220. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4221. ))
  4222. return false;
  4223. if (sd->sc.count && (
  4224. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4225. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4226. sd->sc.data[SC_TRICKDEAD] ||
  4227. sd->sc.data[SC_HIDING] ||
  4228. sd->sc.data[SC__SHADOWFORM] ||
  4229. sd->sc.data[SC__INVISIBILITY] ||
  4230. sd->sc.data[SC__MANHOLE] ||
  4231. sd->sc.data[SC_KAGEHUMI] ||
  4232. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4233. sd->sc.data[SC_HEAT_BARREL_AFTER]))
  4234. return false;
  4235. if (!pc_isItemClass(sd,item))
  4236. return false;
  4237. //Dead Branch items
  4238. if( item->flag.dead_branch )
  4239. log_branch(sd);
  4240. return true;
  4241. }
  4242. /*==========================================
  4243. * Last checks to use an item.
  4244. * Return:
  4245. * 0 = fail
  4246. * 1 = success
  4247. *------------------------------------------*/
  4248. int pc_useitem(struct map_session_data *sd,int n)
  4249. {
  4250. unsigned int tick = gettick();
  4251. int amount;
  4252. unsigned short nameid;
  4253. struct script_code *script;
  4254. struct item item;
  4255. struct item_data *id;
  4256. nullpo_ret(sd);
  4257. if (sd->npc_id) {
  4258. #ifdef RENEWAL
  4259. clif_msg(sd, USAGE_FAIL); // TODO look for the client date that has this message.
  4260. return 0;
  4261. #else
  4262. if( !sd->npc_item_flag )
  4263. return 0;
  4264. #endif
  4265. }
  4266. item = sd->status.inventory[n];
  4267. id = sd->inventory_data[n];
  4268. if (item.nameid == 0 || item.amount <= 0)
  4269. return 0;
  4270. if( !pc_isUseitem(sd,n) )
  4271. return 0;
  4272. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4273. nameid = id->nameid;
  4274. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4275. return 0;
  4276. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4277. if( id->flag.delay_consume ) {
  4278. if( nameid != ITEMID_REINS_OF_MOUNT && &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  4279. return 0;
  4280. else if( pc_issit(sd) )
  4281. return 0;
  4282. }
  4283. //Since most delay-consume items involve using a "skill-type" target cursor,
  4284. //perform a skill-use check before going through. [Skotlex]
  4285. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4286. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  4287. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4288. return 0;
  4289. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) ) {
  4290. int i;
  4291. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == nameid );
  4292. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  4293. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  4294. if( i < MAX_ITEMDELAYS ) {
  4295. if( sd->item_delay[i].nameid ) {// found
  4296. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  4297. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  4298. char e_msg[CHAT_SIZE_MAX];
  4299. if( e_tick > 99 )
  4300. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  4301. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  4302. else
  4303. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  4304. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  4305. clif_colormes(sd->fd,color_table[COLOR_YELLOW],e_msg);
  4306. return 0; // Delay has not expired yet
  4307. }
  4308. } else {// not yet used item (all slots are initially empty)
  4309. sd->item_delay[i].nameid = nameid;
  4310. }
  4311. if( !(nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  4312. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  4313. } else {// should not happen
  4314. ShowError("pc_useitem: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", nameid, sd->status.char_id);
  4315. }
  4316. //clean up used delays so we can give room for more
  4317. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  4318. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  4319. sd->item_delay[i].tick = 0;
  4320. sd->item_delay[i].nameid = 0;
  4321. }
  4322. }
  4323. }
  4324. /* on restricted maps the item is consumed but the effect is not used */
  4325. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4326. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4327. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4328. clif_useitemack(sd,n,item.amount-1,true);
  4329. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4330. }
  4331. return 0;/* regardless, effect is not run */
  4332. }
  4333. sd->itemid = item.nameid;
  4334. sd->itemindex = n;
  4335. if(sd->catch_target_class != -1) //Abort pet catching.
  4336. sd->catch_target_class = -1;
  4337. amount = item.amount;
  4338. script = id->script;
  4339. //Check if the item is to be consumed immediately [Skotlex]
  4340. if (id->flag.delay_consume)
  4341. clif_useitemack(sd, n, amount, true);
  4342. else
  4343. {
  4344. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4345. {
  4346. clif_useitemack(sd, n, amount - 1, true);
  4347. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4348. }
  4349. else
  4350. clif_useitemack(sd, n, 0, false);
  4351. }
  4352. if(item.card[0]==CARD0_CREATE &&
  4353. pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4354. {
  4355. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4356. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  4357. potion_flag = 3; //Even more effective potions.
  4358. }
  4359. //Update item use time.
  4360. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4361. if( itemdb_iscashfood(nameid) )
  4362. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4363. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4364. potion_flag = 0;
  4365. return 1;
  4366. }
  4367. /**
  4368. * Add item on cart for given index.
  4369. * @param sd
  4370. * @param item
  4371. * @param amount
  4372. * @param log_type
  4373. * @return 0 = success; 1 = fail; 2 = no slot
  4374. */
  4375. unsigned char pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4376. {
  4377. struct item_data *data;
  4378. int i,w;
  4379. nullpo_retr(1, sd);
  4380. nullpo_retr(1, item);
  4381. if(item->nameid == 0 || amount <= 0)
  4382. return 1;
  4383. data = itemdb_search(item->nameid);
  4384. if( data->stack.cart && amount > data->stack.amount )
  4385. {// item stack limitation
  4386. return 1;
  4387. }
  4388. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4389. { // Check item trade restrictions [Skotlex]
  4390. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4391. return 1;
  4392. }
  4393. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4394. return 1;
  4395. // ID no longer points to inventory/kafra ID. While we get a new one we don't want to mess up vending creation.
  4396. item->id = 0;
  4397. i = MAX_CART;
  4398. if( itemdb_isstackable2(data) && !item->expire_time )
  4399. {
  4400. for (i = 0; i < MAX_CART; i++) {
  4401. if (sd->status.cart[i].nameid == item->nameid
  4402. && sd->status.cart[i].bound == item->bound
  4403. #ifdef ENABLE_ITEM_GUID
  4404. && sd->status.cart[i].unique_id == item->unique_id
  4405. #endif
  4406. && memcmp(sd->status.cart[i].card, item->card, sizeof(item->card)) == 0
  4407. )
  4408. break;
  4409. }
  4410. }
  4411. if( i < MAX_CART )
  4412. {// item already in cart, stack it
  4413. if( amount > MAX_AMOUNT - sd->status.cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->status.cart[i].amount ) )
  4414. return 2; // no slot
  4415. sd->status.cart[i].amount+=amount;
  4416. clif_cart_additem(sd,i,amount,0);
  4417. }
  4418. else
  4419. {// item not stackable or not present, add it
  4420. ARR_FIND( 0, MAX_CART, i, sd->status.cart[i].nameid == 0 );
  4421. if( i == MAX_CART )
  4422. return 2; // no slot
  4423. memcpy(&sd->status.cart[i],item,sizeof(sd->status.cart[0]));
  4424. sd->status.cart[i].amount=amount;
  4425. sd->cart_num++;
  4426. clif_cart_additem(sd,i,amount,0);
  4427. }
  4428. sd->status.cart[i].favorite = 0;/* clear */
  4429. log_pick_pc(sd, log_type, amount, &sd->status.cart[i]);
  4430. sd->cart_weight += w;
  4431. clif_updatestatus(sd,SP_CARTINFO);
  4432. return 0;
  4433. }
  4434. /*==========================================
  4435. * Delete item on cart for given index.
  4436. *------------------------------------------*/
  4437. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4438. {
  4439. nullpo_retv(sd);
  4440. if(sd->status.cart[n].nameid == 0 ||
  4441. sd->status.cart[n].amount<amount)
  4442. return;
  4443. log_pick_pc(sd, log_type, -amount, &sd->status.cart[n]);
  4444. sd->status.cart[n].amount -= amount;
  4445. sd->cart_weight -= itemdb_weight(sd->status.cart[n].nameid)*amount ;
  4446. if(sd->status.cart[n].amount <= 0){
  4447. memset(&sd->status.cart[n],0,sizeof(sd->status.cart[0]));
  4448. sd->cart_num--;
  4449. }
  4450. if(!type) {
  4451. clif_cart_delitem(sd,n,amount);
  4452. clif_updatestatus(sd,SP_CARTINFO);
  4453. }
  4454. }
  4455. /*==========================================
  4456. * Transfer item from inventory to cart.
  4457. *------------------------------------------*/
  4458. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4459. {
  4460. struct item *item_data;
  4461. char flag;
  4462. nullpo_retv(sd);
  4463. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4464. return;
  4465. item_data = &sd->status.inventory[idx];
  4466. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4467. return;
  4468. if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
  4469. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4470. else {
  4471. clif_dropitem(sd,idx,0);
  4472. clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  4473. }
  4474. }
  4475. /*==========================================
  4476. * Get number of item in cart.
  4477. * Return:
  4478. -1 = itemid not found or no amount found
  4479. x = remaining itemid on cart after get
  4480. *------------------------------------------*/
  4481. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4482. {
  4483. struct item* item_data;
  4484. nullpo_retr(-1, sd);
  4485. item_data = &sd->status.cart[idx];
  4486. if( item_data->nameid == 0 || item_data->amount == 0 )
  4487. return -1;
  4488. return item_data->amount - amount;
  4489. }
  4490. /*==========================================
  4491. * Retrieve an item at index idx from cart.
  4492. *------------------------------------------*/
  4493. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4494. {
  4495. struct item *item_data;
  4496. unsigned char flag = 0;
  4497. nullpo_retv(sd);
  4498. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4499. return;
  4500. item_data=&sd->status.cart[idx];
  4501. if(item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4502. return;
  4503. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4504. pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4505. else {
  4506. clif_dropitem(sd,idx,0);
  4507. clif_additem(sd,0,0,flag);
  4508. }
  4509. }
  4510. /*==========================================
  4511. * Bound Item Check
  4512. * Type:
  4513. * 1 Account Bound
  4514. * 2 Guild Bound
  4515. * 3 Party Bound
  4516. * 4 Character Bound
  4517. *------------------------------------------*/
  4518. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4519. {
  4520. int i=0, j=0;
  4521. for(i=0;i<MAX_INVENTORY;i++){
  4522. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0 && sd->status.inventory[i].bound == type) {
  4523. idxlist[j] = i;
  4524. j++;
  4525. }
  4526. }
  4527. return j;
  4528. }
  4529. /*==========================================
  4530. * Display item stolen msg to player sd
  4531. *------------------------------------------*/
  4532. int pc_show_steal(struct block_list *bl,va_list ap)
  4533. {
  4534. struct map_session_data *sd;
  4535. int itemid;
  4536. struct item_data *item=NULL;
  4537. char output[100];
  4538. sd=va_arg(ap,struct map_session_data *);
  4539. itemid=va_arg(ap,int);
  4540. if((item=itemdb_exists(itemid))==NULL)
  4541. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4542. else
  4543. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4544. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4545. return 0;
  4546. }
  4547. /*==========================================
  4548. * Steal an item from bl (mob).
  4549. * Return:
  4550. * 0 = fail
  4551. * 1 = succes
  4552. *------------------------------------------*/
  4553. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4554. {
  4555. int i,itemid;
  4556. double rate;
  4557. unsigned char flag = 0;
  4558. struct status_data *sd_status, *md_status;
  4559. struct mob_data *md;
  4560. struct item tmp_item;
  4561. if(!sd || !bl || bl->type!=BL_MOB)
  4562. return 0;
  4563. md = (TBL_MOB *)bl;
  4564. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING && md->sc.opt1 != OPT1_CRYSTALIZE ) ) //already stolen from / status change check
  4565. return 0;
  4566. sd_status= status_get_status_data(&sd->bl);
  4567. md_status= status_get_status_data(bl);
  4568. if( md->master_id || md_status->mode&MD_BOSS || mob_is_treasure(md) ||
  4569. map[bl->m].flag.nomobloot || // check noloot map flag [Lorky]
  4570. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4571. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4572. ) { //Can't steal from
  4573. md->state.steal_flag = UCHAR_MAX;
  4574. return 0;
  4575. }
  4576. // base skill success chance (percentual)
  4577. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4578. rate += sd->bonus.add_steal_rate;
  4579. if( rate < 1 )
  4580. return 0;
  4581. // Try dropping one item, in the order from first to last possible slot.
  4582. // Droprate is affected by the skill success rate.
  4583. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4584. if( md->db->dropitem[i].nameid > 0 && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p * rate/100. )
  4585. break;
  4586. if( i == MAX_STEAL_DROP )
  4587. return 0;
  4588. itemid = md->db->dropitem[i].nameid;
  4589. memset(&tmp_item,0,sizeof(tmp_item));
  4590. tmp_item.nameid = itemid;
  4591. tmp_item.amount = 1;
  4592. tmp_item.identify = itemdb_isidentified(itemid);
  4593. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4594. //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?
  4595. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4596. if(flag) { //Failed to steal due to overweight
  4597. clif_additem(sd,0,0,flag);
  4598. return 0;
  4599. }
  4600. if(battle_config.show_steal_in_same_party)
  4601. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4602. //Logs items, Stolen from mobs [Lupus]
  4603. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4604. //A Rare Steal Global Announce by Lupus
  4605. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4606. struct item_data *i_data;
  4607. char message[128];
  4608. i_data = itemdb_search(itemid);
  4609. 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);
  4610. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4611. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4612. }
  4613. return 1;
  4614. }
  4615. /*==========================================
  4616. * Stole zeny from bl (mob)
  4617. * return
  4618. * 0 = fail
  4619. * 1 = success
  4620. *------------------------------------------*/
  4621. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4622. {
  4623. int rate,skill;
  4624. struct mob_data *md;
  4625. if(!sd || !target || target->type != BL_MOB)
  4626. return 0;
  4627. md = (TBL_MOB*)target;
  4628. if( md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || md->status.mode&MD_BOSS )
  4629. return 0;
  4630. if( mob_is_treasure(md) )
  4631. return 0;
  4632. // FIXME: This formula is either custom or outdated.
  4633. skill = pc_checkskill(sd,RG_STEALCOIN)*10;
  4634. rate = skill + (sd->status.base_level - md->level)*3 + sd->battle_status.dex*2 + sd->battle_status.luk*2;
  4635. if(rnd()%1000 < rate)
  4636. {
  4637. int amount = md->level*10 + rnd()%100;
  4638. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4639. md->state.steal_coin_flag = 1;
  4640. return 1;
  4641. }
  4642. return 0;
  4643. }
  4644. /*==========================================
  4645. * Set's a player position.
  4646. * @param sd
  4647. * @param mapindex
  4648. * @param x
  4649. * @param y
  4650. * @param clrtype
  4651. * @return 0 - Success; 1 - Invalid map index; 2 - Map not in this map-server, and failed to locate alternate map-server.
  4652. *------------------------------------------*/
  4653. char pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4654. {
  4655. int16 m;
  4656. nullpo_ret(sd);
  4657. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4658. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4659. return 1;
  4660. }
  4661. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  4662. return 1;
  4663. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  4664. pc_setstand(sd, true);
  4665. pc_setrestartvalue(sd,1);
  4666. }
  4667. m = map_mapindex2mapid(mapindex);
  4668. sd->state.changemap = (sd->mapindex != mapindex);
  4669. sd->state.warping = 1;
  4670. if(sd->status.party_id && map[sd->bl.m].instance_id && sd->state.changemap && !map[m].instance_id) {
  4671. struct party_data *p;
  4672. if((p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  4673. instance_delusers(p->instance_id);
  4674. }
  4675. if( sd->state.changemap ) { // Misc map-changing settings
  4676. int i;
  4677. sd->state.pmap = sd->bl.m;
  4678. if (sd->sc.count) { // Cancel some map related stuff.
  4679. if (sd->sc.data[SC_JAILED])
  4680. return 1; //You may not get out!
  4681. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4682. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4683. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4684. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4685. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4686. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4687. if (sd->sc.data[SC_KNOWLEDGE]) {
  4688. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4689. if (sce->timer != INVALID_TIMER)
  4690. delete_timer(sce->timer, status_change_timer);
  4691. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4692. }
  4693. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4694. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4695. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4696. }
  4697. for( i = 0; i < EQI_MAX; i++ ) {
  4698. if( sd->equip_index[i] >= 0 )
  4699. if( pc_isequip(sd,sd->equip_index[i]) )
  4700. pc_unequipitem(sd,sd->equip_index[i],2);
  4701. }
  4702. if (battle_config.clear_unit_onwarp&BL_PC)
  4703. skill_clear_unitgroup(&sd->bl);
  4704. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4705. guild_send_dot_remove(sd);
  4706. bg_send_dot_remove(sd);
  4707. if (sd->regen.state.gc)
  4708. sd->regen.state.gc = 0;
  4709. // make sure vending is allowed here
  4710. if (sd->state.vending && map[m].flag.novending) {
  4711. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4712. vending_closevending(sd);
  4713. }
  4714. channel_pcquit(sd,4); //quit map chan
  4715. }
  4716. if( m < 0 )
  4717. {
  4718. uint32 ip;
  4719. uint16 port;
  4720. //if can't find any map-servers, just abort setting position.
  4721. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4722. return 2;
  4723. if (sd->npc_id)
  4724. npc_event_dequeue(sd);
  4725. npc_script_event(sd, NPCE_LOGOUT);
  4726. //remove from map, THEN change x/y coordinates
  4727. unit_remove_map_pc(sd,clrtype);
  4728. sd->mapindex = mapindex;
  4729. sd->bl.x=x;
  4730. sd->bl.y=y;
  4731. pc_clean_skilltree(sd);
  4732. chrif_save(sd,2);
  4733. chrif_changemapserver(sd, ip, (short)port);
  4734. //Free session data from this map server [Kevin]
  4735. unit_free_pc(sd);
  4736. return 0;
  4737. }
  4738. if( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  4739. {
  4740. 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);
  4741. x = y = 0; // make it random
  4742. }
  4743. if( x == 0 && y == 0 ) { // pick a random walkable cell
  4744. int c=0;
  4745. do {
  4746. x = rnd()%(map[m].xs-2)+1;
  4747. y = rnd()%(map[m].ys-2)+1;
  4748. c++;
  4749. if(c > (map[m].xs * map[m].ys)*3){ //force out
  4750. 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));
  4751. return 0; //preventing warp
  4752. //break; //allow warp anyway
  4753. }
  4754. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  4755. }
  4756. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  4757. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  4758. vending_closevending(sd);
  4759. }
  4760. if(sd->bl.prev != NULL){
  4761. unit_remove_map_pc(sd,clrtype);
  4762. clif_changemap(sd,m,x,y); // [MouseJstr]
  4763. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  4764. sd->state.rewarp = 1;
  4765. sd->mapindex = mapindex;
  4766. sd->bl.m = m;
  4767. sd->bl.x = sd->ud.to_x = x;
  4768. sd->bl.y = sd->ud.to_y = y;
  4769. if( sd->status.guild_id > 0 && map[m].flag.gvg_castle )
  4770. { // Increased guild castle regen [Valaris]
  4771. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  4772. if(gc && gc->guild_id == sd->status.guild_id)
  4773. sd->regen.state.gc = 1;
  4774. }
  4775. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  4776. {
  4777. sd->pd->bl.m = m;
  4778. sd->pd->bl.x = sd->pd->ud.to_x = x;
  4779. sd->pd->bl.y = sd->pd->ud.to_y = y;
  4780. sd->pd->ud.dir = sd->ud.dir;
  4781. }
  4782. if( hom_is_active(sd->hd) )
  4783. {
  4784. sd->hd->bl.m = m;
  4785. sd->hd->bl.x = sd->hd->ud.to_x = x;
  4786. sd->hd->bl.y = sd->hd->ud.to_y = y;
  4787. sd->hd->ud.dir = sd->ud.dir;
  4788. }
  4789. if( sd->md )
  4790. {
  4791. sd->md->bl.m = m;
  4792. sd->md->bl.x = sd->md->ud.to_x = x;
  4793. sd->md->bl.y = sd->md->ud.to_y = y;
  4794. sd->md->ud.dir = sd->ud.dir;
  4795. }
  4796. pc_cell_basilica(sd);
  4797. //check if we gonna be rewarped [lighta]
  4798. if(npc_check_areanpc(1,m,x,y,0)){
  4799. sd->count_rewarp++;
  4800. }
  4801. else
  4802. sd->count_rewarp = 0;
  4803. return 0;
  4804. }
  4805. /*==========================================
  4806. * Warp player sd to random location on current map.
  4807. * May fail if no walkable cell found (1000 attempts).
  4808. * Return:
  4809. * 0 = Success
  4810. * 1,2,3 = Fail
  4811. *------------------------------------------*/
  4812. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  4813. {
  4814. int x,y,i=0;
  4815. int16 m;
  4816. nullpo_ret(sd);
  4817. m=sd->bl.m;
  4818. if (map[sd->bl.m].flag.noteleport) //Teleport forbidden
  4819. return 3;
  4820. do {
  4821. x = rnd()%(map[m].xs-2)+1;
  4822. y = rnd()%(map[m].ys-2)+1;
  4823. } while((map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1))) && (i++) < 1000);
  4824. if (i < 1000)
  4825. return pc_setpos(sd,map[sd->bl.m].index,x,y,type);
  4826. return 3;
  4827. }
  4828. /*==========================================
  4829. * Records a memo point at sd's current position
  4830. * pos - entry to replace, (-1: shift oldest entry out)
  4831. *------------------------------------------*/
  4832. bool pc_memo(struct map_session_data* sd, int pos)
  4833. {
  4834. int skill;
  4835. nullpo_ret(sd);
  4836. // check mapflags
  4837. 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) ) {
  4838. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  4839. return false;
  4840. }
  4841. // check inputs
  4842. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  4843. return false; // invalid input
  4844. // check required skill level
  4845. skill = pc_checkskill(sd, AL_WARP);
  4846. if( skill < 1 ) {
  4847. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  4848. return false;
  4849. }
  4850. if( skill < 2 || skill - 2 < pos ) {
  4851. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  4852. return false;
  4853. }
  4854. if( pos == -1 )
  4855. {
  4856. uint8 i;
  4857. // prevent memo-ing the same map multiple times
  4858. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  4859. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  4860. pos = 0;
  4861. }
  4862. if( map[sd->bl.m].instance_id ) {
  4863. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  4864. return false;
  4865. }
  4866. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  4867. sd->status.memo_point[pos].x = sd->bl.x;
  4868. sd->status.memo_point[pos].y = sd->bl.y;
  4869. clif_skill_memomessage(sd, 0);
  4870. return true;
  4871. }
  4872. //
  4873. // Skills
  4874. //
  4875. /**
  4876. * Get the skill current cooldown for player.
  4877. * (get the db base cooldown for skill + player specific cooldown)
  4878. * @param sd : player pointer
  4879. * @param id : skill id
  4880. * @param lv : skill lv
  4881. * @return player skill cooldown
  4882. */
  4883. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  4884. uint8 i;
  4885. uint16 idx = skill_get_index(skill_id);
  4886. int cooldown = 0, cooldownlen = ARRAYLENGTH(sd->skillcooldown);
  4887. if (!idx) return 0;
  4888. if (skill_db[idx]->cooldown[skill_lv - 1])
  4889. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  4890. ARR_FIND(0, cooldownlen, i, sd->skillcooldown[i].id == skill_id);
  4891. if (i < cooldownlen) {
  4892. cooldown += sd->skillcooldown[i].val;
  4893. cooldown = max(0,cooldown);
  4894. }
  4895. return cooldown;
  4896. }
  4897. /*==========================================
  4898. * Return player sd skill_lv learned for given skill
  4899. *------------------------------------------*/
  4900. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  4901. {
  4902. uint16 idx = 0;
  4903. if (sd == NULL)
  4904. return 0;
  4905. if ((idx = skill_get_index(skill_id)) == 0) {
  4906. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  4907. return 0;
  4908. }
  4909. if (SKILL_CHK_GUILD(skill_id) ) {
  4910. struct guild *g;
  4911. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  4912. return guild_checkskill(g,skill_id);
  4913. return 0;
  4914. }
  4915. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  4916. }
  4917. /**
  4918. * Chk if we still have the correct weapon to continue the skill (actually status)
  4919. * If not ending it
  4920. * @param sd
  4921. * @return 0:error, 1:check done
  4922. */
  4923. static void pc_checkallowskill(struct map_session_data *sd)
  4924. {
  4925. const enum sc_type scw_list[] = {
  4926. SC_TWOHANDQUICKEN,
  4927. SC_ONEHAND,
  4928. SC_AURABLADE,
  4929. SC_PARRYING,
  4930. SC_SPEARQUICKEN,
  4931. SC_ADRENALINE,
  4932. SC_ADRENALINE2,
  4933. SC_DANCING,
  4934. SC_GATLINGFEVER,
  4935. SC_FEARBREEZE
  4936. };
  4937. uint8 i;
  4938. nullpo_retv(sd);
  4939. if(!sd->sc.count)
  4940. return;
  4941. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  4942. { // Skills requiring specific weapon types
  4943. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  4944. continue;
  4945. if(sd->sc.data[scw_list[i]] &&
  4946. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  4947. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  4948. }
  4949. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  4950. // Spurt requires bare hands (feet, in fact xD)
  4951. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  4952. if(sd->status.shield <= 0) { // Skills requiring a shield
  4953. const enum sc_type scs_list[] = {
  4954. SC_AUTOGUARD,
  4955. SC_DEFENDER,
  4956. SC_REFLECTSHIELD,
  4957. SC_REFLECTDAMAGE
  4958. };
  4959. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  4960. if(sd->sc.data[scs_list[i]])
  4961. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  4962. }
  4963. }
  4964. /*==========================================
  4965. * Return equiped itemid? on player sd at pos
  4966. * Return
  4967. * -1 : mean nothing equiped
  4968. * idx : (this index could be used in inventory to found item_data)
  4969. *------------------------------------------*/
  4970. short pc_checkequip(struct map_session_data *sd,int pos)
  4971. {
  4972. uint8 i;
  4973. nullpo_retr(-1, sd);
  4974. for(i=0;i<EQI_MAX;i++){
  4975. if(pos & equip_pos[i])
  4976. return sd->equip_index[i];
  4977. }
  4978. return -1;
  4979. }
  4980. /*==========================================
  4981. * Check if sd as nameid equiped somewhere
  4982. * @sd : the player session
  4983. * @nameid : id of the item to check
  4984. * @min : : see pc.h enum equip_index from ? to @max
  4985. * @max : see pc.h enum equip_index for @min to ?
  4986. * -return true,false
  4987. *------------------------------------------*/
  4988. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max){
  4989. int i;
  4990. for(i=min;i<max;i++){
  4991. if(equip_pos[i]){
  4992. int idx = sd->equip_index[i];
  4993. if (sd->status.inventory[idx].nameid == nameid)
  4994. return true;
  4995. }
  4996. }
  4997. return false;
  4998. }
  4999. /*==========================================
  5000. * Convert's from the client's lame Job ID system
  5001. * to the map server's 'makes sense' system. [Skotlex]
  5002. *------------------------------------------*/
  5003. int pc_jobid2mapid(unsigned short b_class)
  5004. {
  5005. switch(b_class)
  5006. {
  5007. //Novice And 1-1 Jobs
  5008. case JOB_NOVICE: return MAPID_NOVICE;
  5009. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5010. case JOB_MAGE: return MAPID_MAGE;
  5011. case JOB_ARCHER: return MAPID_ARCHER;
  5012. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5013. case JOB_MERCHANT: return MAPID_MERCHANT;
  5014. case JOB_THIEF: return MAPID_THIEF;
  5015. case JOB_TAEKWON: return MAPID_TAEKWON;
  5016. case JOB_WEDDING: return MAPID_WEDDING;
  5017. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5018. case JOB_NINJA: return MAPID_NINJA;
  5019. case JOB_XMAS: return MAPID_XMAS;
  5020. case JOB_SUMMER: return MAPID_SUMMER;
  5021. case JOB_HANBOK: return MAPID_HANBOK;
  5022. case JOB_GANGSI: return MAPID_GANGSI;
  5023. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5024. //2-1 Jobs
  5025. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5026. case JOB_KNIGHT: return MAPID_KNIGHT;
  5027. case JOB_WIZARD: return MAPID_WIZARD;
  5028. case JOB_HUNTER: return MAPID_HUNTER;
  5029. case JOB_PRIEST: return MAPID_PRIEST;
  5030. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5031. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5032. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5033. case JOB_KAGEROU:
  5034. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5035. case JOB_REBELLION: return MAPID_REBELLION;
  5036. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5037. //2-2 Jobs
  5038. case JOB_CRUSADER: return MAPID_CRUSADER;
  5039. case JOB_SAGE: return MAPID_SAGE;
  5040. case JOB_BARD:
  5041. case JOB_DANCER: return MAPID_BARDDANCER;
  5042. case JOB_MONK: return MAPID_MONK;
  5043. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5044. case JOB_ROGUE: return MAPID_ROGUE;
  5045. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5046. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5047. //Trans Novice And Trans 1-1 Jobs
  5048. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5049. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5050. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5051. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5052. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5053. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5054. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5055. //Trans 2-1 Jobs
  5056. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5057. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5058. case JOB_SNIPER: return MAPID_SNIPER;
  5059. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5060. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5061. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5062. //Trans 2-2 Jobs
  5063. case JOB_PALADIN: return MAPID_PALADIN;
  5064. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5065. case JOB_CLOWN:
  5066. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5067. case JOB_CHAMPION: return MAPID_CHAMPION;
  5068. case JOB_CREATOR: return MAPID_CREATOR;
  5069. case JOB_STALKER: return MAPID_STALKER;
  5070. //Baby Novice And Baby 1-1 Jobs
  5071. case JOB_BABY: return MAPID_BABY;
  5072. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5073. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5074. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5075. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5076. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5077. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5078. //Baby 2-1 Jobs
  5079. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5080. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5081. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5082. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5083. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5084. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5085. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5086. //Baby 2-2 Jobs
  5087. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5088. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5089. case JOB_BABY_BARD:
  5090. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5091. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5092. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5093. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5094. //3-1 Jobs
  5095. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5096. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5097. case JOB_WARLOCK: return MAPID_WARLOCK;
  5098. case JOB_RANGER: return MAPID_RANGER;
  5099. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5100. case JOB_MECHANIC: return MAPID_MECHANIC;
  5101. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5102. //3-2 Jobs
  5103. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5104. case JOB_SORCERER: return MAPID_SORCERER;
  5105. case JOB_MINSTREL:
  5106. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5107. case JOB_SURA: return MAPID_SURA;
  5108. case JOB_GENETIC: return MAPID_GENETIC;
  5109. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5110. //Trans 3-1 Jobs
  5111. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5112. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5113. case JOB_RANGER_T: return MAPID_RANGER_T;
  5114. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5115. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5116. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5117. //Trans 3-2 Jobs
  5118. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5119. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5120. case JOB_MINSTREL_T:
  5121. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5122. case JOB_SURA_T: return MAPID_SURA_T;
  5123. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5124. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5125. //Baby 3-1 Jobs
  5126. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5127. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5128. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5129. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5130. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5131. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5132. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5133. //Baby 3-2 Jobs
  5134. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5135. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5136. case JOB_BABY_MINSTREL:
  5137. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5138. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5139. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5140. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5141. default:
  5142. return -1;
  5143. }
  5144. }
  5145. //Reverts the map-style class id to the client-style one.
  5146. int pc_mapid2jobid(unsigned short class_, int sex)
  5147. {
  5148. switch(class_) {
  5149. //Novice And 1-1 Jobs
  5150. case MAPID_NOVICE: return JOB_NOVICE;
  5151. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5152. case MAPID_MAGE: return JOB_MAGE;
  5153. case MAPID_ARCHER: return JOB_ARCHER;
  5154. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5155. case MAPID_MERCHANT: return JOB_MERCHANT;
  5156. case MAPID_THIEF: return JOB_THIEF;
  5157. case MAPID_TAEKWON: return JOB_TAEKWON;
  5158. case MAPID_WEDDING: return JOB_WEDDING;
  5159. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5160. case MAPID_NINJA: return JOB_NINJA;
  5161. case MAPID_XMAS: return JOB_XMAS;
  5162. case MAPID_SUMMER: return JOB_SUMMER;
  5163. case MAPID_HANBOK: return JOB_HANBOK;
  5164. case MAPID_GANGSI: return JOB_GANGSI;
  5165. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5166. //2-1 Jobs
  5167. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5168. case MAPID_KNIGHT: return JOB_KNIGHT;
  5169. case MAPID_WIZARD: return JOB_WIZARD;
  5170. case MAPID_HUNTER: return JOB_HUNTER;
  5171. case MAPID_PRIEST: return JOB_PRIEST;
  5172. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5173. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5174. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5175. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5176. case MAPID_REBELLION: return JOB_REBELLION;
  5177. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5178. //2-2 Jobs
  5179. case MAPID_CRUSADER: return JOB_CRUSADER;
  5180. case MAPID_SAGE: return JOB_SAGE;
  5181. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5182. case MAPID_MONK: return JOB_MONK;
  5183. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5184. case MAPID_ROGUE: return JOB_ROGUE;
  5185. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5186. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5187. //Trans Novice And Trans 2-1 Jobs
  5188. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5189. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5190. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5191. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5192. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5193. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5194. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5195. //Trans 2-1 Jobs
  5196. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5197. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5198. case MAPID_SNIPER: return JOB_SNIPER;
  5199. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5200. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5201. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5202. //Trans 2-2 Jobs
  5203. case MAPID_PALADIN: return JOB_PALADIN;
  5204. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5205. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5206. case MAPID_CHAMPION: return JOB_CHAMPION;
  5207. case MAPID_CREATOR: return JOB_CREATOR;
  5208. case MAPID_STALKER: return JOB_STALKER;
  5209. //Baby Novice And Baby 1-1 Jobs
  5210. case MAPID_BABY: return JOB_BABY;
  5211. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5212. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5213. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5214. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5215. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5216. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5217. //Baby 2-1 Jobs
  5218. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5219. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5220. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5221. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5222. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5223. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5224. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5225. //Baby 2-2 Jobs
  5226. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5227. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5228. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5229. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5230. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5231. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5232. //3-1 Jobs
  5233. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5234. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5235. case MAPID_WARLOCK: return JOB_WARLOCK;
  5236. case MAPID_RANGER: return JOB_RANGER;
  5237. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5238. case MAPID_MECHANIC: return JOB_MECHANIC;
  5239. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5240. //3-2 Jobs
  5241. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5242. case MAPID_SORCERER: return JOB_SORCERER;
  5243. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5244. case MAPID_SURA: return JOB_SURA;
  5245. case MAPID_GENETIC: return JOB_GENETIC;
  5246. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5247. //Trans 3-1 Jobs
  5248. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5249. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5250. case MAPID_RANGER_T: return JOB_RANGER_T;
  5251. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5252. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5253. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5254. //Trans 3-2 Jobs
  5255. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5256. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5257. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5258. case MAPID_SURA_T: return JOB_SURA_T;
  5259. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5260. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5261. //Baby 3-1 Jobs
  5262. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5263. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5264. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5265. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5266. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5267. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5268. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5269. //Baby 3-2 Jobs
  5270. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5271. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5272. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5273. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5274. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5275. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5276. default:
  5277. return -1;
  5278. }
  5279. }
  5280. /*====================================================
  5281. * This function return the name of the job (by [Yor])
  5282. *----------------------------------------------------*/
  5283. const char* job_name(int class_)
  5284. {
  5285. switch (class_) {
  5286. case JOB_NOVICE:
  5287. case JOB_SWORDMAN:
  5288. case JOB_MAGE:
  5289. case JOB_ARCHER:
  5290. case JOB_ACOLYTE:
  5291. case JOB_MERCHANT:
  5292. case JOB_THIEF:
  5293. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5294. case JOB_KNIGHT:
  5295. case JOB_PRIEST:
  5296. case JOB_WIZARD:
  5297. case JOB_BLACKSMITH:
  5298. case JOB_HUNTER:
  5299. case JOB_ASSASSIN:
  5300. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5301. case JOB_KNIGHT2:
  5302. return msg_txt(NULL,557);
  5303. case JOB_CRUSADER:
  5304. case JOB_MONK:
  5305. case JOB_SAGE:
  5306. case JOB_ROGUE:
  5307. case JOB_ALCHEMIST:
  5308. case JOB_BARD:
  5309. case JOB_DANCER:
  5310. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5311. case JOB_CRUSADER2:
  5312. return msg_txt(NULL,563);
  5313. case JOB_WEDDING:
  5314. case JOB_SUPER_NOVICE:
  5315. case JOB_GUNSLINGER:
  5316. case JOB_NINJA:
  5317. case JOB_XMAS:
  5318. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5319. case JOB_SUMMER:
  5320. return msg_txt(NULL,621);
  5321. case JOB_HANBOK:
  5322. return msg_txt(NULL,694);
  5323. case JOB_OKTOBERFEST:
  5324. return msg_txt(NULL,696);
  5325. case JOB_NOVICE_HIGH:
  5326. case JOB_SWORDMAN_HIGH:
  5327. case JOB_MAGE_HIGH:
  5328. case JOB_ARCHER_HIGH:
  5329. case JOB_ACOLYTE_HIGH:
  5330. case JOB_MERCHANT_HIGH:
  5331. case JOB_THIEF_HIGH:
  5332. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5333. case JOB_LORD_KNIGHT:
  5334. case JOB_HIGH_PRIEST:
  5335. case JOB_HIGH_WIZARD:
  5336. case JOB_WHITESMITH:
  5337. case JOB_SNIPER:
  5338. case JOB_ASSASSIN_CROSS:
  5339. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5340. case JOB_LORD_KNIGHT2:
  5341. return msg_txt(NULL,582);
  5342. case JOB_PALADIN:
  5343. case JOB_CHAMPION:
  5344. case JOB_PROFESSOR:
  5345. case JOB_STALKER:
  5346. case JOB_CREATOR:
  5347. case JOB_CLOWN:
  5348. case JOB_GYPSY:
  5349. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5350. case JOB_PALADIN2:
  5351. return msg_txt(NULL,588);
  5352. case JOB_BABY:
  5353. case JOB_BABY_SWORDMAN:
  5354. case JOB_BABY_MAGE:
  5355. case JOB_BABY_ARCHER:
  5356. case JOB_BABY_ACOLYTE:
  5357. case JOB_BABY_MERCHANT:
  5358. case JOB_BABY_THIEF:
  5359. return msg_txt(NULL,595 - JOB_BABY + class_);
  5360. case JOB_BABY_KNIGHT:
  5361. case JOB_BABY_PRIEST:
  5362. case JOB_BABY_WIZARD:
  5363. case JOB_BABY_BLACKSMITH:
  5364. case JOB_BABY_HUNTER:
  5365. case JOB_BABY_ASSASSIN:
  5366. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5367. case JOB_BABY_KNIGHT2:
  5368. return msg_txt(NULL,602);
  5369. case JOB_BABY_CRUSADER:
  5370. case JOB_BABY_MONK:
  5371. case JOB_BABY_SAGE:
  5372. case JOB_BABY_ROGUE:
  5373. case JOB_BABY_ALCHEMIST:
  5374. case JOB_BABY_BARD:
  5375. case JOB_BABY_DANCER:
  5376. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5377. case JOB_BABY_CRUSADER2:
  5378. return msg_txt(NULL,608);
  5379. case JOB_SUPER_BABY:
  5380. return msg_txt(NULL,615);
  5381. case JOB_TAEKWON:
  5382. return msg_txt(NULL,616);
  5383. case JOB_STAR_GLADIATOR:
  5384. case JOB_STAR_GLADIATOR2:
  5385. return msg_txt(NULL,617);
  5386. case JOB_SOUL_LINKER:
  5387. return msg_txt(NULL,618);
  5388. case JOB_GANGSI:
  5389. case JOB_DEATH_KNIGHT:
  5390. case JOB_DARK_COLLECTOR:
  5391. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5392. case JOB_RUNE_KNIGHT:
  5393. case JOB_WARLOCK:
  5394. case JOB_RANGER:
  5395. case JOB_ARCH_BISHOP:
  5396. case JOB_MECHANIC:
  5397. case JOB_GUILLOTINE_CROSS:
  5398. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5399. case JOB_RUNE_KNIGHT_T:
  5400. case JOB_WARLOCK_T:
  5401. case JOB_RANGER_T:
  5402. case JOB_ARCH_BISHOP_T:
  5403. case JOB_MECHANIC_T:
  5404. case JOB_GUILLOTINE_CROSS_T:
  5405. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5406. case JOB_ROYAL_GUARD:
  5407. case JOB_SORCERER:
  5408. case JOB_MINSTREL:
  5409. case JOB_WANDERER:
  5410. case JOB_SURA:
  5411. case JOB_GENETIC:
  5412. case JOB_SHADOW_CHASER:
  5413. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5414. case JOB_ROYAL_GUARD_T:
  5415. case JOB_SORCERER_T:
  5416. case JOB_MINSTREL_T:
  5417. case JOB_WANDERER_T:
  5418. case JOB_SURA_T:
  5419. case JOB_GENETIC_T:
  5420. case JOB_SHADOW_CHASER_T:
  5421. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5422. case JOB_RUNE_KNIGHT2:
  5423. case JOB_RUNE_KNIGHT_T2:
  5424. return msg_txt(NULL,625);
  5425. case JOB_ROYAL_GUARD2:
  5426. case JOB_ROYAL_GUARD_T2:
  5427. return msg_txt(NULL,631);
  5428. case JOB_RANGER2:
  5429. case JOB_RANGER_T2:
  5430. return msg_txt(NULL,627);
  5431. case JOB_MECHANIC2:
  5432. case JOB_MECHANIC_T2:
  5433. return msg_txt(NULL,629);
  5434. case JOB_BABY_RUNE:
  5435. case JOB_BABY_WARLOCK:
  5436. case JOB_BABY_RANGER:
  5437. case JOB_BABY_BISHOP:
  5438. case JOB_BABY_MECHANIC:
  5439. case JOB_BABY_CROSS:
  5440. case JOB_BABY_GUARD:
  5441. case JOB_BABY_SORCERER:
  5442. case JOB_BABY_MINSTREL:
  5443. case JOB_BABY_WANDERER:
  5444. case JOB_BABY_SURA:
  5445. case JOB_BABY_GENETIC:
  5446. case JOB_BABY_CHASER:
  5447. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5448. case JOB_BABY_RUNE2:
  5449. return msg_txt(NULL,638);
  5450. case JOB_BABY_GUARD2:
  5451. return msg_txt(NULL,644);
  5452. case JOB_BABY_RANGER2:
  5453. return msg_txt(NULL,640);
  5454. case JOB_BABY_MECHANIC2:
  5455. return msg_txt(NULL,642);
  5456. case JOB_SUPER_NOVICE_E:
  5457. case JOB_SUPER_BABY_E:
  5458. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5459. case JOB_KAGEROU:
  5460. case JOB_OBORO:
  5461. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5462. case JOB_REBELLION:
  5463. return msg_txt(NULL,695);
  5464. default:
  5465. return msg_txt(NULL,655);
  5466. }
  5467. }
  5468. /*====================================================
  5469. * Timered function to make id follow a target.
  5470. * @id = bl.id (player only atm)
  5471. * target is define in sd->followtarget (bl.id)
  5472. * used by pc_follow
  5473. *----------------------------------------------------*/
  5474. int pc_follow_timer(int tid, unsigned int tick, int id, intptr_t data)
  5475. {
  5476. struct map_session_data *sd;
  5477. struct block_list *tbl;
  5478. sd = map_id2sd(id);
  5479. nullpo_ret(sd);
  5480. if (sd->followtimer != tid){
  5481. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5482. sd->followtimer = INVALID_TIMER;
  5483. return 0;
  5484. }
  5485. sd->followtimer = INVALID_TIMER;
  5486. tbl = map_id2bl(sd->followtarget);
  5487. if (tbl == NULL || pc_isdead(sd) || status_isdead(tbl))
  5488. {
  5489. pc_stop_following(sd);
  5490. return 0;
  5491. }
  5492. // either player or target is currently detached from map blocks (could be teleporting),
  5493. // but still connected to this map, so we'll just increment the timer and check back later
  5494. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5495. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5496. {
  5497. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5498. if (!check_distance_bl(&sd->bl, tbl, 5))
  5499. unit_walktobl(&sd->bl, tbl, 5, 0);
  5500. } else
  5501. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5502. }
  5503. sd->followtimer = add_timer(
  5504. tick + 1000, // increase time a bit to loosen up map's load
  5505. pc_follow_timer, sd->bl.id, 0);
  5506. return 0;
  5507. }
  5508. int pc_stop_following (struct map_session_data *sd)
  5509. {
  5510. nullpo_ret(sd);
  5511. if (sd->followtimer != INVALID_TIMER) {
  5512. delete_timer(sd->followtimer,pc_follow_timer);
  5513. sd->followtimer = INVALID_TIMER;
  5514. }
  5515. sd->followtarget = -1;
  5516. sd->ud.target_to = 0;
  5517. unit_stop_walking(&sd->bl, 1);
  5518. return 0;
  5519. }
  5520. int pc_follow(struct map_session_data *sd,int target_id)
  5521. {
  5522. struct block_list *bl = map_id2bl(target_id);
  5523. if (bl == NULL /*|| bl->type != BL_PC*/)
  5524. return 1;
  5525. if (sd->followtimer != INVALID_TIMER)
  5526. pc_stop_following(sd);
  5527. sd->followtarget = target_id;
  5528. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5529. return 0;
  5530. }
  5531. int pc_checkbaselevelup(struct map_session_data *sd) {
  5532. unsigned int next = pc_nextbaseexp(sd);
  5533. if (!next || sd->status.base_exp < next)
  5534. return 0;
  5535. do {
  5536. sd->status.base_exp -= next;
  5537. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5538. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  5539. sd->status.base_exp = next-1;
  5540. next = pc_gets_status_point(sd->status.base_level);
  5541. sd->status.base_level ++;
  5542. sd->status.status_point += next;
  5543. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5544. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5545. status_calc_pet(sd->pd,SCO_NONE);
  5546. clif_updatestatus(sd,SP_STATUSPOINT);
  5547. clif_updatestatus(sd,SP_BASELEVEL);
  5548. clif_updatestatus(sd,SP_BASEEXP);
  5549. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5550. status_calc_pc(sd,SCO_FORCE);
  5551. status_percent_heal(&sd->bl,100,100);
  5552. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5553. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5554. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5555. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5556. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5557. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5558. if (sd->state.snovice_dead_flag)
  5559. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5560. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5561. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5562. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5563. }
  5564. clif_misceffect(&sd->bl,0);
  5565. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5566. if(sd->status.party_id)
  5567. party_send_levelup(sd);
  5568. pc_baselevelchanged(sd);
  5569. return 1;
  5570. }
  5571. void pc_baselevelchanged(struct map_session_data *sd) {
  5572. uint8 i;
  5573. for( i = 0; i < EQI_MAX; i++ ) {
  5574. if( sd->equip_index[i] >= 0 ) {
  5575. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5576. pc_unequipitem(sd, sd->equip_index[i], 3);
  5577. }
  5578. }
  5579. }
  5580. int pc_checkjoblevelup(struct map_session_data *sd)
  5581. {
  5582. unsigned int next = pc_nextjobexp(sd);
  5583. nullpo_ret(sd);
  5584. if(!next || sd->status.job_exp < next)
  5585. return 0;
  5586. do {
  5587. sd->status.job_exp -= next;
  5588. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5589. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  5590. sd->status.job_exp = next-1;
  5591. sd->status.job_level ++;
  5592. sd->status.skill_point ++;
  5593. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5594. clif_updatestatus(sd,SP_JOBLEVEL);
  5595. clif_updatestatus(sd,SP_JOBEXP);
  5596. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5597. clif_updatestatus(sd,SP_SKILLPOINT);
  5598. status_calc_pc(sd,SCO_FORCE);
  5599. clif_misceffect(&sd->bl,1);
  5600. if (pc_checkskill(sd, SG_DEVIL) && !pc_nextjobexp(sd))
  5601. clif_status_change(&sd->bl,SI_DEVIL, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5602. npc_script_event(sd, NPCE_JOBLVUP);
  5603. return 1;
  5604. }
  5605. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  5606. * @param sd Player
  5607. * @param base_exp Base EXP before peronal bonuses
  5608. * @param job_exp Job EXP before peronal bonuses
  5609. * @param src Block list that affecting the exp calculation
  5610. */
  5611. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5612. {
  5613. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  5614. if (src) {
  5615. struct status_data *status = status_get_status_data(src);
  5616. if( sd->expaddrace[status->race] )
  5617. bonus += sd->expaddrace[status->race];
  5618. if( sd->expaddrace[RC_ALL] )
  5619. bonus += sd->expaddrace[RC_ALL];
  5620. if( sd->expaddclass[status->class_] )
  5621. bonus += sd->expaddclass[status->class_];
  5622. if( sd->expaddclass[CLASS_ALL] )
  5623. bonus += sd->expaddclass[CLASS_ALL];
  5624. if (battle_config.pk_mode &&
  5625. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5626. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5627. #ifdef VIP_ENABLE
  5628. //EXP bonus for VIP player
  5629. if (src && src->type == BL_MOB && pc_isvip(sd)) {
  5630. vip_bonus_base = battle_config.vip_base_exp_increase;
  5631. vip_bonus_job = battle_config.vip_job_exp_increase;
  5632. }
  5633. #endif
  5634. }
  5635. if (&sd->sc && sd->sc.data[SC_EXPBOOST]) {
  5636. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5637. if( battle_config.vip_bm_increase && pc_isvip(sd) ) // Increase Battle Manual EXP rate for VIP.
  5638. bonus += ( sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase );
  5639. }
  5640. *base_exp = (unsigned int) cap_value(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100., 1, UINT_MAX);
  5641. if (&sd->sc && sd->sc.data[SC_JEXPBOOST])
  5642. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5643. *job_exp = (unsigned int) cap_value(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100., 1, UINT_MAX);
  5644. return;
  5645. }
  5646. /*==========================================
  5647. * Give x exp at sd player and calculate remaining exp for next lvl
  5648. *------------------------------------------*/
  5649. int pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, bool quest)
  5650. {
  5651. float nextbp = 0, nextjp = 0;
  5652. unsigned int nextb = 0, nextj = 0;
  5653. nullpo_ret(sd);
  5654. if(sd->bl.prev == NULL || pc_isdead(sd))
  5655. return 0;
  5656. if(!battle_config.pvp_exp && map[sd->bl.m].flag.pvp) // [MouseJstr]
  5657. return 0; // no exp on pvp maps
  5658. if(sd->status.guild_id>0)
  5659. base_exp-=guild_payexp(sd,base_exp);
  5660. pc_calcexp(sd, &base_exp, &job_exp, src); // Give (J)EXPBOOST for quests even if src is NULL.
  5661. nextb = pc_nextbaseexp(sd);
  5662. nextj = pc_nextjobexp(sd);
  5663. if(sd->state.showexp || battle_config.max_exp_gain_rate){
  5664. if (nextb > 0)
  5665. nextbp = (float) base_exp / (float) nextb;
  5666. if (nextj > 0)
  5667. nextjp = (float) job_exp / (float) nextj;
  5668. if(battle_config.max_exp_gain_rate) {
  5669. if (nextbp > battle_config.max_exp_gain_rate/1000.) {
  5670. //Note that this value should never be greater than the original
  5671. //base_exp, therefore no overflow checks are needed. [Skotlex]
  5672. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  5673. if (sd->state.showexp)
  5674. nextbp = (float) base_exp / (float) nextb;
  5675. }
  5676. if (nextjp > battle_config.max_exp_gain_rate/1000.) {
  5677. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  5678. if (sd->state.showexp)
  5679. nextjp = (float) job_exp / (float) nextj;
  5680. }
  5681. }
  5682. }
  5683. //Cap exp to the level up requirement of the previous level when you are at max level, otherwise cap at UINT_MAX (this is required for some S. Novice bonuses). [Skotlex]
  5684. if (base_exp) {
  5685. nextb = nextb?UINT_MAX:pc_thisbaseexp(sd);
  5686. if(sd->status.base_exp > nextb - base_exp)
  5687. sd->status.base_exp = nextb;
  5688. else
  5689. sd->status.base_exp += base_exp;
  5690. pc_checkbaselevelup(sd);
  5691. clif_updatestatus(sd,SP_BASEEXP);
  5692. }
  5693. if (job_exp) {
  5694. nextj = nextj?UINT_MAX:pc_thisjobexp(sd);
  5695. if(sd->status.job_exp > nextj - job_exp)
  5696. sd->status.job_exp = nextj;
  5697. else
  5698. sd->status.job_exp += job_exp;
  5699. pc_checkjoblevelup(sd);
  5700. clif_updatestatus(sd,SP_JOBEXP);
  5701. }
  5702. if(base_exp)
  5703. clif_displayexp(sd, base_exp, SP_BASEEXP, quest);
  5704. if(job_exp)
  5705. clif_displayexp(sd, job_exp, SP_JOBEXP, quest);
  5706. if(sd->state.showexp) {
  5707. char output[256];
  5708. sprintf(output,
  5709. "Experience Gained Base:%u (%.2f%%) Job:%u (%.2f%%)",base_exp,nextbp*(float)100,job_exp,nextjp*(float)100);
  5710. clif_disp_onlyself(sd,output,strlen(output));
  5711. }
  5712. return 1;
  5713. }
  5714. /*==========================================
  5715. * Returns max level for this character.
  5716. *------------------------------------------*/
  5717. unsigned int pc_maxbaselv(struct map_session_data *sd){
  5718. return job_info[pc_class2idx(sd->status.class_)].max_level[0];
  5719. }
  5720. unsigned int pc_maxjoblv(struct map_session_data *sd){
  5721. return job_info[pc_class2idx(sd->status.class_)].max_level[1];
  5722. }
  5723. /*==========================================
  5724. * base level exp lookup.
  5725. *------------------------------------------*/
  5726. //Base exp needed for next level.
  5727. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  5728. nullpo_ret(sd);
  5729. if(sd->status.base_level>=pc_maxbaselv(sd) || sd->status.base_level==0)
  5730. return 0;
  5731. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  5732. }
  5733. //Base exp needed for this level.
  5734. unsigned int pc_thisbaseexp(struct map_session_data *sd){
  5735. if(sd->status.base_level>pc_maxbaselv(sd) || sd->status.base_level<=1)
  5736. return 0;
  5737. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-2];
  5738. }
  5739. /*==========================================
  5740. * job level exp lookup
  5741. * Return:
  5742. * 0 = not found
  5743. * x = exp for level
  5744. *------------------------------------------*/
  5745. //Job exp needed for next level.
  5746. unsigned int pc_nextjobexp(struct map_session_data *sd){
  5747. nullpo_ret(sd);
  5748. if(sd->status.job_level>=pc_maxjoblv(sd) || sd->status.job_level==0)
  5749. return 0;
  5750. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  5751. }
  5752. //Job exp needed for this level.
  5753. unsigned int pc_thisjobexp(struct map_session_data *sd){
  5754. if(sd->status.job_level>pc_maxjoblv(sd) || sd->status.job_level<=1)
  5755. return 0;
  5756. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-2];
  5757. }
  5758. /// Returns the value of the specified stat.
  5759. static int pc_getstat(struct map_session_data* sd, int type)
  5760. {
  5761. nullpo_retr(-1, sd);
  5762. switch( type ) {
  5763. case SP_STR: return sd->status.str;
  5764. case SP_AGI: return sd->status.agi;
  5765. case SP_VIT: return sd->status.vit;
  5766. case SP_INT: return sd->status.int_;
  5767. case SP_DEX: return sd->status.dex;
  5768. case SP_LUK: return sd->status.luk;
  5769. default:
  5770. return -1;
  5771. }
  5772. }
  5773. /// Sets the specified stat to the specified value.
  5774. /// Returns the new value.
  5775. static int pc_setstat(struct map_session_data* sd, int type, int val)
  5776. {
  5777. nullpo_retr(-1, sd);
  5778. switch( type ) {
  5779. case SP_STR: sd->status.str = val; break;
  5780. case SP_AGI: sd->status.agi = val; break;
  5781. case SP_VIT: sd->status.vit = val; break;
  5782. case SP_INT: sd->status.int_ = val; break;
  5783. case SP_DEX: sd->status.dex = val; break;
  5784. case SP_LUK: sd->status.luk = val; break;
  5785. default:
  5786. return -1;
  5787. }
  5788. return val;
  5789. }
  5790. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  5791. int pc_gets_status_point(int level)
  5792. {
  5793. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  5794. return (statp[level+1] - statp[level]);
  5795. else //Default increase
  5796. return ((level+15) / 5);
  5797. }
  5798. #ifdef RENEWAL_STAT
  5799. /// Renewal status point cost formula
  5800. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  5801. #else
  5802. /// Pre-Renewal status point cost formula
  5803. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  5804. #endif
  5805. /// Returns the number of stat points needed to change the specified stat by val.
  5806. /// If val is negative, returns the number of stat points that would be needed to
  5807. /// raise the specified stat from (current value - val) to current value.
  5808. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  5809. {
  5810. int low, high, sp = 0, max = 0;
  5811. if ( val == 0 )
  5812. return 0;
  5813. low = pc_getstat(sd,type);
  5814. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  5815. if ( low >= max && val > 0 )
  5816. return 0; // Official servers show '0' when max is reached
  5817. high = low + val;
  5818. if ( val < 0 )
  5819. swap(low, high);
  5820. for ( ; low < high; low++ )
  5821. sp += PC_STATUS_POINT_COST(low);
  5822. return sp;
  5823. }
  5824. /**
  5825. * Returns the value the specified stat can be increased by with the current
  5826. * amount of available status points for the current character's class.
  5827. *
  5828. * @param sd The target character.
  5829. * @param type Stat to verify.
  5830. * @return Maximum value the stat could grow by.
  5831. */
  5832. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  5833. {
  5834. int base, final_val, status_points, max_param;
  5835. nullpo_ret(sd);
  5836. base = final_val = pc_getstat(sd, type);
  5837. status_points = sd->status.status_point;
  5838. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  5839. while (final_val <= max_param && status_points >= 0) {
  5840. status_points -= PC_STATUS_POINT_COST(final_val);
  5841. final_val++;
  5842. }
  5843. final_val--;
  5844. return (final_val > base ? final_val-base : 0);
  5845. }
  5846. /**
  5847. * Raises a stat by the specified amount.
  5848. *
  5849. * Obeys max_parameter limits.
  5850. * Subtracts status points according to the cost of the increased stat points.
  5851. *
  5852. * @param sd The target character.
  5853. * @param type The stat to change (see enum _sp)
  5854. * @param increase The stat increase (strictly positive) amount.
  5855. * @retval true if the stat was increased by any amount.
  5856. * @retval false if there were no changes.
  5857. */
  5858. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  5859. {
  5860. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  5861. nullpo_ret(sd);
  5862. // check conditions
  5863. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  5864. clif_statusupack(sd, type, 0, 0);
  5865. return false;
  5866. }
  5867. // check limits
  5868. current = pc_getstat(sd, type);
  5869. max_increase = pc_maxparameterincrease(sd, type);
  5870. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  5871. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  5872. clif_statusupack(sd, type, 0, 0);
  5873. return false;
  5874. }
  5875. // check status points
  5876. needed_points = pc_need_status_point(sd, type, increase);
  5877. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  5878. clif_statusupack(sd, type, 0, 0);
  5879. return false;
  5880. }
  5881. // set new values
  5882. final_value = pc_setstat(sd, type, current + increase);
  5883. sd->status.status_point -= needed_points;
  5884. status_calc_pc(sd,SCO_NONE);
  5885. // update increase cost indicator
  5886. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  5887. // update statpoint count
  5888. clif_updatestatus(sd, SP_STATUSPOINT);
  5889. // update stat value
  5890. clif_statusupack(sd, type, 1, final_value); // required
  5891. if( final_value > 255 )
  5892. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  5893. return true;
  5894. }
  5895. /**
  5896. * Raises a stat by the specified amount.
  5897. *
  5898. * Obeys max_parameter limits.
  5899. * Does not subtract status points for the cost of the modified stat points.
  5900. *
  5901. * @param sd The target character.
  5902. * @param type The stat to change (see enum _sp)
  5903. * @param val The stat increase (or decrease) amount.
  5904. * @return the stat increase amount.
  5905. * @retval 0 if no changes were made.
  5906. */
  5907. int pc_statusup2(struct map_session_data* sd, int type, int val)
  5908. {
  5909. int max, need;
  5910. nullpo_ret(sd);
  5911. if( type < SP_STR || type > SP_LUK )
  5912. {
  5913. clif_statusupack(sd,type,0,0);
  5914. return 0;
  5915. }
  5916. need = pc_need_status_point(sd,type,1);
  5917. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  5918. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  5919. status_calc_pc(sd,SCO_NONE);
  5920. // update increase cost indicator
  5921. if( need != pc_need_status_point(sd,type,1) )
  5922. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  5923. // update stat value
  5924. clif_statusupack(sd,type,1,val); // required
  5925. if( val > 255 )
  5926. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  5927. return val;
  5928. }
  5929. /*==========================================
  5930. * Update skill_lv for player sd
  5931. * Skill point allocation
  5932. *------------------------------------------*/
  5933. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  5934. {
  5935. uint16 idx = skill_get_index(skill_id);
  5936. nullpo_retv(sd);
  5937. if (!idx) {
  5938. if (skill_id)
  5939. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  5940. return;
  5941. }
  5942. // Level up guild skill
  5943. if (SKILL_CHK_GUILD(skill_id)) {
  5944. guild_skillup(sd, skill_id);
  5945. return;
  5946. }
  5947. // Level up homunculus skill
  5948. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  5949. hom_skillup(sd->hd, skill_id);
  5950. return;
  5951. }
  5952. else {
  5953. if( sd->status.skill_point > 0 &&
  5954. sd->status.skill[idx].id &&
  5955. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  5956. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  5957. {
  5958. int lv, range, upgradable;
  5959. sd->status.skill[idx].lv++;
  5960. sd->status.skill_point--;
  5961. if( !skill_get_inf(skill_id) )
  5962. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  5963. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  5964. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  5965. else
  5966. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  5967. lv = sd->status.skill[idx].lv;
  5968. range = skill_get_range2(&sd->bl, skill_id, lv);
  5969. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  5970. clif_skillup(sd,skill_id,lv,range,upgradable);
  5971. clif_updatestatus(sd,SP_SKILLPOINT);
  5972. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  5973. clif_updatestatus(sd,SP_CARTINFO);
  5974. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  5975. clif_skillinfoblock(sd);
  5976. }
  5977. //else
  5978. // 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);
  5979. }
  5980. }
  5981. /*==========================================
  5982. * /allskill
  5983. *------------------------------------------*/
  5984. int pc_allskillup(struct map_session_data *sd)
  5985. {
  5986. int i;
  5987. nullpo_ret(sd);
  5988. for (i = 0; i < MAX_SKILL; i++) {
  5989. 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) {
  5990. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  5991. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5992. if (sd->status.skill[i].lv == 0)
  5993. sd->status.skill[i].id = 0;
  5994. }
  5995. }
  5996. if (!pc_grant_allskills(sd, true)) {
  5997. uint16 sk_id;
  5998. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].id) > 0;i++){
  5999. int inf2 = 0;
  6000. uint16 sk_idx = 0;
  6001. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6002. continue;
  6003. inf2 = skill_get_inf2(sk_id);
  6004. if (
  6005. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6006. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6007. sk_id == SG_DEVIL
  6008. )
  6009. continue; //Cannot be learned normally.
  6010. sd->status.skill[sk_idx].id = sk_id;
  6011. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6012. }
  6013. }
  6014. status_calc_pc(sd,SCO_NONE);
  6015. //Required because if you could level up all skills previously,
  6016. //the update will not be sent as only the lv variable changes.
  6017. clif_skillinfoblock(sd);
  6018. return 0;
  6019. }
  6020. /*==========================================
  6021. * /resetlvl
  6022. *------------------------------------------*/
  6023. int pc_resetlvl(struct map_session_data* sd,int type)
  6024. {
  6025. int i;
  6026. nullpo_ret(sd);
  6027. if (type != 3) //Also reset skills
  6028. pc_resetskill(sd, 0);
  6029. if(type == 1){
  6030. sd->status.skill_point=0;
  6031. sd->status.base_level=1;
  6032. sd->status.job_level=1;
  6033. sd->status.base_exp=0;
  6034. sd->status.job_exp=0;
  6035. if(sd->sc.option !=0)
  6036. sd->sc.option = 0;
  6037. sd->status.str=1;
  6038. sd->status.agi=1;
  6039. sd->status.vit=1;
  6040. sd->status.int_=1;
  6041. sd->status.dex=1;
  6042. sd->status.luk=1;
  6043. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6044. sd->status.status_point=100; // not 88 [celest]
  6045. // give platinum skills upon changing
  6046. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6047. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6048. }
  6049. }
  6050. if(type == 2){
  6051. sd->status.skill_point=0;
  6052. sd->status.base_level=1;
  6053. sd->status.job_level=1;
  6054. sd->status.base_exp=0;
  6055. sd->status.job_exp=0;
  6056. }
  6057. if(type == 3){
  6058. sd->status.base_level=1;
  6059. sd->status.base_exp=0;
  6060. }
  6061. if(type == 4){
  6062. sd->status.job_level=1;
  6063. sd->status.job_exp=0;
  6064. }
  6065. clif_updatestatus(sd,SP_STATUSPOINT);
  6066. clif_updatestatus(sd,SP_STR);
  6067. clif_updatestatus(sd,SP_AGI);
  6068. clif_updatestatus(sd,SP_VIT);
  6069. clif_updatestatus(sd,SP_INT);
  6070. clif_updatestatus(sd,SP_DEX);
  6071. clif_updatestatus(sd,SP_LUK);
  6072. clif_updatestatus(sd,SP_BASELEVEL);
  6073. clif_updatestatus(sd,SP_JOBLEVEL);
  6074. clif_updatestatus(sd,SP_STATUSPOINT);
  6075. clif_updatestatus(sd,SP_BASEEXP);
  6076. clif_updatestatus(sd,SP_JOBEXP);
  6077. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6078. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6079. clif_updatestatus(sd,SP_SKILLPOINT);
  6080. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6081. clif_updatestatus(sd,SP_UAGI);
  6082. clif_updatestatus(sd,SP_UVIT);
  6083. clif_updatestatus(sd,SP_UINT);
  6084. clif_updatestatus(sd,SP_UDEX);
  6085. clif_updatestatus(sd,SP_ULUK); // End Addition
  6086. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6087. if(sd->equip_index[i] >= 0)
  6088. if(pc_isequip(sd,sd->equip_index[i]))
  6089. pc_unequipitem(sd,sd->equip_index[i],2);
  6090. }
  6091. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6092. party_send_levelup(sd);
  6093. status_calc_pc(sd, SCO_FORCE);
  6094. clif_skillinfoblock(sd);
  6095. return 0;
  6096. }
  6097. /*==========================================
  6098. * /resetstate
  6099. *------------------------------------------*/
  6100. int pc_resetstate(struct map_session_data* sd)
  6101. {
  6102. nullpo_ret(sd);
  6103. if (battle_config.use_statpoint_table)
  6104. { // New statpoint table used here - Dexity
  6105. if (sd->status.base_level > MAX_LEVEL)
  6106. { //statp[] goes out of bounds, can't reset!
  6107. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6108. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6109. return 0;
  6110. }
  6111. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6112. }
  6113. else
  6114. {
  6115. int add=0;
  6116. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6117. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6118. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6119. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6120. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6121. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6122. sd->status.status_point+=add;
  6123. }
  6124. pc_setstat(sd, SP_STR, 1);
  6125. pc_setstat(sd, SP_AGI, 1);
  6126. pc_setstat(sd, SP_VIT, 1);
  6127. pc_setstat(sd, SP_INT, 1);
  6128. pc_setstat(sd, SP_DEX, 1);
  6129. pc_setstat(sd, SP_LUK, 1);
  6130. clif_updatestatus(sd,SP_STR);
  6131. clif_updatestatus(sd,SP_AGI);
  6132. clif_updatestatus(sd,SP_VIT);
  6133. clif_updatestatus(sd,SP_INT);
  6134. clif_updatestatus(sd,SP_DEX);
  6135. clif_updatestatus(sd,SP_LUK);
  6136. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6137. clif_updatestatus(sd,SP_UAGI);
  6138. clif_updatestatus(sd,SP_UVIT);
  6139. clif_updatestatus(sd,SP_UINT);
  6140. clif_updatestatus(sd,SP_UDEX);
  6141. clif_updatestatus(sd,SP_ULUK); // End Addition
  6142. clif_updatestatus(sd,SP_STATUSPOINT);
  6143. if( sd->mission_mobid ) { //bugreport:2200
  6144. sd->mission_mobid = 0;
  6145. sd->mission_count = 0;
  6146. pc_setglobalreg(sd,"TK_MISSION_ID", 0);
  6147. }
  6148. status_calc_pc(sd, SCO_NONE);
  6149. return 1;
  6150. }
  6151. /*==========================================
  6152. * /resetskill
  6153. * if flag&1, perform block resync and status_calc call.
  6154. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6155. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6156. *------------------------------------------*/
  6157. int pc_resetskill(struct map_session_data* sd, int flag)
  6158. {
  6159. int i, skill_point=0;
  6160. nullpo_ret(sd);
  6161. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6162. return 0;
  6163. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6164. /**
  6165. * 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)
  6166. **/
  6167. if( pc_is_taekwon_ranker(sd) )
  6168. return 0;
  6169. if( pc_checkskill(sd, SG_DEVIL) && !pc_nextjobexp(sd) )
  6170. clif_status_load(&sd->bl, SI_DEVIL, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6171. i = sd->sc.option;
  6172. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6173. i &= ~OPTION_RIDING;
  6174. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6175. i &= ~OPTION_FALCON;
  6176. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6177. i &= ~OPTION_DRAGON;
  6178. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6179. i &= ~OPTION_WUG;
  6180. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6181. i &= ~OPTION_WUGRIDER;
  6182. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6183. i &= ~OPTION_MADOGEAR;
  6184. #ifndef NEW_CARTS
  6185. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6186. i &= ~OPTION_CART;
  6187. #else
  6188. if( sd->sc.data[SC_PUSH_CART] )
  6189. pc_setcart(sd, 0);
  6190. #endif
  6191. if( i != sd->sc.option )
  6192. pc_setoption(sd, i);
  6193. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6194. hom_vaporize(sd, HOM_ST_ACTIVE);
  6195. }
  6196. for( i = 1; i < MAX_SKILL; i++ )
  6197. {
  6198. uint8 lv = sd->status.skill[i].lv;
  6199. int inf2;
  6200. uint16 skill_id = skill_idx2id(i);
  6201. if (lv == 0 || skill_id == 0)
  6202. continue;
  6203. inf2 = skill_get_inf2(skill_id);
  6204. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6205. continue;
  6206. // Don't reset trick dead if not a novice/baby
  6207. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6208. {
  6209. sd->status.skill[i].lv = 0;
  6210. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6211. continue;
  6212. }
  6213. // do not reset basic skill
  6214. if( skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6215. continue;
  6216. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6217. continue;
  6218. if( flag&4 && !skill_ischangesex(skill_id) )
  6219. continue;
  6220. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6221. { //Only handle quest skills in a special way when you can't learn them manually
  6222. if( battle_config.quest_skill_reset && !(flag&2) )
  6223. { //Wipe them
  6224. sd->status.skill[i].lv = 0;
  6225. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6226. }
  6227. continue;
  6228. }
  6229. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6230. skill_point += lv;
  6231. else
  6232. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6233. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6234. if( !(flag&2) )
  6235. {// reset
  6236. sd->status.skill[i].lv = 0;
  6237. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6238. }
  6239. }
  6240. if( flag&2 || !skill_point ) return skill_point;
  6241. sd->status.skill_point += skill_point;
  6242. if (flag&1) {
  6243. clif_updatestatus(sd,SP_SKILLPOINT);
  6244. clif_skillinfoblock(sd);
  6245. status_calc_pc(sd, SCO_FORCE);
  6246. }
  6247. return skill_point;
  6248. }
  6249. /*==========================================
  6250. * /resetfeel [Komurka]
  6251. *------------------------------------------*/
  6252. int pc_resetfeel(struct map_session_data* sd)
  6253. {
  6254. int i;
  6255. nullpo_ret(sd);
  6256. for (i=0; i<MAX_PC_FEELHATE; i++)
  6257. {
  6258. sd->feel_map[i].m = -1;
  6259. sd->feel_map[i].index = 0;
  6260. pc_setglobalreg(sd,sg_info[i].feel_var,0);
  6261. }
  6262. return 0;
  6263. }
  6264. int pc_resethate(struct map_session_data* sd)
  6265. {
  6266. int i;
  6267. nullpo_ret(sd);
  6268. for (i=0; i<3; i++)
  6269. {
  6270. sd->hate_mob[i] = -1;
  6271. pc_setglobalreg(sd,sg_info[i].hate_var,0);
  6272. }
  6273. return 0;
  6274. }
  6275. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6276. {
  6277. int i, bonus = 0;
  6278. nullpo_ret(sd);
  6279. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  6280. if( i < ARRAYLENGTH(sd->skillatk) )
  6281. bonus = sd->skillatk[i].val;
  6282. return bonus;
  6283. }
  6284. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6285. {
  6286. int i, bonus = 0;
  6287. nullpo_ret(sd);
  6288. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == skill_id);
  6289. if( i < ARRAYLENGTH(sd->subskill) )
  6290. bonus = sd->subskill[i].val;
  6291. return bonus;
  6292. }
  6293. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6294. int i, bonus = sd->bonus.add_heal_rate;
  6295. if( bonus ) {
  6296. switch( skill_id ) {
  6297. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6298. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6299. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6300. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6301. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6302. }
  6303. }
  6304. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  6305. if( i < ARRAYLENGTH(sd->skillheal) )
  6306. bonus += sd->skillheal[i].val;
  6307. return bonus;
  6308. }
  6309. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6310. int i, bonus = sd->bonus.add_heal2_rate;
  6311. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  6312. if( i < ARRAYLENGTH(sd->skillheal2) )
  6313. bonus += sd->skillheal2[i].val;
  6314. return bonus;
  6315. }
  6316. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6317. {
  6318. if( !pc_isdead(sd) )
  6319. return; // not applicable
  6320. if( sd->bg_id && bg_member_respawn(sd) )
  6321. return; // member revived by battleground
  6322. pc_setstand(sd, true);
  6323. pc_setrestartvalue(sd,3);
  6324. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) )
  6325. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6326. }
  6327. static int pc_respawn_timer(int tid, unsigned int tick, int id, intptr_t data)
  6328. {
  6329. struct map_session_data *sd = map_id2sd(id);
  6330. if( sd != NULL )
  6331. {
  6332. sd->pvp_point=0;
  6333. pc_respawn(sd,CLR_OUTSIGHT);
  6334. }
  6335. return 0;
  6336. }
  6337. /*==========================================
  6338. * Invoked when a player has received damage
  6339. *------------------------------------------*/
  6340. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6341. {
  6342. if (sp) clif_updatestatus(sd,SP_SP);
  6343. if (hp) clif_updatestatus(sd,SP_HP);
  6344. else return;
  6345. if( !src || src == &sd->bl )
  6346. return;
  6347. if( pc_issit(sd) ) {
  6348. pc_setstand(sd, true);
  6349. skill_sit(sd,0);
  6350. }
  6351. if( sd->progressbar.npc_id )
  6352. clif_progressbar_abort(sd);
  6353. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6354. pet_target_check(sd,src,1);
  6355. if( sd->status.ele_id > 0 )
  6356. elemental_set_target(sd,src);
  6357. sd->canlog_tick = gettick();
  6358. }
  6359. int pc_close_npc_timer(int tid, unsigned int tick, int id, intptr_t data)
  6360. {
  6361. TBL_PC *sd = map_id2sd(id);
  6362. if(sd) pc_close_npc(sd,data);
  6363. return 0;
  6364. }
  6365. /*
  6366. * Method to properly close npc for player and clear anything related
  6367. * @flag == 1 : produce close button
  6368. * @flag == 2 : directly close it
  6369. */
  6370. void pc_close_npc(struct map_session_data *sd,int flag)
  6371. {
  6372. nullpo_retv(sd);
  6373. if (sd->npc_id || sd->npc_shopid) {
  6374. if (sd->state.using_fake_npc) {
  6375. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6376. sd->state.using_fake_npc = 0;
  6377. }
  6378. if (sd->st) {
  6379. if(sd->st->state == RUN){ //wait ending code execution
  6380. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6381. return;
  6382. }
  6383. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6384. sd->st->mes_active = 0;
  6385. }
  6386. sd->state.menu_or_input = 0;
  6387. sd->npc_menu = 0;
  6388. sd->npc_shopid = 0;
  6389. #ifdef SECURE_NPCTIMEOUT
  6390. sd->npc_idle_timer = INVALID_TIMER;
  6391. #endif
  6392. clif_scriptclose(sd,sd->npc_id);
  6393. clif_scriptclear(sd,sd->npc_id); // [Ind/Hercules]
  6394. if(sd->st && sd->st->state == END ) {// free attached scripts that are waiting
  6395. script_free_state(sd->st);
  6396. sd->st = NULL;
  6397. sd->npc_id = 0;
  6398. }
  6399. }
  6400. }
  6401. /*==========================================
  6402. * Invoked when a player has negative current hp
  6403. *------------------------------------------*/
  6404. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6405. {
  6406. int i=0,k=0;
  6407. unsigned int tick = gettick();
  6408. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6409. // Super Novices have no kill or die functions attached when saved by their angel
  6410. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6411. unsigned int next = pc_nextbaseexp(sd);
  6412. if( next == 0 ) next = pc_thisbaseexp(sd);
  6413. if( get_percentage(sd->status.base_exp,next) >= 99 ) {
  6414. sd->state.snovice_dead_flag = 1;
  6415. pc_setrestartvalue(sd,1);
  6416. status_percent_heal(&sd->bl, 100, 100);
  6417. clif_resurrection(&sd->bl, 1);
  6418. if(battle_config.pc_invincible_time)
  6419. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6420. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6421. if(map_flag_gvg(sd->bl.m))
  6422. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6423. return 0;
  6424. }
  6425. }
  6426. for(k = 0; k < MAX_DEVOTION; k++) {
  6427. if (sd->devotion[k]){
  6428. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6429. if (devsd)
  6430. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6431. sd->devotion[k] = 0;
  6432. }
  6433. }
  6434. if(sd->shadowform_id) { //if we were target of shadowform
  6435. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6436. sd->shadowform_id = 0; //should be remove on status end anyway
  6437. }
  6438. if(sd->status.pet_id > 0 && sd->pd) {
  6439. struct pet_data *pd = sd->pd;
  6440. if( !map[sd->bl.m].flag.noexppenalty ) {
  6441. pet_set_intimate(pd, pd->pet.intimate - pd->petDB->die);
  6442. if( pd->pet.intimate < 0 )
  6443. pd->pet.intimate = 0;
  6444. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  6445. }
  6446. if( sd->pd->target_id ) // Unlock all targets...
  6447. pet_unlocktarget(sd->pd);
  6448. }
  6449. if (sd->status.hom_id > 0) {
  6450. if(battle_config.homunculus_auto_vapor && sd->hd)
  6451. hom_vaporize(sd, HOM_ST_ACTIVE);
  6452. }
  6453. if( sd->md )
  6454. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  6455. if( sd->ed )
  6456. elemental_delete(sd->ed, 0);
  6457. // Leave duel if you die [LuzZza]
  6458. if(battle_config.duel_autoleave_when_die) {
  6459. if(sd->duel_group > 0)
  6460. duel_leave(sd->duel_group, sd);
  6461. if(sd->duel_invite > 0)
  6462. duel_reject(sd->duel_invite, sd);
  6463. }
  6464. pc_close_npc(sd,2); //close npc if we were using one
  6465. /* e.g. not killed thru pc_damage */
  6466. if( pc_issit(sd) ) {
  6467. clif_status_load(&sd->bl,SI_SIT,0);
  6468. }
  6469. pc_setdead(sd);
  6470. pc_setglobalreg(sd,"PC_DIE_COUNTER",sd->die_counter+1);
  6471. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  6472. //Reset menu skills/item skills
  6473. if ((sd->skillitem) != 0)
  6474. sd->skillitem = sd->skillitemlv = 0;
  6475. if ((sd->menuskill_id) != 0)
  6476. sd->menuskill_id = sd->menuskill_val = 0;
  6477. //Reset ticks.
  6478. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  6479. if ( sd->spiritball !=0 )
  6480. pc_delspiritball(sd,sd->spiritball,0);
  6481. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  6482. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  6483. if (src)
  6484. switch (src->type) {
  6485. case BL_MOB:
  6486. {
  6487. struct mob_data *md=(struct mob_data *)src;
  6488. if(md->target_id==sd->bl.id)
  6489. mob_unlocktarget(md,tick);
  6490. if(battle_config.mobs_level_up && md->status.hp &&
  6491. (unsigned int)md->level < pc_maxbaselv(sd) &&
  6492. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  6493. ) { // monster level up [Valaris]
  6494. clif_misceffect(&md->bl,0);
  6495. md->level++;
  6496. status_calc_mob(md, SCO_NONE);
  6497. status_percent_heal(src,10,0);
  6498. if( battle_config.show_mob_info&4 )
  6499. {// update name with new level
  6500. clif_charnameack(0, &md->bl);
  6501. }
  6502. }
  6503. src = battle_get_master(src); // Maybe Player Summon
  6504. }
  6505. break;
  6506. case BL_PET: //Pass on to master...
  6507. case BL_HOM:
  6508. case BL_MER:
  6509. src = battle_get_master(src);
  6510. break;
  6511. }
  6512. if (src && src->type == BL_PC) {
  6513. struct map_session_data *ssd = (struct map_session_data *)src;
  6514. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  6515. npc_script_event(ssd, NPCE_KILLPC);
  6516. if (battle_config.pk_mode&2) {
  6517. ssd->status.manner -= 5;
  6518. if(ssd->status.manner < 0)
  6519. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6520. #if 0
  6521. // PK/Karma system code (not enabled yet) [celest]
  6522. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6523. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6524. // karma going down = more 'good' / more honourable.
  6525. // The Karma System way...
  6526. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6527. sd->status.karma--;
  6528. ssd->status.karma--;
  6529. }
  6530. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6531. ssd->status.karma++;
  6532. // or the PK System way...
  6533. if (sd->status.karma > 0) // player killed is dishonourable?
  6534. ssd->status.karma--; // honour points earned
  6535. sd->status.karma++; // honour points lost
  6536. // To-do: Receive exp on certain occasions
  6537. #endif
  6538. }
  6539. }
  6540. if(battle_config.bone_drop==2
  6541. || (battle_config.bone_drop==1 && map[sd->bl.m].flag.pvp))
  6542. {
  6543. struct item item_tmp;
  6544. memset(&item_tmp,0,sizeof(item_tmp));
  6545. item_tmp.nameid=ITEMID_SKULL_;
  6546. item_tmp.identify=1;
  6547. item_tmp.card[0]=CARD0_CREATE;
  6548. item_tmp.card[1]=0;
  6549. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6550. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6551. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  6552. }
  6553. //Remove bonus_script when dead
  6554. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  6555. // changed penalty options, added death by player if pk_mode [Valaris]
  6556. if(battle_config.death_penalty_type
  6557. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6558. && !map[sd->bl.m].flag.noexppenalty && !map_flag_gvg(sd->bl.m)
  6559. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE])
  6560. {
  6561. uint32 base_penalty = battle_config.death_penalty_base;
  6562. uint32 job_penalty = battle_config.death_penalty_job;
  6563. uint32 zeny_penalty = battle_config.zeny_penalty;
  6564. #ifdef VIP_ENABLE
  6565. if(pc_isvip(sd)){
  6566. base_penalty *= battle_config.vip_exp_penalty_base;
  6567. job_penalty *= battle_config.vip_exp_penalty_job;
  6568. }
  6569. else {
  6570. base_penalty *= battle_config.vip_exp_penalty_base_normal;
  6571. job_penalty *= battle_config.vip_exp_penalty_job_normal;
  6572. }
  6573. #endif
  6574. if (base_penalty > 0) {
  6575. switch (battle_config.death_penalty_type) {
  6576. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  6577. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  6578. }
  6579. if (base_penalty > 0){ //recheck after altering to speedup
  6580. if (battle_config.pk_mode && src && src->type==BL_PC)
  6581. base_penalty*=2;
  6582. sd->status.base_exp -= min(sd->status.base_exp, base_penalty);
  6583. clif_updatestatus(sd,SP_BASEEXP);
  6584. }
  6585. }
  6586. if(job_penalty > 0) {
  6587. switch (battle_config.death_penalty_type) {
  6588. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  6589. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  6590. }
  6591. if(job_penalty) {
  6592. if (battle_config.pk_mode && src && src->type==BL_PC)
  6593. job_penalty*=2;
  6594. sd->status.job_exp -= min(sd->status.job_exp, job_penalty);
  6595. clif_updatestatus(sd,SP_JOBEXP);
  6596. }
  6597. }
  6598. if( zeny_penalty > 0 && !map[sd->bl.m].flag.nozenypenalty) {
  6599. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  6600. if(zeny_penalty)
  6601. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  6602. }
  6603. }
  6604. if(map[sd->bl.m].flag.pvp_nightmaredrop) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  6605. int j;
  6606. for(j=0;j<MAX_DROP_PER_MAP;j++){
  6607. int id = map[sd->bl.m].drop_list[j].drop_id;
  6608. int type = map[sd->bl.m].drop_list[j].drop_type;
  6609. int per = map[sd->bl.m].drop_list[j].drop_per;
  6610. if(id == 0)
  6611. continue;
  6612. if(id == -1){
  6613. int eq_num=0,eq_n[MAX_INVENTORY];
  6614. memset(eq_n,0,sizeof(eq_n));
  6615. for(i=0;i<MAX_INVENTORY;i++) {
  6616. if( (type == 1 && !sd->status.inventory[i].equip)
  6617. || (type == 2 && sd->status.inventory[i].equip)
  6618. || type == 3)
  6619. {
  6620. int l;
  6621. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  6622. if( l < MAX_INVENTORY )
  6623. eq_n[l] = i;
  6624. eq_num++;
  6625. }
  6626. }
  6627. if(eq_num > 0){
  6628. int n = eq_n[rnd()%eq_num];
  6629. if(rnd()%10000 < per) {
  6630. if(sd->status.inventory[n].equip)
  6631. pc_unequipitem(sd,n,3);
  6632. pc_dropitem(sd,n,1);
  6633. }
  6634. }
  6635. }
  6636. else if(id > 0) {
  6637. for(i=0;i<MAX_INVENTORY;i++){
  6638. if(sd->status.inventory[i].nameid == id
  6639. && rnd()%10000 < per
  6640. && ((type == 1 && !sd->status.inventory[i].equip)
  6641. || (type == 2 && sd->status.inventory[i].equip)
  6642. || type == 3) ){
  6643. if(sd->status.inventory[i].equip)
  6644. pc_unequipitem(sd,i,3);
  6645. pc_dropitem(sd,i,1);
  6646. break;
  6647. }
  6648. }
  6649. }
  6650. }
  6651. }
  6652. // pvp
  6653. // disable certain pvp functions on pk_mode [Valaris]
  6654. if( map[sd->bl.m].flag.pvp && !battle_config.pk_mode && !map[sd->bl.m].flag.pvp_nocalcrank ) {
  6655. sd->pvp_point -= 5;
  6656. sd->pvp_lost++;
  6657. if( src && src->type == BL_PC ) {
  6658. struct map_session_data *ssd = (struct map_session_data *)src;
  6659. ssd->pvp_point++;
  6660. ssd->pvp_won++;
  6661. }
  6662. if( sd->pvp_point < 0 ) {
  6663. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  6664. return 1|8;
  6665. }
  6666. }
  6667. //GvG
  6668. if( map_flag_gvg(sd->bl.m) ) {
  6669. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6670. return 1|8;
  6671. }
  6672. else if( sd->bg_id ) {
  6673. struct battleground_data *bg = bg_team_search(sd->bg_id);
  6674. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  6675. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6676. return 1|8;
  6677. }
  6678. }
  6679. //Reset "can log out" tick.
  6680. if( battle_config.prevent_logout )
  6681. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  6682. return 1;
  6683. }
  6684. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  6685. if(hp) clif_updatestatus(sd,SP_HP);
  6686. if(sp) clif_updatestatus(sd,SP_SP);
  6687. pc_setstand(sd, true);
  6688. if(battle_config.pc_invincible_time > 0)
  6689. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6690. if( sd->state.gmaster_flag ) {
  6691. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  6692. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  6693. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  6694. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  6695. }
  6696. }
  6697. // script
  6698. //
  6699. /*==========================================
  6700. * script reading pc status registry
  6701. *------------------------------------------*/
  6702. int pc_readparam(struct map_session_data* sd,int type)
  6703. {
  6704. int val = 0;
  6705. nullpo_ret(sd);
  6706. switch(type) {
  6707. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  6708. case SP_STATUSPOINT: val = sd->status.status_point; break;
  6709. case SP_ZENY: val = sd->status.zeny; break;
  6710. case SP_BASELEVEL: val = sd->status.base_level; break;
  6711. case SP_JOBLEVEL: val = sd->status.job_level; break;
  6712. case SP_CLASS: val = sd->status.class_; break;
  6713. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  6714. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  6715. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  6716. case SP_SEX: val = sd->status.sex; break;
  6717. case SP_WEIGHT: val = sd->weight; break;
  6718. case SP_MAXWEIGHT: val = sd->max_weight; break;
  6719. case SP_BASEEXP: val = sd->status.base_exp; break;
  6720. case SP_JOBEXP: val = sd->status.job_exp; break;
  6721. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  6722. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  6723. case SP_HP: val = sd->battle_status.hp; break;
  6724. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  6725. case SP_SP: val = sd->battle_status.sp; break;
  6726. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  6727. case SP_STR: val = sd->status.str; break;
  6728. case SP_AGI: val = sd->status.agi; break;
  6729. case SP_VIT: val = sd->status.vit; break;
  6730. case SP_INT: val = sd->status.int_; break;
  6731. case SP_DEX: val = sd->status.dex; break;
  6732. case SP_LUK: val = sd->status.luk; break;
  6733. case SP_KARMA: val = sd->status.karma; break;
  6734. case SP_MANNER: val = sd->status.manner; break;
  6735. case SP_FAME: val = sd->status.fame; break;
  6736. case SP_KILLERRID: val = sd->killerrid; break;
  6737. case SP_KILLEDRID: val = sd->killedrid; break;
  6738. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  6739. case SP_CHARMOVE: val = sd->status.character_moves; break;
  6740. case SP_CHARRENAME: val = sd->status.rename; break;
  6741. case SP_CHARFONT: val = sd->status.font; break;
  6742. case SP_BANK_VAULT: val = sd->bank_vault; break;
  6743. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  6744. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  6745. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  6746. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  6747. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  6748. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  6749. case SP_DEF1: val = sd->battle_status.def; break;
  6750. case SP_DEF2: val = sd->battle_status.def2; break;
  6751. case SP_MDEF1: val = sd->battle_status.mdef; break;
  6752. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  6753. case SP_HIT: val = sd->battle_status.hit; break;
  6754. case SP_FLEE1: val = sd->battle_status.flee; break;
  6755. case SP_FLEE2: val = sd->battle_status.flee2; break;
  6756. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  6757. case SP_MAXHPRATE: val = sd->hprate; break;
  6758. case SP_MAXSPRATE: val = sd->sprate; break;
  6759. case SP_SPRATE: val = sd->dsprate; break;
  6760. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  6761. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  6762. case SP_ASPD_RATE:
  6763. #ifndef RENEWAL_ASPD
  6764. val = sd->battle_status.aspd_rate;
  6765. #else
  6766. val = sd->battle_status.aspd_rate2;
  6767. #endif
  6768. break;
  6769. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  6770. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  6771. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  6772. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  6773. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  6774. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  6775. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  6776. case SP_MATK_RATE: val = sd->matk_rate; break;
  6777. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  6778. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  6779. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  6780. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  6781. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  6782. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  6783. case SP_HIT_RATE: val = sd->hit_rate; break;
  6784. case SP_FLEE_RATE: val = sd->flee_rate; break;
  6785. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  6786. case SP_DEF_RATE: val = sd->def_rate; break;
  6787. case SP_DEF2_RATE: val = sd->def2_rate; break;
  6788. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  6789. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  6790. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  6791. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  6792. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  6793. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  6794. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  6795. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  6796. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  6797. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  6798. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  6799. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  6800. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  6801. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  6802. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  6803. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  6804. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  6805. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  6806. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  6807. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  6808. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  6809. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  6810. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  6811. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  6812. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  6813. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  6814. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  6815. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  6816. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  6817. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  6818. case SP_DELAYRATE: val = sd->delayrate; break;
  6819. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  6820. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  6821. case SP_UNSTRIPABLE:
  6822. case SP_UNSTRIPABLE_ARMOR:
  6823. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  6824. break;
  6825. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  6826. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  6827. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  6828. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  6829. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  6830. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  6831. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  6832. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  6833. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  6834. case SP_EMATK: val = sd->bonus.ematk; break;
  6835. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  6836. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  6837. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  6838. case SP_CASTRATE:
  6839. case SP_VARCASTRATE:
  6840. #ifdef RENEWAL_CAST
  6841. val = sd->bonus.varcastrate; break;
  6842. #else
  6843. val = sd->castrate; break;
  6844. #endif
  6845. }
  6846. return val;
  6847. }
  6848. /*==========================================
  6849. * script set pc status registry
  6850. *------------------------------------------*/
  6851. bool pc_setparam(struct map_session_data *sd,int type,int val)
  6852. {
  6853. nullpo_retr(false,sd);
  6854. switch(type){
  6855. case SP_BASELEVEL:
  6856. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  6857. val = pc_maxbaselv(sd);
  6858. if ((unsigned int)val > sd->status.base_level) {
  6859. int i = 0;
  6860. int stat=0;
  6861. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  6862. stat += pc_gets_status_point(sd->status.base_level + i);
  6863. sd->status.status_point += stat;
  6864. }
  6865. sd->status.base_level = (unsigned int)val;
  6866. sd->status.base_exp = 0;
  6867. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  6868. clif_updatestatus(sd, SP_NEXTBASEEXP);
  6869. clif_updatestatus(sd, SP_STATUSPOINT);
  6870. clif_updatestatus(sd, SP_BASEEXP);
  6871. status_calc_pc(sd, SCO_FORCE);
  6872. if(sd->status.party_id)
  6873. party_send_levelup(sd);
  6874. break;
  6875. case SP_JOBLEVEL:
  6876. if ((unsigned int)val >= sd->status.job_level) {
  6877. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  6878. sd->status.skill_point += val - sd->status.job_level;
  6879. clif_updatestatus(sd, SP_SKILLPOINT);
  6880. }
  6881. sd->status.job_level = (unsigned int)val;
  6882. sd->status.job_exp = 0;
  6883. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  6884. clif_updatestatus(sd, SP_NEXTJOBEXP);
  6885. clif_updatestatus(sd, SP_JOBEXP);
  6886. status_calc_pc(sd, SCO_FORCE);
  6887. break;
  6888. case SP_SKILLPOINT:
  6889. sd->status.skill_point = val;
  6890. break;
  6891. case SP_STATUSPOINT:
  6892. sd->status.status_point = val;
  6893. break;
  6894. case SP_ZENY:
  6895. if( val < 0 )
  6896. return false;// can't set negative zeny
  6897. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  6898. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  6899. break;
  6900. case SP_BASEEXP:
  6901. if(pc_nextbaseexp(sd) > 0) {
  6902. sd->status.base_exp = val;
  6903. pc_checkbaselevelup(sd);
  6904. }
  6905. break;
  6906. case SP_JOBEXP:
  6907. if(pc_nextjobexp(sd) > 0) {
  6908. sd->status.job_exp = val;
  6909. pc_checkjoblevelup(sd);
  6910. }
  6911. break;
  6912. case SP_SEX:
  6913. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  6914. break;
  6915. case SP_WEIGHT:
  6916. sd->weight = val;
  6917. break;
  6918. case SP_MAXWEIGHT:
  6919. sd->max_weight = val;
  6920. break;
  6921. case SP_HP:
  6922. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  6923. break;
  6924. case SP_MAXHP:
  6925. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  6926. if( sd->battle_status.max_hp < sd->battle_status.hp )
  6927. {
  6928. sd->battle_status.hp = sd->battle_status.max_hp;
  6929. clif_updatestatus(sd, SP_HP);
  6930. }
  6931. break;
  6932. case SP_SP:
  6933. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  6934. break;
  6935. case SP_MAXSP:
  6936. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  6937. if( sd->battle_status.max_sp < sd->battle_status.sp )
  6938. {
  6939. sd->battle_status.sp = sd->battle_status.max_sp;
  6940. clif_updatestatus(sd, SP_SP);
  6941. }
  6942. break;
  6943. case SP_STR:
  6944. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  6945. break;
  6946. case SP_AGI:
  6947. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  6948. break;
  6949. case SP_VIT:
  6950. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  6951. break;
  6952. case SP_INT:
  6953. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  6954. break;
  6955. case SP_DEX:
  6956. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  6957. break;
  6958. case SP_LUK:
  6959. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  6960. break;
  6961. case SP_KARMA:
  6962. sd->status.karma = val;
  6963. break;
  6964. case SP_MANNER:
  6965. sd->status.manner = val;
  6966. if( val < 0 )
  6967. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  6968. else {
  6969. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  6970. clif_manner_message(sd, 5);
  6971. }
  6972. return true; // status_change_start/status_change_end already sends packets warning the client
  6973. case SP_FAME:
  6974. sd->status.fame = val;
  6975. break;
  6976. case SP_KILLERRID:
  6977. sd->killerrid = val;
  6978. return true;
  6979. case SP_KILLEDRID:
  6980. sd->killedrid = val;
  6981. return true;
  6982. case SP_CHARMOVE:
  6983. sd->status.character_moves = val;
  6984. return true;
  6985. case SP_CHARRENAME:
  6986. sd->status.rename = val;
  6987. return true;
  6988. case SP_CHARFONT:
  6989. sd->status.font = val;
  6990. clif_font(sd);
  6991. return true;
  6992. case SP_BANK_VAULT:
  6993. if (val < 0)
  6994. return false;
  6995. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  6996. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  6997. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  6998. return true;
  6999. case SP_ROULETTE_BRONZE:
  7000. sd->roulette_point.bronze = val;
  7001. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7002. return true;
  7003. case SP_ROULETTE_SILVER:
  7004. sd->roulette_point.silver = val;
  7005. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7006. return true;
  7007. case SP_ROULETTE_GOLD:
  7008. sd->roulette_point.gold = val;
  7009. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7010. return true;
  7011. default:
  7012. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7013. return false;
  7014. }
  7015. clif_updatestatus(sd,type);
  7016. return true;
  7017. }
  7018. /*==========================================
  7019. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7020. *------------------------------------------*/
  7021. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7022. {
  7023. if (type) {
  7024. if (hp)
  7025. clif_heal(sd->fd,SP_HP,hp);
  7026. if (sp)
  7027. clif_heal(sd->fd,SP_SP,sp);
  7028. } else {
  7029. if(hp)
  7030. clif_updatestatus(sd,SP_HP);
  7031. if(sp)
  7032. clif_updatestatus(sd,SP_SP);
  7033. }
  7034. return;
  7035. }
  7036. /*==========================================
  7037. * HP/SP Recovery
  7038. * Heal player hp and/or sp linearly.
  7039. * Calculate bonus by status.
  7040. *------------------------------------------*/
  7041. int pc_itemheal(struct map_session_data *sd,int itemid, int hp,int sp)
  7042. {
  7043. int bonus, tmp;
  7044. if(hp) {
  7045. int i;
  7046. bonus = 100 + (sd->battle_status.vit<<1)
  7047. + pc_checkskill(sd,SM_RECOVERY)*10
  7048. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  7049. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7050. if (potion_flag > 1)
  7051. bonus += bonus*(potion_flag-1)*50/100;
  7052. //All item bonuses.
  7053. bonus += sd->bonus.itemhealrate2;
  7054. //Item Group bonuses
  7055. bonus += bonus*pc_get_itemgroup_bonus(sd, itemid)/100;
  7056. //Individual item bonuses.
  7057. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++)
  7058. {
  7059. if (sd->itemhealrate[i].nameid == itemid) {
  7060. bonus += bonus*sd->itemhealrate[i].rate/100;
  7061. break;
  7062. }
  7063. }
  7064. tmp = hp * bonus / 100; // overflow check
  7065. if(bonus != 100 && tmp > hp)
  7066. hp = tmp;
  7067. // Recovery Potion
  7068. if( sd->sc.data[SC_INCHEALRATE] )
  7069. hp += (int)(hp * sd->sc.data[SC_INCHEALRATE]->val1/100.);
  7070. // 2014 Halloween Event : Pumpkin Bonus
  7071. if(sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7072. hp += (int)(hp * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100.);
  7073. }
  7074. if(sp) {
  7075. bonus = 100 + (sd->battle_status.int_<<1)
  7076. + pc_checkskill(sd,MG_SRECOVERY)*10
  7077. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  7078. if (potion_flag > 1)
  7079. bonus += bonus*(potion_flag-1)*50/100;
  7080. tmp = sp * bonus / 100;
  7081. if(bonus != 100 && tmp > sp)
  7082. sp = tmp;
  7083. }
  7084. if( sd->sc.count ) {
  7085. if ( sd->sc.data[SC_CRITICALWOUND] ) {
  7086. hp -= hp * sd->sc.data[SC_CRITICALWOUND]->val2 / 100;
  7087. sp -= sp * sd->sc.data[SC_CRITICALWOUND]->val2 / 100;
  7088. }
  7089. if ( sd->sc.data[SC_DEATHHURT] ) {
  7090. hp -= hp * 20 / 100;
  7091. sp -= sp * 20 / 100;
  7092. }
  7093. if( sd->sc.data[SC_VITALITYACTIVATION] ){
  7094. hp += hp / 2; // 1.5 times
  7095. sp -= sp / 2;
  7096. }
  7097. if( sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2 ) {
  7098. hp += hp / 10;
  7099. sp += sp / 10;
  7100. }
  7101. #ifdef RENEWAL
  7102. if( sd->sc.data[SC_EXTREMITYFIST2] )
  7103. sp = 0;
  7104. #endif
  7105. }
  7106. return status_heal(&sd->bl, hp, sp, 1);
  7107. }
  7108. /*==========================================
  7109. * HP/SP Recovery
  7110. * Heal player hp nad/or sp by rate
  7111. *------------------------------------------*/
  7112. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7113. {
  7114. nullpo_ret(sd);
  7115. if (hp > 100) hp = 100;
  7116. else if (hp <-100) hp = -100;
  7117. if (sp > 100) sp = 100;
  7118. else if (sp <-100) sp = -100;
  7119. if(hp >= 0 && sp >= 0) //Heal
  7120. return status_percent_heal(&sd->bl, hp, sp);
  7121. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7122. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7123. //Crossed signs
  7124. if(hp) {
  7125. if(hp > 0)
  7126. status_percent_heal(&sd->bl, hp, 0);
  7127. else
  7128. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7129. }
  7130. if(sp) {
  7131. if(sp > 0)
  7132. status_percent_heal(&sd->bl, 0, sp);
  7133. else
  7134. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7135. }
  7136. return 0;
  7137. }
  7138. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7139. {
  7140. struct mob_data *md;
  7141. int flag;
  7142. md = (struct mob_data *)bl;
  7143. nullpo_ret(md);
  7144. flag = va_arg(ap, int);
  7145. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7146. status_kill(&md->bl);
  7147. return 1;
  7148. }
  7149. /**
  7150. * Called when player changes job
  7151. * Rewrote to make it tidider [Celest]
  7152. * @param sd
  7153. * @param job JOB ID. See enum e_job
  7154. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7155. * @return True if success, false if failed
  7156. **/
  7157. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7158. {
  7159. int i, fame_flag = 0;
  7160. int b_class;
  7161. nullpo_retr(false,sd);
  7162. if (job < 0)
  7163. return false;
  7164. //Normalize job.
  7165. b_class = pc_jobid2mapid(job);
  7166. if (b_class == -1)
  7167. return false;
  7168. switch (upper) {
  7169. case 1:
  7170. b_class|= JOBL_UPPER;
  7171. break;
  7172. case 2:
  7173. b_class|= JOBL_BABY;
  7174. break;
  7175. }
  7176. //This will automatically adjust bard/dancer classes to the correct gender
  7177. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7178. job = pc_mapid2jobid(b_class, sd->status.sex);
  7179. if (job == -1)
  7180. return false;
  7181. if ((unsigned short)b_class == sd->class_)
  7182. return false; //Nothing to change.
  7183. // changing from 1st to 2nd job
  7184. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7185. sd->change_level_2nd = sd->status.job_level;
  7186. pc_setglobalreg (sd, "jobchange_level", sd->change_level_2nd);
  7187. }
  7188. // changing from 2nd to 3rd job
  7189. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7190. sd->change_level_3rd = sd->status.job_level;
  7191. pc_setglobalreg (sd, "jobchange_level_3rd", sd->change_level_3rd);
  7192. }
  7193. if(sd->cloneskill_idx > 0) {
  7194. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7195. sd->status.skill[sd->cloneskill_idx].id = 0;
  7196. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7197. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7198. clif_deleteskill(sd,pc_readglobalreg(sd,SKILL_VAR_PLAGIARISM));
  7199. }
  7200. sd->cloneskill_idx = 0;
  7201. pc_setglobalreg(sd,SKILL_VAR_PLAGIARISM, 0);
  7202. pc_setglobalreg(sd,SKILL_VAR_PLAGIARISM_LV, 0);
  7203. }
  7204. if(sd->reproduceskill_idx > 0) {
  7205. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7206. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7207. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7208. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7209. clif_deleteskill(sd,pc_readglobalreg(sd,SKILL_VAR_REPRODUCE));
  7210. }
  7211. sd->reproduceskill_idx = 0;
  7212. pc_setglobalreg(sd,SKILL_VAR_REPRODUCE,0);
  7213. pc_setglobalreg(sd,SKILL_VAR_REPRODUCE_LV,0);
  7214. }
  7215. // Give or reduce transcendent status points
  7216. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7217. sd->status.status_point += battle_config.transcendent_status_points;
  7218. clif_updatestatus(sd,SP_STATUSPOINT);
  7219. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7220. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7221. // The player already used his bonus points, so we have to reset his status points
  7222. pc_resetstate(sd);
  7223. }
  7224. sd->status.status_point -= battle_config.transcendent_status_points;
  7225. clif_updatestatus(sd,SP_STATUSPOINT);
  7226. }
  7227. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7228. const int class_ = pc_class2idx(sd->status.class_);
  7229. short id;
  7230. for(i = 0; i < MAX_SKILL_TREE && (id = skill_tree[class_][i].id) > 0; i++) {
  7231. //Remove status specific to your current tree skills.
  7232. enum sc_type sc = status_skill2sc(id);
  7233. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7234. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7235. }
  7236. }
  7237. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7238. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7239. pc_resetfeel(sd);
  7240. }
  7241. sd->status.class_ = job;
  7242. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7243. sd->class_ = (unsigned short)b_class;
  7244. sd->status.job_level=1;
  7245. sd->status.job_exp=0;
  7246. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7247. sd->status.base_level = pc_maxbaselv(sd);
  7248. sd->status.base_exp=0;
  7249. pc_resetstate(sd);
  7250. clif_updatestatus(sd,SP_STATUSPOINT);
  7251. clif_updatestatus(sd,SP_BASELEVEL);
  7252. clif_updatestatus(sd,SP_BASEEXP);
  7253. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7254. }
  7255. clif_updatestatus(sd,SP_JOBLEVEL);
  7256. clif_updatestatus(sd,SP_JOBEXP);
  7257. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7258. for(i=0;i<EQI_MAX;i++) {
  7259. if(sd->equip_index[i] >= 0)
  7260. if(pc_isequip(sd,sd->equip_index[i]))
  7261. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7262. }
  7263. //Change look, if disguised, you need to undisguise
  7264. //to correctly calculate new job sprite without
  7265. if (sd->disguise)
  7266. pc_disguise(sd, 0);
  7267. status_set_viewdata(&sd->bl, job);
  7268. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7269. if(sd->vd.cloth_color)
  7270. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7271. //Update skill tree.
  7272. pc_calc_skilltree(sd);
  7273. clif_skillinfoblock(sd);
  7274. if (sd->ed)
  7275. elemental_delete(sd->ed, 0);
  7276. if (sd->state.vending)
  7277. vending_closevending(sd);
  7278. if (sd->state.buyingstore)
  7279. buyingstore_close(sd);
  7280. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7281. //Remove peco/cart/falcon
  7282. i = sd->sc.option;
  7283. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7284. i&=~OPTION_RIDING;
  7285. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7286. i&=~OPTION_FALCON;
  7287. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7288. i&=~OPTION_DRAGON;
  7289. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7290. i&=~OPTION_WUGRIDER;
  7291. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7292. i&=~OPTION_WUG;
  7293. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7294. i&=~OPTION_MADOGEAR;
  7295. #ifndef NEW_CARTS
  7296. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7297. i&=~OPTION_CART;
  7298. #else
  7299. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7300. pc_setcart(sd, 0);
  7301. #endif
  7302. if(i != sd->sc.option)
  7303. pc_setoption(sd, i);
  7304. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7305. hom_vaporize(sd, HOM_ST_ACTIVE);
  7306. if(sd->status.manner < 0)
  7307. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7308. status_calc_pc(sd,SCO_FORCE);
  7309. pc_checkallowskill(sd);
  7310. pc_equiplookall(sd);
  7311. //if you were previously famous, not anymore.
  7312. if (fame_flag) {
  7313. chrif_save(sd,0);
  7314. chrif_buildfamelist();
  7315. } else if (sd->status.fame > 0) {
  7316. //It may be that now they are famous?
  7317. switch (sd->class_&MAPID_UPPERMASK) {
  7318. case MAPID_BLACKSMITH:
  7319. case MAPID_ALCHEMIST:
  7320. case MAPID_TAEKWON:
  7321. chrif_save(sd,0);
  7322. chrif_buildfamelist();
  7323. break;
  7324. }
  7325. }
  7326. return true;
  7327. }
  7328. /*==========================================
  7329. * Tell client player sd has change equipement
  7330. *------------------------------------------*/
  7331. void pc_equiplookall(struct map_session_data *sd)
  7332. {
  7333. nullpo_retv(sd);
  7334. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  7335. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7336. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7337. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7338. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7339. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  7340. }
  7341. /*==========================================
  7342. * Tell client player sd has change look (hair,equip...)
  7343. *------------------------------------------*/
  7344. void pc_changelook(struct map_session_data *sd,int type,int val)
  7345. {
  7346. nullpo_retv(sd);
  7347. switch(type){
  7348. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  7349. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  7350. if (sd->status.hair != val)
  7351. {
  7352. sd->status.hair=val;
  7353. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7354. intif_guild_change_memberinfo(sd->status.guild_id,sd->status.account_id,sd->status.char_id,
  7355. GMI_HAIR,&sd->status.hair,sizeof(sd->status.hair));
  7356. }
  7357. break;
  7358. case LOOK_WEAPON:
  7359. sd->status.weapon=val;
  7360. break;
  7361. case LOOK_HEAD_BOTTOM:
  7362. sd->status.head_bottom=val;
  7363. break;
  7364. case LOOK_HEAD_TOP:
  7365. sd->status.head_top=val;
  7366. break;
  7367. case LOOK_HEAD_MID:
  7368. sd->status.head_mid=val;
  7369. break;
  7370. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  7371. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  7372. if (sd->status.hair_color != val) {
  7373. sd->status.hair_color=val;
  7374. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7375. intif_guild_change_memberinfo(sd->status.guild_id,sd->status.account_id,sd->status.char_id,
  7376. GMI_HAIR_COLOR,&sd->status.hair_color,sizeof(sd->status.hair_color));
  7377. }
  7378. break;
  7379. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  7380. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  7381. sd->status.clothes_color=val;
  7382. break;
  7383. case LOOK_SHIELD:
  7384. sd->status.shield=val;
  7385. break;
  7386. case LOOK_SHOES:
  7387. break;
  7388. case LOOK_ROBE:
  7389. sd->status.robe = val;
  7390. break;
  7391. }
  7392. clif_changelook(&sd->bl,type,val);
  7393. }
  7394. /*==========================================
  7395. * Give an option (type) to player (sd) and display it to client
  7396. *------------------------------------------*/
  7397. void pc_setoption(struct map_session_data *sd,int type)
  7398. {
  7399. int p_type, new_look=0;
  7400. nullpo_retv(sd);
  7401. p_type = sd->sc.option;
  7402. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  7403. sd->sc.option=type;
  7404. clif_changeoption(&sd->bl);
  7405. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7406. { // Mounting
  7407. clif_status_load(&sd->bl,SI_RIDING,1);
  7408. status_calc_pc(sd,SCO_NONE);
  7409. }
  7410. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7411. { // Dismount
  7412. clif_status_load(&sd->bl,SI_RIDING,0);
  7413. status_calc_pc(sd,SCO_NONE);
  7414. }
  7415. #ifndef NEW_CARTS
  7416. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  7417. clif_cartlist(sd);
  7418. clif_updatestatus(sd, SP_CARTINFO);
  7419. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7420. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  7421. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  7422. clif_clearcart(sd->fd);
  7423. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7424. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  7425. }
  7426. #endif
  7427. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  7428. clif_status_load(&sd->bl,SI_FALCON,1);
  7429. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  7430. clif_status_load(&sd->bl,SI_FALCON,0);
  7431. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  7432. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  7433. clif_status_load(&sd->bl,SI_WUGRIDER,1);
  7434. status_calc_pc(sd,SCO_NONE);
  7435. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  7436. clif_status_load(&sd->bl,SI_WUGRIDER,0);
  7437. status_calc_pc(sd,SCO_NONE);
  7438. }
  7439. }
  7440. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  7441. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  7442. status_calc_pc(sd,SCO_NONE);
  7443. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  7444. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  7445. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  7446. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  7447. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  7448. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  7449. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  7450. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7451. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  7452. status_calc_pc(sd,SCO_NONE);
  7453. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  7454. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  7455. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  7456. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  7457. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  7458. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7459. }
  7460. }
  7461. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  7462. new_look = JOB_STAR_GLADIATOR2;
  7463. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  7464. new_look = -1;
  7465. if (sd->disguise || !new_look)
  7466. return; //Disguises break sprite changes
  7467. if (new_look < 0) { //Restore normal look.
  7468. status_set_viewdata(&sd->bl, sd->status.class_);
  7469. new_look = sd->vd.class_;
  7470. }
  7471. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  7472. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  7473. if (sd->vd.cloth_color)
  7474. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7475. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  7476. }
  7477. /*==========================================
  7478. * Give player a cart
  7479. *------------------------------------------*/
  7480. bool pc_setcart(struct map_session_data *sd,int type) {
  7481. #ifndef NEW_CARTS
  7482. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  7483. int option;
  7484. #endif
  7485. nullpo_retr(false,sd);
  7486. if( type < 0 || type > MAX_CARTS )
  7487. return false;// Never trust the values sent by the client! [Skotlex]
  7488. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  7489. return false;// Push cart is required
  7490. #ifdef NEW_CARTS
  7491. switch( type ) {
  7492. case 0:
  7493. if( !sd->sc.data[SC_PUSH_CART] )
  7494. return 0;
  7495. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  7496. clif_clearcart(sd->fd);
  7497. break;
  7498. default:/* everything else is an allowed ID so we can move on */
  7499. if( !sd->sc.data[SC_PUSH_CART] ) /* first time, so fill cart data */
  7500. clif_cartlist(sd);
  7501. clif_updatestatus(sd, SP_CARTINFO);
  7502. sc_start(&sd->bl,&sd->bl, SC_PUSH_CART, 100, type, 0);
  7503. clif_status_change2(&sd->bl, sd->bl.id, AREA, SI_ON_PUSH_CART, type, 0, 0);
  7504. if( sd->sc.data[SC_PUSH_CART] )/* forcefully update */
  7505. sd->sc.data[SC_PUSH_CART]->val1 = type;
  7506. break;
  7507. }
  7508. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7509. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  7510. #else
  7511. // Update option
  7512. option = sd->sc.option;
  7513. option &= ~OPTION_CART;// clear cart bits
  7514. option |= cart[type]; // set cart
  7515. pc_setoption(sd, option);
  7516. #endif
  7517. return true;
  7518. }
  7519. /*==========================================
  7520. * Give player a falcon
  7521. *------------------------------------------*/
  7522. void pc_setfalcon(TBL_PC* sd, int flag)
  7523. {
  7524. if( flag ){
  7525. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  7526. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  7527. } else if( pc_isfalcon(sd) ){
  7528. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  7529. }
  7530. }
  7531. /*==========================================
  7532. * Set player riding
  7533. *------------------------------------------*/
  7534. void pc_setriding(TBL_PC* sd, int flag)
  7535. {
  7536. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  7537. return;
  7538. if( flag ){
  7539. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  7540. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  7541. } else if( pc_isriding(sd) ){
  7542. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  7543. }
  7544. }
  7545. /*==========================================
  7546. * Give player a mado
  7547. *------------------------------------------*/
  7548. void pc_setmadogear(TBL_PC* sd, int flag)
  7549. {
  7550. if( flag ){
  7551. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  7552. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  7553. } else if( pc_ismadogear(sd) ){
  7554. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  7555. }
  7556. }
  7557. /*==========================================
  7558. * Check if player can drop an item
  7559. *------------------------------------------*/
  7560. bool pc_candrop(struct map_session_data *sd, struct item *item)
  7561. {
  7562. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  7563. return false;
  7564. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  7565. return false;
  7566. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  7567. }
  7568. /**
  7569. * Determines whether a player can attack based on status changes
  7570. * Why not use status_check_skilluse?
  7571. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  7572. * Even ground-based attacks should be blocked by these statuses
  7573. * Called from unit_attack and unit_attack_timer_sub
  7574. * @retval true Can attack
  7575. **/
  7576. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  7577. nullpo_retr(false, sd);
  7578. if (!&sd->sc)
  7579. return true;
  7580. if( sd->sc.data[SC_BASILICA] ||
  7581. sd->sc.data[SC__SHADOWFORM] ||
  7582. sd->sc.data[SC__MANHOLE] ||
  7583. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  7584. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  7585. sd->sc.data[SC_CRYSTALIZE] ||
  7586. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  7587. sd->sc.data[SC_TRICKDEAD] ||
  7588. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  7589. sd->sc.data[SC_BLADESTOP] ||
  7590. sd->sc.data[SC_DEEPSLEEP] ||
  7591. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) )
  7592. return false;
  7593. return true;
  7594. }
  7595. /*==========================================
  7596. * Read ram register for player sd
  7597. * get val (int) from reg for player sd
  7598. *------------------------------------------*/
  7599. int pc_readreg(struct map_session_data* sd, int reg)
  7600. {
  7601. int i;
  7602. nullpo_ret(sd);
  7603. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].index == reg );
  7604. return ( i < sd->reg_num ) ? sd->reg[i].data : 0;
  7605. }
  7606. /*==========================================
  7607. * Set ram register for player sd
  7608. * memo val(int) at reg for player sd
  7609. *------------------------------------------*/
  7610. bool pc_setreg(struct map_session_data* sd, int reg, int val)
  7611. {
  7612. int i;
  7613. nullpo_retr(false,sd);
  7614. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].index == reg );
  7615. if( i < sd->reg_num )
  7616. {// overwrite existing entry
  7617. sd->reg[i].data = val;
  7618. return true;
  7619. }
  7620. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].data == 0 );
  7621. if( i == sd->reg_num )
  7622. {// nothing free, increase size
  7623. sd->reg_num++;
  7624. RECREATE(sd->reg, struct script_reg, sd->reg_num);
  7625. }
  7626. sd->reg[i].index = reg;
  7627. sd->reg[i].data = val;
  7628. return true;
  7629. }
  7630. /*==========================================
  7631. * Read ram register for player sd
  7632. * get val (str) from reg for player sd
  7633. *------------------------------------------*/
  7634. char* pc_readregstr(struct map_session_data* sd, int reg)
  7635. {
  7636. int i;
  7637. nullpo_ret(sd);
  7638. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].index == reg );
  7639. return ( i < sd->regstr_num ) ? sd->regstr[i].data : NULL;
  7640. }
  7641. /*==========================================
  7642. * Set ram register for player sd
  7643. * memo val(str) at reg for player sd
  7644. *------------------------------------------*/
  7645. bool pc_setregstr(struct map_session_data* sd, int reg, const char* str)
  7646. {
  7647. int i;
  7648. nullpo_retr(false,sd);
  7649. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].index == reg );
  7650. if( i < sd->regstr_num )
  7651. {// found entry, update
  7652. if( str == NULL || *str == '\0' )
  7653. {// empty string
  7654. if( sd->regstr[i].data != NULL )
  7655. aFree(sd->regstr[i].data);
  7656. sd->regstr[i].data = NULL;
  7657. }
  7658. else if( sd->regstr[i].data )
  7659. {// recreate
  7660. size_t len = strlen(str)+1;
  7661. RECREATE(sd->regstr[i].data, char, len);
  7662. memcpy(sd->regstr[i].data, str, len*sizeof(char));
  7663. }
  7664. else
  7665. {// create
  7666. sd->regstr[i].data = aStrdup(str);
  7667. }
  7668. return true;
  7669. }
  7670. if( str == NULL || *str == '\0' )
  7671. return true;// nothing to add, empty string
  7672. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].data == NULL );
  7673. if( i == sd->regstr_num )
  7674. {// nothing free, increase size
  7675. sd->regstr_num++;
  7676. RECREATE(sd->regstr, struct script_regstr, sd->regstr_num);
  7677. }
  7678. sd->regstr[i].index = reg;
  7679. sd->regstr[i].data = aStrdup(str);
  7680. return true;
  7681. }
  7682. int pc_readregistry(struct map_session_data *sd,const char *reg,int type)
  7683. {
  7684. struct global_reg *sd_reg;
  7685. int i,max;
  7686. nullpo_ret(sd);
  7687. switch (type) {
  7688. case 3: //Char reg
  7689. sd_reg = sd->save_reg.global;
  7690. max = sd->save_reg.global_num;
  7691. break;
  7692. case 2: //Account reg
  7693. sd_reg = sd->save_reg.account;
  7694. max = sd->save_reg.account_num;
  7695. break;
  7696. case 1: //Account2 reg
  7697. sd_reg = sd->save_reg.account2;
  7698. max = sd->save_reg.account2_num;
  7699. break;
  7700. default:
  7701. return 0;
  7702. }
  7703. if (max == -1) {
  7704. ShowError("pc_readregistry: Trying to read reg value %s (type %d) before it's been loaded!\n", reg, type);
  7705. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7706. intif_request_registry(sd,type==3?4:type);
  7707. return 0;
  7708. }
  7709. ARR_FIND( 0, max, i, strcmp(sd_reg[i].str,reg) == 0 );
  7710. return ( i < max ) ? atoi(sd_reg[i].value) : 0;
  7711. }
  7712. char* pc_readregistry_str(struct map_session_data *sd,const char *reg,int type)
  7713. {
  7714. struct global_reg *sd_reg;
  7715. int i,max;
  7716. nullpo_ret(sd);
  7717. switch (type) {
  7718. case 3: //Char reg
  7719. sd_reg = sd->save_reg.global;
  7720. max = sd->save_reg.global_num;
  7721. break;
  7722. case 2: //Account reg
  7723. sd_reg = sd->save_reg.account;
  7724. max = sd->save_reg.account_num;
  7725. break;
  7726. case 1: //Account2 reg
  7727. sd_reg = sd->save_reg.account2;
  7728. max = sd->save_reg.account2_num;
  7729. break;
  7730. default:
  7731. return NULL;
  7732. }
  7733. if (max == -1) {
  7734. ShowError("pc_readregistry: Trying to read reg value %s (type %d) before it's been loaded!\n", reg, type);
  7735. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7736. intif_request_registry(sd,type==3?4:type);
  7737. return NULL;
  7738. }
  7739. ARR_FIND( 0, max, i, strcmp(sd_reg[i].str,reg) == 0 );
  7740. return ( i < max ) ? sd_reg[i].value : NULL;
  7741. }
  7742. bool pc_setregistry(struct map_session_data *sd,const char *reg,int val,int type)
  7743. {
  7744. struct global_reg *sd_reg;
  7745. int i,*max, regmax;
  7746. nullpo_retr(false,sd);
  7747. switch( type )
  7748. {
  7749. case 3: //Char reg
  7750. if( !strcmp(reg,"PC_DIE_COUNTER") && sd->die_counter != val ) {
  7751. i = (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE);
  7752. sd->die_counter = val;
  7753. if( i )
  7754. status_calc_pc(sd,SCO_NONE); // Lost the bonus.
  7755. } else if( !strcmp(reg,"COOK_MASTERY") && sd->cook_mastery != val ) {
  7756. val = cap_value(val, 0, 1999);
  7757. sd->cook_mastery = val;
  7758. }
  7759. sd_reg = sd->save_reg.global;
  7760. max = &sd->save_reg.global_num;
  7761. regmax = GLOBAL_REG_NUM;
  7762. break;
  7763. case 2: //Account reg
  7764. if( !strcmp(reg,"#CASHPOINTS") && sd->cashPoints != val ) {
  7765. val = cap_value(val, 0, MAX_ZENY);
  7766. sd->cashPoints = val;
  7767. } else if( !strcmp(reg,"#KAFRAPOINTS") && sd->kafraPoints != val ) {
  7768. val = cap_value(val, 0, MAX_ZENY);
  7769. sd->kafraPoints = val;
  7770. }
  7771. sd_reg = sd->save_reg.account;
  7772. max = &sd->save_reg.account_num;
  7773. regmax = ACCOUNT_REG_NUM;
  7774. break;
  7775. case 1: //Account2 reg
  7776. sd_reg = sd->save_reg.account2;
  7777. max = &sd->save_reg.account2_num;
  7778. regmax = ACCOUNT_REG2_NUM;
  7779. break;
  7780. default:
  7781. return false;
  7782. }
  7783. if (*max == -1) {
  7784. ShowError("pc_setregistry : refusing to set %s (type %d) until vars are received.\n", reg, type);
  7785. return true;
  7786. }
  7787. // delete reg
  7788. if (val == 0) {
  7789. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7790. if( i < *max )
  7791. {
  7792. if (i != *max - 1)
  7793. memcpy(&sd_reg[i], &sd_reg[*max - 1], sizeof(struct global_reg));
  7794. memset(&sd_reg[*max - 1], 0, sizeof(struct global_reg));
  7795. (*max)--;
  7796. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7797. }
  7798. return true;
  7799. }
  7800. // change value if found
  7801. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7802. if( i < *max )
  7803. {
  7804. safesnprintf(sd_reg[i].value, sizeof(sd_reg[i].value), "%d", val);
  7805. sd->state.reg_dirty |= 1<<(type-1);
  7806. return true;
  7807. }
  7808. // add value if not found
  7809. if (i < regmax) {
  7810. memset(&sd_reg[i], 0, sizeof(struct global_reg));
  7811. safestrncpy(sd_reg[i].str, reg, sizeof(sd_reg[i].str));
  7812. safesnprintf(sd_reg[i].value, sizeof(sd_reg[i].value), "%d", val);
  7813. (*max)++;
  7814. sd->state.reg_dirty |= 1<<(type-1);
  7815. return true;
  7816. }
  7817. ShowError("pc_setregistry : couldn't set %s, limit of registries reached (%d)\n", reg, regmax);
  7818. return false;
  7819. }
  7820. bool pc_setregistry_str(struct map_session_data *sd,const char *reg,const char *val,int type)
  7821. {
  7822. struct global_reg *sd_reg;
  7823. int i,*max, regmax;
  7824. nullpo_retr(false,sd);
  7825. if (reg[strlen(reg)-1] != '$') {
  7826. ShowError("pc_setregistry_str : reg %s must be string (end in '$') to use this!\n", reg);
  7827. return false;
  7828. }
  7829. switch (type) {
  7830. case 3: //Char reg
  7831. sd_reg = sd->save_reg.global;
  7832. max = &sd->save_reg.global_num;
  7833. regmax = GLOBAL_REG_NUM;
  7834. break;
  7835. case 2: //Account reg
  7836. sd_reg = sd->save_reg.account;
  7837. max = &sd->save_reg.account_num;
  7838. regmax = ACCOUNT_REG_NUM;
  7839. break;
  7840. case 1: //Account2 reg
  7841. sd_reg = sd->save_reg.account2;
  7842. max = &sd->save_reg.account2_num;
  7843. regmax = ACCOUNT_REG2_NUM;
  7844. break;
  7845. default:
  7846. return false;
  7847. }
  7848. if (*max == -1) {
  7849. ShowError("pc_setregistry_str : refusing to set %s (type %d) until vars are received.\n", reg, type);
  7850. return false;
  7851. }
  7852. // delete reg
  7853. if (!val || strcmp(val,"")==0)
  7854. {
  7855. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7856. if( i < *max )
  7857. {
  7858. if (i != *max - 1)
  7859. memcpy(&sd_reg[i], &sd_reg[*max - 1], sizeof(struct global_reg));
  7860. memset(&sd_reg[*max - 1], 0, sizeof(struct global_reg));
  7861. (*max)--;
  7862. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7863. if (type!=3) intif_saveregistry(sd,type);
  7864. }
  7865. return true;
  7866. }
  7867. // change value if found
  7868. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7869. if( i < *max )
  7870. {
  7871. safestrncpy(sd_reg[i].value, val, sizeof(sd_reg[i].value));
  7872. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7873. if (type!=3) intif_saveregistry(sd,type);
  7874. return true;
  7875. }
  7876. // add value if not found
  7877. if (i < regmax) {
  7878. memset(&sd_reg[i], 0, sizeof(struct global_reg));
  7879. safestrncpy(sd_reg[i].str, reg, sizeof(sd_reg[i].str));
  7880. safestrncpy(sd_reg[i].value, val, sizeof(sd_reg[i].value));
  7881. (*max)++;
  7882. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7883. if (type!=3) intif_saveregistry(sd,type);
  7884. return true;
  7885. }
  7886. ShowError("pc_setregistry : couldn't set %s, limit of registries reached (%d)\n", reg, regmax);
  7887. return false;
  7888. }
  7889. /**
  7890. * Set value of player variable
  7891. * @param sd Player
  7892. * @param reg Variable name
  7893. * @param value
  7894. * @return True if success, false if failed.
  7895. **/
  7896. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  7897. char prefix = reg[0];
  7898. nullpo_retr(false, sd);
  7899. if (reg[strlen(reg)-1] == '$') {
  7900. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  7901. return false;
  7902. }
  7903. val = cap_value(val, 0, INT_MAX);
  7904. switch (prefix) {
  7905. case '.':
  7906. case '\'':
  7907. case '$':
  7908. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  7909. return false;
  7910. case '@':
  7911. return pc_setreg(sd, add_str(reg), val);
  7912. case '#':
  7913. return (reg[1] == '#') ? pc_setaccountreg2(sd, reg, val) : pc_setaccountreg(sd, reg, val);
  7914. default:
  7915. return pc_setglobalreg(sd, reg, val);
  7916. }
  7917. return false;
  7918. }
  7919. /**
  7920. * Get value of player variable
  7921. * @param sd Player
  7922. * @param reg Variable name
  7923. * @return Variable value or 0 if failed.
  7924. **/
  7925. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  7926. char prefix = reg[0];
  7927. nullpo_ret(sd);
  7928. if (reg[strlen(reg)-1] == '$') {
  7929. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  7930. return 0;
  7931. }
  7932. switch (prefix) {
  7933. case '.':
  7934. case '\'':
  7935. case '$':
  7936. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  7937. return 0;
  7938. case '@':
  7939. return pc_readreg(sd, add_str(reg));
  7940. case '#':
  7941. return (reg[1] == '#') ? pc_readaccountreg2(sd, reg) : pc_readaccountreg(sd, reg);
  7942. default:
  7943. return pc_readglobalreg(sd, reg);
  7944. }
  7945. return 0;
  7946. }
  7947. /*==========================================
  7948. * Exec eventtimer for player sd (retrieved from map_session (id))
  7949. *------------------------------------------*/
  7950. static int pc_eventtimer(int tid, unsigned int tick, int id, intptr_t data)
  7951. {
  7952. struct map_session_data *sd=map_id2sd(id);
  7953. char *p = (char *)data;
  7954. int i;
  7955. if(sd==NULL)
  7956. return 0;
  7957. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  7958. if( i < MAX_EVENTTIMER )
  7959. {
  7960. sd->eventtimer[i] = INVALID_TIMER;
  7961. sd->eventcount--;
  7962. npc_event(sd,p,0);
  7963. }
  7964. else
  7965. ShowError("pc_eventtimer: no such event timer\n");
  7966. if (p) aFree(p);
  7967. return 0;
  7968. }
  7969. /*==========================================
  7970. * Add eventtimer for player sd ?
  7971. *------------------------------------------*/
  7972. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  7973. {
  7974. int i;
  7975. nullpo_retr(false,sd);
  7976. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  7977. if( i == MAX_EVENTTIMER )
  7978. return false;
  7979. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  7980. sd->eventcount++;
  7981. return true;
  7982. }
  7983. /*==========================================
  7984. * Del eventtimer for player sd ?
  7985. *------------------------------------------*/
  7986. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  7987. {
  7988. char* p = NULL;
  7989. int i;
  7990. nullpo_retr(false,sd);
  7991. if (sd->eventcount == 0)
  7992. return false;
  7993. // find the named event timer
  7994. ARR_FIND( 0, MAX_EVENTTIMER, i,
  7995. sd->eventtimer[i] != INVALID_TIMER &&
  7996. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  7997. strcmp(p, name) == 0
  7998. );
  7999. if( i == MAX_EVENTTIMER )
  8000. return false; // not found
  8001. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8002. sd->eventtimer[i] = INVALID_TIMER;
  8003. if(sd->eventcount > 0)
  8004. sd->eventcount--;
  8005. aFree(p);
  8006. return true;
  8007. }
  8008. /*==========================================
  8009. * Update eventtimer count for player sd
  8010. *------------------------------------------*/
  8011. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8012. {
  8013. int i;
  8014. nullpo_retv(sd);
  8015. for(i=0;i<MAX_EVENTTIMER;i++)
  8016. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8017. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8018. addtick_timer(sd->eventtimer[i],tick);
  8019. break;
  8020. }
  8021. }
  8022. /*==========================================
  8023. * Remove all eventtimer for player sd
  8024. *------------------------------------------*/
  8025. void pc_cleareventtimer(struct map_session_data *sd)
  8026. {
  8027. int i;
  8028. nullpo_retv(sd);
  8029. if (sd->eventcount == 0)
  8030. return;
  8031. for(i=0;i<MAX_EVENTTIMER;i++){
  8032. if( sd->eventtimer[i] != INVALID_TIMER ){
  8033. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8034. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8035. sd->eventtimer[i] = INVALID_TIMER;
  8036. if(sd->eventcount > 0) //avoid looping to max val
  8037. sd->eventcount--;
  8038. if (p) aFree(p);
  8039. }
  8040. }
  8041. }
  8042. /**
  8043. * Called when an item with combo is worn
  8044. * @param *sd
  8045. * @param *data struct item_data
  8046. * @return success numbers of succeed combo
  8047. */
  8048. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8049. uint16 i;
  8050. int success = 0;
  8051. for( i = 0; i < data->combos_count; i++ ) {
  8052. struct itemchk {
  8053. int idx;
  8054. short card[MAX_SLOTS];
  8055. } *combo_idx;
  8056. int idx, j;
  8057. int nb_itemCombo;
  8058. unsigned int pos = 0;
  8059. /* ensure this isn't a duplicate combo */
  8060. if( sd->combos.bonus != NULL ) {
  8061. int x;
  8062. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8063. /* found a match, skip this combo */
  8064. if( x < sd->combos.count )
  8065. continue;
  8066. }
  8067. nb_itemCombo = data->combos[i]->count;
  8068. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8069. continue;
  8070. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8071. for(j=0; j < nb_itemCombo; j++){
  8072. combo_idx[j].idx=-1;
  8073. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8074. }
  8075. for( j = 0; j < nb_itemCombo; j++ ) {
  8076. uint16 id = data->combos[i]->nameid[j], k;
  8077. bool found = false;
  8078. for( k = 0; k < EQI_MAX; k++ ) {
  8079. short index = sd->equip_index[k];
  8080. if( index < 0 )
  8081. continue;
  8082. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8083. continue;
  8084. if (!sd->inventory_data[index] )
  8085. continue;
  8086. if ( itemdb_type(id) != IT_CARD ) {
  8087. if ( sd->inventory_data[index]->nameid != id )
  8088. continue;
  8089. if(j>0){ //check if this item not already used
  8090. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8091. uint8 z;
  8092. for (z = 0; z < nb_itemCombo-1; z++)
  8093. if(combo_idx[z].idx == index) //we already have that index recorded
  8094. do_continue=true;
  8095. if(do_continue)
  8096. continue;
  8097. }
  8098. combo_idx[j].idx = index;
  8099. pos |= sd->status.inventory[index].equip;
  8100. found = true;
  8101. break;
  8102. } else { //Cards
  8103. uint16 z;
  8104. if ( sd->inventory_data[index]->slot == 0 || itemdb_isspecial(sd->status.inventory[index].card[0]) )
  8105. continue;
  8106. for (z = 0; z < sd->inventory_data[index]->slot; z++) {
  8107. bool do_continue=false;
  8108. if (sd->status.inventory[index].card[z] != id)
  8109. continue;
  8110. if(j>0){
  8111. int c1, c2;
  8112. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8113. if(combo_idx[c1].idx == index){
  8114. for (c2 = 0; c2 < sd->inventory_data[index]->slot; c2++){
  8115. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8116. do_continue = true;
  8117. break;
  8118. }
  8119. }
  8120. }
  8121. }
  8122. }
  8123. if(do_continue)
  8124. continue;
  8125. combo_idx[j].idx = index;
  8126. combo_idx[j].card[z] = id;
  8127. pos |= sd->status.inventory[index].equip;
  8128. found = true;
  8129. break;
  8130. }
  8131. }
  8132. }
  8133. if( !found )
  8134. break;/* we haven't found all the ids for this combo, so we can return */
  8135. }
  8136. aFree(combo_idx);
  8137. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8138. if( j < nb_itemCombo )
  8139. continue;
  8140. /* we got here, means all items in the combo are matching */
  8141. idx = sd->combos.count;
  8142. if( sd->combos.bonus == NULL ) {
  8143. CREATE(sd->combos.bonus, struct script_code *, 1);
  8144. CREATE(sd->combos.id, unsigned short, 1);
  8145. CREATE(sd->combos.pos, unsigned int, 1);
  8146. sd->combos.count = 1;
  8147. } else {
  8148. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8149. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8150. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8151. }
  8152. /* we simply copy the pointer */
  8153. sd->combos.bonus[idx] = data->combos[i]->script;
  8154. /* save this combo's id */
  8155. sd->combos.id[idx] = data->combos[i]->id;
  8156. /* save pos of combo*/
  8157. sd->combos.pos[idx] = pos;
  8158. success++;
  8159. }
  8160. return success;
  8161. }
  8162. /**
  8163. * Called when an item with combo is removed
  8164. * @param *sd
  8165. * @param *data struct item_data
  8166. * @return retval numbers of removed combo
  8167. */
  8168. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8169. int i, retval = 0;
  8170. if( sd->combos.bonus == NULL )
  8171. return 0;/* nothing to do here, player has no combos */
  8172. for( i = 0; i < data->combos_count; i++ ) {
  8173. /* check if this combo exists in this user */
  8174. int x = 0, cursor = 0, j;
  8175. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8176. /* no match, skip this combo */
  8177. if(x >= sd->combos.count)
  8178. continue;
  8179. sd->combos.bonus[x] = NULL;
  8180. sd->combos.id[x] = 0;
  8181. sd->combos.pos[x] = 0;
  8182. retval++;
  8183. /* check if combo requirements still fit */
  8184. if( pc_checkcombo( sd, data ) )
  8185. continue;
  8186. /* move next value to empty slot */
  8187. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8188. if( sd->combos.bonus[j] == NULL )
  8189. continue;
  8190. if( cursor != j ) {
  8191. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8192. sd->combos.id[cursor] = sd->combos.id[j];
  8193. sd->combos.pos[cursor] = sd->combos.pos[j];
  8194. }
  8195. cursor++;
  8196. }
  8197. /* it's empty, we can clear all the memory */
  8198. if( (sd->combos.count = cursor) == 0 ) {
  8199. aFree(sd->combos.bonus);
  8200. aFree(sd->combos.id);
  8201. aFree(sd->combos.pos);
  8202. sd->combos.bonus = NULL;
  8203. sd->combos.id = NULL;
  8204. sd->combos.pos = NULL;
  8205. return retval; /* we also can return at this point for we have no more combos to check */
  8206. }
  8207. }
  8208. return retval;
  8209. }
  8210. /**
  8211. * Load combo data(s) of player
  8212. * @param *sd
  8213. * @return ret numbers of succeed combo
  8214. */
  8215. int pc_load_combo(struct map_session_data *sd) {
  8216. int i, ret = 0;
  8217. for( i = 0; i < EQI_MAX; i++ ) {
  8218. struct item_data *id = NULL;
  8219. short idx = sd->equip_index[i];
  8220. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8221. continue;
  8222. if( id->combos_count )
  8223. ret += pc_checkcombo(sd,id);
  8224. if(!itemdb_isspecial(sd->status.inventory[idx].card[0])) {
  8225. struct item_data *data;
  8226. int j;
  8227. for( j = 0; j < id->slot; j++ ) {
  8228. if (!sd->status.inventory[idx].card[j])
  8229. continue;
  8230. if ( ( data = itemdb_exists(sd->status.inventory[idx].card[j]) ) != NULL ) {
  8231. if( data->combos_count )
  8232. ret += pc_checkcombo(sd,data);
  8233. }
  8234. }
  8235. }
  8236. }
  8237. return ret;
  8238. }
  8239. /*==========================================
  8240. * Equip item on player sd at req_pos from inventory index n
  8241. * return: false - fail; true - success
  8242. *------------------------------------------*/
  8243. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos)
  8244. {
  8245. int i, pos, flag = 0, iflag;
  8246. struct item_data *id;
  8247. uint8 res = ITEM_EQUIP_ACK_OK;
  8248. nullpo_retr(false,sd);
  8249. if( n < 0 || n >= MAX_INVENTORY ) {
  8250. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8251. return false;
  8252. }
  8253. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8254. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8255. return false;
  8256. }
  8257. if (!(id = sd->inventory_data[n]))
  8258. return false;
  8259. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8260. if(battle_config.battle_log)
  8261. ShowInfo("equip %hu(%d) %x:%x\n",sd->status.inventory[n].nameid,n,id?id->equip:0,req_pos);
  8262. if((res = pc_isequip(sd,n))) {
  8263. clif_equipitemack(sd,n,0,res); // fail
  8264. return false;
  8265. }
  8266. if (!(pos&req_pos) || sd->status.inventory[n].equip != 0 || sd->status.inventory[n].attribute==1 ) { // [Valaris]
  8267. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8268. return false;
  8269. }
  8270. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8271. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8272. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8273. return false;
  8274. }
  8275. if (id->flag.bindOnEquip && !sd->status.inventory[n].bound) {
  8276. sd->status.inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8277. clif_notify_bindOnEquip(sd,n);
  8278. }
  8279. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  8280. pos = req_pos&EQP_ACC;
  8281. if (pos == EQP_ACC) //User specified both slots..
  8282. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8283. }
  8284. if(pos == EQP_SHADOW_ACC) { // Shadow System
  8285. pos = req_pos&EQP_SHADOW_ACC;
  8286. if (pos == EQP_SHADOW_ACC)
  8287. pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8288. }
  8289. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8290. pos = (req_pos&EQP_ARMS);
  8291. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8292. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8293. }
  8294. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8295. //Update skill-block range database when weapon range changes. [Skotlex]
  8296. i = sd->equip_index[EQI_HAND_R];
  8297. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8298. flag = 1;
  8299. else
  8300. flag = id->range != sd->inventory_data[i]->range;
  8301. }
  8302. for(i=0;i<EQI_MAX;i++) {
  8303. if(pos & equip_pos[i]) {
  8304. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8305. pc_unequipitem(sd,sd->equip_index[i],2);
  8306. sd->equip_index[i] = n;
  8307. }
  8308. }
  8309. if(pos==EQP_AMMO) {
  8310. clif_arrowequip(sd,n);
  8311. clif_arrow_fail(sd,3);
  8312. }
  8313. else
  8314. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8315. sd->status.inventory[n].equip=pos;
  8316. if(pos & EQP_HAND_R) {
  8317. if(id)
  8318. sd->weapontype1 = id->look;
  8319. else
  8320. sd->weapontype1 = 0;
  8321. pc_calcweapontype(sd);
  8322. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8323. }
  8324. if(pos & EQP_HAND_L) {
  8325. if(id) {
  8326. if(id->type == IT_WEAPON) {
  8327. sd->status.shield = 0;
  8328. sd->weapontype2 = id->look;
  8329. }
  8330. else
  8331. if(id->type == IT_ARMOR) {
  8332. sd->status.shield = id->look;
  8333. sd->weapontype2 = 0;
  8334. }
  8335. }
  8336. else
  8337. sd->status.shield = sd->weapontype2 = 0;
  8338. pc_calcweapontype(sd);
  8339. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8340. }
  8341. //Added check to prevent sending the same look on multiple slots ->
  8342. //causes client to redraw item on top of itself. (suggested by Lupus)
  8343. if(pos & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1) {
  8344. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID)))
  8345. sd->status.head_bottom = id->look;
  8346. else
  8347. sd->status.head_bottom = 0;
  8348. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8349. }
  8350. if(pos & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1) {
  8351. if(id)
  8352. sd->status.head_top = id->look;
  8353. else
  8354. sd->status.head_top = 0;
  8355. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8356. }
  8357. if(pos & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1) {
  8358. if(id && !(pos&EQP_HEAD_TOP))
  8359. sd->status.head_mid = id->look;
  8360. else
  8361. sd->status.head_mid = 0;
  8362. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8363. }
  8364. if(pos & EQP_COSTUME_HEAD_TOP) {
  8365. if(id){
  8366. sd->status.head_top = id->look;
  8367. } else
  8368. sd->status.head_top = 0;
  8369. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8370. }
  8371. if(pos & EQP_COSTUME_HEAD_MID) {
  8372. if(id && !(pos&EQP_HEAD_TOP)){
  8373. sd->status.head_mid = id->look;
  8374. } else
  8375. sd->status.head_mid = 0;
  8376. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8377. }
  8378. if(pos & EQP_COSTUME_HEAD_LOW) {
  8379. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID))){
  8380. sd->status.head_bottom = id->look;
  8381. } else
  8382. sd->status.head_bottom = 0;
  8383. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8384. }
  8385. if(pos & EQP_SHOES)
  8386. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8387. if(pos&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  8388. sd->status.robe = id ? id->look : 0;
  8389. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  8390. }
  8391. if(pos & EQP_COSTUME_GARMENT) {
  8392. sd->status.robe = id ? id->look : 0;
  8393. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  8394. }
  8395. pc_checkallowskill(sd); //Check if status changes should be halted.
  8396. iflag = sd->npc_item_flag;
  8397. /* check for combos (MUST be before status_calc_pc) */
  8398. if( id->combos_count )
  8399. pc_checkcombo(sd,id);
  8400. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8401. ; //No cards
  8402. else {
  8403. for( i = 0; i < id->slot; i++ ) {
  8404. struct item_data *data;
  8405. if (!sd->status.inventory[n].card[i])
  8406. continue;
  8407. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8408. if( data->combos_count )
  8409. pc_checkcombo(sd,data);
  8410. }
  8411. }
  8412. }
  8413. status_calc_pc(sd,SCO_NONE);
  8414. if (flag) //Update skill data
  8415. clif_skillinfoblock(sd);
  8416. //OnEquip script [Skotlex]
  8417. if (id) {
  8418. //only run the script if item isn't restricted
  8419. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  8420. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8421. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8422. ; //No cards
  8423. else {
  8424. for( i = 0; i < id->slot; i++ ) {
  8425. struct item_data *data;
  8426. if (!sd->status.inventory[n].card[i])
  8427. continue;
  8428. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8429. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  8430. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8431. }
  8432. }
  8433. }
  8434. }
  8435. sd->npc_item_flag = iflag;
  8436. return true;
  8437. }
  8438. /*==========================================
  8439. * Called when attemting to unequip an item from player
  8440. * type:
  8441. * 0 - only unequip
  8442. * 1 - calculate status after unequipping
  8443. * 2 - force unequip
  8444. * return: false - fail; true - success
  8445. *------------------------------------------*/
  8446. bool pc_unequipitem(struct map_session_data *sd,int n,int flag) {
  8447. int i,iflag;
  8448. bool status_cacl = false;
  8449. nullpo_retr(false,sd);
  8450. if( n < 0 || n >= MAX_INVENTORY ) {
  8451. clif_unequipitemack(sd,0,0,0);
  8452. return false;
  8453. }
  8454. // status change that makes player cannot unequip equipment
  8455. if( !(flag&2) && sd->sc.count && (
  8456. sd->sc.data[SC_BERSERK] ||
  8457. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8458. sd->sc.data[SC__BLOODYLUST] ||
  8459. sd->sc.data[SC_KYOUGAKU] ||
  8460. (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) // can't switch weapon
  8461. {
  8462. clif_unequipitemack(sd,n,0,0);
  8463. return false;
  8464. }
  8465. if (&sd->sc) {
  8466. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->type == IT_ARMOR && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  8467. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  8468. if (sd->sc.data[SC_HEAT_BARREL])
  8469. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  8470. if (sd->sc.data[SC_P_ALTER] && (sd->inventory_data[n]->type == IT_WEAPON || sd->inventory_data[n]->type == IT_AMMO))
  8471. status_change_end(&sd->bl,SC_P_ALTER,INVALID_TIMER);
  8472. }
  8473. if(battle_config.battle_log)
  8474. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->status.inventory[n].equip);
  8475. if(!sd->status.inventory[n].equip){ //Nothing to unequip
  8476. clif_unequipitemack(sd,n,0,0);
  8477. return false;
  8478. }
  8479. for(i=0;i<EQI_MAX;i++) {
  8480. if(sd->status.inventory[n].equip & equip_pos[i])
  8481. sd->equip_index[i] = -1;
  8482. }
  8483. if(sd->status.inventory[n].equip & EQP_HAND_R) {
  8484. sd->weapontype1 = 0;
  8485. sd->status.weapon = sd->weapontype2;
  8486. pc_calcweapontype(sd);
  8487. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8488. if( !battle_config.dancing_weaponswitch_fix )
  8489. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  8490. }
  8491. if(sd->status.inventory[n].equip & EQP_HAND_L) {
  8492. sd->status.shield = sd->weapontype2 = 0;
  8493. pc_calcweapontype(sd);
  8494. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8495. }
  8496. if(sd->status.inventory[n].equip & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1 ) {
  8497. sd->status.head_bottom = 0;
  8498. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8499. }
  8500. if(sd->status.inventory[n].equip & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1 ) {
  8501. sd->status.head_top = 0;
  8502. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8503. }
  8504. if(sd->status.inventory[n].equip & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1 ) {
  8505. sd->status.head_mid = 0;
  8506. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8507. }
  8508. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_TOP) {
  8509. sd->status.head_top = ( pc_checkequip(sd,EQP_HEAD_TOP) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_TOP)]->look : 0;
  8510. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8511. }
  8512. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_MID) {
  8513. sd->status.head_mid = ( pc_checkequip(sd,EQP_HEAD_MID) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_MID)]->look : 0;
  8514. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8515. }
  8516. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_LOW) {
  8517. sd->status.head_bottom = ( pc_checkequip(sd,EQP_HEAD_LOW) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_LOW)]->look : 0;
  8518. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8519. }
  8520. if(sd->status.inventory[n].equip & EQP_SHOES)
  8521. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8522. if(sd->status.inventory[n].equip&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  8523. sd->status.robe = 0;
  8524. clif_changelook(&sd->bl, LOOK_ROBE, 0);
  8525. }
  8526. if(sd->status.inventory[n].equip & EQP_COSTUME_GARMENT) {
  8527. sd->status.robe = ( pc_checkequip(sd,EQP_GARMENT) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_GARMENT)]->look : 0;
  8528. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  8529. }
  8530. clif_unequipitemack(sd,n,sd->status.inventory[n].equip,1);
  8531. if((sd->status.inventory[n].equip & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON && //On weapon change (right and left hand)
  8532. (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO])) //Check for seven wind (but not level seven!)
  8533. skill_enchant_elemental_end(&sd->bl,SC_NONE);
  8534. if(sd->status.inventory[n].equip & EQP_ARMOR) {
  8535. // On Armor Change...
  8536. status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER);
  8537. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  8538. }
  8539. if( sd->state.autobonus&sd->status.inventory[n].equip )
  8540. sd->state.autobonus &= ~sd->status.inventory[n].equip; //Check for activated autobonus [Inkfish]
  8541. sd->status.inventory[n].equip=0;
  8542. iflag = sd->npc_item_flag;
  8543. /* check for combos (MUST be before status_calc_pc) */
  8544. if ( sd->inventory_data[n] ) {
  8545. if( sd->inventory_data[n]->combos_count ) {
  8546. if( pc_removecombo(sd,sd->inventory_data[n]) )
  8547. status_cacl = true;
  8548. } if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8549. ; //No cards
  8550. else {
  8551. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  8552. struct item_data *data;
  8553. if (!sd->status.inventory[n].card[i])
  8554. continue;
  8555. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8556. if( data->combos_count ) {
  8557. if( pc_removecombo(sd,data) )
  8558. status_cacl = true;
  8559. }
  8560. }
  8561. }
  8562. }
  8563. }
  8564. if(flag&1 || status_cacl) {
  8565. pc_checkallowskill(sd);
  8566. status_calc_pc(sd,SCO_NONE);
  8567. }
  8568. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  8569. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  8570. //OnUnEquip script [Skotlex]
  8571. if (sd->inventory_data[n]) {
  8572. if (sd->inventory_data[n]->unequip_script)
  8573. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8574. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8575. ; //No cards
  8576. else {
  8577. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  8578. struct item_data *data;
  8579. if (!sd->status.inventory[n].card[i])
  8580. continue;
  8581. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8582. if( data->unequip_script )
  8583. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8584. }
  8585. }
  8586. }
  8587. }
  8588. sd->npc_item_flag = iflag;
  8589. return true;
  8590. }
  8591. /*==========================================
  8592. * Checking if player (sd) has an invalid item
  8593. * and is unequiped on map load (item_noequip)
  8594. *------------------------------------------*/
  8595. void pc_checkitem(struct map_session_data *sd) {
  8596. int i, calc_flag = 0;
  8597. struct item it;
  8598. nullpo_retv(sd);
  8599. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  8600. return;
  8601. for( i = 0; i < MAX_INVENTORY; i++ ) {
  8602. it = sd->status.inventory[i];
  8603. if( it.nameid == 0 )
  8604. continue;
  8605. if( !it.equip )
  8606. continue;
  8607. if( it.equip&~pc_equippoint(sd,i) ) {
  8608. pc_unequipitem(sd, i, 2);
  8609. calc_flag = 1;
  8610. continue;
  8611. }
  8612. 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) ) {
  8613. pc_unequipitem(sd, i, 2);
  8614. calc_flag = 1;
  8615. continue;
  8616. }
  8617. }
  8618. if( calc_flag && sd->state.active ) {
  8619. pc_checkallowskill(sd);
  8620. status_calc_pc(sd,SCO_NONE);
  8621. }
  8622. }
  8623. /*==========================================
  8624. * Checks for unavailable items and removes them.
  8625. *------------------------------------------*/
  8626. void pc_check_available_item(struct map_session_data *sd) {
  8627. int i, it;
  8628. char output[256];
  8629. nullpo_retv(sd);
  8630. if( battle_config.item_check&1 ) { // Check for invalid(ated) items in inventory.
  8631. for( i = 0; i < MAX_INVENTORY; i++ ) {
  8632. it = sd->status.inventory[i].nameid;
  8633. if (!it)
  8634. continue;
  8635. if (!itemdb_available(it)) {
  8636. sprintf(output, msg_txt(sd, 709), it); // Item %hu has been removed from your inventory.
  8637. clif_displaymessage(sd->fd, output);
  8638. ShowWarning("Removed invalid/disabled item id %hu from inventory (amount=%d, char_id=%d).\n", it, sd->status.inventory[i].amount, sd->status.char_id);
  8639. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  8640. continue;
  8641. }
  8642. if (!sd->status.inventory[i].unique_id && !itemdb_isstackable(it))
  8643. sd->status.inventory[i].unique_id = pc_generate_unique_id(sd);
  8644. }
  8645. }
  8646. if( battle_config.item_check&2 ) { // Check for invalid(ated) items in cart.
  8647. for( i = 0; i < MAX_CART; i++ ) {
  8648. it = sd->status.cart[i].nameid;
  8649. if (!it)
  8650. continue;
  8651. if (!itemdb_available(it)) {
  8652. sprintf(output, msg_txt(sd, 710), it); // Item %hu has been removed from your cart.
  8653. clif_displaymessage(sd->fd, output);
  8654. ShowWarning("Removed invalid/disabled item id %hu from cart (amount=%d, char_id=%d).\n", it, sd->status.cart[i].amount, sd->status.char_id);
  8655. pc_cart_delitem(sd, i, sd->status.cart[i].amount, 0, LOG_TYPE_OTHER);
  8656. continue;
  8657. }
  8658. if (!sd->status.cart[i].unique_id && !itemdb_isstackable(it))
  8659. sd->status.cart[i].unique_id = pc_generate_unique_id(sd);
  8660. }
  8661. }
  8662. if( battle_config.item_check&4 ) { // Check for invalid(ated) items in storage.
  8663. for( i = 0; i < sd->storage_size; i++ ) {
  8664. it = sd->status.storage.items[i].nameid;
  8665. if (!it)
  8666. continue;
  8667. if (!itemdb_available(it)) {
  8668. sprintf(output, msg_txt(sd, 711), it); // Item %hu has been removed from your storage.
  8669. clif_displaymessage(sd->fd, output);
  8670. ShowWarning("Removed invalid/disabled item id %hu from storage (amount=%d, char_id=%d).\n", it, sd->status.storage.items[i].amount, sd->status.char_id);
  8671. storage_delitem(sd, i, sd->status.storage.items[i].amount);
  8672. continue;
  8673. }
  8674. if (!sd->status.storage.items[i].unique_id && !itemdb_isstackable(it))
  8675. sd->status.storage.items[i].unique_id = pc_generate_unique_id(sd);
  8676. }
  8677. }
  8678. }
  8679. /*==========================================
  8680. * Update PVP rank for sd1 in cmp to sd2
  8681. *------------------------------------------*/
  8682. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  8683. {
  8684. struct map_session_data *sd1,*sd2;
  8685. sd1=(struct map_session_data *)bl;
  8686. sd2=va_arg(ap,struct map_session_data *);
  8687. if( sd1->sc.option&OPTION_INVISIBLE || sd2->sc.option&OPTION_INVISIBLE )
  8688. {// cannot register pvp rank for hidden GMs
  8689. return 0;
  8690. }
  8691. if( sd1->pvp_point > sd2->pvp_point )
  8692. sd2->pvp_rank++;
  8693. return 0;
  8694. }
  8695. /*==========================================
  8696. * Calculate new rank beetween all present players (map_foreachinarea)
  8697. * and display result
  8698. *------------------------------------------*/
  8699. int pc_calc_pvprank(struct map_session_data *sd)
  8700. {
  8701. int old = sd->pvp_rank;
  8702. struct map_data *m = &map[sd->bl.m];
  8703. sd->pvp_rank=1;
  8704. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  8705. if(old!=sd->pvp_rank || sd->pvp_lastusers!=m->users_pvp)
  8706. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=m->users_pvp,0);
  8707. return sd->pvp_rank;
  8708. }
  8709. /*==========================================
  8710. * Calculate next sd ranking calculation from config
  8711. *------------------------------------------*/
  8712. int pc_calc_pvprank_timer(int tid, unsigned int tick, int id, intptr_t data)
  8713. {
  8714. struct map_session_data *sd;
  8715. sd=map_id2sd(id);
  8716. if(sd==NULL)
  8717. return 0;
  8718. sd->pvp_timer = INVALID_TIMER;
  8719. if( sd->sc.option&OPTION_INVISIBLE )
  8720. {// do not calculate the pvp rank for a hidden GM
  8721. return 0;
  8722. }
  8723. if( pc_calc_pvprank(sd) > 0 )
  8724. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  8725. return 0;
  8726. }
  8727. /*==========================================
  8728. * Checking if sd is married
  8729. * Return:
  8730. * partner_id = yes
  8731. * 0 = no
  8732. *------------------------------------------*/
  8733. int pc_ismarried(struct map_session_data *sd)
  8734. {
  8735. if(sd == NULL)
  8736. return -1;
  8737. if(sd->status.partner_id > 0)
  8738. return sd->status.partner_id;
  8739. else
  8740. return 0;
  8741. }
  8742. /*==========================================
  8743. * Marry player sd to player dstsd
  8744. * Return:
  8745. * false = fail
  8746. * true = success
  8747. *------------------------------------------*/
  8748. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  8749. {
  8750. if(sd == NULL || dstsd == NULL ||
  8751. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  8752. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  8753. return false;
  8754. sd->status.partner_id = dstsd->status.char_id;
  8755. dstsd->status.partner_id = sd->status.char_id;
  8756. return true;
  8757. }
  8758. /*==========================================
  8759. * Divorce sd from its partner
  8760. * Return:
  8761. * false = fail
  8762. * true = success
  8763. *------------------------------------------*/
  8764. bool pc_divorce(struct map_session_data *sd)
  8765. {
  8766. struct map_session_data *p_sd;
  8767. int i;
  8768. if( sd == NULL || !pc_ismarried(sd) )
  8769. return false;
  8770. if( !sd->status.partner_id )
  8771. return false; // Char is not married
  8772. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  8773. { // Lets char server do the divorce
  8774. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  8775. return false; // No char server connected
  8776. return true;
  8777. }
  8778. // Both players online, lets do the divorce manually
  8779. sd->status.partner_id = 0;
  8780. p_sd->status.partner_id = 0;
  8781. for( i = 0; i < MAX_INVENTORY; i++ )
  8782. {
  8783. if( sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F )
  8784. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  8785. if( p_sd->status.inventory[i].nameid == WEDDING_RING_M || p_sd->status.inventory[i].nameid == WEDDING_RING_F )
  8786. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  8787. }
  8788. clif_divorced(sd, p_sd->status.name);
  8789. clif_divorced(p_sd, sd->status.name);
  8790. return true;
  8791. }
  8792. /**
  8793. * Get the partner map_session_data of a player
  8794. * @param sd : the husband|wife session
  8795. * @return partner session or NULL
  8796. */
  8797. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  8798. if (!sd || !pc_ismarried(sd))
  8799. return NULL;
  8800. return map_charid2sd(sd->status.partner_id);
  8801. }
  8802. /**
  8803. * Get the father map_session_data of a player
  8804. * @param sd : the baby session
  8805. * @return father session or NULL
  8806. */
  8807. struct map_session_data *pc_get_father (struct map_session_data *sd){
  8808. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  8809. return NULL;
  8810. return map_charid2sd(sd->status.father);
  8811. }
  8812. /**
  8813. * Get the mother map_session_data of a player
  8814. * @param sd : the baby session
  8815. * @return mother session or NULL
  8816. */
  8817. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  8818. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  8819. return NULL;
  8820. return map_charid2sd(sd->status.mother);
  8821. }
  8822. /*==========================================
  8823. * Get sd children charid. (Need to be married)
  8824. *------------------------------------------*/
  8825. struct map_session_data *pc_get_child (struct map_session_data *sd)
  8826. {
  8827. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  8828. // charid2sd returns NULL if not found
  8829. return NULL;
  8830. return map_charid2sd(sd->status.child);
  8831. }
  8832. /*==========================================
  8833. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  8834. *------------------------------------------*/
  8835. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  8836. {
  8837. int hp = 0, sp = 0;
  8838. if( pc_isdead(sd) )
  8839. return;
  8840. if (sd->hp_loss.value) {
  8841. sd->hp_loss.tick += diff_tick;
  8842. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  8843. hp += sd->hp_loss.value;
  8844. sd->hp_loss.tick -= sd->hp_loss.rate;
  8845. }
  8846. if(hp >= sd->battle_status.hp)
  8847. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  8848. }
  8849. if (sd->sp_loss.value) {
  8850. sd->sp_loss.tick += diff_tick;
  8851. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  8852. sp += sd->sp_loss.value;
  8853. sd->sp_loss.tick -= sd->sp_loss.rate;
  8854. }
  8855. }
  8856. if (hp > 0 || sp > 0)
  8857. status_zap(&sd->bl, hp, sp);
  8858. }
  8859. //Character regen. Flag is used to know which types of regen can take place.
  8860. //&1: HP regen
  8861. //&2: SP regen
  8862. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  8863. {
  8864. int hp = 0, sp = 0;
  8865. if (sd->hp_regen.value) {
  8866. sd->hp_regen.tick += diff_tick;
  8867. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  8868. hp += sd->hp_regen.value;
  8869. sd->hp_regen.tick -= sd->hp_regen.rate;
  8870. }
  8871. }
  8872. if (sd->sp_regen.value) {
  8873. sd->sp_regen.tick += diff_tick;
  8874. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  8875. sp += sd->sp_regen.value;
  8876. sd->sp_regen.tick -= sd->sp_regen.rate;
  8877. }
  8878. }
  8879. if (hp > 0 || sp > 0)
  8880. status_heal(&sd->bl, hp, sp, 0);
  8881. }
  8882. /*==========================================
  8883. * Memo player sd savepoint. (map,x,y)
  8884. *------------------------------------------*/
  8885. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  8886. {
  8887. nullpo_retv(sd);
  8888. sd->status.save_point.map = mapindex;
  8889. sd->status.save_point.x = x;
  8890. sd->status.save_point.y = y;
  8891. }
  8892. /*==========================================
  8893. * Save 1 player data at autosave interval
  8894. *------------------------------------------*/
  8895. static int pc_autosave(int tid, unsigned int tick, int id, intptr_t data)
  8896. {
  8897. int interval;
  8898. struct s_mapiterator* iter;
  8899. struct map_session_data* sd;
  8900. static int last_save_id = 0, save_flag = 0;
  8901. if(save_flag == 2) //Someone was saved on last call, normal cycle
  8902. save_flag = 0;
  8903. else
  8904. save_flag = 1; //Noone was saved, so save first found char.
  8905. iter = mapit_getallusers();
  8906. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8907. {
  8908. if(sd->bl.id == last_save_id && save_flag != 1) {
  8909. save_flag = 1;
  8910. continue;
  8911. }
  8912. if(save_flag != 1) //Not our turn to save yet.
  8913. continue;
  8914. //Save char.
  8915. last_save_id = sd->bl.id;
  8916. save_flag = 2;
  8917. #ifdef VIP_ENABLE
  8918. if(sd->vip.enabled) //check if we're still vip
  8919. chrif_req_login_operation(1, sd->status.name, 6, 0, 1, 0);
  8920. #endif
  8921. chrif_save(sd,0);
  8922. break;
  8923. }
  8924. mapit_free(iter);
  8925. interval = autosave_interval/(map_usercount()+1);
  8926. if(interval < minsave_interval)
  8927. interval = minsave_interval;
  8928. add_timer(gettick()+interval,pc_autosave,0,0);
  8929. return 0;
  8930. }
  8931. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  8932. {
  8933. if (sd->state.night != night_flag && map[sd->bl.m].flag.nightenabled)
  8934. { //Night/day state does not match.
  8935. clif_status_load(&sd->bl, SI_NIGHT, night_flag); //New night effect by dynamix [Skotlex]
  8936. sd->state.night = night_flag;
  8937. return 1;
  8938. }
  8939. return 0;
  8940. }
  8941. /*================================================
  8942. * timer to do the day [Yor]
  8943. * data: 0 = called by timer, 1 = gmcommand/script
  8944. *------------------------------------------------*/
  8945. int map_day_timer(int tid, unsigned int tick, int id, intptr_t data)
  8946. {
  8947. char tmp_soutput[1024];
  8948. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  8949. return 0;
  8950. if (!night_flag)
  8951. return 0; //Already day.
  8952. night_flag = 0; // 0=day, 1=night [Yor]
  8953. map_foreachpc(pc_daynight_timer_sub);
  8954. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  8955. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  8956. return 0;
  8957. }
  8958. /*================================================
  8959. * timer to do the night [Yor]
  8960. * data: 0 = called by timer, 1 = gmcommand/script
  8961. *------------------------------------------------*/
  8962. int map_night_timer(int tid, unsigned int tick, int id, intptr_t data)
  8963. {
  8964. char tmp_soutput[1024];
  8965. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  8966. return 0;
  8967. if (night_flag)
  8968. return 0; //Already nigth.
  8969. night_flag = 1; // 0=day, 1=night [Yor]
  8970. map_foreachpc(pc_daynight_timer_sub);
  8971. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  8972. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  8973. return 0;
  8974. }
  8975. /**
  8976. * Attempt to stand up a player
  8977. * @param sd
  8978. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  8979. * @return True if success, Fals if failed
  8980. */
  8981. bool pc_setstand(struct map_session_data *sd, bool force){
  8982. nullpo_ret(sd);
  8983. // Cannot stand yet
  8984. // TODO: Move to SCS_NOSTAND [Cydh]
  8985. if (!force && &sd->sc && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  8986. return false;
  8987. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  8988. clif_status_load(&sd->bl,SI_SIT,0);
  8989. clif_standing(&sd->bl); //Inform area PC is standing
  8990. //Reset sitting tick.
  8991. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  8992. sd->state.dead_sit = sd->vd.dead_sit = 0;
  8993. return true;
  8994. }
  8995. /**
  8996. * Mechanic (MADO GEAR)
  8997. **/
  8998. void pc_overheat(struct map_session_data *sd, int val) {
  8999. int heat = val, skill,
  9000. limit[] = { 10, 20, 28, 46, 66 };
  9001. if( !pc_ismadogear(sd) || sd->sc.data[SC_OVERHEAT] )
  9002. return; // already burning
  9003. skill = cap_value(pc_checkskill(sd,NC_MAINFRAME),0,4);
  9004. if( sd->sc.data[SC_OVERHEAT_LIMITPOINT] ) {
  9005. heat += sd->sc.data[SC_OVERHEAT_LIMITPOINT]->val1;
  9006. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  9007. }
  9008. heat = max(0,heat); // Avoid negative HEAT
  9009. if( heat >= limit[skill] )
  9010. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT,100,0,1000);
  9011. else
  9012. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT_LIMITPOINT,100,heat,30000);
  9013. return;
  9014. }
  9015. /**
  9016. * Check if player is autolooting given itemID.
  9017. */
  9018. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9019. {
  9020. uint8 i = 0;
  9021. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9022. return true;
  9023. if (!sd->state.autolooting)
  9024. return false;
  9025. if (sd->state.autolooting)
  9026. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9027. return (i != AUTOLOOTITEM_SIZE);
  9028. }
  9029. /**
  9030. * Checks if player can use @/#command
  9031. * @param sd Player map session data
  9032. * @param command Command name without @/# and params
  9033. * @param type is it atcommand or charcommand
  9034. */
  9035. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9036. {
  9037. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9038. }
  9039. /**
  9040. * Checks if commands used by a player should be logged
  9041. * according to their group setting.
  9042. * @param sd Player map session data
  9043. */
  9044. bool pc_should_log_commands(struct map_session_data *sd)
  9045. {
  9046. return pc_group_should_log_commands(pc_get_group_id(sd));
  9047. }
  9048. /**
  9049. * Spirit Charm expiration timer.
  9050. * @see TimerFunc
  9051. */
  9052. static int pc_spiritcharm_timer(int tid, unsigned int tick, int id, intptr_t data)
  9053. {
  9054. struct map_session_data *sd;
  9055. int i;
  9056. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9057. return 1;
  9058. if (sd->spiritcharm <= 0) {
  9059. 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);
  9060. sd->spiritcharm = 0;
  9061. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9062. return 0;
  9063. }
  9064. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9065. if (i == sd->spiritcharm) {
  9066. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9067. return 0;
  9068. }
  9069. sd->spiritcharm--;
  9070. if (i != sd->spiritcharm)
  9071. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9072. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9073. if (sd->spiritcharm <= 0)
  9074. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9075. clif_spiritcharm(sd);
  9076. return 0;
  9077. }
  9078. /**
  9079. * Adds a spirit charm.
  9080. * @param sd: Target character
  9081. * @param interval: Duration
  9082. * @param max: Maximum amount of charms to add
  9083. * @param type: Charm type (@see spirit_charm_types)
  9084. */
  9085. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9086. {
  9087. int tid, i;
  9088. nullpo_retv(sd);
  9089. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9090. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9091. if (max > MAX_SPIRITCHARM)
  9092. max = MAX_SPIRITCHARM;
  9093. if (sd->spiritcharm < 0)
  9094. sd->spiritcharm = 0;
  9095. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9096. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9097. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9098. sd->spiritcharm--;
  9099. if (sd->spiritcharm != 0)
  9100. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9101. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9102. }
  9103. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9104. 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);
  9105. if (i != sd->spiritcharm)
  9106. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9107. sd->spiritcharm_timer[i] = tid;
  9108. sd->spiritcharm++;
  9109. sd->spiritcharm_type = type;
  9110. clif_spiritcharm(sd);
  9111. }
  9112. /**
  9113. * Removes one or more spirit charms.
  9114. * @param sd: The target character
  9115. * @param count: Amount of charms to remove
  9116. * @param type: Type of charm to remove
  9117. */
  9118. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9119. {
  9120. int i;
  9121. nullpo_retv(sd);
  9122. if (sd->spiritcharm_type != type)
  9123. return;
  9124. if (sd->spiritcharm <= 0) {
  9125. sd->spiritcharm = 0;
  9126. return;
  9127. }
  9128. if (count <= 0)
  9129. return;
  9130. if (count > sd->spiritcharm)
  9131. count = sd->spiritcharm;
  9132. sd->spiritcharm -= count;
  9133. if (count > MAX_SPIRITCHARM)
  9134. count = MAX_SPIRITCHARM;
  9135. for (i = 0; i < count; i++) {
  9136. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9137. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9138. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9139. }
  9140. }
  9141. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9142. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9143. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9144. }
  9145. if (sd->spiritcharm <= 0)
  9146. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9147. clif_spiritcharm(sd);
  9148. }
  9149. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9150. /*==========================================
  9151. * Renewal EXP/Itemdrop rate modifier base on level penalty
  9152. * 1=exp 2=itemdrop
  9153. *------------------------------------------*/
  9154. int pc_level_penalty_mod(struct map_session_data *sd, int mob_level, uint32 mob_class, int type)
  9155. {
  9156. int diff, rate = 100, i;
  9157. int tmp;
  9158. nullpo_ret(sd);
  9159. diff = mob_level - sd->status.base_level;
  9160. if( diff < 0 )
  9161. diff = MAX_LEVEL + ( ~diff + 1 );
  9162. if((tmp = level_penalty[type][mob_class][diff] ) > 0 ) //use mobclass directly
  9163. return tmp;
  9164. //wtf is that for ? if penalty not found use the 1st one we found ?? ̂[lighta]
  9165. for( i = 0; i < CLASS_ALL; i++ ) {
  9166. if( ( tmp = level_penalty[type][i][diff] ) > 0 ) {
  9167. rate = tmp;
  9168. break;
  9169. }
  9170. }
  9171. return rate;
  9172. }
  9173. #endif
  9174. int pc_split_str(char *str,char **val,int num)
  9175. {
  9176. int i;
  9177. for (i=0; i<num && str; i++){
  9178. val[i] = str;
  9179. str = strchr(str,',');
  9180. if (str && i<num-1) //Do not remove a trailing comma.
  9181. *str++=0;
  9182. }
  9183. return i;
  9184. }
  9185. int pc_split_atoi(char* str, int* val, char sep, int max)
  9186. {
  9187. int i,j;
  9188. for (i=0; i<max; i++) {
  9189. if (!str) break;
  9190. val[i] = atoi(str);
  9191. str = strchr(str,sep);
  9192. if (str)
  9193. *str++=0;
  9194. }
  9195. //Zero up the remaining.
  9196. for(j=i; j < max; j++)
  9197. val[j] = 0;
  9198. return i;
  9199. }
  9200. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9201. {
  9202. static int warning=0;
  9203. int i,j;
  9204. for (i=0; i<max; i++) {
  9205. double f;
  9206. if (!str) break;
  9207. f = atof(str);
  9208. if (f < 0)
  9209. val[i] = 0;
  9210. else if (f > UINT_MAX) {
  9211. val[i] = UINT_MAX;
  9212. if (!warning) {
  9213. warning = 1;
  9214. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9215. }
  9216. } else
  9217. val[i] = (unsigned int)f;
  9218. str = strchr(str,sep);
  9219. if (str)
  9220. *str++=0;
  9221. }
  9222. //Zero up the remaining.
  9223. for(j=i; j < max; j++)
  9224. val[j] = 0;
  9225. return i;
  9226. }
  9227. /*==========================================
  9228. * sub DB reading.
  9229. * Function used to read skill_tree.txt
  9230. *------------------------------------------*/
  9231. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  9232. {
  9233. unsigned char joblv = 0, skill_lv;
  9234. uint16 skill_id;
  9235. int idx, class_;
  9236. unsigned int i, offset = 3, skill_idx;
  9237. class_ = atoi(fields[0]);
  9238. skill_id = (uint16)atoi(fields[1]);
  9239. skill_lv = (unsigned char)atoi(fields[2]);
  9240. if(columns==4+MAX_PC_SKILL_REQUIRE*2)
  9241. {// job level requirement extra column
  9242. joblv = (unsigned char)atoi(fields[3]);
  9243. offset++;
  9244. }
  9245. if(!pcdb_checkid(class_))
  9246. {
  9247. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  9248. return false;
  9249. }
  9250. idx = pc_class2idx(class_);
  9251. //This is to avoid adding two lines for the same skill. [Skotlex]
  9252. ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].id == 0 || skill_tree[idx][skill_idx].id == skill_id );
  9253. if( skill_idx == MAX_SKILL_TREE )
  9254. {
  9255. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree. Maximum number of skills per class has been reached.\n", skill_id, class_);
  9256. return false;
  9257. }
  9258. else if(skill_tree[idx][skill_idx].id)
  9259. {
  9260. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job class %d.\n", skill_id, class_);
  9261. }
  9262. skill_tree[idx][skill_idx].id = skill_id;
  9263. skill_tree[idx][skill_idx].max = skill_lv;
  9264. skill_tree[idx][skill_idx].joblv = joblv;
  9265. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  9266. {
  9267. skill_tree[idx][skill_idx].need[i].id = atoi(fields[i*2+offset]);
  9268. skill_tree[idx][skill_idx].need[i].lv = atoi(fields[i*2+offset+1]);
  9269. }
  9270. return true;
  9271. }
  9272. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9273. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  9274. {
  9275. int type, class_, diff;
  9276. type = atoi(fields[0]); //1=experience, 2=item drop
  9277. class_ = atoi(fields[1]);
  9278. diff = atoi(fields[2]);
  9279. if( type != 1 && type != 2 ){
  9280. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  9281. return false;
  9282. }
  9283. if( !CHK_CLASS(class_) ){
  9284. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  9285. return false;
  9286. }
  9287. diff = min(diff, MAX_LEVEL);
  9288. if( diff < 0 )
  9289. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  9290. level_penalty[type][class_][diff] = atoi(fields[3]);
  9291. return true;
  9292. }
  9293. #endif
  9294. /** [Cydh]
  9295. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  9296. * @param level Base level of player
  9297. * @param class_ Job ID @see enum e_job
  9298. * @return base_hp
  9299. */
  9300. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  9301. double base_hp;
  9302. uint16 i, idx = pc_class2idx(class_);
  9303. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  9304. #ifndef RENEWAL
  9305. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  9306. #endif
  9307. for (i = 2; i <= level; i++)
  9308. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  9309. return (unsigned int)base_hp;
  9310. }
  9311. /** [Playtester]
  9312. * Calculates base sp of player.
  9313. * @param level Base level of player
  9314. * @param class_ Job ID @see enum e_job
  9315. * @return base_sp
  9316. */
  9317. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  9318. double base_sp;
  9319. uint16 idx = pc_class2idx(class_);
  9320. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  9321. switch (class_) {
  9322. case JOB_NINJA:
  9323. if (level >= 10)
  9324. base_sp -= 22;
  9325. else
  9326. base_sp = 11 + 3*level;
  9327. break;
  9328. case JOB_GUNSLINGER:
  9329. if (level > 10)
  9330. base_sp -= 18;
  9331. else
  9332. base_sp = 9 + 3*level;
  9333. break;
  9334. }
  9335. return (unsigned int)base_sp;
  9336. }
  9337. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  9338. static bool pc_readdb_job1(char* fields[], int columns, int current){
  9339. int idx, class_;
  9340. unsigned int i;
  9341. class_ = atoi(fields[0]);
  9342. if (!pcdb_checkid(class_)) {
  9343. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  9344. return false;
  9345. }
  9346. idx = pc_class2idx(class_);
  9347. job_info[idx].max_weight_base = atoi(fields[1]);
  9348. job_info[idx].hp_factor = atoi(fields[2]);
  9349. job_info[idx].hp_multiplicator = atoi(fields[3]);
  9350. job_info[idx].sp_factor = atoi(fields[4]);
  9351. #ifdef RENEWAL_ASPD
  9352. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  9353. #else
  9354. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  9355. #endif
  9356. {
  9357. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  9358. }
  9359. return true;
  9360. }
  9361. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  9362. static bool pc_readdb_job2(char* fields[], int columns, int current)
  9363. {
  9364. int idx, class_, i;
  9365. class_ = atoi(fields[0]);
  9366. if(!pcdb_checkid(class_))
  9367. {
  9368. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  9369. return false;
  9370. }
  9371. idx = pc_class2idx(class_);
  9372. for(i = 1; i < columns; i++)
  9373. {
  9374. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  9375. }
  9376. return true;
  9377. }
  9378. //Reading job_exp.txt line
  9379. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  9380. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  9381. {
  9382. int idx, i, type;
  9383. int job_id,job_count,jobs[CLASS_COUNT];
  9384. unsigned int ui, maxlvl;
  9385. maxlvl = atoi(fields[0]);
  9386. if(maxlvl > MAX_LEVEL || maxlvl<1){
  9387. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  9388. return false;
  9389. }
  9390. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9391. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  9392. return false;
  9393. }
  9394. type = atoi(fields[2]);
  9395. if(type < 0 || type > 1){
  9396. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  9397. return false;
  9398. }
  9399. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  9400. if (job_count < 1)
  9401. return false;
  9402. job_id = jobs[0];
  9403. if(!pcdb_checkid(job_id)){
  9404. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  9405. return false;
  9406. }
  9407. idx = pc_class2idx(job_id);
  9408. job_info[idx].max_level[type] = maxlvl;
  9409. for(i=0; i<maxlvl; i++)
  9410. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  9411. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  9412. //The reasoning behind the -2 is this... if the max level is 5, then the array
  9413. //should look like this:
  9414. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  9415. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  9416. job_info[idx].max_level[type]--;
  9417. if (job_info[idx].max_level[type] < maxlvl) {
  9418. 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]);
  9419. ShowInfo("Filling the missing values with the last exp entry.\n");
  9420. //Fill the requested values with the last entry.
  9421. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  9422. for (; ui+2 < maxlvl; ui++)
  9423. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  9424. job_info[idx].max_level[type] = maxlvl;
  9425. }
  9426. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9427. for (i = 1; i < job_count; i++) {
  9428. job_id = jobs[i];
  9429. if (!pcdb_checkid(job_id)) {
  9430. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  9431. continue;
  9432. }
  9433. idx = pc_class2idx(job_id);
  9434. 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]));
  9435. job_info[idx].max_level[type] = maxlvl;
  9436. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9437. }
  9438. return true;
  9439. }
  9440. /**
  9441. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  9442. * startlvl,endlvl,class,type,values...
  9443. */
  9444. #ifdef HP_SP_TABLES
  9445. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  9446. {
  9447. int i, startlvl, endlvl;
  9448. int job_count,jobs[CLASS_COUNT];
  9449. short type;
  9450. startlvl = atoi(fields[0]);
  9451. if(startlvl<1){
  9452. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9453. return false;
  9454. }
  9455. endlvl = atoi(fields[1]);
  9456. if(endlvl<1 || endlvl<startlvl){
  9457. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  9458. return false;
  9459. }
  9460. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9461. 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);
  9462. return false;
  9463. }
  9464. type = atoi(fields[3]);
  9465. if(type < 0 || type > 1){
  9466. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  9467. return false;
  9468. }
  9469. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  9470. if (job_count < 1)
  9471. return false;
  9472. for (i = 0; i < job_count; i++) {
  9473. int idx, job_id = jobs[i], use_endlvl;
  9474. if (!pcdb_checkid(job_id)) {
  9475. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  9476. return false;
  9477. }
  9478. idx = pc_class2idx(job_id);
  9479. if (startlvl > job_info[idx].max_level[0]) {
  9480. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9481. return false;
  9482. }
  9483. //Just read until available max level for this job, don't use MAX_LEVEL!
  9484. use_endlvl = endlvl;
  9485. if (use_endlvl > job_info[idx].max_level[0])
  9486. use_endlvl = job_info[idx].max_level[0];
  9487. if(type == 0) { //hp type
  9488. uint16 j;
  9489. for(j = 0; j < use_endlvl; j++) {
  9490. if (atoi(fields[j+4])) {
  9491. uint16 lvl_idx = startlvl-1+j;
  9492. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  9493. //Tells if this HP is lower than previous level (but not for 99->100)
  9494. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  9495. 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",
  9496. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  9497. }
  9498. }
  9499. }
  9500. else { //sp type
  9501. uint16 j;
  9502. for(j = 0; j < use_endlvl; j++) {
  9503. if (atoi(fields[j+4])) {
  9504. uint16 lvl_idx = startlvl-1+j;
  9505. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  9506. //Tells if this SP is lower than previous level (but not for 99->100)
  9507. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  9508. 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",
  9509. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  9510. }
  9511. }
  9512. }
  9513. }
  9514. return true;
  9515. }
  9516. #endif
  9517. /** [Cydh]
  9518. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  9519. */
  9520. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  9521. {
  9522. int idx, class_;
  9523. uint16 str, agi, vit, int_, dex, luk;
  9524. script_get_constant(trim(fields[0]),&class_);
  9525. if ((idx = pc_class2idx(class_)) < 0) {
  9526. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  9527. return false;
  9528. }
  9529. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  9530. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  9531. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  9532. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  9533. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  9534. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  9535. job_info[idx].max_param.str = str;
  9536. job_info[idx].max_param.agi = agi;
  9537. job_info[idx].max_param.vit = vit;
  9538. job_info[idx].max_param.int_ = int_;
  9539. job_info[idx].max_param.dex = dex;
  9540. job_info[idx].max_param.luk = luk;
  9541. return true;
  9542. }
  9543. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  9544. int i=1;
  9545. char line[24000]; //FIXME this seem too big
  9546. FILE *fp;
  9547. sprintf(line, "%s/statpoint.txt", basedir);
  9548. fp=fopen(line,"r");
  9549. if(fp == NULL){
  9550. if(silent==0) ShowWarning("Can't read '"CL_WHITE"%s"CL_RESET"'... Generating DB.\n",line);
  9551. return max(last_s,i);
  9552. } else {
  9553. int entries=0;
  9554. while(fgets(line, sizeof(line), fp))
  9555. {
  9556. int stat;
  9557. trim(line);
  9558. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  9559. continue;
  9560. if ((stat=strtoul(line,NULL,10))<0)
  9561. stat=0;
  9562. if (i > MAX_LEVEL)
  9563. break;
  9564. statp[i]=stat;
  9565. i++;
  9566. entries++;
  9567. }
  9568. fclose(fp);
  9569. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/%s"CL_RESET"'.\n", entries, basedir,"statpoint.txt");
  9570. }
  9571. return max(last_s,i);
  9572. }
  9573. /*==========================================
  9574. * pc DB reading.
  9575. * job_exp.txt - required experience values
  9576. * skill_tree.txt - skill tree for every class
  9577. * attr_fix.txt - elemental adjustment table
  9578. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  9579. * job_db2.txt - job,stats bonuses/lvl
  9580. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  9581. *------------------------------------------*/
  9582. void pc_readdb(void) {
  9583. int i, k, s = 1;
  9584. const char* dbsubpath[] = {
  9585. "",
  9586. "/"DBIMPORT,
  9587. //add other path here
  9588. };
  9589. //reset
  9590. memset(job_info,0,sizeof(job_info)); // job_info table
  9591. // Reset and read skilltree
  9592. memset(skill_tree,0,sizeof(skill_tree));
  9593. sv_readdb(db_path, DBPATH"skill_tree.txt", ',', 3+MAX_PC_SKILL_REQUIRE*2, 4+MAX_PC_SKILL_REQUIRE*2, -1, &pc_readdb_skilltree, 0);
  9594. sv_readdb(db_path, DBIMPORT"/skill_tree.txt", ',', 3+MAX_PC_SKILL_REQUIRE*2, 4+MAX_PC_SKILL_REQUIRE*2, -1, &pc_readdb_skilltree, 1);
  9595. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9596. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  9597. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  9598. for( k=1; k < 3; k++ ){ // fill in the blanks
  9599. int j;
  9600. for( j = 0; j < CLASS_ALL; j++ ){
  9601. int tmp = 0;
  9602. for( i = 0; i < MAX_LEVEL*2; i++ ){
  9603. if( i == MAX_LEVEL+1 )
  9604. tmp = level_penalty[k][j][0];// reset
  9605. if( level_penalty[k][j][i] > 0 )
  9606. tmp = level_penalty[k][j][i];
  9607. else
  9608. level_penalty[k][j][i] = tmp;
  9609. }
  9610. }
  9611. }
  9612. #endif
  9613. // reset then read statspoint
  9614. memset(statp,0,sizeof(statp));
  9615. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  9616. uint8 n1 = strlen(db_path)+strlen(dbsubpath[i])+1;
  9617. uint8 n2 = strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1;
  9618. char* dbsubpath1 = (char*)aMalloc(n1+1);
  9619. char* dbsubpath2 = (char*)aMalloc(n2+1);
  9620. if(i==0) {
  9621. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  9622. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  9623. }
  9624. else {
  9625. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  9626. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  9627. }
  9628. s = pc_read_statsdb(dbsubpath2,s,i);
  9629. if (i == 0)
  9630. #ifdef RENEWAL_ASPD
  9631. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9632. #else
  9633. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9634. #endif
  9635. else
  9636. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9637. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i);
  9638. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i); //support till 1000lvl
  9639. #ifdef HP_SP_TABLES
  9640. sv_readdb(dbsubpath2, "job_basehpsp_db.txt", ',', 4, 4+500, CLASS_COUNT*2, &pc_readdb_job_basehpsp, i); //Make it support until lvl 500!
  9641. #endif
  9642. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i);
  9643. aFree(dbsubpath1);
  9644. aFree(dbsubpath2);
  9645. }
  9646. // generate the remaining parts of the db if necessary
  9647. k = battle_config.use_statpoint_table; //save setting
  9648. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  9649. statp[0] = 45; // seed value
  9650. for (; s <= MAX_LEVEL; s++)
  9651. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  9652. battle_config.use_statpoint_table = k; //restore setting
  9653. //Checking if all class have their data
  9654. for (i = 0; i < JOB_MAX; i++) {
  9655. int idx;
  9656. uint16 j;
  9657. if (!pcdb_checkid(i))
  9658. continue;
  9659. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST)
  9660. continue; //Classes that do not need exp tables.
  9661. idx = pc_class2idx(i);
  9662. if (!job_info[idx].max_level[0])
  9663. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  9664. if (!job_info[idx].max_level[1])
  9665. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  9666. //Init and checking the empty value of Base HP/SP [Cydh]
  9667. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  9668. if (job_info[idx].base_hp[j] == 0)
  9669. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  9670. if (job_info[idx].base_sp[j] == 0)
  9671. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  9672. }
  9673. }
  9674. }
  9675. // Read MOTD on startup. [Valaris]
  9676. int pc_read_motd(void)
  9677. {
  9678. FILE* fp;
  9679. // clear old MOTD
  9680. memset(motd_text, 0, sizeof(motd_text));
  9681. // read current MOTD
  9682. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  9683. {
  9684. unsigned int entries = 0;
  9685. while( entries < MOTD_LINE_SIZE && fgets(motd_text[entries], sizeof(motd_text[entries]), fp) )
  9686. {
  9687. char* buf = motd_text[entries];
  9688. unsigned int lines = 0;
  9689. size_t len;
  9690. lines++;
  9691. if( buf[0] == '/' && buf[1] == '/' )
  9692. continue;
  9693. len = strlen(buf);
  9694. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  9695. len--;
  9696. if( len ) {
  9697. char * ptr;
  9698. buf[len] = 0;
  9699. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  9700. 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);
  9701. }
  9702. else {// empty line
  9703. buf[0] = ' ';
  9704. buf[1] = 0;
  9705. }
  9706. entries++;
  9707. }
  9708. fclose(fp);
  9709. ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, motd_txt);
  9710. }
  9711. else
  9712. ShowWarning("File '"CL_WHITE"%s"CL_RESET"' not found.\n", motd_txt);
  9713. return 0;
  9714. }
  9715. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  9716. int i,cursor = 0;
  9717. struct item_cd* cd = NULL;
  9718. if( load ) {
  9719. if( !(cd = idb_get(itemcd_db, sd->status.char_id)) ) {
  9720. // no skill cooldown is associated with this character
  9721. return;
  9722. }
  9723. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  9724. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  9725. sd->item_delay[cursor].tick = cd->tick[i];
  9726. sd->item_delay[cursor].nameid = cd->nameid[i];
  9727. cursor++;
  9728. }
  9729. }
  9730. idb_remove(itemcd_db,sd->status.char_id);
  9731. } else {
  9732. if( !(cd = idb_get(itemcd_db,sd->status.char_id)) ) {
  9733. // create a new skill cooldown object for map storage
  9734. CREATE( cd, struct item_cd, 1 );
  9735. idb_put( itemcd_db, sd->status.char_id, cd );
  9736. }
  9737. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  9738. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  9739. cd->tick[cursor] = sd->item_delay[i].tick;
  9740. cd->nameid[cursor] = sd->item_delay[i].nameid;
  9741. cursor++;
  9742. }
  9743. }
  9744. }
  9745. return;
  9746. }
  9747. /**
  9748. * Clear the dmglog data from player
  9749. * @param sd
  9750. * @param md
  9751. **/
  9752. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  9753. {
  9754. uint8 i;
  9755. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  9756. if (i < DAMAGELOG_SIZE) {
  9757. md->dmglog[i].id=0;
  9758. md->dmglog[i].dmg=0;
  9759. md->dmglog[i].flag=0;
  9760. }
  9761. }
  9762. /**
  9763. * Add log to player's dmglog
  9764. * @param sd
  9765. * @param id Monster's GID
  9766. **/
  9767. void pc_damage_log_add(struct map_session_data *sd, int id)
  9768. {
  9769. uint8 i = 0;
  9770. if (!sd)
  9771. return;
  9772. //Only store new data, don't need to renew the old one with same id
  9773. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  9774. if (sd->dmglog[i] == id)
  9775. return;
  9776. sd->dmglog[i] = id;
  9777. return;
  9778. }
  9779. }
  9780. /**
  9781. * Clear dmglog data from player
  9782. * @param sd
  9783. * @param id Monster's id
  9784. **/
  9785. void pc_damage_log_clear(struct map_session_data *sd, int id)
  9786. {
  9787. uint8 i;
  9788. struct mob_data *md = NULL;
  9789. if (!sd)
  9790. return;
  9791. if (!id) {
  9792. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  9793. if( !sd->dmglog[i] ) //skip the empty value
  9794. continue;
  9795. if ((md = map_id2md(sd->dmglog[i])))
  9796. pc_clear_log_damage_sub(sd->status.char_id,md);
  9797. sd->dmglog[i] = 0;
  9798. }
  9799. }
  9800. else {
  9801. if ((md = map_id2md(id)))
  9802. pc_clear_log_damage_sub(sd->status.char_id,md);
  9803. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  9804. if (i < DAMAGELOG_SIZE_PC)
  9805. sd->dmglog[i] = 0;
  9806. }
  9807. }
  9808. /* Status change data arrived from char-server */
  9809. void pc_scdata_received(struct map_session_data *sd) {
  9810. // Nothing todo yet
  9811. return;
  9812. }
  9813. /** Check expiration time and rental items
  9814. * @param sd
  9815. */
  9816. void pc_check_expiration(struct map_session_data *sd) {
  9817. pc_inventory_rentals(sd);
  9818. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  9819. time_t exp_time = sd->expiration_time;
  9820. char tmpstr[1024];
  9821. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  9822. clif_wis_message(sd->fd,wisp_server_name,tmpstr,strlen(tmpstr) + 1);
  9823. pc_expire_check(sd);
  9824. }
  9825. }
  9826. int pc_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  9827. struct map_session_data *sd = map_id2sd(id);
  9828. if( !sd ) return 0;
  9829. sd->expiration_tid = INVALID_TIMER;
  9830. if( sd->fd )
  9831. clif_authfail_fd(sd->fd,10);
  9832. map_quit(sd);
  9833. return 0;
  9834. }
  9835. int pc_autotrade_timer(int tid, unsigned int tick, int id, intptr_t data) {
  9836. struct map_session_data *sd = map_id2sd(id);
  9837. if (!sd)
  9838. return 0;
  9839. sd->autotrade_tid = INVALID_TIMER;
  9840. if (sd->state.autotrade&2)
  9841. vending_reopen(sd);
  9842. if (sd->state.autotrade&4)
  9843. buyingstore_reopen(sd);
  9844. if (!sd->vender_id && !sd->buyer_id) {
  9845. sd->state.autotrade = 0;
  9846. map_quit(sd);
  9847. }
  9848. return 0;
  9849. }
  9850. /* this timer exists only when a character with a expire timer > 24h is online */
  9851. /* it loops thru online players once an hour to check whether a new < 24h is available */
  9852. int pc_global_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  9853. struct s_mapiterator* iter;
  9854. struct map_session_data* sd;
  9855. iter = mapit_getallusers();
  9856. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9857. if( sd->expiration_time )
  9858. pc_expire_check(sd);
  9859. mapit_free(iter);
  9860. return 0;
  9861. }
  9862. void pc_expire_check(struct map_session_data *sd) {
  9863. /* ongoing timer */
  9864. if( sd->expiration_tid != INVALID_TIMER )
  9865. return;
  9866. /* not within the next 24h, enable the global check */
  9867. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  9868. /* global check not running, enable */
  9869. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  9870. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  9871. return;
  9872. }
  9873. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  9874. }
  9875. /**
  9876. * Deposit some money to bank
  9877. * @param sd
  9878. * @param money Amount of money to deposit
  9879. **/
  9880. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  9881. unsigned int limit_check = money + sd->bank_vault;
  9882. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  9883. return BDA_OVERFLOW;
  9884. } else if ( money > sd->status.zeny ) {
  9885. return BDA_NO_MONEY;
  9886. }
  9887. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  9888. return BDA_NO_MONEY;
  9889. sd->bank_vault += money;
  9890. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  9891. if( save_settings&CHARSAVE_BANK )
  9892. chrif_save(sd,0);
  9893. return BDA_SUCCESS;
  9894. }
  9895. /**
  9896. * Withdraw money from bank
  9897. * @param sd
  9898. * @param money Amount of money that will be withdrawn
  9899. **/
  9900. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  9901. unsigned int limit_check = money + sd->status.zeny;
  9902. if( money <= 0 ) {
  9903. return BWA_UNKNOWN_ERROR;
  9904. } else if ( money > sd->bank_vault ) {
  9905. return BWA_NO_MONEY;
  9906. } else if ( limit_check > MAX_ZENY ) {
  9907. /* no official response for this scenario exists. */
  9908. clif_colormes(sd->fd,color_table[COLOR_RED],msg_txt(sd,1495)); //You can't withdraw that much money
  9909. return BWA_UNKNOWN_ERROR;
  9910. }
  9911. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  9912. return BWA_NO_MONEY;
  9913. sd->bank_vault -= money;
  9914. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  9915. if( save_settings&CHARSAVE_BANK )
  9916. chrif_save(sd,0);
  9917. return BWA_SUCCESS;
  9918. }
  9919. /**
  9920. * Clear Crimson Marker data from caster
  9921. * @param sd: Player
  9922. **/
  9923. void pc_crimson_marker_clear(struct map_session_data *sd) {
  9924. uint8 i;
  9925. if (!sd)
  9926. return;
  9927. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  9928. struct block_list *bl = NULL;
  9929. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  9930. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  9931. sd->c_marker[i] = 0;
  9932. }
  9933. }
  9934. /**
  9935. * Show version to player
  9936. * @param sd: Player
  9937. **/
  9938. void pc_show_version(struct map_session_data *sd) {
  9939. const char* svn = get_svn_revision();
  9940. char buf[CHAT_SIZE_MAX];
  9941. if( svn[0] != UNKNOWN_VERSION )
  9942. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  9943. else {
  9944. const char* git = get_git_hash();
  9945. if( git[0] != UNKNOWN_VERSION )
  9946. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  9947. else
  9948. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  9949. }
  9950. clif_displaymessage(sd->fd,buf);
  9951. }
  9952. /**
  9953. * Run bonus_script on player
  9954. * @param sd
  9955. * @author [Cydh]
  9956. **/
  9957. void pc_bonus_script(struct map_session_data *sd) {
  9958. int now = gettick();
  9959. struct linkdb_node *node = NULL, *next = NULL;
  9960. if (!sd || !(node = sd->bonus_script.head))
  9961. return;
  9962. while (node) {
  9963. struct s_bonus_script_entry *entry = NULL;
  9964. next = node->next;
  9965. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  9966. // Only start timer for new bonus_script
  9967. if (entry->tid == INVALID_TIMER) {
  9968. if (entry->icon != SI_BLANK) // Gives status icon if exist
  9969. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  9970. entry->tick += now;
  9971. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  9972. }
  9973. if (entry->script)
  9974. run_script(entry->script, 0, sd->bl.id, 0);
  9975. else
  9976. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  9977. }
  9978. node = next;
  9979. }
  9980. }
  9981. /**
  9982. * Add bonus_script to player
  9983. * @param sd Player
  9984. * @param script_str Script string
  9985. * @param dur Duration in ms
  9986. * @param icon SI
  9987. * @param flag Flags @see enum e_bonus_script_flags
  9988. * @param type 0 - None, 1 - Buff, 2 - Debuff
  9989. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  9990. * @author [Cydh]
  9991. **/
  9992. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, uint32 dur, enum si_type icon, uint16 flag, uint8 type) {
  9993. struct script_code *script = NULL;
  9994. struct linkdb_node *node = NULL;
  9995. struct s_bonus_script_entry *entry = NULL;
  9996. if (!sd)
  9997. return NULL;
  9998. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  9999. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10000. return NULL;
  10001. }
  10002. // Duplication checks
  10003. if ((node = sd->bonus_script.head)) {
  10004. while (node) {
  10005. entry = (struct s_bonus_script_entry *)node->data;
  10006. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10007. int newdur = gettick() + dur;
  10008. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10009. settick_timer(entry->tid, newdur);
  10010. script_free_code(script);
  10011. return NULL;
  10012. }
  10013. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10014. break;
  10015. else { // No duplicate bonus
  10016. script_free_code(script);
  10017. return NULL;
  10018. }
  10019. }
  10020. node = node->next;
  10021. }
  10022. }
  10023. CREATE(entry, struct s_bonus_script_entry, 1);
  10024. entry->script_buf = StringBuf_Malloc();
  10025. StringBuf_AppendStr(entry->script_buf, script_str);
  10026. entry->tid = INVALID_TIMER;
  10027. entry->flag = flag;
  10028. entry->icon = icon;
  10029. entry->tick = dur; // Use duration first, on run change to expire time
  10030. entry->type = type;
  10031. entry->script = script;
  10032. sd->bonus_script.count++;
  10033. return entry;
  10034. }
  10035. /**
  10036. * Remove bonus_script data from player
  10037. * @param sd: Target player
  10038. * @param list: Bonus script entry from player
  10039. * @author [Cydh]
  10040. **/
  10041. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10042. if (entry->tid != INVALID_TIMER)
  10043. delete_timer(entry->tid, pc_bonus_script_timer);
  10044. if (entry->script)
  10045. script_free_code(entry->script);
  10046. if (entry->script_buf)
  10047. StringBuf_Free(entry->script_buf);
  10048. if (sd) {
  10049. if (entry->icon != SI_BLANK)
  10050. clif_status_load(&sd->bl, entry->icon, 0);
  10051. if (sd->bonus_script.count > 0)
  10052. sd->bonus_script.count--;
  10053. }
  10054. aFree(entry);
  10055. }
  10056. /**
  10057. * Do final process if no entry left
  10058. * @param sd
  10059. **/
  10060. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  10061. if (sd->bonus_script.count == 0) {
  10062. if (sd->bonus_script.head && sd->bonus_script.head->data)
  10063. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  10064. linkdb_final(&sd->bonus_script.head);
  10065. }
  10066. }
  10067. /**
  10068. * Timer for bonus_script
  10069. * @param tid
  10070. * @param tick
  10071. * @param id
  10072. * @param data
  10073. * @author [Cydh]
  10074. **/
  10075. int pc_bonus_script_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10076. struct map_session_data *sd;
  10077. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  10078. sd = map_id2sd(id);
  10079. if (!sd) {
  10080. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  10081. return 0;
  10082. }
  10083. if (tid == INVALID_TIMER)
  10084. return 0;
  10085. if (!sd->bonus_script.head || entry == NULL) {
  10086. ShowError("pc_bonus_script_timer: Invalid entry pointer 0x%08X!\n", entry);
  10087. return 0;
  10088. }
  10089. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10090. pc_bonus_script_free_entry(sd, entry);
  10091. pc_bonus_script_check_final(sd);
  10092. status_calc_pc(sd,SCO_NONE);
  10093. return 0;
  10094. }
  10095. /**
  10096. * Check then clear all active timer(s) of bonus_script data from player based on reason
  10097. * @param sd: Target player
  10098. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  10099. * @author [Cydh]
  10100. **/
  10101. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  10102. struct linkdb_node *node = NULL;
  10103. uint16 count = 0;
  10104. if (!sd || !(node = sd->bonus_script.head))
  10105. return;
  10106. while (node) {
  10107. struct linkdb_node *next = node->next;
  10108. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  10109. if (entry && (
  10110. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  10111. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  10112. (flag&entry->flag) || // Matched flag
  10113. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  10114. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  10115. )
  10116. )
  10117. {
  10118. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10119. pc_bonus_script_free_entry(sd, entry);
  10120. count++;
  10121. }
  10122. node = next;
  10123. }
  10124. pc_bonus_script_check_final(sd);
  10125. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  10126. status_calc_pc(sd,SCO_NONE);
  10127. }
  10128. /** [Cydh]
  10129. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  10130. * @param sd: Target player
  10131. */
  10132. void pc_cell_basilica(struct map_session_data *sd) {
  10133. nullpo_retv(sd);
  10134. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  10135. if (&sd->sc && sd->sc.data[SC_BASILICA])
  10136. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  10137. }
  10138. else if (!(&sd->sc) || !sd->sc.data[SC_BASILICA])
  10139. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,-1);
  10140. }
  10141. /** [Cydh]
  10142. * Get maximum specified parameter for specified class
  10143. * @param class_: sd->class
  10144. * @param sex: sd->status.sex
  10145. * @param flag: parameter will be checked
  10146. * @return max_param
  10147. */
  10148. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  10149. int idx = -1, class_ = sd->class_;
  10150. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  10151. short max_param = 0;
  10152. switch (param) {
  10153. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  10154. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  10155. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  10156. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  10157. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  10158. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  10159. }
  10160. if (max_param > 0)
  10161. return max_param;
  10162. }
  10163. return ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  10164. ((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)) :
  10165. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  10166. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  10167. }
  10168. /**
  10169. * Get max ASPD for player based on Class
  10170. * @param sd Player
  10171. * @return ASPD
  10172. */
  10173. short pc_maxaspd(struct map_session_data *sd) {
  10174. nullpo_ret(sd);
  10175. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  10176. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  10177. battle_config.max_aspd ));
  10178. }
  10179. /**
  10180. * Calculates total item-group related bonuses for the given item
  10181. * @param sd Player
  10182. * @param nameid Item ID
  10183. * @return Heal rate
  10184. **/
  10185. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  10186. short bonus = 0;
  10187. uint8 i;
  10188. if (!sd->itemgrouphealrate_count)
  10189. return bonus;
  10190. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10191. uint16 group_id = sd->itemgrouphealrate[i]->group_id, j;
  10192. struct s_item_group_db *group = NULL;
  10193. if (!group_id || !(group = itemdb_group_exists(group_id)))
  10194. continue;
  10195. for (j = 0; j < group->random[0].data_qty; j++) {
  10196. if (group->random[0].data[j].nameid == nameid) {
  10197. bonus += sd->itemgrouphealrate[i]->rate;
  10198. break;
  10199. }
  10200. }
  10201. }
  10202. return bonus;
  10203. }
  10204. /**
  10205. * Calculates total item-group related bonuses for the given item group
  10206. * @param sd Player
  10207. * @param group_id Item Group ID
  10208. * @return Heal rate
  10209. **/
  10210. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  10211. short bonus = 0;
  10212. uint8 i;
  10213. if (!sd->itemgrouphealrate_count)
  10214. return bonus;
  10215. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10216. if (sd->itemgrouphealrate[i]->group_id == group_id)
  10217. return sd->itemgrouphealrate[i]->rate;
  10218. }
  10219. return bonus;
  10220. }
  10221. /**
  10222. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  10223. * @param eqi Item EQI of enum equip_index
  10224. * @param *equip_index Player's equip_index[]
  10225. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  10226. * @return True if item in same inventory index, False if doesn't
  10227. */
  10228. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  10229. if (index < 0 || index >= MAX_INVENTORY)
  10230. return true;
  10231. // Dual weapon checks
  10232. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  10233. return true;
  10234. // Headgear with Mid & Low location
  10235. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  10236. return true;
  10237. // Headgear with Top & Mid or Low location
  10238. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  10239. return true;
  10240. // Headgear with Mid & Low location
  10241. if (eqi == EQI_COSTUME_MID && equip_index[EQI_COSTUME_LOW] == index)
  10242. return true;
  10243. // Headgear with Top & Mid or Low location
  10244. if (eqi == EQI_COSTUME_TOP && (equip_index[EQI_COSTUME_MID] == index || equip_index[EQI_COSTUME_LOW] == index))
  10245. return true;
  10246. return false;
  10247. }
  10248. /**
  10249. * Generate Unique item ID for player
  10250. * @param sd : Player
  10251. * @return A generated Unique item ID
  10252. */
  10253. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  10254. nullpo_ret(sd);
  10255. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  10256. }
  10257. /**
  10258. * Validating skill from player after logged on
  10259. * @param sd
  10260. **/
  10261. void pc_validate_skill(struct map_session_data *sd) {
  10262. if (sd) {
  10263. uint16 i = 0, count = 0;
  10264. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  10265. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  10266. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  10267. for (i = 0; i < MAX_SKILL; i++) {
  10268. uint16 idx = 0;
  10269. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  10270. continue;
  10271. if ((idx = skill_get_index(tmp_skills[i].id))) {
  10272. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  10273. count++;
  10274. }
  10275. else
  10276. 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);
  10277. }
  10278. }
  10279. }
  10280. /*==========================================
  10281. * pc Init/Terminate
  10282. *------------------------------------------*/
  10283. void do_final_pc(void) {
  10284. db_destroy(itemcd_db);
  10285. do_final_pc_groups();
  10286. ers_destroy(pc_sc_display_ers);
  10287. ers_destroy(pc_itemgrouphealrate_ers);
  10288. }
  10289. void do_init_pc(void) {
  10290. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  10291. pc_readdb();
  10292. pc_read_motd(); // Read MOTD [Valaris]
  10293. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  10294. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  10295. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  10296. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  10297. add_timer_func_list(pc_autosave, "pc_autosave");
  10298. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  10299. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  10300. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  10301. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  10302. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  10303. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  10304. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  10305. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  10306. // 0=day, 1=night [Yor]
  10307. night_flag = battle_config.night_at_start ? 1 : 0;
  10308. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  10309. int day_duration = battle_config.day_duration;
  10310. int night_duration = battle_config.night_duration;
  10311. // add night/day timer [Yor]
  10312. add_timer_func_list(map_day_timer, "map_day_timer");
  10313. add_timer_func_list(map_night_timer, "map_night_timer");
  10314. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  10315. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  10316. }
  10317. do_init_pc_groups();
  10318. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.c:pc_sc_display_ers", ERS_OPT_NONE);
  10319. pc_itemgrouphealrate_ers = ers_new(sizeof(struct s_pc_itemgrouphealrate), "pc.c:pc_itemgrouphealrate_ers", ERS_OPT_NONE);
  10320. }